From: Siraaj Khandkar Date: Mon, 20 Aug 2018 19:11:53 +0000 (-0400) Subject: Add block device event sensor and monitor X-Git-Url: https://git.xandkar.net/?p=khatus.git;a=commitdiff_plain;h=cf7cff1c7647112c30989405661d0b576a13ed62 Add block device event sensor and monitor --- diff --git a/bin/khatus b/bin/khatus index 5fa635d..95bd517 100755 --- a/bin/khatus +++ b/bin/khatus @@ -187,6 +187,7 @@ main() { cmd_sens_memory="khatus_sensor_memory $bin" fork_watcher "$pipe" "$bin" "khatus_sensor_energy $bin" + fork_watcher "$pipe" "$bin" "khatus_sensor_devices $bin" fork_poller "${opts['--interval_datetime']}" "$perf" "$pipe" "$bin" khatus_sensor_datetime fork_poller "${opts['--interval_brightness']}" "$perf" "$pipe" "$bin" "$cmd_sens_screen_brightness" fork_poller "${opts['--interval_weather']}" "$perf" "$pipe" "$bin" "$cmd_sens_weather" diff --git a/bin/khatus_monitor_devices b/bin/khatus_monitor_devices new file mode 100755 index 0000000..915933c --- /dev/null +++ b/bin/khatus_monitor_devices @@ -0,0 +1,20 @@ +#! /usr/bin/awk -f + +BEGIN { + FS = msg_fs ? msg_fs : "|" + OFS = msg_fs ? msg_fs : "|" + Kfs = key_fs ? key_fs : ":" +} + +$1 == "OK" && \ +$2 == "khatus_sensor_devices" \ +{ + alert("low", "BlockDeviceEvent", $3 " " $4) +} + +function alert(priority, subject, body) { + # priority : "low" | "med" | "hi" + # subject : no spaces + # body : anything + print("OK", "khatus_monitor_devices", "alert", priority, subject, body) +} diff --git a/bin/khatus_parse_udevadm_monitor_block b/bin/khatus_parse_udevadm_monitor_block new file mode 100755 index 0000000..75c00c3 --- /dev/null +++ b/bin/khatus_parse_udevadm_monitor_block @@ -0,0 +1,43 @@ +#! /usr/bin/awk -f + +BEGIN { + OFS = msg_fs ? msg_fs : "|" + Kfs = key_fs ? key_fs : ":" + + Re_Begin = "^UDEV + \[ *[0-9]+\.[0-9]+\] +" +} + +($0 ~ Re_Begin) { + handle_event($0) + next +} + +function handle_event(payload, payload_parts, event, + path, n_path_parts, + devname \ +) { + sub(Re_Begin, "", payload) + split(payload, payload_parts, " +") + event = payload_parts[1] + path = payload_parts[2] + if (event == "add" || event == "change") { + devname = devname_lookup(path) + } else { + n_path_parts = split(path, path_parts, "/") + devname = path_parts[n_path_parts] + } + print(event, devname) +} + +function devname_lookup(path, cmd, line_parts, devname) { + cmd = "udevadm info --path=" path + while (cmd | getline line) { + if (line ~ /^E: +DEVNAME/) { + split(line, line_parts, "=") + devname = line_parts[2] + break + } + } + close(cmd) + return devname +} diff --git a/bin/khatus_sensor_devices b/bin/khatus_sensor_devices new file mode 100755 index 0000000..186fc44 --- /dev/null +++ b/bin/khatus_sensor_devices @@ -0,0 +1,10 @@ +#! /bin/bash + +set -e + +dir_bin="$1" + +stdbuf -o L -- \ + udevadm monitor --udev -s block \ +| stdbuf -o L -- \ + "$dir_bin"/khatus_parse_udevadm_monitor_block diff --git a/sanity_check b/sanity_check index 7b525c2..b8dec76 100755 --- a/sanity_check +++ b/sanity_check @@ -11,4 +11,5 @@ ) \ >(stdbuf -o L ./bin/khatus_monitor_energy) \ >(stdbuf -o L ./bin/khatus_monitor_errors) \ + >(stdbuf -o L ./bin/khatus_monitor_devices) \ > /dev/null