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
( threshold
, dbg
, state
, remaining
, subj
, body
) {
126 state = db
["energy_state"]
127 remaining = db
["energy_percentage"]
130 dbg
["remaining"] = remaining
131 debug
("alert_check_energy", dbg
)
133 if (state ==
"discharging") {
135 subj =
"Energy_CRITICALLY_Low"
136 body =
sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining
)
137 alert_trigger_hi
(subj
, body
)
138 } else if (remaining
< 10) {
139 subj =
"Energy_Very_Low"
140 body =
sprintf("%d%% Plug it in ASAP.", remaining
)
141 alert_trigger_hi
(subj
, body
)
142 } else if (remaining
< 15) {
144 body =
sprintf("%d%% Get the charger.", remaining
)
145 alert_trigger_hi
(subj
, body
)
146 } else if (remaining
< 50) {
147 if (!state__alerts__energy__notified_bellow_half
) {
148 state__alerts__energy__notified_bellow_half =
1
149 subj =
"Energy_Bellow_Half"
150 body =
sprintf("%d%% Where is the charger?", remaining
)
151 alert_trigger_hi
(subj
, body
)
155 # TODO: Reconsider the competing global-state organizing-conventions
156 state__alerts__energy__notified_bellow_half =
0
160 function alert_trigger_low
(subject
, body
) {
161 alert_trigger
("low", subject
, body
)
164 function alert_trigger_med
(subject
, body
) {
165 alert_trigger
("med", subject
, body
)
168 function alert_trigger_hi
(subject
, body
) {
169 alert_trigger
("hi", subject
, body
)
172 function alert_trigger
(priority
, subject
, body
, msg
) {
173 # priority : "low" | "med" | "hi"
174 # subject : no spaces
176 msg =
sprintf("%s %s %s", priority
, subject
, body
)
177 output_msg_alert
(msg
)
180 function output_msg_alert
(msg
) {
181 # TODO: Should alerts go into a dedicated channel?
182 output_msg
("ALERT", msg
, "/dev/stdout")
185 function output_msg_status_bar
(msg
) {
186 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
189 function output_msg
(type
, content
, channel
) {
190 print(type
, content
) > channel
193 function set_load_avg
( sched
) {
194 split($
4, sched
, "/")
195 db
["load_avg_1min"] = $
1
196 db
["load_avg_5min"] = $
2
197 db
["load_avg_15min"] = $
3
198 db
["kern_sched_queue_runnable"] = sched
[1]
199 db
["kern_sched_queue_total"] = sched
[2]
200 db
["kern_sched_latest_pid"] = $
5
203 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
206 prev_w = db
["disk_io_curr_w"]
207 prev_r = db
["disk_io_curr_r"]
208 db
["disk_io_curr_w"] = curr_w
209 db
["disk_io_curr_r"] = curr_r
210 db
["disk_io_diff_w"] = curr_w
- prev_w
211 db
["disk_io_diff_r"] = curr_r
- prev_r
214 function set_net_addr_io
( \
215 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
222 if (address
&& io_curr_w
&& io_curr_r
) {
224 io_prev_w = net_io_curr_w
[interface
]
225 io_prev_r = net_io_curr_r
[interface
]
227 net_addr
[interface
] = address
228 net_io_curr_w
[interface
] = io_curr_w
229 net_io_curr_r
[interface
] = io_curr_r
230 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
231 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
234 net_addr
[interface
] =
""
235 net_io_curr_w
[interface
] =
0
236 net_io_curr_r
[interface
] =
0
237 net_io_diff_w
[interface
] =
0
238 net_io_diff_r
[interface
] =
0
243 function set_screen_brightness
( max
, cur
) {
246 db
["screen_brightness"] =
(cur
/ max
) * 100
249 function split_msg_parts
( dbg
) {
251 sub("^" msg_head
" +", "")
253 dbg
["msg_head"] = msg_head
254 dbg
["msg_body"] = msg_body
255 debug
("split_msg_parts", dbg
)
258 function make_status_bar
( position
, bar
, sep
, i
, j
) {
259 position
[++i
] = make_status_energy
()
260 position
[++i
] = make_status_mem
()
261 position
[++i
] = make_status_cpu
()
262 position
[++i
] = make_status_disk
()
263 position
[++i
] = make_status_net
()
264 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
265 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
266 position
[++i
] =
sprintf("(%s)", db
["volume"])
267 position
[++i
] = make_status_mpd
()
268 position
[++i
] = db
["weather_temperature"]
269 position
[++i
] = db
["datetime"]
272 for (j =
1; j
<= i
; j
++) {
273 bar = bar sep position
[j
]
279 function make_status_energy
( state
, direction_of_change
) {
280 state = db
["energy_state"]
281 if (state ==
"discharging") {
282 direction_of_change =
"<"
283 } else if (state ==
"charging") {
284 direction_of_change =
">"
286 direction_of_change =
"="
288 return sprintf("E%s%d%%", direction_of_change
, db
["energy_percentage"])
291 function make_status_mem
( total
, used
, percent
, status
) {
292 total = db
["memory_total"]
293 used = db
["memory_used"]
294 # To avoid division by zero when data is missing
296 percent = round
((used
/ total
) * 100)
297 status =
sprintf("%d%%", percent
)
301 return sprintf("M=%s", status
)
304 function make_status_cpu
( load
, temp
, fan
) {
305 load = db
["load_avg_1min"]
306 temp = db
["temperature"] / 1000
307 fan = db
["fan_speed"]
308 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
311 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
312 bytes_per_sector =
512
313 bytes_per_mb =
1024 * 1024
314 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
315 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
317 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
320 function make_status_net
( \
322 number_of_interfaces_to_show
,
324 array_of_prefixes_of_interfaces_to_show
,
335 number_of_interfaces_to_show = \
337 opt_prefixes_of_net_interfaces_to_show
,\
338 array_of_prefixes_of_interfaces_to_show
,\
341 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
342 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
343 for (interface in net_addr
) {
344 if (interface ~
("^" prefix
)) {
345 label =
substr(interface
, 1, 1)
346 if (net_addr
[interface
]) {
347 bytes_per_mb =
1024 * 1024 # TODO: option
348 dw = net_io_diff_w
[interface
] / bytes_per_mb
349 dr = net_io_diff_r
[interface
] / bytes_per_mb
350 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
354 if (interface ~
"^w") {
355 label = label
":" db
["net_wifi_status"]
357 if (++count_printed
> 1) {
362 out = out sep label
":" io_stat
366 return sprintf("N[%s]", out
)
369 function make_status_mpd
( state
, status
) {
370 state = db
["mpd_state"]
372 if (state ==
"play") {
373 status = make_status_mpd_state_known
("▶")
374 } else if (state ==
"pause") {
375 status = make_status_mpd_state_known
("❚❚")
376 } else if (state ==
"stop") {
377 status = make_status_mpd_state_known
("⬛")
379 status = make_status_mpd_state_unknown
("--")
382 return sprintf("[%s]", status
)
385 function make_status_mpd_state_known
(symbol
) {
389 db
["mpd_curr_song_time"],
390 db
["mpd_curr_song_percent"],
391 substr(db
["mpd_curr_song_name"], 1, opt_mpd_song_max_chars
)\
395 function make_status_mpd_state_unknown
(symbol
) {
396 return sprintf("%s", symbol
)
403 function debug
(location
, values
, sep
, vals
, key
, msg
) {
407 for (key in values
) {
408 vals =
sprintf("%s%s%s: %s", vals
, sep
, key
, values
[key
])
411 msg = location
" ==> " vals
"."
412 output_msg
("DEBUG", msg
, "/dev/stderr")
416 function ensure_numeric
(n
) {
419 #-------------------------------
420 # Why do we need ensure_numeric?
421 #-------------------------------
422 # awk appears to be guessing the type of an inputted scalar based on usage, so
423 # if we read-in a number, but did not use it in any numeric operations, but did
424 # use as a string (even in just a format string!) - it will be treated as a
425 # string and can lead to REALLY SURPRISING behavior in conditional statements,
426 # where smaller number may compare as greater than the bigger ones, such as.
430 # $ 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"}}'
432 # $ 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"}}'
435 # However, once used as a number, seems to stay that way even after being
438 # $ 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"}}'
441 # $ 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"}}'
444 # $ awk 'BEGIN {x = "75"; y = "100"; 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"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'