| 1 | #! /bin/bash |
| 2 | |
| 3 | MSG_FS='|' |
| 4 | |
| 5 | set -e |
| 6 | |
| 7 | executable_name_of_cmd() { |
| 8 | basename "$(echo $1 | awk '{print $1; exit}')" |
| 9 | } |
| 10 | |
| 11 | run_producer() { |
| 12 | pipe="$1" |
| 13 | bin="$2" |
| 14 | cmd="$3" |
| 15 | executable_name="$4" |
| 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 \ |
| 36 | 2> >( |
| 37 | while read line |
| 38 | do |
| 39 | echo "${NODE}${MSG_FS}${executable_name}${MSG_FS}error${MSG_FS}$line" > "$pipe" |
| 40 | done \ |
| 41 | ) \ |
| 42 | | while read line |
| 43 | do |
| 44 | echo "${NODE}${MSG_FS}${executable_name}${MSG_FS}data${MSG_FS}$line" > "$pipe" |
| 45 | done |
| 46 | cmd_exit_code=${PIPESTATUS[0]} |
| 47 | if [ "$cmd_exit_code" -ne 0 ] |
| 48 | then |
| 49 | echo |
| 50 | "${NODE}${MSG_FS}${executable_name}${MSG_FS}error${MSG_FS}NON_ZERO_EXIT_CODE${MSG_FS}$cmd_exit_code" \ |
| 51 | > "$pipe" |
| 52 | fi |
| 53 | } |
| 54 | |
| 55 | fork_watcher() { |
| 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" & |
| 61 | } |
| 62 | |
| 63 | fork_poller() { |
| 64 | interval="$1" |
| 65 | perf_log_dir="$2" |
| 66 | shift 2 |
| 67 | pipe="$1" |
| 68 | bin="$2" |
| 69 | cmd="$3" |
| 70 | |
| 71 | executable_name=$(basename "$(echo $cmd | awk '{print $1; exit}')") |
| 72 | |
| 73 | if [ ! "$perf_log_dir" = '' ] |
| 74 | then |
| 75 | cmd="$3" |
| 76 | perf_log_file=${executable_name}.log |
| 77 | mkdir -p "$perf_log_dir" |
| 78 | perf_log_path="$perf_log_dir/$perf_log_file" |
| 79 | fi |
| 80 | |
| 81 | while : |
| 82 | do |
| 83 | run_producer "$pipe" "$bin" "$cmd" "$executable_name" "$perf_log_path" |
| 84 | sleep "$interval" |
| 85 | done & |
| 86 | } |
| 87 | |
| 88 | main() { |
| 89 | declare -A opts=( |
| 90 | ["--node"]=$(hostname) |
| 91 | ["--dir_bin"]="$HOME/bin" |
| 92 | ["--dir_perf_logs"]='' |
| 93 | ["--file_pipe"]=$(mktemp) |
| 94 | ["--weather_station_id"]='KJFK' |
| 95 | ["--screen_brightness_device_name"]='acpi_video0' |
| 96 | ["--wifi_interface"]='' |
| 97 | ["--disk_space_device"]='/' |
| 98 | ["--disk_io_device"]='sda' |
| 99 | ["--thermal_zone"]=0 |
| 100 | ["--fan_path"]='/proc/acpi/ibm/fan' |
| 101 | ["--pulseaudio_sink"]='0' |
| 102 | ["--interval_datetime"]=1 |
| 103 | ["--interval_procs"]=1 |
| 104 | ["--interval_brightness"]=1 |
| 105 | ["--interval_weather"]=$(( 30 * 60)) # 30 minutes |
| 106 | ["--interval_mpd"]=1 |
| 107 | ["--interval_volume"]=1 |
| 108 | ["--interval_bluetooth"]=1 |
| 109 | ["--interval_net_wifi"]=1 |
| 110 | ["--interval_net_io"]=1 |
| 111 | ["--interval_net_carrier"]=1 |
| 112 | ["--interval_disk_space"]=1 |
| 113 | ["--interval_disk_io"]=1 |
| 114 | ["--interval_loadavg"]=1 |
| 115 | ["--interval_temp"]=1 |
| 116 | ["--interval_fan"]=1 |
| 117 | ["--interval_mem"]=1 |
| 118 | ) |
| 119 | while : |
| 120 | do |
| 121 | key="$1" |
| 122 | val="$2" |
| 123 | case "$key" in |
| 124 | '') |
| 125 | break |
| 126 | ;; |
| 127 | * ) |
| 128 | if [ -v opts["$key"] ] |
| 129 | then |
| 130 | if [ "$val" != "" ] |
| 131 | then |
| 132 | opts["$key"]="$val" |
| 133 | shift |
| 134 | shift |
| 135 | else |
| 136 | echo "Option $key requires an argument" >&2 |
| 137 | exit 1 |
| 138 | fi |
| 139 | else |
| 140 | echo "Unknown option: $key" >&2 |
| 141 | exit 1 |
| 142 | fi |
| 143 | esac |
| 144 | done |
| 145 | |
| 146 | if [ "${opts['--wifi_interface']}" = '' ] |
| 147 | then |
| 148 | echo 'Please provide the required parameter: --wifi_interface' >&2 |
| 149 | exit 1 |
| 150 | fi |
| 151 | |
| 152 | ( |
| 153 | echo '==============================================' |
| 154 | echo "Khatus starting with the following parameters:" |
| 155 | echo '==============================================' |
| 156 | for param in ${!opts[@]} |
| 157 | do |
| 158 | echo "$param := ${opts[$param]}" |
| 159 | done \ |
| 160 | | column -ts: \ |
| 161 | | sort |
| 162 | echo '----------------------------------------------' |
| 163 | ) >&2 |
| 164 | |
| 165 | NODE="${opts['--node']}" |
| 166 | |
| 167 | screen_brightness_device_path='/sys/class/backlight' |
| 168 | screen_brightness_device_path+="/${opts['--screen_brightness_device_name']}" |
| 169 | |
| 170 | # Just shorthand |
| 171 | pipe="${opts['--file_pipe']}" |
| 172 | bin="${opts['--dir_bin']}" |
| 173 | perf="${opts['--dir_perf_logs']}" |
| 174 | |
| 175 | rm -f "$pipe" |
| 176 | mkfifo "$pipe" |
| 177 | |
| 178 | cmd_sens_screen_brightness='khatus_sensor_screen_brightness' |
| 179 | cmd_sens_screen_brightness+=" $screen_brightness_device_path" |
| 180 | |
| 181 | cmd_sens_weather="khatus_sensor_weather $bin ${opts['--weather_station_id']}" |
| 182 | cmd_sens_disk_space="khatus_sensor_disk_space $bin ${opts['--disk_space_device']}" |
| 183 | cmd_sens_disk_io="khatus_sensor_disk_io $bin ${opts['--disk_io_device']}" |
| 184 | cmd_sens_temperature="khatus_sensor_temperature ${opts['--thermal_zone']}" |
| 185 | cmd_sens_fan="khatus_sensor_fan $bin ${opts['--fan_path']}" |
| 186 | cmd_sens_bluetooth="khatus_sensor_bluetooth_power $bin" |
| 187 | cmd_sens_mpd="khatus_sensor_mpd $bin" |
| 188 | cmd_sens_net_addr_io="khatus_sensor_net_addr_io $bin" |
| 189 | cmd_sens_volume="khatus_sensor_volume $bin" |
| 190 | cmd_sens_wifi="khatus_sensor_net_wifi_status $bin ${opts['--wifi_interface']}" |
| 191 | cmd_sens_loadavg="khatus_sensor_loadavg $bin" |
| 192 | cmd_sens_memory="khatus_sensor_memory $bin" |
| 193 | |
| 194 | fork_watcher "$pipe" "$bin" "khatus_sensor_energy $bin" |
| 195 | fork_watcher "$pipe" "$bin" "khatus_sensor_devices $bin" |
| 196 | fork_poller "${opts['--interval_datetime']}" "$perf" "$pipe" "$bin" khatus_sensor_datetime |
| 197 | fork_poller "${opts['--interval_procs']}" "$perf" "$pipe" "$bin" "khatus_sensor_procs $bin" |
| 198 | fork_poller "${opts['--interval_brightness']}" "$perf" "$pipe" "$bin" "$cmd_sens_screen_brightness" |
| 199 | fork_poller "${opts['--interval_weather']}" "$perf" "$pipe" "$bin" "$cmd_sens_weather" |
| 200 | fork_poller "${opts['--interval_mpd']}" "$perf" "$pipe" "$bin" "$cmd_sens_mpd" |
| 201 | fork_poller "${opts['--interval_volume']}" "$perf" "$pipe" "$bin" "$cmd_sens_volume" |
| 202 | fork_poller "${opts['--interval_bluetooth']}" "$perf" "$pipe" "$bin" "$cmd_sens_bluetooth" |
| 203 | fork_poller "${opts['--interval_net_wifi']}" "$perf" "$pipe" "$bin" "$cmd_sens_wifi" |
| 204 | fork_poller "${opts['--interval_net_io']}" "$perf" "$pipe" "$bin" "$cmd_sens_net_addr_io" |
| 205 | fork_poller "${opts['--interval_disk_space']}" "$perf" "$pipe" "$bin" "$cmd_sens_disk_space" |
| 206 | fork_poller "${opts['--interval_disk_io']}" "$perf" "$pipe" "$bin" "$cmd_sens_disk_io" |
| 207 | fork_poller "${opts['--interval_loadavg']}" "$perf" "$pipe" "$bin" "$cmd_sens_loadavg" |
| 208 | fork_poller "${opts['--interval_temp']}" "$perf" "$pipe" "$bin" "$cmd_sens_temperature" |
| 209 | fork_poller "${opts['--interval_fan']}" "$perf" "$pipe" "$bin" "$cmd_sens_fan" |
| 210 | fork_poller "${opts['--interval_mem']}" "$perf" "$pipe" "$bin" "$cmd_sens_memory" |
| 211 | fork_poller "${opts['--interval_net_carrier']}" "$perf" "$pipe" "$bin" khatus_sensor_net_carrier |
| 212 | |
| 213 | stdbuf -o L tail -f "$pipe" |
| 214 | } |
| 215 | |
| 216 | main $@ |