Commit | Line | Data |
---|---|---|
438d0d5f SK |
1 | #! /bin/bash |
2 | ||
75b23ff8 SK |
3 | MSG_FS='|' |
4 | ||
4d314e0f SK |
5 | set -e |
6 | ||
75b23ff8 SK |
7 | executable_name_of_cmd() { |
8 | basename "$(echo $1 | awk '{print $1; exit}')" | |
d33e9656 SK |
9 | } |
10 | ||
9ab9a718 | 11 | run_producer() { |
d33e9656 SK |
12 | pipe="$1" |
13 | bin="$2" | |
14 | cmd="$3" | |
75b23ff8 | 15 | executable_name="$4" |
c40b9076 SK |
16 | perf_log="$5" |
17 | ||
18 | if [ ! "$perf_log" = '' ] | |
19 | then | |
20 | # %S system time in seconds | |
21 | # %U user time in seconds | |
22 | # %e elapsed time in seconds | |
23 | # %c context switches involuntary | |
24 | # %w context switches voluntary | |
25 | # %x exit code | |
26 | time_fmt='%S %U %e %c %w %x' | |
27 | time="/usr/bin/time -ao ${perf_log} -f " | |
28 | time_sep=' ' | |
29 | else | |
30 | time_fmt='' | |
31 | time='' | |
32 | time_sep='' | |
33 | fi | |
34 | ||
35 | ${time}"${time_fmt}"${time_sep}$bin/$cmd \ | |
688fb20e SK |
36 | 2> >( |
37 | while read line | |
15a44039 | 38 | do |
75b23ff8 | 39 | echo "ERROR${MSG_FS}${executable_name}${MSG_FS}$line" > "$pipe" |
15a44039 | 40 | done \ |
688fb20e SK |
41 | ) \ |
42 | | while read line | |
43 | do | |
75b23ff8 | 44 | echo "OK${MSG_FS}${executable_name}${MSG_FS}$line" > "$pipe" |
688fb20e | 45 | done |
7a2b16ba SK |
46 | cmd_exit_code=${PIPESTATUS[0]} |
47 | if [ "$cmd_exit_code" -ne 0 ] | |
48 | then | |
75b23ff8 SK |
49 | echo |
50 | "ERROR${MSG_FS}${executable_name}${MSG_FS}NON_ZERO_EXIT_CODE${MSG_FS}$cmd_exit_code" \ | |
51 | > "$pipe" | |
7a2b16ba | 52 | fi |
ab99b556 SK |
53 | } |
54 | ||
ff74b448 | 55 | fork_watcher() { |
75b23ff8 SK |
56 | pipe="$1" |
57 | bin="$2" | |
58 | cmd="$3" | |
59 | executable_name=$(executable_name_of_cmd "$cmd") | |
60 | run_producer "$pipe" "$bin" "$cmd" "$executable_name" & | |
9ab9a718 SK |
61 | } |
62 | ||
63 | fork_poller() { | |
64 | interval="$1" | |
c40b9076 SK |
65 | perf_log_dir="$2" |
66 | shift 2 | |
75b23ff8 SK |
67 | pipe="$1" |
68 | bin="$2" | |
69 | cmd="$3" | |
70 | ||
71 | executable_name=$(basename "$(echo $cmd | awk '{print $1; exit}')") | |
c40b9076 SK |
72 | |
73 | if [ ! "$perf_log_dir" = '' ] | |
74 | then | |
75 | cmd="$3" | |
75b23ff8 | 76 | perf_log_file=${executable_name}.log |
c40b9076 SK |
77 | mkdir -p "$perf_log_dir" |
78 | perf_log_path="$perf_log_dir/$perf_log_file" | |
79 | fi | |
80 | ||
15a44039 | 81 | while : |
9ab9a718 | 82 | do |
75b23ff8 | 83 | run_producer "$pipe" "$bin" "$cmd" "$executable_name" "$perf_log_path" |
2b221665 | 84 | sleep "$interval" |
438d0d5f SK |
85 | done & |
86 | } | |
87 | ||
88 | main() { | |
d77c7373 | 89 | declare -A opts=( |
d77c7373 | 90 | ["--dir_bin"]="$HOME/bin" |
c40b9076 | 91 | ["--dir_perf_logs"]='' |
d33e9656 | 92 | ["--file_pipe"]=$(mktemp) |
d77c7373 SK |
93 | ["--weather_station_id"]='KJFK' |
94 | ["--screen_brightness_device_name"]='acpi_video0' | |
b4612a8a | 95 | ["--wifi_interface"]='' |
d77c7373 SK |
96 | ["--disk_space_device"]='/' |
97 | ["--disk_io_device"]='sda' | |
98 | ["--thermal_zone"]=0 | |
99 | ["--fan_path"]='/proc/acpi/ibm/fan' | |
75b23ff8 | 100 | ["--pulseaudio_sink"]='0' |
e3f715dd SK |
101 | ["--interval_datetime"]=1 |
102 | ["--interval_brightness"]=1 | |
103 | ["--interval_weather"]=$(( 30 * 60)) # 30 minutes | |
104 | ["--interval_mpd"]=1 | |
105 | ["--interval_volume"]=1 | |
106 | ["--interval_bluetooth"]=1 | |
107 | ["--interval_net_wifi"]=1 | |
108 | ["--interval_net_io"]=1 | |
109 | ["--interval_disk_space"]=1 | |
110 | ["--interval_disk_io"]=1 | |
111 | ["--interval_loadavg"]=1 | |
112 | ["--interval_temp"]=1 | |
113 | ["--interval_fan"]=1 | |
114 | ["--interval_mem"]=1 | |
4d314e0f | 115 | ) |
d77c7373 | 116 | while : |
4d314e0f | 117 | do |
d77c7373 SK |
118 | key="$1" |
119 | val="$2" | |
120 | case "$key" in | |
121 | '') | |
4d314e0f SK |
122 | break |
123 | ;; | |
d77c7373 SK |
124 | * ) |
125 | if [ -v opts["$key"] ] | |
126 | then | |
44cfa739 | 127 | if [ "$val" != "" ] |
d77c7373 SK |
128 | then |
129 | opts["$key"]="$val" | |
130 | shift | |
131 | shift | |
132 | else | |
133 | echo "Option $key requires an argument" >&2 | |
134 | exit 1 | |
135 | fi | |
136 | else | |
137 | echo "Unknown option: $key" >&2 | |
138 | exit 1 | |
139 | fi | |
4d314e0f SK |
140 | esac |
141 | done | |
142 | ||
b4612a8a SK |
143 | if [ "${opts['--wifi_interface']}" = '' ] |
144 | then | |
145 | echo 'Please provide the required parameter: --wifi_interface' >&2 | |
146 | exit 1 | |
147 | fi | |
148 | ||
6a12cab1 SK |
149 | ( |
150 | echo '==============================================' | |
151 | echo "Khatus starting with the following parameters:" | |
152 | echo '==============================================' | |
d77c7373 | 153 | for param in ${!opts[@]} |
6a12cab1 | 154 | do |
d77c7373 | 155 | echo "$param := ${opts[$param]}" |
6a12cab1 | 156 | done \ |
d77c7373 SK |
157 | | column -ts: \ |
158 | | sort | |
6a12cab1 | 159 | echo '----------------------------------------------' |
4d314e0f SK |
160 | ) >&2 |
161 | ||
d77c7373 SK |
162 | screen_brightness_device_path='/sys/class/backlight' |
163 | screen_brightness_device_path+="/${opts['--screen_brightness_device_name']}" | |
164 | ||
165 | # Just shorthand | |
d33e9656 | 166 | pipe="${opts['--file_pipe']}" |
d77c7373 | 167 | bin="${opts['--dir_bin']}" |
c40b9076 | 168 | perf="${opts['--dir_perf_logs']}" |
d77c7373 | 169 | |
d33e9656 SK |
170 | rm -f "$pipe" |
171 | mkfifo "$pipe" | |
172 | ||
f37162a4 SK |
173 | cmd_sens_screen_brightness='khatus_sensor_screen_brightness' |
174 | cmd_sens_screen_brightness+=" $screen_brightness_device_path" | |
2b221665 | 175 | |
654ea6e2 | 176 | cmd_sens_weather="khatus_sensor_weather $bin ${opts['--weather_station_id']}" |
75b23ff8 SK |
177 | cmd_sens_disk_space="khatus_sensor_disk_space $bin ${opts['--disk_space_device']}" |
178 | cmd_sens_disk_io="khatus_sensor_disk_io $bin ${opts['--disk_io_device']}" | |
d77c7373 | 179 | cmd_sens_temperature="khatus_sensor_temperature ${opts['--thermal_zone']}" |
75b23ff8 | 180 | cmd_sens_fan="khatus_sensor_fan $bin ${opts['--fan_path']}" |
d77c7373 | 181 | cmd_sens_bluetooth="khatus_sensor_bluetooth_power $bin" |
75b23ff8 | 182 | cmd_sens_mpd="khatus_sensor_mpd $bin" |
d77c7373 SK |
183 | cmd_sens_net_addr_io="khatus_sensor_net_addr_io $bin" |
184 | cmd_sens_volume="khatus_sensor_volume $bin" | |
b4612a8a | 185 | cmd_sens_wifi="khatus_sensor_net_wifi_status $bin ${opts['--wifi_interface']}" |
75b23ff8 SK |
186 | cmd_sens_loadavg="khatus_sensor_loadavg $bin" |
187 | cmd_sens_memory="khatus_sensor_memory $bin" | |
188 | ||
e3f715dd | 189 | fork_watcher "$pipe" "$bin" "khatus_sensor_energy $bin" |
cf7cff1c | 190 | fork_watcher "$pipe" "$bin" "khatus_sensor_devices $bin" |
e3f715dd SK |
191 | fork_poller "${opts['--interval_datetime']}" "$perf" "$pipe" "$bin" khatus_sensor_datetime |
192 | fork_poller "${opts['--interval_brightness']}" "$perf" "$pipe" "$bin" "$cmd_sens_screen_brightness" | |
193 | fork_poller "${opts['--interval_weather']}" "$perf" "$pipe" "$bin" "$cmd_sens_weather" | |
194 | fork_poller "${opts['--interval_mpd']}" "$perf" "$pipe" "$bin" "$cmd_sens_mpd" | |
195 | fork_poller "${opts['--interval_volume']}" "$perf" "$pipe" "$bin" "$cmd_sens_volume" | |
196 | fork_poller "${opts['--interval_bluetooth']}" "$perf" "$pipe" "$bin" "$cmd_sens_bluetooth" | |
197 | fork_poller "${opts['--interval_net_wifi']}" "$perf" "$pipe" "$bin" "$cmd_sens_wifi" | |
198 | fork_poller "${opts['--interval_net_io']}" "$perf" "$pipe" "$bin" "$cmd_sens_net_addr_io" | |
199 | fork_poller "${opts['--interval_disk_space']}" "$perf" "$pipe" "$bin" "$cmd_sens_disk_space" | |
200 | fork_poller "${opts['--interval_disk_io']}" "$perf" "$pipe" "$bin" "$cmd_sens_disk_io" | |
201 | fork_poller "${opts['--interval_loadavg']}" "$perf" "$pipe" "$bin" "$cmd_sens_loadavg" | |
202 | fork_poller "${opts['--interval_temp']}" "$perf" "$pipe" "$bin" "$cmd_sens_temperature" | |
203 | fork_poller "${opts['--interval_fan']}" "$perf" "$pipe" "$bin" "$cmd_sens_fan" | |
204 | fork_poller "${opts['--interval_mem']}" "$perf" "$pipe" "$bin" "$cmd_sens_memory" | |
75b23ff8 | 205 | |
9d47c179 | 206 | stdbuf -o L tail -f "$pipe" |
438d0d5f SK |
207 | } |
208 | ||
209 | main $@ |