This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
check_postfix_problems
executable file
·127 lines (106 loc) · 3.28 KB
/
check_postfix_problems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env bash
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
# ===============
# check_postfixbounced - plugin to check the number of mail bounced by parsing logfiles
# ===============
# * based on mail processor; adapted by Dave Derycker to check for bounces and errors
# * mail processor written by Cecil Westerhof & Modifications for nagios by Frank IJskes
# * In the beginning of January there can be a problem when the log is not rotated yet. eg 28-12-14 is seen as 28-12-15
# version 1.0 (14.Nov.2011)
# plugin return codes:
# 0 OK
# 1 Warning
# 2 Critical
# 3 Unknown
while getopts "hvw:c:" opt
do
case $opt in
h)
showhelp=1
break
;;
w)
warning="$OPTARG"
;;
c)
critical="$OPTARG"
;;
v)
verbose=1
;;
esac
done
declare -r NO_OF_SECONDS=3000
declare -r UNTIL_TIME=$(date +%s)
declare -i numberOfMails=0
postfixlog="/var/log/mail.log"
postfixerrorlog="/var/log/mail.err"
if [ ! "$postfixlog" ]; then
echo "Could not find postfix log!"
exit 3
fi
if [ ! "$postfixerrorlog" ]; then
echo "Could not find postfix error log!"
exit 3
fi
printUsage() {
echo "Usage: $0 [-h] [-v] -w <warning> -c <critical> "
echo ""
echo "Example: $0 -w 50 -c 100 "
}
printHelp() {
printUsage
echo ""
echo "This plugin checks the number of messages bounced, and other errors generated, by Postfix in the last $NO_OF_SECONDS seconds."
echo ""
echo "For more details, see inside the script ;)"
echo ""
exit 3
}
IFS='
'
getnrMesgs () {
for dateTime in $(grep ' postfix/smtp\[.*, status=bounced ' ${postfixlog} | \
awk '{ print $1, $2, $3; }') ; do
if [[ $((${UNTIL_TIME} - $(date --date="${dateTime}" +%s))) -lt ${NO_OF_SECONDS} ]] ; then
numberOfMails+=1
fi
done
for dateTime in $(grep 'postfix' ${postfixerrorlog} | \
awk '{ print $1, $2, $3; }') ; do
if [[ $((${UNTIL_TIME} - $(date --date="${dateTime}" +%s))) -lt ${NO_OF_SECONDS} ]] ; then
numberOfMails+=1
fi
done
echo ${numberOfMails}
}
if [ "$showhelp" = "1" ]; then
printHelp
exit 3
fi
if [ ! "$warning" ] || [ ! "$critical" ]; then
printUsage
exit 3
fi
if [ $warning -ge $critical ]; then
echo "<warning> has to be smaller than <critical>!"
exit 3
fi
nrmsgs=`getnrMesgs`
echo "Messages bounced or errored in the last $NO_OF_SECONDS seconds: $nrmsgs | problems=$nrmsgs"
if [ "$nrmsgs" -ge "$critical" ]; then
exit 2
elif [ "$nrmsgs" -ge "$warning" ]; then
exit 1
else
exit 0
fi