4 FS = msg_fs ? msg_fs
: "|"
5 OFS = msg_fs ? msg_fs
: "|"
6 Kfs = key_fs ? key_fs
: ":"
8 bat_alert_spec
[100] =
"low|Energy_Bellow_Full|Must have perfection!"
9 bat_alert_spec
[50] =
"low|Energy_Bellow_Half|Where is the charger?"
10 bat_alert_spec
[20] =
"med|Energy_Low|Get the charger."
11 bat_alert_spec
[15] =
"med|Energy_Low|Get the charger!"
12 bat_alert_spec
[10] =
"hi|Energy_Low|Plug it in, ASAP!"
13 bat_alert_spec
[5] =
"hi|Energy_CRITICALLY_Low|CHARGE NOW!!! GO GO GO!!!"
17 $
2 ==
"khatus_sensor_energy" && \
19 line_power_prev = line_power_curr
21 if (line_power_curr ==
"no" && line_power_prev
!= "no") {
22 alert
("low", "PowerUnplugged", "")
27 $
2 ==
"khatus_sensor_energy" && \
28 $
3 ==
"battery_state" {
29 battery_state_prev = battery_state_curr
30 battery_state_curr = $
4
31 printf("BATTERY_STATE prev:%s curr:%s\n", battery_state_prev
, battery_state_curr
)
35 $
2 ==
"khatus_sensor_energy" && \
36 $
3 ==
"battery_percentage" {
37 battery_percentage = ensure_numeric
($
4)
38 printf("BATTERY_PERCENTAGE %s\n", battery_percentage
)
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 # After peeking, let everything pass through!
59 function alert
(priority
, subject
, body
) {
60 # priority : "low" | "med" | "hi"
63 print("OK", "khatus_monitor_energy", "alert", priority
, subject
, body
)
66 function ensure_numeric
(n
) {
70 #-------------------------------
71 # Why do we need ensure_numeric?
72 #-------------------------------
73 # awk appears to be guessing the type of an inputted scalar based on usage, so
74 # if we read-in a number, but did not use it in any numeric operations, but did
75 # use as a string (even in just a format string!) - it will be treated as a
76 # string and can lead to REALLY SURPRISING behavior in conditional statements,
77 # where smaller number may compare as greater than the bigger ones, such as.
81 # $ 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"}}'
83 # $ 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"}}'
86 # However, once used as a number, seems to stay that way even after being
89 # $ 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"}}'
92 # $ 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"}}'
95 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
97 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'