Projet

Général

Profil

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

root / plugins / postfix / postfix_mailvolume_multi @ 95003946

Historique | Voir | Annoter | Télécharger (4,44 ko)

1
#!/usr/bin/perl -w
2
# -*- perl -*-
3

    
4
=head1 NAME
5

    
6
postfix_mailvolume - Plugin to monitor the volume of mails delivered
7
  by multiple postfix and stores per postfix delivered data.
8

    
9
=head1 APPLICABLE SYSTEMS
10

    
11
Any postfix.
12

    
13
=head1 CONFIGURATION
14

    
15
The following shows the default configuration.
16

    
17
  [postfix*]
18
    env.logdir /var/log
19
    env.logfile syslog
20

    
21
=head2 Needed additional configuration
22

    
23
To correctly get all the postfix log data, the postfix system_log prefix names need to be defined with the env.postfix config setting.
24
If this is not set, the script tries to find all the postfix config folders in /etc/postfix* and get the syslog names from there
25

    
26
	env.postfix postfix10 postfix11 postfix12
27

    
28
=head1 INTERPRETATION
29

    
30
The plugin shows the number of bytes of mail that has passed through
31
the postfix installation per postfix mailer running.
32

    
33
=head1 MAGIC MARKERS
34

    
35
  #%# family=auto
36
  #%# capabilities=autoconf
37

    
38
=head1 BUGS
39

    
40
None known
41

    
42
=head1 VERSION
43

    
44
 $Id: postfix_mailvolume.in 2314 2009-08-03 11:28:34Z ssm $
45

    
46
=head1 AUTHOR
47

    
48
Copyright (C) 2011.
49

    
50
Clemens Schwaighofer (gullevek@gullevek.org)
51

    
52
=head1 LICENSE
53

    
54
GPLv2
55

    
56
=cut
57

    
58
use strict;
59
use Munin::Plugin;
60

    
61
my $pos = undef;
62
my $syslog_name = '';
63
my @postfix_syslog_name = ();
64
my %volume = ();
65
my @restore_state = ();
66
my $i = 1;
67
my $LOGDIR  = $ENV{'logdir'}  || '/var/log';
68
my $LOGFILE = $ENV{'logfile'} || 'syslog';
69
my $POSTFIX = $ENV{'postfix'} || '';
70
# get the postfix syslog_name from the POSTFIX env var, if not set, find them in the /etc/postfix* type
71
if (!$POSTFIX)
72
{
73
	foreach my $dir (grep -d, glob "/etc/postfix*")
74
	{
75
		# remove the leading etc
76
		$dir =~ s/\/etc\///g;
77
		# add data to the postfix string
78
		$POSTFIX .= ' ' if ($POSTFIX);
79
		$POSTFIX .= $dir;
80
	}
81
}
82
if ($POSTFIX)
83
{
84
	foreach my $config (split(/ /, $POSTFIX))
85
	{
86
		# find the syslog name
87
		$syslog_name = `postconf -c /etc/$config | grep "syslog_name"`;
88
		# remove any pending whitespace or line breaks
89
		chomp($syslog_name);
90
		$syslog_name =~ s/syslog_name = //g;
91
		# add this to the postfix syslog name array
92
		push(@postfix_syslog_name, $syslog_name);
93
		# also init set the syslog name 0
94
		$volume{$syslog_name} = 0;
95
	}
96
}
97
else
98
{
99
	print "Cannot get any postfix syslog_name data\n";
100
	exit 1;
101
}
102

    
103
sub parseLogfile
104
{
105
	my ($fname, $start) = @_;
106

    
107
	my ($LOGFILE, $rotated) = tail_open($fname, $start);
108

    
109
	my $line;
110

    
111
	while ($line =<$LOGFILE>)
112
	{
113
		chomp ($line);
114
		# get the postfix syslog name and the size
115
		if ($line =~ /\ ([\d\w\-]+)\/qmgr.*from=.*size=([0-9]+)/)
116
		{
117
			$volume{$1} += $2;
118
		}
119
	}
120
	return tail_close($LOGFILE);
121
}
122

    
123
if ($ARGV[0] and $ARGV[0] eq "autoconf")
124
{
125
	my $logfile;
126
	`which postconf >/dev/null 2>/dev/null`;
127
	if (!$?)
128
	{
129
		$logfile = "$LOGDIR/$LOGFILE";
130

    
131
		if (-f $logfile)
132
		{
133
			if (-r "$logfile")
134
			{
135
				print "yes\n";
136
			}
137
			else
138
			{
139
				print "no (logfile '$logfile' not readable)\n";
140
			}
141
		}
142
		else
143
		{
144
			print "no (logfile '$logfile' not found)\n";
145
		}
146
	}
147
	else
148
	{
149
		print "no (postfix not found)\n";
150
	}
151
	exit 0;
152
}
153

    
154
if ($ARGV[0] and $ARGV[0] eq "config")
155
{
156
	print "graph_title Postfix bytes throughput per postfix\n";
157
	print "graph_args --base 1000 -l 0\n";
158
	print "graph_vlabel bytes / \${graph_period}\n";
159
	print "graph_scale yes\n";
160
	print "graph_category mail\n";
161
	print "graph_total Throughput sum\n";
162
	# loop through the postfix names and create per config an entry
163
	foreach $syslog_name (@postfix_syslog_name)
164
	{
165
		print $syslog_name."_volume.label ".$syslog_name." throughput\n";
166
		print $syslog_name."_volume.type DERIVE\n";
167
		print $syslog_name."_volume.min 0\n";
168
	}
169
	exit 0;
170
}
171

    
172

    
173
my $logfile = "$LOGDIR/$LOGFILE";
174

    
175
if (! -f $logfile) {
176
    print "delivered.value U\n";
177
    exit 1;
178
}
179

    
180
@restore_state = restore_state();
181
# first is pos, rest is postfix entries
182
$pos = $restore_state[0];
183
# per postfix values are store: postfix config,value
184
for ($i = 1; $i < @restore_state; $i ++)
185
{
186
	my ($key, $value) = split(/,/, $restore_state[$i]);
187
	$volume{$key} = $value;
188
}
189

    
190
if (!$pos)
191
{
192
	# No state file present.  Avoid startup spike: Do not read log
193
	# file up to now, but remember how large it is now, and next
194
	# time read from there.
195

    
196
	$pos = (stat $logfile)[7]; # File size
197
	foreach $syslog_name (@postfix_syslog_name)
198
	{
199
		$volume{$syslog_name} = 0;
200
	}
201
}
202
else
203
{
204
    $pos = parseLogfile($logfile, $pos);
205
}
206

    
207
@restore_state = ($pos);
208
foreach $syslog_name (sort keys %volume)
209
{
210
	print $syslog_name."_volume.value ".$volume{$syslog_name}."\n";
211
	push(@restore_state, $syslog_name.','.$volume{$syslog_name});
212
}
213

    
214
# save the current state
215
save_state(@restore_state);
216

    
217
# vim:syntax=perl
218

    
219
__END__