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