2008-01-16 08:36:45 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
#
|
2008-02-19 15:43:28 +00:00
|
|
|
# written by Manuel Kasper, Monzoon Networks AG <mkasper@monzoon.net>
|
2009-10-10 16:53:25 +00:00
|
|
|
# mod for rrd path sjc
|
2008-01-16 08:36:45 +00:00
|
|
|
|
2008-02-19 15:43:28 +00:00
|
|
|
use strict;
|
2008-01-16 08:36:45 +00:00
|
|
|
use RRDs;
|
2009-10-10 16:53:25 +00:00
|
|
|
use File::Find;
|
2008-01-16 08:36:45 +00:00
|
|
|
|
2008-02-19 15:43:28 +00:00
|
|
|
if ($#ARGV != 2) {
|
|
|
|
die("Usage: $0 <path to RRD file directory> <path to known links file> outfile\n");
|
2008-01-16 08:36:45 +00:00
|
|
|
}
|
|
|
|
|
2008-02-19 15:43:28 +00:00
|
|
|
my $rrdpath = $ARGV[0];
|
|
|
|
my $knownlinksfile = $ARGV[1];
|
|
|
|
my $statsfile = $ARGV[2];
|
|
|
|
|
|
|
|
my %knownlinks;
|
2008-01-16 08:36:45 +00:00
|
|
|
|
2008-02-19 15:43:28 +00:00
|
|
|
read_knownlinks();
|
|
|
|
|
|
|
|
my @links = values %knownlinks;
|
2008-01-16 08:36:45 +00:00
|
|
|
|
|
|
|
# walk through all RRD files in the given path and extract stats for all links
|
|
|
|
# from them; write the stats to a text file, sorted by total traffic
|
|
|
|
|
2009-10-10 16:53:25 +00:00
|
|
|
my @rrdfiles;
|
|
|
|
find(sub {
|
|
|
|
if (-f $_) {
|
|
|
|
push(@rrdfiles, $File::Find::name);
|
|
|
|
}
|
|
|
|
}, $rrdpath);
|
2008-01-16 08:36:45 +00:00
|
|
|
|
|
|
|
my $astraffic = {};
|
|
|
|
|
|
|
|
$|=1;
|
|
|
|
my $i = 0;
|
|
|
|
foreach my $rrdfile (@rrdfiles) {
|
2009-10-10 16:53:25 +00:00
|
|
|
if ($rrdfile =~ /\/(\d+).rrd$/) {
|
2008-01-16 08:36:45 +00:00
|
|
|
my $as = $1;
|
|
|
|
|
|
|
|
$astraffic->{$as} = gettraffic($as, time - 86400, time);
|
|
|
|
$i++;
|
|
|
|
if ($i % 100 == 0) {
|
|
|
|
print "$i... ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print "\n";
|
|
|
|
|
|
|
|
# now sort the keys in order of descending total traffic
|
|
|
|
my @asorder = sort {
|
|
|
|
my $total_a = 0;
|
|
|
|
|
|
|
|
foreach my $t (values %{$astraffic->{$a}}) {
|
|
|
|
$total_a += $t;
|
|
|
|
}
|
|
|
|
my $total_b = 0;
|
|
|
|
foreach my $t (values %{$astraffic->{$b}}) {
|
|
|
|
$total_b += $t;
|
|
|
|
}
|
|
|
|
return $total_b <=> $total_a;
|
|
|
|
} keys %$astraffic;
|
|
|
|
|
2008-02-19 15:43:28 +00:00
|
|
|
open(STATSFILE, ">$statsfile.tmp");
|
2008-01-16 08:36:45 +00:00
|
|
|
|
|
|
|
# print header line
|
|
|
|
print STATSFILE "as";
|
|
|
|
foreach my $link (@links) {
|
|
|
|
print STATSFILE "\t${link}_in\t${link}_out";
|
|
|
|
}
|
|
|
|
print STATSFILE "\n";
|
|
|
|
|
|
|
|
# print data
|
|
|
|
foreach my $as (@asorder) {
|
|
|
|
print STATSFILE "$as";
|
|
|
|
|
|
|
|
foreach my $link (@links) {
|
|
|
|
print STATSFILE "\t" . $astraffic->{$as}->{"${link}_in"};
|
|
|
|
print STATSFILE "\t" . $astraffic->{$as}->{"${link}_out"};
|
|
|
|
}
|
|
|
|
|
|
|
|
print STATSFILE "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
close(STATSFILE);
|
|
|
|
|
2008-02-19 15:43:28 +00:00
|
|
|
rename("$statsfile.tmp", $statsfile);
|
|
|
|
|
2008-01-16 08:36:45 +00:00
|
|
|
sub gettraffic {
|
|
|
|
|
|
|
|
my $as = shift;
|
|
|
|
my $start = shift;
|
|
|
|
my $end = shift;
|
|
|
|
|
|
|
|
my @cmd = ("dummy", "--start", $start, "--end", $end);
|
|
|
|
|
|
|
|
my $retdata = {};
|
|
|
|
|
2009-10-10 16:53:25 +00:00
|
|
|
my $dirname = "$rrdpath/" . sprintf("%02x", $as % 256);
|
|
|
|
my $rrdfile = "$dirname/$as.rrd";
|
|
|
|
|
2008-01-16 08:36:45 +00:00
|
|
|
foreach my $link (@links) {
|
2009-10-10 16:53:25 +00:00
|
|
|
push(@cmd, "DEF:${link}_in=$rrdfile:${link}_in:AVERAGE");
|
|
|
|
push(@cmd, "DEF:${link}_out=$rrdfile:${link}_out:AVERAGE");
|
2008-01-16 08:36:45 +00:00
|
|
|
push(@cmd, "VDEF:${link}_in_v=${link}_in,TOTAL");
|
|
|
|
push(@cmd, "VDEF:${link}_out_v=${link}_out,TOTAL");
|
|
|
|
push(@cmd, "PRINT:${link}_in_v:%lf");
|
|
|
|
push(@cmd, "PRINT:${link}_out_v:%lf");
|
|
|
|
}
|
|
|
|
|
|
|
|
my @res = RRDs::graph(@cmd);
|
|
|
|
my $ERR = RRDs::error;
|
|
|
|
if ($ERR) {
|
|
|
|
die "Error while getting data for $as: $ERR\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $lines = $res[0];
|
|
|
|
|
|
|
|
for (my $i = 0; $i < scalar(@links); $i++) {
|
|
|
|
my $in = $lines->[$i*2];
|
|
|
|
chomp($in);
|
|
|
|
if ($in eq "nan") {
|
|
|
|
$in = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $out = $lines->[$i*2+1];
|
|
|
|
chomp($out);
|
|
|
|
if ($out eq "nan") {
|
|
|
|
$out = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$retdata->{$links[$i] . '_in'} = $in;
|
|
|
|
$retdata->{$links[$i] . '_out'} = $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $retdata;
|
|
|
|
}
|
2008-02-19 15:43:28 +00:00
|
|
|
|
|
|
|
sub read_knownlinks {
|
|
|
|
open(KLFILE, $knownlinksfile) or die("Cannot open $knownlinksfile!");
|
|
|
|
while (<KLFILE>) {
|
|
|
|
chomp;
|
|
|
|
next if (/(^\s*#)|(^\s*$)/); # empty line or comment
|
|
|
|
|
|
|
|
my ($routerip,$ifindex,$tag,$descr,$color) = split(/\t+/);
|
2010-08-17 07:39:58 +00:00
|
|
|
my $known = 0;
|
|
|
|
foreach my $link (values %knownlinks) {
|
|
|
|
if ($tag =~ $link) { $known=1; last; }
|
|
|
|
}
|
|
|
|
if ($known == 0) {
|
|
|
|
$knownlinks{"${routerip}_${ifindex}"} = $tag;
|
|
|
|
}
|
2008-02-19 15:43:28 +00:00
|
|
|
}
|
|
|
|
close(KLFILE);
|
|
|
|
}
|
2009-10-10 16:53:25 +00:00
|
|
|
|