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