Rename split_msg_parts to fields_shift
[khatus.git] / bin / khatus_controller
1 #! /usr/bin/awk -f
2
3
4 /^in:ENERGY/\
5 {
6 fields_shift()
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 fields_shift()
17 db["memory_total"] = $1
18 db["memory_used"] = $2
19 }
20
21 /^in:FAN +status:/\
22 {
23 fields_shift()
24 db["fan_status"] = $2
25 }
26
27 /^in:FAN +speed:/\
28 {
29 fields_shift()
30 db["fan_speed"] = $2
31 }
32
33 /^in:FAN +level:/\
34 {
35 fields_shift()
36 db["fan_level"] = $2
37 }
38
39 /^in:TEMPERATURE/\
40 {
41 fields_shift()
42 db["temperature"] = $1
43 }
44
45 /^in:LOAD_AVG/\
46 {
47 fields_shift()
48 set_load_avg()
49 }
50
51 /^in:DISK_IO/\
52 {
53 fields_shift()
54 set_disk_io()
55 }
56
57 /^in:DISK_SPACE/\
58 {
59 fields_shift()
60 db["disk_space_used"] = $0
61 }
62
63 /^in:NET_ADDR_IO/\
64 {
65 fields_shift()
66 set_net_addr_io()
67 }
68
69 /^in:NET_WIFI_STATUS/\
70 {
71 fields_shift()
72 db["net_wifi_status"] = $0
73 }
74
75 /^in:BLUETOOTH_POWER/\
76 {
77 fields_shift()
78 db["bluetooth_power"] = $0
79 }
80
81 /^in:SCREEN_BRIGHTNESS/\
82 {
83 fields_shift()
84 set_screen_brightness()
85 }
86
87 /^in:VOLUME/\
88 {
89 fields_shift()
90 db["volume"] = $0
91 }
92
93 /^in:MPD_STATE/\
94 {
95 fields_shift()
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 fields_shift()
104 db["mpd_curr_song_name"] = $0
105 }
106
107 /^in:WEATHER/\
108 {
109 fields_shift()
110 db["weather_temperature"] = $0
111 }
112
113 /^in:DATE_TIME/\
114 {
115 fields_shift()
116 db["datetime"] = $0
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 fields_shift( dbg) {
264 dbg["head"] = $1
265 sub("^" $1 " +", "")
266 dbg["body"] = $0
267 debug("fields_shift", dbg)
268 }
269
270 function make_status_bar( position, bar, sep, i, j) {
271 position[++i] = make_status_energy()
272 position[++i] = make_status_mem()
273 position[++i] = make_status_cpu()
274 position[++i] = make_status_disk()
275 position[++i] = make_status_net()
276 position[++i] = sprintf("B=%s", db["bluetooth_power"])
277 position[++i] = sprintf("*%d%%", db["screen_brightness"])
278 position[++i] = sprintf("(%s)", db["volume"])
279 position[++i] = make_status_mpd()
280 position[++i] = db["weather_temperature"]
281 position[++i] = db["datetime"]
282 bar = ""
283 sep = ""
284 for (j = 1; j <= i; j++) {
285 bar = bar sep position[j]
286 sep = " "
287 }
288 return bar
289 }
290
291 function make_status_energy( state, direction_of_change) {
292 state = db["energy_state_curr"]
293 if (state == "discharging") {
294 direction_of_change = "<"
295 } else if (state == "charging") {
296 direction_of_change = ">"
297 } else {
298 direction_of_change = "="
299 };
300 return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"])
301 }
302
303 function make_status_mem( total, used, percent, status) {
304 total = db["memory_total"]
305 used = db["memory_used"]
306 # To avoid division by zero when data is missing
307 if (total && used) {
308 percent = round((used / total) * 100)
309 status = sprintf("%d%%", percent)
310 } else {
311 status = "__"
312 }
313 return sprintf("M=%s", status)
314 }
315
316 function make_status_cpu( load, temp, fan) {
317 load = db["load_avg_1min"]
318 temp = db["temperature"] / 1000
319 fan = db["fan_speed"]
320 return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan)
321 }
322
323 function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) {
324 bytes_per_sector = 512
325 bytes_per_mb = 1024 * 1024
326 w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb
327 r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb
328 return \
329 sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r)
330 }
331
332 function make_status_net( \
333 out,
334 number_of_interfaces_to_show,
335 n,
336 array_of_prefixes_of_interfaces_to_show,
337 prefix,
338 interface,
339 label,
340 count_printed,
341 sep,
342 io_stat,
343 dw, dr,
344 bytes_per_unit\
345 ) {
346 out = ""
347 number_of_interfaces_to_show = \
348 split(\
349 opt_prefixes_of_net_interfaces_to_show,\
350 array_of_prefixes_of_interfaces_to_show,\
351 ","\
352 )
353 for (n = 1; n <= number_of_interfaces_to_show; n++) {
354 prefix = array_of_prefixes_of_interfaces_to_show[n]
355 for (interface in net_addr) {
356 if (interface ~ ("^" prefix)) {
357 label = substr(interface, 1, 1)
358 if (net_addr[interface]) {
359 bytes_per_mb = 1024 * 1024 # TODO: option
360 dw = net_io_diff_w[interface] / bytes_per_mb
361 dr = net_io_diff_r[interface] / bytes_per_mb
362 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
363 } else {
364 io_stat = "--"
365 }
366 if (interface ~ "^w") {
367 label = label ":" db["net_wifi_status"]
368 }
369 if (++count_printed > 1) {
370 sep = " "
371 } else {
372 sep = ""
373 }
374 out = out sep label ":" io_stat
375 }
376 }
377 }
378 return sprintf("N[%s]", out)
379 }
380
381 function make_status_mpd( state, status) {
382 state = db["mpd_state"]
383
384 if (state == "play") {
385 status = make_status_mpd_state_known("▶")
386 } else if (state == "pause") {
387 status = make_status_mpd_state_known("❚❚")
388 } else if (state == "stop") {
389 status = make_status_mpd_state_known("⬛")
390 } else {
391 status = make_status_mpd_state_unknown("--")
392 }
393
394 return sprintf("[%s]", status)
395 }
396
397 function make_status_mpd_state_known(symbol) {
398 return sprintf(\
399 "%s %s %s %s",
400 symbol,
401 db["mpd_curr_song_time"],
402 db["mpd_curr_song_percent"],
403 substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
404 )
405 }
406
407 function make_status_mpd_state_unknown(symbol) {
408 return sprintf("%s", symbol)
409 }
410
411 function round(n) {
412 return int(n + 0.5)
413 }
414
415 function debug(location, values, sep, vals, key, msg) {
416 if (opt_debug) {
417 sep = ""
418 vals = ""
419 for (key in values) {
420 vals = sprintf("%s%s%s: %s", vals, sep, key, values[key])
421 sep = ", "
422 }
423 msg = location " ==> " vals "."
424 output_msg("DEBUG", msg, "/dev/stderr")
425 }
426 }
427
428 function ensure_numeric(n) {
429 return n + 0
430 }
431 #-------------------------------
432 # Why do we need ensure_numeric?
433 #-------------------------------
434 # awk appears to be guessing the type of an inputted scalar based on usage, so
435 # if we read-in a number, but did not use it in any numeric operations, but did
436 # use as a string (even in just a format string!) - it will be treated as a
437 # string and can lead to REALLY SURPRISING behavior in conditional statements,
438 # where smaller number may compare as greater than the bigger ones, such as.
439 #
440 # Demo:
441 #
442 # $ 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"}}'
443 # 75 < 100
444 # $ 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"}}'
445 # 75 > 100
446
447 # However, once used as a number, seems to stay that way even after being
448 # used as string:
449 #
450 # $ 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"}}'
451 # 75 < 100
452 #
453 # $ 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"}}'
454 # 75 < 100
455 #
456 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
457 # 75 < 100
458 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
459 # 75 > 100
This page took 0.073629 seconds and 5 git commands to generate.