From: Siraaj Khandkar Date: Mon, 10 Sep 2018 23:09:14 +0000 (-0400) Subject: Do not rely on FS for msg parsing X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=81336934f5dcb1895c050d71805ec37e56c400ad;p=khatus.git Do not rely on FS for msg parsing - makes patterns easier to read (no more field addressing by number) - allows inclusion of the whole msg lib without conflicting with existing FS --- diff --git a/Makefile b/Makefile index e001dd0..111b73e 100644 --- a/Makefile +++ b/Makefile @@ -97,121 +97,116 @@ bin/khatus_bar: \ src/awk/exe/bar.awk \ src/awk/lib/cache.awk \ src/awk/lib/str.awk \ - src/awk/lib/msg_in.awk \ - src/awk/lib/msg_out.awk \ + src/awk/lib/msg.awk \ src/awk/lib/num.awk $(BUILD_AWK_EXE) bin/khatus_actuate_alert_to_notify_send: \ src/awk/exe/actuate_alert_to_notify_send.awk \ src/awk/lib/str.awk \ - src/awk/lib/msg_in.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_actuate_device_add_to_automount: \ src/awk/exe/actuate_device_add_to_automount.awk \ src/awk/lib/str.awk \ - src/awk/lib/msg_in.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_actuate_status_bar_to_xsetroot_name: \ src/awk/exe/actuate_status_bar_to_xsetroot_name.awk \ src/awk/lib/str.awk \ - src/awk/lib/msg_in.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_monitor_devices: \ src/awk/exe/monitor_devices.awk \ src/awk/lib/str.awk \ - src/awk/lib/msg_in.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_monitor_energy: \ src/awk/exe/monitor_energy.awk \ src/awk/lib/str.awk \ - src/awk/lib/msg_in.awk \ - src/awk/lib/msg_out.awk \ + src/awk/lib/msg.awk \ src/awk/lib/num.awk $(BUILD_AWK_EXE) bin/khatus_monitor_errors: \ src/awk/exe/monitor_errors.awk \ src/awk/lib/str.awk \ - src/awk/lib/msg_in.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_bluetoothctl_show: \ src/awk/exe/parse_bluetoothctl_show.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_df_pcent: \ src/awk/exe/parse_df_pcent.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_fan_file: \ src/awk/exe/parse_fan_file.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_free: \ src/awk/exe/parse_free.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_ip_addr: \ src/awk/exe/parse_ip_addr.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_iwconfig: \ src/awk/exe/parse_iwconfig.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_loadavg_file: \ src/awk/exe/parse_loadavg_file.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_metar_d_output: \ src/awk/exe/parse_metar_d_output.awk \ - src/awk/lib/msg_out.awk \ + src/awk/lib/msg.awk \ src/awk/lib/str.awk $(BUILD_AWK_EXE) bin/khatus_parse_mpd_status_currentsong: \ src/awk/exe/parse_mpd_status_currentsong.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_pactl_list_sinks: \ src/awk/exe/parse_pactl_list_sinks.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_ps: \ src/awk/exe/parse_ps.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_sys_block_stat: \ src/awk/exe/parse_sys_block_stat.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_udevadm_monitor_block: \ src/awk/exe/parse_udevadm_monitor_block.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) bin/khatus_parse_upower: \ src/awk/exe/parse_upower.awk \ - src/awk/lib/msg_out.awk + src/awk/lib/msg.awk $(BUILD_AWK_EXE) #----------------------------------------------------------------------------- diff --git a/src/awk/exe/actuate_alert_to_notify_send.awk b/src/awk/exe/actuate_alert_to_notify_send.awk index a49a723..3cfbca0 100644 --- a/src/awk/exe/actuate_alert_to_notify_send.awk +++ b/src/awk/exe/actuate_alert_to_notify_send.awk @@ -6,11 +6,12 @@ BEGIN { Display = Display ? Display : ":0" } +{ + delete msg + msg_parse(msg, $0) +} - -$3 == "alert" { - delete msg - msg_in_parse(msg, $0) +msg["type"] == "alert" { body = msg["body"] sep = body ? "\n" : "" body = body sep "--" msg["node"] ":" msg["module"] diff --git a/src/awk/exe/actuate_device_add_to_automount.awk b/src/awk/exe/actuate_device_add_to_automount.awk index 9510720..3683370 100644 --- a/src/awk/exe/actuate_device_add_to_automount.awk +++ b/src/awk/exe/actuate_device_add_to_automount.awk @@ -3,12 +3,17 @@ BEGIN { Execute_On_Mount = Execute_On_Mount ? Execute_On_Mount : "" } -$1 == Node && \ -$2 == "khatus_sensor_devices" && \ -$3 == "data" && \ -$4 == "add" && \ -$5 ~ /[0-9]$/ { - mount_device($5) +{ + delete msg + msg_parse(msg, $0) +} + +msg["node"] == Node && \ +msg["module"] == "khatus_sensor_devices" && \ +msg["type"] == "data" && \ +msg["key"] == "add" && \ +msg["val"] ~ /[0-9]$/ { + mount_device(msg["val"]) } function mount_device(path, cmd, line, lines, line_count, status, i, diff --git a/src/awk/exe/actuate_status_bar_to_xsetroot_name.awk b/src/awk/exe/actuate_status_bar_to_xsetroot_name.awk index 6f87344..3828771 100644 --- a/src/awk/exe/actuate_status_bar_to_xsetroot_name.awk +++ b/src/awk/exe/actuate_status_bar_to_xsetroot_name.awk @@ -1,8 +1,11 @@ -$1 == Node && \ -$2 == "khatus_bar" && \ -$3 == "status_bar" { - delete msg - msg_in_parse(msg, $0) +{ + delete msg + msg_parse(msg, $0) +} + +msg["node"] == Node && \ +msg["module"] == "khatus_bar" && \ +msg["type"] == "status_bar" { system("xsetroot -name \"" msg["status_bar"] "\"") next } diff --git a/src/awk/exe/bar.awk b/src/awk/exe/bar.awk index 43126ff..b902448 100644 --- a/src/awk/exe/bar.awk +++ b/src/awk/exe/bar.awk @@ -10,15 +10,18 @@ # ----------------------------------------------------------------------------- # Input # ----------------------------------------------------------------------------- -$3 == "data" { - delete msg - msg_in_parse(msg, $0) +{ + delete msg + msg_parse(msg, $0) +} + +msg["type"] == "data" { cache_update(msg["node"], msg["module"], msg["key"], msg["val"]) } -$1 == Node && \ -$2 == "khatus_sensor_datetime" && \ -$3 == "data" { +msg["node"] == Node && \ +msg["module"] == "khatus_sensor_datetime" && \ +msg["type"] == "data" { # Code for bar_make_status is expected to be passed as an # additional source file, using -f flag. msg_out_status_bar(bar_make_status()) diff --git a/src/awk/exe/monitor_devices.awk b/src/awk/exe/monitor_devices.awk index efb7b79..9097a9b 100644 --- a/src/awk/exe/monitor_devices.awk +++ b/src/awk/exe/monitor_devices.awk @@ -1,8 +1,11 @@ -$2 == "khatus_sensor_devices" && \ -$3 == "data" \ { - delete msg - msg_in_parse(msg, $0) + delete msg + msg_parse(msg, $0) +} + +msg["module"] == "khatus_sensor_devices" && \ +msg["type"] == "data" \ +{ msg_out_alert_low( \ "BlockDeviceEvent", msg["key"] " " msg["val"] " on " msg["node"]\ diff --git a/src/awk/exe/monitor_energy.awk b/src/awk/exe/monitor_energy.awk index 518d687..5b18c74 100644 --- a/src/awk/exe/monitor_energy.awk +++ b/src/awk/exe/monitor_energy.awk @@ -8,31 +8,36 @@ BEGIN { bat_alert_spec[5] = "hi|Energy_CRITICALLY_Low|CHARGE NOW!!! GO GO GO!!!" } -$1 == Node && \ -$2 == "khatus_sensor_energy" && \ -$3 == "data" && \ -$4 == "line_power" { +{ + delete msg + msg_parse(msg, $0) +} + +msg["node"] == Node && \ +msg["module"] == "khatus_sensor_energy" && \ +msg["type"] == "data" && \ +msg["key"] == "line_power" { line_power_prev = line_power_curr - line_power_curr = $5 + line_power_curr = msg["val"] if (line_power_curr == "no" && line_power_prev != "no") { msg_out_alert_low("PowerUnplugged", "") } } -$1 == Node && \ -$2 == "khatus_sensor_energy" && \ -$3 == "data" && \ -$4 == "battery_state" { +msg["node"] == Node && \ +msg["module"] == "khatus_sensor_energy" && \ +msg["type"] == "data" && \ +msg["key"] == "battery_state" { battery_state_prev = battery_state_curr - battery_state_curr = $5 + battery_state_curr = msg["val"] } -$1 == Node && \ -$2 == "khatus_sensor_energy" && \ -$3 == "data" && \ -$4 == "battery_percentage" { +msg["node"] == Node && \ +msg["module"] == "khatus_sensor_energy" && \ +msg["type"] == "data" && \ +msg["key"] == "battery_percentage" { # TODO: Re-think the spec - can't rely on order of keys - battery_percentage = num_ensure_numeric($5) + battery_percentage = num_ensure_numeric(msg["val"]) if (battery_state_curr == "discharging") { for (threshold in bat_alert_spec) { threshold = num_ensure_numeric(threshold) diff --git a/src/awk/exe/monitor_errors.awk b/src/awk/exe/monitor_errors.awk index 36fd534..ca9f6e6 100644 --- a/src/awk/exe/monitor_errors.awk +++ b/src/awk/exe/monitor_errors.awk @@ -1,6 +1,9 @@ -$3 == "error" { - delete msg - msg_in_parse(msg, $0) +{ + delete msg + msg_parse(msg, $0) +} + +msg["type"] == "error" { subject = "ERROR_IN_" msg["node"] ":" msg["module"] msg_out_alert_hi(subject, msg["line"]) } diff --git a/src/awk/lib/msg_out.awk b/src/awk/lib/msg.awk similarity index 61% rename from src/awk/lib/msg_out.awk rename to src/awk/lib/msg.awk index 24393c6..fba25e7 100644 --- a/src/awk/lib/msg_out.awk +++ b/src/awk/lib/msg.awk @@ -6,6 +6,39 @@ BEGIN { Kfs = FS2 } +function msg_parse(msg, line, status, fields, type) { + split(line, fields, FS1) + msg["node"] = fields[1] + msg["module"] = fields[2] + type = fields[3] + msg["type"] = type + + status = 1 + if (type == "data") { + msg["key"] = fields[4] + msg["val"] = str_tail(str_join(fields, 1, 4, FS1) FS1, line) + } else if (type == "error") { + msg["line"] = str_tail(str_join(fields, 1, 3, FS1) FS1, line) + } else if (type == "alert") { + msg["priority"] = fields[4] + msg["subject"] = fields[5] + msg["body"] = str_tail(str_join(fields, 1, 5, FS1) FS1, line) + } else if (type == "log") { + msg["location"] = fields[4] + msg["level"] = fields[5] + msg["msg"] = str_tail(str_join(fields, 1, 5, FS1) FS1, line) + } else if (type == "status_bar") { + msg["status_bar"] = str_tail(str_join(fields, 1, 3, FS1) FS1, line) + } else { + msg_out_log_error(\ + "msg_parse", + "Unexpected msg type: " type " in given input line: " line \ + ) + status = 0 + } + return status +} + # ----------------------------------------------------------------------------- # alert # ----------------------------------------------------------------------------- diff --git a/src/awk/lib/msg_in.awk b/src/awk/lib/msg_in.awk deleted file mode 100644 index cda89e8..0000000 --- a/src/awk/lib/msg_in.awk +++ /dev/null @@ -1,38 +0,0 @@ -BEGIN { - FS1 = "|" # Fiels separator, level 1 (record to fields) - FS2 = ":" # Fiels separator, level 2 (field to subfields) - - FS = FS1 - Kfs = FS2 -} - -function msg_in_parse(msg, line, fields, type) { - split(line, fields, FS1) - msg["node"] = fields[1] - msg["module"] = fields[2] - type = fields[3] - msg["type"] = type - - if (type == "data") { - msg["key"] = fields[4] - msg["val"] = str_tail(str_join(fields, 1, 4, FS1) FS1, line) - } else if (type == "error") { - msg["line"] = str_tail(str_join(fields, 1, 3, FS1) FS1, line) - } else if (type == "alert") { - msg["priority"] = fields[4] - msg["subject"] = fields[5] - msg["body"] = str_tail(str_join(fields, 1, 5, FS1) FS1, line) - } else if (type == "log") { - msg["location"] = fields[4] - msg["level"] = fields[5] - msg["msg"] = str_tail(str_join(fields, 1, 5, FS1) FS1, line) - } else if (type == "status_bar") { - msg["status_bar"] = str_tail(str_join(fields, 1, 3, FS1) FS1, line) - } else { - msg_out_log_error(\ - "msg_in_parse", - "Unexpected msg type: " type " in given input line: " line \ - ) - exit 1 - } -}