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 | |
113 | } | |
114 | ||
115 | /^out:BAR/\ | |
116 | { | |
117 | split_msg_parts() | |
118 | print make_bar() | |
119 | } | |
120 | ||
121 | function set_load_avg( sched) { | |
122 | split($4, sched, "/") | |
123 | db["load_avg_1min"] = $1 | |
124 | db["load_avg_5min"] = $2 | |
125 | db["load_avg_15min"] = $3 | |
126 | db["kern_sched_queue_runnable"] = sched[1] | |
127 | db["kern_sched_queue_total"] = sched[2] | |
128 | db["kern_sched_latest_pid"] = $5 | |
129 | } | |
130 | ||
131 | function set_disk_io( curr_w, curr_r, prev_w, prev_r) { | |
132 | curr_w = $1 | |
133 | curr_r = $2 | |
134 | prev_w = db["disk_io_curr_w"] | |
135 | prev_r = db["disk_io_curr_r"] | |
136 | db["disk_io_curr_w"] = curr_w | |
137 | db["disk_io_curr_r"] = curr_r | |
138 | db["disk_io_diff_w"] = curr_w - prev_w | |
139 | db["disk_io_diff_r"] = curr_r - prev_r | |
140 | } | |
141 | ||
142 | function set_net_addr_io( \ | |
143 | interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\ | |
144 | ) { | |
145 | interface = $1 | |
146 | address = $2 | |
147 | io_curr_w = $3 | |
148 | io_curr_r = $4 | |
149 | if (interface) { | |
150 | if (address && io_curr_w && io_curr_r) { | |
151 | # recalculate | |
152 | io_prev_w = net_io_curr_w[interface] | |
153 | io_prev_r = net_io_curr_r[interface] | |
154 | ||
155 | net_addr[interface] = address | |
156 | net_io_curr_w[interface] = io_curr_w | |
157 | net_io_curr_r[interface] = io_curr_r | |
158 | net_io_diff_w[interface] = io_curr_w - io_prev_w | |
159 | net_io_diff_r[interface] = io_curr_r - io_prev_r | |
160 | } else { | |
161 | # clear | |
162 | net_addr[interface] = "" | |
163 | net_io_curr_w[interface] = 0 | |
164 | net_io_curr_r[interface] = 0 | |
165 | net_io_diff_w[interface] = 0 | |
166 | net_io_diff_r[interface] = 0 | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | function set_screen_brightness( max, cur) { | |
172 | max = $1 | |
173 | cur = $2 | |
174 | db["screen_brightness"] = (cur / max) * 100 | |
175 | } | |
176 | ||
177 | function split_msg_parts() { | |
178 | msg_head = $1 | |
179 | sub("^" msg_head " +", "") | |
180 | msg_body = $0 | |
181 | debug(msg_head, msg_body) | |
182 | } | |
183 | ||
184 | function make_bar( position, bar, sep, i, j) { | |
185 | position[++i] = make_status_energy() | |
186 | position[++i] = make_status_mem() | |
187 | position[++i] = make_status_cpu() | |
188 | position[++i] = make_status_disk() | |
189 | position[++i] = make_status_net() | |
190 | position[++i] = sprintf("B=%s", db["bluetooth_power"]) | |
191 | position[++i] = sprintf("*%d%%", db["screen_brightness"]) | |
192 | position[++i] = sprintf("(%s)", db["volume"]) | |
193 | position[++i] = make_status_mpd() | |
194 | position[++i] = db["weather_temperature"] | |
195 | position[++i] = db["datetime"] | |
196 | bar = "" | |
197 | sep = "" | |
198 | for (j = 1; j <= i; j++) { | |
199 | bar = bar sep position[j] | |
200 | sep = " " | |
201 | } | |
202 | return bar | |
203 | } | |
204 | ||
205 | function make_status_energy( state, direction_of_change) { | |
206 | state = db["energy_state"] | |
207 | if (state == "discharging") { | |
208 | direction_of_change = "<" | |
209 | } else if (state == "charging") { | |
210 | direction_of_change = ">" | |
211 | } else { | |
212 | direction_of_change = "=" | |
213 | }; | |
214 | printf("E%s%s", direction_of_change, db["energy_percentage"]) | |
215 | } | |
216 | ||
217 | function make_status_mem( total, used, percent, status) { | |
218 | total = db["memory_total"] | |
219 | used = db["memory_used"] | |
220 | # To avoid division by zero when data is missing | |
221 | if (total && used) { | |
222 | percent = round((used / total) * 100) | |
223 | status = sprintf("%d%%", percent) | |
224 | } else { | |
225 | status = "__" | |
226 | } | |
227 | return sprintf("M=%s", status) | |
228 | } | |
229 | ||
230 | function make_status_cpu( load, temp, fan) { | |
231 | load = db["load_avg_1min"] | |
232 | temp = db["temperature"] / 1000 | |
233 | fan = db["fan_speed"] | |
234 | return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan) | |
235 | } | |
236 | ||
237 | function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) { | |
238 | bytes_per_sector = 512 | |
239 | bytes_per_mb = 1024 * 1024 | |
240 | w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb | |
241 | r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb | |
242 | return \ | |
243 | sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r) | |
244 | } | |
245 | ||
246 | function make_status_net( \ | |
247 | out, | |
248 | number_of_interfaces_to_show, | |
249 | n, | |
250 | array_of_prefixes_of_interfaces_to_show, | |
251 | prefix, | |
252 | interface, | |
253 | label, | |
254 | count_printed, | |
255 | sep, | |
256 | io_stat, | |
257 | dw, dr, | |
258 | bytes_per_unit\ | |
259 | ) { | |
260 | out = "" | |
261 | number_of_interfaces_to_show = \ | |
262 | split(\ | |
263 | opt_prefixes_of_net_interfaces_to_show,\ | |
264 | array_of_prefixes_of_interfaces_to_show,\ | |
265 | ","\ | |
266 | ) | |
267 | for (n = 1; n <= number_of_interfaces_to_show; n++) { | |
268 | prefix = array_of_prefixes_of_interfaces_to_show[n] | |
269 | for (interface in net_addr) { | |
270 | if (interface ~ ("^" prefix)) { | |
271 | label = substr(interface, 1, 1) | |
272 | if (net_addr[interface]) { | |
273 | bytes_per_mb = 1024 * 1024 # TODO: option | |
274 | dw = net_io_diff_w[interface] / bytes_per_mb | |
275 | dr = net_io_diff_r[interface] / bytes_per_mb | |
276 | io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr) | |
277 | } else { | |
278 | io_stat = "--" | |
279 | } | |
280 | if (interface ~ "^w") { | |
281 | label = label ":" db["net_wifi_status"] | |
282 | } | |
283 | if (++count_printed > 1) { | |
284 | sep = " " | |
285 | } else { | |
286 | sep = "" | |
287 | } | |
288 | out = out sep label ":" io_stat | |
289 | } | |
290 | } | |
291 | } | |
292 | return sprintf("N[%s]", out) | |
293 | } | |
294 | ||
295 | function make_status_mpd( state, status) { | |
296 | state = db["mpd_state"] | |
297 | ||
298 | if (state == "play") { | |
299 | status = make_status_mpd_state_known("▶") | |
300 | } else if (state == "pause") { | |
301 | status = make_status_mpd_state_known("❚❚") | |
302 | } else if (state == "stop") { | |
303 | status = make_status_mpd_state_known("⬛") | |
304 | } else { | |
305 | status = make_status_mpd_state_unknown("--") | |
306 | } | |
307 | ||
308 | return sprintf("[%s]", status) | |
309 | } | |
310 | ||
311 | function make_status_mpd_state_known(symbol) { | |
312 | return sprintf(\ | |
313 | "%s %s %s %s", | |
314 | symbol, | |
315 | db["mpd_curr_song_time"], | |
316 | db["mpd_curr_song_percent"], | |
317 | substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\ | |
318 | ) | |
319 | } | |
320 | ||
321 | function make_status_mpd_state_unknown(symbol) { | |
322 | return sprintf("%s", symbol) | |
323 | } | |
324 | ||
325 | function round(n) { | |
326 | return int(n + 0.5) | |
327 | } | |
328 | ||
329 | function debug(location, msg) { | |
330 | if (opt_debug) { | |
331 | print_error(location, msg) | |
332 | } | |
333 | } | |
334 | ||
335 | function print_error(location, msg) { | |
336 | print(location " ==> " msg) > "/dev/stderr" | |
337 | } |