All pastes #184324 Raw Edit

Stuff

public text v1 · immutable
#184324 ·published 2006-09-27 20:06 UTC
rendered paste body
#!/usr/bin/perl
#
# copyright Martin Pot 2003
# http://martybugs.net/linux/rrdtool/traffic.cgi
#
# rrd_traffic.pl

use RRDs;

# define location of rrdtool databases
#my $rrd = '/var/lib/rrd';
# define location of images
#my $img = '/var/www/rrdtool';

my $rrd = '/home/staff/drift/bin/rrd/';
my $img = '/home/staff/espenfo/public_html';

# process data for each interface (add/delete as required)
&ProcessInterface();
#&ProcessInterface("eth0", "Itnernet");
#&ProcessInterface("eth1", "Backup-link");
#&ProcessInterface("eth2", "MartinMast wireless link");
#&ProcessInterface("eth3", "home wireless");

sub ProcessInterface
{
# process interface
# inputs: $_[0]: interface name (ie, eth0/eth1/eth2/ppp0)
#         $_[1]: interface description

        # get network interface info
        my $in = `snmpwalk -v1 -c abba2008 158.38.48.62 IF-MIB::ifInOctets.65 | cut -d: -f4`;
        my $out = `snmpwalk -v1 -c abba2008 158.38.48.62 IF-MIB::ifOutOctets.65 | cut -d: -f4`;
        # remove eol chars
        chomp($in);
        chomp($out);

        print "agate traffic in, out: $in, $out\n";

        # if rrdtool database doesn't exist, create it
        if (! -e "$rrd/net_total.rrd")
        {
                print "creating rrd database for agate interface...\n";
                RRDs::create "$rrd/net_total.rrd",
                        "-s 300",
                        "DS:in:GAUGE:600:0:12500000",
                        "DS:out:GAUGE:600:0:12500000",
    "DS:out:GAUGE:600:0:12500000",
                        "RRA:AVERAGE:0.5:1:576",
                        "RRA:AVERAGE:0.5:6:672",
                        "RRA:AVERAGE:0.5:24:732",
                        "RRA:AVERAGE:0.5:144:1460";
        }

        # insert values into rrd
        RRDs::update "$rrd/net_total.rrd",
                "-t", "in:out",
                "N:$in:$out";

        # create traffic graphs
        &CreateGraph(net_total, "day", Total);
        &CreateGraph(net_total, "week", Total);
        &CreateGraph(net_total, "month", Total);
        &CreateGraph(net_total, "year", Total);

}
sub CreateGraph
{
# creates graph
# inputs: $_[0]: interface name (ie, eth0/eth1/eth2/ppp0)
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: interface description
        RRDs::graph "$img/$_[0]-$_[1].png",
                "-s -1$_[1]",
                "-t traffic on $_[0] :: $_[2]",
                "--lazy",
                "-h", "80", "-w", "600",
                "-l 0",
                "-a", "PNG",
                "-v bytes/sec",
                "DEF:in=$rrd/net_total.rrd:in:AVERAGE",
                "DEF:out=$rrd/net_total.rrd:out:AVERAGE",
                "CDEF:out_neg=out,-1,*",
                "AREA:in#32CD32:Incoming",
                "LINE1:in#336600",
                "GPRINT:in:MAX:  Max\\: %5.1lf %s",
                "GPRINT:in:AVERAGE: Avg\\: %5.1lf %S",
                "GPRINT:in:LAST: Current\\: %5.1lf %Sbytes/sec\\n",
                "AREA:out_neg#4169E1:Outgoing",
                "LINE1:out_neg#0033CC",
                "GPRINT:out:MAX:  Max\\: %5.1lf %S",
                "GPRINT:out:AVERAGE: Avg\\: %5.1lf %S",
                "GPRINT:out:LAST: Current\\: %5.1lf %Sbytes/sec",
                "HRULE:0#000000";
        if ($ERROR = RRDs::error) { print "$0: unable to generate $_[0] $_[1] traffic graph: $ERROR\n"; }
}