[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-users] Network Traffic Monitoring



Hi all,
I've modified a network traffic monitoring tool to monitor for each
virtual machine instead of the physical interface.

Here I attach the script.

You only have to add this perl script to cron:

*/5 * * * * root /usr/local/bin/xen_traffic.pl >/dev/null

It allows some customization by modifyng the source code:

# define location of rrdtool databases
my $rrd = '/var/lib/xenrrd';
# define location of web page
my $img = '/var/www/html/xenrrdtool';

regards,
Jordi




#!/usr/bin/perl
#
# original copyright Martin Pot 2003
# http://martybugs.net/linux/rrdtool/traffic.cgi
#
# Modified by Jordi Prats Catala (CESCA) - 2007/04/26
#
# xen_traffic.pl

use RRDs;

# define location of rrdtool databases
my $rrd = '/var/stats/xennet';
# define location of images
my $img = '/var/stats/xennet/web';

# process data for each interface (add/delete as required)

open(INDEXHTML, "> $img/index.html");

print INDEXHTML "<html>\n";
print INDEXHTML "<head>\n";
print INDEXHTML "<title>Traffic statistics</title>\n";
print INDEXHTML "</head>\n";
print INDEXHTML "<body>\n";
print INDEXHTML "<h1>Traffic statistics</h1>\n";
print INDEXHTML "<ul>\n";

open(XENLIST, '/usr/sbin/xm list |');

my $dropfirstline=<XENLIST>;

while($line=<XENLIST>)
{
    #print $line;
    @elements=split(/\s+/,$line);

    $interface='vif'.$elements[1].'.0';

    print INDEXHTML "<li><a 
href='".$elements[0].".html'>".$elements[0]."</a></li>\n";
    &ProcessInterface($interface, $elements[0]);
}

close XENLIST;


print INDEXHTML "<li><a href='eth0.html'>eth0</a></li>\n";
&ProcessInterface("eth0", "eth0");

print INDEXHTML "<li><a href='eth1.html'>eth1</a></li>\n";
&ProcessInterface("eth1", "eth1");

print INDEXHTML "<ul>\n";
print INDEXHTML "</body>\n";
print INDEXHTML "</html>\n";

close INDEXHTML;


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

    $description=$_[1];
    $description=~s/\s/\./g;

    # get network interface info
    my $in = `/sbin/ifconfig $_[0] |/bin/grep bytes|/bin/cut -d":" -f2|/bin/cut 
-d" " -f1`;
    my $out = `/sbin/ifconfig $_[0] |/bin/grep bytes|/bin/cut -d":" 
-f3|/bin/cut -d" " -f1`;

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

    print "$_[0] traffic in, out: $in, $out\n";

    # if rrdtool database doesn't exist, create it
    if (! -e "$rrd/$description.rrd")
    {
        print "creating rrd database for $description interface...\n";
        RRDs::create "$rrd/$description.rrd",
        "-s 300",
        "DS:in:DERIVE:600:0:12500000",
        "DS:out:DERIVE: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/$description.rrd",
    "-t", "in:out",
    "N:$in:$out";

    # create traffic graphs
    &CreateGraph($_[0], "daily", $_[1]);
    &CreateGraph($_[0], "weekly", $_[1]);
    &CreateGraph($_[0], "monthly", $_[1]);
    &CreateGraph($_[0], "yearly", $_[1]);

    &CreateHTMLPage($description);
}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: interface name (ie, eth0/eth1/eth2/ppp0)
#  $_[1]: interval (ie, day, week, month, year)
#  $_[2]: interface description

    $description=$_[2];
    $description=~s/\s/\./g;

    if($_[1]=~/dayly/) { $interval="day" }
    if($_[1]=~/weekly/) { $interval="week" }
    if($_[1]=~/monthly/) { $interval="month" }
    if($_[1]=~/yearly/) { $interval="year" }

    RRDs::graph "$img/$description-$_[1].png",
      "-s -1$interval",
      "-t trafic de $_[2] $_[1]",
      "--lazy",
      "-h", "80", "-w", "600",
      "-l 0",
      "-a", "PNG",
      "-v bytes/sec",
      "DEF:in=$rrd/$description.rrd:in:AVERAGE",
      "DEF:out=$rrd/$description.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"; }
}

sub CreateHTMLPage
{
    $description=$_[0];

    open(HTMLPAGE,"> $img/$description.html");

    print HTMLPAGE "<html>\n";
    print HTMLPAGE "<head>\n";
    print HTMLPAGE "<title>".$description."</title>\n";
    print HTMLPAGE '<meta http-equiv="refresh" content="420">';
    print HTMLPAGE "</head>\n";
    print HTMLPAGE "<body>\n";
    print HTMLPAGE "<center>\n";

    print HTMLPAGE "<h1>Traffic statistics - ".$description."</h1>";
    print HTMLPAGE "<img src='".$description."-diari.png'><br>";
    print HTMLPAGE "<img src='".$description."-setmanal.png'><br>";
    print HTMLPAGE "<img src='".$description."-mensual.png'><br>";
    print HTMLPAGE "<img src='".$description."-anual.png'><br>";


    print HTMLPAGE "</center>\n";
    print HTMLPAGE "</body>\n";
    print HTMLPAGE "</html>\n";

    close HTMLPAGE;
}
_______________________________________________
Xen-users mailing list
Xen-users@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-users

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.