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