Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 rodolico 1
#! /usr/bin/perl -W
2
 
3
#
4
# $Netilium: fetch_mail_statistics.pl,v 1.6 2004/11/07 21:59:11 mad Exp $
5
#
6
# Calculate and return number of sent / received e-mails on a system
7
# running the Postfix MTA. This is done by incrementally reading the
8
# log file, in a ``tail''-like fashion.
9
#                                              7th of November, 2004
10
#                                          http://netilium.org/~mad/
11
#                                Martin Adolfsson <mad@netilium.org>
12
 
13
 
14
#
15
# This defines how many seconds we will cache calculated values.
16
#
17
$MINRESET = 290;
18
$SPAMLVL  = 2.0;
19
 
20
if ( @ARGV < 5 ) {
21
print <<EOF
22
usage: fetch_mail_statistics.pl <maillog> <db file> <base oid> <request type> <requested oid>
23
 
24
 where:
25
   <maillog>         Mail log file, in Postfix compatible format.
26
   <db file>         File for logging statistics and timestamp data.
27
   <base oid>        The base OID this script is mounted on.
28
   <request type>    -n for SNMP getNext, or -g for SNMP get.
29
   <request oid>     Requested OID, in accordance with previous parameter
30
 
31
Report bugs to: <mad\@netilium.org>
32
EOF
33
;
34
exit;
35
}
36
 
37
$logFile  = $ARGV[0];
38
$dbFile   = $ARGV[1];
39
$baseOid  = $ARGV[2];
40
 
41
(@lf = stat $logFile) || die( "Cannot stat log file `".$logFile."'!" );
42
@st = stat $dbFile;
43
 
44
if ( ! @st ) {
45
  $timeSinceUpdated = 99999999;
46
  $logPosition      = $lf[7];
47
} else {
48
  $timeSinceUpdated = time()-$st[9];
49
  open( DBFILE, $dbFile ) || die( "Cannot open db file `".$dbFile."'!" );
50
  $logPosition=<DBFILE>;
51
  close( DBFILE );
52
  if ( $logPosition > $lf[7] ) {
53
    # Log has been truncated since last run.
54
    $logPosition = 0;
55
  }
56
}
57
 
58
if ( $timeSinceUpdated > $MINRESET ) {
59
  # Calculate and store new data.
60
  open( LOGFILE, $logFile ) || die( "Cannot open log file `".$logFile."'!" );
61
  seek( LOGFILE, $logPosition, 0 );
62
  $num_rec = 0;
63
  $num_sent = 0;
64
  $num_deferred = 0;
65
  $num_bounced = 0;
66
  $num_reject = 0;
67
  $num_clean = 0;
68
  $num_pspam = 0;
69
  $num_spam = 0;
70
  $num_infected = 0;
71
  $num_header = 0;
72
  $num_banned = 0;
73
 
74
  while( <LOGFILE> ) {
75
      # modified by RWR since all traffic goes through localhost
76
      #if ( / postfix\/smtpd/ && /client=/ && ! /127.0.0.1/ ) { $num_rec++; }
77
      if ( / postfix\/smtpd/ && /client=/ ) { $num_rec++; }
78
      elsif ( /status=sent/ && ! /relay=(mailfilter|procmail|local|127.0.0.1)/ ) { $num_sent++; }
79
      elsif ( /status=deferred/ ) { $num_deferred++; }
80
      elsif ( /status=bounced/ ) { $num_bounced++; $num_reject++; }
81
      elsif ( / REFUSED and GREYLISTED/ ) { $num_pspam++; }
82
      elsif ( / 554 5.7.1/ ) { $num_banned++; $num_reject++; }
83
      elsif ( /: NOQUEUE: reject: RCPT / ) { $num_spam++; $num_reject++; }
84
      elsif ( / Passed CLEAN/  && /Hits: ([-+]?[0-9]*\.?[0-9]+)/) {
85
          $hits = $1;
86
          $num_clean++ if ($hits < $SPAMLVL);
87
          $num_pspam++ if ($hits > $SPAMLVL);
88
      }
89
 
90
      elsif ( / Not-Delivered CLEAN/  && /Hits: ([-+]?[0-9]*\.?[0-9]+)/) {
91
          $hits = $1;
92
          $num_clean++ if ($hits < $SPAMLVL);
93
          $num_spam++ if ($hits > $SPAMLVL);
94
      }
95
 
96
      elsif ( / INFECTED/ ) { $num_infected++; $num_reject++; }
97
      elsif ( / Passed BAD-HEADER/ ) { $num_header++; }
98
      elsif ( / Not-Delivered BAD-HEADER/ ) { $num_header++; $num_reject++; }
99
      elsif ( / BANNED/ ) { $num_banned++; $num_reject++; }
100
      elsif ( / SPAM/ ) { $num_spam++; $num_reject++; }
101
 
102
 
103
  }
104
 
105
  open( DBFILE, ">".$dbFile ) || die( "Cannot write to db file `".$dbFile."'!" );
106
  print DBFILE tell(LOGFILE)."\n";
107
  print DBFILE $num_rec."\n";
108
  print DBFILE $num_sent."\n";
109
  print DBFILE $num_deferred."\n";
110
  print DBFILE $num_bounced."\n";
111
  print DBFILE $num_reject."\n";
112
  print DBFILE $num_clean."\n";
113
  print DBFILE $num_pspam."\n";
114
  print DBFILE $num_spam."\n";
115
  print DBFILE $num_infected."\n";
116
  print DBFILE $num_header."\n";
117
  print DBFILE $num_banned."\n";
118
  close( DBFILE );
119
}
120
 
