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