Commit | Line | Data |
---|---|---|
f37162a4 SK |
1 | #! /usr/bin/awk -f |
2 | ||
e16fe0ea | 3 | |
f37162a4 SK |
4 | /^in:ENERGY/\ |
5 | { | |
6 | split_msg_parts() | |
cae87802 | 7 | sub("%$", "", $2) |
f37162a4 | 8 | db["energy_state"] = $1 |
e16fe0ea SK |
9 | db["energy_percentage"] = ensure_numeric($2) |
10 | alert_check_energy() | |
f37162a4 SK |
11 | } |
12 | ||
13 | /^in:MEMORY/\ | |
14 | { | |
15 | split_msg_parts() | |
16 | db["memory_total"] = $1 | |
17 | db["memory_used"] = $2 | |
18 | } | |
19 | ||
20 | /^in:FAN +status:/\ | |
21 | { | |
22 | split_msg_parts() | |
23 | db["fan_status"] = $2 | |
24 | } | |
25 | ||
26 | /^in:FAN +speed:/\ | |
27 | { | |
28 | split_msg_parts() | |
29 | db["fan_speed"] = $2 | |
30 | } | |
31 | ||
32 | /^in:FAN +level:/\ | |
33 | { | |
34 | split_msg_parts() | |
35 | db["fan_level"] = $2 | |
36 | } | |
37 | ||
38 | /^in:TEMPERATURE/\ | |
39 | { | |
40 | split_msg_parts() | |
41 | db["temperature"] = $1 | |
42 | } | |
43 | ||
44 | /^in:LOAD_AVG/\ | |
45 | { | |
46 | split_msg_parts() | |
47 | set_load_avg() | |
48 | } | |
49 | ||
50 | /^in:DISK_IO/\ | |
51 | { | |
52 | split_msg_parts() | |
53 | set_disk_io() | |
54 | } | |
55 | ||
56 | /^in:DISK_SPACE/\ | |
57 | { | |
58 | split_msg_parts() | |
59 | db["disk_space_used"] = msg_body | |
60 | } | |
61 | ||
62 | /^in:NET_ADDR_IO/\ | |
63 | { | |
64 | split_msg_parts() | |
65 | set_net_addr_io() | |
66 | } | |
67 | ||
68 | /^in:NET_WIFI_STATUS/\ | |
69 | { | |
70 | split_msg_parts() | |
71 | db["net_wifi_status"] = msg_body | |
72 | } | |
73 | ||
74 | /^in:BLUETOOTH_POWER/\ | |
75 | { | |
76 | split_msg_parts() | |
77 | db["bluetooth_power"] = msg_body | |
78 | } | |
79 | ||
80 | /^in:SCREEN_BRIGHTNESS/\ | |
81 | { | |
82 | split_msg_parts() | |
83 | set_screen_brightness() | |
84 | } | |
85 | ||
86 | /^in:VOLUME/\ | |
87 | { | |
88 | split_msg_parts() | |
89 | db["volume"] = msg_body | |
90 | } | |
91 | ||
92 | /^in:MPD_STATE/\ | |
93 | { | |
94 | split_msg_parts() | |
95 | db["mpd_state"] = $1 | |
96 | db["mpd_curr_song_time"] = $2 | |
97 | db["mpd_curr_song_percent"] = $3 | |
98 | } | |
99 | ||
100 | /^in:MPD_SONG/\ | |
101 | { | |
102 | split_msg_parts() | |
103 | db["mpd_curr_song_name"] = msg_body | |
104 | } | |
105 | ||
106 | /^in:WEATHER/\ | |
107 | { | |
108 | split_msg_parts() | |
109 | db["weather_temperature"] = msg_body | |
110 | } | |
111 | ||
112 | /^in:DATE_TIME/\ | |
113 | { | |
114 | split_msg_parts() | |
115 | db["datetime"] = msg_body | |
cae87802 SK |
116 | output_msg_status_bar(make_status_bar()) |
117 | } | |
118 | ||
cae87802 SK |
119 | # TODO: Generalize alert spec lang |
120 | # - trigger threshold | |
121 | # - above/bellow/equal to threshold value | |
122 | # - priority | |
123 | # - snooze time (if already alerted, when to re-alert?) | |
124 | # - text: subject/body | |
42f8eb57 | 125 | function alert_check_energy( dbg, state, remaining, subj, body) { |
cae87802 SK |
126 | state = db["energy_state"] |
127 | remaining = db["energy_percentage"] | |
e16fe0ea SK |
128 | |
129 | dbg["state"] = state | |
130 | dbg["remaining"] = remaining | |
131 | debug("alert_check_energy", dbg) | |
132 | ||
cae87802 SK |
133 | if (state == "discharging") { |
134 | if (remaining < 5) { | |
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) { | |
143 | subj = "Energy_Low" | |
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) | |
8903f6a7 | 151 | alert_trigger_med(subj, body) |
cae87802 SK |
152 | } |
153 | } | |
154 | } else { | |
155 | # TODO: Reconsider the competing global-state organizing-conventions | |
156 | state__alerts__energy__notified_bellow_half = 0 | |
157 | } | |
158 | } | |
159 | ||
160 | function alert_trigger_low(subject, body) { | |
161 | alert_trigger("low", subject, body) | |
162 | } | |
163 | ||
164 | function alert_trigger_med(subject, body) { | |
165 | alert_trigger("med", subject, body) | |
166 | } | |
167 | ||
168 | function alert_trigger_hi(subject, body) { | |
169 | alert_trigger("hi", subject, body) | |
170 | } | |
171 | ||
172 | function alert_trigger(priority, subject, body, msg) { | |
173 | # priority : "low" | "med" | "hi" | |
174 | # subject : no spaces | |
175 | # body : anything | |
176 | msg = sprintf("%s %s %s", priority, subject, body) | |
177 | output_msg_alert(msg) | |
178 | } | |
179 | ||
180 | function output_msg_alert(msg) { | |
181 | # TODO: Should alerts go into a dedicated channel? | |
182 | output_msg("ALERT", msg, "/dev/stdout") | |
183 | } | |
184 | ||
185 | function output_msg_status_bar(msg) { | |
186 | output_msg("STATUS_BAR", msg, "/dev/stdout") | |
187 | } | |
188 | ||
189 | function output_msg(type, content, channel) { | |
190 | print(type, content) > channel | |
f37162a4 SK |
191 | } |
192 | ||
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 | |
201 | } | |
202 | ||
203 | function set_disk_io( curr_w, curr_r, prev_w, prev_r) { | |
204 | curr_w = $1 | |
205 | curr_r = $2 | |
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 | |
212 | } | |
213 | ||
214 | function set_net_addr_io( \ | |
215 | interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\ | |
216 | ) { | |
217 | interface = $1 | |
218 | address = $2 | |
219 | io_curr_w = $3 | |
220 | io_curr_r = $4 | |
221 | if (interface) { | |
222 | if (address && io_curr_w && io_curr_r) { | |
223 | # recalculate | |
224 | io_prev_w = net_io_curr_w[interface] | |
225 | io_prev_r = net_io_curr_r[interface] | |
226 | ||
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 | |
232 | } else { | |
233 | # clear | |
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 | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
243 | function set_screen_brightness( max, cur) { | |
244 | max = $1 | |
245 | cur = $2 | |
246 | db["screen_brightness"] = (cur / max) * 100 | |
247 | } | |
248 | ||
e16fe0ea | 249 | function split_msg_parts( dbg) { |
f37162a4 SK |
250 | msg_head = $1 |
251 | sub("^" msg_head " +", "") | |
252 | msg_body = $0 | |
e16fe0ea SK |
253 | dbg["msg_head"] = msg_head |
254 | dbg["msg_body"] = msg_body | |
255 | debug("split_msg_parts", dbg) | |
f37162a4 SK |
256 | } |
257 | ||
cae87802 | 258 | function make_status_bar( position, bar, sep, i, j) { |
f37162a4 SK |
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"] | |
270 | bar = "" | |
271 | sep = "" | |
272 | for (j = 1; j <= i; j++) { | |
273 | bar = bar sep position[j] | |
274 | sep = " " | |
275 | } | |
276 | return bar | |
277 | } | |
278 | ||
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 = ">" | |
285 | } else { | |
286 | direction_of_change = "=" | |
287 | }; | |
e16fe0ea | 288 | return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"]) |
f37162a4 SK |
289 | } |
290 | ||
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 | |
295 | if (total && used) { | |
296 | percent = round((used / total) * 100) | |
297 | status = sprintf("%d%%", percent) | |
298 | } else { | |
299 | status = "__" | |
300 | } | |
301 | return sprintf("M=%s", status) | |
302 | } | |
303 | ||
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) | |
309 | } | |
310 | ||
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 | |
316 | return \ | |
317 | sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r) | |
318 | } | |
319 | ||
320 | function make_status_net( \ | |
321 | out, | |
322 | number_of_interfaces_to_show, | |
323 | n, | |
324 | array_of_prefixes_of_interfaces_to_show, | |
325 | prefix, | |
326 | interface, | |
327 | label, | |
328 | count_printed, | |
329 | sep, | |
330 | io_stat, | |
331 | dw, dr, | |
332 | bytes_per_unit\ | |
333 | ) { | |
334 | out = "" | |
335 | number_of_interfaces_to_show = \ | |
336 | split(\ | |
337 | opt_prefixes_of_net_interfaces_to_show,\ | |
338 | array_of_prefixes_of_interfaces_to_show,\ | |
339 | ","\ | |
340 | ) | |
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) | |
351 | } else { | |
352 | io_stat = "--" | |
353 | } | |
354 | if (interface ~ "^w") { | |
355 | label = label ":" db["net_wifi_status"] | |
356 | } | |
357 | if (++count_printed > 1) { | |
358 | sep = " " | |
359 | } else { | |
360 | sep = "" | |
361 | } | |
362 | out = out sep label ":" io_stat | |
363 | } | |
364 | } | |
365 | } | |
366 | return sprintf("N[%s]", out) | |
367 | } | |
368 | ||
369 | function make_status_mpd( state, status) { | |
370 | state = db["mpd_state"] | |
371 | ||
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("⬛") | |
378 | } else { | |
379 | status = make_status_mpd_state_unknown("--") | |
380 | } | |
381 | ||
382 | return sprintf("[%s]", status) | |
383 | } | |
384 | ||
385 | function make_status_mpd_state_known(symbol) { | |
386 | return sprintf(\ | |
387 | "%s %s %s %s", | |
388 | 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)\ | |
392 | ) | |
393 | } | |
394 | ||
395 | function make_status_mpd_state_unknown(symbol) { | |
396 | return sprintf("%s", symbol) | |
397 | } | |
398 | ||
399 | function round(n) { | |
400 | return int(n + 0.5) | |
401 | } | |
402 | ||
e16fe0ea | 403 | function debug(location, values, sep, vals, key, msg) { |
f37162a4 | 404 | if (opt_debug) { |
e16fe0ea SK |
405 | sep = "" |
406 | vals = "" | |
407 | for (key in values) { | |
408 | vals = sprintf("%s%s%s: %s", vals, sep, key, values[key]) | |
409 | sep = ", " | |
410 | } | |
411 | msg = location " ==> " vals "." | |
412 | output_msg("DEBUG", msg, "/dev/stderr") | |
f37162a4 SK |
413 | } |
414 | } | |
e16fe0ea SK |
415 | |
416 | function ensure_numeric(n) { | |
417 | return n + 0 | |
418 | } | |
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. | |
427 | # | |
428 | # Demo: | |
429 | # | |
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"}}' | |
431 | # 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"}}' | |
433 | # 75 > 100 | |
434 | ||
435 | # However, once used as a number, seems to stay that way even after being | |
436 | # used as string: | |
437 | # | |
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"}}' | |
439 | # 75 < 100 | |
440 | # | |
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"}}' | |
442 | # 75 < 100 | |
443 | # | |
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"}}' | |
445 | # 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"}}' | |
447 | # 75 > 100 |