~ubuntu-branches/ubuntu/lucid/munin/lucid

« back to all changes in this revision

Viewing changes to plugins/node.d/courier_mta_mailstats.in

  • Committer: Bazaar Package Importer
  • Author(s): Holger Levsen, Stig Sandbeck Mathisen, Tom Feiner
  • Date: 2010-01-14 12:10:51 UTC
  • mfrom: (8.1.13 sid)
  • Revision ID: james.westby@ubuntu.com-20100114121051-6xovy6hqfh1wrl0u
Tags: 1.4.3-2
[ Stig Sandbeck Mathisen ]
* Add versioned dependency for librrds-perl.
  If used with librrds-perl 1.2 or older, the font path is wrong.

[ Tom Feiner ]
* Update watch file.
* Add patch from munin ticket #828, to suppress "occasional" unknown 
  states to avoid alerts. Thanks to Steve Wilson for the patch!
* Removed asterisks from NEWS.Debian and rewrite as non bulleted list, as
  advised by the developers reference.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!@@PERL@@ -w
 
2
# -*- perl -*-
 
3
 
 
4
=head1 NAME
 
5
 
 
6
courier_mta_mailstats - Plugin to monitor the number of mails
 
7
delivered and rejected by courier-mta.
 
8
 
 
9
=head1 CONFIGURATION
 
10
 
 
11
The following environment variables are used:
 
12
 
 
13
 logdir  - Where to find logfiles (default "/var/log")
 
14
 logfile - What file to read in logdir (default "mail.log")
 
15
 courier - Path to "courier" (default "/usr/sbin/courier")
 
16
 
 
17
=head1 AUTHOR
 
18
 
 
19
Rune Nordb�e Skillingstad <runesk@linpro.no>
 
20
 
 
21
=head1 LICENSE
 
22
 
 
23
Unknown
 
24
 
 
25
=head1 MAGIC MARKERS
 
26
 
 
27
 #%# family=auto
 
28
 #%# capabilities=autoconf
 
29
 
 
30
=cut
 
31
 
 
32
my $statefile = "@@PLUGSTATE@@/munin-plugin-courier_mta_mailstats.state";
 
33
my $pos   = undef;
 
34
my $delivered = 0;
 
35
my $rejects = {};
 
36
my $LOGDIR  = $ENV{'logdir'}  || '/var/log';
 
37
my $LOGFILE = $ENV{'logfile'} || 'mail.log';
 
38
my $COURIER = $ENV{'courier'} || '/usr/sbin/courier';
 
39
 
 
40
if ( $ARGV[0] and $ARGV[0] eq "autoconf" )
 
41
{
 
42
    my $logfile;
 
43
    if(-x $COURIER) {
 
44
        if(-d $LOGDIR) {
 
45
            $logfile = "$LOGDIR/$LOGFILE";
 
46
            if(-f $logfile) {
 
47
                if(-r "$logfile") {
 
48
                    print "yes\n";
 
49
                    exit 0;
 
50
                } else {
 
51
                    print "no (logfile not readable)\n";
 
52
                }
 
53
            } else {
 
54
                print "no (logfile not found)\n";
 
55
            }
 
56
        } else {
 
57
            print "no (could not find logdir)\n";
 
58
        }
 
59
    } else {
 
60
        print "no (could not find executable)\n";
 
61
    }
 
62
    exit 0;
 
63
}
 
64
 
 
65
my $logfile = "$LOGDIR/$LOGFILE";
 
66
my $prevdate = get_prev_date();
 
67
 
 
68
if(-f "$logfile.$prevdate") {
 
69
    $rotlogfile = "$logfile.$prevdate";
 
70
} elsif (-f "$logfile.0") {
 
71
    $rotlogfile = $logfile . ".0";
 
72
} elsif (-f "$logfile.1") {
 
73
    $rotlogfile = $logfile . ".1";
 
74
} elsif (-f "$logfile.01") {
 
75
    $rotlogfile = $logfile . ".01";
 
76
} else {
 
77
    $rotlogfile = $logfile . ".0";
 
78
}
 
