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