| 1 | #! /bin/bash |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | BIN=$HOME/bin |
| 6 | STATUS_DIR=$HOME/var/run/status |
| 7 | STATUS_FILE__WIFI=$STATUS_DIR/wifi |
| 8 | STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF=$STATUS_DIR/notified_energy_bellow_half |
| 9 | |
| 10 | load=$(awk '{printf("%4.2f", $1)}' /proc/loadavg) |
| 11 | |
| 12 | fan=$(awk '/^speed:/ {printf "%4d", $2}' /proc/acpi/ibm/fan) |
| 13 | |
| 14 | cpu=$($BIN/khatus_cpu_usage_from_proc_since_last_check) |
| 15 | |
| 16 | memory=$( |
| 17 | free \ |
| 18 | | awk ' |
| 19 | function round(n) {return int(n + 0.5)} |
| 20 | |
| 21 | $1 == "Mem:" { |
| 22 | total=$2; |
| 23 | used=$3; |
| 24 | cache=$6; |
| 25 | prev_file = "/home/siraaj/var/run/status/memory_used_percentage"; |
| 26 | curr = round(used / total * 100); |
| 27 | getline prev < prev_file; |
| 28 | print curr > prev_file; |
| 29 | if (curr > prev) { |
| 30 | direction = ">"; |
| 31 | } else if (curr < prev) { |
| 32 | direction = "<"; |
| 33 | } else { |
| 34 | direction = "="; |
| 35 | } |
| 36 | printf("%s%d%%", direction, curr); |
| 37 | }') |
| 38 | |
| 39 | temp=$(awk 'NR == 1 {print $1 / 1000}' /sys/class/thermal/thermal_zone0/temp) |
| 40 | |
| 41 | disk_io=$( |
| 42 | awk ' |
| 43 | { |
| 44 | bytes_per_sector = 512 |
| 45 | bytes_per_unit = 1024 * 1024 |
| 46 | |
| 47 | curr_sectors_read = $3 |
| 48 | curr_sectors_write = $7 |
| 49 | |
| 50 | prev_file_prefix = "/home/siraaj/var/run/status/disk_io" |
| 51 | prev_sectors_read_file = prev_file_prefix "_sectors_read" |
| 52 | prev_sectors_write_file = prev_file_prefix "_sectors_write" |
| 53 | |
| 54 | getline prev_sectors_read < prev_sectors_read_file |
| 55 | getline prev_sectors_write < prev_sectors_write_file |
| 56 | |
| 57 | diff_read_sectors = (curr_sectors_read - prev_sectors_read) |
| 58 | diff_write_sectors = (curr_sectors_write - prev_sectors_write) |
| 59 | |
| 60 | diff_read_bytes = diff_read_sectors * bytes_per_sector |
| 61 | diff_write_bytes = diff_write_sectors * bytes_per_sector |
| 62 | |
| 63 | diff_read = diff_read_bytes / bytes_per_unit |
| 64 | diff_write = diff_write_bytes / bytes_per_unit |
| 65 | |
| 66 | print curr_sectors_read > prev_sectors_read_file |
| 67 | print curr_sectors_write > prev_sectors_write_file |
| 68 | |
| 69 | printf("%0.3f▲ %0.3f▼\n", diff_write, diff_read); |
| 70 | |
| 71 | } |
| 72 | ' /sys/block/dm-1/stat |
| 73 | ) |
| 74 | |
| 75 | disk=$( |
| 76 | df \ |
| 77 | | awk -v disk_io="$disk_io" ' |
| 78 | function round(n) {return int(n + 0.5)} |
| 79 | |
| 80 | $1 == "/dev/mapper/kubuntu--vg-root" { |
| 81 | curr_perc = $5; sub("%$", "", curr_perc); |
| 82 | prev_perc_file = "/home/siraaj/var/run/status/disk_space_used"; |
| 83 | getline prev_perc < prev_perc_file; |
| 84 | print curr_perc > prev_perc_file; |
| 85 | if (curr_perc > prev_perc) { |
| 86 | direction = ">"; |
| 87 | } else if (curr_perc < prev_perc) { |
| 88 | direction = "<"; |
| 89 | } else { |
| 90 | direction = "="; |
| 91 | } |
| 92 | printf("%s[%d%% %s]", direction, curr_perc, disk_io); |
| 93 | }') |
| 94 | |
| 95 | network=$( |
| 96 | ip -s addr \ |
| 97 | | awk -v wifi_conn=$(cat $STATUS_FILE__WIFI) ' |
| 98 | BEGIN { |
| 99 | bytes_per_unit = 1024 * 1024 |
| 100 | } |
| 101 | |
| 102 | /^[0-9]+:/ { |
| 103 | sub(":$", "", $1) |
| 104 | sub(":$", "", $2) |
| 105 | sequence = $1 |
| 106 | interface = $2 |
| 107 | interfaces[sequence] = interface |
| 108 | } |
| 109 | |
| 110 | /^ +inet [0-9]/ { |
| 111 | sub("/[0-9]+", "", $2) |
| 112 | addr = $2 |
| 113 | addrs[interface] = addr |
| 114 | } |
| 115 | |
| 116 | /^ +RX: / {transfer_direction = "r"} |
| 117 | /^ +TX: / {transfer_direction = "w"} |
| 118 | |
| 119 | /^ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ *$/ { |
| 120 | io[interface, transfer_direction] = $1; |
| 121 | } |
| 122 | |
| 123 | END { |
| 124 | for (i=1; i<=sequence; i++) { |
| 125 | interface = interfaces[i] |
| 126 | if (addrs[interface]) { |
| 127 | curr_read = io[interface, "r"] |
| 128 | curr_write = io[interface, "w"] |
| 129 | |
| 130 | prefix = "/home/siraaj/var/run/status/io_net_" interface |
| 131 | prev_read_file = prefix "_read" |
| 132 | prev_write_file = prefix "_write" |
| 133 | |
| 134 | getline prev_read < prev_read_file |
| 135 | getline prev_write < prev_write_file |
| 136 | |
| 137 | diff_read = (curr_read - prev_read ) / bytes_per_unit |
| 138 | diff_written = (curr_write - prev_write) / bytes_per_unit |
| 139 | |
| 140 | print curr_read > prev_read_file |
| 141 | print curr_write > prev_write_file |
| 142 | |
| 143 | io_stat = sprintf("%0.3f▲ %0.3f▼", diff_written, diff_read) |
| 144 | } else { |
| 145 | io_stat = "--" |
| 146 | } |
| 147 | |
| 148 | label = substr(interface, 1, 1) |
| 149 | if (interface == "wlp3s0") { |
| 150 | label = label ":" wifi_conn |
| 151 | } |
| 152 | |
| 153 | if (i == sequence) { |
| 154 | sep = "" |
| 155 | } else { |
| 156 | sep = " | " |
| 157 | } |
| 158 | |
| 159 | printf("%s:%s%s", label, io_stat, sep) |
| 160 | } |
| 161 | }' |
| 162 | ) |
| 163 | |
| 164 | energy=$( |
| 165 | upower -e \ |
| 166 | | grep battery \ |
| 167 | | xargs upower -i \ |
| 168 | | awk ' |
| 169 | /^ +percentage: +/ {percentage=$2} |
| 170 | /^ +state: +/ {state=$2} |
| 171 | END { |
| 172 | if (state == "discharging") { |
| 173 | direction_of_change = "<" |
| 174 | } else if (state == "charging") { |
| 175 | direction_of_change = ">" |
| 176 | } else { |
| 177 | direction_of_change = "=" |
| 178 | }; |
| 179 | printf("%s%s", direction_of_change, percentage) |
| 180 | }') |
| 181 | |
| 182 | datetime=$(date +'%a, %b %d, %H:%M:%S') |
| 183 | |
| 184 | #volume_amixer=$( |
| 185 | # amixer get Master \ |
| 186 | # | tail -1 \ |
| 187 | # | awk ' |
| 188 | # { |
| 189 | # level = $4; |
| 190 | # sub("^\\[", "", level); |
| 191 | # sub("\\]$", "", level); |
| 192 | # print level; |
| 193 | # }' \ |
| 194 | # ) |
| 195 | |
| 196 | #volume_amixer=$( |
| 197 | # amixer get Master \ |
| 198 | # | tail -n 1 \ |
| 199 | # | awk '{print $4}' \ |
| 200 | # | tr -d '[]' |
| 201 | #) |
| 202 | |
| 203 | volume_pactl=$( |
| 204 | pactl list sinks \ |
| 205 | | awk ' |
| 206 | /^\tMute:/ { |
| 207 | printf("%s,", $0); |
| 208 | } |
| 209 | /^\tVolume:/ { |
| 210 | for (i=2; i<=NF; i++) printf(" %s", $i); |
| 211 | }' \ |
| 212 | | awk -v RS=',' ' |
| 213 | /^[ \t]*Mute:/ {mute = $2} |
| 214 | /^[ \t]*front-left:/ {left = $4} |
| 215 | /^[ \t]*front-right:/ {right = $4} |
| 216 | END { |
| 217 | if (mute == "yes") { |
| 218 | printf("x") |
| 219 | } else { |
| 220 | printf("%s %s", left, right) |
| 221 | } |
| 222 | } |
| 223 | ' |
| 224 | ) |
| 225 | |
| 226 | volume="[$volume_pactl]" |
| 227 | |
| 228 | screen_brightness=$( |
| 229 | max=$(cat /sys/class/backlight/acpi_video0/max_brightness) |
| 230 | cur=$(cat /sys/class/backlight/acpi_video0/brightness) |
| 231 | awk -v max=$max -v cur=$cur 'BEGIN {printf("%d%%", cur/max*100)}' |
| 232 | ) |
| 233 | |
| 234 | #bluetooth_status=$( |
| 235 | # grep '^status:' /proc/acpi/ibm/bluetooth \ |
| 236 | # | awk ' |
| 237 | # $2 == "disabled" {printf "off"} |
| 238 | # $2 == "enabled" {printf "on"} |
| 239 | # ' |
| 240 | #) |
| 241 | |
| 242 | bluetooth_power=$( |
| 243 | echo -e 'show \n quit' \ |
| 244 | | bluetoothctl \ |
| 245 | | awk ' |
| 246 | /^Controller / { |
| 247 | controller = $2; |
| 248 | controllers[++ctrl_count] = controller; |
| 249 | } |
| 250 | /^\t[A-Z][A-Za-z]+:/ { |
| 251 | key = $1; |
| 252 | sub(":$", "", key); |
| 253 | val = $2; |
| 254 | for (i=3; i<=NF; i++) { |
| 255 | val = val " " $i}; |
| 256 | data[controller, key] = val; |
| 257 | } |
| 258 | END { |
| 259 | # Using the 1st seen controller. Should we select specific instead? |
| 260 | power_status = data[controllers[1], "Powered"]; |
| 261 | if (ctrl_count > 0) { |
| 262 | if (power_status == "no") { |
| 263 | power_status = "off" |
| 264 | } else if (power_status == "yes") { |
| 265 | power_status = "on" |
| 266 | } else { |
| 267 | printf("Unexpected bluetooth power status: %s\n", power_status)\ |
| 268 | > "/dev/stderr"; |
| 269 | power_status = "ERROR" |
| 270 | } |
| 271 | } else { |
| 272 | power_status = "off" # TODO: Perhaps use differentiated marker? |
| 273 | } |
| 274 | printf("%s", power_status); |
| 275 | }' |
| 276 | ) |
| 277 | |
| 278 | #touchpad_status=$( |
| 279 | # xinput list-props 12 \ |
| 280 | # | awk ' |
| 281 | # /^\tDevice Enabled \([0-9]+\):/ { |
| 282 | # status = $4; |
| 283 | # printf("%s", status); |
| 284 | # }' |
| 285 | #) |
| 286 | |
| 287 | #color_off='\033[0m' |
| 288 | #color_on_bg_gray='\033[m\033[40m' |
| 289 | |
| 290 | energy_direction=$(echo "$energy" | cut -b 1) |
| 291 | energy_percentage=$(echo "$energy" | tr -d '<>=%') |
| 292 | if [[ "$energy_direction" = '<' ]] |
| 293 | then |
| 294 | if [[ $energy_percentage -le 5 ]] |
| 295 | then |
| 296 | DISPLAY=:0.0 notify-send \ |
| 297 | -u critical \ |
| 298 | "Energy CRITICALLY low: $energy" \ |
| 299 | 'CHARGE NOW!!! GO GO GO!!!' |
| 300 | elif [[ $energy_percentage -le 10 ]] |
| 301 | then |
| 302 | DISPLAY=:0.0 notify-send \ |
| 303 | -u critical \ |
| 304 | "Energy VERY low: $energy" \ |
| 305 | 'Plug it in ASAP.' |
| 306 | elif [[ $energy_percentage -le 15 ]] |
| 307 | then |
| 308 | DISPLAY=:0.0 notify-send \ |
| 309 | -u critical \ |
| 310 | "Energy low: $energy" \ |
| 311 | 'Get the charger.' |
| 312 | elif [[ $energy_percentage -le 50 ]] |
| 313 | then |
| 314 | if [[ ! -a "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF" ]] |
| 315 | then |
| 316 | DISPLAY=:0.0 notify-send \ |
| 317 | -u normal \ |
| 318 | "Energy bellow half: $energy" \ |
| 319 | 'Where is the charger?' |
| 320 | touch "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF" |
| 321 | fi |
| 322 | fi |
| 323 | else |
| 324 | rm -f "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF" |
| 325 | fi |
| 326 | |
| 327 | weather=$(awk 'NR == 1 {printf("%d°F", $1)}' ~/var/run/metar-KJFK-decoded-temp-fahrenheit) |
| 328 | |
| 329 | signal_last_msg_age=$( |
| 330 | ls -lt --time-style=+%s $HOME/var/lib/signal/latest_message.json \ |
| 331 | | awk -v now_seconds=$(date +%s) \ |
| 332 | '{ |
| 333 | mtime_seconds = $6; |
| 334 | seconds = now_seconds - mtime_seconds; |
| 335 | minutes = seconds / 60; |
| 336 | hours = minutes / 60; |
| 337 | days = hours / 24; |
| 338 | weeks = days / 7; |
| 339 | months = days / 30; |
| 340 | #fmt = "%.1f"; |
| 341 | fmt = "%d"; |
| 342 | printf(fmt " s\n", seconds); |
| 343 | printf(fmt " m\n", minutes); |
| 344 | printf(fmt " h\n", hours); |
| 345 | printf(fmt " d\n", days); |
| 346 | printf(fmt " w\n", weeks); |
| 347 | printf(fmt " mo\n", months); |
| 348 | }' \ |
| 349 | | awk '$1 >= 1' \ |
| 350 | | sort -n -k 1 \ |
| 351 | | head -1 \ |
| 352 | | tr -d ' ' |
| 353 | ) |
| 354 | |
| 355 | mpd_state=$( |
| 356 | echo 'status' \ |
| 357 | | nc 127.0.0.1 6600 \ |
| 358 | | awk ' |
| 359 | { |
| 360 | status[$1] = $2 |
| 361 | } |
| 362 | |
| 363 | END { |
| 364 | state = status["state:"] |
| 365 | symbol = "-" |
| 366 | if (state == "play") { |
| 367 | symbol = "▶" |
| 368 | } else if (state == "pause") { |
| 369 | symbol = "❚❚" |
| 370 | } else if (state == "stop") { |
| 371 | symbol = "⬛" |
| 372 | } |
| 373 | printf("%s", symbol) |
| 374 | } |
| 375 | ' |
| 376 | ) |
| 377 | |
| 378 | mpd_current=$( |
| 379 | echo 'currentsong' \ |
| 380 | | nc 127.0.0.1 6600 \ |
| 381 | | awk -v max_chars=10 ' |
| 382 | /^OK/ { |
| 383 | next |
| 384 | } |
| 385 | |
| 386 | { |
| 387 | key = $1 |
| 388 | val = $2 |
| 389 | for (i=3; i<=NF; i++) {val = val " " $i} |
| 390 | data[key] = val |
| 391 | } |
| 392 | |
| 393 | END { |
| 394 | if (data["Name:"]) { |
| 395 | out = data["Name:"] |
| 396 | } else { |
| 397 | file = data["file:"] |
| 398 | last = split(file, parts, "/") |
| 399 | out = parts[last] |
| 400 | } |
| 401 | printf("%s", substr(out, 1, max_chars)) |
| 402 | } |
| 403 | ' |
| 404 | ) |
| 405 | |
| 406 | graphics_card=$( |
| 407 | nvidia-smi \ |
| 408 | --format=csv,noheader,nounits \ |
| 409 | --query-gpu=memory.total,memory.used,temperature.gpu \ |
| 410 | | awk -F ',' ' |
| 411 | { |
| 412 | mem_total = $1; |
| 413 | mem_used = $2; |
| 414 | temp = $3; |
| 415 | mem_used_percent = (100 * mem_used) / mem_total; |
| 416 | printf("[%d%% %dC]", mem_used_percent, temp); |
| 417 | } |
| 418 | ' |
| 419 | ) |
| 420 | |
| 421 | echo \ |
| 422 | "\ |
| 423 | E$energy\ |
| 424 | \ |
| 425 | G=$graphics_card\ |
| 426 | \ |
| 427 | M$memory\ |
| 428 | \ |
| 429 | C=[$cpu ${temp}°C ${fan}rpm]\ |
| 430 | \ |
| 431 | L=$load\ |
| 432 | \ |
| 433 | D$disk\ |
| 434 | \ |
| 435 | N:[$network]\ |
| 436 | \ |
| 437 | B:$bluetooth_power\ |
| 438 | \ |
| 439 | S=$screen_brightness\ |
| 440 | \ |
| 441 | V=$volume\ |
| 442 | \ |
| 443 | [$mpd_state $mpd_current]\ |
| 444 | \ |
| 445 | $signal_last_msg_age\ |
| 446 | \ |
| 447 | $weather\ |
| 448 | \ |
| 449 | $datetime \ |
| 450 | " |