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
< 20) {
150 body =
sprintf("%d%% Get the charger.", remaining
)
151 alert_trigger_med
(from
, subj
, body
)
152 } else if (remaining
< 50) {
153 if (!state__alerts__energy__notified_bellow_half
) {
154 state__alerts__energy__notified_bellow_half =
1
155 subj =
"Energy_Bellow_Half"
156 body =
sprintf("%d%% Where is the charger?", remaining
)
157 alert_trigger_med
(from
, subj
, body
)
161 # TODO: Reconsider the competing global-state organizing-conventions
162 state__alerts__energy__notified_bellow_half =
0
166 function alert_trigger_low
(from
, subject
, body
) {
167 alert_trigger
("low", from
, subject
, body
)
170 function alert_trigger_med
(from
, subject
, body
) {
171 alert_trigger
("med", from
, subject
, body
)
174 function alert_trigger_hi
(from
, subject
, body
) {
175 alert_trigger
("hi", from
, subject
, body
)
178 function alert_trigger
(priority
, from
, subject
, body
, msg
) {
179 # priority : "low" | "med" | "hi"
180 # subject : no spaces
182 msg =
sprintf("khatus_%s %s %s %s", from
, priority
, subject
, body
)
183 output_msg_alert
(msg
)
186 function output_msg_alert
(msg
) {
187 # TODO: Should alerts go into a dedicated channel?
188 output_msg
("ALERT", msg
, "/dev/stdout")
191 function output_msg_status_bar
(msg
) {
192 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
195 function output_msg
(type
, content
, channel
) {
196 print(type
, content
) > channel
199 function set_load_avg
( sched
) {
200 split($
4, sched
, "/")
201 db
["load_avg_1min"] = $
1
202 db
["load_avg_5min"] = $
2
203 db
["load_avg_15min"] = $
3
204 db
["kern_sched_queue_runnable"] = sched
[1]
205 db
["kern_sched_queue_total"] = sched
[2]
206 db
["kern_sched_latest_pid"] = $
5
209 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
212 prev_w = db
["disk_io_curr_w"]
213 prev_r = db
["disk_io_curr_r"]
214 db
["disk_io_curr_w"] = curr_w
215 db
["disk_io_curr_r"] = curr_r
216 db
["disk_io_diff_w"] = curr_w
- prev_w
217 db
["disk_io_diff_r"] = curr_r
- prev_r
220 function set_net_addr_io
( \
221 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
228 if (address
&& io_curr_w
&& io_curr_r
) {
230 io_prev_w = net_io_curr_w
[interface
]
231 io_prev_r = net_io_curr_r
[interface
]
233 net_addr
[interface
] = address
234 net_io_curr_w
[interface
] = io_curr_w
235 net_io_curr_r
[interface
] = io_curr_r
236 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
237 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
240 net_addr
[interface
] =
""
241 net_io_curr_w
[interface
] =
0
242 net_io_curr_r
[interface
] =
0
243 net_io_diff_w
[interface
] =
0
244 net_io_diff_r
[interface
] =
0
249 function set_screen_brightness
( max
, cur
) {
252 db
["screen_brightness"] =
(cur
/ max
) * 100
255 function split_msg_parts
( dbg
) {
257 sub("^" msg_head
" +", "")
259 dbg
["msg_head"] = msg_head
260 dbg
["msg_body"] = msg_body
261 debug
("split_msg_parts", dbg
)
264 function make_status_bar
( position
, bar
, sep
, i
, j
) {
265 position
[++i
] = make_status_energy
()
266 position
[++i
] = make_status_mem
()
267 position
[++i
] = make_status_cpu
()
268 position
[++i
] = make_status_disk
()
269 position
[++i
] = make_status_net
()
270 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
271 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
272 position
[++i
] =
sprintf("(%s)", db
["volume"])
273 position
[++i
] = make_status_mpd
()
274 position
[++i
] = db
["weather_temperature"]
275 position
[++i
] = db
["datetime"]
278 for (j =
1; j
<= i
; j
++) {
279 bar = bar sep position
[j
]
285 function make_status_energy
( state
, direction_of_change
) {
286 state = db
["energy_state"]
287 if (state ==
"discharging") {
288 direction_of_change =
"<"
289 } else if (state ==
"charging") {
290 direction_of_change =
">"
292 direction_of_change =
"="
294 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
297 function make_status_mem
( total
, used
, percent
, status
) {
298 total = db
["memory_total"]
299 used = db
["memory_used"]
300 # To avoid division by zero when data is missing
302 percent = round
((used
/ total
) * 100)
303 status =
sprintf("%d%%", percent
)
307 return sprintf("M=%s", status
)
310 function make_status_cpu
( load
, temp
, fan
) {
311 load = db
["load_avg_1min"]
312 temp = db
["temperature"] / 1000
313 fan = db
["fan_speed"]
314 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
317 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
318 bytes_per_sector =
512
319 bytes_per_mb =
1024 * 1024
320 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
321 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
323 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
326 function make_status_net
( \
328 number_of_interfaces_to_show
,
330 array_of_prefixes_of_interfaces_to_show
,
341 number_of_interfaces_to_show = \
343 opt_prefixes_of_net_interfaces_to_show
,\
344 array_of_prefixes_of_interfaces_to_show
,\
347 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
348 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
349 for (interface in net_addr
) {
350 if (interface ~
("^" prefix
)) {
351 label =
substr(interface
, 1, 1)
352 if (net_addr
[interface
]) {
353 bytes_per_mb =
1024 * 1024 # TODO: option
354 dw = net_io_diff_w
[interface
] / bytes_per_mb
355 dr = net_io_diff_r
[interface
] / bytes_per_mb
356 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
360 if (interface ~
"^w") {
361 label = label
":" db
["net_wifi_status"]
363 if (++count_printed
> 1) {
368 out = out sep label
":" io_stat
372 return sprintf("N[%s]", out
)
375 function make_status_mpd
( state
, status
) {
376 state = db
["mpd_state"]
378 if (state ==
"play") {
379 status = make_status_mpd_state_known
("▶")
380 } else if (state ==
"pause") {
381 status = make_status_mpd_state_known
("❚❚")
382 } else if (state ==
"stop") {
383 status = make_status_mpd_state_known
("⬛")
385 status = make_status_mpd_state_unknown
("--")
388 return sprintf("[%s]", status
)
391 function make_status_mpd_state_known
(symbol
) {
395 db
["mpd_curr_song_time"],
396 db
["mpd_curr_song_percent"],
397 substr(db
["mpd_curr_song_name"], 1, opt_mpd_song_max_chars
)\
401 function make_status_mpd_state_unknown
(symbol
) {
402 return sprintf("%s", symbol
)
409 function debug
(location
, values
, sep
, vals
, key
, msg
) {
413 for (key in values
) {
414 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
417 msg = location
" ==> " vals
"."
418 output_msg
("DEBUG", msg
, "/dev/stderr")
422 function ensure_numeric
(n
) {
425 #-------------------------------
426 # Why do we need ensure_numeric?
427 #-------------------------------
428 # awk appears to be guessing the type of an inputted scalar based on usage, so
429 # if we read-in a number, but did not use it in any numeric operations, but did
430 # use as a string (even in just a format string!) - it will be treated as a
431 # string and can lead to REALLY SURPRISING behavior in conditional statements,
432 # where smaller number may compare as greater than the bigger ones, such as.
436 # $ 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"}}'
438 # $ 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"}}'
441 # However, once used as a number, seems to stay that way even after being
444 # $ 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"}}'
447 # $ 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"}}'
450 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
452 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'