From: Siraaj Khandkar Date: Fri, 3 Aug 2018 19:07:50 +0000 (-0400) Subject: Propagate sensor errors to controller and trigger alerts X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=688fb20edeb25d9f2da22cf529d97c2db2912329;p=khatus.git Propagate sensor errors to controller and trigger alerts --- diff --git a/bin/khatus b/bin/khatus index 21d3897..dd86149 100755 --- a/bin/khatus +++ b/bin/khatus @@ -17,16 +17,19 @@ consume() { run_producer() { pipe="$1" bin="$2" - log="$3" - cmd="$4" - msg_head="$5" + cmd="$3" + msg_head="$4" "$bin"/$cmd \ - | while read line + 2> >( + while read line do - echo "${msg_head} $line" > "$pipe" + echo "ERROR ${msg_head} $line" > "$pipe" done \ - 2> "$log" - # TODO: Perhaps direct log to pipe, so that controller can monitor sensors? + ) \ + | while read line + do + echo "OK ${msg_head} $line" > "$pipe" + done } fork_reactor() { @@ -47,7 +50,6 @@ main() { declare -A opts=( ["--debug"]=0 ["--dir_bin"]="$HOME/bin" - ["--dir_logs"]=$(mktemp -d) ["--file_pipe"]=$(mktemp) ["--weather_station_id"]='KJFK' ["--screen_brightness_device_name"]='acpi_video0' @@ -122,7 +124,6 @@ main() { # Just shorthand pipe="${opts['--file_pipe']}" bin="${opts['--dir_bin']}" - log="${opts['--dir_logs']}"/khatus_sensors.log rm -f "$pipe" mkfifo "$pipe" @@ -140,23 +141,22 @@ main() { cmd_sens_net_addr_io="khatus_sensor_net_addr_io $bin" cmd_sens_volume="khatus_sensor_volume $bin" - # TODO: Redirect each worker's stderr to a dedicated log file - fork_reactor "$pipe" "$bin" "$log" "khatus_sensor_energy $bin" 'in:ENERGY' - fork_poller "${opts['--interval_inp_datetime']}" "$pipe" "$bin" "$log" khatus_sensor_datetime 'in:DATE_TIME' - fork_poller "${opts['--interval_inp_brightness']}" "$pipe" "$bin" "$log" "$cmd_sens_screen_brightness" 'in:SCREEN_BRIGHTNESS' - fork_poller "${opts['--interval_inp_weather']}" "$pipe" "$bin" "$log" "$cmd_sens_weather" 'in:WEATHER' - fork_poller "${opts['--interval_inp_mpd_state']}" "$pipe" "$bin" "$log" "$cmd_sens_mpd_state" 'in:MPD_STATE' - fork_poller "${opts['--interval_inp_mpd_song']}" "$pipe" "$bin" "$log" khatus_sensor_mpd_song 'in:MPD_SONG' - fork_poller "${opts['--interval_inp_volume']}" "$pipe" "$bin" "$log" "$cmd_sens_volume" 'in:VOLUME' - fork_poller "${opts['--interval_inp_bluetooth']}" "$pipe" "$bin" "$log" "$cmd_sens_bluetooth" 'in:BLUETOOTH_POWER' - fork_poller "${opts['--interval_inp_net_wifi']}" "$pipe" "$bin" "$log" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' - fork_poller "${opts['--interval_inp_net_io']}" "$pipe" "$bin" "$log" "$cmd_sens_net_addr_io" 'in:NET_ADDR_IO' - fork_poller "${opts['--interval_inp_disk_space']}" "$pipe" "$bin" "$log" "$cmd_sens_disk_space" 'in:DISK_SPACE' - fork_poller "${opts['--interval_inp_disk_io']}" "$pipe" "$bin" "$log" "$cmd_sens_disk_io" 'in:DISK_IO' - fork_poller "${opts['--interval_inp_loadavg']}" "$pipe" "$bin" "$log" khatus_sensor_loadavg 'in:LOAD_AVG' - fork_poller "${opts['--interval_inp_temp']}" "$pipe" "$bin" "$log" "$cmd_sens_temperature" 'in:TEMPERATURE' - fork_poller "${opts['--interval_inp_fan']}" "$pipe" "$bin" "$log" "$cmd_sens_fan" 'in:FAN' - fork_poller "${opts['--interval_inp_mem']}" "$pipe" "$bin" "$log" khatus_sensor_memory 'in:MEMORY' + fork_reactor "$pipe" "$bin" "khatus_sensor_energy $bin" 'in:ENERGY' + fork_poller "${opts['--interval_inp_datetime']}" "$pipe" "$bin" khatus_sensor_datetime 'in:DATE_TIME' + fork_poller "${opts['--interval_inp_brightness']}" "$pipe" "$bin" "$cmd_sens_screen_brightness" 'in:SCREEN_BRIGHTNESS' + fork_poller "${opts['--interval_inp_weather']}" "$pipe" "$bin" "$cmd_sens_weather" 'in:WEATHER' + fork_poller "${opts['--interval_inp_mpd_state']}" "$pipe" "$bin" "$cmd_sens_mpd_state" 'in:MPD_STATE' + fork_poller "${opts['--interval_inp_mpd_song']}" "$pipe" "$bin" khatus_sensor_mpd_song 'in:MPD_SONG' + fork_poller "${opts['--interval_inp_volume']}" "$pipe" "$bin" "$cmd_sens_volume" 'in:VOLUME' + fork_poller "${opts['--interval_inp_bluetooth']}" "$pipe" "$bin" "$cmd_sens_bluetooth" 'in:BLUETOOTH_POWER' + fork_poller "${opts['--interval_inp_net_wifi']}" "$pipe" "$bin" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' + fork_poller "${opts['--interval_inp_net_io']}" "$pipe" "$bin" "$cmd_sens_net_addr_io" 'in:NET_ADDR_IO' + fork_poller "${opts['--interval_inp_disk_space']}" "$pipe" "$bin" "$cmd_sens_disk_space" 'in:DISK_SPACE' + fork_poller "${opts['--interval_inp_disk_io']}" "$pipe" "$bin" "$cmd_sens_disk_io" 'in:DISK_IO' + fork_poller "${opts['--interval_inp_loadavg']}" "$pipe" "$bin" khatus_sensor_loadavg 'in:LOAD_AVG' + fork_poller "${opts['--interval_inp_temp']}" "$pipe" "$bin" "$cmd_sens_temperature" 'in:TEMPERATURE' + fork_poller "${opts['--interval_inp_fan']}" "$pipe" "$bin" "$cmd_sens_fan" 'in:FAN' + fork_poller "${opts['--interval_inp_mem']}" "$pipe" "$bin" khatus_sensor_memory 'in:MEMORY' consume \ "$pipe" \ diff --git a/bin/khatus_controller b/bin/khatus_controller index 49195a8..60a6b19 100755 --- a/bin/khatus_controller +++ b/bin/khatus_controller @@ -1,123 +1,148 @@ #! /usr/bin/awk -f -{ debug("LINE", $0) } +/^OK/ { debug("OK line", $0) } -/^in:ENERGY battery/\ +/^ERROR/ { + debug("ERROR line", $0) + shift() + msg_head = $1 + shift() + msg_body = $0 + alert_trigger_hi(msg_head, "KhatusSensorError", msg_body) +} + +/^OK in:ENERGY battery/\ { debug("ENERGY battery", $0) - sub("%$", "", $4) + sub("%$", "", $5) db["energy_state_prev"] = db["energy_state_curr"] - db["energy_state_curr"] = $3 - db["energy_percentage"] = ensure_numeric($4) + db["energy_state_curr"] = $4 + db["energy_percentage"] = ensure_numeric($5) alert_check_energy_battery() } -/^in:ENERGY line_power/\ +/^OK in:ENERGY line_power/\ { debug("ENERGY line_power", $0) db["energy_line_power_prev"] = db["energy_line_power_curr"] - db["energy_line_power_curr"] = $3 + db["energy_line_power_curr"] = $4 alert_check_energy_line_power() } -/^in:MEMORY/\ +/^OK in:MEMORY/\ { + shift() shift() db["memory_total"] = $1 db["memory_used"] = $2 } -/^in:FAN +status:/\ +/^OK in:FAN +status:/\ { + shift() shift() db["fan_status"] = $2 } -/^in:FAN +speed:/\ +/^OK in:FAN +speed:/\ { + shift() shift() db["fan_speed"] = $2 } -/^in:FAN +level:/\ +/^OK in:FAN +level:/\ { + shift() shift() db["fan_level"] = $2 } -/^in:TEMPERATURE/\ +/^OK in:TEMPERATURE/\ { + shift() shift() db["temperature"] = $1 } -/^in:LOAD_AVG/\ +/^OK in:LOAD_AVG/\ { + shift() shift() set_load_avg() } -/^in:DISK_IO/\ +/^OK in:DISK_IO/\ { + shift() shift() set_disk_io() } -/^in:DISK_SPACE/\ +/^OK in:DISK_SPACE/\ { + shift() shift() db["disk_space_used"] = $0 } -/^in:NET_ADDR_IO/\ +/^OK in:NET_ADDR_IO/\ { + shift() shift() set_net_addr_io() } -/^in:NET_WIFI_STATUS/\ +/^OK in:NET_WIFI_STATUS/\ { + shift() shift() db["net_wifi_status"] = $0 } -/^in:BLUETOOTH_POWER/\ +/^OK in:BLUETOOTH_POWER/\ { + shift() shift() db["bluetooth_power"] = $0 } -/^in:SCREEN_BRIGHTNESS/\ +/^OK in:SCREEN_BRIGHTNESS/\ { + shift() shift() set_screen_brightness() } -/^in:VOLUME/\ +/^OK in:VOLUME/\ { + shift() set_volume() } -/^in:MPD_SONG OK +MPD/ { delete db_mpd_song; next } -/^in:MPD_SONG OK$/ { set_mpd_playing() ; next } -/^in:MPD_SONG / { set_mpd_song() ; next } +/^OK in:MPD_SONG OK +MPD/ { delete db_mpd_song; next } +/^OK in:MPD_SONG OK$/ { set_mpd_playing() ; next } +/^OK in:MPD_SONG / { set_mpd_song() ; next } -/^in:MPD_STATE /\ +/^OK in:MPD_STATE /\ { + shift() shift() db["mpd_status_state"] = $1 db["mpd_status_time"] = $2 db["mpd_status_percent"] = $3 } -/^in:WEATHER/\ +/^OK in:WEATHER/\ { + shift() shift() db["weather_temperature"] = $0 } -/^in:DATE_TIME/\ +/^OK in:DATE_TIME/\ { + shift() shift() db["datetime"] = $0 output_msg_status_bar(make_status_bar()) @@ -142,6 +167,7 @@ function set_volume( mute, left, right) { } function set_mpd_song( key, val) { + shift() key = $2 shift() shift()