Initial import
[manu/munin-amavisd-new.git] / amavisd-new
1 #!/bin/sh
2
3 # Name: amavisd-new
4 # Description: Munin plugin to monitor amavisd-new mail-filter
5 #              Based on a plugin authored by Fili Wiese that may be found on
6 #              http://muninexchange.projects.linpro.no/ under the name
7 #              amavis-debian with some addition to show all type of amavis
8 #              mails statuses: Total, Clean, Spammy, spam, Infected, Banned,
9 #              Bad-header and other
10 #
11 # Parameters understood:
12 #       config   (required)
13 #       autoconf (optional)
14 #
15 # Config variables:
16 #   AMAVIS_LOG  - file where amavis logs are written
17 #       STATEFILE       - file which is needed to keep track of AMAVIS_LOG
18 #       LOGTAIL         - location of logtail
19 #       BC              - location of bc
20 #
21 # Copyright (C) 2010 Emmanuel Lacour <elacour@home-dn.net>
22 #
23 #  This program is free software; you can redistribute it and/or modify
24 #  it under the terms of the GNU General Public License as published by
25 #  the Free Software Foundation; either version 2 of the License, or
26 #  (at your option) any later version.
27
28 #  This program is distributed in the hope that it will be useful,
29 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
30 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31 #  GNU General Public License for more details.
32
33 #  You should have received a copy of the GNU General Public License
34 #  along with this program; if not, write to the Free Software
35 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
36 #  USA.
37 #
38
39 AMAVIS_LOG=${logfile:-/var/log/mail.log}
40 STATEFILE=/var/lib/munin/plugin-state/amavisd-new.offset
41 LOGTAIL=${logtail:-`which logtail`}
42 BC=${bc:-`which bc`}
43
44 mktempfile () {
45         mktemp
46 }       
47
48 if [ "$1" = "autoconf" ]; then
49         if [ -f "${AMAVIS_LOG}" -a -n "${LOGTAIL}" -a -x "${LOGTAIL}" -a -n "${BC}" -a -x "${BC}" ] ; then
50                 echo yes
51                 exit 0
52         else
53                 echo no
54                 exit 1
55         fi
56 fi
57
58 if [ "$1" = "config" ]; then
59         echo 'graph_title Amavisd-new statistics'
60         echo 'graph_category amavis'
61         echo 'graph_order total clean spammy spam infected banned badh other'
62         echo 'graph_vlabel Mails'
63         echo 'graph_scale no'
64         echo 'total.info Total count of emails'
65         echo 'total.label Total'
66         echo 'total.draw AREA'
67         echo 'clean.info Count of clean emails'
68         echo 'clean.label Clean'
69         echo 'clean.draw LINE1'
70         echo 'spammy.info Count of spams emails (tag2 level)'
71         echo 'spammy.label Spammy'
72         echo 'spammy.draw LINE1'
73         echo 'spam.info Count of spams emails (kill level)'
74         echo 'spam.label Spam'
75         echo 'spam.draw LINE1'
76         echo 'infected.info Count of infected emails (virus)'
77         echo 'infected.label Infected'
78         echo 'infected.draw LINE1'
79         echo 'banned.info Count of emails with banned files'
80         echo 'banned.label Banned'
81         echo 'banned.draw LINE1'
82         echo 'badh.info Count of emails with bad headers'
83         echo 'badh.label Bad-header'
84         echo 'badh.draw LINE1'
85         echo 'other.info Count of emails that dont fit other categories'
86         echo 'other.label Others'
87         echo 'other.draw LINE1'
88         exit 0
89 fi
90
91 clean=0
92 infected=0
93 spammy=0
94 spam=0
95 banned=0
96 badh=0
97 other=0
98 total=0
99
100 ARGS=0
101 `$LOGTAIL -f /etc/hosts 2>/dev/null >/dev/null`
102 if [ $? = 66 ]; then
103     if [ ! -n "$logtail" ]; then
104         ARGS=1
105     fi
106 fi
107
108 TEMP_FILE=$(mktempfile munin-amavisd-new.XXXXXX)
109
110 if [ -n "$TEMP_FILE" -a -f "$TEMP_FILE" ]
111 then
112         if [ $ARGS != 0 ]; then
113             $LOGTAIL -f ${AMAVIS_LOG} -o $STATEFILE | grep 'amavis\[.*\]:' | grep -v 'TIMED OUT' > ${TEMP_FILE}
114         else
115             $LOGTAIL -f ${AMAVIS_LOG} -o $STATEFILE | grep 'amavis\[.*\]:' | grep -v 'TIMED OUT' > ${TEMP_FILE}
116         fi
117         total=$(wc -l < ${TEMP_FILE})
118         clean=$(grep -Ec '(Passed|Blocked) CLEAN' ${TEMP_FILE})
119         infected=$(grep -Ec '(Passed|Blocked) INFECTED' ${TEMP_FILE})
120         spammy=$(grep -Ec '(Passed|Blocked) SPAMMY' ${TEMP_FILE})
121         spam=$(grep -E '(Passed|Blocked) SPAM' ${TEMP_FILE} | grep -Ev '(Passed|Blocked) SPAMMY' | wc -l)
122         banned=$(grep -Ec '(Passed|Blocked) BANNED' ${TEMP_FILE})
123         badh=$(grep -Ec '(Passed|Blocked) BAD-HEADER' ${TEMP_FILE})
124         other=$(echo ${total}-${clean}-${infected}-${spammy}-${spam}-${banned}-${badh} | ${BC})
125         
126         /bin/rm -f $TEMP_FILE
127 fi
128
129
130 echo "clean.value ${clean}"
131 echo "infected.value ${infected}"
132 echo "spammy.value ${spammy}"
133 echo "spam.value ${spam}"
134 echo "banned.value ${banned}"
135 echo "badh.value ${badh}"
136 echo "other.value ${other}"
137 echo "total.value ${total}"
138
139