Commit | Line | Data |
---|---|---|
f37162a4 SK |
1 | #! /usr/bin/awk -f |
2 | ||
3 | /^in:ENERGY/\ | |
4 | { | |
5 | split_msg_parts() | |
6 | db["energy_state"] = $1 | |
7 | db["energy_percentage"] = $2 | |
8 | } | |
9 | ||
10 | /^in:MEMORY/\ | |
11 | { | |
12 | split_msg_parts() | |
13 | db["memory_total"] = $1 | |
14 | db["memory_used"] = $2 | |
15 | } | |
16 | ||
17 | /^in:FAN +status:/\ | |
18 | { | |
19 | split_msg_parts() | |
20 | db["fan_status"] = $2 | |
21 | } | |
22 | ||
23 | /^in:FAN +speed:/\ | |
24 | { | |
25 | split_msg_parts() | |
26 | db["fan_speed"] = $2 | |
27 | } | |
28 | ||
29 | /^in:FAN +level:/\ | |
30 | { | |
31 | split_msg_parts() | |
32 | db["fan_level"] = $2 | |
33 | } | |
34 | ||
35 | /^in:TEMPERATURE/\ | |
36 | { | |
37 | split_msg_parts() | |
38 | db["temperature"] = $1 | |
39 | } | |
40 | ||
41 | /^in:LOAD_AVG/\ | |
42 | { | |
43 | split_msg_parts() | |
44 | set_load_avg() | |
45 | } | |
46 | ||
47 | /^in:DISK_IO/\ | |
48 | { | |
49 | split_msg_parts() | |
50 | set_disk_io() | |
51 | } | |
52 | ||
53 | /^in:DISK_SPACE/\ | |
54 | { | |
55 | split_msg_parts() | |
56 | db["disk_space_used"] = msg_body | |
57 | } | |
58 | ||
59 | /^in:NET_ADDR_IO/\ | |
60 | { | |
61 | split_msg_parts() | |
62 | set_net_addr_io() | |
63 | } | |
64 | ||
65 | /^in:NET_WIFI_STATUS/\ | |
66 | { | |
67 | split_msg_parts() | |
68 | db["net_wifi_status"] = msg_body | |
69 | } | |
70 | ||
71 | /^in:BLUETOOTH_POWER/\ | |
72 | { | |
73 | split_msg_parts() | |
74 | db["bluetooth_power"] = msg_body | |
75 | } | |
76 | ||
77 | /^in:SCREEN_BRIGHTNESS/\ | |
78 | { | |
79 | split_msg_parts() | |
80 | set_screen_brightness() | |
81 | } | |
82 | ||
83 | /^in:VOLUME/\ | |
84 | { | |
85 | split_msg_parts() | |
86 | db["volume"] = msg_body | |
87 | } | |
88 | ||
89 | /^in:MPD_STATE/\ | |
90 | { | |
91 | split_msg_parts() | |
92 | db["mpd_state"] = $1 | |
93 | db["mpd_curr_song_time"] = $2 | |
94 | db["mpd_curr_song_percent"] = $3 | |
95 | } | |
96 | ||
97 | /^in:MPD_SONG/\ | |
98 | { | |
99 | split_msg_parts() | |
100 | db["mpd_curr_song_name"] = msg_body | |
101 | } | |
102 | ||
103 | /^in:WEATHER/\ | |
104 | { | |
105 | split_msg_parts() | |
106 | db["weather_temperature"] = msg_body | |
107 | } | |
108 | ||
109 | /^in:DATE_TIME/\ | |
110 | { | |
111 | split_msg_parts() | |
112 | db["datetime"] = msg_body | |
f37162a4 SK |
113 | print make_bar() |
114 | } | |
115 | ||
116 | function set_load_avg( sched) { | |
117 | split($4, sched, "/") | |
118 | db["load_avg_1min"] = $1 | |
119 | db["load_avg_5min"] = $2 | |
120 | db["load_avg_15min"] = $3 | |
121 | db["kern_sched_queue_runnable"] = sched[1] | |
122 | db["kern_sched_queue_total"] = sched[2] | |
123 | db["kern_sched_latest_pid"] = $5 | |
124 | } | |
125 | ||
126 | function set_disk_io( curr_w, curr_r, prev_w, prev_r) { | |
127 | curr_w = $1 | |
128 | curr_r = $2 | |
129 | prev_w = db["disk_io_curr_w"] | |
130 | prev_r = db["disk_io_curr_r"] | |
131 | db["disk_io_curr_w"] = curr_w | |
132 | db["disk_io_curr_r"] = curr_r | |
133 | db["disk_io_diff_w"] = curr_w - prev_w | |
134 | db["disk_io_diff_r"] = curr_r - prev_r | |
135 | } | |
136 | ||
137 | function set_net_addr_io( \ | |
138 | interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\ | |
139 | ) { | |
140 | interface = $1 | |
141 | address = $2 | |
142 | io_curr_w = $3 | |
143 | io_curr_r = $4 | |
144 | if (interface) { | |
145 | if (address && io_curr_w && io_curr_r) { | |
146 | # recalculate | |
147 | io_prev_w = net_io_curr_w[interface] | |
148 | io_prev_r = net_io_curr_r[interface] | |
149 | ||
150 | net_addr[interface] = address | |
151 | net_io_curr_w[interface] = io_curr_w | |
152 | net_io_curr_r[interface] = io_curr_r | |
153 | net_io_diff_w[interface] = io_curr_w - io_prev_w | |
154 | net_io_diff_r[interface] = io_curr_r - io_prev_r | |
155 | } else { | |
156 | # clear | |
157 | net_addr[interface] = "" | |
158 | net_io_curr_w[interface] = 0 | |
159 | net_io_curr_r[interface] = 0 | |
160 | net_io_diff_w[interface] = 0 | |
161 | net_io_diff_r[interface] = 0 | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | function set_screen_brightness( max, cur) { | |
167 | max = $1 | |
168 | cur = $2 | |
169 | db["screen_brightness"] = (cur / max) * 100 | |
170 | } | |
171 | ||
172 | function split_msg_parts() { | |
173 | msg_head = $1 | |
174 | sub("^" msg_head " +", "") | |
175 | msg_body = $0 | |
176 | debug(msg_head, msg_body) | |
177 | } | |
178 | ||
179 | function make_bar( position, bar, sep, i, j) { | |
180 | position[++i] = make_status_energy() | |
181 | position[++i] = make_status_mem() | |
182 | position[++i] = make_status_cpu() | |
183 | position[++i] = make_status_disk() | |
184 | position[++i] = make_status_net() | |
185 | position[++i] = sprintf("B=%s", db["bluetooth_power"]) | |
186 | position[++i] = sprintf("*%d%%", db["screen_brightness"]) | |
187 | position[++i] = sprintf("(%s)", db["volume"]) | |
188 | position[++i] = make_status_mpd() | |
189 | position[++i] = db["weather_temperature"] | |
190 | position[++i] = db["datetime"] | |
191 | bar = "" | |
192 | sep = "" | |
193 | for (j = 1; j <= i; j++) { | |
194 | bar = bar sep position[j] | |
195 | sep = " " | |
196 | } | |
197 | return bar | |
198 | } | |
199 | ||
200 | function make_status_energy( state, direction_of_change) { | |
201 | state = db["energy_state"] | |
202 | if (state == "discharging") { | |
203 | direction_of_change = "<" | |
204 | } else if (state == "charging") { | |
205 | direction_of_change = ">" | |
206 | } else { | |
207 | direction_of_change = "=" | |
208 | }; | |
209 | printf("E%s%s", direction_of_change, db["energy_percentage"]) | |
210 | } | |
211 | ||
212 | function make_status_mem( total, used, percent, status) { | |
213 | total = db["memory_total"] | |
214 | used = db["memory_used"] | |
215 | # To avoid division by zero when data is missing | |
216 | if (total && used) { | |
217 | percent = round((used / total) * 100) | |
218 | status = sprintf("%d%%", percent) | |
219 | } else { | |
220 | status = "__" | |
221 | } | |
222 | return sprintf("M=%s", status) | |
223 | } | |
224 | ||
225 | function make_status_cpu( load, temp, fan) { | |
226 | load = db["load_avg_1min"] | |
227 | temp = db["temperature"] / 1000 | |
228 | fan = db["fan_speed"] | |
229 | return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan) | |
230 | } | |
231 | ||
232 | function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) { | |
233 | bytes_per_sector = 512 | |
234 | bytes_per_mb = 1024 * 1024 | |
235 | w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb | |
236 | r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb | |
237 | return \ | |
238 | sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r) | |
239 | } | |
240 | ||
241 | function make_status_net( \ | |
242 | out, | |
243 | number_of_interfaces_to_show, | |
244 | n, | |
245 | array_of_prefixes_of_interfaces_to_show, | |
246 | prefix, | |
247 | interface, | |
248 | label, | |
249 | count_printed, | |
250 | sep, | |
251 | io_stat, | |
252 | dw, dr, | |
253 | bytes_per_unit\ | |
254 | ) { | |
255 | out = "" | |
256 | number_of_interfaces_to_show = \ | |
257 | split(\ | |
258 | opt_prefixes_of_net_interfaces_to_show,\ | |
259 | array_of_prefixes_of_interfaces_to_show,\ | |
260 | ","\ | |
261 | ) | |
262 | for (n = 1; n <= number_of_interfaces_to_show; n++) { | |
263 | prefix = array_of_prefixes_of_interfaces_to_show[n] | |
264 | for (interface in net_addr) { | |
265 | if (interface ~ ("^" prefix)) { | |
266 | label = substr(interface, 1, 1) | |
267 | if (net_addr[interface]) { | |
268 | bytes_per_mb = 1024 * 1024 # TODO: option | |
269 | dw = net_io_diff_w[interface] / bytes_per_mb | |
270 | dr = net_io_diff_r[interface] / bytes_per_mb | |
271 | io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr) | |
272 | } else { | |
273 | io_stat = "--" | |
274 | } | |
275 | if (interface ~ "^w") { | |
276 | label = label ":" db["net_wifi_status"] | |
277 | } | |
278 | if (++count_printed > 1) { | |
279 | sep = " " | |
280 | } else { | |
281 | sep = "" | |
282 | } | |
283 | out = out sep label ":" io_stat | |
284 | } | |
285 | } | |
286 | } | |
287 | return sprintf("N[%s]", out) | |
288 | } | |
289 | ||
290 | function make_status_mpd( state, status) { | |
291 | state = db["mpd_state"] | |
292 | ||
293 | if (state == "play") { | |
294 | status = make_status_mpd_state_known("▶") | |
295 | } else if (state == "pause") { | |
296 | status = make_status_mpd_state_known("❚❚") | |
297 | } else if (state == "stop") { | |
298 | status = make_status_mpd_state_known("⬛") | |
299 | } else { | |
300 | status = make_status_mpd_state_unknown("--") | |
301 | } | |
302 | ||
303 | return sprintf("[%s]", status) | |
304 | } | |
305 | ||
306 | function make_status_mpd_state_known(symbol) { | |
307 | return sprintf(\ | |
308 | "%s %s %s %s", | |
309 | symbol, | |
310 | db["mpd_curr_song_time"], | |
311 | db["mpd_curr_song_percent"], | |
312 | substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\ | |
313 | ) | |
314 | } | |
315 | ||
316 | function make_status_mpd_state_unknown(symbol) { | |
317 | return sprintf("%s", symbol) | |
318 | } | |
319 | ||
320 | function round(n) { | |
321 | return int(n + 0.5) | |
322 | } | |
323 | ||
324 | function debug(location, msg) { | |
325 | if (opt_debug) { | |
326 | print_error(location, msg) | |
327 | } | |
328 | } | |
329 | ||
330 | function print_error(location, msg) { | |
331 | print(location " ==> " msg) > "/dev/stderr" | |
332 | } |