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
+file
:/\
97 db
["mpd_song_curr_file"] = $
0
100 /^in
:MPD_SONG
+Title
:/\
104 db
["mpd_song_curr_title"] = $
0
106 /^in
:MPD_SONG
+Name
:/\
110 db
["mpd_song_curr_name"] = $
0
116 db
["mpd_status_state"] = $
1
117 db
["mpd_status_time"] = $
2
118 db
["mpd_status_percent"] = $
3
124 db
["weather_temperature"] = $
0
131 output_msg_status_bar
(make_status_bar
())
134 # TODO: Generalize alert spec lang
135 # - trigger threshold
136 # - above/bellow/equal to threshold value
138 # - snooze time (if already alerted, when to re-alert?)
139 # - text: subject/body
140 function alert_check_energy
( \
141 from
, dbg
, state_curr
, state_prev
, remaining
, subj
, body\
143 from =
"alert_check_energy"
145 state_curr = db
["energy_state_curr"]
146 state_prev = db
["energy_state_prev"]
147 remaining = db
["energy_percentage"]
149 dbg
["state_curr"] = state_curr
150 dbg
["remaining"] = remaining
153 if (state_curr ==
"discharging") {
154 if (state_prev ==
"charging") {
155 alert_trigger_low
(from
, "Unplugged", "")
159 subj =
"Energy_CRITICALLY_Low"
160 body =
sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining
)
161 alert_trigger_hi
(from
, subj
, body
)
162 } else if (remaining
< 10) {
163 subj =
"Energy_Very_Low"
164 body =
sprintf("%d%% Plug it in ASAP.", remaining
)
165 alert_trigger_hi
(from
, subj
, body
)
166 } else if (remaining
< 15) {
168 body =
sprintf("%d%% Get the charger.", remaining
)
169 alert_trigger_hi
(from
, subj
, body
)
170 } else if (remaining
< 20) {
172 body =
sprintf("%d%% Get the charger.", remaining
)
173 alert_trigger_med
(from
, subj
, body
)
174 } else if (remaining
< 50) {
175 if (!state__alerts__energy__notified_bellow_half
) {
176 state__alerts__energy__notified_bellow_half =
1
177 subj =
"Energy_Bellow_Half"
178 body =
sprintf("%d%% Where is the charger?", remaining
)
179 alert_trigger_med
(from
, subj
, body
)
183 # TODO: Reconsider the competing global-state organizing-conventions
184 state__alerts__energy__notified_bellow_half =
0
188 function alert_trigger_low
(from
, subject
, body
) {
189 alert_trigger
("low", from
, subject
, body
)
192 function alert_trigger_med
(from
, subject
, body
) {
193 alert_trigger
("med", from
, subject
, body
)
196 function alert_trigger_hi
(from
, subject
, body
) {
197 alert_trigger
("hi", from
, subject
, body
)
200 function alert_trigger
(priority
, from
, subject
, body
, msg
) {
201 # priority : "low" | "med" | "hi"
202 # subject : no spaces
204 msg =
sprintf("khatus_%s %s %s %s", from
, priority
, subject
, body
)
205 output_msg_alert
(msg
)
208 function output_msg_alert
(msg
) {
209 # TODO: Should alerts go into a dedicated channel?
210 output_msg
("ALERT", msg
, "/dev/stdout")
213 function output_msg_status_bar
(msg
) {
214 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
217 function output_msg
(type
, content
, channel
) {
218 print(type
, content
) > channel
221 function set_load_avg
( sched
) {
222 split($
4, sched
, "/")
223 db
["load_avg_1min"] = $
1
224 db
["load_avg_5min"] = $
2
225 db
["load_avg_15min"] = $
3
226 db
["kern_sched_queue_runnable"] = sched
[1]
227 db
["kern_sched_queue_total"] = sched
[2]
228 db
["kern_sched_latest_pid"] = $
5
231 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
234 prev_w = db
["disk_io_curr_w"]
235 prev_r = db
["disk_io_curr_r"]
236 db
["disk_io_curr_w"] = curr_w
237 db
["disk_io_curr_r"] = curr_r
238 db
["disk_io_diff_w"] = curr_w
- prev_w
239 db
["disk_io_diff_r"] = curr_r
- prev_r
242 function set_net_addr_io
( \
243 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
250 if (address
&& io_curr_w
&& io_curr_r
) {
252 io_prev_w = net_io_curr_w
[interface
]
253 io_prev_r = net_io_curr_r
[interface
]
255 net_addr
[interface
] = address
256 net_io_curr_w
[interface
] = io_curr_w
257 net_io_curr_r
[interface
] = io_curr_r
258 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
259 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
262 net_addr
[interface
] =
""
263 net_io_curr_w
[interface
] =
0
264 net_io_curr_r
[interface
] =
0
265 net_io_diff_w
[interface
] =
0
266 net_io_diff_r
[interface
] =
0
271 function set_screen_brightness
( max
, cur
) {
274 db
["screen_brightness"] =
(cur
/ max
) * 100
277 function fields_shift
( dbg
) {
281 debug
("fields_shift", dbg
)
284 function make_status_bar
( position
, bar
, sep
, i
, j
) {
285 position
[++i
] = make_status_energy
()
286 position
[++i
] = make_status_mem
()
287 position
[++i
] = make_status_cpu
()
288 position
[++i
] = make_status_disk
()
289 position
[++i
] = make_status_net
()
290 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
291 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
292 position
[++i
] =
sprintf("(%s)", db
["volume"])
293 position
[++i
] = make_status_mpd
()
294 position
[++i
] = db
["weather_temperature"]
295 position
[++i
] = db
["datetime"]
298 for (j =
1; j
<= i
; j
++) {
299 bar = bar sep position
[j
]
305 function make_status_energy
( state
, direction_of_change
) {
306 state = db
["energy_state_curr"]
307 if (state ==
"discharging") {
308 direction_of_change =
"<"
309 } else if (state ==
"charging") {
310 direction_of_change =
">"
312 direction_of_change =
"="
314 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
317 function make_status_mem
( total
, used
, percent
, status
) {
318 total = db
["memory_total"]
319 used = db
["memory_used"]
320 # To avoid division by zero when data is missing
322 percent = round
((used
/ total
) * 100)
323 status =
sprintf("%d%%", percent
)
327 return sprintf("M=%s", status
)
330 function make_status_cpu
( load
, temp
, fan
) {
331 load = db
["load_avg_1min"]
332 temp = db
["temperature"] / 1000
333 fan = db
["fan_speed"]
334 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
337 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
338 bytes_per_sector =
512
339 bytes_per_mb =
1024 * 1024
340 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
341 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
343 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
346 function make_status_net
( \
348 number_of_interfaces_to_show
,
350 array_of_prefixes_of_interfaces_to_show
,
361 number_of_interfaces_to_show = \
363 opt_prefixes_of_net_interfaces_to_show
,\
364 array_of_prefixes_of_interfaces_to_show
,\
367 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
368 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
369 for (interface in net_addr
) {
370 if (interface ~
("^" prefix
)) {
371 label =
substr(interface
, 1, 1)
372 if (net_addr
[interface
]) {
373 bytes_per_mb =
1024 * 1024 # TODO: option
374 dw = net_io_diff_w
[interface
] / bytes_per_mb
375 dr = net_io_diff_r
[interface
] / bytes_per_mb
376 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
380 if (interface ~
"^w") {
381 label = label
":" db
["net_wifi_status"]
383 if (++count_printed
> 1) {
388 out = out sep label
":" io_stat
392 return sprintf("N[%s]", out
)
395 function make_status_mpd
( state
, status
) {
396 state = db
["mpd_status_state"]
398 if (state ==
"play") {
399 status = make_status_mpd_state_known
("▶")
400 } else if (state ==
"pause") {
401 status = make_status_mpd_state_known
("❚❚")
402 } else if (state ==
"stop") {
403 status = make_status_mpd_state_known
("⬛")
405 status = make_status_mpd_state_unknown
("--")
408 return sprintf("[%s]", status
)
411 function make_status_mpd_state_known
(symbol
, \
412 currently_playing
, name
, title
, file\
414 name = db
["mpd_song_curr_name"]
415 title = db
["mpd_song_curr_title:"]
416 file = db
["mpd_song_curr_file"]
419 currently_playing = name
421 currently_playing = title
423 last =
split(file
, parts
, "/")
424 currently_playing = parts
[last
]
426 currently_playing =
""
431 db
["mpd_status_time"],
432 db
["mpd_status_percent"],
433 substr(currently_playing
, 1, opt_mpd_song_max_chars
)\
437 function make_status_mpd_state_unknown
(symbol
) {
438 return sprintf("%s", symbol
)
445 function debug
(location
, values
, sep
, vals
, key
, msg
) {
449 for (key in values
) {
450 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
453 msg = location
" ==> " vals
"."
454 output_msg
("DEBUG", msg
, "/dev/stderr")
458 function ensure_numeric
(n
) {
461 #-------------------------------
462 # Why do we need ensure_numeric?
463 #-------------------------------
464 # awk appears to be guessing the type of an inputted scalar based on usage, so
465 # if we read-in a number, but did not use it in any numeric operations, but did
466 # use as a string (even in just a format string!) - it will be treated as a
467 # string and can lead to REALLY SURPRISING behavior in conditional statements,
468 # where smaller number may compare as greater than the bigger ones, such as.
472 # $ 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"}}'
474 # $ 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"}}'
477 # However, once used as a number, seems to stay that way even after being
480 # $ 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"}}'
483 # $ 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"}}'
486 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
488 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'