Re: [Exim] Exim and MRTG

Top Page
Delete this message
Reply to this message
Author: j.linn
Date:  
To: warren, anthony
CC: exim-users
Subject: Re: [Exim] Exim and MRTG
Yes,

I do it on number of messages and Kb

see
www.abdn.ac.uk/~sys044/routers/index.htm

Assuming you have mrtg up and going it should not take too long to get it
up.

If you need more then let me know

John Linn

In the mailservers

inetd.conf
# add for exim and mrtg
mrtg    stream  tcp     nowait root /opt/exim/bin/mrtg-stats mrtg-stats


(add mrtg to services file)
mrtg        7256/tcp    


/opt/exim/bin/mrtg-stats is

#!/bin/sh
#
# smtp-stats: invoke mailstats from a wrapper for use in inetd.
#
exec /opt/exim/bin/mrtg-KB -nt -ne -h0 -q0 -nr -t0 /var/spool/exim/log/mainlog
#

mrtg-KB is just eximstats but with the part which prints out the volume
munged so that it only prints it in KB - not required if you only want
volume.

This can be check with a telnet call to mailserver port 7256. Don't think
there is any security issues here but you could tcpwrapper it.

At the mrtg host end. Config entry is (with host and directory changed)

WorkDir: /export/www-data/mrtg
#---------------------------------------------------------------
Target[staff]: `/usr/local/etc/eximstats mailserv.abdn.ac.uk`
MaxBytes[staff]: 2000
# AbsMax[staff]: 1800
#Unscaled[staff]: dwmy
Options[staff]: gauge
Title[staff]: Staff Mailserv Exim Statistics
PageTop[staff]: Staff Mailserv Exim Statistics
XSize[staff]: 500
#Supress[staff]: my
YSize[staff]: 200
WithPeak[staff]: dwmy
YLegend[staff]: No. of messages
ShortLegend[staff]: messages
LegendI[staff]:  Received:
LegendO[staff]:  Delivered:
#-----------------------------------------------------------------
Target[staffMB]: `/usr/local/etc/exim-MB mailserv.abdn.ac.uk`
MaxBytes[staffMB]: 100000
# AbsMax[staffMB]: 1800
#Unscaled[staffMB]: dwmy
Options[staffMB]: gauge
Title[staffMB]: Staff Mailserv Exim Statistics (KB)
PageTop[staffMB]: Staff Mailserv Exim Statistics (KB)
XSize[staffMB]: 500
#Supress[staffMB]: my
YSize[staffMB]: 200
WithPeak[staffMB]: dwmy
YLegend[staffMB]: K Bytes
ShortLegend[staffMB]: K Bytes
LegendI[staffMB]:  Received:
LegendO[staffMB]:  Delivered:




the program existats is a a quick fudge of the sendmail one
(exim-MB is the version of the following that picks up the volume instead
of the number - I will send it if you want)


#!/usr/local/bin/perl
#
# Author:  Petter Reinholdtsen <pere@???>
# Date:    1997-07-09
# The original was written by Rachel Polanskis <rachel@???>
# 
# Fetches output from mailstats(1) either via TCP or via exec and
# feeds changes on smtp to mrtg.
#
# Irix 6.x seems to lack mailstats
#
# Usage mailstats [host]
#  if host is missing, localhost is used


use strict;
use Socket;

my($datafile, $source, $sourceport, @mailstatspaths,
$oldfrm, $oldto,
$newfrm, $newto, $uptime);

# Adjust this to your own mailserver.  Uses local `mailstats` if set
# to 'localhost'
$source         = $ARGV[0] || "localhost";
$sourceport     = "7256";


$datafile       = "/export/www-data/mailstat-$source.old";
#@mailstatspaths = ( "/usr/sbin/mailstats", "/usr/bin/mailstats" );


($oldfrm, $oldto) = getOldStats($datafile);

($newfrm, $newto, $uptime) = getStats($source, $sourceport);

putOldStats($datafile, $newfrm, $newto) || warn "$0: Unable to save stats to $datafile";
# cope with day roleover
if ($oldfrm > $newfrm) { $oldfrm = 0;} if ($oldto > $newto) {$oldfrm = 0;}

print $newfrm-$oldfrm,"\n",$newto-$oldto,"\n","$uptime\n$source\n" if ($oldfrm);

##
# Returns first line of file given as param splittet on space
sub getOldStats {
    my($filename) = @_;
    open(OLD, $filename) || warn "$0: Unable to open $filename for reading";
    my($line) = <OLD>;
    close(OLD);
    return split(/ /, $line);
}


sub findFirstExecutable {
    my($filename);
    foreach $filename (@_) {
    return $filename if ( -x $filename && ! -d $filename );
    }
}


sub getStats {
    my($source, $sourceport) = @_;
    my(@output, $port, $proto, $iaddr, $paddr);
    if ( $source eq "localhost" ) {
#    my($progpath) = findFirstExecutable(@mailstatspaths);
#    @output = `$progpath`;
    chomp(@output);
    } else {
       if ($sourceport =~ /\D/) { 
        $port = getservbyname ($sourceport, 'tcp');
         } else { 
            $port = $sourceport;
         }
    die "$0: Bad port \"$sourceport\"" unless ($port);
    $proto = getprotobyname ('tcp') || die "$0: Bad prototype tcp";


    $iaddr = inet_aton($source) or die "$0: no host \"$source\"";
    $paddr = sockaddr_in($port, $iaddr);


    socket (SOCK, PF_INET, SOCK_STREAM, $proto) or die "$0: socket error $!";
    connect (SOCK, $paddr) or die "$0: connect error $!";


    while (<SOCK>) {
        push(@output, $_);
    }
    close(SOCK) || warn "$0: socket close error $!";
    }
    my($curfrm, $curto, $uptime);
    foreach (@output) {
    ($curfrm) = (split(/ +/))[3] if (/Received/);
    ($curto) = (split(/ +/))[3] if (/Delivered/);
    ($uptime) = m/Exim statistics from (.*)/ if (/Exim/);
    }
    return ($curfrm, $curto, $uptime);
}


sub putOldStats {
    my($filename, $frm, $to) = @_;
    open(STAT, ">$filename") || return "";
    print STAT "$frm $to\n";
    close(STAT);
    return "1";
}