5 # - global, builtin : ALLCAPS
6 # - global, public : Camel_Snake_Man_Bear_Pig
7 # - global, private : _snake_case_prefixed_underscore
10 # - global, public : snake_case
13 FS = msg_fs ? msg_fs
: "|"
14 OFS = msg_fs ? msg_fs
: "|"
15 Kfs = key_fs ? key_fs
: ":"
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
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
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
38 # -----------------------------------------------------------------------------
40 # -----------------------------------------------------------------------------
46 $
2 ==
"khatus_sensor_datetime" {
47 print_msg_ok
("status_bar", make_status_bar
())
50 # -----------------------------------------------------------------------------
52 # -----------------------------------------------------------------------------
54 function cache_update
( src
, key
, val
, len_line
, len_head
, len_val
, time
) {
57 # Not just using $4 for val - because an unstructured value (like name of a
58 # song) might contain a character identical to FS
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) {
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
]
81 function cache_res_fmt_or_def
(result
, format
, default
) {
82 return result
["is_expired"] ? default
: sprintf(format
, result
["value"])
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
)
91 function cache_get_time
( src
, key
, time
) {
92 src =
"khatus_sensor_datetime"
94 time = _cache
[src
, key
]
95 _cache_atime
[src
, key
] = time
99 function cache_gc
( src_and_key
, unused_for
) {
100 for (src_and_key in _cache
) {
101 unused_for = cache_get_time
() - _cache_atime
[src_and_key
]
102 if (unused_for
> 3600) {
104 "cache_gc", "Deleting unused src_and_key: " src_and_key \
106 delete _cache
[src_and_key
]
111 function cache_maybe_total_to_diff
(src
, key
, val
, key_parts
) {
112 split(key
, key_parts
, Kfs
)
113 if (_total_to_diff
[src
, key_parts
[1]]) {
114 _prev
[src
, key
] = _curr
[src
, key
]
115 _curr
[src
, key
] = val
116 return (_curr
[src
, key
] - _prev
[src
, key
])
122 function cache_maybe_scale
(src
, key
, val
, key_parts
) {
123 split(key
, key_parts
, Kfs
)
124 if ((src SUBSEP key_parts
[1]) in _scale
) {
125 return val
* _scale
[src
, key_parts
[1]]
131 # -----------------------------------------------------------------------------
133 # -----------------------------------------------------------------------------
135 function make_status_bar
( position
, bar
, sep
, i
, j
) {
137 position
[++i
] = make_status_energy
()
138 position
[++i
] = make_status_mem
()
139 position
[++i
] = make_status_cpu
()
140 position
[++i
] = make_status_disk
()
141 position
[++i
] = make_status_net
()
142 position
[++i
] = make_status_bluetooth
()
143 position
[++i
] = make_status_screen_brightness
()
144 position
[++i
] = make_status_volume
()
145 position
[++i
] = make_status_mpd
()
146 position
[++i
] = make_status_weather
()
147 position
[++i
] = make_status_datetime
()
151 for (j =
1; j
<= i
; j
++) {
152 bar = bar sep position
[j
]
158 function make_status_energy
( state
, charge
, direction_of_change
) {
159 cache_get
(state
, "khatus_sensor_energy", "battery_state" , 0)
160 cache_get
(charge
, "khatus_sensor_energy", "battery_percentage", 0)
162 if (state
["value"] ==
"discharging") {
163 direction_of_change =
"<"
164 } else if (state
["value"] ==
"charging") {
165 direction_of_change =
">"
167 direction_of_change =
"="
170 return sprintf("E%s%d%%", direction_of_change
, charge
["value"])
173 function make_status_mem
( total
, used
, percent
, status
) {
174 cache_get
(total
, "khatus_sensor_memory", "total", 5)
175 cache_get
(used
, "khatus_sensor_memory", "used" , 5)
176 # Checking total["value"] to avoid division by zero when data is missing
177 if (!total
["is_expired"] && \
178 !used
["is_expired"] && \
181 percent = round
((used
["value"] / total
["value"]) * 100)
182 status =
sprintf("%d%%", percent
)
186 return sprintf("M=%s", status
)
189 function make_status_cpu
( l
, t
, f
) {
190 l_src =
"khatus_sensor_loadavg"
191 t_src =
"khatus_sensor_temperature"
192 f_src =
"khatus_sensor_fan"
193 l = cache_get_fmt_def
(l_src
, "load_avg_1min", 5, "%4.2f")
194 t = cache_get_fmt_def
(t_src
, "temp_c" , 5, "%d" )
195 f = cache_get_fmt_def
(f_src
, "speed" , 5, "%4d" )
196 return sprintf("C=[%s %s°C %srpm]", l
, t
, f
)
199 function make_status_disk
( u
, w
, r
, src_u
, src_io
) {
200 src_u =
"khatus_sensor_disk_space"
201 src_io =
"khatus_sensor_disk_io"
202 u = cache_get_fmt_def
(src_u
, "disk_usage_percentage", 10, "%s")
203 w = cache_get_fmt_def
(src_io
, "sectors_written" , 5, "%0.3f")
204 r = cache_get_fmt_def
(src_io
, "sectors_read" , 5, "%0.3f")
205 return sprintf("D=[%s%% %s▲ %s▼]", u
, w
, r
)
208 function make_status_net
( \
209 number_of_net_interfaces_to_show
, \
210 net_interfaces_to_show
, \
224 number_of_net_interfaces_to_show = \
225 split(Opt_Net_Interfaces_To_Show
, net_interfaces_to_show
, ",")
226 io =
"khatus_sensor_net_addr_io"
227 wi =
"khatus_sensor_net_wifi_status"
230 for (i = number_of_net_interfaces_to_show
; i
> 0; i
--) {
231 interface = net_interfaces_to_show
[i
]
232 label =
substr(interface
, 1, 1)
233 if (interface ~
"^w") {
234 wifi = cache_get_fmt_def
(wi
, "status" Kfs interface
, 10, "%s")
235 label = label
":" wifi
237 addr = cache_get_fmt_def
(io
, "addr" Kfs interface
, 5, "%s", "")
238 w = cache_get_fmt_def
(io
, "bytes_written" Kfs interface
, 5, "%0.3f")
239 r = cache_get_fmt_def
(io
, "bytes_read" Kfs interface
, 5, "%0.3f")
240 io_stat = addr ?
sprintf("%s▲ %s▼", w
, r
) : "--"
241 out = out sep label
":" io_stat
244 return sprintf("N[%s]", out
)
247 function make_status_bluetooth
( src
, key
) {
248 src =
"khatus_sensor_bluetooth_power"
250 return sprintf("B=%s", cache_get_fmt_def
(src
, key
, 10, "%s"))
253 function make_status_screen_brightness
( src
, key
) {
254 src =
"khatus_sensor_screen_brightness"
256 return sprintf("*%s%%", cache_get_fmt_def
(src
, key
, 5, "%d"))
259 function make_status_volume
( sink
, mu
, vl
, vr
, show
) {
260 sink = Opt_Pulseaudio_Sink
261 cache_get
(mu
, "khatus_sensor_volume", "mute" Kfs sink
, 5)
262 cache_get
(vl
, "khatus_sensor_volume", "vol_left" Kfs sink
, 5)
263 cache_get
(vr
, "khatus_sensor_volume", "vol_right" Kfs sink
, 5)
265 if (!mu
["is_expired"] && !vl
["is_expired"] && !vr
["is_expired"]) {
266 if (mu
["value"] ==
"yes") {show =
"X"}
267 else if (mu
["value"] ==
"no") {show = vl
["value"] " " vr
["value"]}
270 "make_status_volume", \
271 "Unexpected value for 'mute' field: " mu
["value"] \
275 return sprintf("(%s)", show
)
278 function make_status_mpd
( state
, status
) {
279 cache_get
(state
, "khatus_sensor_mpd", "state", 5)
280 if (!state
["is_expired"] && state
["value"]) {
281 if (state
["value"] ==
"play") {
282 status = make_status_mpd_state_known
("▶")
283 } else if (state
["value"] ==
"pause") {
284 status = make_status_mpd_state_known
("❚❚")
285 } else if (state
["value"] ==
"stop") {
286 status = make_status_mpd_state_known
("⬛")
290 "Unexpected value for 'state' field: " state
["value"] \
298 return sprintf("[%s]", status
)
301 function make_status_mpd_state_known
(symbol
, s
, song
, time
, percentage
) {
302 s =
"khatus_sensor_mpd"
303 song = cache_get_fmt_def
(s
, "song" , 5, "%s", "?")
304 time = cache_get_fmt_def
(s
, "play_time_minimal_units", 5, "%s", "?")
305 percent = cache_get_fmt_def
(s
, "play_time_percentage" , 5, "%s", "?")
306 song =
substr(song
, 1, Opt_Mpd_Song_Max_Chars
)
307 return sprintf("%s %s %s %s", symbol
, time
, percent
, song
)
310 function make_status_weather
( src
, hour
, t_f
) {
311 src =
"khatus_sensor_weather"
313 t_f = cache_get_fmt_def
(src
, "temperature_f", 3 * hour
, "%d")
314 return sprintf("%s°F", t_f
)
317 function make_status_datetime
( dt
) {
318 return cache_get_fmt_def
("khatus_sensor_datetime", "datetime", 5, "%s")
321 # -----------------------------------------------------------------------------
323 # -----------------------------------------------------------------------------
325 function print_msg_ok
(key
, val
) {
326 print_msg
("OK", key
, val
, "/dev/stdout")
329 function print_msg_info
(location
, msg
) {
330 print_msg
("INFO", location
, msg
, "/dev/stderr")
333 function print_msg_error
(location
, msg
) {
334 print_msg
("ERROR", location
, msg
, "/dev/stderr")
337 function print_msg
(status
, key
, val
, channel
) {
338 print(status
, "khatus_bar", key
, val
) > channel
341 # -----------------------------------------------------------------------------
343 # -----------------------------------------------------------------------------