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