Implement a simplified net interface status method
[khatus.git] / x2 / src / awk / exe / bar.awk
1 # Naming convention:
2 # Variables:
3 # - global, builtin : ALLCAPS
4 # - global, public : Camel_Snake_Man_Bear_Pig
5 # - global, private : _snake_case_prefixed_underscore
6 # - local : snake_case
7 # Functions:
8 # - global, public : snake_case
9
10 # -----------------------------------------------------------------------------
11 # Input
12 # -----------------------------------------------------------------------------
13 {
14 delete msg
15 msg_parse(msg, $0)
16 }
17
18 msg["type"] == "data" {
19 cache_update(msg["node"], msg["module"], msg["key"], msg["val"])
20 }
21
22 msg["node"] == Node && \
23 msg["module"] == "khatus_sensor_datetime" && \
24 msg["type"] == "data" {
25 # Code for bar_make_status is expected to be passed as an
26 # additional source file, using -f flag.
27 msg_out_status_bar(bar_make_status())
28 }
29
30 # -----------------------------------------------------------------------------
31 # Energy
32 # -----------------------------------------------------------------------------
33
34 function bar_make_status_energy_percent() {
35 return cache_get_fmt_def("khatus_sensor_energy", "battery_percentage", 0, "%d")
36 }
37
38 function bar_make_status_energy_direction( state, direction_of_change) {
39 cache_get(state, "khatus_sensor_energy", "battery_state", 0)
40 if (state["value"] == "discharging") {
41 direction_of_change = "<"
42 } else if (state["value"] == "charging") {
43 direction_of_change = ">"
44 } else {
45 direction_of_change = "="
46 }
47 return direction_of_change
48 }
49
50 # -----------------------------------------------------------------------------
51 # Memory
52 # -----------------------------------------------------------------------------
53
54 function bar_make_status_mem_percent( total, used, percent, percent_str) {
55 cache_get(total, "khatus_sensor_memory", "total", 5)
56 cache_get(used , "khatus_sensor_memory", "used" , 5)
57 # Checking total["value"] to avoid division by zero when data is missing
58 if (!total["is_expired"] && \
59 !used["is_expired"] && \
60 total["value"] \
61 ) {
62 percent = num_round((used["value"] / total["value"]) * 100)
63 percent_str = sprintf("%d", percent)
64 } else {
65 percent_str = "__"
66 }
67 return percent_str
68 }
69
70 # -----------------------------------------------------------------------------
71 # Processes
72 # -----------------------------------------------------------------------------
73 # From man ps:
74 # D uninterruptible sleep (usually IO)
75 # R running or runnable (on run queue)
76 # S interruptible sleep (waiting for an event to complete)
77 # T stopped by job control signal
78 # t stopped by debugger during the tracing
79 # W paging (not valid since the 2.6.xx kernel)
80 # X dead (should never be seen)
81 # Z defunct ("zombie") process, terminated but not reaped by its parent
82 #
83 # Additionally, not documented in ps man page:
84 # I Idle
85
86 function bar_make_status_procs_count_all() {
87 return cache_get_fmt_def("khatus_sensor_procs", "total_procs", 15, "%d")
88 }
89
90 function bar_make_status_procs_count_r( src) {
91 src = "khatus_sensor_procs"
92 return cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0")
93 }
94
95 function bar_make_status_procs_count_d( src) {
96 src = "khatus_sensor_procs"
97 return cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0")
98 }
99
100 function bar_make_status_procs_count_t( src) {
101 src = "khatus_sensor_procs"
102 return cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0")
103 }
104
105 function bar_make_status_procs_count_i( src) {
106 src = "khatus_sensor_procs"
107 return cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0")
108 }
109
110 function bar_make_status_procs_count_z( src) {
111 src = "khatus_sensor_procs"
112 return cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0")
113 }
114
115 # -----------------------------------------------------------------------------
116 # CPU
117 # -----------------------------------------------------------------------------
118
119 function bar_make_status_cpu_loadavg( src) {
120 src = "khatus_sensor_loadavg"
121 return cache_get_fmt_def(src, "load_avg_1min", 5, "%4.2f")
122 }
123
124 function bar_make_status_cpu_temperature() {
125 return cache_get_fmt_def("khatus_sensor_temperature", "temp_c", 5, "%d")
126 }
127
128 function bar_make_status_cpu_fan_speed() {
129 return cache_get_fmt_def("khatus_sensor_fan", "speed", 5, "%4d")
130 }
131
132 # -----------------------------------------------------------------------------
133 # Disk
134 # -----------------------------------------------------------------------------
135
136 function bar_make_status_disk_space( src) {
137 src = "khatus_sensor_disk_space"
138 return cache_get_fmt_def(src, "disk_usage_percentage", 10, "%s")
139 }
140
141 function bar_make_status_disk_io_w( src) {
142 src = "khatus_sensor_disk_io"
143 return cache_get_fmt_def(src, "sectors_written", 5, "%0.3f")
144 }
145
146 function bar_make_status_disk_io_r( src) {
147 src = "khatus_sensor_disk_io"
148 return cache_get_fmt_def(src, "sectors_read", 5, "%0.3f")
149 }
150
151 # -----------------------------------------------------------------------------
152 # Network
153 # -----------------------------------------------------------------------------
154
155 function bar_make_status_net_iface_status(interface, addr) {
156 cache_get(addr, "khatus_sensor_net_addr_io", "addr" Kfs interface, 5)
157 if (!addr["is_expired"] && addr["value"])
158 return "up"
159 else
160 return "--"
161 }
162
163 function bar_make_status_net_addr(interface, src) {
164 src = "khatus_sensor_net_addr_io"
165 return cache_get_fmt_def(src, "addr" Kfs interface, 5, "%s", "")
166 }
167
168 function bar_make_status_net_io_w(interface, src) {
169 src = "khatus_sensor_net_addr_io"
170 return cache_get_fmt_def(src, "bytes_written" Kfs interface, 5, "%0.3f")
171 }
172
173 function bar_make_status_net_io_r(interface, src) {
174 src = "khatus_sensor_net_addr_io"
175 return cache_get_fmt_def(src, "bytes_read" Kfs interface, 5, "%0.3f")
176 }
177
178 function bar_make_status_net_wifi(interface, src) {
179 src = "khatus_sensor_net_wifi_status"
180 return cache_get_fmt_def(src, "status" Kfs interface, 10, "%s")
181 }
182
183 # -----------------------------------------------------------------------------
184 # Bluetooth
185 # -----------------------------------------------------------------------------
186
187 function bar_make_status_bluetooth_power( src) {
188 src = "khatus_sensor_bluetooth_power"
189 return cache_get_fmt_def(src, "power_status", 10, "%s")
190 }
191
192 # -----------------------------------------------------------------------------
193 # Backlight (screen brightness)
194 # -----------------------------------------------------------------------------
195
196 function bar_make_status_backlight_percent( src) {
197 src = "khatus_sensor_screen_brightness"
198 return cache_get_fmt_def(src, "percentage", 5, "%d")
199 }
200
201 # -----------------------------------------------------------------------------
202 # Volume
203 # -----------------------------------------------------------------------------
204
205 function bar_make_status_volume_alsa_device(device, m, l, r, show) {
206 cache_get(m, "khatus_sensor_volume", "mute" Kfs device, 5)
207 cache_get(l, "khatus_sensor_volume", "vol_left" Kfs device, 5)
208 cache_get(r, "khatus_sensor_volume", "vol_right" Kfs device, 5)
209 show = "--"
210 if (!m["is_expired"] && !l["is_expired"] && !r["is_expired"]) {
211 if (m["value"] == "yes")
212 show = "X"
213 else if (m["value"] == "no")
214 show = l["value"] #" " r["value"]
215 else
216 msg_out_log_error(\
217 "bar_make_status_volume_alsa_device: " device ". ", \
218 "Unexpected value for 'mute' field: " m["value"] \
219 )
220 }
221 return show
222 }
223
224 # -----------------------------------------------------------------------------
225 # MPD
226 # -----------------------------------------------------------------------------
227
228 function bar_make_status_mpd( state, status) {
229 cache_get(state, "khatus_sensor_mpd", "state", 5)
230 if (!state["is_expired"] && state["value"]) {
231 if (state["value"] == "play") {
232 status = bar_make_status_mpd_state_known("▶")
233 } else if (state["value"] == "pause") {
234 status = bar_make_status_mpd_state_known("❚❚")
235 } else if (state["value"] == "stop") {
236 status = bar_make_status_mpd_state_known("⬛")
237 } else {
238 msg_out_log_error(\
239 "bar_make_status_mpd", \
240 "Unexpected value for 'state' field: " state["value"] \
241 )
242 status = "--"
243 }
244 } else {
245 status = "--"
246 }
247 return status
248 }
249
250 function bar_make_status_mpd_state_known(symbol, s, song, time, percentage) {
251 s = "khatus_sensor_mpd"
252 #song = cache_get_fmt_def(s, "song" , 5, "%s", "?")
253 time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?")
254 percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?")
255 #song = substr(song, 1, Opt_Mpd_Song_Max_Chars)
256 return sprintf("%s %s %s", symbol, time, percent)
257 }
258
259 # -----------------------------------------------------------------------------
260 # Weather
261 # -----------------------------------------------------------------------------
262
263 function bar_make_status_weather_temp_f( src, hour) {
264 src = "khatus_sensor_weather"
265 hour = 60 * 60
266 return cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
267 }
268
269 # -----------------------------------------------------------------------------
270 # Datetime
271 # -----------------------------------------------------------------------------
272
273 function bar_make_status_datetime( dt) {
274 return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
275 }
This page took 0.097332 seconds and 4 git commands to generate.