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