[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.1

1.1     ! andrew      1: #!/usr/bin/perl -T
        !             2: # $RedRiver$
        !             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) {
        !            59:                                $totals{ $names->[$i] }{$key}{'count'}++;
        !            60:                                $totals{ $names->[$i] }{$key}{'total'} += $line->[$i];
        !            61:                        }
        !            62:                }
        !            63:        }
        !            64:        print "\n" if VERBOSE;
        !            65:        $start += $step;
        !            66: }
        !            67:
        !            68:
        !            69: foreach my $key (keys %totals) {
        !            70:        foreach my $length (keys %{ $totals{$key} }) {
        !            71:                $totals{$key}{$length}{'average'} =
        !            72:                     $totals{$key}{$length}{'total'} /
        !            73:                 $totals{$key}{$length}{'count'}
        !            74:                  if $totals{$key}{$length}{'count'};
        !            75:        }
        !            76: }
        !            77:
        !            78: foreach my $key (keys %totals) {
        !            79:        print $key, ": ";
        !            80:        print join ", ",
        !            81:                $totals{$key}{'FIVE_MINUTES'}{'average'},
        !            82:                $totals{$key}{'ONE_HOUR'}{'average'},
        !            83:                $totals{$key}{'ONE_DAY'}{'average'};
        !            84:        print "\n";
        !            85: }
        !            86:
        !            87: #print Dumper \%totals;
        !            88:

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