4 FS = msg_fs ? msg_fs
: "|"
5 OFS = msg_fs ? msg_fs
: "|"
6 Kfs = key_fs ? key_fs
: ":"
8 # TODO: Read spec from a file
9 bat_alert_spec
[100] =
"low|Energy_Bellow_Full|Must have perfection!"
10 bat_alert_spec
[50] =
"low|Energy_Bellow_Half|Where is the charger?"
11 bat_alert_spec
[20] =
"med|Energy_Low|Get the charger."
12 bat_alert_spec
[15] =
"med|Energy_Low|Get the charger!"
13 bat_alert_spec
[10] =
"hi|Energy_Low|Plug it in, ASAP!"
14 bat_alert_spec
[5] =
"hi|Energy_CRITICALLY_Low|CHARGE NOW!!! GO GO GO!!!"
18 $
2 ==
"khatus_sensor_energy" && \
20 line_power_prev = line_power_curr
22 if (line_power_curr ==
"no" && line_power_prev
!= "no") {
23 alert
("low", "PowerUnplugged", "")
28 $
2 ==
"khatus_sensor_energy" && \
29 $
3 ==
"battery_state" {
30 battery_state_prev = battery_state_curr
31 battery_state_curr = $
4
35 $
2 ==
"khatus_sensor_energy" && \
36 $
3 ==
"battery_percentage" {
37 # TODO: Re-think the spec - can't rely on order of keys
38 battery_percentage = ensure_numeric
($
4)
39 if (battery_state_curr ==
"discharging") {
40 for (threshold in bat_alert_spec
) {
41 threshold = ensure_numeric
(threshold
)
42 if (battery_percentage
<= threshold
&& !alerted
[threshold
]) {
43 split(bat_alert_spec
[threshold
], msg
, "|")
46 body =
sprintf("%d%% %s", battery_percentage
, msg
[3])
47 alert
(priority
, subject
, body
)
56 function alert
(priority
, subject
, body
) {
57 # priority : "low" | "med" | "hi"
60 print("OK", "khatus_monitor_energy", "alert", priority
, subject
, body
)
63 function ensure_numeric
(n
) {
67 #-------------------------------
68 # Why do we need ensure_numeric?
69 #-------------------------------
70 # awk appears to be guessing the type of an inputted scalar based on usage, so
71 # if we read-in a number, but did not use it in any numeric operations, but did
72 # use as a string (even in just a format string!) - it will be treated as a
73 # string and can lead to REALLY SURPRISING behavior in conditional statements,
74 # where smaller number may compare as greater than the bigger ones, such as.
78 # $ awk 'BEGIN {x = "75"; y = "100"; sprintf("x: %d, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
80 # $ awk 'BEGIN {x = "75"; y = "100"; sprintf("x: %s, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
83 # However, once used as a number, seems to stay that way even after being
86 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; sprintf("x: %s, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
89 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; sprintf("x: %s, y: %d\n", x, y); z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
92 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
94 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'