6 free |
awk '$1 == "Mem:" {print $2, $3}'
14 produce_temperature
() {
16 cat "/sys/class/thermal/thermal_zone${thermal_zone}/temp"
31 ' "/sys/block/$disk_io_device/stat"
34 produce_disk_space
() {
35 disk_space_device
="$1"
36 df
--output=pcent
"$disk_space_device" |
awk 'NR == 2 {print $1}'
39 produce_net_addr_io
() {
43 bytes_per_unit = 1024 * 1024
51 interfaces[sequence] = interface
55 sub("/[0-9]+", "", $2)
57 addrs[interface] = addr
60 /^ +RX: / {transfer_direction = "r"}
61 /^ +TX: / {transfer_direction = "w"}
63 /^ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ *$/ {
64 io[interface, transfer_direction] = $1;
68 for (seq=1; seq<=sequence; seq++) {
69 interface = interfaces[seq]
70 label = substr(interface, 1, 1)
71 if (addrs[interface]) {
72 curr_read = io[interface, "r"]
73 curr_write = io[interface, "w"]
74 print(interface, addrs[interface], curr_write, curr_read)
82 produce_net_wifi_status
() {
84 -f ACTIVE
,SSID
,SIGNAL \
90 BEGIN {wifi_status = "--"}
91 $1 == "yes" {wifi_status = $2 ":" $3 "%"}
92 END {print wifi_status}
96 produce_bluetooth_power
() {
97 echo -e 'show \n quit' \
102 controllers[++ctrl_count] = controller;
104 /^\t[A-Z][A-Za-z]+:/ {
108 for (i=3; i<=NF; i++) {
110 data[controller, key] = val;
113 # Using the 1st seen controller. Should we select specific instead?
114 power_status = data[controllers[1], "Powered"];
115 if (ctrl_count > 0) {
116 if (power_status == "no") {
118 } else if (power_status == "yes") {
121 printf("Unexpected bluetooth power status: %s\n", power_status)\
123 power_status = "ERROR"
126 power_status = "off" # TODO: Perhaps use differentiated marker?
128 printf("%s\n", power_status);
132 produce_screen_brightness
() {
133 screen_brightness_device_path
="$1"
135 $(cat $screen_brightness_device_path/max_brightness) \
136 $(cat $screen_brightness_device_path/brightness)\
147 for (i=2; i<=NF; i++) printf(" %s", $i);
150 /^[ \t]*Mute:/ {mute = $2}
151 /^[ \t]*front-left:/ {left = $4}
152 /^[ \t]*front-right:/ {right = $4}
157 print("%s %s\n", left, right)
163 produce_mpd_state
() {
165 | nc
127.0.0.1 6600 \
171 /^time: +[0-9]+:[0-9]+$/ {
173 seconds_current = time[1]
174 seconds_total = time[2]
176 hours = int(seconds_current / 60 / 60);
177 secs_beyond_hours = seconds_current - (hours * 60 * 60);
178 mins = int(secs_beyond_hours / 60);
179 secs = secs_beyond_hours - (mins * 60);
181 current_time = sprintf("%d:%.2d:%.2d", hours, mins, secs)
183 current_time = sprintf("%.2d:%.2d", mins, secs)
186 if (seconds_total > 0) {
187 time_percentage = (seconds_current / seconds_total) * 100
188 current_percentage = sprintf("%d%%", time_percentage)
190 current_percentage = "~"
195 state = status["state:"]
197 if (state == "play") {
199 } else if (state == "pause") {
201 } else if (state == "stop") {
209 status["state:"], current_time, current_percentage\
217 | nc
127.0.0.1 6600 \
225 sub("^" key " +", "")
232 title = data["Title:"]
240 last = split(file, parts, "/")
251 weather_station_id
="$1"
252 metar
-d "$weather_station_id" 2>&1 \
254 /METAR pattern not found in NOAA data/ {
260 fahrenheit = (celsius * (9 / 5)) + 32;
261 temperature = fahrenheit
268 print temperature "°F"
273 date +'%a %b %d %H:%M:%S'
279 prefixes_of_net_interfaces_to_show
="$3"
282 -v opt_debug
="$debug" \
283 -v opt_mpd_song_max_chars
=10 \
284 -v opt_prefixes_of_net_interfaces_to_show
="$prefixes_of_net_interfaces_to_show" \
289 db["memory_total"] = $1
290 db["memory_used"] = $2
296 db["fan_status"] = $2
314 db["temperature"] = $1
332 db["disk_space_used"] = msg_body
341 /^in:NET_WIFI_STATUS/\
344 db["net_wifi_status"] = msg_body
347 /^in:BLUETOOTH_POWER/\
350 db["bluetooth_power"] = msg_body
353 /^in:SCREEN_BRIGHTNESS/\
356 set_screen_brightness()
362 db["volume"] = msg_body
369 db["mpd_curr_song_time"] = $2
370 db["mpd_curr_song_percent"] = $3
376 db["mpd_curr_song_name"] = msg_body
382 db["weather_temperature"] = msg_body
388 db["datetime"] = msg_body
397 function set_load_avg( sched) {
398 split($4, sched, "/")
399 db["load_avg_1min"] = $1
400 db["load_avg_5min"] = $2
401 db["load_avg_15min"] = $3
402 db["kern_sched_queue_runnable"] = sched[1]
403 db["kern_sched_queue_total"] = sched[2]
404 db["kern_sched_latest_pid"] = $5
407 function set_disk_io( curr_w, curr_r, prev_w, prev_r) {
410 prev_w = db["disk_io_curr_w"]
411 prev_r = db["disk_io_curr_r"]
412 db["disk_io_curr_w"] = curr_w
413 db["disk_io_curr_r"] = curr_r
414 db["disk_io_diff_w"] = curr_w - prev_w
415 db["disk_io_diff_r"] = curr_r - prev_r
418 function set_net_addr_io( \
419 interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\
426 if (address && io_curr_w && io_curr_r) {
428 io_prev_w = net_io_curr_w[interface]
429 io_prev_r = net_io_curr_r[interface]
431 net_addr[interface] = address
432 net_io_curr_w[interface] = io_curr_w
433 net_io_curr_r[interface] = io_curr_r
434 net_io_diff_w[interface] = io_curr_w - io_prev_w
435 net_io_diff_r[interface] = io_curr_r - io_prev_r
438 net_addr[interface] = ""
439 net_io_curr_w[interface] = 0
440 net_io_curr_r[interface] = 0
441 net_io_diff_w[interface] = 0
442 net_io_diff_r[interface] = 0
447 function set_screen_brightness( max, cur) {
450 db["screen_brightness"] = (cur / max) * 100
453 function split_msg_parts() {
455 sub("^" msg_head " +", "")
457 debug(msg_head, msg_body)
460 function make_bar( position, bar, sep, i, j) {
461 position[++i] = make_status_mem()
462 position[++i] = make_status_cpu()
463 position[++i] = make_status_disk()
464 position[++i] = make_status_net()
465 position[++i] = sprintf("B=%s", db["bluetooth_power"])
466 position[++i] = sprintf("*%d%%", db["screen_brightness"])
467 position[++i] = sprintf("(%s)", db["volume"])
468 position[++i] = make_status_mpd()
469 position[++i] = db["weather_temperature"]
470 position[++i] = db["datetime"]
473 for (j = 1; j <= i; j++) {
474 bar = bar sep position[j]
480 function make_status_mem( total, used, percent, status) {
481 total = db["memory_total"]
482 used = db["memory_used"]
483 # To avoid division by zero
485 percent = round((used / total) * 100)
486 status = sprintf("%d%%", percent)
490 return sprintf("M=%s", status)
493 function make_status_cpu( load, temp, fan) {
494 load = db["load_avg_1min"]
495 temp = db["temperature"] / 1000
496 fan = db["fan_speed"]
497 return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan)
500 function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) {
501 bytes_per_sector = 512
502 bytes_per_mb = 1024 * 1024
503 w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb
504 r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb
506 sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r)
509 function make_status_net( \
511 number_of_interfaces_to_show,
513 array_of_prefixes_of_interfaces_to_show,
524 number_of_interfaces_to_show = \
526 opt_prefixes_of_net_interfaces_to_show,\
527 array_of_prefixes_of_interfaces_to_show,\
530 for (n = 1; n <= number_of_interfaces_to_show; n++) {
531 prefix = array_of_prefixes_of_interfaces_to_show[n]
532 for (interface in net_addr) {
533 if (interface ~ ("^" prefix)) {
534 label = substr(interface, 1, 1)
535 if (net_addr[interface]) {
536 bytes_per_mb = 1024 * 1024 # TODO: option
537 dw = net_io_diff_w[interface] / bytes_per_mb
538 dr = net_io_diff_r[interface] / bytes_per_mb
539 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
543 if (interface ~ "^w") {
544 label = label ":" db["net_wifi_status"]
546 if (++count_printed > 1) {
551 out = out sep label ":" io_stat
555 return sprintf("N[%s]", out)
558 function make_status_mpd( state, status) {
559 state = db["mpd_state"]
561 if (state == "play") {
562 status = make_status_mpd_state_known("▶")
563 } else if (state == "pause") {
564 status = make_status_mpd_state_known("❚❚")
565 } else if (state == "stop") {
566 status = make_status_mpd_state_known("⬛")
568 status = make_status_mpd_state_unknown("--")
571 return sprintf("[%s]", status)
574 function make_status_mpd_state_known(symbol) {
578 db["mpd_curr_song_time"],
579 db["mpd_curr_song_percent"],
580 substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
584 function make_status_mpd_state_unknown(symbol) {
585 return sprintf("%s", symbol)
592 function debug(location, msg) {
594 print_error(location, msg)
598 function print_error(location, msg) {
599 print(location " ==> " msg) > "/dev/stderr"
614 $cmd |
while read line
; do
615 echo "${msg_head} $line" > "$pipe"
624 dir_data
="$HOME/.khatus"
625 weather_station_id
='KJFK'
626 screen_brightness_device_name
='acpi_video0'
627 prefixes_of_net_interfaces_to_show
='w' # comma-separated
628 disk_space_device
='/'
631 fan_path
='/proc/acpi/ibm/fan'
635 long_options
+='debug'
636 long_options
+=',data-dir:'
637 long_options
+=',weather-station:'
638 long_options
+=',screen-device:'
639 long_options
+=',prefixes_of_net_interfaces_to_show:'
640 long_options
+=',disk_space_device:'
641 long_options
+=',thermal_zone:'
642 long_options
+=',fan_path:'
662 weather_station_id
="$2"
666 screen_brightness_device_name
="$2"
669 --prefixes_of_net_interfaces_to_show)
670 prefixes_of_net_interfaces_to_show
="$2"
674 disk_space_device
="$2"
696 pipe
="$dir_data/khatus_data_pipe"
697 screen_brightness_device_path
='/sys/class/backlight'
698 screen_brightness_device_path
+="/$screen_brightness_device_name"
700 ( echo "Khatus starting with the following parameters:"
701 ( echo " debug|= $debug"
702 echo " dir_data|= $dir_data"
704 echo " screen_brightness_device_name|= $screen_brightness_device_name"
705 echo " screen_brightness_device_path|= $screen_brightness_device_path"
706 echo " weather_station_id|= $weather_station_id"
707 echo " prefixes_of_net_interfaces_to_show|= $prefixes_of_net_interfaces_to_show"
708 echo " disk_space_device|= $disk_space_device"
709 echo " disk_io_device|= $disk_io_device"
710 echo " thermal_zone|= $thermal_zone"
711 echo " fan_path|= $fan_path"
720 cmd_produce_screen_brightness
='produce_screen_brightness'
721 cmd_produce_screen_brightness
+=" $screen_brightness_device_path"
723 cmd_produce_weather
="produce_weather $weather_station_id"
725 cmd_produce_disk_space
="produce_disk_space $disk_space_device"
727 cmd_produce_disk_io
="produce_disk_io $disk_io_device"
729 cmd_produce_temperature
="produce_temperature $thermal_zone"
731 cmd_produce_fan
="produce_fan $fan_path"
733 # TODO: Redirect each worker's stderr to a dedicated log file
734 spawn produce_datetime
"$pipe" 'in:DATE_TIME' 1
735 spawn
"$cmd_produce_screen_brightness" "$pipe" 'in:SCREEN_BRIGHTNESS' 1
736 spawn
"$cmd_produce_weather" "$pipe" 'in:WEATHER' $
(( 30 * 60 ))
737 spawn produce_mpd_state
"$pipe" 'in:MPD_STATE' 1
738 spawn produce_mpd_song
"$pipe" 'in:MPD_SONG' 1
739 spawn produce_volume
"$pipe" 'in:VOLUME' 1
740 spawn produce_bluetooth_power
"$pipe" 'in:BLUETOOTH_POWER' 5
741 spawn produce_net_wifi_status
"$pipe" 'in:NET_WIFI_STATUS' 5
742 spawn produce_net_addr_io
"$pipe" 'in:NET_ADDR_IO' 1
743 spawn
"$cmd_produce_disk_space" "$pipe" 'in:DISK_SPACE' 1
744 spawn
"$cmd_produce_disk_io" "$pipe" 'in:DISK_IO' 1
745 spawn produce_loadavg
"$pipe" 'in:LOAD_AVG' 1
746 spawn
"$cmd_produce_temperature" "$pipe" 'in:TEMPERATURE' 1
747 spawn
"$cmd_produce_fan" "$pipe" 'in:FAN' 1
748 spawn produce_memory
"$pipe" 'in:MEMORY' 1
749 spawn produce_bar_req
"$pipe" 'out:BAR' 1
754 "$prefixes_of_net_interfaces_to_show"