121
# Return archived data.
122
open( DBFILE, $dbFile ) || die( "Cannot open db file `".$dbFile."'!" );
123
@data = ();
124
while( <DBFILE> ) { chomp; push @data, $_; }
125
 
126
$reqMethod = $ARGV[3];
127
$reqOid    = $ARGV[4];
128
 
129
if ( $reqMethod eq "-n" ) {
130
  # SNMP getNext request.
131
  if ( $reqOid eq $baseOid ) {
132
    print $baseOid.".0\ninteger\n".$data[1]."\n";
133
  } elsif ( $reqOid eq $baseOid.".0" ) {
134
    print $baseOid.".1\ninteger\n".$data[2]."\n";
135
  } elsif ( $reqOid eq $baseOid.".1" ) {
136
    print $baseOid.".2\ninteger\n".$data[3]."\n";
137
  } elsif ( $reqOid eq $baseOid.".2" ) {
138
    print $baseOid.".3\ninteger\n".$data[4]."\n";
139
  } elsif ( $reqOid eq $baseOid.".3" ) {
140
    print $baseOid.".4\ninteger\n".$data[5]."\n";
141
  } elsif ( $reqOid eq $baseOid.".4" ) {
142
    print $baseOid.".5\ninteger\n".$data[6]."\n";
143
  } elsif ( $reqOid eq $baseOid.".5" ) {
144
    print $baseOid.".6\ninteger\n".$data[7]."\n";
145
  } elsif ( $reqOid eq $baseOid.".6" ) {
146
    print $baseOid.".7\ninteger\n".$data[8]."\n";
147
  } elsif ( $reqOid eq $baseOid.".7" ) {
148
    print $baseOid.".8\ninteger\n".$data[9]."\n";
149
  } elsif ( $reqOid eq $baseOid.".8" ) {
150
    print $baseOid.".9\ninteger\n".$data[10]."\n";
151
  } elsif ( $reqOid eq $baseOid.".9" ) {
152
    print $baseOid.".10\ninteger\n".$data[11]."\n";
153
  }
154
} elsif ( $reqMethod eq "-g" ) {
155
  # SNMP get request.
156
  if ( $reqOid eq $baseOid || $reqOid eq $baseOid.".0" ) {
157
    print $baseOid.".0\ninteger\n".$data[1]."\n";
158
  } elsif( $reqOid eq $baseOid.".1" ) {
159
    print $baseOid.".1\ninteger\n".$data[2]."\n";
160
  } elsif( $reqOid eq $baseOid.".2" ) {
161
    print $baseOid.".2\ninteger\n".$data[3]."\n";
162
  } elsif( $reqOid eq $baseOid.".3" ) {
163
    print $baseOid.".3\ninteger\n".$data[4]."\n";
164
  } elsif( $reqOid eq $baseOid.".4" ) {
165
    print $baseOid.".4\ninteger\n".$data[5]."\n";
166
  } elsif( $reqOid eq $baseOid.".5" ) {
167
    print $baseOid.".5\ninteger\n".$data[6]."\n";
168
  } elsif( $reqOid eq $baseOid.".6" ) {
169
    print $baseOid.".6\ninteger\n".$data[7]."\n";
170
  } elsif( $reqOid eq $baseOid.".7" ) {
171
    print $baseOid.".7\ninteger\n".$data[8]."\n";
172
  } elsif( $reqOid eq $baseOid.".8" ) {
173
    print $baseOid.".8\ninteger\n".$data[9]."\n";
174
  } elsif( $reqOid eq $baseOid.".9" ) {
175
    print $baseOid.".9\ninteger\n".$data[10]."\n";
176
  } elsif( $reqOid eq $baseOid.".10" ) {
177
    print $baseOid.".10\ninteger\n".$data[11]."\n";
178
  }
179
}
180