Expose GC interval option
[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 unused_for = cache_get_time() - _cache_atime[src, key]
106 if (unused_for > GC_Interval) {
107 print_msg_info(\
108 "cache_gc",
109 sprintf("Deleting unused data SRC=%s KEY=%s", src, key) \
110 )
111 delete _cache[src, key]
112 }
113 }
114 }
115
116 function cache_maybe_total_to_diff(src, key, val, key_parts) {
117 split(key, key_parts, Kfs)
118 if (_total_to_diff[src, key_parts[1]]) {
119 _prev[src, key] = _curr[src, key]
120 _curr[src, key] = val
121 return (_curr[src, key] - _prev[src, key])
122 } else {
123 return val
124 }
125 }
126
127 function cache_maybe_scale(src, key, val, key_parts) {
128 split(key, key_parts, Kfs)
129 if ((src SUBSEP key_parts[1]) in _scale) {
130 return val * _scale[src, key_parts[1]]
131 } else {
132 return val
133 }
134 }
135
136 # -----------------------------------------------------------------------------
137 # Status bar
138 # -----------------------------------------------------------------------------
139
140 function make_status_bar( position, bar, sep, i, j) {
141 position[++i] = ""
142 position[++i] = make_status_energy()
143 position[++i] = make_status_mem()
144 position[++i] = make_status_cpu()
145 position[++i] = make_status_disk()
146 position[++i] = make_status_net()
147 position[++i] = make_status_bluetooth()
148 position[++i] = make_status_screen_brightness()
149 position[++i] = make_status_volume()
150 position[++i] = make_status_mpd()
151 position[++i] = make_status_weather()
152 position[++i] = make_status_datetime()
153 position[++i] = ""
154 bar = ""
155 sep = ""
156 for (j = 1; j <= i; j++) {
157 bar = bar sep position[j]
158 sep = " "
159 }
160 return bar
161 }
162
163 function make_status_energy( state, charge, direction_of_change) {
164 cache_get(state , "khatus_sensor_energy", "battery_state" , 0)
165 cache_get(charge, "khatus_sensor_energy", "battery_percentage", 0)
166
167 if (state["value"] == "discharging") {
168 direction_of_change = "<"
169 } else if (state["value"] == "charging") {
170 direction_of_change = ">"
171 } else {
172 direction_of_change = "="
173 }
174
175 return sprintf("E%s%d%%", direction_of_change, charge["value"])
176 }
177
178 function make_status_mem( total, used, percent, status) {
179 cache_get(total, "khatus_sensor_memory", "total", 5)
180 cache_get(used , "khatus_sensor_memory", "used" , 5)
181 # Checking total["value"] to avoid division by zero when data is missing
182 if (!total["is_expired"] && \
183 !used["is_expired"] && \
184 total["value"] \
185 ) {
186 percent = round((used["value"] / total["value"]) * 100)
187 status = sprintf("%d%%", percent)
188 } else {
189 status = "__"
190 }
191 return sprintf("M=%s", status)
192 }
193
194 function make_status_cpu( l, t, f) {
195 l_src = "khatus_sensor_loadavg"
196 t_src = "khatus_sensor_temperature"
197 f_src = "khatus_sensor_fan"
198 l = cache_get_fmt_def(l_src, "load_avg_1min", 5, "%4.2f")
199 t = cache_get_fmt_def(t_src, "temp_c" , 5, "%d" )
200 f = cache_get_fmt_def(f_src, "speed" , 5, "%4d" )
201 return sprintf("C=[%s %s°C %srpm]", l, t, f)
202 }
203
204 function make_status_disk( u, w, r, src_u, src_io) {
205 src_u = "khatus_sensor_disk_space"
206 src_io = "khatus_sensor_disk_io"
207 u = cache_get_fmt_def(src_u , "disk_usage_percentage", 10, "%s")
208 w = cache_get_fmt_def(src_io, "sectors_written" , 5, "%0.3f")
209 r = cache_get_fmt_def(src_io, "sectors_read" , 5, "%0.3f")
210 return sprintf("D=[%s%% %s▲ %s▼]", u, w, r)
211 }
212
213 function make_status_net( \
214 number_of_net_interfaces_to_show, \
215 net_interfaces_to_show, \
216 io, \
217 wi, \
218 i, \
219 interface, \
220 label, \
221 wifi, \
222 addr, \
223 w, \
224 r, \
225 io_stat, \
226 out, \
227 sep \
228 ) {
229 number_of_net_interfaces_to_show = \
230 split(Opt_Net_Interfaces_To_Show, net_interfaces_to_show, ",")
231 io = "khatus_sensor_net_addr_io"
232 wi = "khatus_sensor_net_wifi_status"
233 out = ""
234 sep = ""
235 for (i = number_of_net_interfaces_to_show; i > 0; i--) {
236 interface = net_interfaces_to_show[i]
237 label = substr(interface, 1, 1)
238 if (interface ~ "^w") {
239 wifi = cache_get_fmt_def(wi, "status" Kfs interface, 10, "%s")
240 label = label ":" wifi
241 }
242 addr = cache_get_fmt_def(io, "addr" Kfs interface, 5, "%s", "")
243 w = cache_get_fmt_def(io, "bytes_written" Kfs interface, 5, "%0.3f")
244 r = cache_get_fmt_def(io, "bytes_read" Kfs interface, 5, "%0.3f")
245 io_stat = addr ? sprintf("%s▲ %s▼", w, r) : "--"
246 out = out sep label ":" io_stat
247 sep = " "
248 }
249 return sprintf("N[%s]", out)
250 }
251
252 function make_status_bluetooth( src, key) {
253 src = "khatus_sensor_bluetooth_power"
254 key = "power_status"
255 return sprintf("B=%s", cache_get_fmt_def(src, key, 10, "%s"))
256 }
257
258 function make_status_screen_brightness( src, key) {
259 src = "khatus_sensor_screen_brightness"
260 key = "percentage"
261 return sprintf("*%s%%", cache_get_fmt_def(src, key, 5, "%d"))
262 }
263
264 function make_status_volume( sink, mu, vl, vr, show) {
265 sink = Opt_Pulseaudio_Sink
266 cache_get(mu, "khatus_sensor_volume", "mute" Kfs sink, 5)
267 cache_get(vl, "khatus_sensor_volume", "vol_left" Kfs sink, 5)
268 cache_get(vr, "khatus_sensor_volume", "vol_right" Kfs sink, 5)
269 show = "--"
270 if (!mu["is_expired"] && !vl["is_expired"] && !vr["is_expired"]) {
271 if (mu["value"] == "yes") {show = "X"}
272 else if (mu["value"] == "no") {show = vl["value"] " " vr["value"]}
273 else {
274 print_msg_error(\
275 "make_status_volume", \
276 "Unexpected value for 'mute' field: " mu["value"] \
277 )
278 }
279 }
280 return sprintf("(%s)", show)
281 }
282
283 function make_status_mpd( state, status) {
284 cache_get(state, "khatus_sensor_mpd", "state", 5)
285 if (!state["is_expired"] && state["value"]) {
286 if (state["value"] == "play") {
287 status = make_status_mpd_state_known("▶")
288 } else if (state["value"] == "pause") {
289 status = make_status_mpd_state_known("❚❚")
290 } else if (state["value"] == "stop") {
291 status = make_status_mpd_state_known("⬛")
292 } else {
293 print_msg_error(\
294 "make_status_mpd", \
295 "Unexpected value for 'state' field: " state["value"] \
296 )
297 status = "--"
298 }
299 } else {
300 status = "--"
301 }
302
303 return sprintf("[%s]", status)
304 }
305
306 function make_status_mpd_state_known(symbol, s, song, time, percentage) {
307 s = "khatus_sensor_mpd"
308 song = cache_get_fmt_def(s, "song" , 5, "%s", "?")
309 time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?")
310 percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?")
311 song = substr(song, 1, Opt_Mpd_Song_Max_Chars)
312 return sprintf("%s %s %s %s", symbol, time, percent, song)
313 }
314
315 function make_status_weather( src, hour, t_f) {
316 src = "khatus_sensor_weather"
317 hour = 60 * 60
318 t_f = cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
319 return sprintf("%s°F", t_f)
320 }
321
322 function make_status_datetime( dt) {
323 return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
324 }
325
326 # -----------------------------------------------------------------------------
327 # Output
328 # -----------------------------------------------------------------------------
329
330 function print_msg_ok(key, val) {
331 print_msg("OK", key, val, "/dev/stdout")
332 }
333
334 function print_msg_info(location, msg) {
335 print_msg("INFO", location, msg, "/dev/stderr")
336 }
337
338 function print_msg_error(location, msg) {
339 print_msg("ERROR", location, msg, "/dev/stderr")
340 }
341
342 function print_msg(status, key, val, channel) {
343 print(status, "khatus_bar", key, val) > channel
344 }
345
346 # -----------------------------------------------------------------------------
347 # Numbers
348 # -----------------------------------------------------------------------------
349
350 function round(n) {
351 return int(n + 0.5)
352 }
This page took 0.105789 seconds and 4 git commands to generate.