| 1 | #! /usr/bin/awk -f |
| 2 | |
| 3 | |
| 4 | /^in:ENERGY/\ |
| 5 | { |
| 6 | fields_shift() |
| 7 | sub("%$", "", $2) |
| 8 | db["energy_state_prev"] = db["energy_state_curr"] |
| 9 | db["energy_state_curr"] = $1 |
| 10 | db["energy_percentage"] = ensure_numeric($2) |
| 11 | alert_check_energy() |
| 12 | } |
| 13 | |
| 14 | /^in:MEMORY/\ |
| 15 | { |
| 16 | fields_shift() |
| 17 | db["memory_total"] = $1 |
| 18 | db["memory_used"] = $2 |
| 19 | } |
| 20 | |
| 21 | /^in:FAN +status:/\ |
| 22 | { |
| 23 | fields_shift() |
| 24 | db["fan_status"] = $2 |
| 25 | } |
| 26 | |
| 27 | /^in:FAN +speed:/\ |
| 28 | { |
| 29 | fields_shift() |
| 30 | db["fan_speed"] = $2 |
| 31 | } |
| 32 | |
| 33 | /^in:FAN +level:/\ |
| 34 | { |
| 35 | fields_shift() |
| 36 | db["fan_level"] = $2 |
| 37 | } |
| 38 | |
| 39 | /^in:TEMPERATURE/\ |
| 40 | { |
| 41 | fields_shift() |
| 42 | db["temperature"] = $1 |
| 43 | } |
| 44 | |
| 45 | /^in:LOAD_AVG/\ |
| 46 | { |
| 47 | fields_shift() |
| 48 | set_load_avg() |
| 49 | } |
| 50 | |
| 51 | /^in:DISK_IO/\ |
| 52 | { |
| 53 | fields_shift() |
| 54 | set_disk_io() |
| 55 | } |
| 56 | |
| 57 | /^in:DISK_SPACE/\ |
| 58 | { |
| 59 | fields_shift() |
| 60 | db["disk_space_used"] = $0 |
| 61 | } |
| 62 | |
| 63 | /^in:NET_ADDR_IO/\ |
| 64 | { |
| 65 | fields_shift() |
| 66 | set_net_addr_io() |
| 67 | } |
| 68 | |
| 69 | /^in:NET_WIFI_STATUS/\ |
| 70 | { |
| 71 | fields_shift() |
| 72 | db["net_wifi_status"] = $0 |
| 73 | } |
| 74 | |
| 75 | /^in:BLUETOOTH_POWER/\ |
| 76 | { |
| 77 | fields_shift() |
| 78 | db["bluetooth_power"] = $0 |
| 79 | } |
| 80 | |
| 81 | /^in:SCREEN_BRIGHTNESS/\ |
| 82 | { |
| 83 | fields_shift() |
| 84 | set_screen_brightness() |
| 85 | } |
| 86 | |
| 87 | /^in:VOLUME/\ |
| 88 | { |
| 89 | fields_shift() |
| 90 | db["volume"] = $0 |
| 91 | } |
| 92 | |
| 93 | /^in:MPD_SONG OK +MPD/ { delete db_mpd_song; next } |
| 94 | /^in:MPD_SONG OK$/ { set_mpd_playing() ; next } |
| 95 | /^in:MPD_SONG / { set_mpd_song() ; next } |
| 96 | |
| 97 | /^in:MPD_STATE /\ |
| 98 | { |
| 99 | fields_shift() |
| 100 | db["mpd_status_state"] = $1 |
| 101 | db["mpd_status_time"] = $2 |
| 102 | db["mpd_status_percent"] = $3 |
| 103 | } |
| 104 | |
| 105 | /^in:WEATHER/\ |
| 106 | { |
| 107 | fields_shift() |
| 108 | db["weather_temperature"] = $0 |
| 109 | } |
| 110 | |
| 111 | /^in:DATE_TIME/\ |
| 112 | { |
| 113 | fields_shift() |
| 114 | db["datetime"] = $0 |
| 115 | output_msg_status_bar(make_status_bar()) |
| 116 | } |
| 117 | |
| 118 | function set_mpd_song( key, val) { |
| 119 | key = $2 |
| 120 | fields_shift() |
| 121 | fields_shift() |
| 122 | val = $0 |
| 123 | db_mpd_song[key] = val |
| 124 | debug("set_mpd_song", "", db_mpd_song) |
| 125 | } |
| 126 | |
| 127 | function set_mpd_playing( \ |
| 128 | currently_playing, name, title, file, last, parts\ |
| 129 | ) { |
| 130 | debug("set_mpd_playing", "", db_mpd_song) |
| 131 | name = db_mpd_song["Name:"] |
| 132 | title = db_mpd_song["Title:"] |
| 133 | file = db_mpd_song["file:"] |
| 134 | |
| 135 | if (name) { |
| 136 | currently_playing = name |
| 137 | } else if (title) { |
| 138 | currently_playing = title |
| 139 | } else if (file) { |
| 140 | last = split(file, parts, "/") |
| 141 | currently_playing = parts[last] |
| 142 | } else { |
| 143 | currently_playing = "" |
| 144 | } |
| 145 | db["mpd_playing_prev"] = db["mpd_playing_curr"] |
| 146 | db["mpd_playing_curr"] = currently_playing |
| 147 | |
| 148 | alert_check_mpd() |
| 149 | } |
| 150 | |
| 151 | function alert_check_mpd( curr, prev, name, body) { |
| 152 | prev = db["mpd_playing_prev"] |
| 153 | curr = db["mpd_playing_curr"] |
| 154 | if (curr && curr != prev) { |
| 155 | name = db_mpd_song["Name:"] |
| 156 | if (name) { |
| 157 | body = name |
| 158 | } else { |
| 159 | body = \ |
| 160 | db_mpd_song["Artist:"] \ |
| 161 | " - " db_mpd_song["Album:"] \ |
| 162 | " - " db_mpd_song["Title:"] |
| 163 | } |
| 164 | alert_trigger_low("alert_check_mpd", "NowPlaying", body) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | # TODO: Generalize alert spec lang |
| 169 | # - trigger threshold |
| 170 | # - above/bellow/equal to threshold value |
| 171 | # - priority |
| 172 | # - snooze time (if already alerted, when to re-alert?) |
| 173 | # - text: subject/body |
| 174 | function alert_check_energy( \ |
| 175 | from, dbg, state_curr, state_prev, remaining, subj, body\ |
| 176 | ) { |
| 177 | from = "alert_check_energy" |
| 178 | |
| 179 | state_curr = db["energy_state_curr"] |
| 180 | state_prev = db["energy_state_prev"] |
| 181 | remaining = db["energy_percentage"] |
| 182 | |
| 183 | dbg["state_curr"] = state_curr |
| 184 | dbg["remaining"] = remaining |
| 185 | debug(from, "", dbg) |
| 186 | |
| 187 | if (state_curr == "discharging") { |
| 188 | if (state_prev == "charging") { |
| 189 | alert_trigger_low(from, "PowerUnplugged", "") |
| 190 | } |
| 191 | |
| 192 | if (remaining < 5) { |
| 193 | subj = "Energy_CRITICALLY_Low" |
| 194 | body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining) |
| 195 | alert_trigger_hi(from, subj, body) |
| 196 | } else if (remaining < 10) { |
| 197 | subj = "Energy_Very_Low" |
| 198 | body = sprintf("%d%% Plug it in ASAP.", remaining) |
| 199 | alert_trigger_hi(from, subj, body) |
| 200 | } else if (remaining < 15) { |
| 201 | subj = "Energy_Low" |
| 202 | body = sprintf("%d%% Get the charger.", remaining) |
| 203 | alert_trigger_hi(from, subj, body) |
| 204 | } else if (remaining < 20) { |
| 205 | subj = "Energy_Low" |
| 206 | body = sprintf("%d%% Get the charger.", remaining) |
| 207 | alert_trigger_med(from, subj, body) |
| 208 | } else if (remaining < 50) { |
| 209 | if (!state__alerts__energy__notified_bellow_half) { |
| 210 | state__alerts__energy__notified_bellow_half = 1 |
| 211 | subj = "Energy_Bellow_Half" |
| 212 | body = sprintf("%d%% Where is the charger?", remaining) |
| 213 | alert_trigger_med(from, subj, body) |
| 214 | } |
| 215 | } |
| 216 | } else { |
| 217 | # TODO: Reconsider the competing global-state organizing-conventions |
| 218 | state__alerts__energy__notified_bellow_half = 0 |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | function alert_trigger_low(from, subject, body) { |
| 223 | alert_trigger("low", from, subject, body) |
| 224 | } |
| 225 | |
| 226 | function alert_trigger_med(from, subject, body) { |
| 227 | alert_trigger("med", from, subject, body) |
| 228 | } |
| 229 | |
| 230 | function alert_trigger_hi(from, subject, body) { |
| 231 | alert_trigger("hi", from, subject, body) |
| 232 | } |
| 233 | |
| 234 | function alert_trigger(priority, from, subject, body, msg) { |
| 235 | # priority : "low" | "med" | "hi" |
| 236 | # subject : no spaces |
| 237 | # body : anything |
| 238 | msg = sprintf("khatus_%s %s %s %s", from, priority, subject, body) |
| 239 | output_msg_alert(msg) |
| 240 | } |
| 241 | |
| 242 | function output_msg_alert(msg) { |
| 243 | # TODO: Should alerts go into a dedicated channel? |
| 244 | output_msg("ALERT", msg, "/dev/stdout") |
| 245 | } |
| 246 | |
| 247 | function output_msg_status_bar(msg) { |
| 248 | output_msg("STATUS_BAR", msg, "/dev/stdout") |
| 249 | } |
| 250 | |
| 251 | function output_msg(type, content, channel) { |
| 252 | print(type, content) > channel |
| 253 | } |
| 254 | |
| 255 | function set_load_avg( sched) { |
| 256 | split($4, sched, "/") |
| 257 | db["load_avg_1min"] = $1 |
| 258 | db["load_avg_5min"] = $2 |
| 259 | db["load_avg_15min"] = $3 |
| 260 | db["kern_sched_queue_runnable"] = sched[1] |
| 261 | db["kern_sched_queue_total"] = sched[2] |
| 262 | db["kern_sched_latest_pid"] = $5 |
| 263 | } |
| 264 | |
| 265 | function set_disk_io( curr_w, curr_r, prev_w, prev_r) { |
| 266 | curr_w = $1 |
| 267 | curr_r = $2 |
| 268 | prev_w = db["disk_io_curr_w"] |
| 269 | prev_r = db["disk_io_curr_r"] |
| 270 | db["disk_io_curr_w"] = curr_w |
| 271 | db["disk_io_curr_r"] = curr_r |
| 272 | db["disk_io_diff_w"] = curr_w - prev_w |
| 273 | db["disk_io_diff_r"] = curr_r - prev_r |
| 274 | } |
| 275 | |
| 276 | function set_net_addr_io( \ |
| 277 | interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\ |
| 278 | ) { |
| 279 | interface = $1 |
| 280 | address = $2 |
| 281 | io_curr_w = $3 |
| 282 | io_curr_r = $4 |
| 283 | if (interface) { |
| 284 | if (address && io_curr_w && io_curr_r) { |
| 285 | # recalculate |
| 286 | io_prev_w = net_io_curr_w[interface] |
| 287 | io_prev_r = net_io_curr_r[interface] |
| 288 | |
| 289 | net_addr[interface] = address |
| 290 | net_io_curr_w[interface] = io_curr_w |
| 291 | net_io_curr_r[interface] = io_curr_r |
| 292 | net_io_diff_w[interface] = io_curr_w - io_prev_w |
| 293 | net_io_diff_r[interface] = io_curr_r - io_prev_r |
| 294 | } else { |
| 295 | # clear |
| 296 | net_addr[interface] = "" |
| 297 | net_io_curr_w[interface] = 0 |
| 298 | net_io_curr_r[interface] = 0 |
| 299 | net_io_diff_w[interface] = 0 |
| 300 | net_io_diff_r[interface] = 0 |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | function set_screen_brightness( max, cur) { |
| 306 | max = $1 |
| 307 | cur = $2 |
| 308 | db["screen_brightness"] = (cur / max) * 100 |
| 309 | } |
| 310 | |
| 311 | function fields_shift( dbg) { |
| 312 | dbg["head"] = $1 |
| 313 | sub("^" $1 " +", "") |
| 314 | dbg["body"] = $0 |
| 315 | debug("fields_shift", "", dbg) |
| 316 | } |
| 317 | |
| 318 | function make_status_bar( position, bar, sep, i, j) { |
| 319 | position[++i] = make_status_energy() |
| 320 | position[++i] = make_status_mem() |
| 321 | position[++i] = make_status_cpu() |
| 322 | position[++i] = make_status_disk() |
| 323 | position[++i] = make_status_net() |
| 324 | position[++i] = sprintf("B=%s", db["bluetooth_power"]) |
| 325 | position[++i] = sprintf("*%d%%", db["screen_brightness"]) |
| 326 | position[++i] = sprintf("(%s)", db["volume"]) |
| 327 | position[++i] = make_status_mpd() |
| 328 | position[++i] = db["weather_temperature"] |
| 329 | position[++i] = db["datetime"] |
| 330 | bar = "" |
| 331 | sep = "" |
| 332 | for (j = 1; j <= i; j++) { |
| 333 | bar = bar sep position[j] |
| 334 | sep = " " |
| 335 | } |
| 336 | return bar |
| 337 | } |
| 338 | |
| 339 | function make_status_energy( state, direction_of_change) { |
| 340 | state = db["energy_state_curr"] |
| 341 | if (state == "discharging") { |
| 342 | direction_of_change = "<" |
| 343 | } else if (state == "charging") { |
| 344 | direction_of_change = ">" |
| 345 | } else { |
| 346 | direction_of_change = "=" |
| 347 | }; |
| 348 | return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"]) |
| 349 | } |
| 350 | |
| 351 | function make_status_mem( total, used, percent, status) { |
| 352 | total = db["memory_total"] |
| 353 | used = db["memory_used"] |
| 354 | # To avoid division by zero when data is missing |
| 355 | if (total && used) { |
| 356 | percent = round((used / total) * 100) |
| 357 | status = sprintf("%d%%", percent) |
| 358 | } else { |
| 359 | status = "__" |
| 360 | } |
| 361 | return sprintf("M=%s", status) |
| 362 | } |
| 363 | |
| 364 | function make_status_cpu( load, temp, fan) { |
| 365 | load = db["load_avg_1min"] |
| 366 | temp = db["temperature"] / 1000 |
| 367 | fan = db["fan_speed"] |
| 368 | return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan) |
| 369 | } |
| 370 | |
| 371 | function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) { |
| 372 | bytes_per_sector = 512 |
| 373 | bytes_per_mb = 1024 * 1024 |
| 374 | w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb |
| 375 | r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb |
| 376 | return \ |
| 377 | sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r) |
| 378 | } |
| 379 | |
| 380 | function make_status_net( \ |
| 381 | out, |
| 382 | number_of_interfaces_to_show, |
| 383 | n, |
| 384 | array_of_prefixes_of_interfaces_to_show, |
| 385 | prefix, |
| 386 | interface, |
| 387 | label, |
| 388 | count_printed, |
| 389 | sep, |
| 390 | io_stat, |
| 391 | dw, dr, |
| 392 | bytes_per_unit\ |
| 393 | ) { |
| 394 | out = "" |
| 395 | number_of_interfaces_to_show = \ |
| 396 | split(\ |
| 397 | opt_prefixes_of_net_interfaces_to_show,\ |
| 398 | array_of_prefixes_of_interfaces_to_show,\ |
| 399 | ","\ |
| 400 | ) |
| 401 | for (n = 1; n <= number_of_interfaces_to_show; n++) { |
| 402 | prefix = array_of_prefixes_of_interfaces_to_show[n] |
| 403 | for (interface in net_addr) { |
| 404 | if (interface ~ ("^" prefix)) { |
| 405 | label = substr(interface, 1, 1) |
| 406 | if (net_addr[interface]) { |
| 407 | bytes_per_mb = 1024 * 1024 # TODO: option |
| 408 | dw = net_io_diff_w[interface] / bytes_per_mb |
| 409 | dr = net_io_diff_r[interface] / bytes_per_mb |
| 410 | io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr) |
| 411 | } else { |
| 412 | io_stat = "--" |
| 413 | } |
| 414 | if (interface ~ "^w") { |
| 415 | label = label ":" db["net_wifi_status"] |
| 416 | } |
| 417 | if (++count_printed > 1) { |
| 418 | sep = " " |
| 419 | } else { |
| 420 | sep = "" |
| 421 | } |
| 422 | out = out sep label ":" io_stat |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | return sprintf("N[%s]", out) |
| 427 | } |
| 428 | |
| 429 | function make_status_mpd( state, status) { |
| 430 | state = db["mpd_status_state"] |
| 431 | |
| 432 | if (state == "play") { |
| 433 | status = make_status_mpd_state_known("▶") |
| 434 | } else if (state == "pause") { |
| 435 | status = make_status_mpd_state_known("❚❚") |
| 436 | } else if (state == "stop") { |
| 437 | status = make_status_mpd_state_known("⬛") |
| 438 | } else { |
| 439 | status = make_status_mpd_state_unknown("--") |
| 440 | } |
| 441 | |
| 442 | return sprintf("[%s]", status) |
| 443 | } |
| 444 | |
| 445 | function make_status_mpd_state_known(symbol) { |
| 446 | return sprintf(\ |
| 447 | "%s %s %s %s", |
| 448 | symbol, |
| 449 | db["mpd_status_time"], |
| 450 | db["mpd_status_percent"], |
| 451 | substr(db["mpd_playing_curr"], 1, opt_mpd_song_max_chars)\ |
| 452 | ) |
| 453 | } |
| 454 | |
| 455 | function make_status_mpd_state_unknown(symbol) { |
| 456 | return sprintf("%s", symbol) |
| 457 | } |
| 458 | |
| 459 | function round(n) { |
| 460 | return int(n + 0.5) |
| 461 | } |
| 462 | |
| 463 | function debug(location, msg, values, sep, vals, key, payload) { |
| 464 | if (opt_debug) { |
| 465 | sep = "" |
| 466 | vals = "" |
| 467 | for (key in values) { |
| 468 | vals = sprintf("%s%s%s: %s", vals, sep, key, values[key]) |
| 469 | sep = ", " |
| 470 | } |
| 471 | payload = sprintf("[%s] [%s] [%s]", location, msg, vals) |
| 472 | output_msg("DEBUG", payload, "/dev/stderr") |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | function ensure_numeric(n) { |
| 477 | return n + 0 |
| 478 | } |
| 479 | #------------------------------- |
| 480 | # Why do we need ensure_numeric? |
| 481 | #------------------------------- |
| 482 | # awk appears to be guessing the type of an inputted scalar based on usage, so |
| 483 | # if we read-in a number, but did not use it in any numeric operations, but did |
| 484 | # use as a string (even in just a format string!) - it will be treated as a |
| 485 | # string and can lead to REALLY SURPRISING behavior in conditional statements, |
| 486 | # where smaller number may compare as greater than the bigger ones, such as. |
| 487 | # |
| 488 | # Demo: |
| 489 | # |
| 490 | # $ 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"}}' |
| 491 | # 75 < 100 |
| 492 | # $ 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"}}' |
| 493 | # 75 > 100 |
| 494 | |
| 495 | # However, once used as a number, seems to stay that way even after being |
| 496 | # used as string: |
| 497 | # |
| 498 | # $ 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"}}' |
| 499 | # 75 < 100 |
| 500 | # |
| 501 | # $ 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"}}' |
| 502 | # 75 < 100 |
| 503 | # |
| 504 | # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}' |
| 505 | # 75 < 100 |
| 506 | # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}' |
| 507 | # 75 > 100 |