Advertisement
danielhilst

genline.pl

Sep 3rd, 2012
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.82 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # $id: Daniel Hilst, danielhilst at gmail.com
  3. # Description:
  4. # This script will read rddinfo output and generate
  5. # the rrdcreate line needed to create that file. So
  6. # you can change parameters and recreate the file
  7. #
  8. # @TODO: Remove old redundant code.
  9. #
  10.  
  11. use warnings;
  12. use strict;
  13. use Data::Dumper;
  14.  
  15.  
  16. my $dss_index = 0;
  17. my $rrd = {
  18.     filename => undef,
  19.     step => undef,
  20.     dss_ar => [],
  21.     dss => {},
  22.     rras => [],
  23. };
  24.  
  25. while (<>) {
  26.     chomp;
  27. #    print $_, "\n";
  28.  
  29.     if (/filename = "(.+)"/) {
  30.         $rrd->{filename} = $1;
  31.     }
  32.  
  33.     if (/step = (\d+)/) {
  34.         $rrd->{step} = $1;
  35.  
  36.     } elsif (/ds\[(\w+)\]/) {
  37.         my $ds = $1;
  38.  
  39.         unless ($rrd->{dss}->{$ds}) {
  40.             $rrd->{dss}->{$ds} = {};
  41.             $rrd->{dss_ar}->[$dss_index] = $rrd->{dss}->{$ds};
  42.             $dss_index++;
  43.         }
  44.  
  45.         $rrd->{dss}->{$ds}->{ds_name} = $ds;
  46.  
  47.         if (/ds\[\w+\]\.type = "(\w+)"/) {
  48.             my $type = $1;
  49.             $rrd->{dss}->{$ds}->{type} = $type;
  50.         }
  51.  
  52.         if (/ds\[\w+\]\.minimal_heartbeat = (\d+)/) {
  53.             $rrd->{dss}->{$ds}->{minimal_heartbeat} = $1;
  54.         }
  55.  
  56.         if (/ds\[\w+\]\.min = (.+)/) {
  57.             my $min = $1;
  58.             $min = sprintf("%f", $min);
  59.             $min = "U" if $min =~ /nan/;
  60.             $rrd->{dss}->{$ds}->{min} = $min;
  61.         }
  62.  
  63.         if (/ds\[\w+\]\.max = (.+)/) {
  64.             my $max = $1;
  65.             $max = sprintf("%f", $max);
  66.             $max = "U" if $max =~ /nan/;
  67.             $rrd->{dss}->{$ds}->{max} = $max;
  68.         }
  69.  
  70. #        print "DS[$ds]  => ", $_, "\n"
  71.     } elsif (/rra\[(\d+)\]/) {
  72.         my $i = $1;
  73.         $rrd->{rras}->[$i] = {} unless $rrd->{rras}->[$i];
  74.  
  75.         if (/rra\[\d+\].cf = ("\w+")/) {
  76.             $rrd->{rras}->[$i]->{cf} = $1;
  77.         }
  78.  
  79.         if (/rra\[\d+\].rows = (\w+)/) {
  80.             $rrd->{rras}->[$i]->{rows} = $1;
  81.         }
  82.  
  83.         if (/rra\[\d+\].pdp_per_row = (\w+)/) {
  84.             $rrd->{rras}->[$i]->{pdp_per_row} = $1;
  85.         }
  86.  
  87.         if (/rra\[\d+\].xff = (.+)/) {
  88.             my $xff = $1;
  89.             $xff = sprintf("%f", $xff);
  90.             $rrd->{rras}->[$i]->{xff} = $xff;
  91.         }
  92.  
  93.  
  94. #        print "RRA  => ", $_, "\n"
  95.     }
  96. }
  97.  
  98. #print Dumper($rrd);
  99.  
  100. my $cline = "rrdtool create \"" . $rrd->{filename} . "\" --step " . $rrd->{step} . " \\\n";
  101.  
  102. for my $ds (@{ $rrd->{dss_ar} }) {
  103.     my $ds_name = $ds->{ds_name};
  104.     $cline .= " DS:" . $ds_name . ":" . $rrd->{dss}->{$ds_name}->{type} . ":" . $rrd->{dss}->{$ds_name}->{minimal_heartbeat} . ":" . $rrd->{dss}->{$ds_name}->{min} . ":" . $rrd->{dss}->{$ds_name}->{max} . " \\\n";
  105. }
  106.  
  107. for my $rra (@{$rrd->{rras}}) {
  108.     $cline .= " RRA:" . $rra->{cf} . ":" . $rra->{xff} . ":" . $rra->{pdp_per_row} . ":" . $rra->{rows} . " \\\n";
  109. }
  110.  
  111. print $cline, "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement