#!/usr/bin/perl##		For more info about this script, see http://www.seanadams.com#	2002-10-22	[BB]	Rebuilt to handle multi-level MRTG folders by Brian Blood <brian@n0-5pam-macserve.net>#	2003-01-02	[BB]	fix for some variable init bugs (Thanks Mark)#	2003-04-09	[SS]	fix for recursive directories by Shawn Sterling <shawnsterling@n0-5pam-shaw.ca>#$snapsUtilizeDays = 30;		# Number of days to utilize in calculating 95th percentile$snapsRetainDays = 60;		# Max number of days of snapshots to maintain$basedir="/Volumes/Data/MacServe/mrtg/data";		# This directory where mrtg saves its .log and .html files$snapdir="/Volumes/Data/MacServe/mrtg/snapshots";   	# Where 95.pl will save a daily copy of the .log files							# [SS] Note: 95.pl will NOT recurse into your snapdir. $now=0;							# [SS] added this so perl (-w) won't complain.#$today=`date +\%Y\%m\%d`;$today=`date +\%Y-\%m-\%d`;chop $today;#$snapdir="$snapshots/$today";#`mkdir $snapshots` if (!-d$snapshots);#`mkdir $snapdir` if (!-d$snapdir);&ExtractMRTGLogVals($basedir, $snapdir);exit(0);						# [SS] my prefered exitsub	ExtractMRTGLogVals	{	my $basedir = $_[0];	my $Osnapdir = $_[1];				# [SS] $snapdir is now a global, $Osnapdir is for the sub (O as in Other :) )#	print "Checking directory: $basedir\n";#	print "Snapshots going to: $Osnapdir\n";#		Check to see if we have this directory in the Snaps dir and create if we don't	`mkdir $Osnapdir` if (!-d$Osnapdir);	opendir WWW, $basedir;	foreach my $dirItem (readdir(WWW))	{#		print "Current Item: $dirItem\n";		next if ($dirItem eq '.');		next if ($dirItem eq '..');		my $dirItemPath = "$basedir/$dirItem";		if ((-d$dirItemPath)and($dirItemPath ne $snapdir))		# [SS] checks if this dir is recursive w/snapdir		{#			print "Dropping into directory: $dirItemPath\n";			my $curSnapsPath = "$Osnapdir/$dirItem";			&ExtractMRTGLogVals($dirItemPath, $curSnapsPath);#			print "Returning from directory: $dirItemPath\n";			next;		}# continue with loop if the item doesn't have .log in it's name		next if (!($dirItem=~/\.log$/));				$target = $dirItem;		$target=~s/\.log//;#		print "$dirItem\n";				$targetSnapDir = "$Osnapdir/$dirItem";		`mkdir $targetSnapDir` if (!-d$targetSnapDir);#		print "Target Snaps Path: $targetSnapDir\n";				open INFILE, "$basedir/$dirItem" || die "Couldn't read $basedir/$dirItem";		open OUTFILE, ">$targetSnapDir/$today" || die "Couldn't write to $targetSnapDir/$today";		$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/$dirItem";			print OUTFILE "$1 $2 $3\n";		}			close INFILE;		close OUTFILE;		# Now that we've captured the latest data from this log, process the 95th percentile# on the last $snapsUtilizeDays days.		# open the snapshots directory for this target# and get a list of all the dates		@dates=();		opendir SNAPSHOTS, $targetSnapDir;		foreach $date (readdir(SNAPSHOTS))		{			next if ($date eq '.');			next if ($date eq '..');						push @dates,$date;		}		closedir(SNAPSHOTS);# sort this list		@dates=sort {$a<=>$b} @dates;# as the list is being paired down to $snapsRetainDays, remove the old snaps file.		while ((@dates) > $snapsRetainDays)		{			$killDate = shift(@dates);			unlink("$targetSnapDir/$killDate");		}# pair the list down to only the $snapsUtilizeDays most recent values		while ((@dates) > $snapsUtilizeDays) {			shift(@dates);			}		# Roll through each of the dates we've picked up# Read each file and append the lines of that file# to the element corresponding to the files base name# within the $data array#		$data='';		foreach $date (@dates)		{			open LOG, "$targetSnapDir/$date";			$data .= join('',<LOG>);			close(LOG);		}		# Roll through the $data array# pick out the lines and do the 95th % calc		@lines=split(/\n/,$data);		@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";		close(DATA95);	}		#end of reading through this particular directory.	closedir(WWW);}