#!/usr/bin/perl

# Copyright Martin Pot 2002-2003
# http://martybugs.net/linux/rrdtool/traffic.cgi

use RRDs;

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

# define the network interface
my $iface = 'ppp0'; #name
my $ciface;         #device

# hack hack, interface is eth1, but we name it ppp0 for stats

my $checkp = `ifconfig |grep $iface`;
if ($checkp =~ m!$iface!){
	$ciface=$iface;
}else{
	$ciface = 'eth1';
	warn " iface:$ciface ";
}


# define a description for the interface
my $descr = 'local internet';

# get network interface info
my $in = `ifconfig $ciface |grep bytes|cut -d":" -f2|cut -d" " -f1`;
my $out = `ifconfig $ciface |grep bytes|cut -d":" -f3|cut -d" " -f1`;

# remove eol chars
chomp($in);
chomp($out);

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

# insert values into rrd
`$rrdtool update $rrd/$iface.rrd -t in:out N:$in:$out`;

# daily traffic graph
RRDs::graph "$img/$iface-day.png",
        "-t", "traffic :: $iface $descr",
        "-h", "80", "-w", "600",
        "-a", "PNG",
        "-v", "bytes/sec",
        "DEF:in=$rrd/$iface.rrd:in:AVERAGE",
        "DEF:out=$rrd/$iface.rrd:out:AVERAGE",
        "AREA:in#11EE11:Incoming",
        "LINE1:in#009900",
        "GPRINT:in:MAX:  Max\\: %3.lf %s",
        "GPRINT:in:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:in:LAST: Current\\: %3.lf %Sbytes/sec\\n",
        "LINE2:out#0000FF:Outgoing",
        "GPRINT:out:MAX:  Max\\: %3.lf %S",
        "GPRINT:out:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:out:LAST: Current\\: %3.lf %Sbytes/sec";
if ($ERROR = RRDs::error) { print "$0: unable to generate daily traffic graph: $ERROR\n"; }



# daily traffic out graph
RRDs::graph "$img/$iface-out-day.png",
#        "-t", "traffic :: $iface $descr",
        "-h", "80", "-w", "600",
        "-a", "PNG",
        "-v", "bytes/sec",
        "DEF:out=$rrd/$iface.rrd:out:AVERAGE",
        "LINE2:out#0000FF:Outgoing",
        "GPRINT:out:MAX:  Max\\: %3.lf %S",
        "GPRINT:out:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:out:LAST: Current\\: %3.lf %Sbytes/sec";
if ($ERROR = RRDs::error) { print "$0: unable to generate daily traffic graph: $ERROR\n"; }





# weekly traffic graph
RRDs::graph "$img/$iface-week.png",
        "-s", "-1week", "--lazy",
        "-t", "traffic :: $iface $descr",
        "-h", "80", "-w", "600",
        "-a", "PNG",
        "-v", "bytes/sec",
        "DEF:in=$rrd/$iface.rrd:in:AVERAGE",
        "DEF:out=$rrd/$iface.rrd:out:AVERAGE",
        "AREA:in#11EE11:Incoming",
        "LINE1:in#009900",
        "GPRINT:in:MAX:  Max\\: %3.lf %s",
        "GPRINT:in:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:in:LAST: Current\\: %3.lf %Sbytes/sec\\n",
        "LINE2:out#0000FF:Outgoing",
        "GPRINT:out:MAX:  Max\\: %3.lf %S",
        "GPRINT:out:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:out:LAST: Current\\: %3.lf %Sbytes/sec";
if ($ERROR = RRDs::error) { print "$0: unable to generate weekly traffic graph: $ERROR\n"; }

# monthly traffic graph
RRDs::graph "$img/$iface-month.png",
        "-s", "-1month", "--lazy",
        "-t", "traffic :: $iface $descr",
        "-h", "80", "-w", "600",
        "-a", "PNG",
        "-v", "bytes/sec",
        "DEF:in=$rrd/$iface.rrd:in:AVERAGE",
        "DEF:out=$rrd/$iface.rrd:out:AVERAGE",
        "AREA:in#11EE11:Incoming",
        "LINE1:in#009900",
        "GPRINT:in:MAX:  Max\\: %3.lf %s",
        "GPRINT:in:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:in:LAST: Current\\: %3.lf %Sbytes/sec\\n",
        "LINE2:out#0000FF:Outgoing",
        "GPRINT:out:MAX:  Max\\: %3.lf %S",
        "GPRINT:out:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:out:LAST: Current\\: %3.lf %Sbytes/sec";
if ($ERROR = RRDs::error) { print "$0: unable to generate monthly traffic graph: $ERROR\n"; }

# yearly traffic graph
RRDs::graph "$img/$iface-year.png",
        "-s", "-1year", "--lazy",
        "-t", "traffic :: $iface $descr",
        "-h", "80", "-w", "600",
        "-a", "PNG",
        "-v", "bytes/sec",
        "DEF:in=$rrd/$iface.rrd:in:AVERAGE",
        "DEF:out=$rrd/$iface.rrd:out:AVERAGE",
        "AREA:in#11EE11:Incoming",
        "LINE1:in#009900",
        "GPRINT:in:MAX:  Max\\: %3.lf %s",
        "GPRINT:in:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:in:LAST: Current\\: %3.lf %Sbytes/sec\\n",
        "LINE2:out#0000FF:Outgoing",
        "GPRINT:out:MAX:  Max\\: %3.lf %S",
        "GPRINT:out:AVERAGE: Avg\\: %3.lf %S",
        "GPRINT:out:LAST: Current\\: %3.lf %Sbytes/sec";
if ($ERROR = RRDs::error) { print "$0: unable to generate yearly traffic graph: $ERROR\n"; }
