Use temp data directory by default
[khatus.git] / bin / khatus
1 #! /bin/bash
2
3 set -e
4
5 produce_energy() {
6 upower -e \
7 | grep battery \
8 | xargs upower -i \
9 | awk '
10 /^ +percentage: +/ { percentage=$2 }
11 /^ +state: +/ { state=$2 }
12 END { print(state, percentage) }
13 '
14 }
15
16
17 produce_memory() {
18 free | awk '$1 == "Mem:" {print $2, $3}'
19 }
20
21 produce_fan() {
22 fan_path="$1"
23 cat "$fan_path"
24 }
25
26 produce_temperature() {
27 thermal_zone="$1"
28 cat "/sys/class/thermal/thermal_zone${thermal_zone}/temp"
29 }
30
31 produce_loadavg() {
32 cat /proc/loadavg
33 }
34
35 produce_disk_io() {
36 disk_io_device="$1"
37 awk '
38 {
39 r = $3
40 w = $7
41 print w, r
42 }
43 ' "/sys/block/$disk_io_device/stat"
44 }
45
46 produce_disk_space() {
47 disk_space_device="$1"
48 df --output=pcent "$disk_space_device" | awk 'NR == 2 {print $1}'
49 }
50
51 produce_net_addr_io() {
52 ip -s addr \
53 | awk '
54 BEGIN {
55 bytes_per_unit = 1024 * 1024
56 }
57
58 /^[0-9]+:/ {
59 sub(":$", "", $1)
60 sub(":$", "", $2)
61 sequence = $1
62 interface = $2
63 interfaces[sequence] = interface
64 }
65
66 /^ +inet [0-9]/ {
67 sub("/[0-9]+", "", $2)
68 addr = $2
69 addrs[interface] = addr
70 }
71
72 /^ +RX: / {transfer_direction = "r"}
73 /^ +TX: / {transfer_direction = "w"}
74
75 /^ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ *$/ {
76 io[interface, transfer_direction] = $1;
77 }
78
79 END {
80 for (seq=1; seq<=sequence; seq++) {
81 interface = interfaces[seq]
82 label = substr(interface, 1, 1)
83 if (addrs[interface]) {
84 curr_read = io[interface, "r"]
85 curr_write = io[interface, "w"]
86 print(interface, addrs[interface], curr_write, curr_read)
87 } else {
88 print(interface)
89 }
90 }
91 }'
92 }
93
94 produce_net_wifi_status() {
95 nmcli \
96 -f ACTIVE,SSID,SIGNAL \
97 -t \
98 d wifi \
99 | awk \
100 -F ':' \
101 '
102 BEGIN {wifi_status = "--"}
103 $1 == "yes" {wifi_status = $2 ":" $3 "%"}
104 END {print wifi_status}
105 '
106 }
107
108 produce_bluetooth_power() {
109 echo -e 'show \n quit' \
110 | bluetoothctl \
111 | awk '
112 /^Controller / {
113 controller = $2;
114 controllers[++ctrl_count] = controller;
115 }
116 /^\t[A-Z][A-Za-z]+:/ {
117 key = $1;
118 sub(":$", "", key);
119 val = $2;
120 for (i=3; i<=NF; i++) {
121 val = val " " $i};
122 data[controller, key] = val;
123 }
124 END {
125 # Using the 1st seen controller. Should we select specific instead?
126 power_status = data[controllers[1], "Powered"];
127 if (ctrl_count > 0) {
128 if (power_status == "no") {
129 power_status = "off"
130 } else if (power_status == "yes") {
131 power_status = "on"
132 } else {
133 printf("Unexpected bluetooth power status: %s\n", power_status)\
134 > "/dev/stderr";
135 power_status = "ERROR"
136 }
137 } else {
138 power_status = "off" # TODO: Perhaps use differentiated marker?
139 }
140 printf("%s\n", power_status);
141 }'
142 }
143
144 produce_screen_brightness() {
145 screen_brightness_device_path="$1"
146 echo "\
147 $(cat $screen_brightness_device_path/max_brightness) \
148 $(cat $screen_brightness_device_path/brightness)\
149 "
150 }
151
152 produce_volume() {
153 pactl list sinks \
154 | awk '
155 /^\tMute:/ {
156 printf("%s,", $0);
157 }
158 /^\tVolume:/ {
159 for (i=2; i<=NF; i++) printf(" %s", $i);
160 }' \
161 | awk -v RS=',' '
162 /^[ \t]*Mute:/ {mute = $2}
163 /^[ \t]*front-left:/ {left = $4}
164 /^[ \t]*front-right:/ {right = $4}
165 END {
166 if (mute == "yes") {
167 print("x")
168 } else {
169 print("%s %s\n", left, right)
170 }
171 }
172 '
173 }
174
175 produce_mpd_state() {
176 echo 'status' \
177 | nc 127.0.0.1 6600 \
178 | awk '
179 {
180 status[$1] = $2
181 }
182
183 /^time: +[0-9]+:[0-9]+$/ {
184 split($2, time, ":")
185 seconds_current = time[1]
186 seconds_total = time[2]
187
188 hours = int(seconds_current / 60 / 60);
189 secs_beyond_hours = seconds_current - (hours * 60 * 60);
190 mins = int(secs_beyond_hours / 60);
191 secs = secs_beyond_hours - (mins * 60);
192 if (hours > 0) {
193 current_time = sprintf("%d:%.2d:%.2d", hours, mins, secs)
194 } else {
195 current_time = sprintf("%.2d:%.2d", mins, secs)
196 }
197
198 if (seconds_total > 0) {
199 time_percentage = (seconds_current / seconds_total) * 100
200 current_percentage = sprintf("%d%%", time_percentage)
201 } else {
202 current_percentage = "~"
203 }
204 }
205
206 END {
207 state = status["state:"]
208
209 if (state == "play") {
210 symbol = "▶"
211 } else if (state == "pause") {
212 symbol = "❚❚"
213 } else if (state == "stop") {
214 symbol = "⬛"
215 } else {
216 symbol = "--"
217 }
218
219 printf(\
220 "%s %s %s\n",
221 status["state:"], current_time, current_percentage\
222 )
223 }
224 '
225 }
226
227 produce_mpd_song() {
228 echo 'currentsong' \
229 | nc 127.0.0.1 6600 \
230 | awk '
231 /^OK/ {
232 next
233 }
234
235 {
236 key = $1
237 sub("^" key " +", "")
238 val = $0
239 data[key] = val
240 }
241
242 END {
243 name = data["Name:"]
244 title = data["Title:"]
245 file = data["file:"]
246
247 if (name) {
248 out = name
249 } else if (title) {
250 out = title
251 } else if (file) {
252 last = split(file, parts, "/")
253 out = parts[last]
254 } else {
255 out = ""
256 }
257 print out
258 }
259 '
260 }
261
262 produce_weather() {
263 weather_station_id="$1"
264 metar -d "$weather_station_id" 2>&1 \
265 | awk '
266 /METAR pattern not found in NOAA data/ {
267 failures++
268 }
269
270 /^Temperature/ {
271 celsius = $3;
272 fahrenheit = (celsius * (9 / 5)) + 32;
273 temperature = fahrenheit
274 }
275
276 END {
277 if (failures > 0) {
278 temperature = "--"
279 }
280 print temperature "°F"
281 }'
282 }
283
284 produce_datetime() {
285 date +'%a %b %d %H:%M:%S'
286 }
287
288 consume() {
289 pipe="$1"
290 debug="$2"
291 prefixes_of_net_interfaces_to_show="$3"
292 tail -f "$pipe" \
293 | stdbuf -o L awk \
294 -v opt_debug="$debug" \
295 -v opt_mpd_song_max_chars=10 \
296 -v opt_prefixes_of_net_interfaces_to_show="$prefixes_of_net_interfaces_to_show" \
297 '
298 /^in:ENERGY/\
299 {
300 split_msg_parts()
301 db["energy_state"] = $1
302 db["energy_percentage"] = $2
303 }
304
305 /^in:MEMORY/\
306 {
307 split_msg_parts()
308 db["memory_total"] = $1
309 db["memory_used"] = $2
310 }
311
312 /^in:FAN +status:/\
313 {
314 split_msg_parts()
315 db["fan_status"] = $2
316 }
317
318 /^in:FAN +speed:/\
319 {
320 split_msg_parts()
321 db["fan_speed"] = $2
322 }
323
324 /^in:FAN +level:/\
325 {
326 split_msg_parts()
327 db["fan_level"] = $2
328 }
329
330 /^in:TEMPERATURE/\
331 {
332 split_msg_parts()
333 db["temperature"] = $1
334 }
335
336 /^in:LOAD_AVG/\
337 {
338 split_msg_parts()
339 set_load_avg()
340 }
341
342 /^in:DISK_IO/\
343 {
344 split_msg_parts()
345 set_disk_io()
346 }
347
348 /^in:DISK_SPACE/\
349 {
350 split_msg_parts()
351 db["disk_space_used"] = msg_body
352 }
353
354 /^in:NET_ADDR_IO/\
355 {
356 split_msg_parts()
357 set_net_addr_io()
358 }
359
360 /^in:NET_WIFI_STATUS/\
361 {
362 split_msg_parts()
363 db["net_wifi_status"] = msg_body
364 }
365
366 /^in:BLUETOOTH_POWER/\
367 {
368 split_msg_parts()
369 db["bluetooth_power"] = msg_body
370 }
371
372 /^in:SCREEN_BRIGHTNESS/\
373 {
374 split_msg_parts()
375 set_screen_brightness()
376 }
377
378 /^in:VOLUME/\
379 {
380 split_msg_parts()
381 db["volume"] = msg_body
382 }
383
384 /^in:MPD_STATE/\
385 {
386 split_msg_parts()
387 db["mpd_state"] = $1
388 db["mpd_curr_song_time"] = $2
389 db["mpd_curr_song_percent"] = $3
390 }
391
392 /^in:MPD_SONG/\
393 {
394 split_msg_parts()
395 db["mpd_curr_song_name"] = msg_body
396 }
397
398 /^in:WEATHER/\
399 {
400 split_msg_parts()
401 db["weather_temperature"] = msg_body
402 }
403
404 /^in:DATE_TIME/\
405 {
406 split_msg_parts()
407 db["datetime"] = msg_body
408 }
409
410 /^out:BAR/\
411 {
412 split_msg_parts()
413 print make_bar()
414 }
415
416 function set_load_avg( sched) {
417 split($4, sched, "/")
418 db["load_avg_1min"] = $1
419 db["load_avg_5min"] = $2
420 db["load_avg_15min"] = $3
421 db["kern_sched_queue_runnable"] = sched[1]
422 db["kern_sched_queue_total"] = sched[2]
423 db["kern_sched_latest_pid"] = $5
424 }
425
426 function set_disk_io( curr_w, curr_r, prev_w, prev_r) {
427 curr_w = $1
428 curr_r = $2
429 prev_w = db["disk_io_curr_w"]
430 prev_r = db["disk_io_curr_r"]
431 db["disk_io_curr_w"] = curr_w
432 db["disk_io_curr_r"] = curr_r
433 db["disk_io_diff_w"] = curr_w - prev_w
434 db["disk_io_diff_r"] = curr_r - prev_r
435 }
436
437 function set_net_addr_io( \
438 interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\
439 ) {
440 interface = $1
441 address = $2
442 io_curr_w = $3
443 io_curr_r = $4
444 if (interface) {
445 if (address && io_curr_w && io_curr_r) {
446 # recalculate
447 io_prev_w = net_io_curr_w[interface]
448 io_prev_r = net_io_curr_r[interface]
449
450 net_addr[interface] = address
451 net_io_curr_w[interface] = io_curr_w
452 net_io_curr_r[interface] = io_curr_r
453 net_io_diff_w[interface] = io_curr_w - io_prev_w
454 net_io_diff_r[interface] = io_curr_r - io_prev_r
455 } else {
456 # clear
457 net_addr[interface] = ""
458 net_io_curr_w[interface] = 0
459 net_io_curr_r[interface] = 0
460 net_io_diff_w[interface] = 0
461 net_io_diff_r[interface] = 0
462 }
463 }
464 }
465
466 function set_screen_brightness( max, cur) {
467 max = $1
468 cur = $2
469 db["screen_brightness"] = (cur / max) * 100
470 }
471
472 function split_msg_parts() {
473 msg_head = $1
474 sub("^" msg_head " +", "")
475 msg_body = $0
476 debug(msg_head, msg_body)
477 }
478
479 function make_bar( position, bar, sep, i, j) {
480 position[++i] = make_status_energy()
481 position[++i] = make_status_mem()
482 position[++i] = make_status_cpu()
483 position[++i] = make_status_disk()
484 position[++i] = make_status_net()
485 position[++i] = sprintf("B=%s", db["bluetooth_power"])
486 position[++i] = sprintf("*%d%%", db["screen_brightness"])
487 position[++i] = sprintf("(%s)", db["volume"])
488 position[++i] = make_status_mpd()
489 position[++i] = db["weather_temperature"]
490 position[++i] = db["datetime"]
491 bar = ""
492 sep = ""
493 for (j = 1; j <= i; j++) {
494 bar = bar sep position[j]
495 sep = " "
496 }
497 return bar
498 }
499
500 function make_status_energy( state, direction_of_change) {
501 state = db["energy_state"]
502 if (state == "discharging") {
503 direction_of_change = "<"
504 } else if (state == "charging") {
505 direction_of_change = ">"
506 } else {
507 direction_of_change = "="
508 };
509 printf("E%s%s", direction_of_change, db["energy_percentage"])
510 }
511
512 function make_status_mem( total, used, percent, status) {
513 total = db["memory_total"]
514 used = db["memory_used"]
515 # To avoid division by zero when data is missing
516 if (total && used) {
517 percent = round((used / total) * 100)
518 status = sprintf("%d%%", percent)
519 } else {
520 status = "__"
521 }
522 return sprintf("M=%s", status)
523 }
524
525 function make_status_cpu( load, temp, fan) {
526 load = db["load_avg_1min"]
527 temp = db["temperature"] / 1000
528 fan = db["fan_speed"]
529 return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan)
530 }
531
532 function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) {
533 bytes_per_sector = 512
534 bytes_per_mb = 1024 * 1024
535 w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb
536 r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb
537 return \
538 sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r)
539 }
540
541 function make_status_net( \
542 out,
543 number_of_interfaces_to_show,
544 n,
545 array_of_prefixes_of_interfaces_to_show,
546 prefix,
547 interface,
548 label,
549 count_printed,
550 sep,
551 io_stat,
552 dw, dr,
553 bytes_per_unit\
554 ) {
555 out = ""
556 number_of_interfaces_to_show = \
557 split(\
558 opt_prefixes_of_net_interfaces_to_show,\
559 array_of_prefixes_of_interfaces_to_show,\
560 ","\
561 )
562 for (n = 1; n <= number_of_interfaces_to_show; n++) {
563 prefix = array_of_prefixes_of_interfaces_to_show[n]
564 for (interface in net_addr) {
565 if (interface ~ ("^" prefix)) {
566 label = substr(interface, 1, 1)
567 if (net_addr[interface]) {
568 bytes_per_mb = 1024 * 1024 # TODO: option
569 dw = net_io_diff_w[interface] / bytes_per_mb
570 dr = net_io_diff_r[interface] / bytes_per_mb
571 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
572 } else {
573 io_stat = "--"
574 }
575 if (interface ~ "^w") {
576 label = label ":" db["net_wifi_status"]
577 }
578 if (++count_printed > 1) {
579 sep = " "
580 } else {
581 sep = ""
582 }
583 out = out sep label ":" io_stat
584 }
585 }
586 }
587 return sprintf("N[%s]", out)
588 }
589
590 function make_status_mpd( state, status) {
591 state = db["mpd_state"]
592
593 if (state == "play") {
594 status = make_status_mpd_state_known("▶")
595 } else if (state == "pause") {
596 status = make_status_mpd_state_known("❚❚")
597 } else if (state == "stop") {
598 status = make_status_mpd_state_known("⬛")
599 } else {
600 status = make_status_mpd_state_unknown("--")
601 }
602
603 return sprintf("[%s]", status)
604 }
605
606 function make_status_mpd_state_known(symbol) {
607 return sprintf(\
608 "%s %s %s %s",
609 symbol,
610 db["mpd_curr_song_time"],
611 db["mpd_curr_song_percent"],
612 substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
613 )
614 }
615
616 function make_status_mpd_state_unknown(symbol) {
617 return sprintf("%s", symbol)
618 }
619
620 function round(n) {
621 return int(n + 0.5)
622 }
623
624 function debug(location, msg) {
625 if (opt_debug) {
626 print_error(location, msg)
627 }
628 }
629
630 function print_error(location, msg) {
631 print(location " ==> " msg) > "/dev/stderr"
632 }
633 '
634 }
635
636 produce_bar_req() {
637 echo ''
638 }
639
640 spawn() {
641 cmd="$1"
642 pipe="$2"
643 msg_head="$3"
644 interval="$4"
645 while true; do
646 $cmd | while read line; do
647 echo "${msg_head} $line" > "$pipe"
648 done
649 sleep "$interval"
650 done &
651 }
652
653 main() {
654 # Defaults
655 debug=0
656 dir_data=$(mktemp -d)
657 weather_station_id='KJFK'
658 screen_brightness_device_name='acpi_video0'
659 prefixes_of_net_interfaces_to_show='w' # comma-separated
660 disk_space_device='/'
661 disk_io_device='sda'
662 thermal_zone=0
663 fan_path='/proc/acpi/ibm/fan'
664
665 # User-overrides
666 long_options=''
667 long_options+='debug'
668 long_options+=',data_dir:'
669 long_options+=',weather_station:'
670 long_options+=',screen_device:'
671 long_options+=',prefixes_of_net_interfaces_to_show:'
672 long_options+=',disk_space_device:'
673 long_options+=',disk_io_device:'
674 long_options+=',thermal_zone:'
675 long_options+=',fan_path:'
676 OPTS=$(
677 getopt \
678 -o 'd' \
679 -l $long_options \
680 -- "$@"
681 )
682 eval set -- "$OPTS"
683 while true
684 do
685 case "$1" in
686 -d|--debug)
687 debug=1
688 shift
689 ;;
690 --data_dir)
691 dir_data="$2"
692 shift 2
693 ;;
694 --weather_station)
695 weather_station_id="$2"
696 shift 2
697 ;;
698 --screen_device)
699 screen_brightness_device_name="$2"
700 shift 2
701 ;;
702 --prefixes_of_net_interfaces_to_show)
703 prefixes_of_net_interfaces_to_show="$2"
704 shift 2
705 ;;
706 --disk_space_device)
707 disk_space_device="$2"
708 shift 2
709 ;;
710 --disk_io_device)
711 disk_io_device="$2"
712 shift 2
713 ;;
714 --thermal_zone)
715 thermal_zone="$2"
716 shift 2
717 ;;
718 --fan_path)
719 fan_path="$2"
720 shift 2
721 ;;
722 --)
723 shift
724 break
725 ;;
726 esac
727 done
728
729 pipe="$dir_data/khatus_data_pipe"
730 screen_brightness_device_path='/sys/class/backlight'
731 screen_brightness_device_path+="/$screen_brightness_device_name"
732
733 ( echo "Khatus starting with the following parameters:"
734 ( echo " debug|= $debug"
735 echo " dir_data|= $dir_data"
736 echo " pipe|= $pipe"
737 echo " screen_brightness_device_name|= $screen_brightness_device_name"
738 echo " screen_brightness_device_path|= $screen_brightness_device_path"
739 echo " weather_station_id|= $weather_station_id"
740 echo " prefixes_of_net_interfaces_to_show|= $prefixes_of_net_interfaces_to_show"
741 echo " disk_space_device|= $disk_space_device"
742 echo " disk_io_device|= $disk_io_device"
743 echo " thermal_zone|= $thermal_zone"
744 echo " fan_path|= $fan_path"
745 ) | column -ts\|
746 echo ''
747 ) >&2
748
749 mkdir -p "$dir_data"
750 rm -f "$pipe"
751 mkfifo "$pipe"
752
753 cmd_produce_screen_brightness='produce_screen_brightness'
754 cmd_produce_screen_brightness+=" $screen_brightness_device_path"
755
756 cmd_produce_weather="produce_weather $weather_station_id"
757
758 cmd_produce_disk_space="produce_disk_space $disk_space_device"
759
760 cmd_produce_disk_io="produce_disk_io $disk_io_device"
761
762 cmd_produce_temperature="produce_temperature $thermal_zone"
763
764 cmd_produce_fan="produce_fan $fan_path"
765
766 # TODO: Redirect each worker's stderr to a dedicated log file
767 spawn produce_datetime "$pipe" 'in:DATE_TIME' 1
768 spawn "$cmd_produce_screen_brightness" "$pipe" 'in:SCREEN_BRIGHTNESS' 1
769 spawn "$cmd_produce_weather" "$pipe" 'in:WEATHER' $(( 30 * 60 ))
770 spawn produce_mpd_state "$pipe" 'in:MPD_STATE' 1
771 spawn produce_mpd_song "$pipe" 'in:MPD_SONG' 1
772 spawn produce_volume "$pipe" 'in:VOLUME' 1
773 spawn produce_bluetooth_power "$pipe" 'in:BLUETOOTH_POWER' 5
774 spawn produce_net_wifi_status "$pipe" 'in:NET_WIFI_STATUS' 5
775 spawn produce_net_addr_io "$pipe" 'in:NET_ADDR_IO' 1
776 spawn "$cmd_produce_disk_space" "$pipe" 'in:DISK_SPACE' 1
777 spawn "$cmd_produce_disk_io" "$pipe" 'in:DISK_IO' 1
778 spawn produce_loadavg "$pipe" 'in:LOAD_AVG' 1
779 spawn "$cmd_produce_temperature" "$pipe" 'in:TEMPERATURE' 1
780 spawn "$cmd_produce_fan" "$pipe" 'in:FAN' 1
781 spawn produce_memory "$pipe" 'in:MEMORY' 1
782 spawn produce_energy "$pipe" 'in:ENERGY' 1
783 spawn produce_bar_req "$pipe" 'out:BAR' 1
784
785 consume \
786 "$pipe" \
787 "$debug" \
788 "$prefixes_of_net_interfaces_to_show"
789 }
790
791 main $@
This page took 0.16982 seconds and 5 git commands to generate.