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 /^in
:MPD_SONG OK
+MPD
/ { delete db_mpd_song
; next }
94 /^in
:MPD_SONG OK$
/ { set_mpd_playing
() ; next }
95 /^in
:MPD_SONG
/ { set_mpd_song
() ; next }
100 db
["mpd_status_state"] = $
1
101 db
["mpd_status_time"] = $
2
102 db
["mpd_status_percent"] = $
3
108 db
["weather_temperature"] = $
0
115 output_msg_status_bar
(make_status_bar
())
118 function set_mpd_song
( key
, val
) {
123 db_mpd_song
[key
] = val
124 debug
("set_mpd_song", "", db_mpd_song
)
127 function set_mpd_playing
( \
128 currently_playing
, name
, title
, file
, last
, parts\
130 debug
("set_mpd_playing", "", db_mpd_song
)
131 name = db_mpd_song
["Name:"]
132 title = db_mpd_song
["Title:"]
133 file = db_mpd_song
["file:"]
136 currently_playing = name
138 currently_playing = title
140 last =
split(file
, parts
, "/")
141 currently_playing = parts
[last
]
143 currently_playing =
""
145 db
["mpd_playing_prev"] = db
["mpd_playing_curr"]
146 db
["mpd_playing_curr"] = currently_playing
151 function alert_check_mpd
( curr
, prev
, name
, body
) {
152 prev = db
["mpd_playing_prev"]
153 curr = db
["mpd_playing_curr"]
154 if (curr
&& curr
!= prev
) {
155 name = db_mpd_song
["Name:"]
160 db_mpd_song
["Artist:"] \
161 " - " db_mpd_song
["Album:"] \
162 " - " db_mpd_song
["Title:"]
164 alert_trigger_low
("alert_check_mpd", "NowPlaying", body
)
168 # TODO: Generalize alert spec lang
169 # - trigger threshold
170 # - above/bellow/equal to threshold value
172 # - snooze time (if already alerted, when to re-alert?)
173 # - text: subject/body
174 function alert_check_energy
( \
175 from
, dbg
, state_curr
, state_prev
, remaining
, subj
, body\
177 from =
"alert_check_energy"
179 state_curr = db
["energy_state_curr"]
180 state_prev = db
["energy_state_prev"]
181 remaining = db
["energy_percentage"]
183 dbg
["state_curr"] = state_curr
184 dbg
["remaining"] = remaining
187 if (state_curr ==
"discharging") {
188 if (state_prev ==
"charging") {
189 alert_trigger_low
(from
, "PowerUnplugged", "")
193 subj =
"Energy_CRITICALLY_Low"
194 body =
sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining
)
195 alert_trigger_hi
(from
, subj
, body
)
196 } else if (remaining
< 10) {
197 subj =
"Energy_Very_Low"
198 body =
sprintf("%d%% Plug it in ASAP.", remaining
)
199 alert_trigger_hi
(from
, subj
, body
)
200 } else if (remaining
< 15) {
202 body =
sprintf("%d%% Get the charger.", remaining
)
203 alert_trigger_hi
(from
, subj
, body
)
204 } else if (remaining
< 20) {
206 body =
sprintf("%d%% Get the charger.", remaining
)
207 alert_trigger_med
(from
, subj
, body
)
208 } else if (remaining
< 50) {
209 if (!state__alerts__energy__notified_bellow_half
) {
210 state__alerts__energy__notified_bellow_half =
1
211 subj =
"Energy_Bellow_Half"
212 body =
sprintf("%d%% Where is the charger?", remaining
)
213 alert_trigger_med
(from
, subj
, body
)
217 # TODO: Reconsider the competing global-state organizing-conventions
218 state__alerts__energy__notified_bellow_half =
0
222 function alert_trigger_low
(from
, subject
, body
) {
223 alert_trigger
("low", from
, subject
, body
)
226 function alert_trigger_med
(from
, subject
, body
) {
227 alert_trigger
("med", from
, subject
, body
)
230 function alert_trigger_hi
(from
, subject
, body
) {
231 alert_trigger
("hi", from
, subject
, body
)
234 function alert_trigger
(priority
, from
, subject
, body
, msg
) {
235 # priority : "low" | "med" | "hi"
236 # subject : no spaces
238 msg =
sprintf("khatus_%s %s %s %s", from
, priority
, subject
, body
)
239 output_msg_alert
(msg
)
242 function output_msg_alert
(msg
) {
243 # TODO: Should alerts go into a dedicated channel?
244 output_msg
("ALERT", msg
, "/dev/stdout")
247 function output_msg_status_bar
(msg
) {
248 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
251 function output_msg
(type
, content
, channel
) {
252 print(type
, content
) > channel
255 function set_load_avg
( sched
) {
256 split($
4, sched
, "/")
257 db
["load_avg_1min"] = $
1
258 db
["load_avg_5min"] = $
2
259 db
["load_avg_15min"] = $
3
260 db
["kern_sched_queue_runnable"] = sched
[1]
261 db
["kern_sched_queue_total"] = sched
[2]
262 db
["kern_sched_latest_pid"] = $
5
265 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
268 prev_w = db
["disk_io_curr_w"]
269 prev_r = db
["disk_io_curr_r"]
270 db
["disk_io_curr_w"] = curr_w
271 db
["disk_io_curr_r"] = curr_r
272 db
["disk_io_diff_w"] = curr_w
- prev_w
273 db
["disk_io_diff_r"] = curr_r
- prev_r
276 function set_net_addr_io
( \
277 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
284 if (address
&& io_curr_w
&& io_curr_r
) {
286 io_prev_w = net_io_curr_w
[interface
]
287 io_prev_r = net_io_curr_r
[interface
]
289 net_addr
[interface
] = address
290 net_io_curr_w
[interface
] = io_curr_w
291 net_io_curr_r
[interface
] = io_curr_r
292 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
293 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
296 net_addr
[interface
] =
""
297 net_io_curr_w
[interface
] =
0
298 net_io_curr_r
[interface
] =
0
299 net_io_diff_w
[interface
] =
0
300 net_io_diff_r
[interface
] =
0
305 function set_screen_brightness
( max
, cur
) {
308 db
["screen_brightness"] =
(cur
/ max
) * 100
311 function fields_shift
( dbg
) {
315 debug
("fields_shift", "", dbg
)
318 function make_status_bar
( position
, bar
, sep
, i
, j
) {
319 position
[++i
] = make_status_energy
()
320 position
[++i
] = make_status_mem
()
321 position
[++i
] = make_status_cpu
()
322 position
[++i
] = make_status_disk
()
323 position
[++i
] = make_status_net
()
324 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
325 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
326 position
[++i
] =
sprintf("(%s)", db
["volume"])
327 position
[++i
] = make_status_mpd
()
328 position
[++i
] = db
["weather_temperature"]
329 position
[++i
] = db
["datetime"]
332 for (j =
1; j
<= i
; j
++) {
333 bar = bar sep position
[j
]
339 function make_status_energy
( state
, direction_of_change
) {
340 state = db
["energy_state_curr"]
341 if (state ==
"discharging") {
342 direction_of_change =
"<"
343 } else if (state ==
"charging") {
344 direction_of_change =
">"
346 direction_of_change =
"="
348 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
351 function make_status_mem
( total
, used
, percent
, status
) {
352 total = db
["memory_total"]
353 used = db
["memory_used"]
354 # To avoid division by zero when data is missing
356 percent = round
((used
/ total
) * 100)
357 status =
sprintf("%d%%", percent
)
361 return sprintf("M=%s", status
)
364 function make_status_cpu
( load
, temp
, fan
) {
365 load = db
["load_avg_1min"]
366 temp = db
["temperature"] / 1000
367 fan = db
["fan_speed"]
368 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
371 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
372 bytes_per_sector =
512
373 bytes_per_mb =
1024 * 1024
374 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
375 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
377 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
380 function make_status_net
( \
382 number_of_interfaces_to_show
,
384 array_of_prefixes_of_interfaces_to_show
,
395 number_of_interfaces_to_show = \
397 opt_prefixes_of_net_interfaces_to_show
,\
398 array_of_prefixes_of_interfaces_to_show
,\
401 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
402 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
403 for (interface in net_addr
) {
404 if (interface ~
("^" prefix
)) {
405 label =
substr(interface
, 1, 1)
406 if (net_addr
[interface
]) {
407 bytes_per_mb =
1024 * 1024 # TODO: option
408 dw = net_io_diff_w
[interface
] / bytes_per_mb
409 dr = net_io_diff_r
[interface
] / bytes_per_mb
410 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
414 if (interface ~
"^w") {
415 label = label
":" db
["net_wifi_status"]
417 if (++count_printed
> 1) {
422 out = out sep label
":" io_stat
426 return sprintf("N[%s]", out
)
429 function make_status_mpd
( state
, status
) {
430 state = db
["mpd_status_state"]
432 if (state ==
"play") {
433 status = make_status_mpd_state_known
("▶")
434 } else if (state ==
"pause") {
435 status = make_status_mpd_state_known
("❚❚")
436 } else if (state ==
"stop") {
437 status = make_status_mpd_state_known
("⬛")
439 status = make_status_mpd_state_unknown
("--")
442 return sprintf("[%s]", status
)
445 function make_status_mpd_state_known
(symbol
) {
449 db
["mpd_status_time"],
450 db
["mpd_status_percent"],
451 substr(db
["mpd_playing_curr"], 1, opt_mpd_song_max_chars
)\
455 function make_status_mpd_state_unknown
(symbol
) {
456 return sprintf("%s", symbol
)
463 function debug
(location
, msg
, values
, sep
, vals
, key
, payload
) {
467 for (key in values
) {
468 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
471 payload =
sprintf("[%s] [%s] [%s]", location
, msg
, vals
)
472 output_msg
("DEBUG", payload
, "/dev/stderr")
476 function ensure_numeric
(n
) {
479 #-------------------------------
480 # Why do we need ensure_numeric?
481 #-------------------------------
482 # awk appears to be guessing the type of an inputted scalar based on usage, so
483 # if we read-in a number, but did not use it in any numeric operations, but did
484 # use as a string (even in just a format string!) - it will be treated as a
485 # string and can lead to REALLY SURPRISING behavior in conditional statements,
486 # where smaller number may compare as greater than the bigger ones, such as.
490 # $ 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"}}'
492 # $ 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"}}'
495 # However, once used as a number, seems to stay that way even after being
498 # $ 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"}}'
501 # $ 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"}}'
504 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
506 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'