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