watchcat: run through shfmt

Cosmetic fixes mainly.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
This commit is contained in:
Rosen Penev 2021-08-08 20:03:54 -07:00
parent b4a4aab5fa
commit 933de68f3f
4 changed files with 196 additions and 196 deletions

View File

@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=watchcat PKG_NAME:=watchcat
PKG_VERSION:=1 PKG_VERSION:=1
PKG_RELEASE:=12 PKG_RELEASE:=13
PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com> PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
PKG_LICENSE:=GPL-2.0 PKG_LICENSE:=GPL-2.0

View File

@ -3,21 +3,21 @@
. /lib/functions.sh . /lib/functions.sh
upgrade_watchcat() { upgrade_watchcat() {
local cfg="$1" local cfg="$1"
config_get period "$cfg" period config_get period "$cfg" period
config_get mode "$cfg" mode config_get mode "$cfg" mode
config_get pinghosts "$cfg" pinghosts config_get pinghosts "$cfg" pinghosts
config_get forcedelay "$cfg" forcedelay config_get forcedelay "$cfg" forcedelay
[ -f "/etc/config/watchcat" ] || touch /etc/config/watchcat [ -f "/etc/config/watchcat" ] || touch /etc/config/watchcat
uci_add watchcat watchcat uci_add watchcat watchcat
uci_set watchcat @watchcat[-1] period "$period" uci_set watchcat @watchcat[-1] period "$period"
uci_set watchcat @watchcat[-1] mode "$mode" uci_set watchcat @watchcat[-1] mode "$mode"
uci_set watchcat @watchcat[-1] pinghosts "$pinghosts" uci_set watchcat @watchcat[-1] pinghosts "$pinghosts"
uci_set watchcat @watchcat[-1] forcedelay "$forcedelay" uci_set watchcat @watchcat[-1] forcedelay "$forcedelay"
uci_remove system "$cfg" uci_remove system "$cfg"
} }
config_load system config_load system

View File

@ -19,11 +19,11 @@ append_string() {
time_to_seconds() { time_to_seconds() {
time=$1 time=$1
{ [ "$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)); }
echo $seconds echo $seconds
unset seconds unset seconds
@ -46,7 +46,7 @@ config_watchcat() {
[ "$mode" = "allways" ] && mode="periodic_reboot" [ "$mode" = "allways" ] && mode="periodic_reboot"
[ "$mode" = "always" ] && mode="periodic_reboot" [ "$mode" = "always" ] && mode="periodic_reboot"
[ "$mode" = "ping" ] && mode="ping_reboot" [ "$mode" = "ping" ] && mode="ping_reboot"
# Checks for settings common to all operation modes # Checks for settings common to all operation modes
if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ]; then if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ]; then
append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface'" "; " append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface'" "; "
@ -87,30 +87,30 @@ config_watchcat() {
logger -p user.err -t "watchcat" "reboot program $1 not started - $error" logger -p user.err -t "watchcat" "reboot program $1 not started - $error"
return return
} }
# Need to conditionally run mode functions because they have different signatures # Need to conditionally run mode functions because they have different signatures
case "$mode" in case "$mode" in
periodic_reboot) periodic_reboot)
procd_open_instance "watchcat_${1}" procd_open_instance "watchcat_${1}"
procd_set_param command /usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay" procd_set_param command /usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay"
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5} procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
procd_close_instance procd_close_instance
;; ;;
ping_reboot) ping_reboot)
procd_open_instance "watchcat_${1}" procd_open_instance "watchcat_${1}"
procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize" procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize"
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5} procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
procd_close_instance procd_close_instance
;; ;;
restart_iface) restart_iface)
procd_open_instance "watchcat_${1}" procd_open_instance "watchcat_${1}"
procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename" procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename"
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5} procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
procd_close_instance procd_close_instance
;; ;;
*) *)
echo "Error starting Watchcat service. Invalid mode selection: $mode" echo "Error starting Watchcat service. Invalid mode selection: $mode"
;; ;;
esac esac
} }

View File

