6fa05ba3e6d864725bf06494753e096cd44e4389
[khatus.git] / bin / khatus_bar
1 #! /usr/bin/awk -f
2
3 # Naming convention:
4 # Variables:
5 # - global, builtin : ALLCAPS
6 # - global, public : Camel_Snake_Man_Bear_Pig
7 # - global, private : _snake_case_prefixed_underscore
8 # - local : snake_case
9 # Functions:
10 # - global, public : snake_case
11
12 BEGIN {
13 FS = msg_fs ? msg_fs : "|"
14 OFS = msg_fs ? msg_fs : "|"
15 Kfs = key_fs ? key_fs : ":"
16 GC_Interval = GC_Interval ? GC_Interval : 3600 # seconds
17
18 _total_to_diff["khatus_sensor_net_addr_io", "bytes_read" ] = 1
19 _total_to_diff["khatus_sensor_net_addr_io", "bytes_written" ] = 1
20 _total_to_diff["khatus_sensor_disk_io" , "sectors_read" ] = 1
21 _total_to_diff["khatus_sensor_disk_io" , "sectors_written"] = 1
22
23 # (x * y) / z = x * w
24 # ==> w = y / z
25 # (x * bytes_per_sector) / bytes_per_mb = x * scaling_factor
26 # ==> scaling_factor = bytes_per_sector / bytes_per_mb
27 _bytes_per_sector = 512
28 _bytes_per_mb = 1024 * 1024
29 _scale["khatus_sensor_disk_io", "sectors_written"] = _bytes_per_sector / _bytes_per_mb
30 _scale["khatus_sensor_disk_io", "sectors_read" ] = _bytes_per_sector / _bytes_per_mb
31 # (x / y) = x * z
32 # ==> z = 1 / y
33 # x / bytes_per_mb = x * scaling_factor
34 # ==> scaling_factor = 1 / bytes_per_mb
35 _scale["khatus_sensor_net_addr_io", "bytes_written"] = 1 / _bytes_per_mb
36 _scale["khatus_sensor_net_addr_io", "bytes_read" ] = 1 / _bytes_per_mb
37 }
38
39 # -----------------------------------------------------------------------------
40 # Input
41 # -----------------------------------------------------------------------------
42 $1 == "OK" {
43 cache_update()
44 }
45
46 $1 == "OK" && \
47 $2 == "khatus_sensor_datetime" {
48 print_msg_ok("status_bar", make_status_bar())
49 }
50
51 # -----------------------------------------------------------------------------
52 # Cache
53 # -----------------------------------------------------------------------------
54
55 function cache_update( src, key, val, len_line, len_head, len_val, time) {
56 src = $2
57 key = $3
58 # Not just using $4 for val - because an unstructured value (like name of a
59 # song) might contain a character identical to FS
60 len_line = length($0)
61 len_head = length($1 FS $2 FS $3 FS)
62 len_val = len_line - len_head
63 val = substr($0, len_head + 1, len_val)
64 val = cache_maybe_total_to_diff(src, key, val)
65 val = cache_maybe_scale(src, key, val)
66 _cache[src, key] = val
67 time = cache_get_time()
68 _cache_mtime[src, key] = time
69 if (time % GC_Interval == 0) {
70 cache_gc()
71 }
72 }
73
74 function cache_get(result, src, key, ttl, time, age, is_expired) {
75 time = cache_get_time()
76 _cache_atime[src, key] = time
77 age = time - _cache_mtime[src, key]
78 result["is_expired"] = ttl && age > ttl # ttl = 0 => forever
79 result["value"] = _cache[src, key]
80 }
81
82 function cache_res_fmt_or_def(result, format, default) {
83 return result["is_expired"] ? default : sprintf(format, result["value"])
84 }
85
86 function cache_get_fmt_def(src, key, ttl, format, default, result) {
87 default = default ? default : "--"
88 cache_get(result, src, key, ttl)
89 return cache_res_fmt_or_def(result, format, default)
90 }
91
92 function cache_get_time( src, key, time) {
93 src = "khatus_sensor_datetime"
94 key = "epoch"
95 time = _cache[src, key]
96 _cache_atime[src, key] = time
97 return time
98 }
99
100 function cache_gc( src_and_key, parts, src, key, unused_for) {
101 for (src_and_key in _cache) {
102 split(src_and_key, parts, SUBSEP)
103 src = parts[1]
104 key = parts[2]
105 val = _cache[src, key]
106 unused_for = cache_get_time() - _cache_atime[src, key]
107 if (unused_for > GC_Interval) {
108 print_msg_info(\
109 "cache_gc",
110 sprintf(\
111 "Deleting unused data SRC=%s KEY=%s VAL=%s",
112 src, key, val\
113 ) \
114 )
115 delete _cache[src, key]
116 }
117 }
118 }
119
120 function cache_maybe_total_to_diff(src, key, val, key_parts) {
121 split(key, key_parts, Kfs)
122 if (_total_to_diff[src, key_parts[1]]) {
123 _prev[src, key] = _curr[src, key]
124 _curr[src, key] = val
125 return (_curr[src, key] - _prev[src, key])
126 } else {
127 return val
128 }
129 }
130
131 function cache_maybe_scale(src, key, val, key_parts) {
132 split(key, key_parts, Kfs)
133 if ((src SUBSEP key_parts[1]) in _scale) {
134 return val * _scale[src, key_parts[1]]
135 } else {
136 return val
137 }
138 }
139
140 # -----------------------------------------------------------------------------
141 # Status bar
142 # -----------------------------------------------------------------------------
143
144 function make_status_bar( position, bar, sep, i, j) {
145 position[++i] = ""
146 position[++i] = make_status_energy()
147 position[++i] = make_status_mem()
148 position[++i] = make_status_procs()
149 position[++i] = make_status_cpu()
150 position[++i] = make_status_disk()
151 position[++i] = make_status_net()
152 position[++i] = make_status_bluetooth()
153 position[++i] = make_status_screen_brightness()
154 position[++i] = make_status_volume()
155 position[++i] = make_status_mpd()
156 position[++i] = make_status_weather()
157 position[++i] = make_status_datetime()
158 position[++i] = ""
159 bar = ""
160 sep = ""
161 for (j = 1; j <= i; j++) {
162 bar = bar sep position[j]
163 sep = " "
164 }
165 return bar
166 }
167
168 function make_status_energy( state, charge, direction_of_change) {
169 cache_get(state , "khatus_sensor_energy", "battery_state" , 0)
170 cache_get(charge, "khatus_sensor_energy", "battery_percentage", 0)
171
172 if (state["value"] == "discharging") {
173 direction_of_change = "<"
174 } else if (state["value"] == "charging") {
175 direction_of_change = ">"
176 } else {
177 direction_of_change = "="
178 }
179
180 return sprintf("E%s%d%%", direction_of_change, charge["value"])
181 }
182
183 function make_status_mem( total, used, percent, status) {
184 cache_get(total, "khatus_sensor_memory", "total", 5)
185 cache_get(used , "khatus_sensor_memory", "used" , 5)
186 # Checking total["value"] to avoid division by zero when data is missing
187 if (!total["is_expired"] && \
188 !used["is_expired"] && \
189 total["value"] \
190 ) {
191 percent = round((used["value"] / total["value"]) * 100)
192 status = sprintf("%d%%", percent)
193 } else {
194 status = "__"
195 }
196 return sprintf("M=%s", status)
197 }
198
199 function make_status_procs() {
200 src = "khatus_sensor_procs"
201 all = cache_get_fmt_def(src, "total_procs" , 15, "%d")
202 r = cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0")
203 d = cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0")
204 t = cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0")
205 i = cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0")
206 z = cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0")
207 return sprintf("P=[%s %sr %sd %st %si %sz]", all, r, d, t, i, z)
208 }
209
210 function make_status_cpu( l, t, f) {
211 l_src = "khatus_sensor_loadavg"
212 t_src = "khatus_sensor_temperature"
213 f_src = "khatus_sensor_fan"
214 l = cache_get_fmt_def(l_src, "load_avg_1min", 5, "%4.2f")
215 t = cache_get_fmt_def(t_src, "temp_c" , 5, "%d" )
216 f = cache_get_fmt_def(f_src, "speed" , 5, "%4d" )
217 return sprintf("C=[%s %s°C %srpm]", l, t, f)
218 }
219
220 function make_status_disk( u, w, r, src_u, src_io) {
221 src_u = "khatus_sensor_disk_space"
222 src_io = "khatus_sensor_disk_io"
223 u = cache_get_fmt_def(src_u , "disk_usage_percentage", 10, "%s")
224 w = cache_get_fmt_def(src_io, "sectors_written" , 5, "%0.3f")
225 r = cache_get_fmt_def(src_io, "sectors_read" , 5, "%0.3f")
226 return sprintf("D=[%s%% %s▲ %s▼]", u, w, r)
227 }
228
229 function make_status_net( \
230 number_of_net_interfaces_to_show, \
231 net_interfaces_to_show, \
232 io, \
233 wi, \
234 i, \
235 interface, \
236 label, \
237 wifi, \
238 addr, \
239 w, \
240 r, \
241 io_stat, \
242 out, \
243 sep \
244 ) {
245 number_of_net_interfaces_to_show = \
246 split(Opt_Net_Interfaces_To_Show, net_interfaces_to_show, ",")
247 io = "khatus_sensor_net_addr_io"
248 wi = "khatus_sensor_net_wifi_status"
249 out = ""
250 sep = ""
251 for (i = number_of_net_interfaces_to_show; i > 0; i--) {
252 interface = net_interfaces_to_show[i]
253 label = substr(interface, 1, 1)
254 if (interface ~ "^w") {
255 wifi = cache_get_fmt_def(wi, "status" Kfs interface, 10, "%s")
256 label = label ":" wifi
257 }
258 addr = cache_get_fmt_def(io, "addr" Kfs interface, 5, "%s", "")
259 w = cache_get_fmt_def(io, "bytes_written" Kfs interface, 5, "%0.3f")
260 r = cache_get_fmt_def(io, "bytes_read" Kfs interface, 5, "%0.3f")
261 io_stat = addr ? sprintf("%s▲ %s▼", w, r) : "--"
262 out = out sep label ":" io_stat
263 sep = " "
264 }
265 return sprintf("N[%s]", out)
266 }
267
268 function make_status_bluetooth( src, key) {
269 src = "khatus_sensor_bluetooth_power"
270 key = "power_status"
271 return sprintf("B=%s", cache_get_fmt_def(src, key, 10, "%s"))
272 }
273
274 function make_status_screen_brightness( src, key) {
275 src = "khatus_sensor_screen_brightness"
276 key = "percentage"
277 return sprintf("*%s%%", cache_get_fmt_def(src, key, 5, "%d"))
278 }
279
280 function make_status_volume( sink, mu, vl, vr, show) {
281 sink = Opt_Pulseaudio_Sink
282 cache_get(mu, "khatus_sensor_volume", "mute" Kfs sink, 5)
283 cache_get(vl, "khatus_sensor_volume", "vol_left" Kfs sink, 5)
284 cache_get(vr, "khatus_sensor_volume", "vol_right" Kfs sink, 5)
285 show = "--"
286 if (!mu["is_expired"] && !vl["is_expired"] && !vr["is_expired"]) {
287 if (mu["value"] == "yes") {show = "X"}
288 else if (mu["value"] == "no") {show = vl["value"] " " vr["value"]}
289 else {
290 print_msg_error(\
291 "make_status_volume", \
292 "Unexpected value for 'mute' field: " mu["value"] \
293 )
294 }
295 }
296 return sprintf("(%s)", show)
297 }
298
299 function make_status_mpd( state, status) {
300 cache_get(state, "khatus_sensor_mpd", "state", 5)
301 if (!state["is_expired"] && state["value"]) {
302 if (state["value"] == "play") {
303 status = make_status_mpd_state_known("▶")
304 } else if (state["value"] == "pause") {
305 status = make_status_mpd_state_known("❚❚")
306 } else if (state["value"] == "stop") {
307 status = make_status_mpd_state_known("⬛")
308 } else {
309 print_msg_error(\
310 "make_status_mpd", \
311 "Unexpected value for 'state' field: " state["value"] \
312 )
313 status = "--"
314 }
315 } else {
316 status = "--"
317 }
318
319 return sprintf("[%s]", status)
320 }
321
322 function make_status_mpd_state_known(symbol, s, song, time, percentage) {
323 s = "khatus_sensor_mpd"
324 song = cache_get_fmt_def(s, "song" , 5, "%s", "?")
325 time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?")
326 percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?")
327 song = substr(song, 1, Opt_Mpd_Song_Max_Chars)
328 return sprintf("%s %s %s %s", symbol, time, percent, song)
329 }
330
331 function make_status_weather( src, hour, t_f) {
332 src = "khatus_sensor_weather"
333 hour = 60 * 60
334 t_f = cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
335 return sprintf("%s°F", t_f)
336 }
337
338 function make_status_datetime( dt) {
339 return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
340 }
341
342 # -----------------------------------------------------------------------------
343 # Output
344 # -----------------------------------------------------------------------------
345
346 function print_msg_ok(key, val) {
347 print_msg("OK", key, val, "/dev/stdout")
348 }
349
350 function print_msg_info(location, msg) {
351 print_msg("INFO", location, msg, "/dev/stderr")
352 }
353
354 function print_msg_error(location, msg) {
355 print_msg("ERROR", location, msg, "/dev/stderr")
356 }
357
358 function print_msg(status, key, val, channel) {
359 print(status, "khatus_bar", key, val) > channel
360 }
361
362 # -----------------------------------------------------------------------------
363 # Numbers
364 # -----------------------------------------------------------------------------
365
366 function round(n) {
367 return int(n + 0.5)
368 }
This page took 0.086645 seconds and 3 git commands to generate.