17 ' "/sys/block/$disk_io_device/stat"
20 produce_disk_space
() {
21 disk_space_device
="$1"
22 df
--output=pcent
"$disk_space_device" |
awk 'NR == 2 {print $1}'
25 produce_net_addr_io
() {
29 bytes_per_unit = 1024 * 1024
37 interfaces[sequence] = interface
41 sub("/[0-9]+", "", $2)
43 addrs[interface] = addr
46 /^ +RX: / {transfer_direction = "r"}
47 /^ +TX: / {transfer_direction = "w"}
49 /^ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ *$/ {
50 io[interface, transfer_direction] = $1;
54 for (seq=1; seq<=sequence; seq++) {
55 interface = interfaces[seq]
56 label = substr(interface, 1, 1)
57 if (addrs[interface]) {
58 curr_read = io[interface, "r"]
59 curr_write = io[interface, "w"]
60 print(interface, addrs[interface], curr_write, curr_read)
68 produce_net_wifi_status
() {
70 -f ACTIVE
,SSID
,SIGNAL \
76 BEGIN {wifi_status = "--"}
77 $1 == "yes" {wifi_status = $2 ":" $3 "%"}
78 END {print wifi_status}
82 produce_bluetooth_power
() {
83 echo -e 'show \n quit' \
88 controllers[++ctrl_count] = controller;
90 /^\t[A-Z][A-Za-z]+:/ {
94 for (i=3; i<=NF; i++) {
96 data[controller, key] = val;
99 # Using the 1st seen controller. Should we select specific instead?
100 power_status = data[controllers[1], "Powered"];
101 if (ctrl_count > 0) {
102 if (power_status == "no") {
104 } else if (power_status == "yes") {
107 printf("Unexpected bluetooth power status: %s\n", power_status)\
109 power_status = "ERROR"
112 power_status = "off" # TODO: Perhaps use differentiated marker?
114 printf("%s\n", power_status);
118 produce_screen_brightness
() {
119 screen_brightness_device_path
="$1"
121 $(cat $screen_brightness_device_path/max_brightness) \
122 $(cat $screen_brightness_device_path/brightness)\
133 for (i=2; i<=NF; i++) printf(" %s", $i);
136 /^[ \t]*Mute:/ {mute = $2}
137 /^[ \t]*front-left:/ {left = $4}
138 /^[ \t]*front-right:/ {right = $4}
143 print("%s %s\n", left, right)
149 produce_mpd_state
() {
151 | nc
127.0.0.1 6600 \
157 /^time: +[0-9]+:[0-9]+$/ {
159 seconds_current = time[1]
160 seconds_total = time[2]
162 hours = int(seconds_current / 60 / 60);
163 secs_beyond_hours = seconds_current - (hours * 60 * 60);
164 mins = int(secs_beyond_hours / 60);
165 secs = secs_beyond_hours - (mins * 60);
167 current_time = sprintf("%d:%.2d:%.2d", hours, mins, secs)
169 current_time = sprintf("%.2d:%.2d", mins, secs)
172 if (seconds_total > 0) {
173 time_percentage = (seconds_current / seconds_total) * 100
174 current_percentage = sprintf("%d%%", time_percentage)
176 current_percentage = "~"
181 state = status["state:"]
183 if (state == "play") {
185 } else if (state == "pause") {
187 } else if (state == "stop") {
195 status["state:"], current_time, current_percentage\
203 | nc
127.0.0.1 6600 \
211 sub("^" key " +", "")
218 title = data["Title:"]
226 last = split(file, parts, "/")
237 weather_station_id
="$1"
238 metar
-d "$weather_station_id" 2>&1 \
240 /METAR pattern not found in NOAA data/ {
246 fahrenheit = (celsius * (9 / 5)) + 32;
247 temperature = fahrenheit
254 print temperature "°F"
259 date +'%a %b %d %H:%M:%S'
265 prefixes_of_net_interfaces_to_show
="$3"
268 -v opt_debug
="$debug" \
269 -v opt_mpd_song_max_chars
=10 \
270 -v opt_prefixes_of_net_interfaces_to_show
="$prefixes_of_net_interfaces_to_show" \
287 db["disk_space_used"] = msg_body
296 /^in:NET_WIFI_STATUS/\
299 db["net_wifi_status"] = msg_body
302 /^in:BLUETOOTH_POWER/\
305 db["bluetooth_power"] = msg_body
308 /^in:SCREEN_BRIGHTNESS/\
311 set_screen_brightness()
317 db["volume"] = msg_body
324 db["mpd_curr_song_time"] = $2
325 db["mpd_curr_song_percent"] = $3
331 db["mpd_curr_song_name"] = msg_body
337 db["weather_temperature"] = msg_body
343 db["datetime"] = msg_body
352 function set_load_avg( sched) {
353 split($4, sched, "/")
354 db["load_avg_1min"] = $1
355 db["load_avg_5min"] = $2
356 db["load_avg_15min"] = $3
357 db["kern_sched_queue_runnable"] = sched[1]
358 db["kern_sched_queue_total"] = sched[2]
359 db["kern_sched_latest_pid"] = $5
362 function set_disk_io( curr_w, curr_r, prev_w, prev_r) {
365 prev_w = db["disk_io_curr_w"]
366 prev_r = db["disk_io_curr_r"]
367 db["disk_io_curr_w"] = curr_w
368 db["disk_io_curr_r"] = curr_r
369 db["disk_io_diff_w"] = curr_w - prev_w
370 db["disk_io_diff_r"] = curr_r - prev_r
373 function set_net_addr_io( \
374 interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\
381 if (address && io_curr_w && io_curr_r) {
383 io_prev_w = net_io_curr_w[interface]
384 io_prev_r = net_io_curr_r[interface]
386 net_addr[interface] = address
387 net_io_curr_w[interface] = io_curr_w
388 net_io_curr_r[interface] = io_curr_r
389 net_io_diff_w[interface] = io_curr_w - io_prev_w
390 net_io_diff_r[interface] = io_curr_r - io_prev_r
393 net_addr[interface] = ""
394 net_io_curr_w[interface] = 0
395 net_io_curr_r[interface] = 0
396 net_io_diff_w[interface] = 0
397 net_io_diff_r[interface] = 0
402 function set_screen_brightness( max, cur) {
405 db["screen_brightness"] = (cur / max) * 100
408 function split_msg_parts() {
410 sub("^" msg_head " +", "")
412 debug(msg_head, msg_body)
415 function make_bar( position, bar, sep, i, j) {
416 position[++i] = make_status_cpu()
417 position[++i] = make_status_disk()
418 position[++i] = make_status_net()
419 position[++i] = sprintf("B=%s", db["bluetooth_power"])
420 position[++i] = sprintf("*%d%%", db["screen_brightness"])
421 position[++i] = sprintf("(%s)", db["volume"])
422 position[++i] = make_status_mpd()
423 position[++i] = db["weather_temperature"]
424 position[++i] = db["datetime"]
427 for (j = 1; j <= i; j++) {
428 bar = bar sep position[j]
434 function make_status_cpu( load) {
435 load = db["load_avg_1min"]
436 return sprintf("C=[%4.2f]", load)
439 function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) {
440 bytes_per_sector = 512
441 bytes_per_mb = 1024 * 1024
442 w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb
443 r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb
445 sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r)
448 function make_status_net( \
450 number_of_interfaces_to_show,
452 array_of_prefixes_of_interfaces_to_show,
463 number_of_interfaces_to_show = \
465 opt_prefixes_of_net_interfaces_to_show,\
466 array_of_prefixes_of_interfaces_to_show,\
469 for (n = 1; n <= number_of_interfaces_to_show; n++) {
470 prefix = array_of_prefixes_of_interfaces_to_show[n]
471 for (interface in net_addr) {
472 if (interface ~ ("^" prefix)) {
473 label = substr(interface, 1, 1)
474 if (net_addr[interface]) {
475 bytes_per_mb = 1024 * 1024 # TODO: option
476 dw = net_io_diff_w[interface] / bytes_per_mb
477 dr = net_io_diff_r[interface] / bytes_per_mb
478 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
482 if (interface ~ "^w") {
483 label = label ":" db["net_wifi_status"]
485 if (++count_printed > 1) {
490 out = out sep label ":" io_stat
494 return sprintf("N[%s]", out)
497 function make_status_mpd( state, status) {
498 state = db["mpd_state"]
500 if (state == "play") {
501 status = make_status_mpd_state_known("▶")
502 } else if (state == "pause") {
503 status = make_status_mpd_state_known("❚❚")
504 } else if (state == "stop") {
505 status = make_status_mpd_state_known("⬛")
507 status = make_status_mpd_state_unknown("--")
510 return sprintf("[%s]", status)
513 function make_status_mpd_state_known(symbol) {
517 db["mpd_curr_song_time"],
518 db["mpd_curr_song_percent"],
519 substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
523 function make_status_mpd_state_unknown(symbol) {
524 return sprintf("%s", symbol)
527 function debug(location, msg) {
529 print_error(location, msg)
533 function print_error(location, msg) {
534 print(location " ==> " msg) > "/dev/stderr"
549 $cmd |
while read line
; do
550 echo "${msg_head} $line" > "$pipe"
559 dir_data
="$HOME/.khatus"
560 weather_station_id
='KJFK'
561 screen_brightness_device_name
='acpi_video0'
562 prefixes_of_net_interfaces_to_show
='w' # comma-separated
563 disk_space_device
='/'
568 long_options
+='debug'
569 long_options
+=',data-dir:'
570 long_options
+=',weather-station:'
571 long_options
+=',screen-device:'
572 long_options
+=',prefixes_of_net_interfaces_to_show:'
573 long_options
+=',disk_space_device:'
593 weather_station_id
="$2"
597 screen_brightness_device_name
="$2"
600 --prefixes_of_net_interfaces_to_show)
601 prefixes_of_net_interfaces_to_show
="$2"
605 disk_space_device
="$2"
619 pipe
="$dir_data/khatus_data_pipe"
620 screen_brightness_device_path
='/sys/class/backlight'
621 screen_brightness_device_path
+="/$screen_brightness_device_name"
623 ( echo "Khatus starting with the following parameters:"
624 ( echo " debug|= $debug"
625 echo " dir_data|= $dir_data"
627 echo " screen_brightness_device_name|= $screen_brightness_device_name"
628 echo " screen_brightness_device_path|= $screen_brightness_device_path"
629 echo " weather_station_id|= $weather_station_id"
630 echo " prefixes_of_net_interfaces_to_show|= $prefixes_of_net_interfaces_to_show"
631 echo " disk_space_device|= $disk_space_device"
632 echo " disk_io_device|= $disk_io_device"
641 cmd_produce_screen_brightness
='produce_screen_brightness'
642 cmd_produce_screen_brightness
+=" $screen_brightness_device_path"
644 cmd_produce_weather
="produce_weather $weather_station_id"
646 cmd_produce_disk_space
="produce_disk_space $disk_space_device"
648 cmd_produce_disk_io
="produce_disk_io $disk_io_device"
650 # TODO: Redirect each worker's stderr to a dedicated log file
651 spawn produce_datetime
"$pipe" 'in:DATE_TIME' 1
652 spawn
"$cmd_produce_screen_brightness" "$pipe" 'in:SCREEN_BRIGHTNESS' 1
653 spawn
"$cmd_produce_weather" "$pipe" 'in:WEATHER' $
(( 30 * 60 ))
654 spawn produce_mpd_state
"$pipe" 'in:MPD_STATE' 1
655 spawn produce_mpd_song
"$pipe" 'in:MPD_SONG' 1
656 spawn produce_volume
"$pipe" 'in:VOLUME' 1
657 spawn produce_bluetooth_power
"$pipe" 'in:BLUETOOTH_POWER' 5
658 spawn produce_net_wifi_status
"$pipe" 'in:NET_WIFI_STATUS' 5
659 spawn produce_net_addr_io
"$pipe" 'in:NET_ADDR_IO' 1
660 spawn
"$cmd_produce_disk_space" "$pipe" 'in:DISK_SPACE' 1
661 spawn
"$cmd_produce_disk_io" "$pipe" 'in:DISK_IO' 1
662 spawn produce_loadavg
"$pipe" 'in:LOAD_AVG' 1
663 spawn produce_bar_req
"$pipe" 'out:BAR' 1
668 "$prefixes_of_net_interfaces_to_show"