forked from roger-/hass-sysmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysmon.sh
572 lines (439 loc) · 15.9 KB
/
sysmon.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/bin/sh
# sysmon.sh v0.32
# Home Assistant configuration defaults (can be overwritten in .config file)
HASS_TOKEN=""
HASS_SERVER="homeassistant.local"
HASS_PORT="8123"
HASS_MQTT_PREFIX="homeassistant"
# MQTT path to publish states
MQTT_PUB_PATH="/home/servers"
MQTT_PUBLISH_PERIOD=20
# Define sensor prefix. Options: 0 = none (default and if not 1 or string), 1 = hostname, custom if string
SENSOR_PREFIX_OPTION=1
# Choos wether the unique machine ID should be at the end or beginning of the device name. Options: 0 = beginning (default), 1 = end.
POSITION_UNIQUE_ID=1
# name of network interface (e.g. eth0) otherwise first active one will be used
NETWORK_IFACE=""
# debug levels: 0 = DEBUG, 1 = INFO, 2 = WARN, 3 = ERROR
DEBUG_LEVEL=0
# how long to wait for the Home Assistant server to respond (or to sleep if wait not supported)
TIMEOUT_SERVER=1
# sensor config
ENABLE_DISK=1
ENABLE_LOAD=1
ENABLE_MEMORY=1
ENABLE_SWAP=1
ENABLE_WIFI=1
ENABLE_UPTIME=1
ENABLE_TEMPERATURE=1
ENABLE_PING=1
ENABLE_TOP_CPU=1
CONFIG_PING_HOST="192.168.0.1"
CONFIG_TOP_CPU_IGNORE_COMMAND="top"
CONFIG_TOP_CPU_MAX=3
##### Logging functions #####
COLOR_RESET="\033[0m"
COLOR_RED="\033[31;1m"
COLOR_GREEN="\033[32;1m"
COLOR_BLUE="\033[33;1m"
COLOR_GRAY="\033[37;1m"
error() {
if [ $DEBUG_LEVEL -le 3 ]; then
printf '%b' "${COLOR_RED}ERROR:${COLOR_RESET} $@\n"
fi
}
warning() {
if [ $DEBUG_LEVEL -le 2 ]; then
printf '%b' "${COLOR_BLUE}WARNING:${COLOR_RESET} $@\n"
fi
}
info() {
if [ $DEBUG_LEVEL -le 1 ]; then
printf '%b' "${COLOR_GREEN}INFO:${COLOR_RESET} $@\n"
fi
}
debug() {
if [ $DEBUG_LEVEL -le 0 ]; then
printf '%b' "${COLOR_GRAY}DEBUG:${COLOR_RESET} $@\n"
fi
}
##### Helper functions #####
cmd_exists() {
command -v "$1" 2>&1 >/dev/null
}
beginswith() {
case "$2" in "$1"*) true;; *) false;; esac;
}
print_key_vals() {
echo -n "\"$1\":$2"
shift 2
while [ $# -ge 2 ]; do
echo -n ",\"$1\":$2"
shift 2
done
}
print_json() {
echo -n "{$(print_key_vals "$@")}"
}
deref_path_symlink() {
echo $(cd "$*" && pwd -P)
}
macid() {
nic_name=$1
cat /sys/class/net/$nic_name/address | head -n 1 | sed 's/://g'
}
active_nic() {
for nic_path in /sys/class/net/*; do
nic_name=$(basename "$nic_path")
is_virtual=$(deref_path_symlink "$nic_path" | grep virtual)
# skip possibly virtual or inactive devices
if [ -z "$is_virtual" ] && [ "$(cat $nic_path/operstate)" = "up" ]; then
echo "$nic_name"
return 0
fi
done
return 1
}
temperature_sensor_names() {
if ! ls /sys/class/thermal/thermal_zone* > /dev/null 2>&1; then
return
fi
for tz in /sys/class/thermal/thermal_zone*; do
cat "$tz"/type | sed 's/-/_/'
done
}
sensor_prefix_generator() {
if [ "$SENSOR_PREFIX_OPTION" = "0" ]; then
SENSOR_PREFIX=""
elif [ "$SENSOR_PREFIX_OPTION" = "1" ]; then
SENSOR_PREFIX=$(cat /proc/sys/kernel/hostname)
else
SENSOR_PREFIX=$SENSOR_PREFIX_OPTION
fi
SENSOR_PREFIX=$(echo $SENSOR_PREFIX | sed "s/[^a-zA-Z0-9']/_/g" | sed "s/\_\{1,\}/_/g" | sed "s/[A-Z]/\L&/g")
}
##### Sensor functions #####
CPU_COUNT=$(grep -c ^processor /proc/cpuinfo)
avg_load() {
loadavg=$(cat /proc/loadavg)
avg_load_1min_pc=$(echo $loadavg | awk '{printf 100 * $1 / '$CPU_COUNT'}')
avg_load_5min_pc=$(echo $loadavg | awk '{printf 100 * $2 / '$CPU_COUNT'}')
avg_load_10min_pc=$(echo $loadavg | awk '{printf 100 * $3 / '$CPU_COUNT'}')
print_key_vals \
avg_load_1min_pc $avg_load_1min_pc \
avg_load_5min_pc $avg_load_5min_pc \
avg_load_10min_pc $avg_load_10min_pc
}
top_cpu() {
field_ind_to_name="$(top -bn1 | grep -E '^ +PID' | sed 's/\s\s*/\n/g' |tail -n +2| grep -nx ".*")"
cpu_ind=$(echo "$field_ind_to_name" | grep "%CPU" | cut -d: -f1)
command_ind=$(echo "$field_ind_to_name" | grep "COMMAND" | cut -d: -f1)
# below assumes COMMAND is last column and PID is first
top_commands=$(top -bn1 | sed -n '/\s*PID.*$/,$p' | tail -n +2 | sed 's/\s\s*/ /g' | sed 's/^\s*//g' | cut -d' ' -f"${cpu_ind},${command_ind}-")
i=1
did_print=0
echo "$top_commands" | while read line; do
cpu_pc=$(echo $line | cut -d' ' -f1 | awk '{printf $1 / '$CPU_COUNT'}')
command_name=$(echo $line | cut -d' ' -f2-)
# ignore given command
if [ ! -z "$CONFIG_TOP_CPU_IGNORE_COMMAND" ] && (beginswith "$command_name" "$CONFIG_TOP_CPU_IGNORE_COMMAND"); then
continue
fi
if [ $did_print -eq 1 ]; then
echo -n ","
fi
did_print=1
print_key_vals \
"top_${i}_command_name" "\"$command_name\"" \
"top_${i}_command_cpu_pc" "$cpu_pc"
if [ $i -ge $CONFIG_TOP_CPU_MAX ]; then
return
fi
i=$((i+1))
done
}
uptime_duration() {
uptime_sec=$(awk '{print $1}' /proc/uptime)
print_key_vals uptime_sec $uptime_sec
}
disk_usage() {
root_usage=$(df | grep " /$")
disk_free_Kb=$(echo $root_usage | awk '{print $4}')
disk_used_pc=$(echo $root_usage | awk '{print 100 * $3 / $2}')
print_key_vals \
disk_free_kb $disk_free_Kb \
disk_used_pc $disk_used_pc
}
wifi_signal() {
signal=$(awk '/wlp/ || /wlan0/ { print $0; exit}' /proc/net/wireless)
wifi_link_pc=$(echo $signal | awk '{printf "%d", $3}')
wifi_level_dbm=$(echo $signal | awk '{printf "%d", $4}')
print_key_vals \
wifi_link_pc $wifi_link_pc \
wifi_level_dbm $wifi_level_dbm
}
temperature() {
if ! ls /sys/class/thermal/thermal_zone* > /dev/null 2>&1; then
return 1
fi
sep=""
for tz in /sys/class/thermal/thermal_zone*; do
sensor_name="temperature_$(cat $tz/type | sed 's/-/_/')_C"
temp=$(awk '{printf $1 / 1000}' $tz/temp)
echo -n "$sep"
sep=","
print_key_vals $sensor_name $temp
done
}
memory_usage() {
mem=$(cat /proc/meminfo)
mem_free_kB=$(echo "$mem" | grep MemAvailable | awk '{printf $2}')
mem_used_pc=$(echo "$mem" | grep MemTotal | awk '{printf 100 * ($2 - '$mem_free_kB') / $2}')
print_key_vals \
mem_free_kB $mem_free_kB \
mem_used_pc $mem_used_pc
}
swap_usage() {
mem=$(cat /proc/meminfo)
swap_total_kB=$(echo "$mem" | grep SwapTotal | awk '{printf $2}')
# check if no swap
if [ $swap_total_kB -eq 0 ]; then
return 1
fi
swap_free_kB=$(echo "$mem" | grep SwapFree | awk '{printf $2}')
swap_used_pc=$(echo | awk '{printf 100 * ('$swap_total_kB' - '$swap_free_kB') / '$swap_total_kB'}')
print_key_vals \
swap_free_kB $swap_free_kB \
swap_used_pc $swap_used_pc
}
host_name() {
print_key_vals host_name \"$(cat /proc/sys/kernel/hostname)\"
}
ping_host() {
result="$(ping $CONFIG_PING_HOST -q -c 1 -W 1)"
retval=$?
if ! [ $retval -eq 0 ]; then
info ping $CONFIG_PING_HOST failed: $(echo "$result" | tail -n 1)
return 1
fi
rtt=$(echo $result | tail -n 1 | cut -d= -f2 | cut -d\/ -f 1 | tr -d ' ')
print_key_vals ping_rtt_ms $rtt
}
##### Core network functions #####
HTTP_HEADER_CTYPE="Content-Type: application/json"
HTTP_PATH_SENSOR="/api/states/sensor"
HTTP_PATH_MQTT="/api/services/mqtt/publish"
post_json_message_curl() {
path="$1"
json="$2"
url="http://${HASS_SERVER}:${HASS_PORT}${path}"
response=$(curl -m $TIMEOUT_SERVER -s -X POST -w "\\n%{http_code}\\n" -H "$HTTP_HEADER_AUTH" -H "$HTTP_HEADER_CTYPE" -d "$json" "$url")
debug "curl return code: $?"
http_code=$(echo "$response" | tail -n 1)
if [ "$http_code" != "200" ]; then
debug "sent JSON: $json"
error "received HTTP code $http_code, full response:"
printf '%b' "$response"
else
debug "message receipt confirmed"
fi
}
post_json_message_netcat() {
path="$1"
json="$2"
data="POST $path HTTP/1.1\r
$HTTP_HEADER_AUTH\r
$HTTP_HEADER_CTYPE\r
Content-Length: ${#json}\r
\r
"
data="${data}${json}"
response=$(printf '%b' "$data" | eval "$NETCAT_CMD $HASS_SERVER $HASS_PORT" 2>&1)
sleep $TIMEOUT_SERVER
http_code=$(echo "$response" | awk '/^HTTP/{print $2; exit}')
if [ ! $http_code ]; then
info "message receipt not confirmed"
return
fi
if [ "$http_code" != "200" ]; then
debug "sent JSON: $json"
error "received HTTP code $http_code, full response:"
printf '%b' "$response"
else
debug "message receipt confirmed"
fi
}
##### Network helper functions #####
post_json_message() {
if [ $HAS_CURL -eq 1 ]; then
post_json_message_curl "$1" "$2"
else
post_json_message_netcat "$1" "$2"
fi
}
post_sensor_state() {
path="$HTTP_PATH_SENSOR.$1"
json="$2"
post_json_message "$path" "$json"
}
post_mqtt() {
topic="$1"
json_str=$(echo "$2" | sed 's/"/\\"/g')
msg=$(print_json topic \"$topic\" payload "\"$json_str\"" retain true)
post_json_message "$HTTP_PATH_MQTT" "$msg"
}
##### Auto-discovery and state publishing functions #####
publish_state_loop() {
info "publishing state to topic $STATE_TOPIC every $MQTT_PUBLISH_PERIOD s"
time_pub_next=$(date +"%s")
while true
do
time_now=$(date +"%s")
time_sleep=$(( $time_pub_next - $time_now ))
if [ $time_sleep -gt 0 ]; then
debug "sleeping for $time_sleep s"
sleep $time_sleep
fi
time_pub_next=$(( $time_pub_next + $MQTT_PUBLISH_PERIOD))
data="$(host_name)"
[ $ENABLE_MEMORY -eq 1 ] && val=$(memory_usage) && data="${data},$val"
[ $ENABLE_SWAP -eq 1 ] && val=$(swap_usage) && data="${data},$val"
[ $ENABLE_DISK -eq 1 ] && val=$(disk_usage) && data="${data},$val"
[ $ENABLE_LOAD -eq 1 ] && val=$(avg_load) && data="${data},$val"
[ $ENABLE_WIFI -eq 1 ] && val=$(wifi_signal) && data="${data},$val"
[ $ENABLE_UPTIME -eq 1 ] && val=$(uptime_duration) && data="${data},$val"
[ $ENABLE_PING -eq 1 ] && val=$(ping_host) && data="${data},$val"
[ $ENABLE_TEMPERATURE -eq 1 ] && val=$(temperature) && data="${data},$val"
[ $ENABLE_TOP_CPU -eq 1 ] && val=$(top_cpu) && data="${data},$val"
json="{${data}}"
debug "publishing state message"
post_mqtt "$STATE_TOPIC" "$json"
done
}
publish_discovery_sensor() {
param="$1"
name="$2"
unit_of_measurement="$3"
device_class="$4"
config_topic="$HASS_MQTT_PREFIX/sensor/$DEVICE_NAME/$param/config"
device_name="System statistics $DEVICE_NAME"
version="$(uname -a)"
device=$(print_json identifiers '["'"$DEVICE_NAME"'"]' name "\"$device_name\"" sw_version "\"$version\"")
msg=$(print_key_vals name "\"$name\"")
msg=$msg,$(print_key_vals state_topic \"$STATE_TOPIC\")
msg=$msg,$(print_key_vals json_attributes_topic \"$STATE_TOPIC\")
msg=$msg,$(print_key_vals expire_after \"$((5 * $MQTT_PUBLISH_PERIOD))\")
msg=$msg,$(print_key_vals value_template "\"{{ value_json.${param} }}\"")
msg=$msg,$(print_key_vals unique_id \"$DEVICE_NAME-$param\")
msg=$msg,$(print_key_vals device "$device")
[ $device_class ] && msg="$msg,$(print_key_vals device_class \"$device_class\")"
[ $unit_of_measurement ] && msg=$msg,$(print_key_vals unit_of_measurement \"$unit_of_measurement\")
msg={$msg}
debug "sending discovery message for state $param"
post_mqtt "$config_topic" "$msg"
}
publish_discovery_all() {
info "sending discovery messages"
[ $ENABLE_UPTIME -eq 1 ] && publish_discovery_sensor uptime_sec "${SENSOR_PREFIX} Uptime" "s" "duration"
[ $ENABLE_PING -eq 1 ] && publish_discovery_sensor ping_rtt_ms "${SENSOR_PREFIX} Ping RTT" "ms" "duration"
[ $ENABLE_LOAD -eq 1 ] && publish_discovery_sensor avg_load_1min_pc "${SENSOR_PREFIX} CPU load (1 min avg)" "%"
[ $ENABLE_LOAD -eq 1 ] && publish_discovery_sensor avg_load_5min_pc "${SENSOR_PREFIX} CPU load (5 min avg)" "%"
[ $ENABLE_LOAD -eq 1 ] && publish_discovery_sensor avg_load_10min_pc "${SENSOR_PREFIX} CPU load (10 min avg)" "%"
[ $ENABLE_MEMORY -eq 1 ] && publish_discovery_sensor mem_free_kB "${SENSOR_PREFIX} Memory free" "kB"
[ $ENABLE_MEMORY -eq 1 ] && publish_discovery_sensor mem_used_pc "${SENSOR_PREFIX} Memory used" "%"
[ $ENABLE_SWAP -eq 1 ] && publish_discovery_sensor swap_free_kB "${SENSOR_PREFIX} Swap free" "kB"
[ $ENABLE_SWAP -eq 1 ] && publish_discovery_sensor swap_used_pc "${SENSOR_PREFIX} Swap used" "%"
[ $ENABLE_WIFI -eq 1 ] && publish_discovery_sensor wifi_link_pc "${SENSOR_PREFIX} WiFi link" "%"
[ $ENABLE_WIFI -eq 1 ] && publish_discovery_sensor wifi_level_dbm "${SENSOR_PREFIX} WiFi level" "dBm" "signal_strength"
[ $ENABLE_DISK -eq 1 ] && publish_discovery_sensor disk_free_kb "${SENSOR_PREFIX} Disk free" "kB"
[ $ENABLE_DISK -eq 1 ] && publish_discovery_sensor disk_used_pc "${SENSOR_PREFIX} Disk used" "%"
if [ $ENABLE_TOP_CPU -eq 1 ]; then
i=1
while [ $i -le $CONFIG_TOP_CPU_MAX ]; do
publish_discovery_sensor top_${i}_command_cpu_pc "${SENSOR_PREFIX} Top $i command CPU load" "%"
publish_discovery_sensor top_${i}_command_name "${SENSOR_PREFIX} Top $i command name" ""
i=$(($i+1))
done
fi
if [ $ENABLE_TEMPERATURE -eq 1 ]; then
for sensor_name in $(temperature_sensor_names); do
var_name="temperature_${sensor_name}_C"
sensor_name=$(echo $sensor_name | sed 's/_temp$//' | sed 's/_/ /')
publish_discovery_sensor $var_name "${SENSOR_PREFIX} Temperature $sensor_name" "°C" "temperature"
done
fi
}
##### main #####
setup() {
config_file_name="$(basename $0 .sh).config"
config_path_full="${PATH_CONFIG}/${config_file_name}"
if [ -f "$config_path_full" ]; then
info "loading config from $config_path_full"
. "$config_path_full"
fi
# need to set this here in case token gets loaded from file
HTTP_HEADER_AUTH="Authorization: Bearer $HASS_TOKEN"
if cmd_exists curl; then
HAS_CURL=1
debug "using curl"
return 0
elif cmd_exists netcat; then
HAS_NETCAT=1
NETCAT_CMD="netcat"
debug "using netcat"
elif cmd_exists nc; then
HAS_NETCAT=1
NETCAT_CMD="nc"
debug "using nc"
else
error "missing curl and netcat/nc, please install one"
exit 1
fi
if ( $NETCAT_CMD 2>&1 | grep "\-w" > /dev/null ); then
debug "netcat supports delay parameter"
NETCAT_CMD="$NETCAT_CMD -w $TIMEOUT_SERVER"
TIMEOUT_SERVER=0
else
warning "netcat does not support delay parameter (-w), messages may not be reliably sent and confirmed"
HAS_NETCAT_BASIC=1
fi
}
start() {
HAS_CURL=0
HAS_NETCAT=0
HAS_NETCAT_BASIC=0
NETCAT_CMD="netcat"
# figure out MAC address on given interface, or first active one found
[ -z "$NETWORK_IFACE" ] && NETWORK_IFACE=$(active_nic)
MAC_ID="$(macid $NETWORK_IFACE)"
if [ -z "$MAC_ID" ]; then
error "couldn't determine MAC address, is interface up?"
exit 1
fi
if [ "$POSITION_UNIQUE_ID" = "1" ]; then
DEVICE_NAME="$(cat /proc/sys/kernel/hostname)-$MAC_ID"
else
DEVICE_NAME="$MAC_ID-$(cat /proc/sys/kernel/hostname)"
fi
STATE_TOPIC="$MQTT_PUB_PATH/$DEVICE_NAME/state"
info "using device name: $DEVICE_NAME"
sensor_prefix_generator
if ! [ -z "$SENSOR_PREFIX" ]; then
info "using sensor prefix: $SENSOR_PREFIX"
fi
setup
publish_discovery_all
# HACK: send discovery twice to improve chances with basic netcat
if [ $HAS_NETCAT_BASIC -eq 1 ]; then
info "discovery messages may not have been delivered, resending"
publish_discovery_all
fi
publish_state_loop
}
# determine where to load config -- first parameter or script directory
if [ ! -z "$1" ]; then
PATH_CONFIG="$1"
else
PATH_CONFIG="$(dirname $0)"
fi
start