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