Add block device event sensor and monitor
authorSiraaj Khandkar <siraaj@khandkar.net>
Mon, 20 Aug 2018 19:11:53 +0000 (15:11 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Mon, 20 Aug 2018 19:11:53 +0000 (15:11 -0400)
bin/khatus
bin/khatus_monitor_devices [new file with mode: 0755]
bin/khatus_parse_udevadm_monitor_block [new file with mode: 0755]
bin/khatus_sensor_devices [new file with mode: 0755]
sanity_check

index 5fa635d..95bd517 100755 (executable)
@@ -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 (executable)
index 0000000..915933c
--- /dev/null
@@ -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 (executable)
index 0000000..75c00c3
--- /dev/null
@@ -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 (executable)
index 0000000..186fc44
--- /dev/null
@@ -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
index 7b525c2..b8dec76 100755 (executable)
@@ -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
This page took 0.027803 seconds and 4 git commands to generate.