| 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 | function 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 | |
| 63 | function 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 |