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
()
97 db
["mpd_curr_song_time"] = $
2
98 db
["mpd_curr_song_percent"] = $
3
104 db
["mpd_curr_song_name"] = $
0
110 db
["weather_temperature"] = $
0
117 output_msg_status_bar
(make_status_bar
())
120 # TODO: Generalize alert spec lang
121 # - trigger threshold
122 # - above/bellow/equal to threshold value
124 # - snooze time (if already alerted, when to re-alert?)
125 # - text: subject/body
126 function alert_check_energy
( \
127 from
, dbg
, state_curr
, state_prev
, remaining
, subj
, body\
129 from =
"alert_check_energy"
131 state_curr = db
["energy_state_curr"]
132 state_prev = db
["energy_state_prev"]
133 remaining = db
["energy_percentage"]
135 dbg
["state_curr"] = state_curr
136 dbg
["remaining"] = remaining
139 if (state_curr ==
"discharging") {
140 if (state_prev ==
"charging") {
141 alert_trigger_low
(from
, "Unplugged", "")
145 subj =
"Energy_CRITICALLY_Low"
146 body =
sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining
)
147 alert_trigger_hi
(from
, subj
, body
)
148 } else if (remaining
< 10) {
149 subj =
"Energy_Very_Low"
150 body =
sprintf("%d%% Plug it in ASAP.", remaining
)
151 alert_trigger_hi
(from
, subj
, body
)
152 } else if (remaining
< 15) {
154 body =
sprintf("%d%% Get the charger.", remaining
)
155 alert_trigger_hi
(from
, subj
, body
)
156 } else if (remaining
< 20) {
158 body =
sprintf("%d%% Get the charger.", remaining
)
159 alert_trigger_med
(from
, subj
, body
)
160 } else if (remaining
< 50) {
161 if (!state__alerts__energy__notified_bellow_half
) {
162 state__alerts__energy__notified_bellow_half =
1
163 subj =
"Energy_Bellow_Half"
164 body =
sprintf("%d%% Where is the charger?", remaining
)
165 alert_trigger_med
(from
, subj
, body
)
169 # TODO: Reconsider the competing global-state organizing-conventions
170 state__alerts__energy__notified_bellow_half =
0
174 function alert_trigger_low
(from
, subject
, body
) {
175 alert_trigger
("low", from
, subject
, body
)
178 function alert_trigger_med
(from
, subject
, body
) {
179 alert_trigger
("med", from
, subject
, body
)
182 function alert_trigger_hi
(from
, subject
, body
) {
183 alert_trigger
("hi", from
, subject
, body
)
186 function alert_trigger
(priority
, from
, subject
, body
, msg
) {
187 # priority : "low" | "med" | "hi"
188 # subject : no spaces
190 msg =
sprintf("khatus_%s %s %s %s", from
, priority
, subject
, body
)
191 output_msg_alert
(msg
)
194 function output_msg_alert
(msg
) {
195 # TODO: Should alerts go into a dedicated channel?
196 output_msg
("ALERT", msg
, "/dev/stdout")
199 function output_msg_status_bar
(msg
) {
200 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
203 function output_msg
(type
, content
, channel
) {
204 print(type
, content
) > channel
207 function set_load_avg
( sched
) {
208 split($
4, sched
, "/")
209 db
["load_avg_1min"] = $
1
210 db
["load_avg_5min"] = $
2
211 db
["load_avg_15min"] = $
3
212 db
["kern_sched_queue_runnable"] = sched
[1]
213 db
["kern_sched_queue_total"] = sched
[2]
214 db
["kern_sched_latest_pid"] = $
5
217 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
220 prev_w = db
["disk_io_curr_w"]
221 prev_r = db
["disk_io_curr_r"]
222 db
["disk_io_curr_w"] = curr_w
223 db
["disk_io_curr_r"] = curr_r
224 db
["disk_io_diff_w"] = curr_w
- prev_w
225 db
["disk_io_diff_r"] = curr_r
- prev_r
228 function set_net_addr_io
( \
229 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
236 if (address
&& io_curr_w
&& io_curr_r
) {
238 io_prev_w = net_io_curr_w
[interface
]
239 io_prev_r = net_io_curr_r
[interface
]
241 net_addr
[interface
] = address
242 net_io_curr_w
[interface
] = io_curr_w
243 net_io_curr_r
[interface
] = io_curr_r
244 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
245 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
248 net_addr
[interface
] =
""
249 net_io_curr_w
[interface
] =
0
250 net_io_curr_r
[interface
] =
0
251 net_io_diff_w
[interface
] =
0
252 net_io_diff_r
[interface
] =
0
257 function set_screen_brightness
( max
, cur
) {
260 db
["screen_brightness"] =
(cur
/ max
) * 100
263 function fields_shift
( dbg
) {
267 debug
("fields_shift", dbg
)
270 function make_status_bar
( position
, bar
, sep
, i
, j
) {
271 position
[++i
] = make_status_energy
()
272 position
[++i
] = make_status_mem
()
273 position
[++i
] = make_status_cpu
()
274 position
[++i
] = make_status_disk
()
275 position
[++i
] = make_status_net
()
276 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
277 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
278 position
[++i
] =
sprintf("(%s)", db
["volume"])
279 position
[++i
] = make_status_mpd
()
280 position
[++i
] = db
["weather_temperature"]
281 position
[++i
] = db
["datetime"]
284 for (j =
1; j
<= i
; j
++) {
285 bar = bar sep position
[j
]
291 function make_status_energy
( state
, direction_of_change
) {
292 state = db
["energy_state_curr"]
293 if (state ==
"discharging") {
294 direction_of_change =
"<"
295 } else if (state ==
"charging") {
296 direction_of_change =
">"
298 direction_of_change =
"="
300 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
303 function make_status_mem
( total
, used
, percent
, status
) {
304 total = db
["memory_total"]
305 used = db
["memory_used"]
306 # To avoid division by zero when data is missing
308 percent = round
((used
/ total
) * 100)
309 status =
sprintf("%d%%", percent
)
313 return sprintf("M=%s", status
)
316 function make_status_cpu
( load
, temp
, fan
) {
317 load = db
["load_avg_1min"]
318 temp = db
["temperature"] / 1000
319 fan = db
["fan_speed"]
320 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
323 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
324 bytes_per_sector =
512
325 bytes_per_mb =
1024 * 1024
326 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
327 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
329 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
332 function make_status_net
( \
334 number_of_interfaces_to_show
,
336 array_of_prefixes_of_interfaces_to_show
,
347 number_of_interfaces_to_show = \
349 opt_prefixes_of_net_interfaces_to_show
,\
350 array_of_prefixes_of_interfaces_to_show
,\
353 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
354 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
355 for (interface in net_addr
) {
356 if (interface ~
("^" prefix
)) {
357 label =
substr(interface
, 1, 1)
358 if (net_addr
[interface
]) {
359 bytes_per_mb =
1024 * 1024 # TODO: option
360 dw = net_io_diff_w
[interface
] / bytes_per_mb
361 dr = net_io_diff_r
[interface
] / bytes_per_mb
362 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
366 if (interface ~
"^w") {
367 label = label
":" db
["net_wifi_status"]
369 if (++count_printed
> 1) {
374 out = out sep label
":" io_stat
378 return sprintf("N[%s]", out
)
381 function make_status_mpd
( state
, status
) {
382 state = db
["mpd_state"]
384 if (state ==
"play") {
385 status = make_status_mpd_state_known
("▶")
386 } else if (state ==
"pause") {
387 status = make_status_mpd_state_known
("❚❚")
388 } else if (state ==
"stop") {
389 status = make_status_mpd_state_known
("⬛")
391 status = make_status_mpd_state_unknown
("--")
394 return sprintf("[%s]", status
)
397 function make_status_mpd_state_known
(symbol
) {
401 db
["mpd_curr_song_time"],
402 db
["mpd_curr_song_percent"],
403 substr(db
["mpd_curr_song_name"], 1, opt_mpd_song_max_chars
)\
407 function make_status_mpd_state_unknown
(symbol
) {
408 return sprintf("%s", symbol
)
415 function debug
(location
, values
, sep
, vals
, key
, msg
) {
419 for (key in values
) {
420 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
423 msg = location
" ==> " vals
"."
424 output_msg
("DEBUG", msg
, "/dev/stderr")
428 function ensure_numeric
(n
) {
431 #-------------------------------
432 # Why do we need ensure_numeric?
433 #-------------------------------
434 # awk appears to be guessing the type of an inputted scalar based on usage, so
435 # if we read-in a number, but did not use it in any numeric operations, but did
436 # use as a string (even in just a format string!) - it will be treated as a
437 # string and can lead to REALLY SURPRISING behavior in conditional statements,
438 # where smaller number may compare as greater than the bigger ones, such as.
442 # $ 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"}}'
444 # $ 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"}}'
447 # However, once used as a number, seems to stay that way even after being
450 # $ 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"}}'
453 # $ 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"}}'
456 # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
458 # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'