#!/usr/bin/perl
#
# For more info about this script, see http://www.seanadams.com
#
$basedir= "/www/www.seanadams.com/mrtgfiles"; # This directory where mrtg saves its .log and .html files
$snapshots="/www/www.seanadams.com/mrtgfiles/snapshots"; # Where 95.pl will save a daily copy of the .log files
$today=`date +\%Y\%m\%d`;
chop $today;
$snapdir="$snapshots/$today";
`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=;
for ($i=1;$i<=288;$i++) { # 288 five minute samples for a 24hr window
$sample=;
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('',);
}
}
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";
}