#!/usr/bin/perl use strict; use warnings; =head1 NAME extractDS.px - Extract a single data-source from an RRD-file =head1 SYNOPSYS extractDS.px -i input.rrd -s source_ds -o output.rrd -d destination_ds =head1 DEPENDENCIES extractDS.px requires Perl and the included L module, as well as the L module. =cut use Getopt::Long ('GetOptions'); use XML::Simple (qw(xml_in xml_out)); use Data::Dumper (); our $InFile; our $InDS; our $OutFile; our $OutDS; GetOptions ("infile|i=s" => \$InFile, "inds|s=s" => \$InDS, "outfile|o=s" => \$OutFile, "outds|d=s" => \$OutDS) or exit (1); if (!$InFile || !$OutFile || !$InDS || !$OutDS) { print "$InFile $InDS $OutFile $OutDS\n"; print STDERR "Usage: $0 -i -I -o -O \n"; exit (1); } if (!-f $InFile) { print STDERR "Input file does not exist\n"; exit (1); } if (-f $OutFile) { print STDERR "Output file does exist\n"; exit (1); } extract_ds ($InFile, $OutFile, $InDS, $OutDS); exit (0); { my $ds_index = -1; my $current_index = -1; # state 0 == searching for DS index # state 1 == parse RRA header # state 2 == parse in RRA header # state 3 == parse values my $state = 0; my $out_cache = ''; sub handle_line { my $fh = shift; my $line = shift; if (!defined ($state)) { $ds_index = -1; $current_index = -1; $state = 0; $out_cache = ''; } if ($state == 0) { if ($line =~ m//) { $out_cache = $line; $current_index++; } elsif ($line =~ m#\s*([^<\s]+)\s*#) { if ($1 eq $InDS) { $ds_index = $current_index; $out_cache .= "\t\t$OutDS\n"; } } elsif ($line =~ m##) { $out_cache .= $line; if ($ds_index == $current_index) { print $fh $out_cache; } } elsif ($line =~ m##) { print $fh $line; $current_index = -1; $state = 1; } elsif ($current_index == -1) { print $fh $line; } else { $out_cache .= $line; } } elsif ($state == 1) { if ($line =~ m##) { $current_index++; if ($current_index == $ds_index) { print $fh $line; } if ($line =~ m##) { $state = 1; } else { $state = 2; } } elsif ($line =~ m##) { print $fh $line; $state = 3; } else { print $fh $line; } } elsif ($state == 2) { if ($current_index == $ds_index) { print STDERR $line; print $fh $line; } if ($line =~ m##) { $state = 1; } } else { if ($line =~ m##) { print $fh $line; $current_index = -1; $state = 1; } else { my $line_begin = "\t\t"; $current_index = 0; if ($line =~ m#()#) { $line_begin .= "$1 "; } while ($line =~ m#\s*([^<\s]+)\s*#) { my $value = $1; if ($current_index == $ds_index) { print $fh "$line_begin $value \n"; last; } $current_index++; } } } }} # handle_line sub extract_ds { my $in_file = shift; my $out_file = shift; my $in_ds = shift; my $out_ds = shift; my $in_fh; my $out_fh; open ($in_fh, '-|', 'rrdtool', 'dump', $in_file) or die ("open (rrdtool): $!"); open ($out_fh, '|-', 'rrdtool', 'restore', '-', $out_file) or die ("open (rrdtool): $!"); while (my $line = <$in_fh>) { handle_line ($out_fh, $line); } close ($in_fh); close ($out_fh); } # extract_ds =head1 AUTHOR Florian octo Forster Eocto at verplant.orgE