Add summary of process state meanings
[khatus.git] / bin / khatus_monitor_energy
CommitLineData
75b23ff8
SK
1#! /usr/bin/awk -f
2
3BEGIN {
4 FS = msg_fs ? msg_fs : "|"
5 OFS = msg_fs ? msg_fs : "|"
6 Kfs = key_fs ? key_fs : ":"
7
8da9a819 8 # TODO: Read spec from a file
75b23ff8
SK
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!!!"
15}
16
17$1 == "OK" && \
18$2 == "khatus_sensor_energy" && \
19$3 == "line_power" {
20 line_power_prev = line_power_curr
21 line_power_curr = $4
22 if (line_power_curr == "no" && line_power_prev != "no") {
23 alert("low", "PowerUnplugged", "")
24 }
25}
26
27$1 == "OK" && \
28$2 == "khatus_sensor_energy" && \
29$3 == "battery_state" {
30 battery_state_prev = battery_state_curr
31 battery_state_curr = $4
75b23ff8
SK
32}
33
34$1 == "OK" && \
35$2 == "khatus_sensor_energy" && \
36$3 == "battery_percentage" {
8da9a819 37 # TODO: Re-think the spec - can't rely on order of keys
75b23ff8 38 battery_percentage = ensure_numeric($4)
75b23ff8
SK
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
75b23ff8
SK
56function alert(priority, subject, body) {
57 # priority : "low" | "med" | "hi"
58 # subject : no spaces
59 # body : anything
60 print("OK", "khatus_monitor_energy", "alert", priority, subject, body)
61}
62
63function ensure_numeric(n) {
64 return n + 0
65}
66
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.
75#
76# Demo:
77#
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"}}'
79# 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"}}'
81# 75 > 100
82
83# However, once used as a number, seems to stay that way even after being
84# used as string:
85#
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"}}'
87# 75 < 100
88#
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"}}'
90# 75 < 100
91#
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"}}'
93# 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"}}'
95# 75 > 100
This page took 0.023445 seconds and 4 git commands to generate.