jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-pre6/] [tools/] [rrdtool_add_ds.pl] - Rev 8 Go to most recent revision

Compare with Previous - Blame - Download


#! /usr/bin/perl

# add_ds.pl, program to add datasources to an existing RRD
#
#    Copyright (C) 2000 Selena M. Brewington
#    fixed 30-JUL-2003 by Taylor A. Steil
#    verified working with rrdtool version 1.1.x
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use strict;

my $ds = shift || die "need number of additional datasources desired";
if ($ds eq '-h') {
  &Usage;
  exit 0;
  }

my $default_val = shift || 'NaN';
my $type = shift || 'GAUGE';
my $heartbeat = shift || '1800';
my $rrdmin = shift || '0';
my $rrdmax = shift || 'NaN';

my $cdp_prep_start = '<cdp_prep>';
my $cdp_prep_end = '</cdp_prep>';

my $row_end = '</row>';
my $name = '<name>';
my $name_end = '</name>';
my $ds_end = '</ds>';

my $field = '<v> ' . $default_val . ' </v>';

my $found_ds = 0;
my $num_sources = 0;
my $last;
my $fields = " ";
my $datasource;
my $record;
my $x;
my $dsData;

while (<STDIN>) {

  if (/$cdp_prep_start/) {
    $record = 1;
    }
  elsif ($record) {
    if (/$ds_end/) {
      $dsData .= $_;
      $record = 0;
      }
    else {
      my $line = $_;
      if ($line =~ s/(<minimal_heartbeat>)(.*?)(<\/minimal_heartbeat>)/$1 $2 $3/) {}
      elsif ($line =~ s/(<min>)(.*?)(<\/min>)/$1 $rrdmin $3/) {}
      elsif ($line =~ s/(<max>)(.*?)(<\/max>)/$1 $rrdmax $3/) {}
      elsif ($line =~ s/(<last_ds>)(.*?)(<\/last_ds>)/$1 UNKN $3/) {}
      elsif ($line =~ s/(<value>)(.*?)(<\/value>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<unknown_sec>)(.*?)(<\/unknown_sec>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<primary_value>)(.*?)(<\/primary_value>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<secondary_value>)(.*?)(<\/secondary_value>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<intercept>)(.*?)(<\/intercept>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<last_intercept>)(.*?)(<\/last_intercept>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<slope>)(.*?)(<\/slope>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<last_slope>)(.*?)(<\/last_slope>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<nan_count>)(.*?)(<\/nan_count>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<last_nan_count>)(.*?)(<\/last_nan_count>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<seasonal>)(.*?)(<\/seasonal>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<last_seasonal>)(.*?)(<\/last_seasonal>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<init_flag>)(.*?)(<\/init_flag>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<history>)(.*?)(<\/history>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<unknown_datapoints>)(.*?)(<\/unknown_datapoints>)/$1 $default_val $3/) {}
      elsif ($line =~ s/(<\w+>)(.*?)(<\/\w+>)/$1 $default_val $3/) {}
      $dsData .= $line;
      }
    }
  
  if (($_ =~ s/$row_end$/$fields$row_end/) && $found_ds) {
    # need to hit <ds> types first, if we don't, we're screwed
    print $_;
    }
  elsif (/$cdp_prep_end/) {
    print $dsData x $ds;
    print $_;
    $dsData = '';
    }
  elsif (/$name_end$/) {
    ($datasource) = /$name (\w+)/;
    $found_ds++;
    print $_;
    }
  elsif (/Round Robin Archives/) {
    # print out additional datasource definitions
    ($num_sources) = ($datasource =~ /(\d+)/);
    for ($x = $num_sources+1; $x < $num_sources+$ds+1; $x++) {
      $fields .= $field;
      print "\n\t<ds>\n";
      print "\t\t<name> ds$x <\/name>\n";
      print "\t\t<type> $type <\/type>\n";
      print "\t\t<minimal_heartbeat> $heartbeat <\/minimal_heartbeat>\n";
      print "\t\t<min> $rrdmin <\/min>\n";
      print "\t\t<max> $rrdmax <\/max>\n\n";
      print "\t\t<!-- PDP Status -->\n";
      print "\t\t<last_ds> NaN <\/last_ds>\n";
      print "\t\t<value> 0.0000000000e+00 <\/value>\n";
      print "\t\t<unknown_sec> NaN <\/unknown_sec>\n";
      print "\t<\/ds>\n\n";
      }
    print $_;
    }
  else { print $_; }
  $last = $_;
  }


sub Usage {
  print "rrdtool_add_ds.pl <add'l ds> [default_val] [type] [heartbeat] [rrdmin] [rrdmax] < file.xml\n";
  print "\t<add'l ds>\tnumber of additional datasources\n";
  print "\t[default_val]\tdefault value to be entered in add'l fields\n";
  print "\t[type]\ttype of datasource (i.e. COUNTER, GAUGE...)\n";
  print "\t[heatbeat]\tlength of time in seconds before RRD thinks your DS is dead\n";
  print "\t[rrdmin]\tminimum value allowed for each datasource\n";
  print "\t[rrdmax]\tmax value allowed for each datasource\n\n";
  print "\tOptions are read in order, so if you want to change the\n";
  print "\tdefault heartbeat, you need to specify the default_val and\n";
  print "\ttype as well, etc.\n";
  print "\n\tOutput goes to STDOUT.\n";
  }


Powered by WebSVN 2.2.1