7 db
["energy_state"] = $
1
8 db
["energy_percentage"] = $
2
14 db
["memory_total"] = $
1
15 db
["memory_used"] = $
2
39 db
["temperature"] = $
1
57 db
["disk_space_used"] = msg_body
66 /^in
:NET_WIFI_STATUS
/\
69 db
["net_wifi_status"] = msg_body
72 /^in
:BLUETOOTH_POWER
/\
75 db
["bluetooth_power"] = msg_body
78 /^in
:SCREEN_BRIGHTNESS
/\
81 set_screen_brightness
()
87 db
["volume"] = msg_body
94 db
["mpd_curr_song_time"] = $
2
95 db
["mpd_curr_song_percent"] = $
3
101 db
["mpd_curr_song_name"] = msg_body
107 db
["weather_temperature"] = msg_body
113 db
["datetime"] = msg_body
114 output_msg_status_bar
(make_status_bar
())
121 function alert_check_all
() {
125 # TODO: Generalize alert spec lang
126 # - trigger threshold
127 # - above/bellow/equal to threshold value
129 # - snooze time (if already alerted, when to re-alert?)
130 # - text: subject/body
131 function alert_check_energy
( state
, remaining
, subj
, body
) {
132 state = db
["energy_state"]
133 remaining = db
["energy_percentage"]
134 if (state ==
"discharging") {
136 subj =
"Energy_CRITICALLY_Low"
137 body =
sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining
)
138 alert_trigger_hi
(subj
, body
)
139 } else if (remaining
< 10) {
140 subj =
"Energy_Very_Low"
141 body =
sprintf("%d%% Plug it in ASAP.", remaining
)
142 alert_trigger_hi
(subj
, body
)
143 } else if (remaining
< 15) {
145 body =
sprintf("%d%% Get the charger.", remaining
)
146 alert_trigger_hi
(subj
, body
)
147 } else if (remaining
< 50) {
148 if (!state__alerts__energy__notified_bellow_half
) {
149 state__alerts__energy__notified_bellow_half =
1
150 subj =
"Energy_Bellow_Half"
151 body =
sprintf("%d%% Where is the charger?", remaining
)
152 alert_trigger_hi
(subj
, body
)
156 # TODO: Reconsider the competing global-state organizing-conventions
157 state__alerts__energy__notified_bellow_half =
0
161 function alert_trigger_low
(subject
, body
) {
162 alert_trigger
("low", subject
, body
)
165 function alert_trigger_med
(subject
, body
) {
166 alert_trigger
("med", subject
, body
)
169 function alert_trigger_hi
(subject
, body
) {
170 alert_trigger
("hi", subject
, body
)
173 function alert_trigger
(priority
, subject
, body
, msg
) {
174 # priority : "low" | "med" | "hi"
175 # subject : no spaces
177 msg =
sprintf("%s %s %s", priority
, subject
, body
)
178 output_msg_alert
(msg
)
181 function output_msg_alert
(msg
) {
182 # TODO: Should alerts go into a dedicated channel?
183 output_msg
("ALERT", msg
, "/dev/stdout")
186 function output_msg_status_bar
(msg
) {
187 output_msg
("STATUS_BAR", msg
, "/dev/stdout")
190 function output_msg
(type
, content
, channel
) {
191 print(type
, content
) > channel
194 function set_load_avg
( sched
) {
195 split($
4, sched
, "/")
196 db
["load_avg_1min"] = $
1
197 db
["load_avg_5min"] = $
2
198 db
["load_avg_15min"] = $
3
199 db
["kern_sched_queue_runnable"] = sched
[1]
200 db
["kern_sched_queue_total"] = sched
[2]
201 db
["kern_sched_latest_pid"] = $
5
204 function set_disk_io
( curr_w
, curr_r
, prev_w
, prev_r
) {
207 prev_w = db
["disk_io_curr_w"]
208 prev_r = db
["disk_io_curr_r"]
209 db
["disk_io_curr_w"] = curr_w
210 db
["disk_io_curr_r"] = curr_r
211 db
["disk_io_diff_w"] = curr_w
- prev_w
212 db
["disk_io_diff_r"] = curr_r
- prev_r
215 function set_net_addr_io
( \
216 interface
, address
, io_curr_w
, io_curr_r
, io_prev_w
, io_prev_r\
223 if (address
&& io_curr_w
&& io_curr_r
) {
225 io_prev_w = net_io_curr_w
[interface
]
226 io_prev_r = net_io_curr_r
[interface
]
228 net_addr
[interface
] = address
229 net_io_curr_w
[interface
] = io_curr_w
230 net_io_curr_r
[interface
] = io_curr_r
231 net_io_diff_w
[interface
] = io_curr_w
- io_prev_w
232 net_io_diff_r
[interface
] = io_curr_r
- io_prev_r
235 net_addr
[interface
] =
""
236 net_io_curr_w
[interface
] =
0
237 net_io_curr_r
[interface
] =
0
238 net_io_diff_w
[interface
] =
0
239 net_io_diff_r
[interface
] =
0
244 function set_screen_brightness
( max
, cur
) {
247 db
["screen_brightness"] =
(cur
/ max
) * 100
250 function split_msg_parts
() {
252 sub("^" msg_head
" +", "")
254 debug
(msg_head
, msg_body
)
257 function make_status_bar
( position
, bar
, sep
, i
, j
) {
258 position
[++i
] = make_status_energy
()
259 position
[++i
] = make_status_mem
()
260 position
[++i
] = make_status_cpu
()
261 position
[++i
] = make_status_disk
()
262 position
[++i
] = make_status_net
()
263 position
[++i
] =
sprintf("B=%s", db
["bluetooth_power"])
264 position
[++i
] =
sprintf("*%d%%", db
["screen_brightness"])
265 position
[++i
] =
sprintf("(%s)", db
["volume"])
266 position
[++i
] = make_status_mpd
()
267 position
[++i
] = db
["weather_temperature"]
268 position
[++i
] = db
["datetime"]
271 for (j =
1; j
<= i
; j
++) {
272 bar = bar sep position
[j
]
278 function make_status_energy
( state
, direction_of_change
) {
279 state = db
["energy_state"]
280 if (state ==
"discharging") {
281 direction_of_change =
"<"
282 } else if (state ==
"charging") {
283 direction_of_change =
">"
285 direction_of_change =
"="
287 printf("E%s%s%%", direction_of_change
, db
["energy_percentage"])
290 function make_status_mem
( total
, used
, percent
, status
) {
291 total = db
["memory_total"]
292 used = db
["memory_used"]
293 # To avoid division by zero when data is missing
295 percent = round
((used
/ total
) * 100)
296 status =
sprintf("%d%%", percent
)
300 return sprintf("M=%s", status
)
303 function make_status_cpu
( load
, temp
, fan
) {
304 load = db
["load_avg_1min"]
305 temp = db
["temperature"] / 1000
306 fan = db
["fan_speed"]
307 return sprintf("C=[%4.2f %d°C %4drpm]", load
, temp
, fan
)
310 function make_status_disk
( bytes_per_sector
, bytes_per_mb
, w
, r
) {
311 bytes_per_sector =
512
312 bytes_per_mb =
1024 * 1024
313 w =
(db
["disk_io_diff_w"] * bytes_per_sector
) / bytes_per_mb
314 r =
(db
["disk_io_diff_r"] * bytes_per_sector
) / bytes_per_mb
316 sprintf("D=[%s %0.3f▲ %0.3f▼]", db
["disk_space_used"], w
, r
)
319 function make_status_net
( \
321 number_of_interfaces_to_show
,
323 array_of_prefixes_of_interfaces_to_show
,
334 number_of_interfaces_to_show = \
336 opt_prefixes_of_net_interfaces_to_show
,\
337 array_of_prefixes_of_interfaces_to_show
,\
340 for (n =
1; n
<= number_of_interfaces_to_show
; n
++) {
341 prefix = array_of_prefixes_of_interfaces_to_show
[n
]
342 for (interface in net_addr
) {
343 if (interface ~
("^" prefix
)) {
344 label =
substr(interface
, 1, 1)
345 if (net_addr
[interface
]) {
346 bytes_per_mb =
1024 * 1024 # TODO: option
347 dw = net_io_diff_w
[interface
] / bytes_per_mb
348 dr = net_io_diff_r
[interface
] / bytes_per_mb
349 io_stat =
sprintf("%0.3f▲ %0.3f▼", dw
, dr
)
353 if (interface ~
"^w") {
354 label = label
":" db
["net_wifi_status"]
356 if (++count_printed
> 1) {
361 out = out sep label
":" io_stat
365 return sprintf("N[%s]", out
)
368 function make_status_mpd
( state
, status
) {
369 state = db
["mpd_state"]
371 if (state ==
"play") {
372 status = make_status_mpd_state_known
("▶")
373 } else if (state ==
"pause") {
374 status = make_status_mpd_state_known
("❚❚")
375 } else if (state ==
"stop") {
376 status = make_status_mpd_state_known
("⬛")
378 status = make_status_mpd_state_unknown
("--")
381 return sprintf("[%s]", status
)
384 function make_status_mpd_state_known
(symbol
) {
388 db
["mpd_curr_song_time"],
389 db
["mpd_curr_song_percent"],
390 substr(db
["mpd_curr_song_name"], 1, opt_mpd_song_max_chars
)\
394 function make_status_mpd_state_unknown
(symbol
) {
395 return sprintf("%s", symbol
)
402 function debug
(location
, msg
) {
404 output_msg
("DEBUG", location
" ==> " msg
, "/dev/stderr")