From 43e499034fbf9fddc79b8ee9168634cb4edc98bd Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Mon, 6 Aug 2018 17:12:28 -0400 Subject: [PATCH] Split actuator into 2 - khatus_actuate_status_bar_to_xsetroot_name - khatus_actuate_alert_to_notify_send --- README.md | 96 +++++++++++-------- bin/khatus_actuate_alert_to_notify_send | 31 ++++++ ...khatus_actuate_status_bar_to_xsetroot_name | 8 ++ bin/khatus_actuator | 73 -------------- bin/khatus_controller | 2 +- 5 files changed, 98 insertions(+), 112 deletions(-) create mode 100755 bin/khatus_actuate_alert_to_notify_send create mode 100755 bin/khatus_actuate_status_bar_to_xsetroot_name delete mode 100755 bin/khatus_actuator diff --git a/README.md b/README.md index 7ab320b..ea6a29c 100644 --- a/README.md +++ b/README.md @@ -12,68 +12,88 @@ Design ------ ``` - parallel +----------+ +----------+ +----------+ - stateless | sensor_1 | | sensor_2 | ... | sensor_n | - collectors +----------+ +----------+ +----------+ - | | | - data data data - | | | - V V V - serial +-----------------------------------------+ - stateful | controller | - observer +-----------------------------------------+ - | - decisions - | - V - serial +-----------------------------------------+ - stateless | actuator | - executor +-----------------------------------------+ - | - system commands - | - V - ~~~~~~ - ~ OS ~ - ~~~~~~ +parallel +----------+ +----------+ +----------+ +stateless | sensor_1 | | sensor_2 | ... | sensor_n | +collectors +----------+ +----------+ +----------+ + | | | | + data data data data + | | | | + V V V V +serial +----------------------------------------------+ +stateful | controller | +observer +----------------------------------------------+ + | + decision messages +decision | +messages | +copied to | +any number | +of interested | +filter/actuator | +combinations | + | + V + +-------------+-+---------+---------+ + | | | | + V V V V +parallel +------------+ +------------+ +------------+ +stateless | filter_1 | | filter_2 | ... | filter_n | +filters +------------+ +------------+ +------------+ + | | | | + V V V V +parallel +------------+ +------------+ +------------+ +stateless | actuator_1 | | actuator_2 | ... | actuator_n | +executors +------------+ +------------+ +------------+ + | | | | + commands commands commands commands + | | | | + V V V V + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~~~~~~ operating system ~~~~~~~~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` ### Actuator -By default, actuator is left disconnected from the controller's output, so if +Actuator is anything that takes action upon controller messages. A few generic +ones are included: + +- `khatus_actuate_alert_to_notify_send` +- `khatus_actuate_status_bar_to_xsetroot_name` + +and, by default, are left disconnected from the controller's output, so if desired - it needs to be manually attached when starting `khatus`. For example, in my `.xinitrc` I have: ```sh $BIN/khatus \ -2> >($BIN/twrap.sh >> $HOME/var/log/khatus.log) \ -| $BIN/khatus_actuator \ - -v pid="$$" \ - -v display=":0" \ -2> >($BIN/twrap.sh >> $HOME/var/log/khatus-actuator.log) \ +2> >($BIN/twrap >> $HOME/var/log/khatus.log) \ +| tee \ + >($BIN/khatus_actuate_status_bar_to_xsetroot_name) \ + >(grep -v MpdNowPlaying | $BIN/khatus_actuate_alert_to_notify_send) \ +2> >($BIN/twrap >> $HOME/var/log/khatus-actuators.log) \ & ``` +(where `twrap` is a simple script which prefixes a timestamp to each line) -(`twrap.sh` is a simple script which prefixes a timestamp to each line) - -The idea is to later have multiple, (some more-general and some more-specific) -actuators which can be selected as needed, say for example: +The idea is to give maximum flexibility for what to do with the controller +output, say, for instance: ```sh $BIN/khatus \ | tee \ ->(awk '/^STATUS_BAR/ {sub("^" $1 " *", ""); system("xsetroot -name \" " $0 " \"")}') \ +... \ >(grep '^REPORT' | actuate_report_to_email) \ >(grep '^ALERT' | grep mpd | actuate_alert_to_email) \ >(grep '^ALERT' | grep IntrusionAttempt | actuate_intruder_to_iptables_drop) \ >(grep '^ALERT' | grep NewDevice | actuate_alert_to_notify_send) >(grep '^ALERT' | grep DiskError | actuate_call_mom) +... ``` - -... and/or any other such fun you might imagine. +... and so on, for any other such fun you might imagine. ### Errors Any errors encountered by any sensor are propagated as alerts by the controller, which are in turn actualized as desktop notifications by the -actuator: +`khatus_actuate_alert_to_notify_send` actuator: ![screenshot-self-error-propagation](screenshot-self-error-propagation.jpg) diff --git a/bin/khatus_actuate_alert_to_notify_send b/bin/khatus_actuate_alert_to_notify_send new file mode 100755 index 0000000..b1c695a --- /dev/null +++ b/bin/khatus_actuate_alert_to_notify_send @@ -0,0 +1,31 @@ +#! /usr/bin/awk -f + +BEGIN { + # Set the correct value as any other AWK variable: + # + # khatus_actuate_alert_to_notify_send -v display="$CORRECT_DISPLAY" + # + display = ":0" +} + +/^ALERT / { + src = $2 + priority = $3 + subject = $4 + sub("^" $1 " +" $2 " +" $3 " +" $4 " +", "") + body = $0 + + sep = body ? "\n" : "" + body = body sep "--" src + urgency = priority + sub("hi" , "critical", urgency) + sub("med", "normal" , urgency) + + cmd = \ + sprintf(\ + "DISPLAY=%s notify-send -u %s %s \" %s\"", + display, urgency, subject, body \ + ) + system(cmd) + next +} diff --git a/bin/khatus_actuate_status_bar_to_xsetroot_name b/bin/khatus_actuate_status_bar_to_xsetroot_name new file mode 100755 index 0000000..a7463e2 --- /dev/null +++ b/bin/khatus_actuate_status_bar_to_xsetroot_name @@ -0,0 +1,8 @@ +#! /usr/bin/awk -f + +/^STATUS_BAR / { + sub("^" $1 " +", "") + # TODO: Move padding back to controller, now that we no-longer use readline + system("xsetroot -name \" " $0 "\" ") + next +} diff --git a/bin/khatus_actuator b/bin/khatus_actuator deleted file mode 100755 index 2e5d003..0000000 --- a/bin/khatus_actuator +++ /dev/null @@ -1,73 +0,0 @@ -#! /usr/bin/awk -f - -BEGIN { - n_ignored = split(opt_ignore_alerts_from, ignored, "[ \t\n]+") - for (i = 1; i <= n_ignored; i++) { - ignored_alert_sources[ignored[i]] = 1 - } -} - -/^STATUS_BAR / { shift(); handle_status_bar(); next } -/^ALERT / { shift(); handle_alert() ; next } -/^ERROR / { shift(); handle_error() ; next } - -function handle_status_bar() { - system("xsetroot -name \" " $0 "\" ") -} - -function handle_alert( src, priority, subject, sep, body, urgency, dbg) { - src = $1 - priority = $2 - subject = $3 - shift() - shift() - shift() - body = $0 - sep = body ? "\n" : "" - body = body sep "--" src - - urgency = priority - sub("hi" , "critical", urgency) - sub("med", "normal" , urgency) - - dbg["priority"] = priority - dbg["urgency"] = urgency - dbg["subject"] = subject - dbg["body"] = body - debug("ALERT properties", dbg) - - if (src in ignored_alert_sources) { - debug("ALERT ignoring from " src, ignored_alert_sources) - } else { - debug("ALERT sending from " src, ignored_alert_sources) - notify_send(urgency, subject, body) - } -} - -function handle_error() { - notify_send("normal", "Khatus_Error", $0) -} - -function notify_send(urgency, subject, body, cmd) { - cmd = \ - sprintf(\ - "DISPLAY=%s notify-send -u %s %s \" %s\"", - display, urgency, subject, body \ - ) - system(cmd) -} - -function shift() { - sub("^" $1 " +", "") -} - -function debug(msg, data, sep, vals, key, payload) { - sep = "" - vals = "" - for (key in data) { - vals = sprintf("%s%s%s: \"%s\"", vals, sep, key, data[key]) - sep = ", " - } - payload = sprintf("[%s] [%s] [%s]", pid, msg, vals) - print payload > "/dev/stderr" -} diff --git a/bin/khatus_controller b/bin/khatus_controller index c56370e..de971b2 100755 --- a/bin/khatus_controller +++ b/bin/khatus_controller @@ -213,7 +213,7 @@ function alert_check_mpd( curr, prev, name, body) { " - " db_mpd_song["Album:"] \ " - " db_mpd_song["Title:"] } - alert_trigger_low("alert_check_mpd", "NowPlaying", body) + alert_trigger_low("alert_check_mpd", "MpdNowPlaying", body) } } -- 2.20.1