From a1d51af6569587641140ca30a9a46907dc6b7134 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 2 Aug 2021 15:18:03 +1000 Subject: [PATCH] Refactored watchdog to be easier to add additional services. --- CPScripts/watchdog.sh | 198 +++++++++++++++++------------------------- 1 file changed, 78 insertions(+), 120 deletions(-) diff --git a/CPScripts/watchdog.sh b/CPScripts/watchdog.sh index 0e01a8c39..b0a9aa129 100644 --- a/CPScripts/watchdog.sh +++ b/CPScripts/watchdog.sh @@ -1,83 +1,54 @@ #!/bin/bash +# Add any services to be watched by the watchdog to the SERVICE_LIST +# Format of the service list: "Display Name" "Service Name" "semicolon delimited list of watchdog arguments" +SERVICE_LIST=( + "LiteSpeed" "lsws" "lsws;web;litespeed;openlitespeed" + "MariaDB" "mariadb" "mariadb;database;mysql" + "PowerDNS" "pdns" "powerdns;dns" + "Dovecot" "dovecot" "dovecot;imap;pop3" + "PostFix" "postfix" "postfix;smtp" + "Pure-FTPd" "pure-ftpd" "pureftpd;pure-ftpd;ftp" +) + +SERVICE_COUNT=$((${#SERVICE_LIST[@]}/3)) + show_help() { -echo -e "\nrun command: \e[31mnohup bash /etc/cyberpanel/watchdog.sh SERVICE_NAME >/dev/null 2>&1 &\e[39m" -echo -e "\nreplace \e[31mSERVICE_NAME\e[39m to the service name, acceptable word: \e[31mmariadb\e[39m, \e[31mlsws\e[39m, \e[31mpowerdns\e[39m, \e[31mdovecot\e[39m, \e[31mpostfix\e[39m, or \e[31mpureftpd\e[39m" -echo -e "\nWatchdog will check service status every 60 seconds and tries to restart if it is not running and also send an email to designated address" -echo -e "\nto exit watchdog , run command \e[31mbash /etc/cyberpanel/watchdog.sh kill\e[39m" -echo -e "\n\nplease also create \e[31m/etc/cyberpanel/watchdog.flag\e[39m file with following format:" -echo -e "TO=address@email.com" -echo -e "SENDER=sender name" -echo -e "FROM=sender@email.com" -echo -e "You may proceed without flag file , but that will make email sending failed." + echo -e "\nrun command: \e[31mnohup bash /etc/cyberpanel/watchdog.sh SERVICE_NAME >/dev/null 2>&1 &\e[39m" + echo -e "\nreplace \e[31mSERVICE_NAME\e[39m to the service name, acceptable word:" + + for ((x=0; x /dev/null 2>&1 & - echo -e "\nWatchDog for LSWS has been started..." + echo -e "\nWatchDog for ${DISPLAY_NAME} is gone , restarting..." + nohup watchdog ${SERVICE_ARG} > /dev/null 2>&1 & + echo -e "\nWatchDog for ${DISPLAY_NAME} has been started..." else - echo -e "\nWatchDog for LSWS is running...\n" - echo $(ps aux | grep "watchdog lsws" | grep -v grep) - fi - -echo -e "\nChecking MariaDB ..." -pid=$(ps aux | grep "watchdog mariadb" | grep -v grep | awk '{print $2}') - if [[ "$pid" == "" ]] ; then - echo -e "\nWatchDog for MariaDB is gone , restarting..." - nohup watchdog mariadb > /dev/null 2>&1 & - echo -e "\nWatchDog for MariaDB has been started..." - else - echo -e "\nWatchDog for MariaDB is running...\n" - echo $(ps aux | grep "watchdog mariadb" | grep -v grep) - fi - -echo -e "\nChecking PowerDNS ..." -pid=$(ps aux | grep "watchdog powerdns" | grep -v grep | awk '{print $2}') - if [[ "$pid" == "" ]] ; then - echo -e "\nWatchDog for PowerDNS is gone , restarting..." - nohup watchdog powerdns > /dev/null 2>&1 & - echo -e "\nWatchDog for PowerDNS has been started..." - else - echo -e "\nWatchDog for PowerDNS is running...\n" - echo $(ps aux | grep "watchdog powerdns" | grep -v grep) - fi - -echo -e "\nChecking Dovecot ..." -pid=$(ps aux | grep "watchdog dovecot" | grep -v grep | awk '{print $2}') - if [[ "$pid" == "" ]] ; then - echo -e "\nWatchDog for Dovecot is gone , restarting..." - nohup watchdog dovecot > /dev/null 2>&1 & - echo -e "\nWatchDog for Dovecot has been started..." - else - echo -e "\nWatchDog for Dovecot is running...\n" - echo $(ps aux | grep "watchdog dovecot" | grep -v grep) - fi - -echo -e "\nChecking Postfix ..." -pid=$(ps aux | grep "watchdog postfix" | grep -v grep | awk '{print $2}') - if [[ "$pid" == "" ]] ; then - echo -e "\nWatchDog for Postfix is gone , restarting..." - nohup watchdog postfix > /dev/null 2>&1 & - echo -e "\nWatchDog for Postfix has been started..." - else - echo -e "\nWatchDog for Postfix is running...\n" - echo $(ps aux | grep "watchdog postfix" | grep -v grep) - fi - -echo -e "\nChecking Pure-FTPd ..." -pid=$(ps aux | grep "watchdog pureftpd" | grep -v grep | awk '{print $2}') - if [[ "$pid" == "" ]] ; then - echo -e "\nWatchDog for Pure-FTPd is gone , restarting..." - nohup watchdog pureftpd > /dev/null 2>&1 & - echo -e "\nWatchDog for Pure-FTPd has been started..." - else - echo -e "\nWatchDog for Pure-FTPd is running...\n" - echo $(ps aux | grep "watchdog pureftpd" | grep -v grep) + echo -e "\nWatchDog for ${DISPLAY_NAME} is running...\n" + echo $(ps aux | grep "watchdog ${SERVICE_ARG}" | grep -v grep) fi +done } check_service() { @@ -130,62 +101,49 @@ MAIL_END fi } -if [[ $1 == "mariadb" ]] || [[ $1 == "database" ]] || [[ $1 == "mysql" ]] ; then - NAME="mariadb" - echo "Watchdog on MariaDB is started up ..." -elif [[ $1 == "web" ]] || [[ $1 == "lsws" ]] || [[ $1 == "litespeed" ]] || [[ $1 == "openlitespeed" ]] ; then - NAME="lsws" - echo "Watchdog on LiteSpeed is started up ..." -elif [[ $1 == "powerdns" ]] || [[ $1 == "dns" ]] ; then - NAME="pdns" - echo "Watchdog on PowerDNS is starting up ..." -elif [[ $1 == "dovecot" ]] || [[ $1 == "imap" ]] || [[ $1 == "pop3" ]]; then - NAME="dovecot" - echo "Watchdog on Dovecot is starting up ..." -elif [[ $1 == "postfix" ]] || [[ $1 == "smtp" ]] ; then - NAME="postfix" - echo "Watchdog on PostFix is starting up ..." -elif [[ $1 == "pureftpd" ]] || [[ $1 == "ftp" ]] || [[ $1 == "pure-ftpd" ]]; then - NAME="pure-ftpd" - echo "Watchdog on Pure-FTPd is starting up ..." -elif [[ $1 == "help" ]] || [[ $1 == "-h" ]] || [[ $1 == "--help" ]] || [[ $1 == "" ]] ; then + +if [[ $1 == "help" ]] || [[ $1 == "-h" ]] || [[ $1 == "--help" ]] || [[ $1 == "" ]] ; then show_help -exit + exit elif [[ $1 == "check" ]] || [[ $1 == "status" ]] ; then watchdog_check exit elif [[ $1 == "kill" ]] ; then -pid=$(ps aux | grep "watchdog lsws" | grep -v grep | awk '{print $2}') -if [[ "$pid" != "" ]] ; then -kill -15 $pid + for ((x=0; x