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