#! /usr/bin/awk -f
+# Naming convention:
+# Variables:
+# - global, builtin : ALLCAPS
+# - global, public : Camel_Snake_Man_Bear_Pig
+# - global, private : _snake_case_prefixed_underscore
+# - local : snake_case
+# Functions:
+# - global, public : snake_case
+
BEGIN {
FS = msg_fs ? msg_fs : "|"
OFS = msg_fs ? msg_fs : "|"
Kfs = key_fs ? key_fs : ":"
+ GC_Interval = GC_Interval ? GC_Interval : 3600 # seconds
_total_to_diff["khatus_sensor_net_addr_io", "bytes_read" ] = 1
_total_to_diff["khatus_sensor_net_addr_io", "bytes_written" ] = 1
_total_to_diff["khatus_sensor_disk_io" , "sectors_read" ] = 1
_total_to_diff["khatus_sensor_disk_io" , "sectors_written"] = 1
+
+ # (x * y) / z = x * w
+ # ==> w = y / z
+ # (x * bytes_per_sector) / bytes_per_mb = x * scaling_factor
+ # ==> scaling_factor = bytes_per_sector / bytes_per_mb
+ _bytes_per_sector = 512
+ _bytes_per_mb = 1024 * 1024
+ _scale["khatus_sensor_disk_io", "sectors_written"] = _bytes_per_sector / _bytes_per_mb
+ _scale["khatus_sensor_disk_io", "sectors_read" ] = _bytes_per_sector / _bytes_per_mb
+ # (x / y) = x * z
+ # ==> z = 1 / y
+ # x / bytes_per_mb = x * scaling_factor
+ # ==> scaling_factor = 1 / bytes_per_mb
+ _scale["khatus_sensor_net_addr_io", "bytes_written"] = 1 / _bytes_per_mb
+ _scale["khatus_sensor_net_addr_io", "bytes_read" ] = 1 / _bytes_per_mb
}
# -----------------------------------------------------------------------------
# Input
# -----------------------------------------------------------------------------
$1 == "OK" {
- Data_update()
+ cache_update()
}
$1 == "OK" && \
}
# -----------------------------------------------------------------------------
-# Data
+# Cache
# -----------------------------------------------------------------------------
-function Data_update( src, key, val, len_line, len_head, len_val, time) {
+function cache_update( src, key, val, len_line, len_head, len_val, time) {
src = $2
key = $3
-
# Not just using $4 for val - because an unstructured value (like name of a
# song) might contain a character identical to FS
len_line = length($0)
len_head = length($1 FS $2 FS $3 FS)
len_val = len_line - len_head
val = substr($0, len_head + 1, len_val)
+ val = cache_maybe_total_to_diff(src, key, val)
+ val = cache_maybe_scale(src, key, val)
+ _cache[src, key] = val
+ time = cache_get_time()
+ _cache_mtime[src, key] = time
+ if (time % GC_Interval == 0) {
+ cache_gc()
+ }
+}
- Data[src, key] = Data_maybe_total_to_diff(src, key, val)
- time = Data_get_time()
- M_time[src, key] = time
+function cache_get(result, src, key, ttl, time, age, is_expired) {
+ time = cache_get_time()
+ _cache_atime[src, key] = time
+ age = time - _cache_mtime[src, key]
+ result["is_expired"] = ttl && age > ttl # ttl = 0 => forever
+ result["value"] = _cache[src, key]
+}
- if (time % 3600 == 0) {
- Data_gc()
- }
+function cache_res_fmt_or_def(result, format, default) {
+ return result["is_expired"] ? default : sprintf(format, result["value"])
}
-function Data_get(src, key, ttl, time, age, is_expired) {
- time = Data_get_time()
- A_time[src, key] = time
- age = time - M_time[src, key]
- is_expired = ttl && age > ttl
- return is_expired ? "" : Data[src, key]
+function cache_get_fmt_def(src, key, ttl, format, default, result) {
+ default = default ? default : "--"
+ cache_get(result, src, key, ttl)
+ return cache_res_fmt_or_def(result, format, default)
}
-function Data_get_time( src, key, time) {
+function cache_get_time( src, key, time) {
src = "khatus_sensor_datetime"
key = "epoch"
- time = Data[src, key]
- A_time[src, key] = time
+ time = _cache[src, key]
+ _cache_atime[src, key] = time
return time
}
-function Data_gc( src_and_key, unused_for) {
- for (src_and_key in Data) {
- unused_for = Data_get_time() - A_time[src_and_key]
- if (unused_for > 3600) {
- print_msg_error(\
- "Data_gc", "Deleting unused src_and_key: " src_and_key \
+function cache_gc( src_and_key, parts, src, key, unused_for) {
+ for (src_and_key in _cache) {
+ split(src_and_key, parts, SUBSEP)
+ src = parts[1]
+ key = parts[2]
+ val = _cache[src, key]
+ unused_for = cache_get_time() - _cache_atime[src, key]
+ if (unused_for > GC_Interval) {
+ print_msg_info(\
+ "cache_gc",
+ sprintf(\
+ "Deleting unused data SRC=%s KEY=%s VAL=%s",
+ src, key, val\
+ ) \
)
- delete Data[src_and_key]
+ delete _cache[src, key]
}
}
}
-function Data_maybe_total_to_diff(src, key, val, key_parts) {
+function cache_maybe_total_to_diff(src, key, val, key_parts) {
split(key, key_parts, Kfs)
if (_total_to_diff[src, key_parts[1]]) {
_prev[src, key] = _curr[src, key]
}
}
+function cache_maybe_scale(src, key, val, key_parts) {
+ split(key, key_parts, Kfs)
+ if ((src SUBSEP key_parts[1]) in _scale) {
+ return val * _scale[src, key_parts[1]]
+ } else {
+ return val
+ }
+}
+
# -----------------------------------------------------------------------------
# Status bar
# -----------------------------------------------------------------------------
position[++i] = ""
position[++i] = make_status_energy()
position[++i] = make_status_mem()
+ position[++i] = make_status_procs()
position[++i] = make_status_cpu()
position[++i] = make_status_disk()
position[++i] = make_status_net()
}
function make_status_energy( state, charge, direction_of_change) {
- state = Data_get("khatus_sensor_energy", "battery_state")
- charge = Data_get("khatus_sensor_energy", "battery_percentage")
+ cache_get(state , "khatus_sensor_energy", "battery_state" , 0)
+ cache_get(charge, "khatus_sensor_energy", "battery_percentage", 0)
- if (state == "discharging") {
+ if (state["value"] == "discharging") {
direction_of_change = "<"
- } else if (state == "charging") {
+ } else if (state["value"] == "charging") {
direction_of_change = ">"
} else {
direction_of_change = "="
}
- return sprintf("E%s%d%%", direction_of_change, charge)
+ return sprintf("E%s%d%%", direction_of_change, charge["value"])
}
function make_status_mem( total, used, percent, status) {
- total = Data_get("khatus_sensor_memory", "total", 5)
- used = Data_get("khatus_sensor_memory", "used" , 5)
- # To avoid division by zero when data is missing
- if (total && used) {
- percent = round((used / total) * 100)
+ cache_get(total, "khatus_sensor_memory", "total", 5)
+ cache_get(used , "khatus_sensor_memory", "used" , 5)
+ # Checking total["value"] to avoid division by zero when data is missing
+ if (!total["is_expired"] && \
+ !used["is_expired"] && \
+ total["value"] \
+ ) {
+ percent = round((used["value"] / total["value"]) * 100)
status = sprintf("%d%%", percent)
} else {
status = "__"
return sprintf("M=%s", status)
}
-function make_status_cpu( load, temp, fan) {
- load = Data_get("khatus_sensor_loadavg" , "load_avg_1min", 5)
- temp = Data_get("khatus_sensor_temperature", "temp_c" , 5)
- fan = Data_get("khatus_sensor_fan" , "speed" , 5)
-
- load = load ? sprintf("%4.2f", load) : "--"
- temp = temp ? sprintf("%d" , temp) : "--"
- fan = fan ? sprintf("%4d" , fan) : "--"
-
- return sprintf("C=[%s %s°C %srpm]", load, temp, fan)
+function make_status_procs() {
+ src = "khatus_sensor_procs"
+ all = cache_get_fmt_def(src, "total_procs" , 15, "%d")
+ r = cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0")
+ d = cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0")
+ t = cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0")
+ i = cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0")
+ z = cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0")
+ return sprintf("P=[%s %sr %sd %st %si %sz]", all, r, d, t, i, z)
}
-function make_status_disk( bytes_per_sector, bytes_per_mb, w, r, u) {
- bytes_per_sector = 512
- bytes_per_mb = 1024 * 1024
-
- w = Data_get("khatus_sensor_disk_io" , "sectors_written" , 5)
- r = Data_get("khatus_sensor_disk_io" , "sectors_read" , 5)
- u = Data_get("khatus_sensor_disk_space", "disk_usage_percentage", 10)
-
- w = w ? sprintf("%0.3f", (w * bytes_per_sector) / bytes_per_mb) : "--"
- r = r ? sprintf("%0.3f", (r * bytes_per_sector) / bytes_per_mb) : "--"
- u = u ? u : "--"
+function make_status_cpu( l, t, f) {
+ l_src = "khatus_sensor_loadavg"
+ t_src = "khatus_sensor_temperature"
+ f_src = "khatus_sensor_fan"
+ l = cache_get_fmt_def(l_src, "load_avg_1min", 5, "%4.2f")
+ t = cache_get_fmt_def(t_src, "temp_c" , 5, "%d" )
+ f = cache_get_fmt_def(f_src, "speed" , 5, "%4d" )
+ return sprintf("C=[%s %s°C %srpm]", l, t, f)
+}
+function make_status_disk( u, w, r, src_u, src_io) {
+ src_u = "khatus_sensor_disk_space"
+ src_io = "khatus_sensor_disk_io"
+ u = cache_get_fmt_def(src_u , "disk_usage_percentage", 10, "%s")
+ w = cache_get_fmt_def(src_io, "sectors_written" , 5, "%0.3f")
+ r = cache_get_fmt_def(src_io, "sectors_read" , 5, "%0.3f")
return sprintf("D=[%s%% %s▲ %s▼]", u, w, r)
}
function make_status_net( \
number_of_net_interfaces_to_show, \
net_interfaces_to_show, \
- sensor_io, \
- sensor_wi, \
- out, \
- sep, \
+ io, \
+ wi, \
i, \
interface, \
label, \
+ wifi, \
addr, \
w, \
r, \
- bytes_per_mb, \
io_stat, \
- wifi \
+ out, \
+ sep \
) {
number_of_net_interfaces_to_show = \
- split(opt_net_interfaces_to_show, net_interfaces_to_show, ",")
-
- sensor_io = "khatus_sensor_net_addr_io"
- sensor_wi = "khatus_sensor_net_wifi_status"
-
+ split(Opt_Net_Interfaces_To_Show, net_interfaces_to_show, ",")
+ io = "khatus_sensor_net_addr_io"
+ wi = "khatus_sensor_net_wifi_status"
out = ""
sep = ""
for (i = number_of_net_interfaces_to_show; i > 0; i--) {
interface = net_interfaces_to_show[i]
label = substr(interface, 1, 1)
-
- addr = Data_get(sensor_io, "addr" Kfs interface, 5)
- w = Data_get(sensor_io, "bytes_written" Kfs interface, 5)
- r = Data_get(sensor_io, "bytes_read" Kfs interface, 5)
-
- if (addr) {
- bytes_per_mb = 1024 * 1024
- w = w ? sprintf("%0.3f", w / bytes_per_mb) : "--"
- r = r ? sprintf("%0.3f", r / bytes_per_mb) : "--"
- io_stat = sprintf("%s▲ %s▼", w, r)
- } else {
- io_stat = "--"
- }
-
if (interface ~ "^w") {
- wifi = Data_get(sensor_wi, "status" Kfs interface, 10)
- label = label ":" (wifi ? wifi : "--")
+ wifi = cache_get_fmt_def(wi, "status" Kfs interface, 10, "%s")
+ label = label ":" wifi
}
-
+ addr = cache_get_fmt_def(io, "addr" Kfs interface, 5, "%s", "")
+ w = cache_get_fmt_def(io, "bytes_written" Kfs interface, 5, "%0.3f")
+ r = cache_get_fmt_def(io, "bytes_read" Kfs interface, 5, "%0.3f")
+ io_stat = addr ? sprintf("%s▲ %s▼", w, r) : "--"
out = out sep label ":" io_stat
sep = " "
}
-
return sprintf("N[%s]", out)
}
-function make_status_bluetooth( status) {
- status = Data_get("khatus_sensor_bluetooth_power", "power_status", 10)
- return sprintf("B=%s", status ? status : "--")
+function make_status_bluetooth( src, key) {
+ src = "khatus_sensor_bluetooth_power"
+ key = "power_status"
+ return sprintf("B=%s", cache_get_fmt_def(src, key, 10, "%s"))
}
-function make_status_screen_brightness( percentage) {
- percentage = Data_get("khatus_sensor_screen_brightness", "percentage", 5)
- percentage = percentage ? sprintf("%d", percentage) : "--"
- return sprintf("*%s%%", percentage)
+function make_status_screen_brightness( src, key) {
+ src = "khatus_sensor_screen_brightness"
+ key = "percentage"
+ return sprintf("*%s%%", cache_get_fmt_def(src, key, 5, "%d"))
}
-function make_status_volume( sink, mute, vol_l, vol_r, status) {
- sink = opt_pulseaudio_sink
- mute = Data_get("khatus_sensor_volume", "mute" Kfs sink, 5)
- vol_l = Data_get("khatus_sensor_volume", "vol_left" Kfs sink, 5)
- vol_r = Data_get("khatus_sensor_volume", "vol_right" Kfs sink, 5)
-
- if (mute && vol_l && vol_r) {
- if (mute == "yes") {status = "X"}
- else if (mute == "no") {status = sprintf("%s %s", vol_l, vol_r)}
+function make_status_volume( sink, mu, vl, vr, show) {
+ sink = Opt_Pulseaudio_Sink
+ cache_get(mu, "khatus_sensor_volume", "mute" Kfs sink, 5)
+ cache_get(vl, "khatus_sensor_volume", "vol_left" Kfs sink, 5)
+ cache_get(vr, "khatus_sensor_volume", "vol_right" Kfs sink, 5)
+ show = "--"
+ if (!mu["is_expired"] && !vl["is_expired"] && !vr["is_expired"]) {
+ if (mu["value"] == "yes") {show = "X"}
+ else if (mu["value"] == "no") {show = vl["value"] " " vr["value"]}
else {
print_msg_error(\
"make_status_volume", \
- "Unexpected value for 'mute' field: " mute \
+ "Unexpected value for 'mute' field: " mu["value"] \
)
}
- } else {
- status = "--"
}
-
- return sprintf("(%s)", status)
+ return sprintf("(%s)", show)
}
function make_status_mpd( state, status) {
- if (state = Data_get("khatus_sensor_mpd", "state", 5)) {
- if (state == "play") {
+ cache_get(state, "khatus_sensor_mpd", "state", 5)
+ if (!state["is_expired"] && state["value"]) {
+ if (state["value"] == "play") {
status = make_status_mpd_state_known("▶")
- } else if (state == "pause") {
+ } else if (state["value"] == "pause") {
status = make_status_mpd_state_known("❚❚")
- } else if (state == "stop") {
+ } else if (state["value"] == "stop") {
status = make_status_mpd_state_known("⬛")
} else {
print_msg_error(\
"make_status_mpd", \
- "Unexpected value for 'state' field: " state \
+ "Unexpected value for 'state' field: " state["value"] \
)
status = "--"
}
function make_status_mpd_state_known(symbol, s, song, time, percentage) {
s = "khatus_sensor_mpd"
- song = Data_get(s, "song" , 5)
- time = Data_get(s, "play_time_minimal_units", 5)
- percent = Data_get(s, "play_time_percentage" , 5)
- song = substr(song, 1, opt_mpd_song_max_chars)
+ song = cache_get_fmt_def(s, "song" , 5, "%s", "?")
+ time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?")
+ percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?")
+ song = substr(song, 1, Opt_Mpd_Song_Max_Chars)
return sprintf("%s %s %s %s", symbol, time, percent, song)
}
-function make_status_weather( hour, t_f) {
+function make_status_weather( src, hour, t_f) {
+ src = "khatus_sensor_weather"
hour = 60 * 60
- t_f = Data_get("khatus_sensor_weather", "temperature_f", 3 * hour)
- t_f = t_f ? sprintf("%d", t_f) : "--"
+ t_f = cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
return sprintf("%s°F", t_f)
}
function make_status_datetime( dt) {
- dt = Data_get("khatus_sensor_datetime", "datetime", 5)
- return dt ? dt : "--"
+ return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
}
# -----------------------------------------------------------------------------
print_msg("OK", key, val, "/dev/stdout")
}
+function print_msg_info(location, msg) {
+ print_msg("INFO", location, msg, "/dev/stderr")
+}
+
function print_msg_error(location, msg) {
print_msg("ERROR", location, msg, "/dev/stderr")
}