Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / postfix / postfix_mailstats @ 1fe97cbf

Historique | Voir | Annoter | Télécharger (3,32 ko)

1
#! /usr/bin/perl
2

    
3
#  Copyright (C) 2008 Joey Schulze <joey@infodrom.org>
4
#
5
#  This program is free software; you can redistribute it and/or modify
6
#  it under the terms of the GNU General Public License as published by
7
#  the Free Software Foundation; version 2 dated June, 1991.
8
#
9
#  This program is distributed in the hope that it will be useful,
10
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
#  GNU General Public License for more details.
13
#
14
#  You should have received a copy of the GNU General Public License
15
#  along with this program;  if not, write to the Free Software
16
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17

    
18
# Rewrite of the postfix_mailstats plugin with Munin::Plugin
19

    
20
# Source: http://www.infodrom.org/Infodrom/tools/munin.html
21

    
22
# Supported configuration:
23
#
24
#   [postfix_mailstats]
25
#   user root
26
#   group adm
27
#   env.logdir /var/log
28
#   env.logfile mail.log
29

    
30
use strict;
31
use warnings;
32

    
33
use Munin::Plugin;
34

    
35
my $LOGDIR  = $ENV{'logdir'}  || '/var/log';
36
my $LOGFILE = $ENV{'logfile'} || 'mail.log';
37
my $logfile = $LOGDIR .'/'. $LOGFILE;
38

    
39
my $delivered;
40
my %rejects;
41

    
42
sub autoconf
43
{
44
    if (-d $LOGDIR) {
45
	if (-f $logfile) {
46
	    if (open(my $f, $logfile)) {
47
		while (<$f>) {
48
		    if (m,postfix/smtpd\[\d+\]: ,) {
49
			close $f;
50
			print "yes\n";
51
			exit 0;
52
		    }
53
		}
54
		print "no (no smtpd logs in logfile)\n";
55
		close $f;
56
	    } else {
57
		print "no (logfile not readable)\n";
58
	    }
59
	} else {
60
	    print "no (logfile not found)\n";
61
	}
62
    } else {
63
	print "no (could not find logdir)\n";
64
    }
65
    exit 1;
66
}
67

    
68
sub config
69
{
70
    print "graph_title Postfix message throughput\n";
71
    print "graph_args --base 1000 -l 0\n";
72
    print "graph_vlabel mails / \${graph_period}\n";
73
    print "graph_scale  no\n";
74
    print "graph_total  Total\n";
75
    print "graph_category mail\n";
76
    print "delivered.label delivered\n";
77
    print "delivered.type ABSOLUTE\n";
78
    print "delivered.draw AREA\n";
79
    print "delivered.min 0\n";
80
    foreach my $code (sort keys %rejects) {
81
	my $name = clean_fieldname('reject ' . $code);
82
	printf "%s.label reject %d\n", $name, $code;
83
	printf "%s.type ABSOLUTE\n", $name;
84
	printf "%s.draw STACK\n", $name;
85
	printf "%s.min 0\n", $name;
86
    }
87
    exit 0;
88
}
89

    
90
sub parse
91
{
92
    my $logfile = shift;
93
    my $pos = shift;
94

    
95
    my ($log,$rotated) = tail_open $logfile, $pos;
96

    
97
    while (<$log>) {
98
	if (m,.* postfix/smtpd\[\d+\]: NOQUEUE: reject: [^:]+: (\d+) .*,) {
99
	    $rejects{$1}++;
100
	    next;
101
	} elsif (m/qmgr.*from=.*size=[0-9]*/) {
102
	    $delivered++;
103
	    next;
104
	}
105
    }
106
    return tail_close $log;
107
}
108

    
109
autoconf if $#ARGV > -1 && $ARGV[0] eq "autoconf";
110

    
111
my @state_vector = restore_state;
112
my $pos = shift @state_vector || 0;
113
my $time = shift @state_vector;
114
$delivered = shift @state_vector || 0;
115
%rejects = @state_vector;
116

    
117
config if $#ARGV > -1 && $ARGV[0] eq "config";
118

    
119
if (defined $time) {
120
    if ($time < time-(60*5)+15) {
121
	$delivered = 0;
122
	$rejects{$_} = 0 foreach keys %rejects;
123
	$pos = parse $logfile, $pos;
124
	save_state $pos, time, $delivered, %rejects;
125
    }
126
} else {
127
    # Prevent start peak
128
    $pos = (stat $logfile)[7];
129
    save_state $pos, time, $delivered;
130
    exit 0;
131
}
132

    
133
printf "delivered.value %d\n", $delivered;
134
foreach my $code (sort keys %rejects) {
135
    printf "%s.value %d\n", clean_fieldname('reject ' . $code), $rejects{$code};
136
}