Re-use AWK components
[khatus.git] / src / awk / exe / monitor_energy.awk
CommitLineData
03c229bf
SK
1BEGIN {
2 # TODO: Read spec from a file
3 bat_alert_spec[100] = "low|Energy_Bellow_Full|Must have perfection!"
4 bat_alert_spec[50] = "low|Energy_Bellow_Half|Where is the charger?"
5 bat_alert_spec[20] = "med|Energy_Low|Get the charger."
6 bat_alert_spec[15] = "med|Energy_Low|Get the charger!"
7 bat_alert_spec[10] = "hi|Energy_Low|Plug it in, ASAP!"
8 bat_alert_spec[5] = "hi|Energy_CRITICALLY_Low|CHARGE NOW!!! GO GO GO!!!"
9}
10
11$1 == "OK" && \
12$2 == "khatus_sensor_energy" && \
13$3 == "line_power" {
14 line_power_prev = line_power_curr
15 line_power_curr = $4
16 if (line_power_curr == "no" && line_power_prev != "no") {
17 alert("low", "PowerUnplugged", "")
18 }
19}
20
21$1 == "OK" && \
22$2 == "khatus_sensor_energy" && \
23$3 == "battery_state" {
24 battery_state_prev = battery_state_curr
25 battery_state_curr = $4
26}
27
28$1 == "OK" && \
29$2 == "khatus_sensor_energy" && \
30$3 == "battery_percentage" {
31 # TODO: Re-think the spec - can't rely on order of keys
32 battery_percentage = util_ensure_numeric($4)
33 if (battery_state_curr == "discharging") {
34 for (threshold in bat_alert_spec) {
35 threshold = util_ensure_numeric(threshold)
36 if (battery_percentage <= threshold && !_alerted[threshold]) {
37 split(bat_alert_spec[threshold], msg, "|")
38 priority = msg[1]
39 subject = msg[2]
40 body = sprintf("%d%% %s", battery_percentage, msg[3])
41 alert(priority, subject, body)
42 _alerted[threshold]++
43 }
44 }
45 } else {
46 delete _alerted
47 }
48}
This page took 0.021342 seconds and 4 git commands to generate.