7 debug
("ENERGY battery", $
0)
9 db
["energy_state_prev"] = db
["energy_state_curr"]
10 db
["energy_state_curr"] = $
3
11 db
["energy_percentage"] = ensure_numeric
($
4)
12 alert_check_energy_battery
()
15 /^in
:ENERGY line_power
/\
17 debug
("ENERGY line_power", $
0)
18 db
["energy_line_power_prev"] = db
["energy_line_power_curr"]
19 db
["energy_line_power_curr"] = $
3
20 alert_check_energy_line_power
()
26 db
["memory_total"] = $
1
27 db
["memory_used"] = $
2
51 db
["temperature"] = $
1
69 db
["disk_space_used"] = $
0
78 /^in
:NET_WIFI_STATUS
/\
81 db
["net_wifi_status"] = $
0
84 /^in
:BLUETOOTH_POWER
/\
87 db
["bluetooth_power"] = $
0
90 /^in
:SCREEN_BRIGHTNESS
/\
93 set_screen_brightness
()
101 /^in
:MPD_SONG OK
+MPD
/ { delete db_mpd_song
; next }
102 /^in
:MPD_SONG OK$
/ { set_mpd_playing
() ; next }
103 /^in
:MPD_SONG
/ { set_mpd_song
() ; next }
108 db
["mpd_status_state"] = $
1
109 db
["mpd_status_time"] = $
2
110 db
["mpd_status_percent"] = $
3
116 db
["weather_temperature"] = $
0
123 output_msg_status_bar
(make_status_bar
())
126 function set_volume
( mute
, left
, right
) {
127 # 0 RUNNING no 75% 75%
136 db
["volume"] =
sprintf("%s %s", left
, right
)
137 } else if (mute ==
"yes") {
140 # TODO: Log error - unexpected mute value
144 function set_mpd_song
( key
, val
) {
149 db_mpd_song
[key
] = val
150 debug
("set_mpd_song", "", db_mpd_song
)
153 function set_mpd_playing
( \
154 currently_playing
, name
, title
, file
, last
, parts\
156 debug
("set_mpd_playing", "", db_mpd_song
)
157 name = db_mpd_song
["Name:"]
158 title = db_mpd_song
["Title:"]
159 file = db_mpd_song
["file:"]
162 currently_playing = name
164 currently_playing = title
166 last =
split(file
, parts
, "/")
167 currently_playing = parts
[last
]
169 currently_playing =
""
171 db
["mpd_playing_prev"] = db
["mpd_playing_curr"]
172 db
["mpd_playing_curr"] = currently_playing
177 function alert_check_mpd
( curr
, prev
, name
, body
) {
178 prev = db
["mpd_playing_prev"]
179 curr = db
["mpd_playing_curr"]
180 if (curr
&& curr
!= prev
) {
181 name = db_mpd_song
["Name:"]
186 db_mpd_song
["Artist:"] \
187 " - " db_mpd_song
["Album:"] \
188 " - " db_mpd_song
["Title:"]
190 alert_trigger_low
("alert_check_mpd", "NowPlaying", body
)
194 # TODO: Generalize alert spec lang
195 # - trigger threshold
196 # - above/bellow/equal to threshold value
198 # - snooze time (if already alerted, when to re-alert?)
199 # - text: subject/body
200 function alert_check_energy_battery
( \
201 from
, dbg
, state_curr
, state_prev
, remaining
, subj
, body\
203 from =
"alert_check_energy_battery"
205 state_curr = db
["energy_state_curr"]
206 state_prev = db
["energy_state_prev"]
207 remaining = db
["energy_percentage"]
209 dbg
["state_curr"] = state_curr
210 dbg
["remaining"] = remaining
213 if (state_curr ==
"discharging") {
215 subj =
"Energy_CRITICALLY_Low"
216 body =
sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining
)
217 alert_trigger_hi
(from
, subj
, body
)
218 } else if (remaining
< 10) {
219 subj =
"Energy_Very_Low"
220 body =
sprintf("%d%% Plug it in ASAP.", remaining
)
221 alert_trigger_hi
(from
, subj
, body
)
222 } else if (remaining
< 15) {
224 body =
sprintf("%d%% Get the charger.", remaining
)
225 alert_trigger_hi
(from
, subj
, body
)
226 } else if (remaining
< 20) {
228 body =
sprintf("%d%% Get the charger.", remaining
)
229 alert_trigger_med
(from
, subj
, body
)
230 } else if (remaining
< 50) {
231 if (!state__alerts__energy__notified_bellow_half
) {
232 state__alerts__energy__notified_bellow_half =
1
233 subj =
"Energy_Bellow_Half"
234 body =
sprintf("%d%% Where is the charger?", remaining
)
235 alert_trigger_med
(from
, subj
, body
)
239 # TODO: Reconsider the competing global-state organizing-conventions
240 state__alerts__energy__notified_bellow_half =
0
244 function alert_check_energy_line_power
( \
245 from
, dbg
, line_power_curr
, line_power_prev
, subj
, body \
247 from =
"alert_check_energy_line_power"
249 dbg
["energy_line_power_prev"] = db
["energy_line_power_prev"]
250 dbg
["energy_line_power_curr"] = db
["energy_line_power_curr"]
253 line_power_curr = db
["energy_line_power_curr"]
254 line_power_prev = db
["energy_line_power_prev"]
256 if (line_power_curr ==
"no" && line_power_prev
!= "no") {
257 alert_trigger_low
(from
, "PowerUnplugged", "")
261 function alert_trigger_low
(from
, subject
, body
) {
262 alert_trigger
("low", from
, subject
, body
)
265 function alert_trigger_med
(from
, subject
, body
) {
266 alert_trigger
("med", from
, subject
, body
)
269 function alert_trigger_hi
(from
, subject
, body
) {
270 alert_trigger
("hi", from
, subject
, body
)
273 function alert_trigger
(priority
, from
, subject
, body
, msg
) {
274 # priority : "low" | "med" | "hi"
275 # subject : no spaces
277 msg =
sprintf("khatus_%s %s %s %s", from
, priority
, subject
, body
)
278 output_msg_alert
(msg
)
281 function output_msg_alert
(msg
) {
282 # TODO: Should alerts go into a dedicated channel?
283 output_msg
("ALERT", msg
, "/dev/stdout")
286 function output_msg_status_bar
(msg
) {
287 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
290 function output_msg
(type
, content
, channel
) {
291 print(type
, content
) > channel
294 function set_load_avg
( sched
) {
295 split($
4, sched
, "/")
296 db
["load_avg_1min"] = $
1
297 db
["load_avg_5min"] = $
2
298 db
["load_avg_15min"] = $
3
299 db
["kern_sched_queue_runnable"] = sched
[1]
300 db
["kern_sched_queue_total"] = sched
[2]
301 db
["kern_sched_latest_pid"] = $
5
304 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
307 prev_w = db
["disk_io_curr_w"]
308 prev_r = db
["disk_io_curr_r"]
309 db
["disk_io_curr_w"] = curr_w
310 db
["disk_io_curr_r"] = curr_r
311 db
["disk_io_diff_w"] = curr_w
- prev_w
312 db
["disk_io_diff_r"] = curr_r
- prev_r
315 function set_net_addr_io
( \
316 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
323 if (address
&& io_curr_w
&& io_curr_r
) {
325 io_prev_w = net_io_curr_w
[interface
]
326 io_prev_r = net_io_curr_r
[interface
]
328 net_addr
[interface
] = address
329 net_io_curr_w
[interface
] = io_curr_w
330 net_io_curr_r
[interface
] = io_curr_r
331 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
332 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
335 net_addr
[interface
] =
""
336 net_io_curr_w
[interface
] =
0
337 net_io_curr_r
[interface
] =
0
338 net_io_diff_w
[interface
] =
0
339 net_io_diff_r
[interface
] =
0
344 function set_screen_brightness
( max
, cur
) {
347 db
["screen_brightness"] =
(cur
/ max
) * 100
350 # TODO: Revise overuse of shift() where it is not really needed
355 function make_status_bar
( position
, bar
, sep
, i
, j
) {
356 position
[++i
] = make_status_energy
()
357 position
[++i
] = make_status_mem
()
358 position
[++i
] = make_status_cpu
()
359 position
[++i
] = make_status_disk
()
360 position
[++i
] = make_status_net
()
361 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
362 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
363 position
[++i
] =
sprintf("(%s)", db
["volume"])
364 position
[++i
] = make_status_mpd
()
365 position
[++i
] = db
["weather_temperature"]
366 position
[++i
] = db
["datetime"]
369 for (j =
1; j
<= i
; j
++) {
370 bar = bar sep position
[j
]
376 function make_status_energy
( state
, direction_of_change
) {
377 state = db
["energy_state_curr"]
378 if (state ==
"discharging") {
379 direction_of_change =
"<"
380 } else if (state ==
"charging") {
381 direction_of_change =
">"
383 direction_of_change =
"="
385 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
388 function make_status_mem
( total
, used
, percent
, status
) {
389 total = db
["memory_total"]
390 used = db
["memory_used"]
391 # To avoid division by zero when data is missing
393 percent = round
((used
/ total
) * 100)
394 status =
sprintf("%d%%", percent
)
398 return sprintf("M=%s", status
)
401 function make_status_cpu
( load
, temp
, fan
) {
402 load = db
["load_avg_1min"]
403 temp = db
["temperature"] / 1000
404 fan = db
["fan_speed"]
405 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
408 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
409 bytes_per_sector =
512
410 bytes_per_mb =
1024 * 1024
411 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
412 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
414 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
417 function make_status_net
( \
419 number_of_interfaces_to_show
,
421 array_of_prefixes_of_interfaces_to_show
,
432 number_of_interfaces_to_show = \
434 opt_prefixes_of_net_interfaces_to_show
,\
435 array_of_prefixes_of_interfaces_to_show
,\
438 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
439 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
440 for (interface in net_addr
) {
441 if (interface ~
("^" prefix
)) {
442 label =
substr(interface
, 1, 1)
443 if (net_addr
[interface
]) {
444 bytes_per_mb =
1024 * 1024 # TODO: option
445 dw = net_io_diff_w
[interface
] / bytes_per_mb
446 dr = net_io_diff_r
[interface
] / bytes_per_mb
447 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
451 if (interface ~
"^w") {
452 label = label
":" db
["net_wifi_status"]
454 if (++count_printed
> 1) {
459 out = out sep label
":" io_stat
463 return sprintf("N[%s]", out
)
466 function make_status_mpd
( state
, status
) {
467 state = db
["mpd_status_state"]
469 if (state ==
"play") {
470 status = make_status_mpd_state_known
("▶")
471 } else if (state ==
"pause") {
472 status = make_status_mpd_state_known
("❚❚")
473 } else if (state ==
"stop") {
474 status = make_status_mpd_state_known
("⬛")
476 status = make_status_mpd_state_unknown
("--")
479 return sprintf("[%s]", status
)
482 function make_status_mpd_state_known
(symbol
) {
486 db
["mpd_status_time"],
487 db
["mpd_status_percent"],
488 substr(db
["mpd_playing_curr"], 1, opt_mpd_song_max_chars
)\
492 function make_status_mpd_state_unknown
(symbol
) {
493 return sprintf("%s", symbol
)
500 function debug
(location
, msg
, values
, sep
, vals
, key
, payload
) {
504 for (key in values
) {
505 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
509 sprintf("LOCATION[%s] MSG[%s] DATA[%s]", location
, msg
, vals
)
510 output_msg
("DEBUG", payload
, "/dev/stderr")
514 function ensure_numeric
(n
) {
517 #-------------------------------
518 # Why do we need ensure_numeric?
519 #-------------------------------
520 # awk appears to be guessing the type of an inputted scalar based on usage, so
521 # if we read-in a number, but did not use it in any numeric operations, but did
522 # use as a string (even in just a format string!) - it will be treated as a
523 # string and can lead to REALLY SURPRISING behavior in conditional statements,
524 # where smaller number may compare as greater than the bigger ones, such as.
528 # $ 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"}}'
530 # $ 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"}}'
533 # However, once used as a number, seems to stay that way even after being
536 # $ 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"}}'
539 # $ 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"}}'
542 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
544 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'