watchcat: add support to run a script
authorJohn Kohl <jtk.git@bostonpog.org>
Sun, 5 Jun 2022 00:56:56 +0000 (20:56 -0400)
committerRosen Penev <rosenp@gmail.com>
Fri, 15 Jul 2022 19:14:06 +0000 (12:14 -0700)
Signed-off-by: John Kohl <jtk.git@bostonpog.org>
utils/watchcat/Makefile
utils/watchcat/files/watchcat.init
utils/watchcat/files/watchcat.sh

index da5e70ace90a9a58f19fa97d735e30187997c50d..8bdf864a43385c0de914945d89ec0b9392fd18ed 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=watchcat
 PKG_VERSION:=1
-PKG_RELEASE:=15
+PKG_RELEASE:=16
 
 PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
 PKG_LICENSE:=GPL-2.0
index ba047f31fd97365104d7dfbdaa0fe1e4a0b2faee..688b44bb8cf444f70eda6900282119601711be88 100644 (file)
@@ -42,6 +42,7 @@ config_watchcat() {
        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"
@@ -49,8 +50,8 @@ config_watchcat() {
        [ "$mode" = "ping" ] && mode="ping_reboot"
 
        # Checks for settings common to all operation modes
-       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'" "; "
+       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")"
@@ -58,7 +59,7 @@ config_watchcat() {
                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" ]; then
+       if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ] || [ "$mode" = "run_script" ]; then
                if [ -z "$error" ]; then
                        pingperiod_default="$((period / 5))"
                        pingperiod="$(time_to_seconds "$pingperiod")"
@@ -78,6 +79,10 @@ config_watchcat() {
                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")"
@@ -109,6 +114,12 @@ config_watchcat() {
                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"
                ;;
index 9999defa829cb7e3d517d6f6d6552d57ca0ca05f..dd0e9b27fc8e209ea677ff5170838098d8dd01f2 100644 (file)
@@ -87,6 +87,11 @@ watchcat_restart_network_iface() {
        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
@@ -101,6 +106,7 @@ watchcat_monitor_network() {
        mm_iface_name="$6"
        mm_iface_unlock_bands="$7"
        address_family="$8"
+       script="$9"
 
        time_now="$(cat /proc/uptime)"
        time_now="${time_now%%.*}"
@@ -144,7 +150,9 @@ watchcat_monitor_network() {
                        if [ "$ping_result" -eq 0 ]; then
                                time_lastcheck_withinternet="$time_now"
                        else
-                               if [ "$iface" != "" ]; then
+                               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"
@@ -153,13 +161,17 @@ watchcat_monitor_network() {
                done
 
                [ "$((time_now - time_lastcheck_withinternet))" -ge "$failure_period" ] && {
-                       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"
+                       if [ "$script" != "" ]; then
+                               watchcat_run_script "$script" "$iface"
                        else
-                               watchcat_restart_all_network
+                               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.
@@ -245,7 +257,11 @@ ping_reboot)
        ;;
 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"
+       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"