Bash · 1433 bytes Raw Blame History
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"