Changeset 1748

Show
Ignore:
Timestamp:
12/02/08 17:42:39 (1 month ago)
Author:
ton
Message:

New style RRDs created if the old style ones do not exist

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/DEV-opsview3/CHANGES

    r1742 r1748  
    333.0.beta1 ?? 
    44        Convert all mysql datetime fields into UTC 
    5         nagiosgraph forked and now part of opsview-core package 
     5        nagiosgraph forked and now part of opsview-core package. Creates new style RRDs if the old ones do not exist 
    66        Updated scriptaculous to 1.8.2 
    77 
  • branches/DEV-opsview3/opsview-core/bin/insert.pl

    r1742 r1748  
    66#          http://www.opensource.org/licenses/artistic-license.php 
    77 
     8# Portions Copyright (C) 2008 Opsera Limited 
     9 
    810use strict; 
    9 use lib "/usr/local/nagios/perl/lib"
     11use lib "/usr/local/nagios/perl/lib", "/usr/local/nagios/lib"
    1012use RRDs; 
    1113use RRD::Simple; 
    1214use Nagios::Plugin::Performance use_die => 1; 
    13  
    14 $SIG{__DIE__} = sub { debug(1, "DIED exception: @_") }; 
     15use Utils::Nagiosgraph; 
     16 
     17$SIG{__DIE__} = sub { $_ = "@_"; chomp; debug(1, "DIED exception: $_") }; 
    1518 
    1619# Configuration 
     
    8487} 
    8588 
    86 # URL encode a string 
    87 # 
    88 sub urlencode { 
    89   $_[0] =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg; 
    90   return $_[0]; 
    91 } 
    92  
    9389# Create new rrd databases if necessary 
    9490# 
    9591sub createrrd { 
    96   my($host,$service,$start,$labels) = @_; 
     92  my($host,$service,$start,$db,$labels) = @_; 
    9793  my($f,$v,$t,$ds,$db); 
    9894 
    99   $db = shift @$labels; 
    10095  $f = urlencode("${host}_${service}_${db}") . '.rrd'; 
    10196  debug(5, "INSERT Checking $Config{rrddir}/$f"); 
     
    124119sub rrdupdate { 
    125120  my($file,$time,$values) = @_; 
    126   my %ds; 
     121  my $ds; 
    127122 
    128123  my $rrd = RRD::Simple->new( 
    129     file => $Config{rrddir}.'/'.$file, 
     124    file => $file, 
    130125    on_missing_ds => 'add', 
    131126  ); 
    132127 
    133   for ( @$values ) { 
    134     $ds{$_->[0]} = $_->[2] || 0; 
    135   } 
    136  
    137   debug(4, "INSERT RRDs::update ". join ' ', %ds); 
    138   $rrd->update($Config{rrddir}.'/'.$file, $time, %ds); 
    139   debug(2, "INSERT RRDs::update ERR " . RRDs::error) if RRDs::error; 
     128  debug(4, "INSERT RRDs::update ". join ' ', %$values); 
     129  # Need to eval because RRD::Simple will croak on error 
     130  eval { $rrd->update($file, $time, %$values) }; 
     131  # This is not required because the $rrd->update will check for errors and die, which is caught 
     132  #debug(2, "INSERT RRDs::update ERR1 " . RRDs::error) if RRDs::error; 
    140133} 
    141134 
     
    148141  my $s = evalrules($_); 
    149142  if (@$s) { 
    150         return $s; 
     143        # Convert to new hash type here 
     144        # Can assume that only one array is returned (as that is the assumption in earlier insert.pl) 
     145        return convert_map( $s ); 
    151146  } else { 
     147        my @metrics = (); 
     148        my $result = { list => \@metrics, dbname => "" }; 
    152149        local $SIG{__DIE__} = 'DEFAULT'; 
    153150        eval { 
    154151                my @a = Nagios::Plugin::Performance->parse_perfstring($P{perfdata}); 
    155152                if (@a) { 
    156                         my @graphs = (); 
    157153                        foreach my $a (@a) { 
    158                                 my $label = $a->rrdlabel; 
    159                                 push @graphs, [ $label, "GAUGE", $a->value ]; 
    160                                 my $shorter_label = substr($label, 0, 14); 
    161                                 push @graphs, [ $shorter_label."_warn", "GAUGE", $a->threshold->warning->end ] if ($a->threshold->warning->is_set); 
    162                                 push @graphs, [ $shorter_label."_crit", "GAUGE", $a->threshold->critical->end ] if ($a->threshold->critical->is_set); 
     154                                push @metrics, { metric => $a->clean_label, dstype => "GAUGE", value => $a->value, threshold => $a->threshold }; 
    163155                        } 
    164                         push @$s, [ "", @graphs ]; 
    165156                } 
    166157        }; 
     
    168159                debug(2, "PARSE Cannot parse $_: $@" ); 
    169160        } 
    170         return $s
     161        return $result
    171162  } 
    172163} 
     
    200191    my %P = parseinput($l); 
    201192    dumpperfdata(%P); 
    202     my $S = parseperfdata(%P); 
    203     for my $s ( @$S ) { 
    204       my $rrd = createrrd($P{hostname}, $P{servicedescr}, $P{lastcheck}-1, $s); 
    205       rrdupdate($rrd, $P{lastcheck}, $s); 
     193    my $result = parseperfdata(%P); 
     194     
     195    next unless @{$result->{list}}; 
     196 
     197    # If old file exists, update the old fashioned way - this can be dropped in Opsview 4 
     198    my $old_filename = urlencode($P{hostname}."_".$P{servicedescr}."_".$result->{dbname}) . '.rrd'; 
     199    if ( -e "$Config{rrddir}/$old_filename" ) { 
     200      my $ds = {}; 
     201      for ( @{$result->{list}} ) { 
     202        #$ds->{$_->[0]} = $_->[2] || 0; 
     203        $ds->{$_->{metric}} = $_->{value}; 
     204      } 
     205      rrdupdate("$Config{rrddir}/$old_filename", $P{lastcheck}, $ds); 
     206    } else { 
     207      for my $s ( @{$result->{list}} ) { 
     208        my $rrddir = join("/", $Config{rrddir}, urlencode($P{hostname}), urlencode($P{servicedescr}), urlencode($s->{metric})); 
     209        my $values_filename = "$rrddir/value.rrd"; 
     210        if (! -e $values_filename) { 
     211          make_rrd_dir( $Config{rrddir}, urlencode($P{hostname}), urlencode($P{servicedescr}), urlencode($s->{metric}) ); 
     212          my @ds = ($values_filename, "--start", $P{lastcheck}-1); 
     213          my $u = $s->{dstype} eq 'DERIVE' ? '0' : 'U' ; 
     214          push @ds,  
     215            "DS:value:".$s->{dstype}.":$Config{heartbeat}:$u:U", 
     216            "RRA:AVERAGE:0.5:1:600", 
     217            "RRA:AVERAGE:0.5:6:700", 
     218            "RRA:AVERAGE:0.5:24:775", 
     219            "RRA:AVERAGE:0.5:288:797"; 
     220          debug(4, "INSERT RRDs::create @ds"); 
     221          RRDs::create(@ds); 
     222          debug(2, "INSERT RRDs::create ERR " . RRDs::error) if RRDs::error; 
     223        } 
     224        # Update values.rrd 
     225        my @ds = ($values_filename, "$P{lastcheck}:".$s->{value}); 
     226        debug(4, "INSERT RRDs::update @ds"); 
     227        RRDs::update(@ds); 
     228        debug(2, "INSERT RRDs::update ERR " . RRDs::error) if RRDs::error; 
     229         
     230        my %data; 
     231        if ($s->{threshold}) { 
     232          if ($s->{threshold}->warning->is_set) { 
     233            $data{warning_end} = $s->{threshold}->warning->end; 
     234          } 
     235          if ($s->{threshold}->critical->is_set) { 
     236            $data{critical_end} = $s->{threshold}->critical->end; 
     237          } 
     238        } 
     239        if (%data) { 
     240          # We need to create the rrd. If RRD::Simple does it, will choose large retention periods 
     241          my $thresholds_rrd = "$rrddir/thresholds.rrd"; 
     242          if (! -e $thresholds_rrd) { 
     243            # We duplicate this section. In future, we may have different parameters for threshold storage 
     244            my @ds = ($thresholds_rrd, "--start", $P{lastcheck}-1); 
     245            my $u = $s->{dstype} eq 'DERIVE' ? '0' : 'U' ; 
     246            push @ds,  
     247              "DS:warning_end:".$s->{dstype}.":$Config{heartbeat}:$u:U", 
     248              "DS:critical_end:".$s->{dstype}.":$Config{heartbeat}:$u:U", 
     249              "RRA:AVERAGE:0.5:1:600", 
     250              "RRA:AVERAGE:0.5:6:700", 
     251              "RRA:AVERAGE:0.5:24:775", 
     252              "RRA:AVERAGE:0.5:288:797"; 
     253            debug(4, "INSERT RRDs::create::thresholds @ds"); 
     254            RRDs::create(@ds); 
     255            debug(2, "INSERT RRDs::create::thresholds ERR " . RRDs::error) if RRDs::error; 
     256          } 
     257          # Update thresholds.rrd - use RRD::Simple because may extend in future with other threshold information 
     258          rrdupdate( $thresholds_rrd, $P{lastcheck}, \%data ); 
     259        } 
     260      } 
    206261    } 
    207262  } 
     263} 
     264 
     265sub make_rrd_dir { 
     266        my ($root, $hostname, $servicename, $metric) = @_; 
     267        mkdir "$root/$hostname" unless (-d "$root/$hostname"); 
     268        mkdir "$root/$hostname/$servicename" unless (-d "$root/$hostname/$servicename"); 
     269        mkdir "$root/$hostname/$servicename/$metric" unless (-d "$root/$hostname/$servicename/$metric"); 
    208270} 
    209271 
  • branches/DEV-opsview3/opsview-core/installer/migrate_rrds

    r1742 r1748  
    210210                # RRDrestore to create the new rrd - create directory structure first 
    211211                my $metric = $pot_for_metric->{$i}; 
    212                 my $rrdname = "values"; 
     212                my $rrdname = "value"; 
    213213                if ($pot_for_thresholds->{$i}) { 
    214214                        $rrdname = "thresholds";