-
Notifications
You must be signed in to change notification settings - Fork 26
/
zabbix_postfix_passive.sh
123 lines (99 loc) · 3.94 KB
/
zabbix_postfix_passive.sh
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
#!/usr/bin/env bash
# For Debian/Ubuntu
[ -f /var/log/mail.log ] && MAILLOG=/var/log/mail.log
# For RHEL/Centos
[ -f /var/log/maillog ] && MAILLOG=/var/log/maillog
# pygtail from Debian 11, Ubuntu 22.04 and later
if [ -f /usr/bin/pygtail ]; then
PYGTAIL=/usr/bin/pygtail
# pygtail from Python PIP install to Zabbix user
elif [ -f /var/lib/zabbix/.local/bin/pygtail ]; then
PYGTAIL=/var/lib/zabbix/.local/bin/pygtail
# pygtail from copy within this repository
else
PYGTAIL=/usr/local/sbin/pygtail
fi
PFOFFSETFILE=/tmp/zabbix-postfix-passive-offset.dat
PFSTATSFILE=/tmp/zabbix-postfix-passive-statsfile.dat
TEMPFILE=$(mktemp --suffix=-zabbix-postfix-passive)
PFLOGSUMM=/usr/sbin/pflogsumm
# list of values we are interested in
PFVALS=( 'received' 'delivered' 'forwarded' 'deferred' 'bounced' 'rejected' 'held' 'discarded' 'reject_warnings' 'bytes_received' 'bytes_delivered' )
# write result of running this script
write_result () {
echo "$2"
rm "${TEMPFILE}"
exit $1
}
# check for binaries we need to run the script
if [ ! -x ${PFLOGSUMM} ] ; then
echo "ERROR: ${PFLOGSUMM} not found"
exit 1
fi
if [ ! -x ${PYGTAIL} ] ; then
echo "ERROR: ${PYGTAIL} not found"
exit 1
fi
if [ ! -r ${MAILLOG} ] ; then
echo "ERROR: ${MAILLOG} not readable"
exit 1
fi
# check whether file exists and the write permissions are granted
if [ ! -w "${PFSTATSFILE}" ]; then
touch "${PFSTATSFILE}" > /dev/null 2>&1
if [ ! $? -eq 0 ]; then
result_text="ERROR: wrong exit code returned while creating file ${PFSTATSFILE} and setting its owner to zaabbix:zabbix"
result_code="1"
write_result "${result_code}" "${result_text}"
fi
fi
# read specific value from data file and print it
readvalue () {
local $key
key=$(echo ${PFVALS[@]} | grep -wo $1)
if [ -n "${key}" ]; then
value=$(grep -e "^${key};" "${PFSTATSFILE}" | cut -d ";" -f2)
echo "${value}"
else
rm "${TEMPFILE}"
result_text="ERROR: could not get value \"$1\" from ${PFSTATSFILE}"
result_code="1"
write_result "${result_code}" "${result_text}"
fi
}
# update value in data file
updatevalue() {
local $key
local $pfkey
key=$1
pfkey=$(echo "$1" | tr '_' ' ')
# convert value to bytes
value=$(grep -m 1 "$pfkey" $TEMPFILE | awk '{print $1}' | awk '/k|m/{p = /k/?1:2}{printf "%d\n", int($1) * 1024 ^ p}')
# update values in data file
old_value=$(grep -e "^${key};" "${PFSTATSFILE}" | cut -d ";" -f2)
if [ -n "${old_value}" ]; then
sed -i -e "s/^${key};${old_value}/${key};$((${old_value}+${value}))/" "${PFSTATSFILE}"
else
echo "${key};${value}" >> "${PFSTATSFILE}"
fi
}
# is there a requests for specific value or do we update all values ?
if [ -n "$1" ]; then
readvalue "$1"
else
# read the new part of mail log and read it with pflogsumm to get the summary
"${PYGTAIL}" -o"${PFOFFSETFILE}" "${MAILLOG}" | "${PFLOGSUMM}" -h 0 -u 0 --no_bounce_detail --no_deferral_detail --no_reject_detail --no_smtpd_warnings --no_no_msg_size > "${TEMPFILE}" 2>/dev/null
if [ ! $? -eq 0 ]; then
result_text="ERROR: wrong exit code returned while running \"${PYGTAIL}\" -o\"${PFOFFSETFILE}\" \"${MAILLOG}\" | \"${PFLOGSUMM}\" -h 0 -u 0 --no_bounce_detail --no_deferral_detail --no_reject_detail --no_smtpd_warnings --no_no_msg_size > \"${TEMPFILE}\" 2>/dev/null"
result_code="1"
write_result "${result_code}" "${result_text}"
fi
# update all values from pflogsumm summary
for i in "${PFVALS[@]}"; do
updatevalue "$i"
done
result_text="OK: statistics updated"
result_code="0"
write_result "${result_code}" "${result_text}"
fi
rm "${TEMPFILE}"