8 db
["energy_state"] = $
1
9 db
["energy_percentage"] = ensure_numeric
($
2)
16 db
["memory_total"] = $
1
17 db
["memory_used"] = $
2
41 db
["temperature"] = $
1
59 db
["disk_space_used"] = msg_body
68 /^in
:NET_WIFI_STATUS
/\
71 db
["net_wifi_status"] = msg_body
74 /^in
:BLUETOOTH_POWER
/\
77 db
["bluetooth_power"] = msg_body
80 /^in
:SCREEN_BRIGHTNESS
/\
83 set_screen_brightness
()
89 db
["volume"] = msg_body
96 db
["mpd_curr_song_time"] = $
2
97 db
["mpd_curr_song_percent"] = $
3
103 db
["mpd_curr_song_name"] = msg_body
109 db
["weather_temperature"] = msg_body
115 db
["datetime"] = msg_body
116 output_msg_status_bar
(make_status_bar
())
119 # TODO: Generalize alert spec lang
120 # - trigger threshold
121 # - above/bellow/equal to threshold value
123 # - snooze time (if already alerted, when to re-alert?)
124 # - text: subject/body
125 function alert_check_energy
( from
, dbg
, state
, remaining
, subj
, body
) {
126 from =
"alert_check_energy"
128 state = db
["energy_state"]
129 remaining = db
["energy_percentage"]
132 dbg
["remaining"] = remaining
135 if (state ==
"discharging") {
137 subj =
"Energy_CRITICALLY_Low"
138 body =
sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining
)
139 alert_trigger_hi
(from
, subj
, body
)
140 } else if (remaining
< 10) {
141 subj =
"Energy_Very_Low"
142 body =
sprintf("%d%% Plug it in ASAP.", remaining
)
143 alert_trigger_hi
(from
, subj
, body
)
144 } else if (remaining
< 15) {
146 body =
sprintf("%d%% Get the charger.", remaining
)
147 alert_trigger_hi
(from
, subj
, body
)
148 } else if (remaining
< 50) {
149 if (!state__alerts__energy__notified_bellow_half
) {
150 state__alerts__energy__notified_bellow_half =
1
151 subj =
"Energy_Bellow_Half"
152 body =
sprintf("%d%% Where is the charger?", remaining
)
153 alert_trigger_med
(from
, subj
, body
)
157 # TODO: Reconsider the competing global-state organizing-conventions
158 state__alerts__energy__notified_bellow_half =
0
162 function alert_trigger_low
(from
, subject
, body
) {
163 alert_trigger
("low", from
, subject
, body
)
166 function alert_trigger_med
(from
, subject
, body
) {
167 alert_trigger
("med", from
, subject
, body
)
170 function alert_trigger_hi
(from
, subject
, body
) {
171 alert_trigger
("hi", from
, subject
, body
)
174 function alert_trigger
(priority
, from
, subject
, body
, msg
) {
175 # priority : "low" | "med" | "hi"
176 # subject : no spaces
178 msg =
sprintf("khatus_%s %s %s %s", from
, priority
, subject
, body
)
179 output_msg_alert
(msg
)
182 function output_msg_alert
(msg
) {
183 # TODO: Should alerts go into a dedicated channel?
184 output_msg
("ALERT", msg
, "/dev/stdout")
187 function output_msg_status_bar
(msg
) {
188 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
191 function output_msg
(type
, content
, channel
) {
192 print(type
, content
) > channel
195 function set_load_avg
( sched
) {
196 split($
4, sched
, "/")
197 db
["load_avg_1min"] = $
1
198 db
["load_avg_5min"] = $
2
199 db
["load_avg_15min"] = $
3
200 db
["kern_sched_queue_runnable"] = sched
[1]
201 db
["kern_sched_queue_total"] = sched
[2]
202 db
["kern_sched_latest_pid"] = $
5
205 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
208 prev_w = db
["disk_io_curr_w"]
209 prev_r = db
["disk_io_curr_r"]
210 db
["disk_io_curr_w"] = curr_w
211 db
["disk_io_curr_r"] = curr_r
212 db
["disk_io_diff_w"] = curr_w
- prev_w
213 db
["disk_io_diff_r"] = curr_r
- prev_r
216 function set_net_addr_io
( \
217 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
224 if (address
&& io_curr_w
&& io_curr_r
) {
226 io_prev_w = net_io_curr_w
[interface
]
227 io_prev_r = net_io_curr_r
[interface
]
229 net_addr
[interface
] = address
230 net_io_curr_w
[interface
] = io_curr_w
231 net_io_curr_r
[interface
] = io_curr_r
232 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
233 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
236 net_addr
[interface
] =
""
237 net_io_curr_w
[interface
] =
0
238 net_io_curr_r
[interface
] =
0
239 net_io_diff_w
[interface
] =
0
240 net_io_diff_r
[interface
] =
0
245 function set_screen_brightness
( max
, cur
) {
248 db
["screen_brightness"] =
(cur
/ max
) * 100
251 function split_msg_parts
( dbg
) {
253 sub("^" msg_head
" +", "")
255 dbg
["msg_head"] = msg_head
256 dbg
["msg_body"] = msg_body
257 debug
("split_msg_parts", dbg
)
260 function make_status_bar
( position
, bar
, sep
, i
, j
) {
261 position
[++i
] = make_status_energy
()
262 position
[++i
] = make_status_mem
()
263 position
[++i
] = make_status_cpu
()
264 position
[++i
] = make_status_disk
()
265 position
[++i
] = make_status_net
()
266 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
267 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
268 position
[++i
] =
sprintf("(%s)", db
["volume"])
269 position
[++i
] = make_status_mpd
()
270 position
[++i
] = db
["weather_temperature"]
271 position
[++i
] = db
["datetime"]
274 for (j =
1; j
<= i
; j
++) {
275 bar = bar sep position
[j
]
281 function make_status_energy
( state
, direction_of_change
) {
282 state = db
["energy_state"]
283 if (state ==
"discharging") {
284 direction_of_change =
"<"
285 } else if (state ==
"charging") {
286 direction_of_change =
">"
288 direction_of_change =
"="
290 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
293 function make_status_mem
( total
, used
, percent
, status
) {
294 total = db
["memory_total"]
295 used = db
["memory_used"]
296 # To avoid division by zero when data is missing
298 percent = round
((used
/ total
) * 100)
299 status =
sprintf("%d%%", percent
)
303 return sprintf("M=%s", status
)
306 function make_status_cpu
( load
, temp
, fan
) {
307 load = db
["load_avg_1min"]
308 temp = db
["temperature"] / 1000
309 fan = db
["fan_speed"]
310 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
313 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
314 bytes_per_sector =
512
315 bytes_per_mb =
1024 * 1024
316 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
317 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
319 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
322 function make_status_net
( \
324 number_of_interfaces_to_show
,
326 array_of_prefixes_of_interfaces_to_show
,
337 number_of_interfaces_to_show = \
339 opt_prefixes_of_net_interfaces_to_show
,\
340 array_of_prefixes_of_interfaces_to_show
,\
343 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
344 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
345 for (interface in net_addr
) {
346 if (interface ~
("^" prefix
)) {
347 label =
substr(interface
, 1, 1)
348 if (net_addr
[interface
]) {
349 bytes_per_mb =
1024 * 1024 # TODO: option
350 dw = net_io_diff_w
[interface
] / bytes_per_mb
351 dr = net_io_diff_r
[interface
] / bytes_per_mb
352 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
356 if (interface ~
"^w") {
357 label = label
":" db
["net_wifi_status"]
359 if (++count_printed
> 1) {
364 out = out sep label
":" io_stat
368 return sprintf("N[%s]", out
)
371 function make_status_mpd
( state
, status
) {
372 state = db
["mpd_state"]
374 if (state ==
"play") {
375 status = make_status_mpd_state_known
("▶")
376 } else if (state ==
"pause") {
377 status = make_status_mpd_state_known
("❚❚")
378 } else if (state ==
"stop") {
379 status = make_status_mpd_state_known
("⬛")
381 status = make_status_mpd_state_unknown
("--")
384 return sprintf("[%s]", status
)
387 function make_status_mpd_state_known
(symbol
) {
391 db
["mpd_curr_song_time"],
392 db
["mpd_curr_song_percent"],
393 substr(db
["mpd_curr_song_name"], 1, opt_mpd_song_max_chars
)\
397 function make_status_mpd_state_unknown
(symbol
) {
398 return sprintf("%s", symbol
)
405 function debug
(location
, values
, sep
, vals
, key
, msg
) {
409 for (key in values
) {
410 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
413 msg = location
" ==> " vals
"."
414 output_msg
("DEBUG", msg
, "/dev/stderr")
418 function ensure_numeric
(n
) {
421 #-------------------------------
422 # Why do we need ensure_numeric?
423 #-------------------------------
424 # awk appears to be guessing the type of an inputted scalar based on usage, so
425 # if we read-in a number, but did not use it in any numeric operations, but did
426 # use as a string (even in just a format string!) - it will be treated as a
427 # string and can lead to REALLY SURPRISING behavior in conditional statements,
428 # where smaller number may compare as greater than the bigger ones, such as.
432 # $ 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"}}'
434 # $ 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"}}'
437 # However, once used as a number, seems to stay that way even after being
440 # $ 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"}}'
443 # $ 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"}}'
446 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
448 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'