Subversion Repositories sysadmin_scripts

Rev

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
if ( ! @st ) {
44
  $timeSinceUpdated = 99999999;
45
  $logPosition      = $lf[7];  
46
} else {
47
  $timeSinceUpdated = time()-$st[9];
48
  open( DBFILE, $dbFile ) || die( "Cannot open db file `".$dbFile."'!" );
49
  $logPosition=<DBFILE>;
50
  close( DBFILE );
51
  if ( $logPosition > $lf[7] ) {
52
    # Log has been truncated since last run.
53
    $logPosition = 0;
54
  }
55
}  
56
 
57
if ( $timeSinceUpdated > $MINRESET ) {
58
  # Calculate and store new data.
59
  open( LOGFILE, $logFile ) || die( "Cannot open log file `".$logFile."'!" );
60
  seek( LOGFILE, $logPosition, 0 );
61
  $num_rec = 0;
62
  $num_sent = 0;
63
  $num_deferred = 0;
64
  $num_bounced = 0; 
65
  $num_reject = 0;  
66
  $num_clean = 0;   
67
  $num_pspam = 0;   
68
  $num_spam = 0;    
69
  $num_infected = 0;
70
  $num_header = 0;  
71
  $num_banned = 0;  
72
  while( <LOGFILE> ) {
73
      if ( / postfix\/smtpd/ && /client=/ && ! /127.0.0.1/ ) { $num_rec++; }
74
      elsif ( /status=sent/ && ! /relay=(mailfilter|procmail|local|127.0.0.1)/ ) { $num_sent++; }
75
      elsif ( /status=deferred/ ) { $num_deferred++; }
76
      elsif ( /status=bounced/ ) { $num_bounced++; $num_reject++; }
77
      elsif ( / REFUSED and GREYLISTED/ ) { $num_pspam++; }
78
      elsif ( / 554 5.7.1/ ) { $num_banned++; $num_reject++; }
79
      elsif ( /: NOQUEUE: reject: RCPT / ) { $num_spam++; $num_reject++; }
80
      elsif ( / Passed CLEAN/  && /Hits: ([-+]?[0-9]*\.?[0-9]+)/) {
81
          $hits = $1;
82
          $num_clean++ if ($hits < $SPAMLVL);
83
          $num_pspam++ if ($hits > $SPAMLVL);
84
      }
85
 
86
      elsif ( / Not-Delivered CLEAN/  && /Hits: ([-+]?[0-9]*\.?[0-9]+)/) {
87
          $hits = $1;
88
          $num_clean++ if ($hits < $SPAMLVL);
89
          $num_spam++ if ($hits > $SPAMLVL); 
90
      }
91
 
92
      elsif ( / INFECTED/ ) { $num_infected++; $num_reject++; }
93
      elsif ( / Passed BAD-HEADER/ ) { $num_header++; }
94
      elsif ( / Not-Delivered BAD-HEADER/ ) { $num_header++; $num_reject++; }
95
      elsif ( / BANNED/ ) { $num_banned++; $num_reject++; }
96
      elsif ( / SPAM/ ) { $num_spam++; $num_reject++; }
97
 
98
 
99
  }
100
 
101
  open( DBFILE, ">".$dbFile ) || die( "Cannot write to db file `".$dbFile."'!" );
102
  print DBFILE tell(LOGFILE)."\n";
103
  print DBFILE $num_rec."\n";
104
  print DBFILE $num_sent."\n";
105
  print DBFILE $num_deferred."\n";
106
  print DBFILE $num_bounced."\n"; 
107
  print DBFILE $num_reject."\n";  
108
  print DBFILE $num_clean."\n";   
109
  print DBFILE $num_pspam."\n";   
110
  print DBFILE $num_spam."\n";    
111
  print DBFILE $num_infected."\n";
112
  print DBFILE $num_header."\n";  
113
  print DBFILE $num_banned."\n";  
114
  close( DBFILE );
115
 
116
}
117
 
