Include src alert name in alert msg
[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 < 50) {
149 if (!state__alerts__energy__notified_bellow_half) {
150 state__alerts__energy__notified_bellow_half = 1
151 subj = "Energy_Bellow_Half"
152 body = sprintf("%d%% Where is the charger?", remaining)
153 alert_trigger_med(from, subj, body)
154 }
155 }
156 } else {
157 # TODO: Reconsider the competing global-state organizing-conventions
158 state__alerts__energy__notified_bellow_half = 0
159 }
160 }
161
162 function alert_trigger_low(from, subject, body) {
163 alert_trigger("low", from, subject, body)
164 }
165
166 function alert_trigger_med(from, subject, body) {
167 alert_trigger("med", from, subject, body)
168 }
169
170 function alert_trigger_hi(from, subject, body) {
171 alert_trigger("hi", from, subject, body)
172 }
173
174 function alert_trigger(priority, from, subject, body, msg) {
175 # priority : "low" | "med" | "hi"
176 # subject : no spaces
177 # body : anything
178 msg = sprintf("khatus_%s %s %s %s", from, priority, subject, body)
179 output_msg_alert(msg)
180 }
181
182 function output_msg_alert(msg) {
183 # TODO: Should alerts go into a dedicated channel?
184 output_msg("ALERT", msg, "/dev/stdout")
185 }
186
187 function output_msg_status_bar(msg) {
188 output_msg("STATUS_BAR", msg, "/dev/stdout")
189 }
190
191 function output_msg(type, content, channel) {
192 print(type, content) > channel
193 }
194
195 function set_load_avg( sched) {
196 split($4, sched, "/")
197 db["load_avg_1min"] = $1
198 db["load_avg_5min"] = $2
199 db["load_avg_15min"] = $3
200 db["kern_sched_queue_runnable"] = sched[1]
201 db["kern_sched_queue_total"] = sched[2]
202 db["kern_sched_latest_pid"] = $5
203 }
204
205 function set_disk_io( curr_w, curr_r, prev_w, prev_r) {
206 curr_w = $1
207 curr_r = $2
208 prev_w = db["disk_io_curr_w"]
209 prev_r = db["disk_io_curr_r"]
210 db["disk_io_curr_w"] = curr_w
211 db["disk_io_curr_r"] = curr_r
212 db["disk_io_diff_w"] = curr_w - prev_w
213 db["disk_io_diff_r"] = curr_r - prev_r
214 }
215
216 function set_net_addr_io( \
217 interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\
218 ) {
219 interface = $1
220 address = $2
221 io_curr_w = $3
222 io_curr_r = $4
223 if (interface) {
224 if (address && io_curr_w && io_curr_r) {
225 # recalculate
226 io_prev_w = net_io_curr_w[interface]
227 io_prev_r = net_io_curr_r[interface]
228
229 net_addr[interface] = address
230 net_io_curr_w[interface] = io_curr_w
231 net_io_curr_r[interface] = io_curr_r
232 net_io_diff_w[interface] = io_curr_w - io_prev_w
233 net_io_diff_r[interface] = io_curr_r - io_prev_r
234 } else {
235 # clear
236 net_addr[interface] = ""
237 net_io_curr_w[interface] = 0
238 net_io_curr_r[interface] = 0
239 net_io_diff_w[interface] = 0
240 net_io_diff_r[interface] = 0
241 }
242 }
243 }
244
245 function set_screen_brightness( max, cur) {
246 max = $1
247 cur = $2
248 db["screen_brightness"] = (cur / max) * 100
249 }
250
251 function split_msg_parts( dbg) {
252 msg_head = $1
253 sub("^" msg_head " +", "")
254 msg_body = $0
255 dbg["msg_head"] = msg_head
256 dbg["msg_body"] = msg_body
257 debug("split_msg_parts", dbg)
258 }
259
260 function make_status_bar( position, bar, sep, i, j) {
261 position[++i] = make_status_energy()
262 position[++i] = make_status_mem()
263 position[++i] = make_status_cpu()
264 position[++i] = make_status_disk()
265 position[++i] = make_status_net()
266 position[++i] = sprintf("B=%s", db["bluetooth_power"])
267 position[++i] = sprintf("*%d%%", db["screen_brightness"])
268 position[++i] = sprintf("(%s)", db["volume"])
269 position[++i] = make_status_mpd()
270 position[++i] = db["weather_temperature"]
271 position[++i] = db["datetime"]
272 bar = ""
273 sep = ""
274 for (j = 1; j <= i; j++) {
275 bar = bar sep position[j]
276 sep = " "
277 }
278 return bar
279 }
280
281 function make_status_energy( state, direction_of_change) {
282 state = db["energy_state"]
283 if (state == "discharging") {
284 direction_of_change = "<"
285 } else if (state == "charging") {
286 direction_of_change = ">"
287 } else {
288 direction_of_change = "="
289 };
290 return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"])
291 }
292
293 function make_status_mem( total, used, percent, status) {
294 total = db["memory_total"]
295 used = db["memory_used"]
296 # To avoid division by zero when data is missing
297 if (total && used) {
298 percent = round((used / total) * 100)
299 status = sprintf("%d%%", percent)
300 } else {
301 status = "__"
302 }
303 return sprintf("M=%s", status)
304 }
305
306 function make_status_cpu( load, temp, fan) {
307 load = db["load_avg_1min"]
308 temp = db["temperature"] / 1000
309 fan = db["fan_speed"]
310 return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan)
311 }
312
313 function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) {
314 bytes_per_sector = 512
315 bytes_per_mb = 1024 * 1024
316 w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb
317 r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb
318 return \
319 sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r)
320 }
321
322 function make_status_net( \
323 out,
324 number_of_interfaces_to_show,
325 n,
326 array_of_prefixes_of_interfaces_to_show,
327 prefix,
328 interface,
329 label,
330 count_printed,
331 sep,
332 io_stat,
333 dw, dr,
334 bytes_per_unit\
335 ) {
336 out = ""
337 number_of_interfaces_to_show = \
338 split(\
339 opt_prefixes_of_net_interfaces_to_show,\
340 array_of_prefixes_of_interfaces_to_show,\
341 ","\
342 )
343 for (n = 1; n <= number_of_interfaces_to_show; n++) {
344 prefix = array_of_prefixes_of_interfaces_to_show[n]
345 for (interface in net_addr) {
346 if (interface ~ ("^" prefix)) {
347 label = substr(interface, 1, 1)
348 if (net_addr[interface]) {
349 bytes_per_mb = 1024 * 1024 # TODO: option
350 dw = net_io_diff_w[interface] / bytes_per_mb
351 dr = net_io_diff_r[interface] / bytes_per_mb
352 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
353 } else {
354 io_stat = "--"
355 }
356 if (interface ~ "^w") {
357 label = label ":" db["net_wifi_status"]
358 }
359 if (++count_printed > 1) {
360 sep = " "
361 } else {
362 sep = ""
363 }
364 out = out sep label ":" io_stat
365 }
366 }
367 }
368 return sprintf("N[%s]", out)
369 }
370
371 function make_status_mpd( state, status) {
372 state = db["mpd_state"]
373
374 if (state == "play") {
375 status = make_status_mpd_state_known("▶")
376 } else if (state == "pause") {
377 status = make_status_mpd_state_known("❚❚")
378 } else if (state == "stop") {
379 status = make_status_mpd_state_known("⬛")
380 } else {
381 status = make_status_mpd_state_unknown("--")
382 }
383
384 return sprintf("[%s]", status)
385 }
386
387 function make_status_mpd_state_known(symbol) {
388 return sprintf(\
389 "%s %s %s %s",
390 symbol,
391 db["mpd_curr_song_time"],
392 db["mpd_curr_song_percent"],
393 substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
394 )
395 }
396
397 function make_status_mpd_state_unknown(symbol) {
398 return sprintf("%s", symbol)
399 }
400
401 function round(n) {
402 return int(n + 0.5)
403 }
404
405 function debug(location, values, sep, vals, key, msg) {
406 if (opt_debug) {
407 sep = ""
408 vals = ""
409 for (key in values) {
410 vals = sprintf("%s%s%s: %s", vals, sep, key, values[key])
411 sep = ", "
412 }
413 msg = location " ==> " vals "."
414 output_msg("DEBUG", msg, "/dev/stderr")
415 }
416 }
417
418 function ensure_numeric(n) {
419 return n + 0
420 }
421 #-------------------------------
422 # Why do we need ensure_numeric?
423 #-------------------------------
424 # awk appears to be guessing the type of an inputted scalar based on usage, so
425 # if we read-in a number, but did not use it in any numeric operations, but did
426 # use as a string (even in just a format string!) - it will be treated as a
427 # string and can lead to REALLY SURPRISING behavior in conditional statements,
428 # where smaller number may compare as greater than the bigger ones, such as.
429 #
430 # Demo:
431 #
432 # $ 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"}}'
433 # 75 < 100
434 # $ 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"}}'
435 # 75 > 100
436
437 # However, once used as a number, seems to stay that way even after being
438 # used as string:
439 #
440 # $ 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"}}'
441 # 75 < 100
442 #
443 # $ 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"}}'
444 # 75 < 100
445 #
446 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
447 # 75 < 100
448 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
449 # 75 > 100
This page took 0.085216 seconds and 5 git commands to generate.