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