8 db
["energy_state_prev"] = db
["energy_state_curr"]
9 db
["energy_state_curr"] = $
1
10 db
["energy_percentage"] = ensure_numeric
($
2)
17 db
["memory_total"] = $
1
18 db
["memory_used"] = $
2
42 db
["temperature"] = $
1
60 db
["disk_space_used"] = $
0
69 /^in
:NET_WIFI_STATUS
/\
72 db
["net_wifi_status"] = $
0
75 /^in
:BLUETOOTH_POWER
/\
78 db
["bluetooth_power"] = $
0
81 /^in
:SCREEN_BRIGHTNESS
/\
84 set_screen_brightness
()
93 # -----------------------------------------------------------------------------
97 delete db
["mpd_song_curr_file"]
98 delete db
["mpd_song_curr_title"]
99 delete db
["mpd_song_curr_name"]
102 /^in
:MPD_SONG
+file
:/\
106 db
["mpd_song_curr_file"] = $
0
109 /^in
:MPD_SONG
+Title
:/\
113 db
["mpd_song_curr_title"] = $
0
116 /^in
:MPD_SONG
+Name
:/\
120 db
["mpd_song_curr_name"] = $
0
128 # -----------------------------------------------------------------------------
133 db
["mpd_status_state"] = $
1
134 db
["mpd_status_time"] = $
2
135 db
["mpd_status_percent"] = $
3
141 db
["weather_temperature"] = $
0
148 output_msg_status_bar
(make_status_bar
())
151 function set_mpd_playing
( \
152 currently_playing
, name
, title
, file
, last
, parts\
154 name = db
["mpd_song_curr_name"]
155 title = db
["mpd_song_curr_title"]
156 file = db
["mpd_song_curr_file"]
159 currently_playing = name
161 currently_playing = title
163 last =
split(file
, parts
, "/")
164 currently_playing = parts
[last
]
166 currently_playing =
""
168 db
["mpd_playing_prev"] = db
["mpd_playing_curr"]
169 db
["mpd_playing_curr"] = currently_playing
174 function alert_check_mpd
( curr
, prev
) {
175 prev = db
["mpd_playing_prev"]
176 curr = db
["mpd_playing_curr"]
177 if (curr
&& curr
!= prev
) {
178 alert_trigger_low
("alert_check_mpd", "NowPlaying", curr
)
182 # TODO: Generalize alert spec lang
183 # - trigger threshold
184 # - above/bellow/equal to threshold value
186 # - snooze time (if already alerted, when to re-alert?)
187 # - text: subject/body
188 function alert_check_energy
( \
189 from
, dbg
, state_curr
, state_prev
, remaining
, subj
, body\
191 from =
"alert_check_energy"
193 state_curr = db
["energy_state_curr"]
194 state_prev = db
["energy_state_prev"]
195 remaining = db
["energy_percentage"]
197 dbg
["state_curr"] = state_curr
198 dbg
["remaining"] = remaining
201 if (state_curr ==
"discharging") {
202 if (state_prev ==
"charging") {
203 alert_trigger_low
(from
, "Unplugged", "")
207 subj =
"Energy_CRITICALLY_Low"
208 body =
sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining
)
209 alert_trigger_hi
(from
, subj
, body
)
210 } else if (remaining
< 10) {
211 subj =
"Energy_Very_Low"
212 body =
sprintf("%d%% Plug it in ASAP.", remaining
)
213 alert_trigger_hi
(from
, subj
, body
)
214 } else if (remaining
< 15) {
216 body =
sprintf("%d%% Get the charger.", remaining
)
217 alert_trigger_hi
(from
, subj
, body
)
218 } else if (remaining
< 20) {
220 body =
sprintf("%d%% Get the charger.", remaining
)
221 alert_trigger_med
(from
, subj
, body
)
222 } else if (remaining
< 50) {
223 if (!state__alerts__energy__notified_bellow_half
) {
224 state__alerts__energy__notified_bellow_half =
1
225 subj =
"Energy_Bellow_Half"
226 body =
sprintf("%d%% Where is the charger?", remaining
)
227 alert_trigger_med
(from
, subj
, body
)
231 # TODO: Reconsider the competing global-state organizing-conventions
232 state__alerts__energy__notified_bellow_half =
0
236 function alert_trigger_low
(from
, subject
, body
) {
237 alert_trigger
("low", from
, subject
, body
)
240 function alert_trigger_med
(from
, subject
, body
) {
241 alert_trigger
("med", from
, subject
, body
)
244 function alert_trigger_hi
(from
, subject
, body
) {
245 alert_trigger
("hi", from
, subject
, body
)
248 function alert_trigger
(priority
, from
, subject
, body
, msg
) {
249 # priority : "low" | "med" | "hi"
250 # subject : no spaces
252 msg =
sprintf("khatus_%s %s %s %s", from
, priority
, subject
, body
)
253 output_msg_alert
(msg
)
256 function output_msg_alert
(msg
) {
257 # TODO: Should alerts go into a dedicated channel?
258 output_msg
("ALERT", msg
, "/dev/stdout")
261 function output_msg_status_bar
(msg
) {
262 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
265 function output_msg
(type
, content
, channel
) {
266 print(type
, content
) > channel
269 function set_load_avg
( sched
) {
270 split($
4, sched
, "/")
271 db
["load_avg_1min"] = $
1
272 db
["load_avg_5min"] = $
2
273 db
["load_avg_15min"] = $
3
274 db
["kern_sched_queue_runnable"] = sched
[1]
275 db
["kern_sched_queue_total"] = sched
[2]
276 db
["kern_sched_latest_pid"] = $
5
279 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
282 prev_w = db
["disk_io_curr_w"]
283 prev_r = db
["disk_io_curr_r"]
284 db
["disk_io_curr_w"] = curr_w
285 db
["disk_io_curr_r"] = curr_r
286 db
["disk_io_diff_w"] = curr_w
- prev_w
287 db
["disk_io_diff_r"] = curr_r
- prev_r
290 function set_net_addr_io
( \
291 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
298 if (address
&& io_curr_w
&& io_curr_r
) {
300 io_prev_w = net_io_curr_w
[interface
]
301 io_prev_r = net_io_curr_r
[interface
]
303 net_addr
[interface
] = address
304 net_io_curr_w
[interface
] = io_curr_w
305 net_io_curr_r
[interface
] = io_curr_r
306 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
307 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
310 net_addr
[interface
] =
""
311 net_io_curr_w
[interface
] =
0
312 net_io_curr_r
[interface
] =
0
313 net_io_diff_w
[interface
] =
0
314 net_io_diff_r
[interface
] =
0
319 function set_screen_brightness
( max
, cur
) {
322 db
["screen_brightness"] =
(cur
/ max
) * 100
325 function fields_shift
( dbg
) {
329 debug
("fields_shift", dbg
)
332 function make_status_bar
( position
, bar
, sep
, i
, j
) {
333 position
[++i
] = make_status_energy
()
334 position
[++i
] = make_status_mem
()
335 position
[++i
] = make_status_cpu
()
336 position
[++i
] = make_status_disk
()
337 position
[++i
] = make_status_net
()
338 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
339 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
340 position
[++i
] =
sprintf("(%s)", db
["volume"])
341 position
[++i
] = make_status_mpd
()
342 position
[++i
] = db
["weather_temperature"]
343 position
[++i
] = db
["datetime"]
346 for (j =
1; j
<= i
; j
++) {
347 bar = bar sep position
[j
]
353 function make_status_energy
( state
, direction_of_change
) {
354 state = db
["energy_state_curr"]
355 if (state ==
"discharging") {
356 direction_of_change =
"<"
357 } else if (state ==
"charging") {
358 direction_of_change =
">"
360 direction_of_change =
"="
362 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
365 function make_status_mem
( total
, used
, percent
, status
) {
366 total = db
["memory_total"]
367 used = db
["memory_used"]
368 # To avoid division by zero when data is missing
370 percent = round
((used
/ total
) * 100)
371 status =
sprintf("%d%%", percent
)
375 return sprintf("M=%s", status
)
378 function make_status_cpu
( load
, temp
, fan
) {
379 load = db
["load_avg_1min"]
380 temp = db
["temperature"] / 1000
381 fan = db
["fan_speed"]
382 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
385 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
386 bytes_per_sector =
512
387 bytes_per_mb =
1024 * 1024
388 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
389 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
391 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
394 function make_status_net
( \
396 number_of_interfaces_to_show
,
398 array_of_prefixes_of_interfaces_to_show
,
409 number_of_interfaces_to_show = \
411 opt_prefixes_of_net_interfaces_to_show
,\
412 array_of_prefixes_of_interfaces_to_show
,\
415 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
416 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
417 for (interface in net_addr
) {
418 if (interface ~
("^" prefix
)) {
419 label =
substr(interface
, 1, 1)
420 if (net_addr
[interface
]) {
421 bytes_per_mb =
1024 * 1024 # TODO: option
422 dw = net_io_diff_w
[interface
] / bytes_per_mb
423 dr = net_io_diff_r
[interface
] / bytes_per_mb
424 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
428 if (interface ~
"^w") {
429 label = label
":" db
["net_wifi_status"]
431 if (++count_printed
> 1) {
436 out = out sep label
":" io_stat
440 return sprintf("N[%s]", out
)
443 function make_status_mpd
( state
, status
) {
444 state = db
["mpd_status_state"]
446 if (state ==
"play") {
447 status = make_status_mpd_state_known
("▶")
448 } else if (state ==
"pause") {
449 status = make_status_mpd_state_known
("❚❚")
450 } else if (state ==
"stop") {
451 status = make_status_mpd_state_known
("⬛")
453 status = make_status_mpd_state_unknown
("--")
456 return sprintf("[%s]", status
)
459 function make_status_mpd_state_known
(symbol
) {
463 db
["mpd_status_time"],
464 db
["mpd_status_percent"],
465 substr(db
["mpd_playing_curr"], 1, opt_mpd_song_max_chars
)\
469 function make_status_mpd_state_unknown
(symbol
) {
470 return sprintf("%s", symbol
)
477 function debug
(location
, values
, sep
, vals
, key
, msg
) {
481 for (key in values
) {
482 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
485 msg = location
" ==> " vals
"."
486 output_msg
("DEBUG", msg
, "/dev/stderr")
490 function ensure_numeric
(n
) {
493 #-------------------------------
494 # Why do we need ensure_numeric?
495 #-------------------------------
496 # awk appears to be guessing the type of an inputted scalar based on usage, so
497 # if we read-in a number, but did not use it in any numeric operations, but did
498 # use as a string (even in just a format string!) - it will be treated as a
499 # string and can lead to REALLY SURPRISING behavior in conditional statements,
500 # where smaller number may compare as greater than the bigger ones, such as.
504 # $ 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"}}'
506 # $ 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"}}'
509 # However, once used as a number, seems to stay that way even after being
512 # $ 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"}}'
515 # $ 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"}}'
518 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
520 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'