3 /^OK
/ { debug
("OK line", $
0) }
6 debug
("ERROR line", $
0)
11 alert_trigger_hi
(msg_head
, "KhatusSensorError", msg_body
)
14 /^OK in
:ENERGY battery
/\
16 debug
("ENERGY battery", $
0)
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
()
24 /^OK in
:ENERGY line_power
/\
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
()
36 db
["memory_total"] = $
1
37 db
["memory_used"] = $
2
40 /^OK in
:FAN
+status
:/\
65 db
["temperature"] = $
1
86 db
["disk_space_used"] = $
0
96 /^OK in
:NET_WIFI_STATUS
/\
100 db
["net_wifi_status"] = $
0
103 /^OK in
:BLUETOOTH_POWER
/\
107 db
["bluetooth_power"] = $
0
110 /^OK in
:SCREEN_BRIGHTNESS
/\
114 set_screen_brightness
()
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 }
131 db
["mpd_status_state"] = $
1
132 db
["mpd_status_time"] = $
2
133 db
["mpd_status_percent"] = $
3
140 db
["weather_temperature"] = $
0
148 output_msg_status_bar
(make_status_bar
())
151 function set_volume
( mute
, left
, right
) {
152 # 0 RUNNING no 75% 75%
161 db
["volume"] =
sprintf("%s %s", left
, right
)
162 } else if (mute ==
"yes") {
165 error
("set_volume", "Unexpected value for 'mute' field: " mute
)
169 function set_mpd_song
( key
, val
) {
175 db_mpd_song
[key
] = val
176 debug
("set_mpd_song", "", db_mpd_song
)
179 function set_mpd_playing
( \
180 currently_playing
, name
, title
, file
, last
, parts\
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:"]
188 currently_playing = name
190 currently_playing = title
192 last =
split(file
, parts
, "/")
193 currently_playing = parts
[last
]
195 currently_playing =
""
197 db
["mpd_playing_prev"] = db
["mpd_playing_curr"]
198 db
["mpd_playing_curr"] = currently_playing
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:"]
212 db_mpd_song
["Artist:"] \
213 " - " db_mpd_song
["Album:"] \
214 " - " db_mpd_song
["Title:"]
216 alert_trigger_low
("alert_check_mpd", "MpdNowPlaying", body
)
220 # TODO: Generalize alert spec lang
221 # - trigger threshold
222 # - above/bellow/equal to threshold value
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\
229 from =
"alert_check_energy_battery"
231 state_curr = db
["energy_state_curr"]
232 state_prev = db
["energy_state_prev"]
233 remaining = db
["energy_percentage"]
235 dbg
["state_curr"] = state_curr
236 dbg
["remaining"] = remaining
239 if (state_curr ==
"discharging") {
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) {
250 body =
sprintf("%d%% Get the charger.", remaining
)
251 alert_trigger_hi
(from
, subj
, body
)
252 } else if (remaining
< 20) {
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
)
265 # TODO: Reconsider the competing global-state organizing-conventions
266 state__alerts__energy__notified_bellow_half =
0
270 function alert_check_energy_line_power
( \
271 from
, dbg
, line_power_curr
, line_power_prev
, subj
, body \
273 from =
"alert_check_energy_line_power"
275 dbg
["energy_line_power_prev"] = db
["energy_line_power_prev"]
276 dbg
["energy_line_power_curr"] = db
["energy_line_power_curr"]
279 line_power_curr = db
["energy_line_power_curr"]
280 line_power_prev = db
["energy_line_power_prev"]
282 if (line_power_curr ==
"no" && line_power_prev
!= "no") {
283 alert_trigger_low
(from
, "PowerUnplugged", "")
287 function alert_trigger_low
(from
, subject
, body
) {
288 alert_trigger
("low", from
, subject
, body
)
291 function alert_trigger_med
(from
, subject
, body
) {
292 alert_trigger
("med", from
, subject
, body
)
295 function alert_trigger_hi
(from
, subject
, body
) {
296 alert_trigger
("hi", from
, subject
, body
)
299 function alert_trigger
(priority
, from
, subject
, body
, msg
) {
300 # priority : "low" | "med" | "hi"
301 # subject : no spaces
303 msg =
sprintf("khatus_%s %s %s %s", from
, priority
, subject
, body
)
304 output_msg_alert
(msg
)
307 function output_msg_alert
(msg
) {
308 # TODO: Should alerts go into a dedicated channel?
309 output_msg
("ALERT", msg
, "/dev/stdout")
312 function output_msg_status_bar
(msg
) {
313 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
316 function output_msg
(type
, content
, channel
) {
317 print(type
, content
) > channel
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
330 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
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
341 function set_net_addr_io
( \
342 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
349 if (address
&& io_curr_w
&& io_curr_r
) {
351 io_prev_w = net_io_curr_w
[interface
]
352 io_prev_r = net_io_curr_r
[interface
]
354 net_addr
[interface
] = address
355 net_io_curr_w
[interface
] = io_curr_w
356 net_io_curr_r
[interface
] = io_curr_r
357 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
358 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
361 net_addr
[interface
] =
""
362 net_io_curr_w
[interface
] =
0
363 net_io_curr_r
[interface
] =
0
364 net_io_diff_w
[interface
] =
0
365 net_io_diff_r
[interface
] =
0
370 function set_screen_brightness
( max
, cur
) {
373 db
["screen_brightness"] =
(cur
/ max
) * 100
376 # TODO: Revise overuse of shift() where it is not really needed
381 function make_status_bar
( position
, bar
, sep
, i
, j
) {
382 position
[++i
] = make_status_energy
()
383 position
[++i
] = make_status_mem
()
384 position
[++i
] = make_status_cpu
()
385 position
[++i
] = make_status_disk
()
386 position
[++i
] = make_status_net
()
387 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
388 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
389 position
[++i
] =
sprintf("(%s)", db
["volume"])
390 position
[++i
] = make_status_mpd
()
391 position
[++i
] = db
["weather_temperature"]
392 position
[++i
] = db
["datetime"]
395 for (j =
1; j
<= i
; j
++) {
396 bar = bar sep position
[j
]
402 function make_status_energy
( state
, direction_of_change
) {
403 state = db
["energy_state_curr"]
404 if (state ==
"discharging") {
405 direction_of_change =
"<"
406 } else if (state ==
"charging") {
407 direction_of_change =
">"
409 direction_of_change =
"="
411 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
414 function make_status_mem
( total
, used
, percent
, status
) {
415 total = db
["memory_total"]
416 used = db
["memory_used"]
417 # To avoid division by zero when data is missing
419 percent = round
((used
/ total
) * 100)
420 status =
sprintf("%d%%", percent
)
424 return sprintf("M=%s", status
)
427 function make_status_cpu
( load
, temp
, fan
) {
428 load = db
["load_avg_1min"]
429 temp = db
["temperature"] / 1000
430 fan = db
["fan_speed"]
431 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
434 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
435 bytes_per_sector =
512
436 bytes_per_mb =
1024 * 1024
437 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
438 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
440 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
443 function make_status_net
( \
445 number_of_interfaces_to_show
,
447 array_of_prefixes_of_interfaces_to_show
,
458 number_of_interfaces_to_show = \
460 opt_prefixes_of_net_interfaces_to_show
,\
461 array_of_prefixes_of_interfaces_to_show
,\
464 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
465 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
466 for (interface in net_addr
) {
467 if (interface ~
("^" prefix
)) {
468 label =
substr(interface
, 1, 1)
469 if (net_addr
[interface
]) {
470 bytes_per_mb =
1024 * 1024 # TODO: option
471 dw = net_io_diff_w
[interface
] / bytes_per_mb
472 dr = net_io_diff_r
[interface
] / bytes_per_mb
473 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
477 if (interface ~
"^w") {
478 label = label
":" db
["net_wifi_status"]
480 if (++count_printed
> 1) {
485 out = out sep label
":" io_stat
489 return sprintf("N[%s]", out
)
492 function make_status_mpd
( state
, status
) {
493 state = db
["mpd_status_state"]
495 if (state ==
"play") {
496 status = make_status_mpd_state_known
("▶")
497 } else if (state ==
"pause") {
498 status = make_status_mpd_state_known
("❚❚")
499 } else if (state ==
"stop") {
500 status = make_status_mpd_state_known
("⬛")
502 status = make_status_mpd_state_unknown
("--")
505 return sprintf("[%s]", status
)
508 function make_status_mpd_state_known
(symbol
) {
512 db
["mpd_status_time"],
513 db
["mpd_status_percent"],
514 substr(db
["mpd_playing_curr"], 1, opt_mpd_song_max_chars
)\
518 function make_status_mpd_state_unknown
(symbol
) {
519 return sprintf("%s", symbol
)
526 function debug
(location
, msg
, values
, sep
, vals
, key
, payload
) {
530 for (key in values
) {
531 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
535 sprintf("LOCATION[%s] MSG[%s] DATA[%s]", location
, msg
, vals
)
536 output_msg
("DEBUG", payload
, "/dev/stderr")
540 function error
(location
, msg
) {
541 # TODO: Reconsider classifying internal errors as alerts
542 # Maybe better to keep the error class distinct and provide a
543 # an optional transformation from error to alert
544 alert_trigger_hi
(location
, "KhatusControllerError", msg
)
547 function ensure_numeric
(n
) {
550 #-------------------------------
551 # Why do we need ensure_numeric?
552 #-------------------------------
553 # awk appears to be guessing the type of an inputted scalar based on usage, so
554 # if we read-in a number, but did not use it in any numeric operations, but did
555 # use as a string (even in just a format string!) - it will be treated as a
556 # string and can lead to REALLY SURPRISING behavior in conditional statements,
557 # where smaller number may compare as greater than the bigger ones, such as.
561 # $ 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"}}'
563 # $ 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"}}'
566 # However, once used as a number, seems to stay that way even after being
569 # $ 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"}}'
572 # $ 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"}}'
575 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
577 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'