79
 
 
80
if(-f "$statefile") {
 
81
    open (IN, "$statefile") or exit 4;
 
82
    if(<IN> =~ /^(\d+):(\d+)/) {
 
83
        ($pos, $delivered) = ($1, $2);
 
84
    }
 
85
    while (<IN>) {
 
86
        if (/^(\d+):(\d+)$/) {
 
87
            $rejects->{$1} = $2;
 
88
        }
 
89
    }
 
90
    close IN;
 
91
}
 
92
 
 
93
if(! -f $logfile and ! -f $rotlogfile) {
 
94
    print "delivered.value U\n";
 
95
    foreach my $i (keys %{$rejects}) {
 
96
        print "r$i.value U\n";
 
97
    }
 
98
    exit 0;
 
99
}
 
100
 
 
101
 
 
102
$startsize = (stat $logfile)[7];
 
103
 
 
104
if(!defined $pos) {
 
105
    # Initial run.
 
106
    $pos = $startsize;
 
107
}
 
108
 
 
109
if($startsize < $pos) {
 
110
    # Log rotated
 
111
    parseLogfile ($rotlogfile, $pos, (stat $rotlogfile)[7]);
 
112
    $pos = 0;
 
113
}
 
114
 
 
115
parseLogfile ($logfile, $pos, $startsize);
 
116
$pos = $startsize;
 
117
 
 
118
if($ARGV[0] and $ARGV[0] eq "config") {
 
119
    print "graph_title Courier MTA mail throughput\n";
 
120
    print "graph_args --base 1000 -l 0\n";
 
121
    print "graph_vlabel mails / \${graph_period}\n";
 
122
    print "graph_scale  no\n";
 
123
    print "graph_total  Total\n";
 
124
    print "delivered.label delivered\n";
 
125
    print "delivered.type DERIVE\n";
 
126
    print "delivered.draw AREA\n";
 
127
    print "delivered.min 0\n";
 
128
    foreach my $i (keys %{$rejects}) {
 
129
            print "r$i.label reject $i\n";
 
130
            print "r$i.type DERIVE\n";
 
131
            print "r$i.draw STACK\n";
 
132
            print "r$i.min 0\n";
 
133
    }
 
134
    exit 0;
 
135
}
 
136
 
 
137
print "delivered.value $delivered\n";
 
138
foreach my $i (keys %{$rejects}) {
 
139
        print "r$i.value ", $rejects->{$i}, "\n";
 
140
}
 
141
 
 
142
if(-l $statefile) {
 
143
    die("$statefile is a symbolic link, refusing to touch it.");
 
144
}                               
 
145
open (OUT, ">$statefile") or exit 4;
 
146
print OUT "$pos:$delivered\n";
 
147
foreach my $i (keys %{$rejects}) {
 
148
    print OUT "$i:", $rejects->{$i}, "\n";
 
149
}
 
150
close OUT;
 
151
 
 
152
sub parseLogfile {    
 
153
    my ($fname, $start, $stop) = @_;
 
154
    open (LOGFILE, $fname) or exit 3;
 
155
    seek (LOGFILE, $start, 0) or exit 2;
 
156
    
 
157
    while (tell (LOGFILE) < $stop) {
 
158
        my $line =<LOGFILE>;
 
159
        chomp ($line);
 
160
        if($line =~ /(?:courierlocal|courieresmtp): id=.*,from=.*,addr=.*,size=\d+,success:.*delivered/) {
 
161
            $delivered++;
 
162
        } elsif($line =~ /courieresmtpd: error,relay=.*(?:\d{1,3}\.){3}\d{1,3},from=[^:]+: (\d{3})/) {
 
163
            $rejects->{$1} ++;
 
164
        }
 
165
    }
 
166
    close(LOGFILE);    
 
167
}
 
168
 
 
169
sub get_prev_date {
 
170
        my @date = localtime (time - 86400);
 
171
        my $prevdate = $date[5]+1900 . $date[4]+1 . $date[3];
 
172
        return $prevdate;
 
173
}