Projet

Général

Profil

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

root / plugins / postfix / postfix_mailvolume_multi @ 17f78427

Historique | Voir | Annoter | Télécharger (4,45 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
				exit 0;
137
			}
138
			else
139
			{
140
				print "no (logfile '$logfile' not readable)\n";
141
			}
142
		}
143
		else
144
		{
145
			print "no (logfile '$logfile' not found)\n";
146
		}
147
	}
148
	else
149
	{
150
		print "no (postfix not found)\n";
151
	}
152
	exit 0;
153
}
154

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

    
173

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

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

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

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

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

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

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

    
218
# vim:syntax=perl
219

    
220
__END__