zmapi/camwatch
Frank de Lange 5bb1b8cef2 all: create $confdir if it does not exist
zmapi, cam: refactor, use main() so that bash finds trap_error/trap_clean
2022-10-02 10:49:16 +00:00

60 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
version="1.0.1"
release="20221001"
declare -a monitors
threshold=3
confdir="${XDG_CONFIG_HOME:-$HOME/.config}/zmapi"
config="camwatch.conf"
mon_stat_store="$confdir/mon_stat_store"
if [ -f "$confdir/$config" ]; then
source "$confdir/$config"
else
echo "configuration file ($confdir/$config) missing"
echo "new file created, edit it and restart program"
mkdir -p "$confdir"
cat <<-EOT > "$confdir/$config"
monitors=()
threshold=3
mon_stat_store="$confdir/mon_stat_store"
EOT
exit 1
fi
declare -A mon_fps
declare -A mon_stat
declare -A cam_reboot
[ -f $mon_stat_store ] && source $mon_stat_store
if ! declare -p mon_stat 2>&1>/dev/null;then declare -A mon_stat;fi
while read mon fps; do
mon_fps["$mon"]="$fps"
done <<<"$(zmapi monitors|jq -r '.[][]["Monitor_Status"]|[.MonitorId,.CaptureFPS] |join(" ")')"
for mon in ${monitors[*]};do
if [[ ${mon_fps[$mon]} == "0.00" ]]; then
echo "$mon not capturing"
mon_stat[$mon]=$((${mon_stat[$mon]}+1))
if [ ${mon_stat[$mon]} -gt $threshold ]; then
echo "Monitor $mon above threshold"
cam_ip=$(zmapi monitor $mon|jq '.[].Monitor.Path'|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
cam_reboot[$cam_ip]=1
mon_stat[$mon]=0
fi
else
mon_stat[$mon]=0
fi
done
declare -p mon_stat > $mon_stat_store
for ip in ${!cam_reboot[*]};do
echo "rebooting cam $ip..."
cam "$ip" reboot
done