@ -7,197 +7,197 @@
# #
get_ping_size() { get_ping_size() {
ps=$1 ps=$1
case "$ps" in case "$ps" in
small) small)
ps="1" ps="1"
;; ;;
windows) windows)
ps="32" ps="32"
;; ;;
standard) standard)
ps="56" ps="56"
;; ;;
big) big)
ps="248" ps="248"
;; ;;
huge) huge)
ps="1492" ps="1492"
;; ;;
jumbo) jumbo)
ps="9000" ps="9000"
;; ;;
*) *)
echo "Error: invalid ping_size. ping_size should be either: small, windows, standard, big, huge or jumbo" echo "Error: invalid ping_size. ping_size should be either: small, windows, standard, big, huge or jumbo"
echo "Cooresponding ping packet sizes (bytes): small=1, windows=32, standard=56, big=248, huge=1492, jumbo=9000" echo "Cooresponding ping packet sizes (bytes): small=1, windows=32, standard=56, big=248, huge=1492, jumbo=9000"
;; ;;
esac esac
echo $ps echo $ps
} }
reboot_now() { reboot_now() {
reboot & reboot &
[ "$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_periodic() { watchcat_periodic() {
failure_period="$1" failure_period="$1"
force_reboot_delay="$2" force_reboot_delay="$2"
sleep "$failure_period" && reboot_now "$force_reboot_delay" sleep "$failure_period" && reboot_now "$force_reboot_delay"
} }
watchcat_restart_modemmanager_iface() { watchcat_restart_modemmanager_iface() {
[ "$2" -gt 0 ] && { [ "$2" -gt 0 ] && {
logger -t INFO "Resetting current-bands to 'any' on modem: \"$1\" now." logger -t INFO "Resetting current-bands to 'any' on modem: \"$1\" now."
/usr/bin/mmcli -m any --set-current-bands=any /usr/bin/mmcli -m any --set-current-bands=any
} }
logger -t INFO "Reconnecting modem: \"$1\" now." logger -t INFO "Reconnecting modem: \"$1\" now."
/etc/init.d/modemmanager restart /etc/init.d/modemmanager restart
ifup "$1" ifup "$1"
} }
watchcat_restart_network_iface() { watchcat_restart_network_iface() {
logger -t INFO "Restarting network interface: \"$1\"." logger -t INFO "Restarting network interface: \"$1\"."
ip link set "$1" down ip link set "$1" down
ip link set "$1" up ip link set "$1" up
} }
watchcat_restart_all_network() { watchcat_restart_all_network() {
logger -t INFO "Restarting networking now by running: /etc/init.d/network restart" logger -t INFO "Restarting networking now by running: /etc/init.d/network restart"
/etc/init.d/network restart /etc/init.d/network restart
} }
watchcat_monitor_network() { watchcat_monitor_network() {
failure_period="$1" failure_period="$1"
ping_hosts="$2" ping_hosts="$2"
ping_frequency_interval="$3" ping_frequency_interval="$3"
ping_size="$4" ping_size="$4"
iface="$5" iface="$5"
mm_iface_name="$6" mm_iface_name="$6"
mm_iface_unlock_bands="$7" mm_iface_unlock_bands="$7"
time_now="$(cat /proc/uptime)" time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
[ "$time_now" -lt "$failure_period" ] && sleep "$((failure_period - time_now))" [ "$time_now" -lt "$failure_period" ] && sleep "$((failure_period - time_now))"
time_now="$(cat /proc/uptime)" time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
time_lastcheck="$time_now" time_lastcheck="$time_now"
time_lastcheck_withinternet="$time_now" time_lastcheck_withinternet="$time_now"
ping_size="$(get_ping_size "$ping_size")" ping_size="$(get_ping_size "$ping_size")"
while true; do while true; 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="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
time_diff="$((time_now - time_lastcheck))" time_diff="$((time_now - time_lastcheck))"
[ "$time_diff" -lt "$ping_frequency_interval" ] && sleep "$((ping_frequency_interval - time_diff))" [ "$time_diff" -lt "$ping_frequency_interval" ] && sleep "$((ping_frequency_interval - time_diff))"
time_now="$(cat /proc/uptime)" time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
time_lastcheck="$time_now" time_lastcheck="$time_now"
for host in $ping_hosts; do for host in $ping_hosts; do
if [ "$iface" != "" ]; then if [ "$iface" != "" ]; then
ping_result="$( ping_result="$(
ping -I "$iface" -s "$ping_size" -c 1 "$host" &>/dev/null ping -I "$iface" -s "$ping_size" -c 1 "$host" &> /dev/null
echo $? echo $?
)" )"
else else
ping_result="$( ping_result="$(
ping -s "$ping_size" -c 1 "$host" &>/dev/null ping -s "$ping_size" -c 1 "$host" &> /dev/null
echo $? echo $?
)" )"
fi fi
if [ "$ping_result" -eq 0 ]; then if [ "$ping_result" -eq 0 ]; then
time_lastcheck_withinternet="$time_now" time_lastcheck_withinternet="$time_now"
else else
if [ "$iface" != "" ]; then if [ "$iface" != "" ]; then
logger -p daemon.info -t "watchcat[$$]" "Could not reach $host via \"$iface\" for \"$((time_now - time_lastcheck_withinternet))\" seconds. Restarting \"$iface\" after reaching \"$failure_period\" seconds" logger -p daemon.info -t "watchcat[$$]" "Could not reach $host via \"$iface\" for \"$((time_now - time_lastcheck_withinternet))\" seconds. Restarting \"$iface\" after reaching \"$failure_period\" seconds"
else else
logger -p daemon.info -t "watchcat[$$]" "Could not reach $host for \"$((time_now - time_lastcheck_withinternet))\" seconds. Restarting networking after reaching \"$failure_period\" seconds" logger -p daemon.info -t "watchcat[$$]" "Could not reach $host for \"$((time_now - time_lastcheck_withinternet))\" seconds. Restarting networking after reaching \"$failure_period\" seconds"
fi fi
fi fi
done done
[ "$((time_now - time_lastcheck_withinternet))" -ge "$failure_period" ] && { [ "$((time_now - time_lastcheck_withinternet))" -ge "$failure_period" ] && {
if [ "$mm_iface_name" != "" ]; then if [ "$mm_iface_name" != "" ]; then
watchcat_restart_modemmanager_iface "$mm_iface_name" "$mm_iface_unlock_bands" watchcat_restart_modemmanager_iface "$mm_iface_name" "$mm_iface_unlock_bands"
fi fi
if [ "$iface" != "" ]; then if [ "$iface" != "" ]; then
watchcat_restart_network_iface "$iface" watchcat_restart_network_iface "$iface"
else else
watchcat_restart_all_network watchcat_restart_all_network
fi fi
/etc/init.d/watchcat start /etc/init.d/watchcat start
} }
done done
} }
watchcat_ping() { watchcat_ping() {
failure_period="$1" failure_period="$1"
force_reboot_delay="$2" force_reboot_delay="$2"
ping_hosts="$3" ping_hosts="$3"
ping_frequency_interval="$4" ping_frequency_interval="$4"
ping_size="$5" ping_size="$5"
time_now="$(cat /proc/uptime)" time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
[ "$time_now" -lt "$failure_period" ] && sleep "$((failure_period - time_now))" [ "$time_now" -lt "$failure_period" ] && sleep "$((failure_period - time_now))"
time_now="$(cat /proc/uptime)" time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
time_lastcheck="$time_now" time_lastcheck="$time_now"
time_lastcheck_withinternet="$time_now" time_lastcheck_withinternet="$time_now"
ping_size="$(get_ping_size "$ping_size")" ping_size="$(get_ping_size "$ping_size")"
while true; do while true; 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="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
time_diff="$((time_now - time_lastcheck))" time_diff="$((time_now - time_lastcheck))"
[ "$time_diff" -lt "$ping_frequency_interval" ] && sleep "$((ping_frequency_interval - time_diff))" [ "$time_diff" -lt "$ping_frequency_interval" ] && sleep "$((ping_frequency_interval - time_diff))"
time_now="$(cat /proc/uptime)" time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
time_lastcheck="$time_now" time_lastcheck="$time_now"
for host in $ping_hosts; do for host in $ping_hosts; do
if [ "$iface" != "" ]; then if [ "$iface" != "" ]; then
ping_result="$( ping_result="$(
ping -I "$iface" -s "$ping_size" -c 1 "$host" &>/dev/null ping -I "$iface" -s "$ping_size" -c 1 "$host" &> /dev/null
echo $? echo $?
)" )"
else else
ping_result="$( ping_result="$(
ping -s "$ping_size" -c 1 "$host" &>/dev/null ping -s "$ping_size" -c 1 "$host" &> /dev/null
echo $? echo $?
)" )"
fi fi
if [ "$ping_result" -eq 0 ]; then if [ "$ping_result" -eq 0 ]; then
time_lastcheck_withinternet="$time_now" time_lastcheck_withinternet="$time_now"
else else
logger -p daemon.info -t "watchcat[$$]" "Could not reach $host for $((time_now - time_lastcheck_withinternet)). Rebooting after reaching $failure_period" logger -p daemon.info -t "watchcat[$$]" "Could not reach $host for $((time_now - time_lastcheck_withinternet)). Rebooting after reaching $failure_period"
fi fi
done done
[ "$((time_now - time_lastcheck_withinternet))" -ge "$failure_period" ] && reboot_now "$force_reboot_delay" [ "$((time_now - time_lastcheck_withinternet))" -ge "$failure_period" ] && reboot_now "$force_reboot_delay"
done done
} }
mode="$1" mode="$1"
@ -209,15 +209,15 @@ mode="$1"
case "$mode" in case "$mode" in
periodic_reboot) periodic_reboot)
watchcat_periodic "$2" "$3" watchcat_periodic "$2" "$3"
;; ;;
ping_reboot) ping_reboot)
watchcat_ping "$2" "$3" "$4" "$5" "$6" watchcat_ping "$2" "$3" "$4" "$5" "$6"
;; ;;
restart_iface) restart_iface)
watchcat_monitor_network "$2" "$3" "$4" "$5" "$6" "$7" watchcat_monitor_network "$2" "$3" "$4" "$5" "$6" "$7"
;; ;;
*) *)
echo "Error: invalid mode selected: $mode" echo "Error: invalid mode selected: $mode"
;; ;;
esac esac