#! /usr/pkg/bin/perl -W # # $Netilium: fetch_mail_statistics.pl,v 1.6 2004/11/07 21:59:11 mad Exp $ # # Calculate and return number of sent / received e-mails on a system # running the Postfix MTA. This is done by incrementally reading the # log file, in a ``tail''-like fashion. # 7th of November, 2004 # http://netilium.org/~mad/ # Martin Adolfsson # # This defines how many seconds we will cache calculated values. # $MINRESET = 1200; if ( @ARGV < 5 ) { print < where: Mail log file, in Postfix compatible format. File for logging statistics and timestamp data. The base OID this script is mounted on. -n for SNMP getNext, or -g for SNMP get. Requested OID, in accordance with previous parameter Report bugs to: EOF ; exit; } $logFile = $ARGV[0]; $dbFile = $ARGV[1]; $baseOid = $ARGV[2]; (@lf = stat $logFile) || die( "Cannot stat log file `".$logFile."'!" ); @st = stat $dbFile; if ( ! @st ) { $timeSinceUpdated = 99999999; $logPosition = $lf[7]; } else { $timeSinceUpdated = time()-$st[9]; open( DBFILE, $dbFile ) || die( "Cannot open db file `".$dbFile."'!" ); $logPosition=; close( DBFILE ); if ( $logPosition > $lf[7] ) { # Log has been truncated since last run. $logPosition = 0; } } if ( $timeSinceUpdated > $MINRESET ) { # Calculate and store new data. open( LOGFILE, $logFile ) || die( "Cannot open log file `".$logFile."'!" ); seek( LOGFILE, $logPosition, 0 ); $num_rec = 0; $num_sent = 0; while( ) { if ( /: message-id=".$dbFile ) || die( "Cannot write to db file `".$dbFile."'!" ); print DBFILE tell(LOGFILE)."\n"; print DBFILE $num_rec."\n"; print DBFILE $num_sent."\n"; close( DBFILE ); } # Return archived data. open( DBFILE, $dbFile ) || die( "Cannot open db file `".$dbFile."'!" ); @data = (); while( ) { chomp; push @data, $_; } $reqMethod = $ARGV[3]; $reqOid = $ARGV[4]; if ( $reqMethod eq "-n" ) { # SNMP getNext request. if ( $reqOid eq $baseOid ) { print $baseOid.".0\ninteger\n".$data[1]."\n"; } elsif ( $reqOid eq $baseOid.".0" ) { print $baseOid.".1\ninteger\n".$data[2]."\n"; } } elsif ( $reqMethod eq "-g" ) { # SNMP get request. if ( $reqOid eq $baseOid || $reqOid eq $baseOid.".0" ) { print $baseOid.".0\ninteger\n".$data[1]."\n"; } elsif( $reqOid eq $baseOid.".1" ) { print $baseOid.".1\ninteger\n".$data[2]."\n"; } }