3 # collectd - contrib/rrd_filter.px
4 # Copyright (C) 2007-2008 Florian octo Forster
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; only version 2 of the License is applicable.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # Florian octo Forster <octo at verplant.org>
27 rrd_filter.px - Perform same advanced non-standard operations on an RRD file.
31 rrd_filter.px -i input.rrd -o output.rrd [options]
35 rrd_filter.px requires the RRDTool binary, Perl and the included
36 L<Getopt::Long> module.
40 use Getopt::Long ('GetOptions');
53 The following options can be passed on the command line:
57 =item B<--infile> I<file>
61 Reads from I<file>. If I<file> ends in C<.rrd>, then C<rrdtool dump> is invoked
62 to create an XML dump of the RRD file. Otherwise the XML dump is expected
63 directly. The special filename C<-> can be used to read from STDIN.
65 =item B<--outfile> I<file>
69 Writes output to I<file>. If I<file> ends in C<.rrd>, then C<rrdtool restore>
70 is invoked to create a binary RRD file. Otherwise an XML output is written. The
71 special filename C<-> can be used to write to STDOUT.
73 =item B<--map> I<in_ds>:I<out_ds>
75 =item B<-m> I<in_ds>:I<out_ds>
77 Writes the datasource I<in_ds> to the output and renames it to I<out_ds>. This
78 is useful to extract one DS from an RRD file.
80 =item B<--step> I<seconds>
82 =item B<-s> I<seconds>
84 Changes the step of the output RRD file to be I<seconds>. The new stepsize must
85 be a multiple of the old stepsize of the other way around. When increasing the
86 stepsize the number of PDPs in each RRA must be dividable by the factor by
87 which the stepsize is increased. The length of CDPs and the absolute length of
88 RRAs (and thus the data itself) is not altered.
92 step = 10, rra_steps = 12 => step = 60, rra_steps = 2
93 step = 300, rra_steps = 1 => step = 10, rra_steps = 30
95 =item B<--rra> B<RRA>:I<CF>:I<XFF>:I<steps>:I<rows>
97 =item B<-a> B<RRA>:I<CF>:I<XFF>:I<steps>:I<rows>
99 Inserts a new RRA in the generated RRD file. This is done B<after> the step has
100 been adjusted, take that into account when specifying I<steps> and I<rows>. For
101 an explanation of the format please see L<rrdcreate(1)>.
107 GetOptions ("infile|i=s" => \$InFile,
108 "outfile|o=s" => \$OutFile,
111 my ($in_ds, $out_ds) = split (':', $_[1]);
112 if (!defined ($in_ds) || !defined ($out_ds))
114 print STDERR "Argument for `map' incorrect! The format is `--map in_ds:out_ds'\n";
117 push (@$InDS, $in_ds);
118 push (@$OutDS, $out_ds);
120 'step|s=i' => \$Step,
123 my ($rra, $cf, $xff, $steps, $rows) = split (':', $_[1]);
124 if (($rra ne 'RRA') || !defined ($rows))
126 print STDERR "Please use the standard RRDTool syntax when adding RRAs. I. e. RRA:<cf><xff>:<steps>:<rows>.\n";
129 push (@$NewRRAs, {cf => $cf, xff => $xff, steps => $steps, rows => $rows});
133 if (!$InFile || !$OutFile)
135 print STDERR "Usage: $0 -i <infile> -m <in_ds>:<out_ds> -s <step>\n";
138 if ((1 + @$InDS) != (1 + @$OutDS))
140 print STDERR "You need the same amount of in- and out-DSes\n";
144 main ($InFile, $OutFile);
150 # state 0 == searching for DS index
151 # state 1 == parse RRA header
152 # state 2 == parse values
155 sub handle_line_dsmap
161 if ((@$InDS == 0) || (@$OutDS == 0))
163 post_line ($line, $index + 1);
167 if (!defined ($state))
173 # $ds_index->[new_index] = old_index
175 for (my $i = 0; $i < @$InDS; $i++)
177 $ds_index->[$i] = -1;
183 if ($line =~ m/<ds>/)
186 $out_cache->[$current_index] = $line;
188 elsif ($line =~ m#<name>\s*([^<\s]+)\s*</name>#)
190 # old_index == $current_index
192 for (my $i = 0; $i < @$InDS; $i++)
194 next if ($ds_index->[$i] >= 0);
196 if ($1 eq $InDS->[$i])
198 $line =~ s#<name>\s*([^<\s]+)\s*</name>#<name> $OutDS->[$i] </name>#;
199 $ds_index->[$i] = $current_index;
204 $out_cache->[$current_index] .= $line;
206 elsif ($line =~ m#</ds>#)
208 $out_cache->[$current_index] .= $line;
210 elsif ($line =~ m#<rra>#)
212 # Print out all the DS definitions we need
213 for (my $new_index = 0; $new_index < @$InDS; $new_index++)
215 my $old_index = $ds_index->[$new_index];
216 while ($out_cache->[$old_index] =~ m/^(.*)$/gm)
218 post_line ("$1\n", $index + 1);
222 # Clear the cache - it's used in state1, too.
223 for (my $i = 0; $i <= $current_index; $i++)
225 $out_cache->[$i] = '';
232 elsif ($current_index == -1)
234 # Print all the lines before the first DS definition
239 # Something belonging to a DS-definition
240 $out_cache->[$current_index] .= $line;
245 if ($line =~ m#<ds>#)
248 $out_cache->[$current_index] .= $line;
250 elsif ($line =~ m#</cdp_prep>#)
252 # Print out all the DS definitions we need
253 for (my $new_index = 0; $new_index < @$InDS; $new_index++)
255 my $old_index = $ds_index->[$new_index];
256 while ($out_cache->[$old_index] =~ m/^(.*)$/gm)
258 post_line ("$1\n", $index + 1);
263 for (my $i = 0; $i <= $current_index; $i++)
265 $out_cache->[$i] = '';
271 elsif ($line =~ m#<database>#)
276 elsif ($current_index == -1)
278 # Print all the lines before the first DS definition
284 # Something belonging to a DS-definition
285 $out_cache->[$current_index] .= $line;
290 if ($line =~ m#</database>#)
303 if ($line =~ m#(<!-- .*? -->)#)
310 while ($line =~ m#<v>\s*([^<\s]+)\s*</v>#g)
316 for (my $new_index = 0; $new_index < @$InDS; $new_index++)
318 my $old_index = $ds_index->[$new_index];
319 $ret .= '<v> ' . $values[$old_index] . ' </v> ';
331 post_line ($ret, $index + 1);
333 }} # handle_line_dsmap
340 my $step_factor_down;
348 post_line ($line, $index + 1);
352 $step_factor_up ||= 0;
353 $step_factor_down ||= 0;
355 if (($step_factor_up == 0) && ($step_factor_down == 0))
357 if ($line =~ m#<step>\s*(\d+)\s*</step>#i)
359 my $old_step = 0 + $1;
360 if ($Step < $old_step)
362 $step_factor_down = int ($old_step / $Step);
363 if (($step_factor_down * $Step) != $old_step)
365 print STDERR "The old step ($old_step seconds) "
366 . "is not a multiple of the new step "
367 . "($Step seconds).\n";
370 $line = "<step> $Step </step>\n";
372 elsif ($Step > $old_step)
374 $step_factor_up = int ($Step / $old_step);
375 if (($step_factor_up * $old_step) != $Step)
377 print STDERR "The new step ($Step seconds) "
378 . "is not a multiple of the old step "
379 . "($old_step seconds).\n";
382 $line = "<step> $Step </step>\n";
390 elsif ($line =~ m#<pdp_per_row>\s*(\d+)\s*</pdp_per_row>#i)
392 my $old_val = 0 + $1;
396 $new_val = int ($old_val / $step_factor_up);
397 if (($new_val * $step_factor_up) != $old_val)
399 print STDERR "Can't divide number of PDPs per row ($old_val) by step-factor ($step_factor_up).\n";
405 $new_val = $step_factor_down * $old_val;
407 $line = "<pdp_per_row> $new_val </pdp_per_row>\n";
410 post_line ($line, $index + 1);
411 }} # handle_line_step
414 # The _add RRA_ handler
419 sub handle_line_add_rra
424 my $post = sub { for (@_) { post_line ($_, $index + 1); } };
428 if (!@$NewRRAs || $add_rra_done)
434 if ($line =~ m#<ds>#i)
438 elsif ($line =~ m#<rra>#i)
440 for (my $i = 0; $i < @$NewRRAs; $i++)
442 my $rra = $NewRRAs->[$i];
445 "\t\t<cf> $rra->{'cf'} </cf>\n",
446 "\t\t<pdp_per_row> $rra->{'steps'} </pdp_per_row>\n",
448 "\t\t\t<xff> $rra->{'xff'} </xff>\n",
452 for (my $j = 0; $j < $num_ds; $j++)
454 $post->("\t\t\t<ds>\n",
455 "\t\t\t\t<primary_value> NaN </primary_value>\n",
456 "\t\t\t\t<secondary_value> NaN </secondary_value>\n",
457 "\t\t\t\t<value> NaN </value>\n",
458 "\t\t\t\t<unknown_datapoints> 0 </unknown_datapoints>\n",
462 $post->("\t\t</cdp_prep>\n", "\t\t<database>\n");
463 $temp = "\t\t\t<row>" . join ('', map { "<v> NaN </v>" } (1 .. $num_ds)) . "</row>\n";
464 for (my $j = 0; $j < $rra->{'rows'}; $j++)
468 $post->("\t\t</database>\n");
473 }} # handle_line_add_rra
476 # The _output_ handler
485 sub handle_line_output
492 post_line ($line, $index + 1);
497 }} # handle_line_output
508 die unless (ref ($handler) eq 'CODE');
509 push (@handlers, $handler);
521 print "DEBUG: post_line ($copy, $index);\n";
524 if ($index > $#handlers)
528 $handlers[$index]->($line, $index);
536 set_output ($out_fh);
540 add_handler (\&handle_line_dsmap);
545 add_handler (\&handle_line_step);
550 add_handler (\&handle_line_add_rra);
553 add_handler (\&handle_line_output);
555 while (my $line = <$in_fh>)
557 post_line ($line, 0);
564 my $out_file = shift;
569 my $in_needs_close = 1;
570 my $out_needs_close = 1;
572 if ($in_file =~ m/\.rrd$/i)
574 open ($in_fh, '-|', 'rrdtool', 'dump', $in_file) or die ("open (rrdtool): $!");
576 elsif ($in_file eq '-')
583 open ($in_fh, '<', $in_file) or die ("open ($in_file): $!");
586 if ($out_file =~ m/\.rrd$/i)
588 open ($out_fh, '|-', 'rrdtool', 'restore', '-', $out_file) or die ("open (rrdtool): $!");
590 elsif ($out_file eq '-')
593 $out_needs_close = 0;
597 open ($out_fh, '>', $out_file) or die ("open ($out_file): $!");
600 handle_fh ($in_fh, $out_fh);
606 if ($out_needs_close)
614 This script is licensed under the GNU general public license, versionE<nbsp>2
619 Florian octo Forster E<lt>octo at verplant.orgE<gt>