Commit | Line | Data |
---|---|---|
438d0d5f SK |
1 | #! /bin/bash |
2 | ||
4d314e0f SK |
3 | set -e |
4 | ||
6339a4f8 | 5 | consume() { |
438d0d5f | 6 | pipe="$1" |
2b221665 | 7 | debug="$2" |
f37162a4 SK |
8 | dir_bin="$3" |
9 | prefixes_of_net_interfaces_to_show="$4" | |
438d0d5f | 10 | tail -f "$pipe" \ |
f37162a4 | 11 | | stdbuf -o L "$dir_bin"/khatus_controller \ |
2b221665 | 12 | -v opt_debug="$debug" \ |
365549a9 | 13 | -v opt_mpd_song_max_chars=10 \ |
f37162a4 | 14 | -v opt_prefixes_of_net_interfaces_to_show="$prefixes_of_net_interfaces_to_show" |
438d0d5f SK |
15 | } |
16 | ||
17 | spawn() { | |
f37162a4 SK |
18 | pipe="$1" |
19 | dir_bin="$2" | |
20 | cmd="$3" | |
21 | msg_head="$4" | |
22 | interval="$5" | |
438d0d5f | 23 | while true; do |
f37162a4 | 24 | "$dir_bin"/$cmd | while read line; do |
2b221665 SK |
25 | echo "${msg_head} $line" > "$pipe" |
26 | done | |
27 | sleep "$interval" | |
438d0d5f SK |
28 | done & |
29 | } | |
30 | ||
31 | main() { | |
4d314e0f | 32 | # Defaults |
2b221665 | 33 | debug=0 |
f37162a4 | 34 | dir_bin="$HOME/bin" |
b8e4c2fa | 35 | dir_data=$(mktemp -d) |
4d314e0f | 36 | weather_station_id='KJFK' |
bbb42518 | 37 | screen_brightness_device_name='acpi_video0' |
2b221665 | 38 | prefixes_of_net_interfaces_to_show='w' # comma-separated |
c0e8473e | 39 | disk_space_device='/' |
cf698ca8 | 40 | disk_io_device='sda' |
5dd1535a | 41 | thermal_zone=0 |
b9bdec44 | 42 | fan_path='/proc/acpi/ibm/fan' |
4d314e0f | 43 | |
3716523b SK |
44 | interval_inp_datetime=1 |
45 | interval_inp_brightness=1 | |
46 | interval_inp_weather=$(( 30 * 60)) # 30 minutes | |
47 | interval_inp_mpd_state=1 | |
48 | interval_inp_mpd_song=1 | |
49 | interval_inp_volume=1 | |
50 | interval_inp_bluetooth=5 | |
51 | interval_inp_net_wifi=5 | |
52 | interval_inp_net_io=1 | |
53 | interval_inp_disk_space=5 | |
54 | interval_inp_disk_io=1 | |
55 | interval_inp_loadavg=1 | |
56 | interval_inp_temp=1 | |
57 | interval_inp_fan=1 | |
58 | interval_inp_mem=1 | |
59 | interval_inp_energy=1 | |
29b3b10d | 60 | |
4d314e0f | 61 | # User-overrides |
6a12cab1 SK |
62 | # --------------------------------------------------------------------- |
63 | # IMPORTANT: | |
64 | # In order for automatic value reporting, at startup, to work - ensure that | |
65 | # long option names match corresponding variable names! | |
66 | # --------------------------------------------------------------------- | |
2b221665 SK |
67 | long_options='' |
68 | long_options+='debug' | |
6a12cab1 SK |
69 | long_options+=',dir_bin:' |
70 | long_options+=',dir_data:' | |
71 | long_options+=',weather_station_id:' | |
72 | long_options+=',screen_brightness_device_name:' | |
2b221665 | 73 | long_options+=',prefixes_of_net_interfaces_to_show:' |
c0e8473e | 74 | long_options+=',disk_space_device:' |
8fe8971a | 75 | long_options+=',disk_io_device:' |
5dd1535a | 76 | long_options+=',thermal_zone:' |
b9bdec44 | 77 | long_options+=',fan_path:' |
3716523b SK |
78 | long_options+=',interval_inp_datetime:' |
79 | long_options+=',interval_inp_brightness:' | |
80 | long_options+=',interval_inp_weather:' | |
81 | long_options+=',interval_inp_mpd_state:' | |
82 | long_options+=',interval_inp_mpd_song:' | |
83 | long_options+=',interval_inp_volume:' | |
84 | long_options+=',interval_inp_bluetooth:' | |
85 | long_options+=',interval_inp_net_wifi:' | |
86 | long_options+=',interval_inp_net_io:' | |
87 | long_options+=',interval_inp_disk_space:' | |
88 | long_options+=',interval_inp_disk_io:' | |
89 | long_options+=',interval_inp_loadavg:' | |
90 | long_options+=',interval_inp_temp:' | |
91 | long_options+=',interval_inp_fan:' | |
92 | long_options+=',interval_inp_mem:' | |
93 | long_options+=',interval_inp_energy:' | |
4d314e0f SK |
94 | OPTS=$( |
95 | getopt \ | |
2b221665 SK |
96 | -o 'd' \ |
97 | -l $long_options \ | |
4d314e0f SK |
98 | -- "$@" |
99 | ) | |
100 | eval set -- "$OPTS" | |
101 | while true | |
102 | do | |
103 | case "$1" in | |
2b221665 SK |
104 | -d|--debug) |
105 | debug=1 | |
106 | shift | |
107 | ;; | |
6a12cab1 | 108 | --dir_bin) |
f37162a4 SK |
109 | dir_bin="$2" |
110 | shift 2 | |
111 | ;; | |
6a12cab1 | 112 | --dir_data) |
4d314e0f SK |
113 | dir_data="$2" |
114 | shift 2 | |
115 | ;; | |
6a12cab1 | 116 | --weather_station_id) |
4d314e0f SK |
117 | weather_station_id="$2" |
118 | shift 2 | |
119 | ;; | |
6a12cab1 | 120 | --screen_brightness_device_name) |
bbb42518 SK |
121 | screen_brightness_device_name="$2" |
122 | shift 2 | |
123 | ;; | |
2b221665 SK |
124 | --prefixes_of_net_interfaces_to_show) |
125 | prefixes_of_net_interfaces_to_show="$2" | |
126 | shift 2 | |
127 | ;; | |
c0e8473e SK |
128 | --disk_space_device) |
129 | disk_space_device="$2" | |
130 | shift 2 | |
131 | ;; | |
cf698ca8 SK |
132 | --disk_io_device) |
133 | disk_io_device="$2" | |
134 | shift 2 | |
135 | ;; | |
5dd1535a SK |
136 | --thermal_zone) |
137 | thermal_zone="$2" | |
138 | shift 2 | |
139 | ;; | |
b9bdec44 SK |
140 | --fan_path) |
141 | fan_path="$2" | |
142 | shift 2 | |
143 | ;; | |
3716523b SK |
144 | --interval_inp_datetime) |
145 | interval_inp_datetime="$2" | |
29b3b10d SK |
146 | shift 2 |
147 | ;; | |
3716523b SK |
148 | --interval_inp_brightness) |
149 | interval_inp_brightness="$2" | |
29b3b10d SK |
150 | shift 2 |
151 | ;; | |
3716523b SK |
152 | --interval_inp_weather) |
153 | interval_inp_weather="$2" | |
29b3b10d SK |
154 | shift 2 |
155 | ;; | |
3716523b SK |
156 | --interval_inp_mpd_state) |
157 | interval_inp_mpd_state="$2" | |
29b3b10d SK |
158 | shift 2 |
159 | ;; | |
3716523b SK |
160 | --interval_inp_mpd_song) |
161 | interval_inp_mpd_song="$2" | |
29b3b10d SK |
162 | shift 2 |
163 | ;; | |
3716523b SK |
164 | --interval_inp_volume) |
165 | interval_inp_volume="$2" | |
29b3b10d SK |
166 | shift 2 |
167 | ;; | |
3716523b SK |
168 | --interval_inp_bluetooth) |
169 | interval_inp_bluetooth="$2" | |
29b3b10d SK |
170 | shift 2 |
171 | ;; | |
3716523b SK |
172 | --interval_inp_net_wifi) |
173 | interval_inp_net_wifi="$2" | |
29b3b10d SK |
174 | shift 2 |
175 | ;; | |
3716523b SK |
176 | --interval_inp_net_io) |
177 | interval_inp_net_io="$2" | |
29b3b10d SK |
178 | shift 2 |
179 | ;; | |
3716523b SK |
180 | --interval_inp_disk_space) |
181 | interval_inp_disk_space="$2" | |
29b3b10d SK |
182 | shift 2 |
183 | ;; | |
3716523b SK |
184 | --interval_inp_disk_io) |
185 | interval_inp_disk_io="$2" | |
29b3b10d SK |
186 | shift 2 |
187 | ;; | |
3716523b SK |
188 | --interval_inp_loadavg) |
189 | interval_inp_loadavg="$2" | |
29b3b10d SK |
190 | shift 2 |
191 | ;; | |
3716523b SK |
192 | --interval_inp_temp) |
193 | interval_inp_temp="$2" | |
29b3b10d SK |
194 | shift 2 |
195 | ;; | |
3716523b SK |
196 | --interval_inp_fan) |
197 | interval_inp_fan="$2" | |
29b3b10d SK |
198 | shift 2 |
199 | ;; | |
3716523b SK |
200 | --interval_inp_mem) |
201 | interval_inp_mem="$2" | |
29b3b10d SK |
202 | shift 2 |
203 | ;; | |
3716523b SK |
204 | --interval_inp_energy) |
205 | interval_inp_energy="$2" | |
29b3b10d SK |
206 | shift 2 |
207 | ;; | |
4d314e0f SK |
208 | --) |
209 | shift | |
210 | break | |
211 | ;; | |
212 | esac | |
213 | done | |
214 | ||
0c0ebbe1 | 215 | pipe="$dir_data/khatus_data_pipe" |
bbb42518 SK |
216 | screen_brightness_device_path='/sys/class/backlight' |
217 | screen_brightness_device_path+="/$screen_brightness_device_name" | |
0c0ebbe1 | 218 | |
6a12cab1 SK |
219 | ( |
220 | echo '==============================================' | |
221 | echo "Khatus starting with the following parameters:" | |
222 | echo '==============================================' | |
223 | for param in \ | |
224 | $(echo -n "$long_options" \ | |
225 | | awk -v RS=, '{sub(":$", ""); print $0}' | |
226 | ) | |
227 | do | |
228 | echo "$param := ${!param}" | |
229 | done \ | |
230 | | column -ts: | |
231 | echo '----------------------------------------------' | |
4d314e0f SK |
232 | ) >&2 |
233 | ||
4d314e0f | 234 | mkdir -p "$dir_data" |
438d0d5f SK |
235 | rm -f "$pipe" |
236 | mkfifo "$pipe" | |
237 | ||
f37162a4 SK |
238 | cmd_sens_screen_brightness='khatus_sensor_screen_brightness' |
239 | cmd_sens_screen_brightness+=" $screen_brightness_device_path" | |
2b221665 | 240 | |
f37162a4 | 241 | cmd_sens_weather="khatus_sensor_weather $weather_station_id" |
0c0ebbe1 | 242 | |
f37162a4 | 243 | cmd_sens_disk_space="khatus_sensor_disk_space $disk_space_device" |
c0e8473e | 244 | |
f37162a4 | 245 | cmd_sens_disk_io="khatus_sensor_disk_io $disk_io_device" |
cf698ca8 | 246 | |
f37162a4 | 247 | cmd_sens_temperature="khatus_sensor_temperature $thermal_zone" |
5dd1535a | 248 | |
f37162a4 | 249 | cmd_sens_fan="khatus_sensor_fan $fan_path" |
b9bdec44 | 250 | |
4d314e0f | 251 | # TODO: Redirect each worker's stderr to a dedicated log file |
f37162a4 SK |
252 | spawn "$pipe" "$dir_bin" khatus_sensor_datetime 'in:DATE_TIME' $interval_inp_datetime |
253 | spawn "$pipe" "$dir_bin" "$cmd_sens_screen_brightness" 'in:SCREEN_BRIGHTNESS' $interval_inp_brightness | |
254 | spawn "$pipe" "$dir_bin" "$cmd_sens_weather" 'in:WEATHER' $interval_inp_weather | |
255 | spawn "$pipe" "$dir_bin" khatus_sensor_mpd_state 'in:MPD_STATE' $interval_inp_mpd_state | |
256 | spawn "$pipe" "$dir_bin" khatus_sensor_mpd_song 'in:MPD_SONG' $interval_inp_mpd_song | |
257 | spawn "$pipe" "$dir_bin" khatus_sensor_volume 'in:VOLUME' $interval_inp_volume | |
258 | spawn "$pipe" "$dir_bin" khatus_sensor_bluetooth_power 'in:BLUETOOTH_POWER' $interval_inp_bluetooth | |
259 | spawn "$pipe" "$dir_bin" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' $interval_inp_net_wifi | |
260 | spawn "$pipe" "$dir_bin" khatus_sensor_net_addr_io 'in:NET_ADDR_IO' $interval_inp_net_io | |
261 | spawn "$pipe" "$dir_bin" "$cmd_sens_disk_space" 'in:DISK_SPACE' $interval_inp_disk_space | |
262 | spawn "$pipe" "$dir_bin" "$cmd_sens_disk_io" 'in:DISK_IO' $interval_inp_disk_io | |
263 | spawn "$pipe" "$dir_bin" khatus_sensor_loadavg 'in:LOAD_AVG' $interval_inp_loadavg | |
264 | spawn "$pipe" "$dir_bin" "$cmd_sens_temperature" 'in:TEMPERATURE' $interval_inp_temp | |
265 | spawn "$pipe" "$dir_bin" "$cmd_sens_fan" 'in:FAN' $interval_inp_fan | |
266 | spawn "$pipe" "$dir_bin" khatus_sensor_memory 'in:MEMORY' $interval_inp_mem | |
267 | spawn "$pipe" "$dir_bin" khatus_sensor_energy 'in:ENERGY' $interval_inp_energy | |
2b221665 SK |
268 | |
269 | consume \ | |
270 | "$pipe" \ | |
271 | "$debug" \ | |
f37162a4 | 272 | "$dir_bin" \ |
2b221665 | 273 | "$prefixes_of_net_interfaces_to_show" |
438d0d5f SK |
274 | } |
275 | ||
276 | main $@ |