-v opt_prefixes_of_net_interfaces_to_show="$prefixes_of_net_interfaces_to_show"
}
-spawn() {
+spawn_reactor() {
pipe="$1"
dir_bin="$2"
- cmd="$3"
- msg_head="$4"
- interval="$5"
+ log="$3"
+ cmd="$4"
+ msg_head="$5"
+ "$dir_bin"/$cmd | while read line; do
+ echo "${msg_head} $line" > "$pipe"
+ done \
+ 2> "$log" \
+ &
+}
+
+spawn_poller() {
+ pipe="$1"
+ dir_bin="$2"
+ log="$3"
+ cmd="$4"
+ msg_head="$5"
+ interval="$6"
while true; do
"$dir_bin"/$cmd | while read line; do
echo "${msg_head} $line" > "$pipe"
- done
+ done \
+ 2> "$log"
sleep "$interval"
done &
}
# Defaults
debug=0
dir_bin="$HOME/bin"
- dir_data=$(mktemp -d)
+ dir_logs=$(mktemp -d)
+ file_pipe=$(mktemp)
weather_station_id='KJFK'
screen_brightness_device_name='acpi_video0'
prefixes_of_net_interfaces_to_show='w' # comma-separated
interval_inp_temp=1
interval_inp_fan=1
interval_inp_mem=1
- interval_inp_energy=1
# User-overrides
+ # ---------------------------------------------------------------------
+ # IMPORTANT:
+ # In order for automatic value reporting, at startup, to work - ensure that
+ # long option names match corresponding variable names!
+ # ---------------------------------------------------------------------
long_options=''
long_options+='debug'
- long_options+=',bin_dir:'
- long_options+=',data_dir:'
- long_options+=',weather_station:'
- long_options+=',screen_device:'
+ long_options+=',dir_bin:'
+ long_options+=',dir_logs:'
+ long_options+=',file_pipe:'
+ long_options+=',weather_station_id:'
+ long_options+=',screen_brightness_device_name:'
long_options+=',prefixes_of_net_interfaces_to_show:'
long_options+=',disk_space_device:'
long_options+=',disk_io_device:'
long_options+=',interval_inp_temp:'
long_options+=',interval_inp_fan:'
long_options+=',interval_inp_mem:'
- long_options+=',interval_inp_energy:'
OPTS=$(
getopt \
-o 'd' \
debug=1
shift
;;
- --bin_dir)
+ --dir_bin)
dir_bin="$2"
shift 2
;;
- --data_dir)
- dir_data="$2"
+ --dir_logs)
+ dir_logs="$2"
+ shift 2
+ ;;
+ --file_pipe)
+ file_pipe="$2"
shift 2
;;
- --weather_station)
+ --weather_station_id)
weather_station_id="$2"
shift 2
;;
- --screen_device)
+ --screen_brightness_device_name)
screen_brightness_device_name="$2"
shift 2
;;
interval_inp_mem="$2"
shift 2
;;
- --interval_inp_energy)
- interval_inp_energy="$2"
- shift 2
- ;;
--)
shift
break
esac
done
- pipe="$dir_data/khatus_data_pipe"
screen_brightness_device_path='/sys/class/backlight'
screen_brightness_device_path+="/$screen_brightness_device_name"
- ( echo "Khatus starting with the following parameters:"
- ( echo " debug|= $debug"
- echo " dir_bin|= $dir_bin"
- echo " dir_data|= $dir_data"
- echo " pipe|= $pipe"
- echo " screen_brightness_device_name|= $screen_brightness_device_name"
- echo " screen_brightness_device_path|= $screen_brightness_device_path"
- echo " weather_station_id|= $weather_station_id"
- echo " prefixes_of_net_interfaces_to_show|= $prefixes_of_net_interfaces_to_show"
- echo " disk_space_device|= $disk_space_device"
- echo " disk_io_device|= $disk_io_device"
- echo " thermal_zone|= $thermal_zone"
- echo " fan_path|= $fan_path"
- ) | column -ts\|
- echo ''
+ (
+ echo '=============================================='
+ echo "Khatus starting with the following parameters:"
+ echo '=============================================='
+ for param in \
+ $(echo -n "$long_options" \
+ | awk -v RS=, '{sub(":$", ""); print $0}'
+ )
+ do
+ echo "$param := ${!param}"
+ done \
+ | column -ts:
+ echo '----------------------------------------------'
) >&2
- mkdir -p "$dir_data"
- rm -f "$pipe"
- mkfifo "$pipe"
+ rm -f "$file_pipe"
+ mkfifo "$file_pipe"
cmd_sens_screen_brightness='khatus_sensor_screen_brightness'
cmd_sens_screen_brightness+=" $screen_brightness_device_path"
cmd_sens_fan="khatus_sensor_fan $fan_path"
# TODO: Redirect each worker's stderr to a dedicated log file
- spawn "$pipe" "$dir_bin" khatus_sensor_datetime 'in:DATE_TIME' $interval_inp_datetime
- spawn "$pipe" "$dir_bin" "$cmd_sens_screen_brightness" 'in:SCREEN_BRIGHTNESS' $interval_inp_brightness
- spawn "$pipe" "$dir_bin" "$cmd_sens_weather" 'in:WEATHER' $interval_inp_weather
- spawn "$pipe" "$dir_bin" khatus_sensor_mpd_state 'in:MPD_STATE' $interval_inp_mpd_state
- spawn "$pipe" "$dir_bin" khatus_sensor_mpd_song 'in:MPD_SONG' $interval_inp_mpd_song
- spawn "$pipe" "$dir_bin" khatus_sensor_volume 'in:VOLUME' $interval_inp_volume
- spawn "$pipe" "$dir_bin" khatus_sensor_bluetooth_power 'in:BLUETOOTH_POWER' $interval_inp_bluetooth
- spawn "$pipe" "$dir_bin" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' $interval_inp_net_wifi
- spawn "$pipe" "$dir_bin" khatus_sensor_net_addr_io 'in:NET_ADDR_IO' $interval_inp_net_io
- spawn "$pipe" "$dir_bin" "$cmd_sens_disk_space" 'in:DISK_SPACE' $interval_inp_disk_space
- spawn "$pipe" "$dir_bin" "$cmd_sens_disk_io" 'in:DISK_IO' $interval_inp_disk_io
- spawn "$pipe" "$dir_bin" khatus_sensor_loadavg 'in:LOAD_AVG' $interval_inp_loadavg
- spawn "$pipe" "$dir_bin" "$cmd_sens_temperature" 'in:TEMPERATURE' $interval_inp_temp
- spawn "$pipe" "$dir_bin" "$cmd_sens_fan" 'in:FAN' $interval_inp_fan
- spawn "$pipe" "$dir_bin" khatus_sensor_memory 'in:MEMORY' $interval_inp_mem
- spawn "$pipe" "$dir_bin" khatus_sensor_energy 'in:ENERGY' $interval_inp_energy
+ pipe="$file_pipe"
+ log="$dir_logs"/khatus_sensors.log
+
+ spawn_reactor "$pipe" "$dir_bin" "$log" "khatus_sensor_energy $dir_bin" 'in:ENERGY'
+
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_datetime 'in:DATE_TIME' $interval_inp_datetime
+ spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_screen_brightness" 'in:SCREEN_BRIGHTNESS' $interval_inp_brightness
+ spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_weather" 'in:WEATHER' $interval_inp_weather
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_state 'in:MPD_STATE' $interval_inp_mpd_state
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_song 'in:MPD_SONG' $interval_inp_mpd_song
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_volume 'in:VOLUME' $interval_inp_volume
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_bluetooth_power 'in:BLUETOOTH_POWER' $interval_inp_bluetooth
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' $interval_inp_net_wifi
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_net_addr_io 'in:NET_ADDR_IO' $interval_inp_net_io
+ spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_space" 'in:DISK_SPACE' $interval_inp_disk_space
+ spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_io" 'in:DISK_IO' $interval_inp_disk_io
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_loadavg 'in:LOAD_AVG' $interval_inp_loadavg
+ spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_temperature" 'in:TEMPERATURE' $interval_inp_temp
+ spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_fan" 'in:FAN' $interval_inp_fan
+ spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_memory 'in:MEMORY' $interval_inp_mem
consume \
"$pipe" \