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

Annotation of nagios/check_rrd/bin/average_rrd, Revision 1.2

1.1       andrew      1: #!/usr/bin/perl -T
1.2     ! andrew      2: # $RedRiver: average_rrd,v 1.1 2006/05/08 19:52:53 andrew Exp $
1.1       andrew      3: use strict;
                      4: use warnings;
                      5:
                      6: use RRDs;
                      7: #use Data::Dumper;
                      8:
                      9: use constant VERBOSE => 0;
                     10:
                     11: my $file = shift || '/var/www/wstationinfo/rrlhcwap3056/rrlhcwap3056-id_6-RSSI.rrd';
                     12:
                     13: my %TIMES = (
                     14:        FIVE_MINUTES =>           5 * 60,
                     15:        ONE_HOUR     =>      1 * 60 * 60,
                     16:        ONE_DAY      => 1 * 24 * 60 * 60,
                     17: );
                     18:
                     19: my $info = RRDs::info($file);
                     20: #print Dumper $info;
                     21: #exit;
                     22:
                     23: my $resolution = $info->{'step'};
                     24: my $last = $info->{'last_update'};
                     25:
                     26: my $end = int($last / $resolution) * $resolution;
                     27: my $start = int( ($end - $TIMES{'ONE_DAY'}) / $resolution) * $resolution;
                     28:
                     29: my ($first, $step, $names, $data) = RRDs::fetch(
                     30:        $file,
                     31:        'AVERAGE',
                     32:        '-r', $resolution,
                     33:        '-s', $start,
                     34:        '-e', $end
                     35: );
                     36: #print Dumper $first, $step, $names, $data;
                     37: #exit;
                     38:
                     39: if (VERBOSE) {
                     40:        print "Last:        ", scalar localtime($last), " ($last)\n";
                     41:        print "Start:       ", scalar localtime($start), " ($start)\n";
                     42:        print "End:         ", scalar localtime($end), " ($end)\n";
                     43:        print "First:       ", scalar localtime($first), " ($first)\n";
                     44:        print "Step size:   $step seconds\n";
                     45:        print "DS names:    ", join (", ", @$names)."\n";
                     46:        print "Data points: ", $#$data + 1, "\n";
                     47:        print "Data:\n";
                     48: }
                     49:
                     50: my %totals;
                     51: foreach my $line (@$data) {
                     52:        print "  ", scalar localtime($start), " ($start) " if VERBOSE;
                     53:        foreach my $i (0 .. $#{ $line }) {
                     54:                printf "%12.1f ", $line->[$i] if VERBOSE;
                     55:
                     56:                next unless defined $line->[$i];
                     57:                foreach my $key (keys %TIMES) {
                     58:                        if ($end - $TIMES{$key} < $start) {
1.2     ! andrew     59:                                foreach ('max', 'min') {
        !            60:                                        $totals{ $names->[$i] }{$key}{$_} = $line->[$i]
        !            61:                                                unless defined $totals{ $names->[$i] }{$key}{$_};
        !            62:                                }
        !            63:                                no warnings q/uninitialized/;
1.1       andrew     64:                                $totals{ $names->[$i] }{$key}{'count'}++;
                     65:                                $totals{ $names->[$i] }{$key}{'total'} += $line->[$i];
1.2     ! andrew     66:                                $totals{ $names->[$i] }{$key}{'max'} = $line->[$i]
        !            67:                                        if $totals{ $names->[$i] }{$key}{'max'} < $line->[$i];
        !            68:                                $totals{ $names->[$i] }{$key}{'min'} = $line->[$i]
        !            69:                                        if $totals{ $names->[$i] }{$key}{'min'} > $line->[$i];
1.1       andrew     70:                        }
                     71:                }
                     72:        }
                     73:        print "\n" if VERBOSE;
                     74:        $start += $step;
                     75: }
                     76:
                     77:
                     78: foreach my $key (keys %totals) {
                     79:        foreach my $length (keys %{ $totals{$key} }) {
                     80:                $totals{$key}{$length}{'average'} =
                     81:                     $totals{$key}{$length}{'total'} /
                     82:                 $totals{$key}{$length}{'count'}
                     83:                  if $totals{$key}{$length}{'count'};
                     84:        }
                     85: }
                     86:
                     87: foreach my $key (keys %totals) {
                     88:        print $key, ": ";
                     89:        print join ", ",
                     90:                $totals{$key}{'FIVE_MINUTES'}{'average'},
                     91:                $totals{$key}{'ONE_HOUR'}{'average'},
                     92:                $totals{$key}{'ONE_DAY'}{'average'};
                     93:        print "\n";
                     94: }
                     95:
                     96: #print Dumper \%totals;
                     97:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>