MRTG
Jump to navigation
Jump to search
Multi Router Traffic Grapher
95th Percentile
Patch
Below is the patch to apply to the main mrtg file.
# cd /usr/bin # nano mrtg-2.17.3_95patch
--- mrtg 2014-02-24 10:07:34.000000000 -0600
+++ mrtg.95 2014-02-24 11:13:58.497671950 -0600
@@ -1445,7 +1445,7 @@
$avin, $avout, $avmxin, $avmxout, $avpercent,
$cuin, $cuout, $cupercent, $uptime, $name, $LOC) = @_;
- my($VERSION,$Today,$peri);
+ my($VERSION,$Today,$peri,$nf);
my($persec);
@@ -1505,6 +1505,8 @@
$Today=&$LOC(datestr(time));
$VERSION = "2.17.3";
+ open NF, "$$cfg{'workdir'}${main::SL}$$rcfg{'directory'}{$router}$router.95" or die "Could not open percentile file, $!";
+ $nf=<NF>;
open (HTML,">$$cfg{'htmldir'}$$rcfg{'directory'}{$router}$router.$$rcfg{'extension'}{$router}") ||
do { warn ("$NOW: WARNING: Writing $router.$$rcfg{'extension'}{$router}: $!");
next };
@@ -1679,6 +1681,7 @@
",<br />\n".
&$LOC("at which time $namestring had been up for <strong>$uptime</strong>.")
}
+ print HTML "<br>Last 30 days usage (Metered 95%):<font color=\"red\"> $nf kb/sec</font>";
print HTML "</p>\n<!-- End Head -->";
my %sample= ('d' => "`Daily' Graph (".$interval.' Minute',
# patch < mrtg-2.17.3_95patch
95.pl
# cd /var/www/mrtg # nano 95.pl
#!/usr/bin/perl
#
# For more info about this script, see http://www.seanadams.com
#
$basedir= "/var/www/mrtg/$ARGV[0]"; # This directory where mrtg saves its .log and .html files
$snapshots="/var/www/mrtg/$ARGV[0]/snapshots"; # Where 95.pl will save a daily copy of the .log files
$today=`date +\%Y\%m\%d`;
chop $today;
$snapdir="$snapshots/$today";
if (-d $basedir) {
} else {
print "\t$basedir does not exist or is a file!\n";
exit 0;
}
`mkdir $snapshots` if (!-d$snapshots);
`mkdir $snapdir` if (!-d$snapdir);
opendir WWW, $basedir;
foreach $logname (readdir(WWW)) {
next if (!($logname=~/\.log$/));
#print "$logname\n";
open INFILE, "$basedir/$logname" || die "Couldn't read $basedir/$logname";
open OUTFILE, ">$snapdir/$logname" || die "Couldn't write to $snapdir/$logname";
$now=<INFILE>;
for ($i=1;$i<=288;$i++) { # 288 five minute samples for a 24hr window
$sample=<INFILE>;
chop $sample;
$sample=~/(\d+) \d+ \d+ (\d+) (\d+)/ || die "Couldn't parse $basedir/$logname";
print OUTFILE "$1 $2 $3\n";
}
close INFILE;
close OUTFILE;
}
opendir SNAPSHOTS, "$snapshots";
foreach $date (readdir(SNAPSHOTS)) {
next if ($date eq '.');
next if ($date eq '..');
push @dates,$date;
}
@dates=sort {$a<=>$b} @dates;
while ((@dates)>30) {
shift(@dates);
}
foreach $date (@dates) {
opendir SNAPSHOTS, "$snapshots/$date";
foreach $log (readdir(SNAPSHOTS)) {
next if ($log eq '.');
next if ($log eq '..');
open LOG, "$snapshots/$date/$log";
$log=~s/\.log//;
$data{$log}.=join('',<LOG>);
}
}
foreach $target (keys(%data)) {
@lines=split(/\n/,$data{$target});
@samples=();
%samples=();
foreach $line (@lines) {
($time,$in,$out)=split(/\s+/,$line);
if ($in>$out) {
$samples{$time}=$in; #filter identical times using a hash
} else {
$samples{$time}=$out;
}
}
foreach $key (keys(%samples)) {
push @samples,$samples{$key};
}
$nfindex=int((@samples)*.95);
@sorted=sort {$a<=>$b} @samples;
$nf=int($sorted[$nfindex]*8/1024*100)/100;
open DATA95, ">$basedir/$target.95";
print DATA95 "$nf";
}
Cron
# nano /etc/cron.d/mrtg
# Run MRTG every five minutes: */5 * * * * root /usr/bin/mrtg /etc/mrtg.cfg # Run 95.pl every hour, at one minute past the hour 1 * * * * root /var/www/mrtg/95.pl