-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzabbix_mailflow
executable file
·118 lines (95 loc) · 2.65 KB
/
zabbix_mailflow
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
#!/bin/bash
#
# Zabbix agent extension for monitoring SMTP flows
BUFFER=/tmp/zabbix-mailflow
CFG=/etc/zabbix/zabbix-mailflow.conf
AWKSCRIPT=/etc/zabbix/zabbix_mailflow_getheader.awk
SOFTWARE="awk fetchmail jo ssh"
source $CFG
function discover_mailflow() {
jo -p data=$( jo -a $(
for ((i=0; i<$LAST_RELAY; i++))
do
jo {\#RELAY}=${RELAY[i]}
done
) )
}
function send_mail() {
mkdir -p $BUFFER; chmod 750 $BUFFER
cd $BUFFER
NOW=$(date +%s)
ssh $MAILCLIENT "echo 'This message is intended for Zabbix.' | mail -s $NOW $EMAILADDR"
echo $NOW >> sent.log
# clean up old messages
for mail in $(head -n-$(($MAILCOUNT * 3)) sent.log)
do
rm -f $mail
done
tail -n $(($MAILCOUNT * 3)) sent.log > sent.log.tmp
mv sent.log.tmp sent.log
# also check for unreceived mail, it has no use to put this in a seperate function, as this function already runs each minute
fetchmail
}
function process_mail() {
cd $BUFFER || exit 1
while IFS=- read timestamp index
do
[[ $index -le $LAST_RELAY ]] && MAILSTATS="${MAILSTATS}${RELAY[$index]}:$timestamp\n"
[[ $index -eq $SUBJECT ]] && MAILID=$timestamp
done <<< $(awk -v first_relay="$FIRST_RELAY" -v last_relay="$LAST_RELAY" -f $AWKSCRIPT /dev/stdin)
echo -e $MAILSTATS > $MAILID
}
function getmaster() {
cd $BUFFER || exit 1
# skip the last message, to prevent false positives
MAILS=$(tail -n $MAILCOUNT $BUFFER/sent.log | head -n-1)
MISSED=0
declare -a RELAY_SUM
# gather sum delay for all relay for all mails checked
for mail in $MAILS
do
test -f $mail || {
let "MISSED++"
continue
}
for ((i=$FIRST_RELAY; i<$LAST_RELAY; i++))
do
DELAY=$(grep ${RELAY[i]} $mail | cut -d: -f2)
test -z $DELAY && continue
RELAY_SUM[i]=$((${RELAY_SUM[i]} + $DELAY))
done
done
# calculate the average delay for each relay
for ((i=$FIRST_RELAY; i<$LAST_RELAY; i++))
do
echo ${RELAY[i]}:$((${RELAY_SUM[i]} / $(( $MAILCOUNT - $MISSED)) ))
done
echo missing:$MISSED
}
function sanity_check() {
ok=1
for prog in $SOFTWARE
do
which $prog > /dev/null || { echo "Missing program $prog."; ok=0; }
done
test -f $CONFIG || { echo "Missing configfile $CONFIG"; ok=0; }
test $ok -ne 1 && { echo "Sanity check failed. Aboring." ; exit 1; }
mkdir -p $BUFFER
}
sanity_check
action=$1
arg=$2
case "$action" in
mailflow.discover)
discover_mailflow
;;
mailflow.master)
getmaster
;;
process_mail)
process_mail
;;
send_mail)
send_mail
;;
esac