mirror of
https://github.com/kenzok8/small-package
synced 2025-09-20 19:11:30 +08:00
update 2023-02-01 20:09:57
This commit is contained in:
@ -1,48 +0,0 @@
|
||||
#
|
||||
# Copyright (C) 2010 segal.di.ubi.pt
|
||||
#
|
||||
# This is free software, licensed under the GNU General Public License v2.
|
||||
# See /LICENSE for more information.
|
||||
#
|
||||
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
PKG_NAME:=watchcat
|
||||
PKG_VERSION:=1
|
||||
PKG_RELEASE:=17
|
||||
|
||||
PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
|
||||
PKG_LICENSE:=GPL-2.0
|
||||
|
||||
include $(INCLUDE_DIR)/package.mk
|
||||
|
||||
define Package/watchcat
|
||||
SECTION:=utils
|
||||
CATEGORY:=Utilities
|
||||
TITLE:=Enable the configuration of programmed reboots or network interface restarts
|
||||
PKGARCH:=all
|
||||
endef
|
||||
|
||||
define Package/watchcat/description
|
||||
Restart network interfaces or reboot if pings to hosts fail, or set up periodic reboots. Configured via UCI /etc/config/watchcat
|
||||
endef
|
||||
|
||||
define Package/watchcat/conffiles
|
||||
/etc/config/watchcat
|
||||
endef
|
||||
|
||||
define Build/Compile
|
||||
endef
|
||||
|
||||
define Package/watchcat/install
|
||||
$(INSTALL_DIR) $(1)/etc/init.d
|
||||
$(INSTALL_BIN) ./files/watchcat.init $(1)/etc/init.d/watchcat
|
||||
$(INSTALL_DIR) $(1)/usr/bin
|
||||
$(INSTALL_BIN) ./files/watchcat.sh $(1)/usr/bin/watchcat.sh
|
||||
$(INSTALL_DIR) $(1)/etc/config
|
||||
$(INSTALL_DATA) ./files/watchcat.config $(1)/etc/config/watchcat
|
||||
$(INSTALL_DIR) $(1)/etc/uci-defaults
|
||||
$(INSTALL_BIN) ./files/migrate-watchcat $(1)/etc/uci-defaults/migrate-watchcat
|
||||
endef
|
||||
|
||||
$(eval $(call BuildPackage,watchcat))
|
@ -1,27 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
. /lib/functions.sh
|
||||
|
||||
upgrade_watchcat() {
|
||||
local cfg="$1"
|
||||
|
||||
config_get period "$cfg" period
|
||||
config_get mode "$cfg" mode
|
||||
config_get pinghosts "$cfg" pinghosts
|
||||
config_get forcedelay "$cfg" forcedelay
|
||||
|
||||
[ -f "/etc/config/watchcat" ] || touch /etc/config/watchcat
|
||||
uci_add watchcat watchcat
|
||||
uci_set watchcat @watchcat[-1] period "$period"
|
||||
uci_set watchcat @watchcat[-1] mode "$mode"
|
||||
uci_set watchcat @watchcat[-1] pinghosts "$pinghosts"
|
||||
uci_set watchcat @watchcat[-1] forcedelay "$forcedelay"
|
||||
|
||||
uci_remove system "$cfg"
|
||||
}
|
||||
|
||||
config_load system
|
||||
config_foreach upgrade_watchcat watchcat
|
||||
|
||||
uci_commit watchcat
|
||||
uci commit system
|
@ -1,5 +0,0 @@
|
||||
config watchcat
|
||||
option period '6h'
|
||||
option mode 'ping_reboot'
|
||||
option pinghosts '8.8.8.8'
|
||||
option forcedelay '30'
|
@ -1,136 +0,0 @@
|
||||
#!/bin/sh /etc/rc.common
|
||||
|
||||
USE_PROCD=1
|
||||
|
||||
START=97
|
||||
STOP=01
|
||||
|
||||
append_string() {
|
||||
varname="$1"
|
||||
add="$2"
|
||||
separator="${3:- }"
|
||||
local actual
|
||||
eval "actual=\$$varname"
|
||||
|
||||
new="${actual:+$actual$separator}$add"
|
||||
eval "$varname=\$new"
|
||||
}
|
||||
|
||||
time_to_seconds() {
|
||||
time=$1
|
||||
|
||||
{ [ "$time" -ge 1 ] 2> /dev/null && seconds="$time"; } ||
|
||||
{ [ "${time%s}" -ge 1 ] 2> /dev/null && seconds="${time%s}"; } ||
|
||||
{ [ "${time%m}" -ge 1 ] 2> /dev/null && seconds=$((${time%m} * 60)); } ||
|
||||
{ [ "${time%h}" -ge 1 ] 2> /dev/null && seconds=$((${time%h} * 3600)); } ||
|
||||
{ [ "${time%d}" -ge 1 ] 2> /dev/null && seconds=$((${time%d} * 86400)); }
|
||||
|
||||
echo $seconds
|
||||
unset seconds
|
||||
unset time
|
||||
}
|
||||
|
||||
config_watchcat() {
|
||||
# Read config
|
||||
config_get period "$1" period "120"
|
||||
config_get mode "$1" mode "ping_reboot"
|
||||
config_get pinghosts "$1" pinghosts "8.8.8.8"
|
||||
config_get pingperiod "$1" pingperiod "60"
|
||||
config_get forcedelay "$1" forcedelay "60"
|
||||
config_get pingsize "$1" pingsize "standard"
|
||||
config_get interface "$1" interface
|
||||
config_get mmifacename "$1" mmifacename
|
||||
config_get_bool unlockbands "$1" unlockbands "0"
|
||||
config_get addressfamily "$1" addressfamily "any"
|
||||
config_get script "$1" script
|
||||
|
||||
# Fix potential typo in mode and provide backward compatibility.
|
||||
[ "$mode" = "allways" ] && mode="periodic_reboot"
|
||||
[ "$mode" = "always" ] && mode="periodic_reboot"
|
||||
[ "$mode" = "ping" ] && mode="ping_reboot"
|
||||
|
||||
# Checks for settings common to all operation modes
|
||||
if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ] && [ "$mode" != "run_script" ]; then
|
||||
append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface' or 'run_script'" "; "
|
||||
fi
|
||||
|
||||
period="$(time_to_seconds "$period")"
|
||||
[ "$period" -ge 1 ] ||
|
||||
append_string "error" "period has invalid format. Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
|
||||
|
||||
# ping_reboot mode and restart_iface mode specific checks
|
||||
if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ] || [ "$mode" = "run_script" ]; then
|
||||
if [ -z "$error" ]; then
|
||||
pingperiod_default="$((period / 5))"
|
||||
pingperiod="$(time_to_seconds "$pingperiod")"
|
||||
|
||||
if [ "$pingperiod" -ge 0 ] && [ "$pingperiod" -ge "$period" ]; then
|
||||
pingperiod="$(time_to_seconds "$pingperiod_default")"
|
||||
append_string "warn" "pingperiod cannot be greater than $period. Defaulted to $pingperiod_default seconds (1/5 of period)" "; "
|
||||
fi
|
||||
|
||||
if [ "$pingperiod" -lt 0 ]; then
|
||||
append_string "warn" "pingperiod cannot be a negative value." "; "
|
||||
fi
|
||||
|
||||
if [ "$mmifacename" != "" ] && [ "$period" -lt 30 ]; then
|
||||
append_string "error" "Check interval is less than 30s. For robust operation with ModemManager modem interfaces it is recommended to set the period to at least 30s."
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$mode" = "run_script" ] && [ -z "$script" ]; then
|
||||
append_string "error" "run_script mode requires a script"
|
||||
fi
|
||||
|
||||
# ping_reboot mode and periodic_reboot mode specific checks
|
||||
if [ "$mode" = "ping_reboot" ] || [ "$mode" = "periodic_reboot" ]; then
|
||||
forcedelay="$(time_to_seconds "$forcedelay")"
|
||||
fi
|
||||
|
||||
[ -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
|
||||
}
|
||||
|
||||
# Need to conditionally run mode functions because they have different signatures
|
||||
case "$mode" in
|
||||
periodic_reboot)
|
||||
procd_open_instance "watchcat_${1}"
|
||||
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_close_instance
|
||||
;;
|
||||
ping_reboot)
|
||||
procd_open_instance "watchcat_${1}"
|
||||
procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize" "$addressfamily"
|
||||
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
|
||||
procd_close_instance
|
||||
;;
|
||||
restart_iface)
|
||||
procd_open_instance "watchcat_${1}"
|
||||
procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename" "$unlockbands" "$addressfamily"
|
||||
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
|
||||
procd_close_instance
|
||||
;;
|
||||
run_script)
|
||||
procd_open_instance "watchcat_${1}"
|
||||
procd_set_param command /usr/bin/watchcat.sh "run_script" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$addressfamily" "$script"
|
||||
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
|
||||
procd_close_instance
|
||||
;;
|
||||
*)
|
||||
echo "Error starting Watchcat service. Invalid mode selection: $mode"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
start_service() {
|
||||
config_load watchcat
|
||||
config_foreach config_watchcat watchcat
|
||||
}
|
||||
|
||||
service_triggers() {
|
||||
procd_add_reload_trigger "watchcat"
|
||||
}
|
@ -1,269 +0,0 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# Copyright (C) 2010 segal.di.ubi.pt
|
||||
# Copyright (C) 2020 nbembedded.com
|
||||
#
|
||||
# This is free software, licensed under the GNU General Public License v2.
|
||||
#
|
||||
|
||||
get_ping_size() {
|
||||
ps=$1
|
||||
case "$ps" in
|
||||
small)
|
||||
ps="1"
|
||||
;;
|
||||
windows)
|
||||
ps="32"
|
||||
;;
|
||||
standard)
|
||||
ps="56"
|
||||
;;
|
||||
big)
|
||||
ps="248"
|
||||
;;
|
||||
huge)
|
||||
ps="1492"
|
||||
;;
|
||||
jumbo)
|
||||
ps="9000"
|
||||
;;
|
||||
*)
|
||||
echo "Error: invalid ping_size. ping_size should be either: small, windows, standard, big, huge or jumbo" 1>&2
|
||||
echo "Corresponding ping packet sizes (bytes): small=1, windows=32, standard=56, big=248, huge=1492, jumbo=9000" 1>&2
|
||||
;;
|
||||
esac
|
||||
echo $ps
|
||||
}
|
||||
|
||||
get_ping_family_flag() {
|
||||
family=$1
|
||||
case "$family" in
|
||||
any)
|
||||
family=""
|
||||
;;
|
||||
ipv4)
|
||||
family="-4"
|
||||
;;
|
||||
ipv6)
|
||||
family="-6"
|
||||
;;
|
||||
*)
|
||||
echo "Error: invalid address_family \"$family\". address_family should be one of: any, ipv4, ipv6" 1>&2
|
||||
;;
|
||||
esac
|
||||
echo $family
|
||||
}
|
||||
|
||||
reboot_now() {
|
||||
reboot &
|
||||
|
||||
[ "$1" -ge 1 ] && {
|
||||
sleep "$1"
|
||||
echo 1 > /proc/sys/kernel/sysrq
|
||||
echo b > /proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
|
||||
}
|
||||
}
|
||||
|
||||
watchcat_periodic() {
|
||||
failure_period="$1"
|
||||
force_reboot_delay="$2"
|
||||
|
||||
sleep "$failure_period" && reboot_now "$force_reboot_delay"
|
||||
}
|
||||
|
||||
watchcat_restart_modemmanager_iface() {
|
||||
[ "$2" -gt 0 ] && {
|
||||
logger -p daemon.info -t "watchcat[$$]" "Resetting current-bands to 'any' on modem: \"$1\" now."
|
||||
/usr/bin/mmcli -m any --set-current-bands=any
|
||||
}
|
||||
logger -p daemon.info -t "watchcat[$$]" "Reconnecting modem: \"$1\" now."
|
||||
/etc/init.d/modemmanager restart
|
||||
ifup "$1"
|
||||
}
|
||||
|
||||
watchcat_restart_network_iface() {
|
||||
logger -p daemon.info -t "watchcat[$$]" "Restarting network interface: \"$1\"."
|
||||
ip link set "$1" down
|
||||
ip link set "$1" up
|
||||
}
|
||||
|
||||
watchcat_run_script() {
|
||||
logger -p daemon.info -t "watchcat[$$]" "Running script \"$1\" for network interface: \"$2\"."
|
||||
"$1" "$2"
|
||||
}
|
||||
|
||||
watchcat_restart_all_network() {
|
||||
logger -p daemon.info -t "watchcat[$$]" "Restarting networking now by running: /etc/init.d/network restart"
|
||||
/etc/init.d/network restart
|
||||
}
|
||||
|
||||
watchcat_monitor_network() {
|
||||
failure_period="$1"
|
||||
ping_hosts="$2"
|
||||
ping_frequency_interval="$3"
|
||||
ping_size="$4"
|
||||
iface="$5"
|
||||
mm_iface_name="$6"
|
||||
mm_iface_unlock_bands="$7"
|
||||
address_family="$8"
|
||||
script="$9"
|
||||
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
|
||||
[ "$time_now" -lt "$failure_period" ] && sleep "$((failure_period - time_now))"
|
||||
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_lastcheck="$time_now"
|
||||
time_lastcheck_withinternet="$time_now"
|
||||
|
||||
ping_size="$(get_ping_size "$ping_size")"
|
||||
|
||||
ping_family="$(get_ping_family_flag "$address_family")"
|
||||
|
||||
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.
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_diff="$((time_now - time_lastcheck))"
|
||||
|
||||
[ "$time_diff" -lt "$ping_frequency_interval" ] && sleep "$((ping_frequency_interval - time_diff))"
|
||||
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_lastcheck="$time_now"
|
||||
|
||||
for host in $ping_hosts; do
|
||||
if [ "$iface" != "" ]; then
|
||||
ping_result="$(
|
||||
ping $ping_family -I "$iface" -s "$ping_size" -c 1 "$host" &> /dev/null
|
||||
echo $?
|
||||
)"
|
||||
else
|
||||
ping_result="$(
|
||||
ping $ping_family -s "$ping_size" -c 1 "$host" &> /dev/null
|
||||
echo $?
|
||||
)"
|
||||
fi
|
||||
|
||||
if [ "$ping_result" -eq 0 ]; then
|
||||
time_lastcheck_withinternet="$time_now"
|
||||
else
|
||||
if [ "$script" != "" ]; then
|
||||
logger -p daemon.info -t "watchcat[$$]" "Could not reach $host via \"$iface\" for \"$((time_now - time_lastcheck_withinternet))\" seconds. Running script after reaching \"$failure_period\" seconds"
|
||||
elif [ "$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"
|
||||
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"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
[ "$((time_now - time_lastcheck_withinternet))" -ge "$failure_period" ] && {
|
||||
if [ "$script" != "" ]; then
|
||||
watchcat_run_script "$script" "$iface"
|
||||
else
|
||||
if [ "$mm_iface_name" != "" ]; then
|
||||
watchcat_restart_modemmanager_iface "$mm_iface_name" "$mm_iface_unlock_bands"
|
||||
fi
|
||||
if [ "$iface" != "" ]; then
|
||||
watchcat_restart_network_iface "$iface"
|
||||
else
|
||||
watchcat_restart_all_network
|
||||
fi
|
||||
fi
|
||||
/etc/init.d/watchcat start
|
||||
# Restart timer cycle.
|
||||
time_lastcheck_withinternet="$time_now"
|
||||
}
|
||||
|
||||
done
|
||||
}
|
||||
|
||||
watchcat_ping() {
|
||||
failure_period="$1"
|
||||
force_reboot_delay="$2"
|
||||
ping_hosts="$3"
|
||||
ping_frequency_interval="$4"
|
||||
ping_size="$5"
|
||||
address_family="$6"
|
||||
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
|
||||
[ "$time_now" -lt "$failure_period" ] && sleep "$((failure_period - time_now))"
|
||||
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_lastcheck="$time_now"
|
||||
time_lastcheck_withinternet="$time_now"
|
||||
|
||||
ping_size="$(get_ping_size "$ping_size")"
|
||||
|
||||
ping_family="$(get_ping_family_flag "$address_family")"
|
||||
|
||||
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.
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_diff="$((time_now - time_lastcheck))"
|
||||
|
||||
[ "$time_diff" -lt "$ping_frequency_interval" ] && sleep "$((ping_frequency_interval - time_diff))"
|
||||
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_lastcheck="$time_now"
|
||||
|
||||
for host in $ping_hosts; do
|
||||
if [ "$iface" != "" ]; then
|
||||
ping_result="$(
|
||||
ping $ping_family -I "$iface" -s "$ping_size" -c 1 "$host" &> /dev/null
|
||||
echo $?
|
||||
)"
|
||||
else
|
||||
ping_result="$(
|
||||
ping $ping_family -s "$ping_size" -c 1 "$host" &> /dev/null
|
||||
echo $?
|
||||
)"
|
||||
fi
|
||||
|
||||
if [ "$ping_result" -eq 0 ]; then
|
||||
time_lastcheck_withinternet="$time_now"
|
||||
else
|
||||
logger -p daemon.info -t "watchcat[$$]" "Could not reach $host for $((time_now - time_lastcheck_withinternet)). Rebooting after reaching $failure_period"
|
||||
fi
|
||||
done
|
||||
|
||||
[ "$((time_now - time_lastcheck_withinternet))" -ge "$failure_period" ] && reboot_now "$force_reboot_delay"
|
||||
done
|
||||
}
|
||||
|
||||
mode="$1"
|
||||
|
||||
# Fix potential typo in mode and provide backward compatibility.
|
||||
[ "$mode" = "allways" ] && mode="periodic_reboot"
|
||||
[ "$mode" = "always" ] && mode="periodic_reboot"
|
||||
[ "$mode" = "ping" ] && mode="ping_reboot"
|
||||
|
||||
case "$mode" in
|
||||
periodic_reboot)
|
||||
# args from init script: period forcedelay
|
||||
watchcat_periodic "$2" "$3"
|
||||
;;
|
||||
ping_reboot)
|
||||
# args from init script: period forcedelay pinghosts pingperiod pingsize addressfamily
|
||||
watchcat_ping "$2" "$3" "$4" "$5" "$6" "$7"
|
||||
;;
|
||||
restart_iface)
|
||||
# args from init script: period pinghosts pingperiod pingsize interface mmifacename unlockbands addressfamily
|
||||
watchcat_monitor_network "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" ""
|
||||
;;
|
||||
run_script)
|
||||
# args from init script: period pinghosts pingperiod pingsize interface addressfamily script
|
||||
watchcat_monitor_network "$2" "$3" "$4" "$5" "$6" "" "" "$7" "$8"
|
||||
;;
|
||||
*)
|
||||
echo "Error: invalid mode selected: $mode"
|
||||
;;
|
||||
esac
|
Reference in New Issue
Block a user