watchcat: refactor function names
Signed-off-by: Nicholas Smith <nicholas@nbembedded.com>
This commit is contained in:
parent
dc33ae8027
commit
ddd2729313
3 changed files with 116 additions and 96 deletions
|
@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
PKG_NAME:=watchcat
|
PKG_NAME:=watchcat
|
||||||
PKG_VERSION:=1
|
PKG_VERSION:=1
|
||||||
PKG_RELEASE:=8
|
PKG_RELEASE:=9
|
||||||
|
|
||||||
PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
|
PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
|
||||||
PKG_LICENSE:=GPL-2.0
|
PKG_LICENSE:=GPL-2.0
|
||||||
|
|
|
@ -5,7 +5,10 @@ START=97
|
||||||
PIDFILE="/tmp/run/watchcat"
|
PIDFILE="/tmp/run/watchcat"
|
||||||
|
|
||||||
append_string() {
|
append_string() {
|
||||||
local varname="$1"; local add="$2"; local separator="${3:- }"; local actual
|
local varname="$1"
|
||||||
|
local add="$2"
|
||||||
|
local separator="${3:- }"
|
||||||
|
local actual
|
||||||
eval "actual=\$$varname"
|
eval "actual=\$$varname"
|
||||||
|
|
||||||
new="${actual:+$actual$separator}$add"
|
new="${actual:+$actual$separator}$add"
|
||||||
|
@ -16,112 +19,119 @@ timetoseconds() {
|
||||||
local time=$1
|
local time=$1
|
||||||
unset seconds
|
unset seconds
|
||||||
|
|
||||||
{ [ "$time" -ge 1 ] 2> /dev/null && seconds="$time"; } || \
|
{ [ "$time" -ge 1 ] 2>/dev/null && seconds="$time"; } ||
|
||||||
{ [ "${time%s}" -ge 1 ] 2> /dev/null && seconds="${time%s}"; } || \
|
{ [ "${time%s}" -ge 1 ] 2>/dev/null && seconds="${time%s}"; } ||
|
||||||
{ [ "${time%m}" -ge 1 ] 2> /dev/null && seconds=$((${time%m}*60)); } || \
|
{ [ "${time%m}" -ge 1 ] 2>/dev/null && seconds=$((${time%m} * 60)); } ||
|
||||||
{ [ "${time%h}" -ge 1 ] 2> /dev/null && seconds=$((${time%h}*3600)); } || \
|
{ [ "${time%h}" -ge 1 ] 2>/dev/null && seconds=$((${time%h} * 3600)); } ||
|
||||||
{ [ "${time%d}" -ge 1 ] 2> /dev/null && seconds=$((${time%d}*86400)); }
|
{ [ "${time%d}" -ge 1 ] 2>/dev/null && seconds=$((${time%d} * 86400)); }
|
||||||
}
|
}
|
||||||
|
|
||||||
load_watchcat() {
|
load_watchcat() {
|
||||||
config_get period $1 period
|
config_get period $1 period
|
||||||
config_get mode $1 mode
|
config_get mode $1 mode
|
||||||
config_get pinghosts $1 pinghosts
|
config_get pinghosts $1 pinghosts
|
||||||
config_get pingperiod $1 pingperiod
|
config_get pingperiod $1 pingperiod
|
||||||
config_get nopingtime $1 nopingtime
|
config_get nopingtime $1 nopingtime
|
||||||
config_get forcedelay $1 forcedelay
|
config_get forcedelay $1 forcedelay
|
||||||
|
|
||||||
local nopingtime_dflt="900"
|
local nopingtime_dflt="900"
|
||||||
local forcedelay_dflt="60"
|
local forcedelay_dflt="60"
|
||||||
|
|
||||||
# Fix potential typo in mode (backward compatibility).
|
|
||||||
[ "$mode" = "allways" ] && mode="always"
|
|
||||||
|
|
||||||
|
# Fix potential typo in mode (backward compatibility).
|
||||||
|
[ "$mode" = "allways" ] && mode="periodic_reboot"
|
||||||
|
[ "$mode" = "always" ] && mode="periodic_reboot"
|
||||||
|
[ "$mode" = "ping" ] && mode="ping_reboot"
|
||||||
|
|
||||||
error=""
|
error=""
|
||||||
warn=""
|
warn=""
|
||||||
|
|
||||||
if [ -z "$period" ]
|
if [ -z "$period" ]; then
|
||||||
then
|
append_string "error" "period is not set! Use time value(ex: '30'; '4m'; '6h'; '2d')." "; "
|
||||||
append_string "error" "period is not set! Use time value(ex: '30'; '4m'; '6h'; '2d')." "; "
|
|
||||||
else
|
else
|
||||||
timetoseconds "$period";period="$seconds"
|
timetoseconds "$period"
|
||||||
[ "$period" -ge 1 ] \
|
period="$seconds"
|
||||||
|| append_string "error" "period has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
|
[ "$period" -ge 1 ] ||
|
||||||
|
append_string "error" "period has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ "$mode" = "always" -o "$mode" = "ping" ] \
|
[ "$mode" = "periodic_reboot" -o "$mode" = "ping_reboot" ] ||
|
||||||
|| append_string "error" "mode must be 'always' or 'ping'" "; "
|
append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot'" "; "
|
||||||
|
|
||||||
if [ -z "$forcedelay" ]
|
if [ -z "$forcedelay" ]; then
|
||||||
then
|
forcedelay="$forcedelay_dflt"
|
||||||
forcedelay="$forcedelay_dflt"
|
append_string "warn" "forcedelay is not configured! Defaulted to $forcedelay seconds" "; "
|
||||||
append_string "warn" "forcedelay is not configured! Defaulted to $forcedelay seconds" "; "
|
|
||||||
else
|
else
|
||||||
[ "$forcedelay" -ge 0 ] || {
|
[ "$forcedelay" -ge 0 ] || {
|
||||||
forcedelay="$forcedelay_dflt"
|
forcedelay="$forcedelay_dflt"
|
||||||
append_string "warn" "forcedelay is invalid! Defaulted to $forcedelay seconds" "; "
|
append_string "warn" "forcedelay is invalid! Defaulted to $forcedelay seconds" "; "
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ -z "$error" -a "$mode" = "ping" ] && {
|
[ -z "$error" -a "$mode" = "ping_reboot" ] && {
|
||||||
[ -z "$pinghosts" ] \
|
[ -z "$pinghosts" ] &&
|
||||||
&& append_string "error" "pinghosts must be set in 'ping' mode! Use space separated address list (ex: '8.8.8.8 9.9.9.9')" "; "
|
append_string "error" "pinghosts must be set in 'ping_reboot' mode! Use space separated address list (ex: '8.8.8.8 9.9.9.9')" "; "
|
||||||
|
|
||||||
if [ -z "$nopingtime" ]
|
if [ -z "$nopingtime" ]; then
|
||||||
then
|
|
||||||
nopingtime="$nopingtime_dflt"
|
nopingtime="$nopingtime_dflt"
|
||||||
append_string "warn" "nopingtime is not configured! Defaulted to $nopingtime seconds" "; "
|
append_string "warn" "nopingtime is not configured! Defaulted to $nopingtime seconds" "; "
|
||||||
else
|
else
|
||||||
timetoseconds "$nopingtime";nopingtime="$seconds"
|
timetoseconds "$nopingtime"
|
||||||
|
nopingtime="$seconds"
|
||||||
[ "$nopingtime" -ge 0 ] || {
|
[ "$nopingtime" -ge 0 ] || {
|
||||||
nopingtime="$nopingtime_dflt"
|
nopingtime="$nopingtime_dflt"
|
||||||
append_string "warn" "nopingtime invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $nopingtime seconds" "; "
|
append_string "warn" "nopingtime invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $nopingtime seconds" "; "
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local pingperiod_dflt="$((period/5))"
|
local pingperiod_dflt="$((period / 5))"
|
||||||
|
|
||||||
if [ -z "$pingperiod" ]
|
if [ -z "$pingperiod" ]; then
|
||||||
then
|
|
||||||
pingperiod="$pingperiod_dflt"
|
pingperiod="$pingperiod_dflt"
|
||||||
append_string "warn" "pingperiod is not configured! Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
append_string "warn" "pingperiod is not configured! Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
||||||
else
|
else
|
||||||
timetoseconds "$pingperiod";pingperiod="$seconds"
|
timetoseconds "$pingperiod"
|
||||||
|
pingperiod="$seconds"
|
||||||
[ "$pingperiod" -ge 0 -a "$pingperiod" -ge "$period" ] && {
|
[ "$pingperiod" -ge 0 -a "$pingperiod" -ge "$period" ] && {
|
||||||
pingperiod="$pingperiod_dflt"
|
pingperiod="$pingperiod_dflt"
|
||||||
append_string "warn" "pingperiod is invalid value(greater than period)! Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
append_string "warn" "pingperiod is invalid value(greater than period)! Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
||||||
}
|
}
|
||||||
[ "$pingperiod" -ge 0 ] || {
|
[ "$pingperiod" -ge 0 ] || {
|
||||||
pingperiod="$pingperiod_dflt"
|
pingperiod="$pingperiod_dflt"
|
||||||
append_string "warn" "pingperiod has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
append_string "warn" "pingperiod has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
[ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
|
[ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
|
||||||
[ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
|
[ -n "$error" ] && {
|
||||||
|
logger -p user.err -t "watchcat" "reboot program $1 not started - $error"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if [ "$mode" = "always" ]
|
case "$mode" in
|
||||||
then
|
periodic_reboot)
|
||||||
/usr/bin/watchcat.sh "always" "$period" "$forcedelay" &
|
/usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay" &
|
||||||
logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
|
logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
|
||||||
else
|
;;
|
||||||
/usr/bin/watchcat.sh "ping" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$nopingtime" &
|
ping_reboot)
|
||||||
logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay;nopingtime=$nopingtime)"
|
/usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$ping_hosts" "$ping_period" "$nopingtime" "$ping_size" &
|
||||||
fi
|
logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;ping_hosts=$ping_hosts;ping_period=$ping_period;forcedelay=$forcedelay;nopingtime=$nopingtime)"
|
||||||
|
;;
|
||||||
echo $! >> "${PIDFILE}.pids"
|
*)
|
||||||
|
echo "Error starting Watchcat service. Invalid mode selection: $mode"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
echo $! >>"${PIDFILE}.pids"
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
if [ -f "${PIDFILE}.pids" ]
|
if [ -f "${PIDFILE}.pids" ]; then
|
||||||
then
|
|
||||||
logger -p user.info -t "watchcat" "stopping all tasks"
|
logger -p user.info -t "watchcat" "stopping all tasks"
|
||||||
|
|
||||||
while read pid
|
while read pid; do
|
||||||
do
|
|
||||||
kill -KILL "$pid"
|
kill -KILL "$pid"
|
||||||
done < "${PIDFILE}.pids"
|
done <"${PIDFILE}.pids"
|
||||||
|
|
||||||
rm "${PIDFILE}.pids"
|
rm "${PIDFILE}.pids"
|
||||||
|
|
||||||
|
@ -135,8 +145,7 @@ start() {
|
||||||
[ -f "${PIDFILE}.pids" ] && stop
|
[ -f "${PIDFILE}.pids" ] && stop
|
||||||
|
|
||||||
config_load system
|
config_load system
|
||||||
if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
|
if [ -n "$(uci show system.@watchcat[0])" ]; then # at least one watchcat section exists
|
||||||
then
|
|
||||||
logger -p user.info -t "watchcat" "starting all tasks"
|
logger -p user.info -t "watchcat" "starting all tasks"
|
||||||
config_foreach load_watchcat watchcat
|
config_foreach load_watchcat watchcat
|
||||||
logger -p user.info -t "watchcat" "all tasks started"
|
logger -p user.info -t "watchcat" "all tasks started"
|
||||||
|
|
|
@ -10,56 +10,67 @@ reboot_now() {
|
||||||
|
|
||||||
[ "$1" -ge 1 ] && {
|
[ "$1" -ge 1 ] && {
|
||||||
sleep "$1"
|
sleep "$1"
|
||||||
echo 1 > /proc/sys/kernel/sysrq
|
echo 1 >/proc/sys/kernel/sysrq
|
||||||
echo b > /proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
|
echo b >/proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
watchcat_always() {
|
watchcat_periodic() {
|
||||||
local period="$1"; local forcedelay="$2"
|
local period="$1"
|
||||||
|
local forcedelay="$2"
|
||||||
|
|
||||||
sleep "$period" && reboot_now "$forcedelay"
|
sleep "$period" && reboot_now "$forcedelay"
|
||||||
}
|
}
|
||||||
|
|
||||||
watchcat_ping() {
|
watchcat_ping() {
|
||||||
local period="$1"; local forcedelay="$2"; local pinghosts="$3"; local pingperiod="$4"; local nopingtime="$5"
|
local period="$1"
|
||||||
|
local forcedelay="$2"
|
||||||
|
local pinghosts="$3"
|
||||||
|
local pingperiod="$4"
|
||||||
|
local nopingtime="$5"
|
||||||
|
|
||||||
local time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
|
local time_now="$(cat /proc/uptime)"
|
||||||
|
time_now="${time_now%%.*}"
|
||||||
|
|
||||||
[ "$time_now" -lt "$nopingtime" ] && sleep "$((nopingtime-time_now))"
|
[ "$time_now" -lt "$nopingtime" ] && sleep "$((nopingtime - time_now))"
|
||||||
|
|
||||||
time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
|
time_now="$(cat /proc/uptime)"
|
||||||
|
time_now="${time_now%%.*}"
|
||||||
local time_lastcheck="$time_now"
|
local time_lastcheck="$time_now"
|
||||||
local time_lastcheck_withinternet="$time_now"
|
local time_lastcheck_withinternet="$time_now"
|
||||||
|
|
||||||
while true
|
while true; do
|
||||||
do
|
|
||||||
# account for the time ping took to return. With a ping time of 5s, ping might take more than that, so it is important to avoid even more delay.
|
# account for the time ping took to return. With a ping time of 5s, ping might take more than that, so it is important to avoid even more delay.
|
||||||
time_now="$(cat /proc/uptime)"; time_now="${time_now%%.*}"
|
time_now="$(cat /proc/uptime)"
|
||||||
local time_diff="$((time_now-time_lastcheck))"
|
time_now="${time_now%%.*}"
|
||||||
|
local time_diff="$((time_now - time_lastcheck))"
|
||||||
|
|
||||||
[ "$time_diff" -lt "$pingperiod" ] && sleep "$((pingperiod-time_diff))"
|
[ "$time_diff" -lt "$pingperiod" ] && sleep "$((pingperiod - time_diff))"
|
||||||
|
|
||||||
time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
|
time_now="$(cat /proc/uptime)"
|
||||||
|
time_now="${time_now%%.*}"
|
||||||
time_lastcheck="$time_now"
|
time_lastcheck="$time_now"
|
||||||
|
|
||||||
for host in $pinghosts
|
for host in $pinghosts; do
|
||||||
do
|
if ping -c 1 "$host" &>/dev/null; then
|
||||||
if ping -c 1 "$host" &> /dev/null
|
|
||||||
then
|
|
||||||
time_lastcheck_withinternet="$time_now"
|
time_lastcheck_withinternet="$time_now"
|
||||||
else
|
else
|
||||||
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now-time_lastcheck_withinternet)). Reseting when reaching $period"
|
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now - time_lastcheck_withinternet)). Reseting when reaching $period"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
[ "$((time_now-time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$forcedelay"
|
[ "$((time_now - time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$forcedelay"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ "$1" = "always" ]
|
case "$mode" in
|
||||||
then
|
periodic_reboot)
|
||||||
watchcat_always "$2" "$3"
|
watchcat_periodic "$2" "$3"
|
||||||
else
|
;;
|
||||||
|
ping_reboot)
|
||||||
watchcat_ping "$2" "$3" "$4" "$5" "$6"
|
watchcat_ping "$2" "$3" "$4" "$5" "$6"
|
||||||
fi
|
;;
|
||||||
|
*)
|
||||||
|
echo "Error: invalid mode selected: $mode"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
Loading…
Reference in a new issue