blob: cb342c1380a2e6313822bd18cb2af9cf31f9d8fb [file] [log] [blame]
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001#!/bin/bash
2
3trap "exit" INT TERM
4trap "kill 0" EXIT
5
6# Prepare
7BACKGROUND_TASKS=()
8echo "Waiting for containers to settle..."
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01009for i in {30..1}; do
10 echo "${i}"
11 sleep 1
12done
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010013
14if [[ "${USE_WATCHDOG}" =~ ^([nN][oO]|[nN])+$ ]]; then
15 echo -e "$(date) - USE_WATCHDOG=n, skipping watchdog..."
16 sleep 365d
17 exec $(readlink -f "$0")
18fi
19
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010020if [[ "${WATCHDOG_VERBOSE}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
21 SMTP_VERBOSE="--verbose"
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +010022 CURL_VERBOSE="--verbose"
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010023 set -xv
24else
25 SMTP_VERBOSE=""
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +010026 CURL_VERBOSE=""
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010027 exec 2>/dev/null
28fi
29
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010030# Checks pipe their corresponding container name in this pipe
31if [[ ! -p /tmp/com_pipe ]]; then
32 mkfifo /tmp/com_pipe
33fi
34
35# Wait for containers
36while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent; do
37 echo "Waiting for SQL..."
38 sleep 2
39done
40
41# Do not attempt to write to slave
42if [[ ! -z ${REDIS_SLAVEOF_IP} ]]; then
43 REDIS_CMDLINE="redis-cli -h ${REDIS_SLAVEOF_IP} -p ${REDIS_SLAVEOF_PORT}"
44else
45 REDIS_CMDLINE="redis-cli -h redis -p 6379"
46fi
47
48until [[ $(${REDIS_CMDLINE} PING) == "PONG" ]]; do
49 echo "Waiting for Redis..."
50 sleep 2
51done
52
53${REDIS_CMDLINE} DEL F2B_RES > /dev/null
54
55# Common functions
56get_ipv6(){
57 local IPV6=
58 local IPV6_SRCS=
59 local TRY=
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020060 IPV6_SRCS[0]="ip6.mailcow.email"
61 IPV6_SRCS[1]="ip6.nevondo.com"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010062 until [[ ! -z ${IPV6} ]] || [[ ${TRY} -ge 10 ]]; do
63 IPV6=$(curl --connect-timeout 3 -m 10 -L6s ${IPV6_SRCS[$RANDOM % ${#IPV6_SRCS[@]} ]} | grep "^\([0-9a-fA-F]\{0,4\}:\)\{1,7\}[0-9a-fA-F]\{0,4\}$")
64 [[ ! -z ${TRY} ]] && sleep 1
65 TRY=$((TRY+1))
66 done
67 echo ${IPV6}
68}
69
70array_diff() {
71 # https://stackoverflow.com/questions/2312762, Alex Offshore
72 eval local ARR1=\(\"\${$2[@]}\"\)
73 eval local ARR2=\(\"\${$3[@]}\"\)
74 local IFS=$'\n'
75 mapfile -t $1 < <(comm -23 <(echo "${ARR1[*]}" | sort) <(echo "${ARR2[*]}" | sort))
76}
77
78progress() {
79 SERVICE=${1}
80 TOTAL=${2}
81 CURRENT=${3}
82 DIFF=${4}
83 [[ -z ${DIFF} ]] && DIFF=0
84 [[ -z ${TOTAL} || -z ${CURRENT} ]] && return
85 [[ ${CURRENT} -gt ${TOTAL} ]] && return
86 [[ ${CURRENT} -lt 0 ]] && CURRENT=0
87 PERCENT=$(( 200 * ${CURRENT} / ${TOTAL} % 2 + 100 * ${CURRENT} / ${TOTAL} ))
88 ${REDIS_CMDLINE} LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"service\":\"${SERVICE}\",\"lvl\":\"${PERCENT}\",\"hpnow\":\"${CURRENT}\",\"hptotal\":\"${TOTAL}\",\"hpdiff\":\"${DIFF}\"}" > /dev/null
89 log_msg "${SERVICE} health level: ${PERCENT}% (${CURRENT}/${TOTAL}), health trend: ${DIFF}" no_redis
90 # Return 10 to indicate a dead service
91 [ ${CURRENT} -le 0 ] && return 10
92}
93
94log_msg() {
95 if [[ ${2} != "no_redis" ]]; then
96 ${REDIS_CMDLINE} LPUSH WATCHDOG_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${1}" | \
97 tr '\r\n%&;$"_[]{}-' ' ')\"}" > /dev/null
98 fi
99 echo $(date) $(printf '%s\n' "${1}")
100}
101
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100102function notify_error() {
103 # Check if one of the notification options is enabled
104 [[ -z ${WATCHDOG_NOTIFY_EMAIL} ]] && [[ -z ${WATCHDOG_NOTIFY_WEBHOOK} ]] && return 0
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100105 THROTTLE=
106 [[ -z ${1} ]] && return 1
107 # If exists, body will be the content of "/tmp/${1}", even if ${2} is set
108 [[ -z ${2} ]] && BODY="Service was restarted on $(date), please check your mailcow installation." || BODY="$(date) - ${2}"
109 # If exists, mail will be throttled by argument in seconds
110 [[ ! -z ${3} ]] && THROTTLE=${3}
111 if [[ ! -z ${THROTTLE} ]]; then
112 TTL_LEFT="$(${REDIS_CMDLINE} TTL THROTTLE_${1} 2> /dev/null)"
113 if [[ "${TTL_LEFT}" == "-2" ]]; then
114 # Delay key not found, setting a delay key now
115 ${REDIS_CMDLINE} SET THROTTLE_${1} 1 EX ${THROTTLE}
116 else
117 log_msg "Not sending notification email now, blocked for ${TTL_LEFT} seconds..."
118 return 1
119 fi
120 fi
121 WATCHDOG_NOTIFY_EMAIL=$(echo "${WATCHDOG_NOTIFY_EMAIL}" | sed 's/"//;s|"$||')
122 # Some exceptions for subject and body formats
123 if [[ ${1} == "fail2ban" ]]; then
124 SUBJECT="${BODY}"
125 BODY="Please see netfilter-mailcow for more details and triggered rules."
126 else
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200127 SUBJECT="${WATCHDOG_SUBJECT}: ${1}"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100128 fi
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100129
130 # Send mail notification if enabled
131 if [[ ! -z ${WATCHDOG_NOTIFY_EMAIL} ]]; then
132 IFS=',' read -r -a MAIL_RCPTS <<< "${WATCHDOG_NOTIFY_EMAIL}"
133 for rcpt in "${MAIL_RCPTS[@]}"; do
134 RCPT_DOMAIN=
135 RCPT_MX=
136 RCPT_DOMAIN=$(echo ${rcpt} | awk -F @ {'print $NF'})
137 CHECK_FOR_VALID_MX=$(dig +short ${RCPT_DOMAIN} mx)
138 if [[ -z ${CHECK_FOR_VALID_MX} ]]; then
139 log_msg "Cannot determine MX for ${rcpt}, skipping email notification..."
140 return 1
141 fi
142 [ -f "/tmp/${1}" ] && BODY="/tmp/${1}"
143 timeout 10s ./smtp-cli --missing-modules-ok \
144 "${SMTP_VERBOSE}" \
145 --charset=UTF-8 \
146 --subject="${SUBJECT}" \
147 --body-plain="${BODY}" \
148 --add-header="X-Priority: 1" \
149 --to=${rcpt} \
150 --from="watchdog@${MAILCOW_HOSTNAME}" \
151 --hello-host=${MAILCOW_HOSTNAME} \
152 --ipv4
153 if [[ $? -eq 1 ]]; then # exit code 1 is fine
154 log_msg "Sent notification email to ${rcpt}"
155 else
156 if [[ "${SMTP_VERBOSE}" == "" ]]; then
157 log_msg "Error while sending notification email to ${rcpt}. You can enable verbose logging by setting 'WATCHDOG_VERBOSE=y' in mailcow.conf."
158 else
159 log_msg "Error while sending notification email to ${rcpt}."
160 fi
161 fi
162 done
163 fi
164
165 # Send webhook notification if enabled
166 if [[ ! -z ${WATCHDOG_NOTIFY_WEBHOOK} ]]; then
167 if [[ -z ${WATCHDOG_NOTIFY_WEBHOOK_BODY} ]]; then
168 log_msg "No webhook body set, skipping webhook notification..."
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100169 return 1
170 fi
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100171
172 # Replace subject and body placeholders
173 WEBHOOK_BODY=$(echo ${WATCHDOG_NOTIFY_WEBHOOK_BODY} | sed "s/\$SUBJECT\|\${SUBJECT}/$SUBJECT/g" | sed "s/\$BODY\|\${BODY}/$BODY/g")
174
175 # POST to webhook
176 curl -X POST -H "Content-Type: application/json" ${CURL_VERBOSE} -d "${WEBHOOK_BODY}" ${WATCHDOG_NOTIFY_WEBHOOK}
177
178 log_msg "Sent notification using webhook"
179 fi
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100180}
181
182get_container_ip() {
183 # ${1} is container
184 CONTAINER_ID=()
185 CONTAINER_IPS=()
186 CONTAINER_IP=
187 LOOP_C=1
188 until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
189 if [ ${IP_BY_DOCKER_API} -eq 0 ]; then
190 CONTAINER_IP=$(dig a "${1}" +short)
191 else
192 sleep 0.5
193 # get long container id for exact match
194 CONTAINER_ID=($(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], project: .Config.Labels[\"com.docker.compose.project\"], id: .Id}" | jq -rc "select( .name | tostring == \"${1}\") | select( .project | tostring | contains(\"${COMPOSE_PROJECT_NAME,,}\")) | .id"))
195 # returned id can have multiple elements (if scaled), shuffle for random test
196 CONTAINER_ID=($(printf "%s\n" "${CONTAINER_ID[@]}" | shuf))
197 if [[ ! -z ${CONTAINER_ID} ]]; then
198 for matched_container in "${CONTAINER_ID[@]}"; do
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100199 CONTAINER_IPS=($(curl --silent --insecure https://dockerapi/containers/${matched_container}/json | jq -r '.NetworkSettings.Networks[].IPAddress'))
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100200 for ip_match in "${CONTAINER_IPS[@]}"; do
201 # grep will do nothing if one of these vars is empty
202 [[ -z ${ip_match} ]] && continue
203 [[ -z ${IPV4_NETWORK} ]] && continue
204 # only return ips that are part of our network
205 if ! grep -q ${IPV4_NETWORK} <(echo ${ip_match}); then
206 continue
207 else
208 CONTAINER_IP=${ip_match}
209 break
210 fi
211 done
212 [[ ! -z ${CONTAINER_IP} ]] && break
213 done
214 fi
215 fi
216 LOOP_C=$((LOOP_C + 1))
217 done
218 [[ ${LOOP_C} -gt 5 ]] && echo 240.0.0.0 || echo ${CONTAINER_IP}
219}
220
221# One-time check
222if grep -qi "$(echo ${IPV6_NETWORK} | cut -d: -f1-3)" <<< "$(ip a s)"; then
223 if [[ -z "$(get_ipv6)" ]]; then
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100224 notify_error "ipv6-config" "enable_ipv6 is true in docker-compose.yml, but an IPv6 link could not be established. Please verify your IPv6 connection."
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100225 fi
226fi
227
228external_checks() {
229 err_count=0
230 diff_c=0
231 THRESHOLD=${EXTERNAL_CHECKS_THRESHOLD}
232 # Reduce error count by 2 after restarting an unhealthy container
233 GUID=$(mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT version FROM versions WHERE application = 'GUID'" -BN)
234 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
235 while [ ${err_count} -lt ${THRESHOLD} ]; do
236 err_c_cur=${err_count}
237 CHECK_REPONSE="$(curl --connect-timeout 3 -m 10 -4 -s https://checks.mailcow.email -X POST -dguid=${GUID} 2> /dev/null)"
238 if [[ ! -z "${CHECK_REPONSE}" ]] && [[ "$(echo ${CHECK_REPONSE} | jq -r .response)" == "critical" ]]; then
239 echo ${CHECK_REPONSE} | jq -r .out > /tmp/external_checks
240 err_count=$(( ${err_count} + 1 ))
241 fi
242 CHECK_REPONSE6="$(curl --connect-timeout 3 -m 10 -6 -s https://checks.mailcow.email -X POST -dguid=${GUID} 2> /dev/null)"
243 if [[ ! -z "${CHECK_REPONSE6}" ]] && [[ "$(echo ${CHECK_REPONSE6} | jq -r .response)" == "critical" ]]; then
244 echo ${CHECK_REPONSE} | jq -r .out > /tmp/external_checks
245 err_count=$(( ${err_count} + 1 ))
246 fi
247 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
248 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
249 progress "External checks" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
250 if [[ $? == 10 ]]; then
251 diff_c=0
252 sleep 60
253 else
254 diff_c=0
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200255 sleep $(( ( RANDOM % 20 ) + 1800 ))
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100256 fi
257 done
258 return 1
259}
260
261nginx_checks() {
262 err_count=0
263 diff_c=0
264 THRESHOLD=${NGINX_THRESHOLD}
265 # Reduce error count by 2 after restarting an unhealthy container
266 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
267 while [ ${err_count} -lt ${THRESHOLD} ]; do
268 touch /tmp/nginx-mailcow; echo "$(tail -50 /tmp/nginx-mailcow)" > /tmp/nginx-mailcow
269 host_ip=$(get_container_ip nginx-mailcow)
270 err_c_cur=${err_count}
271 /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u / -p 8081 2>> /tmp/nginx-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
272 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
273 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
274 progress "Nginx" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
275 if [[ $? == 10 ]]; then
276 diff_c=0
277 sleep 1
278 else
279 diff_c=0
280 sleep $(( ( RANDOM % 60 ) + 20 ))
281 fi
282 done
283 return 1
284}
285
286unbound_checks() {
287 err_count=0
288 diff_c=0
289 THRESHOLD=${UNBOUND_THRESHOLD}
290 # Reduce error count by 2 after restarting an unhealthy container
291 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
292 while [ ${err_count} -lt ${THRESHOLD} ]; do
293 touch /tmp/unbound-mailcow; echo "$(tail -50 /tmp/unbound-mailcow)" > /tmp/unbound-mailcow
294 host_ip=$(get_container_ip unbound-mailcow)
295 err_c_cur=${err_count}
Matthias Andreas Benkardb7a13512023-11-18 16:44:25 +0100296 /usr/lib/nagios/plugins/check_dns -s ${host_ip} -H stackoverflow.com 2>> /tmp/unbound-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
297 DNSSEC=$(dig com +dnssec | egrep 'flags:.+ad')
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100298 if [[ -z ${DNSSEC} ]]; then
299 echo "DNSSEC failure" 2>> /tmp/unbound-mailcow 1>&2
300 err_count=$(( ${err_count} + 1))
301 else
302 echo "DNSSEC check succeeded" 2>> /tmp/unbound-mailcow 1>&2
303 fi
304 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
305 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
306 progress "Unbound" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
307 if [[ $? == 10 ]]; then
308 diff_c=0
309 sleep 1
310 else
311 diff_c=0
312 sleep $(( ( RANDOM % 60 ) + 20 ))
313 fi
314 done
315 return 1
316}
317
318redis_checks() {
319 # A check for the local redis container
320 err_count=0
321 diff_c=0
322 THRESHOLD=${REDIS_THRESHOLD}
323 # Reduce error count by 2 after restarting an unhealthy container
324 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
325 while [ ${err_count} -lt ${THRESHOLD} ]; do
326 touch /tmp/redis-mailcow; echo "$(tail -50 /tmp/redis-mailcow)" > /tmp/redis-mailcow
327 host_ip=$(get_container_ip redis-mailcow)
328 err_c_cur=${err_count}
329 /usr/lib/nagios/plugins/check_tcp -4 -H redis-mailcow -p 6379 -E -s "PING\n" -q "QUIT" -e "PONG" 2>> /tmp/redis-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
330 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
331 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
332 progress "Redis" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
333 if [[ $? == 10 ]]; then
334 diff_c=0
335 sleep 1
336 else
337 diff_c=0
338 sleep $(( ( RANDOM % 60 ) + 20 ))
339 fi
340 done
341 return 1
342}
343
344mysql_checks() {
345 err_count=0
346 diff_c=0
347 THRESHOLD=${MYSQL_THRESHOLD}
348 # Reduce error count by 2 after restarting an unhealthy container
349 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
350 while [ ${err_count} -lt ${THRESHOLD} ]; do
351 touch /tmp/mysql-mailcow; echo "$(tail -50 /tmp/mysql-mailcow)" > /tmp/mysql-mailcow
352 err_c_cur=${err_count}
353 /usr/lib/nagios/plugins/check_mysql -s /var/run/mysqld/mysqld.sock -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} 2>> /tmp/mysql-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
354 /usr/lib/nagios/plugins/check_mysql_query -s /var/run/mysqld/mysqld.sock -u ${DBUSER} -p ${DBPASS} -d ${DBNAME} -q "SELECT COUNT(*) FROM information_schema.tables" 2>> /tmp/mysql-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
355 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
356 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
357 progress "MySQL/MariaDB" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
358 if [[ $? == 10 ]]; then
359 diff_c=0
360 sleep 1
361 else
362 diff_c=0
363 sleep $(( ( RANDOM % 60 ) + 20 ))
364 fi
365 done
366 return 1
367}
368
369mysql_repl_checks() {
370 err_count=0
371 diff_c=0
372 THRESHOLD=${MYSQL_REPLICATION_THRESHOLD}
373 # Reduce error count by 2 after restarting an unhealthy container
374 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
375 while [ ${err_count} -lt ${THRESHOLD} ]; do
376 touch /tmp/mysql_repl_checks; echo "$(tail -50 /tmp/mysql_repl_checks)" > /tmp/mysql_repl_checks
377 err_c_cur=${err_count}
378 /usr/lib/nagios/plugins/check_mysql_slavestatus.sh -S /var/run/mysqld/mysqld.sock -u root -p ${DBROOT} 2>> /tmp/mysql_repl_checks 1>&2; err_count=$(( ${err_count} + $? ))
379 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
380 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
381 progress "MySQL/MariaDB replication" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
382 if [[ $? == 10 ]]; then
383 diff_c=0
384 sleep 60
385 else
386 diff_c=0
387 sleep $(( ( RANDOM % 60 ) + 20 ))
388 fi
389 done
390 return 1
391}
392
393sogo_checks() {
394 err_count=0
395 diff_c=0
396 THRESHOLD=${SOGO_THRESHOLD}
397 # Reduce error count by 2 after restarting an unhealthy container
398 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
399 while [ ${err_count} -lt ${THRESHOLD} ]; do
400 touch /tmp/sogo-mailcow; echo "$(tail -50 /tmp/sogo-mailcow)" > /tmp/sogo-mailcow
401 host_ip=$(get_container_ip sogo-mailcow)
402 err_c_cur=${err_count}
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100403 /usr/lib/nagios/plugins/check_http -4 -H ${host_ip} -u /SOGo.index/ -p 20000 2>> /tmp/sogo-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100404 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
405 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
406 progress "SOGo" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
407 if [[ $? == 10 ]]; then
408 diff_c=0
409 sleep 1
410 else
411 diff_c=0
412 sleep $(( ( RANDOM % 60 ) + 20 ))
413 fi
414 done
415 return 1
416}
417
418postfix_checks() {
419 err_count=0
420 diff_c=0
421 THRESHOLD=${POSTFIX_THRESHOLD}
422 # Reduce error count by 2 after restarting an unhealthy container
423 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
424 while [ ${err_count} -lt ${THRESHOLD} ]; do
425 touch /tmp/postfix-mailcow; echo "$(tail -50 /tmp/postfix-mailcow)" > /tmp/postfix-mailcow
426 host_ip=$(get_container_ip postfix-mailcow)
427 err_c_cur=${err_count}
428 /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -f "watchdog@invalid" -C "RCPT TO:watchdog@localhost" -C DATA -C . -R 250 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
429 /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 589 -S 2>> /tmp/postfix-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
430 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
431 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
432 progress "Postfix" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
433 if [[ $? == 10 ]]; then
434 diff_c=0
435 sleep 1
436 else
437 diff_c=0
438 sleep $(( ( RANDOM % 60 ) + 20 ))
439 fi
440 done
441 return 1
442}
443
444clamd_checks() {
445 err_count=0
446 diff_c=0
447 THRESHOLD=${CLAMD_THRESHOLD}
448 # Reduce error count by 2 after restarting an unhealthy container
449 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
450 while [ ${err_count} -lt ${THRESHOLD} ]; do
451 touch /tmp/clamd-mailcow; echo "$(tail -50 /tmp/clamd-mailcow)" > /tmp/clamd-mailcow
452 host_ip=$(get_container_ip clamd-mailcow)
453 err_c_cur=${err_count}
454 /usr/lib/nagios/plugins/check_clamd -4 -H ${host_ip} 2>> /tmp/clamd-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
455 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
456 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
457 progress "Clamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
458 if [[ $? == 10 ]]; then
459 diff_c=0
460 sleep 1
461 else
462 diff_c=0
463 sleep $(( ( RANDOM % 120 ) + 20 ))
464 fi
465 done
466 return 1
467}
468
469dovecot_checks() {
470 err_count=0
471 diff_c=0
472 THRESHOLD=${DOVECOT_THRESHOLD}
473 # Reduce error count by 2 after restarting an unhealthy container
474 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
475 while [ ${err_count} -lt ${THRESHOLD} ]; do
476 touch /tmp/dovecot-mailcow; echo "$(tail -50 /tmp/dovecot-mailcow)" > /tmp/dovecot-mailcow
477 host_ip=$(get_container_ip dovecot-mailcow)
478 err_c_cur=${err_count}
479 /usr/lib/nagios/plugins/check_smtp -4 -H ${host_ip} -p 24 -f "watchdog@invalid" -C "RCPT TO:<watchdog@invalid>" -L -R "User doesn't exist" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
480 /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 993 -S -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
481 /usr/lib/nagios/plugins/check_imap -4 -H ${host_ip} -p 143 -e "OK " 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
482 /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10001 -e "VERSION" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
483 /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 4190 -e "Dovecot ready" 2>> /tmp/dovecot-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
484 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
485 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
486 progress "Dovecot" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
487 if [[ $? == 10 ]]; then
488 diff_c=0
489 sleep 1
490 else
491 diff_c=0
492 sleep $(( ( RANDOM % 60 ) + 20 ))
493 fi
494 done
495 return 1
496}
497
498dovecot_repl_checks() {
499 err_count=0
500 diff_c=0
501 THRESHOLD=${DOVECOT_REPL_THRESHOLD}
502 D_REPL_STATUS=$(redis-cli -h redis -r GET DOVECOT_REPL_HEALTH)
503 # Reduce error count by 2 after restarting an unhealthy container
504 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
505 while [ ${err_count} -lt ${THRESHOLD} ]; do
506 err_c_cur=${err_count}
507 D_REPL_STATUS=$(redis-cli --raw -h redis GET DOVECOT_REPL_HEALTH)
508 if [[ "${D_REPL_STATUS}" != "1" ]]; then
509 err_count=$(( ${err_count} + 1 ))
510 fi
511 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
512 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
513 progress "Dovecot replication" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
514 if [[ $? == 10 ]]; then
515 diff_c=0
516 sleep 60
517 else
518 diff_c=0
519 sleep $(( ( RANDOM % 60 ) + 20 ))
520 fi
521 done
522 return 1
523}
524
525cert_checks() {
526 err_count=0
527 diff_c=0
528 THRESHOLD=7
529 # Reduce error count by 2 after restarting an unhealthy container
530 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
531 while [ ${err_count} -lt ${THRESHOLD} ]; do
532 touch /tmp/certcheck; echo "$(tail -50 /tmp/certcheck)" > /tmp/certcheck
533 host_ip_postfix=$(get_container_ip postfix)
534 host_ip_dovecot=$(get_container_ip dovecot)
535 err_c_cur=${err_count}
536 /usr/lib/nagios/plugins/check_smtp -H ${host_ip_postfix} -p 589 -4 -S -D 7 2>> /tmp/certcheck 1>&2; err_count=$(( ${err_count} + $? ))
537 /usr/lib/nagios/plugins/check_imap -H ${host_ip_dovecot} -p 993 -4 -S -D 7 2>> /tmp/certcheck 1>&2; err_count=$(( ${err_count} + $? ))
538 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
539 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
540 progress "Primary certificate expiry check" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
541 # Always sleep 5 minutes, mail notifications are limited
542 sleep 300
543 done
544 return 1
545}
546
547phpfpm_checks() {
548 err_count=0
549 diff_c=0
550 THRESHOLD=${PHPFPM_THRESHOLD}
551 # Reduce error count by 2 after restarting an unhealthy container
552 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
553 while [ ${err_count} -lt ${THRESHOLD} ]; do
554 touch /tmp/php-fpm-mailcow; echo "$(tail -50 /tmp/php-fpm-mailcow)" > /tmp/php-fpm-mailcow
555 host_ip=$(get_container_ip php-fpm-mailcow)
556 err_c_cur=${err_count}
557 /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9001 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
558 /usr/lib/nagios/plugins/check_tcp -H ${host_ip} -p 9002 2>> /tmp/php-fpm-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
559 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
560 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
561 progress "PHP-FPM" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
562 if [[ $? == 10 ]]; then
563 diff_c=0
564 sleep 1
565 else
566 diff_c=0
567 sleep $(( ( RANDOM % 60 ) + 20 ))
568 fi
569 done
570 return 1
571}
572
573ratelimit_checks() {
574 err_count=0
575 diff_c=0
576 THRESHOLD=${RATELIMIT_THRESHOLD}
577 RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
578 # Reduce error count by 2 after restarting an unhealthy container
579 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
580 while [ ${err_count} -lt ${THRESHOLD} ]; do
581 err_c_cur=${err_count}
582 RL_LOG_STATUS_PREV=${RL_LOG_STATUS}
583 RL_LOG_STATUS=$(redis-cli -h redis LRANGE RL_LOG 0 0 | jq .qid)
584 if [[ ${RL_LOG_STATUS_PREV} != ${RL_LOG_STATUS} ]]; then
585 err_count=$(( ${err_count} + 1 ))
586 echo 'Last 10 applied ratelimits (may overlap with previous reports).' > /tmp/ratelimit
587 echo 'Full ratelimit buckets can be emptied by deleting the ratelimit hash from within mailcow UI (see /debug -> Protocols -> Ratelimit):' >> /tmp/ratelimit
588 echo >> /tmp/ratelimit
589 redis-cli --raw -h redis LRANGE RL_LOG 0 10 | jq . >> /tmp/ratelimit
590 fi
591 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
592 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
593 progress "Ratelimit" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
594 if [[ $? == 10 ]]; then
595 diff_c=0
596 sleep 1
597 else
598 diff_c=0
599 sleep $(( ( RANDOM % 60 ) + 20 ))
600 fi
601 done
602 return 1
603}
604
605mailq_checks() {
606 err_count=0
607 diff_c=0
608 THRESHOLD=${MAILQ_THRESHOLD}
609 # Reduce error count by 2 after restarting an unhealthy container
610 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
611 while [ ${err_count} -lt ${THRESHOLD} ]; do
612 touch /tmp/mail_queue_status; echo "$(tail -50 /tmp/mail_queue_status)" > /tmp/mail_queue_status
613 MAILQ_LOG_STATUS=$(find /var/spool/postfix/deferred -type f | wc -l)
614 echo "Mail queue contains ${MAILQ_LOG_STATUS} items (critical limit is ${MAILQ_CRIT}) at $(date)" >> /tmp/mail_queue_status
615 err_c_cur=${err_count}
616 if [ ${MAILQ_LOG_STATUS} -ge ${MAILQ_CRIT} ]; then
617 err_count=$(( ${err_count} + 1 ))
618 echo "Mail queue contains ${MAILQ_LOG_STATUS} items (critical limit is ${MAILQ_CRIT}) at $(date)" >> /tmp/mail_queue_status
619 fi
620 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
621 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
622 progress "Mail queue" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
623 if [[ $? == 10 ]]; then
624 diff_c=0
625 sleep 60
626 else
627 diff_c=0
628 sleep $(( ( RANDOM % 60 ) + 20 ))
629 fi
630 done
631 return 1
632}
633
634fail2ban_checks() {
635 err_count=0
636 diff_c=0
637 THRESHOLD=${FAIL2BAN_THRESHOLD}
638 F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
639 F2B_RES=
640 # Reduce error count by 2 after restarting an unhealthy container
641 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
642 while [ ${err_count} -lt ${THRESHOLD} ]; do
643 err_c_cur=${err_count}
644 F2B_LOG_STATUS_PREV=(${F2B_LOG_STATUS[@]})
645 F2B_LOG_STATUS=($(${REDIS_CMDLINE} --raw HKEYS F2B_ACTIVE_BANS))
646 array_diff F2B_RES F2B_LOG_STATUS F2B_LOG_STATUS_PREV
647 if [[ ! -z "${F2B_RES}" ]]; then
648 err_count=$(( ${err_count} + 1 ))
649 echo -n "${F2B_RES[@]}" | tr -cd "[a-fA-F0-9.:/] " | timeout 3s ${REDIS_CMDLINE} -x SET F2B_RES > /dev/null
650 if [ $? -ne 0 ]; then
651 ${REDIS_CMDLINE} -x DEL F2B_RES
652 fi
653 fi
654 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
655 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
656 progress "Fail2ban" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
657 if [[ $? == 10 ]]; then
658 diff_c=0
659 sleep 1
660 else
661 diff_c=0
662 sleep $(( ( RANDOM % 60 ) + 20 ))
663 fi
664 done
665 return 1
666}
667
668acme_checks() {
669 err_count=0
670 diff_c=0
671 THRESHOLD=${ACME_THRESHOLD}
672 ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME)
673 if [[ -z "${ACME_LOG_STATUS}" ]]; then
674 ${REDIS_CMDLINE} SET ACME_FAIL_TIME 0
675 ACME_LOG_STATUS=0
676 fi
677 # Reduce error count by 2 after restarting an unhealthy container
678 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
679 while [ ${err_count} -lt ${THRESHOLD} ]; do
680 err_c_cur=${err_count}
681 ACME_LOG_STATUS_PREV=${ACME_LOG_STATUS}
682 ACME_LC=0
683 until [[ ! -z ${ACME_LOG_STATUS} ]] || [ ${ACME_LC} -ge 3 ]; do
684 ACME_LOG_STATUS=$(redis-cli -h redis GET ACME_FAIL_TIME 2> /dev/null)
685 sleep 3
686 ACME_LC=$((ACME_LC+1))
687 done
688 if [[ ${ACME_LOG_STATUS_PREV} != ${ACME_LOG_STATUS} ]]; then
689 err_count=$(( ${err_count} + 1 ))
690 fi
691 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
692 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
693 progress "ACME" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
694 if [[ $? == 10 ]]; then
695 diff_c=0
696 sleep 1
697 else
698 diff_c=0
699 sleep $(( ( RANDOM % 60 ) + 20 ))
700 fi
701 done
702 return 1
703}
704
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100705rspamd_checks() {
706 err_count=0
707 diff_c=0
708 THRESHOLD=${RSPAMD_THRESHOLD}
709 # Reduce error count by 2 after restarting an unhealthy container
710 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
711 while [ ${err_count} -lt ${THRESHOLD} ]; do
712 touch /tmp/rspamd-mailcow; echo "$(tail -50 /tmp/rspamd-mailcow)" > /tmp/rspamd-mailcow
713 host_ip=$(get_container_ip rspamd-mailcow)
714 err_c_cur=${err_count}
715 SCORE=$(echo 'To: null@localhost
716From: watchdog@localhost
717
718Empty
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100719' | usr/bin/curl --max-time 10 -s --data-binary @- --unix-socket /var/lib/rspamd/rspamd.sock http://rspamd/scan | jq -rc .default.required_score | sed 's/\..*//' )
720 if [[ ${SCORE} -ne 9999 ]]; then
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100721 echo "Rspamd settings check failed, score returned: ${SCORE}" 2>> /tmp/rspamd-mailcow 1>&2
722 err_count=$(( ${err_count} + 1))
723 else
724 echo "Rspamd settings check succeeded, score returned: ${SCORE}" 2>> /tmp/rspamd-mailcow 1>&2
725 fi
726 # A dirty hack until a PING PONG event is implemented to worker proxy
727 # We expect an empty response, not a timeout
728 if [ "$(curl -s --max-time 10 ${host_ip}:9900 2> /dev/null ; echo $?)" == "28" ]; then
729 echo "Milter check failed" 2>> /tmp/rspamd-mailcow 1>&2; err_count=$(( ${err_count} + 1 ));
730 else
731 echo "Milter check succeeded" 2>> /tmp/rspamd-mailcow 1>&2
732 fi
733 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
734 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
735 progress "Rspamd" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
736 if [[ $? == 10 ]]; then
737 diff_c=0
738 sleep 1
739 else
740 diff_c=0
741 sleep $(( ( RANDOM % 60 ) + 20 ))
742 fi
743 done
744 return 1
745}
746
747olefy_checks() {
748 err_count=0
749 diff_c=0
750 THRESHOLD=${OLEFY_THRESHOLD}
751 # Reduce error count by 2 after restarting an unhealthy container
752 trap "[ ${err_count} -gt 1 ] && err_count=$(( ${err_count} - 2 ))" USR1
753 while [ ${err_count} -lt ${THRESHOLD} ]; do
754 touch /tmp/olefy-mailcow; echo "$(tail -50 /tmp/olefy-mailcow)" > /tmp/olefy-mailcow
755 host_ip=$(get_container_ip olefy-mailcow)
756 err_c_cur=${err_count}
757 /usr/lib/nagios/plugins/check_tcp -4 -H ${host_ip} -p 10055 -s "PING\n" 2>> /tmp/olefy-mailcow 1>&2; err_count=$(( ${err_count} + $? ))
758 [ ${err_c_cur} -eq ${err_count} ] && [ ! $((${err_count} - 1)) -lt 0 ] && err_count=$((${err_count} - 1)) diff_c=1
759 [ ${err_c_cur} -ne ${err_count} ] && diff_c=$(( ${err_c_cur} - ${err_count} ))
760 progress "Olefy" ${THRESHOLD} $(( ${THRESHOLD} - ${err_count} )) ${diff_c}
761 if [[ $? == 10 ]]; then
762 diff_c=0
763 sleep 1
764 else
765 diff_c=0
766 sleep $(( ( RANDOM % 60 ) + 20 ))
767 fi
768 done
769 return 1
770}
771
772# Notify about start
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100773if [[ ${WATCHDOG_NOTIFY_START} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
774 notify_error "watchdog-mailcow" "Watchdog started monitoring mailcow."
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100775fi
776
777# Create watchdog agents
778
779(
780while true; do
781 if ! nginx_checks; then
782 log_msg "Nginx hit error limit"
783 echo nginx-mailcow > /tmp/com_pipe
784 fi
785done
786) &
787PID=$!
788echo "Spawned nginx_checks with PID ${PID}"
789BACKGROUND_TASKS+=(${PID})
790
791if [[ ${WATCHDOG_EXTERNAL_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
792(
793while true; do
794 if ! external_checks; then
795 log_msg "External checks hit error limit"
796 echo external_checks > /tmp/com_pipe
797 fi
798done
799) &
800PID=$!
801echo "Spawned external_checks with PID ${PID}"
802BACKGROUND_TASKS+=(${PID})
803fi
804
805if [[ ${WATCHDOG_MYSQL_REPLICATION_CHECKS} =~ ^([yY][eE][sS]|[yY])+$ ]]; then
806(
807while true; do
808 if ! mysql_repl_checks; then
809 log_msg "MySQL replication check hit error limit"
810 echo mysql_repl_checks > /tmp/com_pipe
811 fi
812done
813) &
814PID=$!
815echo "Spawned mysql_repl_checks with PID ${PID}"
816BACKGROUND_TASKS+=(${PID})
817fi
818
819(
820while true; do
821 if ! mysql_checks; then
822 log_msg "MySQL hit error limit"
823 echo mysql-mailcow > /tmp/com_pipe
824 fi
825done
826) &
827PID=$!
828echo "Spawned mysql_checks with PID ${PID}"
829BACKGROUND_TASKS+=(${PID})
830
831(
832while true; do
833 if ! redis_checks; then
834 log_msg "Local Redis hit error limit"
835 echo redis-mailcow > /tmp/com_pipe
836 fi
837done
838) &
839PID=$!
840echo "Spawned redis_checks with PID ${PID}"
841BACKGROUND_TASKS+=(${PID})
842
843(
844while true; do
845 if ! phpfpm_checks; then
846 log_msg "PHP-FPM hit error limit"
847 echo php-fpm-mailcow > /tmp/com_pipe
848 fi
849done
850) &
851PID=$!
852echo "Spawned phpfpm_checks with PID ${PID}"
853BACKGROUND_TASKS+=(${PID})
854
855if [[ "${SKIP_SOGO}" =~ ^([nN][oO]|[nN])+$ ]]; then
856(
857while true; do
858 if ! sogo_checks; then
859 log_msg "SOGo hit error limit"
860 echo sogo-mailcow > /tmp/com_pipe
861 fi
862done
863) &
864PID=$!
865echo "Spawned sogo_checks with PID ${PID}"
866BACKGROUND_TASKS+=(${PID})
867fi
868
869if [ ${CHECK_UNBOUND} -eq 1 ]; then
870(
871while true; do
872 if ! unbound_checks; then
873 log_msg "Unbound hit error limit"
874 echo unbound-mailcow > /tmp/com_pipe
875 fi
876done
877) &
878PID=$!
879echo "Spawned unbound_checks with PID ${PID}"
880BACKGROUND_TASKS+=(${PID})
881fi
882
883if [[ "${SKIP_CLAMD}" =~ ^([nN][oO]|[nN])+$ ]]; then
884(
885while true; do
886 if ! clamd_checks; then
887 log_msg "Clamd hit error limit"
888 echo clamd-mailcow > /tmp/com_pipe
889 fi
890done
891) &
892PID=$!
893echo "Spawned clamd_checks with PID ${PID}"
894BACKGROUND_TASKS+=(${PID})
895fi
896
897(
898while true; do
899 if ! postfix_checks; then
900 log_msg "Postfix hit error limit"
901 echo postfix-mailcow > /tmp/com_pipe
902 fi
903done
904) &
905PID=$!
906echo "Spawned postfix_checks with PID ${PID}"
907BACKGROUND_TASKS+=(${PID})
908
909(
910while true; do
911 if ! mailq_checks; then
912 log_msg "Mail queue hit error limit"
913 echo mail_queue_status > /tmp/com_pipe
914 fi
915done
916) &
917PID=$!
918echo "Spawned mailq_checks with PID ${PID}"
919BACKGROUND_TASKS+=(${PID})
920
921(
922while true; do
923 if ! dovecot_checks; then
924 log_msg "Dovecot hit error limit"
925 echo dovecot-mailcow > /tmp/com_pipe
926 fi
927done
928) &
929PID=$!
930echo "Spawned dovecot_checks with PID ${PID}"
931BACKGROUND_TASKS+=(${PID})
932
933(
934while true; do
935 if ! dovecot_repl_checks; then
936 log_msg "Dovecot hit error limit"
937 echo dovecot_repl_checks > /tmp/com_pipe
938 fi
939done
940) &
941PID=$!
942echo "Spawned dovecot_repl_checks with PID ${PID}"
943BACKGROUND_TASKS+=(${PID})
944
945(
946while true; do
947 if ! rspamd_checks; then
948 log_msg "Rspamd hit error limit"
949 echo rspamd-mailcow > /tmp/com_pipe
950 fi
951done
952) &
953PID=$!
954echo "Spawned rspamd_checks with PID ${PID}"
955BACKGROUND_TASKS+=(${PID})
956
957(
958while true; do
959 if ! ratelimit_checks; then
960 log_msg "Ratelimit hit error limit"
961 echo ratelimit > /tmp/com_pipe
962 fi
963done
964) &
965PID=$!
966echo "Spawned ratelimit_checks with PID ${PID}"
967BACKGROUND_TASKS+=(${PID})
968
969(
970while true; do
971 if ! fail2ban_checks; then
972 log_msg "Fail2ban hit error limit"
973 echo fail2ban > /tmp/com_pipe
974 fi
975done
976) &
977PID=$!
978echo "Spawned fail2ban_checks with PID ${PID}"
979BACKGROUND_TASKS+=(${PID})
980
981(
982while true; do
983 if ! cert_checks; then
984 log_msg "Cert check hit error limit"
985 echo certcheck > /tmp/com_pipe
986 fi
987done
988) &
989PID=$!
990echo "Spawned cert_checks with PID ${PID}"
991BACKGROUND_TASKS+=(${PID})
992
993(
994while true; do
995 if ! olefy_checks; then
996 log_msg "Olefy hit error limit"
997 echo olefy-mailcow > /tmp/com_pipe
998 fi
999done
1000) &
1001PID=$!
1002echo "Spawned olefy_checks with PID ${PID}"
1003BACKGROUND_TASKS+=(${PID})
1004
1005(
1006while true; do
1007 if ! acme_checks; then
1008 log_msg "ACME client hit error limit"
1009 echo acme-mailcow > /tmp/com_pipe
1010 fi
1011done
1012) &
1013PID=$!
1014echo "Spawned acme_checks with PID ${PID}"
1015BACKGROUND_TASKS+=(${PID})
1016
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001017# Monitor watchdog agents, stop script when agents fails and wait for respawn by Docker (restart:always:n)
1018(
1019while true; do
1020 for bg_task in ${BACKGROUND_TASKS[*]}; do
1021 if ! kill -0 ${bg_task} 1>&2; then
1022 log_msg "Worker ${bg_task} died, stopping watchdog and waiting for respawn..."
1023 kill -TERM 1
1024 fi
1025 sleep 10
1026 done
1027done
1028) &
1029
1030# Monitor dockerapi
1031(
1032while true; do
1033 while nc -z dockerapi 443; do
1034 sleep 3
1035 done
1036 log_msg "Cannot find dockerapi-mailcow, waiting to recover..."
1037 kill -STOP ${BACKGROUND_TASKS[*]}
1038 until nc -z dockerapi 443; do
1039 sleep 3
1040 done
1041 kill -CONT ${BACKGROUND_TASKS[*]}
1042 kill -USR1 ${BACKGROUND_TASKS[*]}
1043done
1044) &
1045
1046# Actions when threshold limit is reached
1047while true; do
1048 CONTAINER_ID=
1049 HAS_INITDB=
1050 read com_pipe_answer </tmp/com_pipe
1051 if [ -s "/tmp/${com_pipe_answer}" ]; then
1052 cat "/tmp/${com_pipe_answer}"
1053 fi
1054 if [[ ${com_pipe_answer} == "ratelimit" ]]; then
1055 log_msg "At least one ratelimit was applied"
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001056 notify_error "${com_pipe_answer}"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001057 elif [[ ${com_pipe_answer} == "mail_queue_status" ]]; then
1058 log_msg "Mail queue status is critical"
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001059 notify_error "${com_pipe_answer}"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001060 elif [[ ${com_pipe_answer} == "external_checks" ]]; then
1061 log_msg "Your mailcow is an open relay!"
1062 # Define $2 to override message text, else print service was restarted at ...
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001063 notify_error "${com_pipe_answer}" "Please stop mailcow now and check your network configuration!"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001064 elif [[ ${com_pipe_answer} == "mysql_repl_checks" ]]; then
1065 log_msg "MySQL replication is not working properly"
1066 # Define $2 to override message text, else print service was restarted at ...
1067 # Once mail per 10 minutes
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001068 notify_error "${com_pipe_answer}" "Please check the SQL replication status" 600
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001069 elif [[ ${com_pipe_answer} == "dovecot_repl_checks" ]]; then
1070 log_msg "Dovecot replication is not working properly"
1071 # Define $2 to override message text, else print service was restarted at ...
1072 # Once mail per 10 minutes
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001073 notify_error "${com_pipe_answer}" "Please check the Dovecot replicator status" 600
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001074 elif [[ ${com_pipe_answer} == "certcheck" ]]; then
1075 log_msg "Certificates are about to expire"
1076 # Define $2 to override message text, else print service was restarted at ...
1077 # Only mail once a day
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001078 notify_error "${com_pipe_answer}" "Please renew your certificate" 86400
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001079 elif [[ ${com_pipe_answer} == "acme-mailcow" ]]; then
1080 log_msg "acme-mailcow did not complete successfully"
1081 # Define $2 to override message text, else print service was restarted at ...
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001082 notify_error "${com_pipe_answer}" "Please check acme-mailcow for further information."
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001083 elif [[ ${com_pipe_answer} == "fail2ban" ]]; then
1084 F2B_RES=($(timeout 4s ${REDIS_CMDLINE} --raw GET F2B_RES 2> /dev/null))
1085 if [[ ! -z "${F2B_RES}" ]]; then
1086 ${REDIS_CMDLINE} DEL F2B_RES > /dev/null
1087 host=
1088 for host in "${F2B_RES[@]}"; do
1089 log_msg "Banned ${host}"
1090 rm /tmp/fail2ban 2> /dev/null
1091 timeout 2s whois "${host}" > /tmp/fail2ban
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001092 [[ ${WATCHDOG_NOTIFY_BAN} =~ ^([yY][eE][sS]|[yY])+$ ]] && notify_error "${com_pipe_answer}" "IP ban: ${host}"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001093 done
1094 fi
1095 elif [[ ${com_pipe_answer} =~ .+-mailcow ]]; then
1096 kill -STOP ${BACKGROUND_TASKS[*]}
1097 sleep 10
1098 CONTAINER_ID=$(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], project: .Config.Labels[\"com.docker.compose.project\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${com_pipe_answer}\")) | select( .project | tostring | contains(\"${COMPOSE_PROJECT_NAME,,}\")) | .id")
1099 if [[ ! -z ${CONTAINER_ID} ]]; then
1100 if [[ "${com_pipe_answer}" == "php-fpm-mailcow" ]]; then
1101 HAS_INITDB=$(curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/top | jq '.msg.Processes[] | contains(["php -c /usr/local/etc/php -f /web/inc/init_db.inc.php"])' | grep true)
1102 fi
1103 S_RUNNING=$(($(date +%s) - $(curl --silent --insecure https://dockerapi/containers/${CONTAINER_ID}/json | jq .State.StartedAt | xargs -n1 date +%s -d)))
1104 if [ ${S_RUNNING} -lt 360 ]; then
1105 log_msg "Container is running for less than 360 seconds, skipping action..."
1106 elif [[ ! -z ${HAS_INITDB} ]]; then
1107 log_msg "Database is being initialized by php-fpm-mailcow, not restarting but delaying checks for a minute..."
1108 sleep 60
1109 else
1110 log_msg "Sending restart command to ${CONTAINER_ID}..."
1111 curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/restart
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001112 notify_error "${com_pipe_answer}"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001113 log_msg "Wait for restarted container to settle and continue watching..."
1114 sleep 35
1115 fi
1116 fi
1117 kill -CONT ${BACKGROUND_TASKS[*]}
1118 sleep 1
1119 kill -USR1 ${BACKGROUND_TASKS[*]}
1120 fi
1121done
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001122