Bash · 2061 bytes Raw Blame History
1 #!/usr/bin/env bash
2 # Simple working version that avoids complex quoting
3 set -euo pipefail
4
5 HOST="espadon@almanta"
6 SSH_OPTS=(-T -o BatchMode=yes -o ConnectTimeout=3 -o ConnectionAttempts=1)
7
8 # Test connectivity
9 if ! ssh "${SSH_OPTS[@]}" "$HOST" "echo 'test'" >/dev/null 2>&1; then
10 echo '{"text":"⚬ almanta: down","tooltip":"SSH connection failed","class":"down"}'
11 exit 0
12 fi
13
14 # Get metrics with very simple commands
15 get_load() {
16 ssh "${SSH_OPTS[@]}" "$HOST" "cut -d' ' -f1 /proc/loadavg" 2>/dev/null || echo "?"
17 }
18
19 get_disk() {
20 ssh "${SSH_OPTS[@]}" "$HOST" "df / | tail -1 | awk '{print \$5}' | tr -d '%'" 2>/dev/null || echo "?"
21 }
22
23 get_memory() {
24 ssh "${SSH_OPTS[@]}" "$HOST" "free | grep '^Mem:' | awk '{printf \"%.0f\", (\$3/\$2)*100}'" 2>/dev/null || echo "?"
25 }
26
27 get_service() {
28 # Check for web services
29 for service in nginx httpd apache2; do
30 if ssh "${SSH_OPTS[@]}" "$HOST" "systemctl is-active --quiet $service" 2>/dev/null; then
31 echo "$service|active"
32 return
33 elif ssh "${SSH_OPTS[@]}" "$HOST" "systemctl list-unit-files | grep -q '^$service\.service'" 2>/dev/null; then
34 echo "$service|inactive"
35 return
36 fi
37 done
38 echo "web|unknown"
39 }
40
41 get_uptime() {
42 ssh "${SSH_OPTS[@]}" "$HOST" "uptime -p" 2>/dev/null || echo ""
43 }
44
45 # Gather all metrics
46 load=$(get_load)
47 disk=$(get_disk)
48 mem=$(get_memory)
49 up=$(get_uptime)
50
51 # Get service info
52 service_info=$(get_service)
53 IFS='|' read -r svc web <<<"$service_info"
54
55 # Build output - escape newlines properly for JSON
56 text="⚬ ${svc:-web} L:$load M:$mem% D:$disk%"
57 tip="almanta (${svc:-}): ${web:-unknown}\\nload(1m): $load\\nmemory: $mem%\\ndisk /: $disk%\\n$up"
58
59 # Determine class
60 class="ok"
61 [ "${web:-unknown}" != "active" ] && class="warn"
62 [[ "$mem" =~ ^[0-9]+$ ]] && [ "$mem" -ge 90 ] && class="warn"
63 [[ "$disk" =~ ^[0-9]+$ ]] && [ "$disk" -ge 90 ] && class="warn"
64
65 # Use jq for proper JSON encoding to handle all special characters
66 jq -cn --arg text "$text" --arg tooltip "$tip" --arg class "$class" \
67 '{text:$text, tooltip:$tooltip, class:$class}'