a295e3ed7a1937da536d0ba6300d809f044e16fd
[khatus.git] / bin / khatus_monitor_energy
1 #! /usr/bin/awk -f
2
3 BEGIN {
4 FS = msg_fs ? msg_fs : "|"
5 OFS = msg_fs ? msg_fs : "|"
6 Kfs = key_fs ? key_fs : ":"
7
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!!!"
14 }
15
16 $1 == "OK" && \
17 $2 == "khatus_sensor_energy" && \
18 $3 == "line_power" {
19 line_power_prev = line_power_curr
20 line_power_curr = $4
21 if (line_power_curr == "no" && line_power_prev != "no") {
22 alert("low", "PowerUnplugged", "")
23 }
24 }
25
26 $1 == "OK" && \
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)
32 }
33
34 $1 == "OK" && \
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, "|")
44 priority = msg[1]
45 subject = msg[2]
46 body = sprintf("%d%% %s", battery_percentage, msg[3])
47 alert(priority, subject, body)
48 alerted[threshold]++
49 }
50 }
51 } else {
52 delete alerted
53 }
54 }
55
56 # After peeking, let everything pass through!
57 //
58
59 function alert(priority, subject, body) {
60 # priority : "low" | "med" | "hi"
61 # subject : no spaces
62 # body : anything
63 print("OK", "khatus_monitor_energy", "alert", priority, subject, body)
64 }
65
66 function ensure_numeric(n) {
67 return n + 0
68 }
69
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.
78 #
79 # Demo:
80 #
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"}}'
82 # 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"}}'
84 # 75 > 100
85
86 # However, once used as a number, seems to stay that way even after being
87 # used as string:
88 #
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"}}'
90 # 75 < 100
91 #
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"}}'
93 # 75 < 100
94 #
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"}}'
96 # 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"}}'
98 # 75 > 100
This page took 0.070983 seconds and 3 git commands to generate.