[BACK]Return to average_rrd CVS log [TXT][DIR] Up to [local] / nagios / check_rrd / bin

File: [local] / nagios / check_rrd / bin / average_rrd (download)

Revision 1.3, Tue Nov 7 19:55:30 2006 UTC (17 years, 6 months ago) by andrew
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +4 -2 lines

add a test RRD so I can work on this other places

#!/usr/bin/perl -T
# $RedRiver: average_rrd,v 1.2 2006/05/08 22:22:19 andrew Exp $
use strict;
use warnings;

use RRDs;
#use Data::Dumper;

use constant VERBOSE => 0;

my $file = shift || 'test.rrd';

die "File '$file' does not exist!" unless -e $file;

my %TIMES = (
	FIVE_MINUTES =>           5 * 60,
	ONE_HOUR     =>      1 * 60 * 60,
	ONE_DAY      => 1 * 24 * 60 * 60,
);

my $info = RRDs::info($file);
#print Dumper $info;
#exit;

my $resolution = $info->{'step'};
my $last = $info->{'last_update'};

my $end = int($last / $resolution) * $resolution;
my $start = int( ($end - $TIMES{'ONE_DAY'}) / $resolution) * $resolution;

my ($first, $step, $names, $data) = RRDs::fetch(
	$file, 
	'AVERAGE', 
	'-r', $resolution, 
	'-s', $start, 
	'-e', $end
);
#print Dumper $first, $step, $names, $data;
#exit;

if (VERBOSE) {
	print "Last:        ", scalar localtime($last), " ($last)\n";
	print "Start:       ", scalar localtime($start), " ($start)\n";
	print "End:         ", scalar localtime($end), " ($end)\n";
	print "First:       ", scalar localtime($first), " ($first)\n";
	print "Step size:   $step seconds\n";
	print "DS names:    ", join (", ", @$names)."\n";
	print "Data points: ", $#$data + 1, "\n";
	print "Data:\n";
}

my %totals;
foreach my $line (@$data) {
	print "  ", scalar localtime($start), " ($start) " if VERBOSE;
	foreach my $i (0 .. $#{ $line }) {
		printf "%12.1f ", $line->[$i] if VERBOSE; 

		next unless defined $line->[$i];
		foreach my $key (keys %TIMES) {
			if ($end - $TIMES{$key} < $start) {
				foreach ('max', 'min') {
					$totals{ $names->[$i] }{$key}{$_} = $line->[$i]
						unless defined $totals{ $names->[$i] }{$key}{$_};
				}
				no warnings q/uninitialized/;
				$totals{ $names->[$i] }{$key}{'count'}++;
				$totals{ $names->[$i] }{$key}{'total'} += $line->[$i];
				$totals{ $names->[$i] }{$key}{'max'} = $line->[$i]
					if $totals{ $names->[$i] }{$key}{'max'} < $line->[$i];
				$totals{ $names->[$i] }{$key}{'min'} = $line->[$i]
					if $totals{ $names->[$i] }{$key}{'min'} > $line->[$i];
			}
		}
	}
	print "\n" if VERBOSE;
	$start += $step;
}


foreach my $key (keys %totals) {
	foreach my $length (keys %{ $totals{$key} }) {
		$totals{$key}{$length}{'average'} = 
		     $totals{$key}{$length}{'total'} / 
	         $totals{$key}{$length}{'count'}
		  if $totals{$key}{$length}{'count'};
	}
}

foreach my $key (keys %totals) {
	print $key, ": ";
	print join ", ", 
		$totals{$key}{'FIVE_MINUTES'}{'average'},
		$totals{$key}{'ONE_HOUR'}{'average'},
		$totals{$key}{'ONE_DAY'}{'average'};
	print "\n";
}

#print Dumper \%totals;