tenseleyflow/ndotfiles / 943f2fd

Browse files

server things, tailnet things

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
943f2fd3bb55f04e8ff6be17aa64f202cea587a2
Parents
60fa1ba
Tree
92ad3a1

3 changed files

StatusFile+-
A sketchybar/plugins/server-stats.sh 48 0
A sketchybar/plugins/ssh-quick-connect.sh 78 0
M sketchybar/sketchybarrc 34 6
sketchybar/plugins/server-stats.shadded
@@ -0,0 +1,48 @@
1
+
2
+
3
+#!/usr/bin/env zsh
4
+#
5
+# server-stats.sh <host>
6
+#   Polls <host> over SSH for one‑minute load average and memory usage,
7
+#   then updates the SketchyBar item named $NAME (set automatically).
8
+#
9
+# Example in sketchybarrc:
10
+#   sketchybar --add item srv_right right \
11
+#              --set srv_right update_freq=30 \
12
+#                           script="$PLUGDIR/server-stats.sh myserver" \
13
+#                           icon=$'\uf233'
14
+#
15
+########################################################################
16
+
17
+HOST="$1"
18
+ITEM_NAME="${NAME:-server_stats}"
19
+
20
+[[ -z "$HOST" ]] && sketchybar --set "$ITEM_NAME" label="No host" icon="󰜺" && exit 0
21
+
22
+# Use ControlMaster for speed if configured
23
+SSH="ssh -o ConnectTimeout=2 -o BatchMode=yes $HOST"
24
+
25
+LOAD=$($SSH "awk '{print \$1}' /proc/loadavg" 2>/dev/null)
26
+MEM=$($SSH "free -m | awk '/Mem:/ {printf \"%d\", (\$3*100)/\$2}'" 2>/dev/null)
27
+
28
+if [[ -z "$LOAD" || -z "$MEM" ]]; then
29
+  sketchybar --set "$ITEM_NAME" label="offline" icon="󱈸"
30
+  exit 0
31
+fi
32
+
33
+LABEL="${LOAD} / ${MEM}%"
34
+# ICON=$'\uf1c0'   # database icon
35
+
36
+# Colour shifts with load relative to 1.0
37
+COLOR_GREEN=0xff8AED9C
38
+COLOR_ORANGE=0xffffc300
39
+COLOR_RED=0xffff3c00
40
+if (( ${LOAD/.*} < 1 )); then COLOR=$COLOR_GREEN
41
+elif (( ${LOAD/.*} < 2 )); then COLOR=$COLOR_ORANGE
42
+else COLOR=$COLOR_RED
43
+fi
44
+
45
+sketchybar --set "$ITEM_NAME" \
46
+           icon.font="Font Awesome 6 Pro:Regular:18" \
47
+           icon.color="$COLOR" \
48
+           label="$LABEL"
sketchybar/plugins/ssh-quick-connect.shadded
@@ -0,0 +1,78 @@
1
+
2
+
3
+#!/usr/bin/env zsh
4
+#
5
+# ssh‑quick‑connect.sh
6
+# A SketchyBar plugin that shows the number of online Tailnet peers and,
7
+# on click, pops up a list of online hosts that you can SSH into.
8
+#
9
+# Requirements:
10
+#   * Tailscale CLI (`tailscale`) and `jq`
11
+#   * iTerm2 or Terminal for the click‑launch (adjust OPEN_CMD below)
12
+#
13
+# Add to sketchybarrc:
14
+#   sketchybar --add item ssh_menu right \
15
+#              --set ssh_menu update_freq=60 \
16
+#                           script="$PLUGDIR/ssh-quick-connect.sh" \
17
+#                           icon=$'\uf489' \
18
+#              --subscribe ssh_menu mouse.clicked
19
+#
20
+########################################################################
21
+
22
+ITEM_NAME="${NAME:-ssh_menu}"   # When run by SketchyBar, $NAME is set.
23
+SENDER="${SENDER:-routine}"     # "routine" when run via update_freq
24
+
25
+OPEN_CMD='osascript -e "tell application \"iTerm2\" to create window with default profile command \"%s\""'   # change to Terminal as desired
26
+
27
+# ---------- Helpers --------------------------------------------------
28
+
29
+json_status() {
30
+  tailscale status --json 2>/dev/null
31
+}
32
+
33
+online_hosts() {
34
+  jq -r '.Peer[] | select(.Online==true) | .HostName' <<<"$1"
35
+}
36
+
37
+# ---------- Update routine (every 60 s) ------------------------------
38
+
39
+if [[ "$SENDER" == "routine" ]]; then
40
+  TS_JSON=$(json_status)
41
+  if [[ -z "$TS_JSON" ]]; then
42
+    sketchybar --set "$ITEM_NAME" icon="󰼀" label="TS Down"
43
+    exit 0
44
+  fi
45
+
46
+  ONLINE=$(jq '[.Peer[] | select(.Online==true)] | length' <<<"$TS_JSON")
47
+  TOTAL=$(jq '.Peer | length' <<<"$TS_JSON")
48
+
49
+  ICON="󰼂"   # default t/s icon
50
+  LABEL="$ONLINE/$TOTAL"
51
+
52
+  sketchybar --set "$ITEM_NAME" icon="$ICON" label="$LABEL"
53
+  # Toggle popup visibility so the newly‑created list appears
54
+  sketchybar --set "$ITEM_NAME" popup.drawing=toggle
55
+  exit 0
56
+fi
57
+
58
+# ---------- Click handler -------------------------------------------
59
+
60
+if [[ "$SENDER" == "mouse.clicked" ]]; then
61
+  # Clear any previous popup items
62
+  sketchybar --remove popup."$ITEM_NAME" 2>/dev/null
63
+
64
+  TS_JSON=$(json_status)
65
+  HOSTS=($(online_hosts "$TS_JSON"))
66
+  (( ${#HOSTS} == 0 )) && exit 0
67
+
68
+  # Build popup items dynamically
69
+  INDEX=0
70
+  for HOST in "${HOSTS[@]}"; do
71
+    POP="ssh_pop_${INDEX}"
72
+    sketchybar --add item "$POP" popup."$ITEM_NAME" \
73
+               --set "$POP" label="$HOST" icon="" \
74
+                    click_script=${(qq)${(e)${(printf "$OPEN_CMD" "ssh $HOST")}}}
75
+    (( INDEX++ ))
76
+  done
77
+  exit 0
78
+fi
sketchybar/sketchybarrcmodified
@@ -121,6 +121,14 @@ sketchybar --add item batt right \
121121
         icon.padding_right=5 \
122122
         label.padding_right=10
123123
 
124
+# wifi status
125
+sketchybar --add item wifi right \
126
+           --set wifi \
127
+                update_freq=5 \
128
+                script="$PLUGDIR/wifi.sh" \
129
+                icon.padding_right=5 \
130
+                label.padding_right=10
131
+
124132
 # cpu usage
125133
 sketchybar --add item cpu right \
126134
   --set cpu \
@@ -142,10 +150,30 @@ sketchybar --add item mem right \
142150
     label.padding_right=10 \
143151
     script="$PLUGDIR/mem.sh" \
144152
 
145
-sketchybar --add item wifi right \
146
-           --set wifi \
147
-                update_freq=5 \
148
-                script="$PLUGDIR/wifi.sh" \
153
+# separator
154
+sketchybar --add item filler2 right \
155
+    --set filler2 \
156
+        label.padding_right=15 \
157
+        label="||" \
158
+
159
+# tailscale popup menu
160
+sketchybar --add item ssh_menu right \
161
+    --set ssh_menu \
162
+        update_freq=60 \
163
+        script="$PLUGDIR/ssh-quick-connect.sh" \
164
+        icon=$'\uf489' \
165
+        icon.padding_right=5 \
166
+        label.padding_right=10 \
167
+    --subscribe ssh_menu mouse.clicked
168
+
169
+# server statistics
170
+# (load average and memory usage)
171
+# over SSH
172
+sketchybar --add item srv_right right \
173
+    --set srv_right \
174
+        update_freq=30 \
175
+        script="$PLUGDIR/server-stats.sh StarchNet" \
176
+        icon=$'\uf1c0' \
149177
         icon.padding_right=5 \
150178
         label.padding_right=10
151179