|
1 | 1 | #!/bin/bash |
2 | 2 |
|
| 3 | +# Add any services to be watched by the watchdog to the SERVICE_LIST |
| 4 | +# Format of the service list: "Display Name" "Service Name" "semicolon delimited list of watchdog arguments" |
| 5 | +SERVICE_LIST=( |
| 6 | + "LiteSpeed" "lsws" "lsws;web;litespeed;openlitespeed" |
| 7 | + "MariaDB" "mariadb" "mariadb;database;mysql" |
| 8 | + "PowerDNS" "pdns" "powerdns;dns" |
| 9 | + "Dovecot" "dovecot" "dovecot;imap;pop3" |
| 10 | + "PostFix" "postfix" "postfix;smtp" |
| 11 | + "Pure-FTPd" "pure-ftpd" "pureftpd;pure-ftpd;ftp" |
| 12 | +) |
| 13 | + |
| 14 | +SERVICE_COUNT=$((${#SERVICE_LIST[@]}/3)) |
| 15 | + |
3 | 16 | show_help() { |
4 | | -echo -e "\nrun command: \e[31mnohup bash /etc/cyberpanel/watchdog.sh SERVICE_NAME >/dev/null 2>&1 &\e[39m" |
5 | | -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" |
6 | | -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" |
7 | | -echo -e "\nto exit watchdog , run command \e[31mbash /etc/cyberpanel/watchdog.sh kill\e[39m" |
8 | | -echo -e "\n\nplease also create \e[31m/etc/cyberpanel/watchdog.flag\e[39m file with following format:" |
9 | | -echo -e "TO=address@email.com" |
10 | | -echo -e "SENDER=sender name" |
11 | | -echo -e "FROM=sender@email.com" |
12 | | -echo -e "You may proceed without flag file , but that will make email sending failed." |
| 17 | + echo -e "\nrun command: \e[31mnohup bash /etc/cyberpanel/watchdog.sh SERVICE_NAME >/dev/null 2>&1 &\e[39m" |
| 18 | + echo -e "\nreplace \e[31mSERVICE_NAME\e[39m to the service name, acceptable word:" |
| 19 | + |
| 20 | + for ((x=0; x<SERVICE_COUNT; x++)) ; do |
| 21 | + IFS=';' read -ra SERVICE_ARGS <<< "${SERVICE_LIST[(x*3)+2]}" |
| 22 | + echo -e " \e[31m${SERVICE_ARGS[0]}\e[39m" |
| 23 | + done |
| 24 | + |
| 25 | + 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" |
| 26 | + echo -e "\nto exit watchdog , run command \e[31mbash /etc/cyberpanel/watchdog.sh kill\e[39m" |
| 27 | + echo -e "\n\nplease also create \e[31m/etc/cyberpanel/watchdog.flag\e[39m file with following format:" |
| 28 | + echo -e "TO=address@email.com" |
| 29 | + echo -e "SENDER=sender name" |
| 30 | + echo -e "FROM=sender@email.com" |
| 31 | + echo -e "You may proceed without flag file , but that will make email sending failed." |
13 | 32 | } |
14 | 33 |
|
15 | 34 | watchdog_check() { |
16 | | -echo -e "\nChecking LiteSpeed ..." |
17 | | -pid=$(ps aux | grep "watchdog lsws" | grep -v grep | awk '{print $2}') |
18 | | - if [[ "$pid" == "" ]] ; then |
19 | | - echo -e "\nWatchDog for LSWS is gone , restarting..." |
20 | | - nohup watchdog lsws > /dev/null 2>&1 & |
21 | | - echo -e "\nWatchDog for LSWS has been started..." |
22 | | - else |
23 | | - echo -e "\nWatchDog for LSWS is running...\n" |
24 | | - echo $(ps aux | grep "watchdog lsws" | grep -v grep) |
25 | | - fi |
26 | | - |
27 | | -echo -e "\nChecking MariaDB ..." |
28 | | -pid=$(ps aux | grep "watchdog mariadb" | grep -v grep | awk '{print $2}') |
| 35 | +for ((x=0; x<SERVICE_COUNT; x++)) ; do |
| 36 | + DISPLAY_NAME=${SERVICE_LIST[x*3]} |
| 37 | + SERVICE_NAME=${SERVICE_LIST[(x*3)+1]} |
| 38 | + IFS=';' read -ra SERVICE_ARGS <<< "${SERVICE_LIST[(x*3)+2]}" |
| 39 | + SERVICE_ARG=${SERVICE_ARGS[0]} |
| 40 | + |
| 41 | + echo -e "\nChecking ${DISPLAY_NAME}..." |
| 42 | + pid=$(ps aux | grep "watchdog ${SERVICE_ARG}" | grep -v grep | awk '{print $2}') |
29 | 43 | if [[ "$pid" == "" ]] ; then |
30 | | - echo -e "\nWatchDog for MariaDB is gone , restarting..." |
31 | | - nohup watchdog mariadb > /dev/null 2>&1 & |
32 | | - echo -e "\nWatchDog for MariaDB has been started..." |
| 44 | + echo -e "\nWatchDog for ${DISPLAY_NAME} is gone , restarting..." |
| 45 | + nohup watchdog ${SERVICE_ARG} > /dev/null 2>&1 & |
| 46 | + echo -e "\nWatchDog for ${DISPLAY_NAME} has been started..." |
33 | 47 | else |
34 | | - echo -e "\nWatchDog for MariaDB is running...\n" |
35 | | - echo $(ps aux | grep "watchdog mariadb" | grep -v grep) |
36 | | - fi |
37 | | - |
38 | | -echo -e "\nChecking PowerDNS ..." |
39 | | -pid=$(ps aux | grep "watchdog powerdns" | grep -v grep | awk '{print $2}') |
40 | | - if [[ "$pid" == "" ]] ; then |
41 | | - echo -e "\nWatchDog for PowerDNS is gone , restarting..." |
42 | | - nohup watchdog powerdns > /dev/null 2>&1 & |
43 | | - echo -e "\nWatchDog for PowerDNS has been started..." |
44 | | - else |
45 | | - echo -e "\nWatchDog for PowerDNS is running...\n" |
46 | | - echo $(ps aux | grep "watchdog powerdns" | grep -v grep) |
47 | | - fi |
48 | | - |
49 | | -echo -e "\nChecking Dovecot ..." |
50 | | -pid=$(ps aux | grep "watchdog dovecot" | grep -v grep | awk '{print $2}') |
51 | | - if [[ "$pid" == "" ]] ; then |
52 | | - echo -e "\nWatchDog for Dovecot is gone , restarting..." |
53 | | - nohup watchdog dovecot > /dev/null 2>&1 & |
54 | | - echo -e "\nWatchDog for Dovecot has been started..." |
55 | | - else |
56 | | - echo -e "\nWatchDog for Dovecot is running...\n" |
57 | | - echo $(ps aux | grep "watchdog dovecot" | grep -v grep) |
58 | | - fi |
59 | | - |
60 | | -echo -e "\nChecking Postfix ..." |
61 | | -pid=$(ps aux | grep "watchdog postfix" | grep -v grep | awk '{print $2}') |
62 | | - if [[ "$pid" == "" ]] ; then |
63 | | - echo -e "\nWatchDog for Postfix is gone , restarting..." |
64 | | - nohup watchdog postfix > /dev/null 2>&1 & |
65 | | - echo -e "\nWatchDog for Postfix has been started..." |
66 | | - else |
67 | | - echo -e "\nWatchDog for Postfix is running...\n" |
68 | | - echo $(ps aux | grep "watchdog postfix" | grep -v grep) |
69 | | - fi |
70 | | - |
71 | | -echo -e "\nChecking Pure-FTPd ..." |
72 | | -pid=$(ps aux | grep "watchdog pureftpd" | grep -v grep | awk '{print $2}') |
73 | | - if [[ "$pid" == "" ]] ; then |
74 | | - echo -e "\nWatchDog for Pure-FTPd is gone , restarting..." |
75 | | - nohup watchdog pureftpd > /dev/null 2>&1 & |
76 | | - echo -e "\nWatchDog for Pure-FTPd has been started..." |
77 | | - else |
78 | | - echo -e "\nWatchDog for Pure-FTPd is running...\n" |
79 | | - echo $(ps aux | grep "watchdog pureftpd" | grep -v grep) |
| 48 | + echo -e "\nWatchDog for ${DISPLAY_NAME} is running...\n" |
| 49 | + echo $(ps aux | grep "watchdog ${SERVICE_ARG}" | grep -v grep) |
80 | 50 | fi |
| 51 | +done |
81 | 52 | } |
82 | 53 |
|
83 | 54 | check_service() { |
@@ -130,62 +101,49 @@ MAIL_END |
130 | 101 | fi |
131 | 102 | } |
132 | 103 |
|
133 | | -if [[ $1 == "mariadb" ]] || [[ $1 == "database" ]] || [[ $1 == "mysql" ]] ; then |
134 | | - NAME="mariadb" |
135 | | - echo "Watchdog on MariaDB is started up ..." |
136 | | -elif [[ $1 == "web" ]] || [[ $1 == "lsws" ]] || [[ $1 == "litespeed" ]] || [[ $1 == "openlitespeed" ]] ; then |
137 | | - NAME="lsws" |
138 | | - echo "Watchdog on LiteSpeed is started up ..." |
139 | | -elif [[ $1 == "powerdns" ]] || [[ $1 == "dns" ]] ; then |
140 | | - NAME="pdns" |
141 | | - echo "Watchdog on PowerDNS is starting up ..." |
142 | | -elif [[ $1 == "dovecot" ]] || [[ $1 == "imap" ]] || [[ $1 == "pop3" ]]; then |
143 | | - NAME="dovecot" |
144 | | - echo "Watchdog on Dovecot is starting up ..." |
145 | | -elif [[ $1 == "postfix" ]] || [[ $1 == "smtp" ]] ; then |
146 | | - NAME="postfix" |
147 | | - echo "Watchdog on PostFix is starting up ..." |
148 | | -elif [[ $1 == "pureftpd" ]] || [[ $1 == "ftp" ]] || [[ $1 == "pure-ftpd" ]]; then |
149 | | - NAME="pure-ftpd" |
150 | | - echo "Watchdog on Pure-FTPd is starting up ..." |
151 | | -elif [[ $1 == "help" ]] || [[ $1 == "-h" ]] || [[ $1 == "--help" ]] || [[ $1 == "" ]] ; then |
| 104 | + |
| 105 | +if [[ $1 == "help" ]] || [[ $1 == "-h" ]] || [[ $1 == "--help" ]] || [[ $1 == "" ]] ; then |
152 | 106 | show_help |
153 | | -exit |
| 107 | + exit |
154 | 108 | elif [[ $1 == "check" ]] || [[ $1 == "status" ]] ; then |
155 | 109 | watchdog_check |
156 | 110 | exit |
157 | 111 | elif [[ $1 == "kill" ]] ; then |
158 | | -pid=$(ps aux | grep "watchdog lsws" | grep -v grep | awk '{print $2}') |
159 | | -if [[ "$pid" != "" ]] ; then |
160 | | -kill -15 $pid |
161 | | -fi |
162 | | -pid=$(ps aux | grep "watchdog mariadb" | grep -v grep | awk '{print $2}') |
163 | | -if [[ "$pid" != "" ]] ; then |
164 | | -kill -15 $pid |
165 | | -fi |
166 | | -pid=$(ps aux | grep "watchdog powerdns" | grep -v grep | awk '{print $2}') |
167 | | -if [[ "$pid" != "" ]] ; then |
168 | | -kill -15 $pid |
169 | | -fi |
170 | | -pid=$(ps aux | grep "watchdog dovecot" | grep -v grep | awk '{print $2}') |
171 | | -if [[ "$pid" != "" ]] ; then |
172 | | -kill -15 $pid |
173 | | -fi |
174 | | -pid=$(ps aux | grep "watchdog postfix" | grep -v grep | awk '{print $2}') |
175 | | -if [[ "$pid" != "" ]] ; then |
176 | | -kill -15 $pid |
177 | | -fi |
178 | | -pid=$(ps aux | grep "watchdog pureftpd" | grep -v grep | awk '{print $2}') |
179 | | -if [[ "$pid" != "" ]] ; then |
180 | | -kill -15 $pid |
| 112 | + for ((x=0; x<SERVICE_COUNT; x++)); do |
| 113 | + IFS=';' read -ra SERVICE_ARGS <<< "${SERVICE_LIST[(x*3)+2]}" |
| 114 | + SERVICE_ARG=${SERVICE_ARGS[0]} |
| 115 | + |
| 116 | + pid=$(ps aux | grep "watchdog ${SERVICE_ARG}" | grep -v grep | awk '{print $2}') |
| 117 | + if [[ "$pid" != "" ]] ; then |
| 118 | + kill -15 $pid |
| 119 | + fi |
| 120 | + done |
| 121 | + echo "watchdog has been killed..." |
| 122 | + exit |
181 | 123 | fi |
182 | | -echo "watchdog has been killed..." |
183 | | -exit |
184 | | -else |
185 | | -show_help |
186 | 124 |
|
187 | | -echo -e "\n\n\nunknown service name..." |
188 | | -exit |
| 125 | +# Check if $1 matches any service argument names |
| 126 | +SERVICE_FOUND=0 |
| 127 | +for ((x=0; x<SERVICE_COUNT; x++)) ; do |
| 128 | + DISPLAY_NAME=${SERVICE_LIST[x*3]} |
| 129 | + SERVICE_NAME=${SERVICE_LIST[(x*3)+1]} |
| 130 | + IFS=';' read -ra SERVICE_ARGS <<< "${SERVICE_LIST[(x*3)+2]}" |
| 131 | + SERVICE_ARG=${SERVICE_ARGS[0]} |
| 132 | + |
| 133 | + for arg in "${SERVICE_ARGS[@]}" ; do |
| 134 | + if [[ $1 == "$arg" ]] ; then |
| 135 | + SERVICE_FOUND=1 |
| 136 | + NAME=$SERVICE_NAME |
| 137 | + echo "Watchdog on ${DISPLAY_NAME} is starting up ..." |
| 138 | + fi |
| 139 | + done |
| 140 | +done |
| 141 | + |
| 142 | +if [[ $SERVICE_FOUND == 0 ]] ; then |
| 143 | + echo -e "unknown service name \e[31m$1\e[39m..." |
| 144 | + |
| 145 | + show_help |
| 146 | + exit |
189 | 147 | fi |
190 | 148 |
|
191 | 149 |
|
|
0 commit comments