~ubuntu-branches/ubuntu/gutsy/munin/gutsy

« back to all changes in this revision

Viewing changes to node/node.d/postfix_mailvolume.in

  • Committer: Bazaar Package Importer
  • Author(s): Tore Anderson
  • Date: 2004-05-21 20:51:19 UTC
  • Revision ID: james.westby@ubuntu.com-20040521205119-oz8bllbjp9hs80ig
Tags: upstream-0+1.0.0pre5
ImportĀ upstreamĀ versionĀ 0+1.0.0pre5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!@@PERL@@ -w
 
2
#
 
3
# Plugin to monitor the volume of mails delivered by postfix.
 
4
#
 
5
# Usage: copy or link into /etc/munin/node.d/
 
6
#
 
7
# Parameters:
 
8
#
 
9
#       config   (required)
 
10
#       autoconf (optional - used by munin-config)
 
11
#
 
12
# Config variables:
 
13
#
 
14
#       logdir       - Which logfile to use
 
15
#       logfile      - What file to read in logdir
 
16
#
 
17
# $Log: postfix_mailvolume.in,v $
 
18
# Revision 1.1  2004/01/02 18:50:00  jimmyo
 
19
# Renamed occurrances of lrrd -> munin
 
20
#
 
21
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
 
22
# Import of LRRD CVS tree after renaming to Munin
 
23
#
 
24
# Revision 1.1  2003/11/26 14:45:34  jimmyo
 
25
# New plugins
 
26
#
 
27
# Revision 1.7  2003/11/15 11:10:28  jimmyo
 
28
# Various fixes
 
29
#
 
30
# Revision 1.6  2003/11/07 17:43:16  jimmyo
 
31
# Cleanups and log entries
 
32
#
 
33
#
 
34
#
 
35
# Magic markers (optional - used by munin-config and some installation
 
36
# scripts):
 
37
#
 
38
#%# family=contrib
 
39
#%# capabilities=autoconf
 
40
 
 
41
my $statefile = "@@PLUGSTATE@@/munin-plugin-postfix_mailvolume.state";
 
42
my $pos   = undef;
 
43
my $volume = 0;
 
44
my $LOGDIR  = $ENV{'logdir'}  || '/var/log';
 
45
my $LOGFILE = $ENV{'logfile'} || 'syslog';
 
46
 
 
47
 
 
48
if ( $ARGV[0] and $ARGV[0] eq "autoconf" )
 
49
{
 
50
    my $logfile;
 
51
    if (-d $LOGDIR)
 
52
    {
 
53
        $logfile = "$LOGDIR/$LOGFILE";
 
54
 
 
55
        if (-f $logfile)
 
56
        {
 
57
            if (-r "$logfile")
 
58
            {
 
59
                print "yes\n";
 
60
                exit 0;
 
61
            }
 
62
            else
 
63
            {
 
64
                print "no (logfile not readable)\n";
 
65
            }
 
66
        }
 
67
        else
 
68
        {
 
69
            print "no (logfile not found)\n";
 
70
        }
 
71
    }
 
72
    else
 
73
    {
 
74
        print "no (could not find logdir)\n";
 
75
    }
 
76
 
 
77
    exit 1;
 
78
}
 
79
 
 
80
my $logfile = "$LOGDIR/$LOGFILE";
 
81
my $prevdate = get_prev_date();
 
82
 
 
83
if (-f "$logfile.$prevdate")
 
84
{
 
85
    $rotlogfile = "$logfile.$prevdate";
 
86
}
 
87
elsif (-f "$logfile.0")
 
88
{
 
89
    $rotlogfile = $logfile . ".0";
 
90
}
 
91
elsif (-f "$logfile.1")
 
92
{
 
93
    $rotlogfile = $logfile . ".1";
 
94
}
 
95
elsif (-f "$logfile.01")
 
96
{
 
97
    $rotlogfile = $logfile . ".01";
 
98
}
 
99
else
 
100
{
 
101
    $rotlogfile = $logfile . ".0";
 
102
}
 
103
 
 
104
if (-f "$statefile")
 
105
{
 
106
    open (IN, "$statefile") or exit 4;
 
107
    if (<IN> =~ /^(\d+):(\d+)/)
 
108
    {
 
109
        ($pos, $volume) = ($1, $2);
 
110
    }
 
111
    close IN;
 
112
}
 
113
 
 
114
if (! -f $logfile and ! -f $rotlogfile)
 
115
{
 
116
    print "delivered.value U\n";
 
117
    exit 0;
 
118
}
 
119
 
 
120
 
 
121
$startsize = (stat $logfile)[7];
 
122
 
 
123
if (!defined $pos)
 
124
{
 
125
    # Initial run.
 
126
    $pos = $startsize;
 
127
}
 
128
 
 
129
if ($startsize < $pos)
 
130
{
 
131
    # Log rotated
 
132
    parseLogfile ($rotlogfile, $pos, (stat $rotlogfile)[7]);
 
133
    $pos = 0;
 
134
}
 
135
 
 
136
parseLogfile ($logfile, $pos, $startsize);
 
137
$pos = $startsize;
 
138
 
 
139
if ( $ARGV[0] and $ARGV[0] eq "config" )
 
140
{
 
141
    print "graph_title Postfix mail throughput\n";
 
142
    print "graph_args --base 1000 -l 0\n";
 
143
    print "graph_vlabel bytes / min\n";
 
144
    print "graph_scale  no\n";
 
145
    print "volume.label throughput\n";
 
146
    print "volume.type DERIVE\n";
 
147
    print "volume.cdef volume,60,*\n";
 
148
    print "volume.min 0\n";
 
149
    exit 0;
 
150
}
 
151
 
 
152
print "volume.value $volume\n";
 
153
 
 
154
if(-l $statefile) {
 
155
        die("$statefile is a symbolic link, refusing to touch it.");
 
156
}                               
 
157
open (OUT, ">$statefile") or exit 4;
 
158
print OUT "$pos:$volume\n";
 
159
close OUT;
 
160
 
 
161
sub parseLogfile 
 
162
{    
 
163
    my ($fname, $start, $stop) = @_;
 
164
    open (LOGFILE, $fname) or exit 3;
 
165
    seek (LOGFILE, $start, 0) or exit 2;
 
166
 
 
167
    while (tell (LOGFILE) < $stop) 
 
168
    {
 
169
        my $line =<LOGFILE>;
 
170
        chomp ($line);
 
171
 
 
172
        if ($line =~ /qmgr.*from=.*size=([0-9]+)/) 
 
173
        {
 
174
            $volume += $1;
 
175
        } 
 
176
    }
 
177
    close(LOGFILE);    
 
178
}
 
179
 
 
180
sub get_prev_date
 
181
{
 
182
        my @date = localtime (time - 86400);
 
183
        my $prevdate = $date[5]+1900 . $date[4]+1 . $date[3];
 
184
        return $prevdate;
 
185
}
 
186
 
 
187
# vim:syntax=perl