Grab wifi status from iwconfig instead of nmcli
[khatus.git] / bin / khatus_controller
1 #! /usr/bin/awk -f
2
3 /^OK/ { debug("OK line", $0) }
4
5 /^ERROR/ {
6 debug("ERROR line", $0)
7 shift()
8 msg_head = $1
9 shift()
10 msg_body = $0
11 alert_trigger_hi(msg_head, "KhatusSensorError", msg_body)
12 }
13
14 /^OK in:ENERGY battery/\
15 {
16 debug("ENERGY battery", $0)
17 sub("%$", "", $5)
18 db["energy_state_prev"] = db["energy_state_curr"]
19 db["energy_state_curr"] = $4
20 db["energy_percentage"] = ensure_numeric($5)
21 alert_check_energy_battery()
22 }
23
24 /^OK in:ENERGY line_power/\
25 {
26 debug("ENERGY line_power", $0)
27 db["energy_line_power_prev"] = db["energy_line_power_curr"]
28 db["energy_line_power_curr"] = $4
29 alert_check_energy_line_power()
30 }
31
32 /^OK in:MEMORY/\
33 {
34 shift()
35 shift()
36 db["memory_total"] = $1
37 db["memory_used"] = $2
38 }
39
40 /^OK in:FAN +status:/\
41 {
42 shift()
43 shift()
44 db["fan_status"] = $2
45 }
46
47 /^OK in:FAN +speed:/\
48 {
49 shift()
50 shift()
51 db["fan_speed"] = $2
52 }
53
54 /^OK in:FAN +level:/\
55 {
56 shift()
57 shift()
58 db["fan_level"] = $2
59 }
60
61 /^OK in:TEMPERATURE/\
62 {
63 shift()
64 shift()
65 db["temperature"] = $1
66 }
67
68 /^OK in:LOAD_AVG/\
69 {
70 shift()
71 shift()
72 set_load_avg()
73 }
74
75 /^OK in:DISK_IO/\
76 {
77 shift()
78 shift()
79 set_disk_io()
80 }
81
82 /^OK in:DISK_SPACE/\
83 {
84 shift()
85 shift()
86 db["disk_space_used"] = $0
87 }
88
89 /^OK in:NET_ADDR_IO/\
90 {
91 shift()
92 shift()
93 set_net_addr_io()
94 }
95
96 /^OK in:NET_WIFI_STATUS/\
97 {
98 shift()
99 shift()
100 set_net_wifi_status()
101 }
102
103 /^OK in:BLUETOOTH_POWER/\
104 {
105 shift()
106 shift()
107 db["bluetooth_power"] = $0
108 }
109
110 /^OK in:SCREEN_BRIGHTNESS/\
111 {
112 shift()
113 shift()
114 set_screen_brightness()
115 }
116
117 /^OK in:VOLUME/\
118 {
119 shift()
120 set_volume()
121 }
122
123 /^OK in:MPD_SONG OK +MPD/ { delete db_mpd_song; next }
124 /^OK in:MPD_SONG OK$/ { set_mpd_playing() ; next }
125 /^OK in:MPD_SONG / { set_mpd_song() ; next }
126
127 /^OK in:MPD_STATE /\
128 {
129 shift()
130 shift()
131 db["mpd_status_state"] = $1
132 db["mpd_status_time"] = $2
133 db["mpd_status_percent"] = $3
134 }
135
136 /^OK in:WEATHER/\
137 {
138 shift()
139 shift()
140 db["weather_temperature"] = $0
141 }
142
143 /^OK in:DATE_TIME/\
144 {
145 shift()
146 shift()
147 db["datetime"] = $0
148 output_msg_status_bar(make_status_bar())
149 }
150
151 function set_volume( mute, left, right) {
152 # 0 RUNNING no 75% 75%
153 #msg_head = $1
154 #sink = $2
155 #state = $3
156 mute = $4
157 left = $5
158 right = $6
159
160 if (mute == "no") {
161 db["volume"] = sprintf("%s %s", left, right)
162 } else if (mute == "yes") {
163 db["volume"] = "X"
164 } else {
165 error("set_volume", "Unexpected value for 'mute' field: " mute)
166 }
167 }
168
169 function set_mpd_song( key, val) {
170 shift()
171 key = $2
172 shift()
173 shift()
174 val = $0
175 db_mpd_song[key] = val
176 debug("set_mpd_song", "", db_mpd_song)
177 }
178
179 function set_mpd_playing( \
180 currently_playing, name, title, file, last, parts\
181 ) {
182 debug("set_mpd_playing", "", db_mpd_song)
183 name = db_mpd_song["Name:"]
184 title = db_mpd_song["Title:"]
185 file = db_mpd_song["file:"]
186
187 if (name) {
188 currently_playing = name
189 } else if (title) {
190 currently_playing = title
191 } else if (file) {
192 last = split(file, parts, "/")
193 currently_playing = parts[last]
194 } else {
195 currently_playing = ""
196 }
197 db["mpd_playing_prev"] = db["mpd_playing_curr"]
198 db["mpd_playing_curr"] = currently_playing
199
200 alert_check_mpd()
201 }
202
203 function alert_check_mpd( curr, prev, name, body) {
204 prev = db["mpd_playing_prev"]
205 curr = db["mpd_playing_curr"]
206 if (curr && curr != prev) {
207 name = db_mpd_song["Name:"]
208 if (name) {
209 body = name
210 } else {
211 body = \
212 db_mpd_song["Artist:"] \
213 " - " db_mpd_song["Album:"] \
214 " - " db_mpd_song["Title:"]
215 }
216 alert_trigger_low("alert_check_mpd", "MpdNowPlaying", body)
217 }
218 }
219
220 # TODO: Generalize alert spec lang
221 # - trigger threshold
222 # - above/bellow/equal to threshold value
223 # - priority
224 # - snooze time (if already alerted, when to re-alert?)
225 # - text: subject/body
226 function alert_check_energy_battery( \
227 from, dbg, state_curr, state_prev, remaining, subj, body\
228 ) {
229 from = "alert_check_energy_battery"
230
231 state_curr = db["energy_state_curr"]
232 state_prev = db["energy_state_prev"]
233 remaining = db["energy_percentage"]
234
235 dbg["state_curr"] = state_curr
236 dbg["remaining"] = remaining
237 debug(from, "", dbg)
238
239 if (state_curr == "discharging") {
240 if (remaining < 5) {
241 subj = "Energy_CRITICALLY_Low"
242 body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining)
243 alert_trigger_hi(from, subj, body)
244 } else if (remaining < 10) {
245 subj = "Energy_Very_Low"
246 body = sprintf("%d%% Plug it in ASAP.", remaining)
247 alert_trigger_hi(from, subj, body)
248 } else if (remaining < 15) {
249 subj = "Energy_Low"
250 body = sprintf("%d%% Get the charger.", remaining)
251 alert_trigger_hi(from, subj, body)
252 } else if (remaining < 20) {
253 subj = "Energy_Low"
254 body = sprintf("%d%% Get the charger.", remaining)
255 alert_trigger_med(from, subj, body)
256 } else if (remaining < 50) {
257 if (!state__alerts__energy__notified_bellow_half) {
258 state__alerts__energy__notified_bellow_half = 1
259 subj = "Energy_Bellow_Half"
260 body = sprintf("%d%% Where is the charger?", remaining)
261 alert_trigger_med(from, subj, body)
262 }
263 }
264 } else {
265 # TODO: Reconsider the competing global-state organizing-conventions
266 state__alerts__energy__notified_bellow_half = 0
267 }
268 }
269
270 function alert_check_energy_line_power( \
271 from, dbg, line_power_curr, line_power_prev, subj, body \
272 ) {
273 from = "alert_check_energy_line_power"
274
275 dbg["energy_line_power_prev"] = db["energy_line_power_prev"]
276 dbg["energy_line_power_curr"] = db["energy_line_power_curr"]
277 debug(from, "", dbg)
278
279 line_power_curr = db["energy_line_power_curr"]
280 line_power_prev = db["energy_line_power_prev"]
281
282 if (line_power_curr == "no" && line_power_prev != "no") {
283 alert_trigger_low(from, "PowerUnplugged", "")
284 }
285 }
286
287 function alert_trigger_low(from, subject, body) {
288 alert_trigger("low", from, subject, body)
289 }
290
291 function alert_trigger_med(from, subject, body) {
292 alert_trigger("med", from, subject, body)
293 }
294
295 function alert_trigger_hi(from, subject, body) {
296 alert_trigger("hi", from, subject, body)
297 }
298
299 function alert_trigger(priority, from, subject, body, msg) {
300 # priority : "low" | "med" | "hi"
301 # subject : no spaces
302 # body : anything
303 msg = sprintf("khatus_%s %s %s %s", from, priority, subject, body)
304 output_msg_alert(msg)
305 }
306
307 function output_msg_alert(msg) {
308 # TODO: Should alerts go into a dedicated channel?
309 output_msg("ALERT", msg, "/dev/stdout")
310 }
311
312 function output_msg_status_bar(msg) {
313 output_msg("STATUS_BAR", msg, "/dev/stdout")
314 }
315
316 function output_msg(type, content, channel) {
317 print(type, content) > channel
318 }
319
320 function set_load_avg( sched) {
321 split($4, sched, "/")
322 db["load_avg_1min"] = $1
323 db["load_avg_5min"] = $2
324 db["load_avg_15min"] = $3
325 db["kern_sched_queue_runnable"] = sched[1]
326 db["kern_sched_queue_total"] = sched[2]
327 db["kern_sched_latest_pid"] = $5
328 }
329
330 function set_disk_io( curr_w, curr_r, prev_w, prev_r) {
331 curr_w = $1
332 curr_r = $2
333 prev_w = db["disk_io_curr_w"]
334 prev_r = db["disk_io_curr_r"]
335 db["disk_io_curr_w"] = curr_w
336 db["disk_io_curr_r"] = curr_r
337 db["disk_io_diff_w"] = curr_w - prev_w
338 db["disk_io_diff_r"] = curr_r - prev_r
339 }
340
341 function set_net_wifi_status( interface) {
342 interface = $1
343 shift()
344 db["net_wifi_status", interface] = $0
345 }
346
347 function set_net_addr_io( \
348 interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\
349 ) {
350 interface = $1
351 address = $2
352 io_curr_w = $3
353 io_curr_r = $4
354 if (interface) {
355 if (address && io_curr_w && io_curr_r) {
356 # recalculate
357 io_prev_w = net_io_curr_w[interface]
358 io_prev_r = net_io_curr_r[interface]
359
360 net_addr[interface] = address
361 net_io_curr_w[interface] = io_curr_w
362 net_io_curr_r[interface] = io_curr_r
363 net_io_diff_w[interface] = io_curr_w - io_prev_w
364 net_io_diff_r[interface] = io_curr_r - io_prev_r
365 } else {
366 # clear
367 net_addr[interface] = ""
368 net_io_curr_w[interface] = 0
369 net_io_curr_r[interface] = 0
370 net_io_diff_w[interface] = 0
371 net_io_diff_r[interface] = 0
372 }
373 }
374 }
375
376 function set_screen_brightness( max, cur) {
377 max = $1
378 cur = $2
379 db["screen_brightness"] = (cur / max) * 100
380 }
381
382 # TODO: Revise overuse of shift() where it is not really needed
383 function shift() {
384 sub("^" $1 " +", "")
385 }
386
387 function make_status_bar( position, bar, sep, i, j) {
388 position[++i] = make_status_energy()
389 position[++i] = make_status_mem()
390 position[++i] = make_status_cpu()
391 position[++i] = make_status_disk()
392 position[++i] = make_status_net()
393 position[++i] = sprintf("B=%s", db["bluetooth_power"])
394 position[++i] = sprintf("*%d%%", db["screen_brightness"])
395 position[++i] = sprintf("(%s)", db["volume"])
396 position[++i] = make_status_mpd()
397 position[++i] = db["weather_temperature"]
398 position[++i] = db["datetime"]
399 bar = ""
400 sep = ""
401 for (j = 1; j <= i; j++) {
402 bar = bar sep position[j]
403 sep = " "
404 }
405 return bar
406 }
407
408 function make_status_energy( state, direction_of_change) {
409 state = db["energy_state_curr"]
410 if (state == "discharging") {
411 direction_of_change = "<"
412 } else if (state == "charging") {
413 direction_of_change = ">"
414 } else {
415 direction_of_change = "="
416 };
417 return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"])
418 }
419
420 function make_status_mem( total, used, percent, status) {
421 total = db["memory_total"]
422 used = db["memory_used"]
423 # To avoid division by zero when data is missing
424 if (total && used) {
425 percent = round((used / total) * 100)
426 status = sprintf("%d%%", percent)
427 } else {
428 status = "__"
429 }
430 return sprintf("M=%s", status)
431 }
432
433 function make_status_cpu( load, temp, fan) {
434 load = db["load_avg_1min"]
435 temp = db["temperature"] / 1000
436 fan = db["fan_speed"]
437 return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan)
438 }
439
440 function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) {
441 bytes_per_sector = 512
442 bytes_per_mb = 1024 * 1024
443 w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb
444 r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb
445 return \
446 sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r)
447 }
448
449 function make_status_net( \
450 out,
451 number_of_interfaces_to_show,
452 n,
453 array_of_prefixes_of_interfaces_to_show,
454 prefix,
455 interface,
456 label,
457 count_printed,
458 sep,
459 io_stat,
460 dw, dr,
461 bytes_per_unit\
462 ) {
463 out = ""
464 number_of_interfaces_to_show = \
465 split(\
466 opt_prefixes_of_net_interfaces_to_show,\
467 array_of_prefixes_of_interfaces_to_show,\
468 ","\
469 )
470 for (n = 1; n <= number_of_interfaces_to_show; n++) {
471 prefix = array_of_prefixes_of_interfaces_to_show[n]
472 for (interface in net_addr) {
473 if (interface ~ ("^" prefix)) {
474 label = substr(interface, 1, 1)
475 if (net_addr[interface]) {
476 bytes_per_mb = 1024 * 1024 # TODO: option
477 dw = net_io_diff_w[interface] / bytes_per_mb
478 dr = net_io_diff_r[interface] / bytes_per_mb
479 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
480 } else {
481 io_stat = "--"
482 }
483 if (interface ~ "^w") {
484 label = label ":" db["net_wifi_status", interface]
485 }
486 if (++count_printed > 1) {
487 sep = " "
488 } else {
489 sep = ""
490 }
491 out = out sep label ":" io_stat
492 }
493 }
494 }
495 return sprintf("N[%s]", out)
496 }
497
498 function make_status_mpd( state, status) {
499 state = db["mpd_status_state"]
500
501 if (state == "play") {
502 status = make_status_mpd_state_known("▶")
503 } else if (state == "pause") {
504 status = make_status_mpd_state_known("❚❚")
505 } else if (state == "stop") {
506 status = make_status_mpd_state_known("⬛")
507 } else {
508 status = make_status_mpd_state_unknown("--")
509 }
510
511 return sprintf("[%s]", status)
512 }
513
514 function make_status_mpd_state_known(symbol) {
515 return sprintf(\
516 "%s %s %s %s",
517 symbol,
518 db["mpd_status_time"],
519 db["mpd_status_percent"],
520 substr(db["mpd_playing_curr"], 1, opt_mpd_song_max_chars)\
521 )
522 }
523
524 function make_status_mpd_state_unknown(symbol) {
525 return sprintf("%s", symbol)
526 }
527
528 function round(n) {
529 return int(n + 0.5)
530 }
531
532 function debug(location, msg, values, sep, vals, key, payload) {
533 if (opt_debug) {
534 sep = ""
535 vals = ""
536 for (key in values) {
537 vals = sprintf("%s%s%s: %s", vals, sep, key, values[key])
538 sep = ", "
539 }
540 payload = \
541 sprintf("LOCATION[%s] MSG[%s] DATA[%s]", location, msg, vals)
542 output_msg("DEBUG", payload, "/dev/stderr")
543 }
544 }
545
546 function error(location, msg) {
547 # TODO: Reconsider classifying internal errors as alerts
548 # Maybe better to keep the error class distinct and provide a
549 # an optional transformation from error to alert
550 alert_trigger_hi(location, "KhatusControllerError", msg)
551 }
552
553 function ensure_numeric(n) {
554 return n + 0
555 }
556 #-------------------------------
557 # Why do we need ensure_numeric?
558 #-------------------------------
559 # awk appears to be guessing the type of an inputted scalar based on usage, so
560 # if we read-in a number, but did not use it in any numeric operations, but did
561 # use as a string (even in just a format string!) - it will be treated as a
562 # string and can lead to REALLY SURPRISING behavior in conditional statements,
563 # where smaller number may compare as greater than the bigger ones, such as.
564 #
565 # Demo:
566 #
567 # $ 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"}}'
568 # 75 < 100
569 # $ 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"}}'
570 # 75 > 100
571
572 # However, once used as a number, seems to stay that way even after being
573 # used as string:
574 #
575 # $ 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"}}'
576 # 75 < 100
577 #
578 # $ 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"}}'
579 # 75 < 100
580 #
581 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
582 # 75 < 100
583 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
584 # 75 > 100
This page took 0.086648 seconds and 4 git commands to generate.