[mrtg] Re: Whither the 95th percentile parser?

Pete Templin templin at urdirect.net
Thu Dec 23 20:26:32 MET 1999


I've modified the two programs I've been using to track 95th percentile
statistics.  Those using these programs should update, as some data was
either not being archived or not being reported correctly.  I apologize
for not having diffs - I'm not (yet) in the habit of keeping that good of
a record of my work.  :(

mrtg_archive:

#!/usr/bin/perl
#

# Configurations
$mrtg_dir = "/usr/local/mrtg";
$archive_dir = "/usr/local/mrtg/archives";
$conf = "/usr/local/scripts/mrtg_archive.conf";

# Read a config file if present.
if (-e $conf) {
    open(CONF,"$conf");
    while(<CONF>) {
	chomp;
	push(@elements,$_);
    }
}

# Take arguments from the command line if present.
@elements = @ARGV if (@ARGV);

# Fail if no arguments.
die "Need a list of parameters to monitor!\n" unless (@elements);

# Process each of the elements
for ($a=0; $a<=$#elements; $a++) {
    $count = undef;
    ($path,$comment) = split(/:/,$elements[$a]);
    (@path) = split(/\//,$path);
    unless (-d "$archive_dir/$path[0]") {
	system("mkdir $archive_dir/$path[0]");
    }

    unless (-f "$archive_dir/$path") {
	system("touch $archive_dir/$path");
    }

    open(LOG,"$mrtg_dir/$path");
    open(COPY,">>$archive_dir/$path");
    while (<LOG>) {
	if ($count >= 3) {
	    print COPY;
	}
	if ($count >= 598) {
	    last;
	}
	$count++;
    }
}


mrtg95:

#!/usr/bin/perl
#

# Configurations
$archive_dir = "/usr/local/mrtg/archives";
$threshold = "95";
$conf = "/usr/local/scripts/mrtg_archive.conf";
@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

# Check for configuration file
if (-e $conf) {
    open(CONF,"$conf");
    while(<CONF>) {
	chomp;
	push(@elements,$_);
    }
}

# Use command-line arguments
@elements = @ARGV if (@ARGV);

# Fail if no arguments present
die "Need a list of parameters to monitor!\n" unless (@elements);

# Process the elements
for ($a=0; $a<=$#elements; $a++) {	# Each logfile as an argument.
    %data = undef;
    %record = undef;

    ($path,$comment) = split(/:/,$elements[$a]);
    open(LOG,"$archive_dir/$path") || next;
    while (<LOG>) {
	(@data) = split(/ /);
	@date_info = localtime($data[0]); # Get the date info
	$date_info[5] = $date_info[5] + 1900; # Calendar Year
	if ($date_info[4] < 10) {
	    $date_info[4] = "0" . $date_info[4];
	}
	$date_info[4] = "00" unless ($date_info[4]);
	$datestamp = "$date_info[5]$date_info[4]";
	$record{$datestamp} = 1;	# Know which months our data covers
	$data{$datestamp}{$data[0]} = "$data[1]:$data[2]";
    }

    @dates = keys ( %record );
    @sorted_dates = sort {$a <=> $b} @dates;

    for ($b=0; $b<=$#sorted_dates; $b++) { # Loop through each month
	@in = undef;
	@out = undef;

	$year = substr($sorted_dates[$b],0,4);
	$month = substr($sorted_dates[$b],4,2);
	$month_name = $months[$month];

	@values = values %{ $data{$sorted_dates[$b]}};

	$entries = scalar (@values);

	$index = -1 + int($entries * $threshold / 100);

	for ($c=0; $c<=$#values; $c++) {
	    ($in,$out) = split(/:/,$values[$c]);
	    push(@in,$in);
	    push(@out,$out);
	}
	
	@sort_in = sort {$a <=> $b} @in;
	@sort_out = sort {$a <=> $b} @out;

	$high_in = $sort_in[$index];
	$high_out = $sort_out[$index];

	$level = $high_in;
	$level = $high_out if ($high_out > $level);

	$level = $level * 8;

	if ($comment) {
	    print "$comment usage for $month_name $ year is ";
	}
	print "$level\n";
    }
}





Pete

--
Peter J. Templin, Jr., CCNA
Systems and Networks Administrator

On-Line Internet Services - URDirect.net
A division of Global On-Line Computers
2414 Babcock Rd. Suite 106		templin at urdirect.net
San Antonio, TX 78229			(210)692-9911

--
Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:mrtg-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/mrtg



More information about the mrtg mailing list