[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.1, Mon May 8 19:52:53 2006 UTC (18 years, 2 months ago) by andrew
Branch: MAIN

Get started on something so I can check stuff we graph.  One kewl application will be to be able to monitor our customers signal strength and get notified if their signal goes to crap and contact them.  We will be able to be much more proactive!

#!/usr/bin/perl -T
# $RedRiver$
use strict;
use warnings;

use RRDs;
#use Data::Dumper;

use constant VERBOSE => 0;

my $file = shift || '/var/www/wstationinfo/rrlhcwap3056/rrlhcwap3056-id_6-RSSI.rrd';

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) {
				$totals{ $names->[$i] }{$key}{'count'}++;
				$totals{ $names->[$i] }{$key}{'total'} += $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;