1 #! /usr/bin/perl
3 # add_ds.pl, program to add datasources to an existing RRD
4 #
5 # Copyright (C) 2000 Selena M. Brewington
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 use strict;
23 my $ds = shift || die "need number of additional datasources desired";
24 if ($ds eq '-h') {
25 &Usage;
26 exit 0;
27 }
29 my $default_val = shift || 'NaN';
30 my $type = shift || 'COUNTER';
31 my $heartbeat = shift || '1800';
32 my $rrdmin = shift || 'NaN';
33 my $rrdmax = shift || 'NaN';
35 my $cdp_prep_end = '</cdp_prep>';
37 my $row_end = '</row>';
38 my $name = '<name>';
39 my $name_end = '</name>';
41 my $field = '<v> ' . $default_val . ' </v>';
43 my $found_ds = 0;
44 my $num_sources = 0;
45 my $last;
46 my $fields = " ";
47 my $datasource;
48 my $x;
50 while (<STDIN>) {
52 if (($_ =~ s/$row_end$/$fields$row_end/) && $found_ds) {
53 # need to hit <ds> types first, if we don't, we're screwed
54 print $_;
56 } elsif (/$cdp_prep_end/) {
57 print "\t\t\t<ds><value> NaN </value> <unknown_datapoints> 0 </unknown_datapoints></ds>\n" x $ds;
58 print $_;
60 } elsif (/$name_end$/) {
61 ($datasource) = /$name (\w+)/;
62 $found_ds++;
63 print $_;
65 } elsif (/Round Robin Archives/) {
66 # print out additional datasource definitions
68 ($num_sources) = ($datasource =~ /(\d+)/);
70 for ($x = $num_sources+1; $x < $num_sources+$ds+1; $x++) {
72 $fields .= $field;
74 print "\n\t<ds>\n";
75 print "\t\t<name> ds$x <\/name>\n";
76 print "\t\t<type> $type <\/type>\n";
77 print "\t\t<minimal_heartbeat> $heartbeat <\/minimal_heartbeat>\n";
78 print "\t\t<min> $rrdmin <\/min>\n";
79 print "\t\t<max> $rrdmax <\/max>\n\n";
80 print "\t\t<!-- PDP Status-->\n";
81 print "\t\t<last_ds> NaN <\/last_ds>\n";
82 print "\t\t<value> NaN <\/value>\n";
83 print "\t\t<unknown_sec> NaN <\/unknown_sec>\n";
84 print "\t<\/ds>\n\n";
86 }
88 print $_;
89 } else {
90 print $_;
91 }
93 $last = $_;
94 }
99 sub Usage {
101 print "add-ds.pl <add'l ds> [default_val] [type] [heartbeat] [rrdmin] [rrdmax] < file.xml\n";
102 print "\t<add'l ds>\tnumber of additional datasources\n";
103 print "\t[default_val]\tdefault value to be entered in add'l fields\n";
104 print "\t[type]\ttype of datasource (i.e. COUNTER, GAUGE...)\n";
105 print "\t[heatbeat]\tlength of time in seconds before RRD thinks your DS is dead\n";
106 print "\t[rrdmin]\tminimum value allowed for each datasource\n";
107 print "\t[rrdmax]\tmax value allowed for each datasource\n\n";
108 print "\tOptions are read in order, so if you want to change the\n";
109 print "\tdefault heartbeat, you need to specify the default_val and\n";
110 print "\ttype as well, etc.\n";
111 print "\n\tOutput goes to STDOUT.\n";
112 }