Code

initial release
[rrdtool-all.git] / contrib / add_ds / batch.pl
1 #! /usr/sepp/bin/perl
3 # batch.pl, simple program to help add datasources to an existing RRD
4 #   goes with add_ds.pl
5 #
6 #    Copyright (C) 2000 Selena M. Brewington
7 #
8 #    This program is free software; you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation; either version 2 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program; if not, write to the Free Software
20 #    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 # for use with add_ds.pl script to add datasources to an RRD
23 #
24 # usage: ls -1 | ./batch.pl [-o] <# ds to add>
25 #
26 # -o will let you overwrite your rrds.  i don't recommend using this 
27 # switch the first time you run this program.  If something
28 # gets messed up, the original xml files will be in the xml
29 # directory and you can run 'rrdtool restore' on all of them
30 # uncomment the commented out lines below to make this work.
31 #
32 # also, you can change the name of the directory the XML is
33 # getting dumped to, where your rrdtool binary is located, 
34 # and where add_ds.pl is located.  the variables are listed 
35 # below.
36 #
37 # This script could theoretically take fully-qualified pathnames
38 # as input from STDIN rather than the output from ls -1. 
39 #
41 use strict;
43 ########### USER CONFIGURABLE SECTION #######################
45 my $newdir = "xml";
46 my $rrdtool = "/usr/local/rrdtool-1.0.30/bin/rrdtool";
47 my $add_ds = "./add_ds.pl";  # path to add_ds.pl script
49 ########### END CONFIGURE SECTION ###########################
52 my $ds = shift || die 'need number of datasources to add';
54 my $overwrite = 0;
56 if ($ds eq '-o') {
57   $overwrite = 1;
58   $ds = shift || die 'need number of datasources to add';
59
61 if (! (-x $newdir)) {
62   `mkdir xml` || die "can't make directory xml";
63 }
65 while (<STDIN>) {
67   next if (! /rrd/);
68   chop;
69   my $file = $_;
71   $_ =~ s/rrd$/xml/;
73   open(FILE, ">$newdir/$_");
75   my @output = `$rrdtool dump $file`;
76   print FILE @output;
78   close(FILE);
80   open (FILE, ">$newdir/$_.2");
82   my @new = `cat $newdir/$_ | $add_ds $ds`; 
84   print FILE @new;
86   close (FILE);
88   system("$rrdtool restore $newdir/$_.2 $newdir/$file") == 0 
89     or die "rrdtool restore failed for $file";
91   if ($overwrite == 1) {
92     system("mv $newdir/$file $file") == 0
93       or die "can't overwrite $file";
94   }
95 }