118
# Return archived data.
119
open( DBFILE, $dbFile ) || die( "Cannot open db file `".$dbFile."'!" );
120
@data = ();
121
while( <DBFILE> ) { chomp; push @data, $_; }
122
 
123
$reqMethod = $ARGV[3];
124
$reqOid    = $ARGV[4];
125
 
126
if ( $reqMethod eq "-n" ) {
127
  # SNMP getNext request.  
128
  if ( $reqOid eq $baseOid ) {
129
    print $baseOid.".0\ninteger\n".$data[1]."\n";
130
  } elsif ( $reqOid eq $baseOid.".0" ) {
131
    print $baseOid.".1\ninteger\n".$data[2]."\n";
132
  } elsif ( $reqOid eq $baseOid.".1" ) {
133
    print $baseOid.".2\ninteger\n".$data[3]."\n";
134
  } elsif ( $reqOid eq $baseOid.".2" ) {
135
    print $baseOid.".3\ninteger\n".$data[4]."\n";
136
  } elsif ( $reqOid eq $baseOid.".3" ) {
137
    print $baseOid.".4\ninteger\n".$data[5]."\n";
138
  } elsif ( $reqOid eq $baseOid.".4" ) {
139
    print $baseOid.".5\ninteger\n".$data[6]."\n";
140
  } elsif ( $reqOid eq $baseOid.".5" ) {
141
    print $baseOid.".6\ninteger\n".$data[7]."\n";
142
  } elsif ( $reqOid eq $baseOid.".6" ) {
143
    print $baseOid.".7\ninteger\n".$data[8]."\n";
144
  } elsif ( $reqOid eq $baseOid.".7" ) {
145
    print $baseOid.".8\ninteger\n".$data[9]."\n";
146
  } elsif ( $reqOid eq $baseOid.".8" ) {
147
    print $baseOid.".9\ninteger\n".$data[10]."\n";
148
  } elsif ( $reqOid eq $baseOid.".9" ) {
149
    print $baseOid.".10\ninteger\n".$data[11]."\n";
150
  }
151
} elsif ( $reqMethod eq "-g" ) {
152
  # SNMP get request.
153
  if ( $reqOid eq $baseOid || $reqOid eq $baseOid.".0" ) {
154
    print $baseOid.".0\ninteger\n".$data[1]."\n";
155
  } elsif( $reqOid eq $baseOid.".1" ) {
156
    print $baseOid.".1\ninteger\n".$data[2]."\n";
157
  } elsif( $reqOid eq $baseOid.".2" ) {
158
    print $baseOid.".2\ninteger\n".$data[3]."\n";
159
  } elsif( $reqOid eq $baseOid.".3" ) {
160
    print $baseOid.".3\ninteger\n".$data[4]."\n";
161
  } elsif( $reqOid eq $baseOid.".4" ) {
162
    print $baseOid.".4\ninteger\n".$data[5]."\n";
163
  } elsif( $reqOid eq $baseOid.".5" ) {
164
    print $baseOid.".5\ninteger\n".$data[6]."\n";
165
  } elsif( $reqOid eq $baseOid.".6" ) {
166
    print $baseOid.".6\ninteger\n".$data[7]."\n";
167
  } elsif( $reqOid eq $baseOid.".7" ) {
168
    print $baseOid.".7\ninteger\n".$data[8]."\n";
169
  } elsif( $reqOid eq $baseOid.".8" ) {
170
    print $baseOid.".8\ninteger\n".$data[9]."\n";
171
  } elsif( $reqOid eq $baseOid.".9" ) {
172
    print $baseOid.".9\ninteger\n".$data[10]."\n";
173
  } elsif( $reqOid eq $baseOid.".10" ) {
174
    print $baseOid.".10\ninteger\n".$data[11]."\n";
175
  }
176
}