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