mailscanner-mrtg
Raymond Dijkxhoorn
raymond at PROLOCATION.NET
Wed May 14 14:08:03 IST 2003
Hi!
> > > Unknown 'strict' tag(s) '%Config $hostname $Total $Total2 $debug $cmd' at
> > > /usr/sbin/mailscanner-mrtg line 30
> > > BEGIN failed--compilation aborted at /usr/sbin/mailscanner-mrtg line 30.
> > You have to edit the script, its a little ugly script :))
> > There is a line that isnt needed, can look it up if needed.
> Maybe you should I cannot see it
Try this one, workd for me on RH9. =)
Bye,
Raymond.
-------------- next part --------------
#!/usr/bin/perl -w
# mailscanner-mrtg - Extensive monitoring for MailScanner machines
# Copyright (C) 2002 Dale Lovelace <dlovelace at hotels.com>
# With various bits shamelessly stolen from others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if Queue Dir now not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Ok, so this is not very good perl code. I'm not a perl coder!
# Hell, I'm not ANY coder... Please Please make modifications
# and send them back to me!!!
#
# Dale Lovelace
#
# RDL 11/15/2002 Comments to dlovelace at hotels.com
#
# All Your Base Are Belong To Us!
#
#use strict qw{%Config $hostname $Total $Total2 $debug $cmd};
use FileHandle;
# Change this line if your config file is in a non-standard location!
my($configfile) = "/etc/MailScanner/mailscanner-mrtg.conf";
my($hostname) = `hostname`;
my($Total) = 0;
my($Total2) = 0;
my($debug) = 0;
my($cmd) = shift;
ReadConfFile();
my($UpTime) = GetUpTime();
if ($debug) { Debug ("Using Stuff!") }
# You mean to tell me perl doesn't have a friggin' switch command????
if (defined($cmd)) {
SWITCH: {
if ($cmd eq "cpu") { Cpu(); last SWITCH; };
if ($cmd eq "inqueue") { InQueue(); last SWITCH; };
if ($cmd eq "iptraffic") { IpTraffic(); last SWITCH; };
if ($cmd eq "loadavg") { LoadAvg(); last SWITCH; };
if ($cmd eq "mail") { Mail(); last SWITCH; };
if ($cmd eq "mailbytes") { MailBytes(); last SWITCH; };
if ($cmd eq "mailscanner") { MailScanner(); last SWITCH; };
if ($cmd eq "memory") { Memory(); last SWITCH; };
if ($cmd eq "outqueue") { OutQueue(); last SWITCH; };
if ($cmd eq "rootusage") { RootUsage(); last SWITCH; };
if ($cmd eq "sendmail") { SendMail(); last SWITCH; };
if ($cmd eq "spam") { Spam(); last SWITCH; };
if ($cmd eq "spoolusage") { SpoolUsage(); last SWITCH; };
if ($cmd eq "virus") { Virus(); last SWITCH; }
print"\n";
print "ERROR: Unknown command-line option $cmd\n";
Usage();
}
} else {
Usage();
}
if ($debug) {
Debug("Non-Debug Program Output Begins Here");
}
print "$Total\n";
print "$Total2\n";
print "$UpTime\n";
print "MailScanner at $hostname\n";
sub Cpu {
if ($debug) { Debug("Beginning sub Cpu") }
my($getcpu);
if ($Config{"sarbinary"}) {
$getcpu= `$Config{"sarbinary"} -u 1 5 | grep Average`;
$getcpu=~ /^Average:\s+all\s+(\d+)\.(\d+)\s+\d+\.\d+\s+(\d+)\.(\d+)\s+\d+\.\d+/;
# We output the total of User + System CPU Utilization
$Total= $1 + $3;
# We want to output a whole number, so round up if the decimal is > .50
if (($2 + $4) > 50) {
$Total ++;
}
if ($debug) {
Debug("getcpu", $getcpu);
Debug("User CPU Percentage", $1);
Debug("User CPU Fraction", $2);
Debug("System CPU Percentage", $3);
Debug("System CPU Fraction", $4);
Debug("Total", $Total);
}
} else {
die "ERROR: Sar Binary not specified in $configfile\n";
}
if ($debug) { Debug("Leaving sub Cpu") }
}
sub Debug {
print "DEBUG : " . $_[0];
print " = " . $_[1] if ($_[1]);
print "\n";
}
sub FixMonth {
if ($debug) { Debug("Beginning sub FixMonth") }
my($month) = shift;
if ($debug) { Debug("month before fixing", $month) }
SWITCH: {
if ($month eq "0") { $month = "Jan"; last SWITCH; }
if ($month eq "1") { $month = "Feb"; last SWITCH; }
if ($month eq "2") { $month = "Mar"; last SWITCH; }
if ($month eq "3") { $month = "Apr"; last SWITCH; }
if ($month eq "4") { $month = "May"; last SWITCH; }
if ($month eq "5") { $month = "Jun"; last SWITCH; }
if ($month eq "6") { $month = "Jul"; last SWITCH; }
if ($month eq "7") { $month = "Aug"; last SWITCH; }
if ($month eq "8") { $month = "Sep"; last SWITCH; }
if ($month eq "9") { $month = "Oct"; last SWITCH; }
if ($month eq "10") { $month = "Nov"; last SWITCH; }
if ($month eq "11") { $month = "Dec"; last SWITCH; }
$month = "BAD";
}
if ($debug) { Debug("month after fixing", $month) }
if ($debug) { Debug("Leaving sub FixMonth") }
return $month
}
sub GetUpTime {
if ($debug) { Debug("Starting sub GetUpTime") }
my($getuptime);
# Thanks to Kris Stumpner for this new and improved GetUpTime!
# If your box is up less than 1 day the first regex will hit.
# The second hits for uptime > 1 day!
if ($Config{"uptimecommand"}) {
$getuptime = `$Config{"uptimecommand"}`;
if ($debug) { Debug("getuptime", $getuptime) }
if ($getuptime =~ /^\s+\d+:\d+.+\s+up\s+(\d+:\d+.+),/) {
return $1;
} elsif ($getuptime =~ /^\s+\d+:\d+.+\s+up\s+(\d+)\s+(\w+),/) {
return $1 . " " . $2;
} else {
return "UpTime can not be computed";
}
} else {
die "ERROR: UpTime Command not specified in $configfile\n";
}
if ($debug) { Debug("Leaving sub GetUpTime") }
}
sub IpTraffic {
if ($debug) { Debug("Beginning sub IpTraffic") }
my($iptraffic, @iptraffic, $ethdev, $bytesIN, $bytesOUT);
if ($Config{"sarbinary"}) {
$iptraffic=`$Config{"sarbinary"} -n DEV 1 5`;
@iptraffic=split /\n/, $iptraffic;
foreach $line (@iptraffic) {
if ($line =~ /Average:\s+eth\d\s+/) {
$line =~ /Average:\s+eth(\d)\s+\d+\.\d+\s+\d+\.\d+\s+(\d+)\.\d+\s+(\d+)\.\d+.+/;
$ethdev = $1;
$bytesIN = $2;
$bytesOUT = $3;
if ($debug) {
Debug("line", $line);
Debug("Ethernet Device", "eth" . $ethdev);
Debug("bytesIN", $bytesIN);
Debug("bytesOUT", $bytesOUT);
}
$Total += $bytesIN;
$Total2 += $bytesOUT;
}
}
} else {
die "ERROR: Sar Binary not specified in $configfile\n";
}
if ($debug) {
Debug("Total", $Total);
Debug("Total2", $Total2);
Debug("Leaving sub IpTraffic");
}
}
sub InQueue {
if ($debug) { Debug("Beginning sub InQueue") }
if ($Config{"incomingqueuedir"}) {
use File::Find;
if ($debug) { Debug ("use File::Find", "True") }
find(\&Wanted, $Config{"incomingqueuedir"});
# File::Find counts the directory name as a file... Shrug...
$Total--;
} else {
die "ERROR: Incoming Queue Dir not specified in $configfile\n";
}
if ($debug) {
Debug("Total", $Total);
Debug("Leaving sub InQueue");
}
}
sub LoadAvg {
if ($debug) { Debug("Beginning sub LoadAvg") }
my($loadavg, @loadavg, $l);
if ($Config{"sarbinary"}) {
$loadavg = `$Config{"sarbinary"} -q 1 5`;
if ($debug) { Debug("loadavg", $loadavg) }
@loadavg = split /\n/, $loadavg;
foreach $line (@loadavg) {
if ($line =~ /Average:/) {
$line =~ /Average:\s+\d+\s+\d+\s+(\d+)\.(\d+)\s+\d+\.\d+.+/;
if ($debug) {
Debug("line", $line);
Debug("Load Average Real", $1);
Debug("Load Average Decimal", $2);
}
$Total = $1;
if ($2 gt 50) { $Total++ }
}
}
} else {
die "ERROR: Sar Binary not specified in $configfile\n";
}
if ($debug) {
Debug("Total", $Total);
Debug("Leaving sub LoadAvg");
}
}
sub Mail {
if ($debug) { Debug("Beginning sub Mail") }
my($sec,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$month);
if ($Config{"parseentiremaillog"} eq "yes") {
if ($debug) { Debug("Parse Entire Mail Log", "yes") }
} else {
if ($debug) { Debug("Parse Entire Mail Log", "no") }
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$month = FixMonth($mon);
if ($debug) {
Debug("sec", $sec);
Debug("min", $min);
Debug("hour", $hour);
Debug("mday", $mday);
Debug("mon", $mon);
Debug("year", $year);
Debug("wday", $wday);
Debug("yday", $yday);
Debug("isdst", $isdst);
Debug("month", $month);
}
}
open(LOG, $Config{"maillog"})
or (die "Cannot access maillog $Config{'maillog'} $!");
while(<LOG>) {
chomp;
if ($Config{"parseentiremaillog"} eq "yes") {
if (/sendmail\[\d+\]/i) { $Total += $1 if /nrcpts=(\d+),/i; }
} else {
if (/$month\s+$mday.+/) {
if (/sendmail\[\d+\]/i) { $Total += $1 if /nrcpts=(\d+),/i; }
}
}
next;
}
close LOG;
if ($debug) {
Debug("Total", $Total);
Debug("Leaving sub Mail");
}
}
sub MailBytes {
if ($debug) { Debug("Beginning sub MailBytes") }
my($sec,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$month);
if ($Config{"parseentiremaillog"} eq "yes") {
if ($debug) { Debug("Parse Entire Mail Log", "yes") }
} else {
if ($debug) { Debug("Parse Entire Mail Log", "no") }
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$month = FixMonth($mon);
if ($debug) {
Debug("sec", $sec);
Debug("min", $min);
Debug("hour", $hour);
Debug("mday", $mday);
Debug("mon", $mon);
Debug("year", $year);
Debug("wday", $wday);
Debug("yday", $yday);
Debug("isdst", $isdst);
Debug("month", $month);
}
}
sub BytesFromSendmail {
if (/.+sendmail\[\d+\]:\s+\w+:\s+from=\S+,\s+size=(\d+),.+/i) {
return $1;
} else {
return 0;
}
}
sub BytesFromMailScanner {
if (/.+MailScanner\[\d+\]:\s+New\s+Batch:\s+Scanning\s+\d+\s+Messages,\s+(\d+)\s+bytes.+/) {
return $1;
} else {
return 0;
}
}
open(LOG, $Config{"maillog"})
or (die "Cannot access maillog $Config{'maillog'} $!");
while(<LOG>) {
chomp;
if ($Config{"parseentiremaillog"} eq "yes") {
if (lc($Config{"calculatemailbytes"}) eq "sendmail") {
$Total += &BytesFromSendmail($_);
} elsif (lc($Config{"calculatemailbytes"}) eq "mailscanner") {
$Total += &BytesFromMailScanner($_);
} else {
die "ERROR: Calculate Mail Bytes value not specified in $configfile";
}
} else {
if (/$month\s+$mday.+/) {
if (lc($Config{"calculatemailbytes"}) eq "sendmail") {
$Total += &BytesFromSendmail($_);
} elsif (lc($Config{"calculatemailbytes"}) eq "mailscanner") {
$Total += &BytesFromMailScanner($_);
} else {
die "ERROR: Calculate Mail Bytes value not specified in $configfile";
}
}
}
next;
}
close LOG;
if ($debug) {
Debug("Total", $Total);
Debug("Leaving sub Mail");
}
}
sub MailScanner {
if ($debug) { Debug("Beginning sub MailScanner") }
@ps = `ps ax`;
foreach $p (@ps) {
if ($p =~ /MailScanner/) { $Total ++ }
}
if ($debug) { Debug("Restart Threshhold", $Config{"restartthreshhold"}) }
if ($Config{"restartthreshhold"}) {
if ($Total < $Config{"restartthreshhold"}) {
if ($debug) { Debug("Restarting MailScanner") }
`$Config{"restartmailscanner"}`;
}
}
if ($debug) {
Debug("Total", $Total);
Debug("Leaving sub MailScanner");
}
}
sub Memory {
if ($debug) { Debug("Beginning sub Memory") }
my($meminfo, @meminfo);
if ($Config{"sarbinary"}) {
$meminfo = `$Config{"sarbinary"} -r 1 5`;
if($debug) { Debug("meminfo", $meminfo) }
@meminfo = split /\n/, $meminfo;
foreach $line (@meminfo) {
if ($line =~ /^Average:/) {
$line =~ /Average:\s+(\d+)\s+(\d+)\s+(\d+)\.(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\.(\d+).+/;
$kbmemfree = $1;
$kbmemused = $2;
$percentmemusedReal = $3;
$percentmemusedDecimal = $4;
$kbmemshrd = $5;
$kbbuffers = $6;
$kbcached = $7;
$kbswpfree = $8;
$kbswpused = $9;
$percentswpusedReal = $10;
$percentswpusedDecimal = $11;
if ($debug) {
Debug("line", $line);
Debug("kbmemfree", $kbmemfree);
Debug("kbmemused", $kbmemused);
Debug("percentmemusedReal", $percentmemusedReal);
Debug("percentmemusedDecimal", $percentmemusedDecimal);
Debug("kbmemshrd", $kbmemshrd);
Debug("kbbuffers", $kbbuffers);
Debug("kbcached", $kbcached);
Debug("kbswpfree", $kbswpfree);
Debug("kbswpused", $kbswpused);
Debug("percentswpusedReal", $percentswpusedReal);
Debug("percentswpusedDecimal", $percentswpusedDecimal);
}
# Total used memory
$Total = $kbmemused;
# Total memory -actually- in use
$Total2 = $Total-$kbmemshrd-$kbbuffers-$kbcached;
# I want the output in MegaBytes!
$Total = int($Total / 1000);
$Total2 = int($Total2 / 1000);
}
}
} else {
die "ERROR: Sar Binary not specified in $configfile\n";
}
if ($debug) {
Debug("Total", $Total);
Debug("Total2", $Total2);
Debug("Leaving sub Memory");
}
}
sub OutQueue {
if ($debug) { Debug("Beginning sub OutQueue") }
if ($Config{"outgoingqueuedir"}) {
use File::Find;
if ($debug) { Debug ("use File::Find", "True") }
find(\&Wanted, $Config{"outgoingqueuedir"});
# File::Find counts the directory name as a file... Shrug...
$Total--;
} else {
die "ERROR: Outgoing Queue Dir not specified in $configfile\n";
}
if ($debug) {
Debug("Total", $Total);
Debug("Leaving sub OutQueue");
}
}
sub RootUsage {
if ($debug) { Debug("Beginning sub RootUsage") }
$_ = `df -k / | grep -v "Filesystem"`;
($device, $size, $used, $free, $Total, $mount) = split(/\s+/);
chop($Total);
if ($debug) {
Debug("device", $device);
Debug("size", $size);
Debug("used", $used);
Debug("free", $free);
Debug("mount", $mount);
Debug("Total (percent)", $Total);
Debug("Leaving sub RootUsage");
}
}
sub SendMail {
if ($debug) { Debug("Beginning sub SendMail") }
@ps = `ps ax`;
foreach $p (@ps) {
if ($p =~ /sendmail:/) {
$Total ++;
}
}
if ($debug) {
Debug("Total", $Total);
Debug("Leaving sub SendMail");
}
}
sub Spam {
if ($debug) { Debug("Beginning sub Spam") }
my($sec,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$month);
my($spam) = 0;
my($blocked) = 0;
if ($Config{"parseentirespamlog"} eq "yes") {
if ($debug) { Debug("Parse Entire Spam Log", "yes") }
} else {
if ($debug) { Debug("Parse Entire Spam Log", "no") }
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$month = FixMonth($mon);
if ($debug) {
Debug("sec", $sec);
Debug("min", $min);
Debug("hour", $hour);
Debug("mday", $mday);
Debug("mon", $mon);
Debug("year", $year);
Debug("wday", $wday);
Debug("yday", $yday);
Debug("isdst", $isdst);
Debug("month", $month);
}
}
open(LOG, $Config{"spamlog"})
or (warn("Cannot access file $Config{'spamlog'}"), next);
while(<LOG>) {
chomp;
if ($Config{"parseentirespamlog"} eq "yes") {
if (/MailScanner\[\d+\]/i) {
if (/Spam Checks: Found (\d+) spam messages/i) { $spam += $1 }
} elsif (/sendmail\[\d+\]/i) {
if (/reject=55\d/i) { $blocked ++ }
}
} else {
if (/$month\s+$mday/) {
if (/MailScanner\[\d+\]/i) {
if (/Spam Checks: Found (\d+) spam messages/i) { $spam += $1 }
} elsif (/sendmail\[\d+\]/i) {
if (/reject=55\d/i) { $blocked ++ }
}
}
}
next;
}
close LOG;
# Ok. $blocked = the number of mails Sendmail rejected with a 550
# error code. This is the error code you should use to reject domains,
# senders, receivers etc. you have problems with. We want the total
# to be everything MailScanner & SpamAssassin caught ($spam) plus
# everything rejected by sendmail with a 550 ($blocked) so the total
# that shows up on mrtg will be all mails blocked, stopped, rejected
# et. al. The mails that were blocked by a sendmail with a 550 will
# then show up as the red line on the graph.
$Total = $spam + $blocked;
$Total2 = $blocked;
if ($debug) {
Debug("Total", $Total);
Debug("Total2", $Total2);
Debug("Leaving sub Spam");
}
}
sub SpoolUsage {
if ($debug) { Debug("Beginning sub Spoolusage") }
$_ = `df -k /var/spool | grep -v "Filesystem"`;
($device, $size, $used, $free, $Total, $mount) = split(/\s+/);
chop($Total);
if ($debug) {
Debug("device", $device);
Debug("size", $size);
Debug("used", $used);
Debug("free", $free);
Debug("mount", $mount);
Debug("Total (percent)", $Total);
Debug("Leaving sub SpoolUsage");
}
}
sub Usage {
print "\n";
print "USAGE:\n";
print $0 . " option\n";
print "Where option is one of the following:\n";
print "\n";
print "cpu : returns the cpu utilization percentage\n";
print "iptraffic : returns the amount of ip traffic on all ethernet device\n";
print "inqueue : returns the number of files in the incoming mail queue\n";
print "loadavg : returns the current load average\n";
print "mail : returns the amount of mail relayed today\n";
print "mailbytes : returns the bytes of mail relayed today\n";
print "mailscanner : returns the number of copies of mailscanner running\n";
print "memory : returns the amount of ram being used\n";
print "outqueue : returns the number of files in the outgoing mail queue\n";
print "rootusage : returns the percent of disk space available in \\ \n";
print "sendmail : returns the number of copies of sendmail running\n";
print "spam : returns the number of mails marked as spam today\n";
print "spoolusage : returns the percent of disk space available in \\var\\spool \n";
print "virus : returns the number of virus' caught today\n";
print "\n";
exit(1);
}
sub Virus {
if ($debug) { Debug("Beginning sub Virus") }
my($sec,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$month);
if ($Config{"parseentireviruslog"} eq "yes") {
if ($debug) { Debug("Parse Entire Virus Log", "yes") }
} else {
if ($debug) { Debug("Parse Entire Virus Log", "no") }
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$month = FixMonth($mon);
if ($debug) {
Debug("sec", $sec);
Debug("min", $min);
Debug("hour", $hour);
Debug("mday", $mday);
Debug("mon", $mon);
Debug("year", $year);
Debug("wday", $wday);
Debug("yday", $yday);
Debug("isdst", $isdst);
Debug("month", $month);
}
}
open(LOG, $Config{"viruslog"})
or (warn("Cannot access viruslog skipping, $!"), next);
while(<LOG>) {
chomp;
if ($Config{"parseentireviruslog"} eq "yes") {
if (/MailScanner\[\d+\]/i) { $Total += $1 if /Found (\d+) viruses/i; }
} else {
if (/$month\s+$mday.+/) {
if (/MailScanner\[\d+\]/i) { $Total += $1 if /Found (\d+) viruses/i; }
}
}
next;
}
close LOG;
if ($debug) {
Debug("Total", $Total);
Debug("Leaving sub Virus");
}
}
sub Wanted {
$Total ++;
}
#
# Read the configuration file. Borrowed from MailScanner :-)
# These 3 subs are copyright Julian Field. Released under the GPL.
#
sub ItoE {
my($val) = @_;
lc($ItoE{$val}) or lc($val);
}
sub EtoI {
my($val) = @_;
lc($EtoI{$val}) or lc($val);
}
sub ReadConfFile {
if ($debug) {
Debug("Beginning sub ReadConfFile") ;
Debug("configfile", $configfile);
}
# Slurp the whole file into a big hash.
# Don't Complain if we see the same keyword more than once.
my($fileh, $linecounter, $key, $value);
$fileh = new FileHandle;
$fileh->open($configfile) or
die "Could not read configuration file " . $configfile . " " . $!;
$linecounter = 0;
while(<$fileh>) {
$linecounter++;
chomp;
s/#.*$//;
s/^\s+//;
s/\s+$//;
next if /^$/;
$key = undef;
$value = undef;
/^(.*?)\s*=\s*(.*)$/;
($key,$value) = ($1,$2);
$key = lc($key);
$key =~ s/[^a-z0-9]//g; # Leave numbers and letters only
# Translate the value to the internal (shorter) version of it
$key = EtoI($key);
$Config{$key} = $value;
if ($debug) { Debug($key, $value) }
# Not sure what this next line was for...
# $LineNos doesn't show up anywhere else...
#$LineNos{$key} = $linecounter; # Save where the value was stored
}
$fileh->close();
if ($debug) { Debug("Leaving sub ReadConfFile") }
}
1;
More information about the MailScanner
mailing list