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

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

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