Add energy alert for <20%
[khatus.git] / bin / khatus_controller
1 #! /usr/bin/awk -f
2
3
4 /^in:ENERGY/\
5 {
6 split_msg_parts()
7 sub("%$", "", $2)
8 db["energy_state"] = $1
9 db["energy_percentage"] = ensure_numeric($2)
10 alert_check_energy()
11 }
12
13 /^in:MEMORY/\
14 {
15 split_msg_parts()
16 db["memory_total"] = $1
17 db["memory_used"] = $2
18 }
19
20 /^in:FAN +status:/\
21 {
22 split_msg_parts()
23 db["fan_status"] = $2
24 }
25
26 /^in:FAN +speed:/\
27 {
28 split_msg_parts()
29 db["fan_speed"] = $2
30 }
31
32 /^in:FAN +level:/\
33 {
34 split_msg_parts()
35 db["fan_level"] = $2
36 }
37
38 /^in:TEMPERATURE/\
39 {
40 split_msg_parts()
41 db["temperature"] = $1
42 }
43
44 /^in:LOAD_AVG/\
45 {
46 split_msg_parts()
47 set_load_avg()
48 }
49
50 /^in:DISK_IO/\
51 {
52 split_msg_parts()
53 set_disk_io()
54 }
55
56 /^in:DISK_SPACE/\
57 {
58 split_msg_parts()
59 db["disk_space_used"] = msg_body
60 }
61
62 /^in:NET_ADDR_IO/\
63 {
64 split_msg_parts()
65 set_net_addr_io()
66 }
67
68 /^in:NET_WIFI_STATUS/\
69 {
70 split_msg_parts()
71 db["net_wifi_status"] = msg_body
72 }
73
74 /^in:BLUETOOTH_POWER/\
75 {
76 split_msg_parts()
77 db["bluetooth_power"] = msg_body
78 }
79
80 /^in:SCREEN_BRIGHTNESS/\
81 {
82 split_msg_parts()
83 set_screen_brightness()
84 }
85
86 /^in:VOLUME/\
87 {
88 split_msg_parts()
89 db["volume"] = msg_body
90 }
91
92 /^in:MPD_STATE/\
93 {
94 split_msg_parts()
95 db["mpd_state"] = $1
96 db["mpd_curr_song_time"] = $2
97 db["mpd_curr_song_percent"] = $3
98 }
99
100 /^in:MPD_SONG/\
101 {
102 split_msg_parts()
103 db["mpd_curr_song_name"] = msg_body
104 }
105
106 /^in:WEATHER/\
107 {
108 split_msg_parts()
109 db["weather_temperature"] = msg_body
110 }
111
112 /^in:DATE_TIME/\
113 {
114 split_msg_parts()
115 db["datetime"] = msg_body
116 output_msg_status_bar(make_status_bar())
117 }
118
119 # TODO: Generalize alert spec lang
120 # - trigger threshold
121 # - above/bellow/equal to threshold value
122 # - priority
123 # - snooze time (if already alerted, when to re-alert?)
124 # - text: subject/body
125 function alert_check_energy( from, dbg, state, remaining, subj, body) {
126 from = "alert_check_energy"
127
128 state = db["energy_state"]
129 remaining = db["energy_percentage"]
130
131 dbg["state"] = state
132 dbg["remaining"] = remaining
133 debug(from, dbg)
134
135 if (state == "discharging") {
136 if (remaining < 5) {
137 subj = "Energy_CRITICALLY_Low"
138 body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining)
139 alert_trigger_hi(from, subj, body)
140 } else if (remaining < 10) {
141 subj = "Energy_Very_Low"
142 body = sprintf("%d%% Plug it in ASAP.", remaining)
143 alert_trigger_hi(from, subj, body)
144 } else if (remaining < 15) {
145 subj = "Energy_Low"
146 body = sprintf("%d%% Get the charger.", remaining)
147 alert_trigger_hi(from, subj, body)
148 } else if (remaining < 20) {
149 subj = "Energy_Low"
150 body = sprintf("%d%% Get the charger.", remaining)
151 alert_trigger_med(from, subj, body)
152 } else if (remaining < 50) {
153 if (!state__alerts__energy__notified_bellow_half) {
154 state__alerts__energy__notified_bellow_half = 1
155 subj = "Energy_Bellow_Half"
156 body = sprintf("%d%% Where is the charger?", remaining)
157 alert_trigger_med(from, subj, body)
158 }
159 }
160 } else {
161 # TODO: Reconsider the competing global-state organizing-conventions
162 state__alerts__energy__notified_bellow_half = 0
163 }
164 }
165
166 function alert_trigger_low(from, subject, body) {
167 alert_trigger("low", from, subject, body)
168 }
169
170 function alert_trigger_med(from, subject, body) {
171 alert_trigger("med", from, subject, body)
172 }
173
174 function alert_trigger_hi(from, subject, body) {
175 alert_trigger("hi", from, subject, body)
176 }
177
178 function alert_trigger(priority, from, subject, body, msg) {
179 # priority : "low" | "med" | "hi"
180 # subject : no spaces
181 # body : anything
182 msg = sprintf("khatus_%s %s %s %s", from, priority, subject, body)
183 output_msg_alert(msg)
184 }
185
186 function output_msg_alert(msg) {
187 # TODO: Should alerts go into a dedicated channel?
188 output_msg("ALERT", msg, "/dev/stdout")
189 }
190
191 function output_msg_status_bar(msg) {
192 output_msg("STATUS_BAR", msg, "/dev/stdout")
193 }
194
195 function output_msg(type, content, channel) {
196 print(type, content) > channel
197 }
198
199 function set_load_avg( sched) {
200 split($4, sched, "/")
201 db["load_avg_1min"] = $1
202 db["load_avg_5min"] = $2
203 db["load_avg_15min"] = $3
204 db["kern_sched_queue_runnable"] = sched[1]
205 db["kern_sched_queue_total"] = sched[2]
206 db["kern_sched_latest_pid"] = $5
207 }
208
209 function set_disk_io( curr_w, curr_r, prev_w, prev_r) {
210 curr_w = $1
211 curr_r = $2
212 prev_w = db["disk_io_curr_w"]
213 prev_r = db["disk_io_curr_r"]
214 db["disk_io_curr_w"] = curr_w
215 db["disk_io_curr_r"] = curr_r
216 db["disk_io_diff_w"] = curr_w - prev_w
217 db["disk_io_diff_r"] = curr_r - prev_r
218 }
219
220 function set_net_addr_io( \
221 interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\
222 ) {
223 interface = $1
224 address = $2
225 io_curr_w = $3
226 io_curr_r = $4
227 if (interface) {
228 if (address && io_curr_w && io_curr_r) {
229 # recalculate
230 io_prev_w = net_io_curr_w[interface]
231 io_prev_r = net_io_curr_r[interface]
232
233 net_addr[interface] = address
234 net_io_curr_w[interface] = io_curr_w
235 net_io_curr_r[interface] = io_curr_r
236 net_io_diff_w[interface] = io_curr_w - io_prev_w
237 net_io_diff_r[interface] = io_curr_r - io_prev_r
238 } else {
239 # clear
240 net_addr[interface] = ""
241 net_io_curr_w[interface] = 0
242 net_io_curr_r[interface] = 0
243 net_io_diff_w[interface] = 0
244 net_io_diff_r[interface] = 0
245 }
246 }
247 }
248
249 function set_screen_brightness( max, cur) {
250 max = $1
251 cur = $2
252 db["screen_brightness"] = (cur / max) * 100
253 }
254
255 function split_msg_parts( dbg) {
256 msg_head = $1
257 sub("^" msg_head " +", "")
258 msg_body = $0
259 dbg["msg_head"] = msg_head
260 dbg["msg_body"] = msg_body
261 debug("split_msg_parts", dbg)
262 }
263
264 function make_status_bar( position, bar, sep, i, j) {
265 position[++i] = make_status_energy()
266 position[++i] = make_status_mem()
267 position[++i] = make_status_cpu()
268 position[++i] = make_status_disk()
269 position[++i] = make_status_net()
270 position[++i] = sprintf("B=%s", db["bluetooth_power"])
271 position[++i] = sprintf("*%d%%", db["screen_brightness"])
272 position[++i] = sprintf("(%s)", db["volume"])
273 position[++i] = make_status_mpd()
274 position[++i] = db["weather_temperature"]
275 position[++i] = db["datetime"]
276 bar = ""
277 sep = ""
278 for (j = 1; j <= i; j++) {
279 bar = bar sep position[j]
280 sep = " "
281 }
282 return bar
283 }
284
285 function make_status_energy( state, direction_of_change) {
286 state = db["energy_state"]
287 if (state == "discharging") {
288 direction_of_change = "<"
289 } else if (state == "charging") {
290 direction_of_change = ">"
291 } else {
292 direction_of_change = "="
293 };
294 return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"])
295 }
296
297 function make_status_mem( total, used, percent, status) {
298 total = db["memory_total"]
299 used = db["memory_used"]
300 # To avoid division by zero when data is missing
301 if (total && used) {
302 percent = round((used / total) * 100)
303 status = sprintf("%d%%", percent)
304 } else {
305 status = "__"
306 }
307 return sprintf("M=%s", status)
308 }
309
310 function make_status_cpu( load, temp, fan) {
311 load = db["load_avg_1min"]
312 temp = db["temperature"] / 1000
313 fan = db["fan_speed"]
314 return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan)
315 }
316
317 function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) {
318 bytes_per_sector = 512
319 bytes_per_mb = 1024 * 1024
320 w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb
321 r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb
322 return \
323 sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r)
324 }
325
326 function make_status_net( \
327 out,
328 number_of_interfaces_to_show,
329 n,
330 array_of_prefixes_of_interfaces_to_show,
331 prefix,
332 interface,
333 label,
334 count_printed,
335 sep,
336 io_stat,
337 dw, dr,
338 bytes_per_unit\
339 ) {
340 out = ""
341 number_of_interfaces_to_show = \
342 split(\
343 opt_prefixes_of_net_interfaces_to_show,\
344 array_of_prefixes_of_interfaces_to_show,\
345 ","\
346 )
347 for (n = 1; n <= number_of_interfaces_to_show; n++) {
348 prefix = array_of_prefixes_of_interfaces_to_show[n]
349 for (interface in net_addr) {
350 if (interface ~ ("^" prefix)) {
351 label = substr(interface, 1, 1)
352 if (net_addr[interface]) {
353 bytes_per_mb = 1024 * 1024 # TODO: option
354 dw = net_io_diff_w[interface] / bytes_per_mb
355 dr = net_io_diff_r[interface] / bytes_per_mb
356 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
357 } else {
358 io_stat = "--"
359 }
360 if (interface ~ "^w") {
361 label = label ":" db["net_wifi_status"]
362 }
363 if (++count_printed > 1) {
364 sep = " "
365 } else {
366 sep = ""
367 }
368 out = out sep label ":" io_stat
369 }
370 }
371 }
372 return sprintf("N[%s]", out)
373 }
374
375 function make_status_mpd( state, status) {
376 state = db["mpd_state"]
377
378 if (state == "play") {
379 status = make_status_mpd_state_known("▶")
380 } else if (state == "pause") {
381 status = make_status_mpd_state_known("❚❚")
382 } else if (state == "stop") {
383 status = make_status_mpd_state_known("⬛")
384 } else {
385 status = make_status_mpd_state_unknown("--")
386 }
387
388 return sprintf("[%s]", status)
389 }
390
391 function make_status_mpd_state_known(symbol) {
392 return sprintf(\
393 "%s %s %s %s",
394 symbol,
395 db["mpd_curr_song_time"],
396 db["mpd_curr_song_percent"],
397 substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
398 )
399 }
400
401 function make_status_mpd_state_unknown(symbol) {
402 return sprintf("%s", symbol)
403 }
404
405 function round(n) {
406 return int(n + 0.5)
407 }
408
409 function debug(location, values, sep, vals, key, msg) {
410 if (opt_debug) {
411 sep = ""
412 vals = ""
413 for (key in values) {
414 vals = sprintf("%s%s%s: %s", vals, sep, key, values[key])
415 sep = ", "
416 }
417 msg = location " ==> " vals "."
418 output_msg("DEBUG", msg, "/dev/stderr")
419 }
420 }
421
422 function ensure_numeric(n) {
423 return n + 0
424 }
425 #-------------------------------
426 # Why do we need ensure_numeric?
427 #-------------------------------
428 # awk appears to be guessing the type of an inputted scalar based on usage, so
429 # if we read-in a number, but did not use it in any numeric operations, but did
430 # use as a string (even in just a format string!) - it will be treated as a
431 # string and can lead to REALLY SURPRISING behavior in conditional statements,
432 # where smaller number may compare as greater than the bigger ones, such as.
433 #
434 # Demo:
435 #
436 # $ 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"}}'
437 # 75 < 100
438 # $ 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"}}'
439 # 75 > 100
440
441 # However, once used as a number, seems to stay that way even after being
442 # used as string:
443 #
444 # $ 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"}}'
445 # 75 < 100
446 #
447 # $ 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"}}'
448 # 75 < 100
449 #
450 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
451 # 75 < 100
452 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
453 # 75 > 100
This page took 0.100243 seconds and 5 git commands to generate.