8 extractDS.px - Extract a single data-source from an RRD-file
12 extractDS.px -i input.rrd -s source_ds -o output.rrd -d destination_ds
16 extractDS.px requires Perl and the included L<Getopt::Long> module, as well as
17 the L<XML::Simple> module.
21 use Getopt::Long ('GetOptions');
22 use XML::Simple (qw(xml_in xml_out));
30 GetOptions ("infile|i=s" => \$InFile,
32 "outfile|o=s" => \$OutFile,
33 "outds|d=s" => \$OutDS) or exit (1);
35 if (!$InFile || !$OutFile || !$InDS || !$OutDS)
37 print "$InFile $InDS $OutFile $OutDS\n";
38 print STDERR "Usage: $0 -i <infile> -I <inds> -o <outfile> -O <outds>\n";
43 print STDERR "Input file does not exist\n";
48 print STDERR "Output file does exist\n";
52 extract_ds ($InFile, $OutFile, $InDS, $OutDS);
57 my $current_index = -1;
58 # state 0 == searching for DS index
59 # state 1 == parse RRA header
60 # state 2 == parse <ds> in RRA header
61 # state 3 == parse values
69 if (!defined ($state))
84 elsif ($line =~ m#<name>\s*([^<\s]+)\s*</name>#)
88 $ds_index = $current_index;
89 $out_cache .= "\t\t<name>$OutDS</name>\n";
92 elsif ($line =~ m#</ds>#)
95 if ($ds_index == $current_index)
100 elsif ($line =~ m#<rra>#)
106 elsif ($current_index == -1)
117 if ($line =~ m#<ds>#)
120 if ($current_index == $ds_index)
125 if ($line =~ m#</ds>#) { $state = 1; }
128 elsif ($line =~ m#<database>#)
140 if ($current_index == $ds_index)
145 if ($line =~ m#</ds>#)
152 if ($line =~ m#</database>#)
160 my $line_begin = "\t\t";
162 if ($line =~ m#(<!-- .*? -->)#)
164 $line_begin .= "$1 ";
167 while ($line =~ m#<v>\s*([^<\s]+)\s*</v>#)
170 if ($current_index == $ds_index)
172 print $fh "$line_begin<row> <v>$value</v> </row>\n";
184 my $out_file = shift;
191 open ($in_fh, '-|', 'rrdtool', 'dump', $in_file) or die ("open (rrdtool): $!");
192 open ($out_fh, '|-', 'rrdtool', 'restore', '-', $out_file) or die ("open (rrdtool): $!");
194 while (my $line = <$in_fh>)
196 handle_line ($out_fh, $line);
205 Florian octo Forster E<lt>octo at verplant.orgE<gt>