Sort and de-dup motd servers
[khome.git] / home / lib / login_functions.sh
index 29b00ee..df255d7 100644 (file)
@@ -267,9 +267,10 @@ gh_fetch_repos() {
 }
 
 gh_clone() {
-    gh_user_type="$1"
-    gh_user_name="$2"
-    gh_dir="${DIR_GITHUB}/${gh_user_name}"
+    local -r gh_user_type="$1"
+    local -r gh_user_name="$2"
+
+    local -r gh_dir="${DIR_GITHUB}/${gh_user_name}"
     mkdir -p "$gh_dir"
     cd "$gh_dir" || kill -INT $$
     gh_fetch_repos "$gh_user_type" "$gh_user_name" \
@@ -317,7 +318,7 @@ EOF
 
 work_log() {
     mkdir -p "$DIR_WORK_LOG"
-    file_work_log_today="${DIR_WORK_LOG}/$(date +%F).md"
+    local -r file_work_log_today="${DIR_WORK_LOG}/$(date +%F).md"
     if [ ! -f "$file_work_log_today" ]
     then
         work_log_template > "$file_work_log_today"
@@ -353,10 +354,12 @@ bt_devs() {
 }
 
 run() {
-    stderr="$(mktemp)"
+    local -r stderr="$(mktemp)"
+
+    local code urgency
+
     $@ 2> >(tee "$stderr")
     code="$?"
-    urgency=''
     case "$code" in
         0) urgency='normal';;
         *) urgency='critical'
@@ -377,8 +380,8 @@ bar_gauge() {
 
             printf "["
             for (i=1; i<=width; i++) {
-                c = i <= u ? "|" : " "
-                printf "%c", c
+                c = i <= u ? "|" : "-"
+                printf "%s", c
             }
             printf "]\n"
         }
@@ -389,22 +392,26 @@ bar_gauge() {
     '
 }
 
-motd() {
-    uname -srvmo
-    hostname | figlet
-    uptime
+motd_mem() {
+    local -r bar_width="$1"
 
-    echo
-
-    printf 'mem  '
+    printf 'mem '
     free \
     | awk '$1 == "Mem:" {total=$2; used=$3; print used, total}' \
-    | bar_gauge 73
+    | bar_gauge "$bar_width"
+}
+
+motd_disk() {
+    local -r bar_width="$1"
 
     printf 'disk '
     df ~ \
     | awk 'NR == 2 {used=$3; avail=$4; total=used+avail; print used, total}' \
-    | bar_gauge 73
+    | bar_gauge "$bar_width"
+}
+
+motd_batt() {
+    local -r bar_width="$1"
 
     case "$(uname)" in
         'Linux')
@@ -433,12 +440,42 @@ motd() {
                     delete device
                 }
             ' \
-            | bar_gauge 73
+            | bar_gauge "$bar_width"
         ;;
     esac
+}
+
+indent() {
+    awk -v unit="$1" '{printf "%s%s\n", unit, $0}'
+}
+
+motd() {
+    local -r bar_width='60'
+    local -r indent_unit='    '
+
+    uname -srvmo
+    hostname | figlet
+    uptime
 
     echo
 
+    printf 'tmux sessions: %d\n' "$(tmux ls 2> /dev/null | wc -l)"
+
+    echo
+
+    echo 'Resources'
+    (
+        motd_mem "$bar_width"
+        motd_disk "$bar_width"
+        motd_batt "$bar_width"
+    ) \
+    | column -t \
+    | indent "$indent_unit"
+
+    echo
+
+    echo 'Network'
+    echo "${indent_unit}interfaces:"
     (ifconfig; iwconfig) 2> /dev/null \
     | awk '
         /^[^ ]/ {
@@ -472,16 +509,22 @@ motd() {
                 if (device != "lo") {
                     l = link[device]
                     e = essid[device]
-                    l = l ? l : "--"
+                    l = l ? sprintf("%.0f%%", l) : "--"
                     e = e ? e : "--"
                     print device, address[device], e, l
                 }
         }
         ' \
-    | column -t
+    | column -t \
+    | indent "${indent_unit}${indent_unit}"
 
-    #echo
-    # TODO: netstat summary
+    echo "${indent_unit}servers:"
     # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
-    #sudo -n netstat -tulpn | awk '/^udp/ && !first++ {printf "\n"} 1'
+    sudo netstat -tlnp \
+    | awk 'NR > 2 {print $7}' \
+    | awk -F/ '{print $2}' \
+    | sort -u \
+    | xargs \
+    | column -t \
+    | indent "${indent_unit}${indent_unit}"
 }
This page took 0.035791 seconds and 4 git commands to generate.