This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / torrus / bin / genddx.in
1 #!@PERL@
2 #  Copyright (C) 2002  Stanislav Sinyagin
3 #
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
18 # $Id: genddx.in,v 1.1 2010-12-27 00:04:00 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 # Generate the SNMP discovery instructions XML file out of plaintext
22 # list of hosts.
23
24 BEGIN { require '@devdiscover_config_pl@'; }
25
26 use strict;
27 use Getopt::Long;
28 use XML::LibXML;
29
30 use Torrus::Log;
31
32 our $outFormatVersion = '1.0';
33
34 our @hosts = ();
35 our $hostfile;
36
37 our %globalParams =
38     (
39      'output-file'         => 'routers.xml',
40      'domain-name'         => '',
41      'host-subtree'        => '/Routers',
42      'snmp-port'           => '161',
43      'snmp-community'      => 'public',
44      'snmp-version'        => '2c',
45      'snmp-timeout'        => 10,
46      'snmp-retries'        => 2,
47      'rrd-hwpredict'       => 0,
48      'data-dir'            => '@defrrddir@',
49      );
50
51 our $outfile = 'routers.ddx';
52
53
54 my $creator = "Torrus version @VERSION@\n" .
55     "This file was generated by command:\n" .
56     $0 . " \\\n";
57 foreach my $arg ( @ARGV )
58 {
59     if( $arg =~ /^--/ )
60     {
61         $creator .= ' ' . $arg . ' ';
62     }
63     else
64     {
65         $creator .= "\'" . $arg . "\'\\\n";
66     }
67 }
68 $creator .= "\nOn " . scalar(localtime(time));
69
70 my $ok = GetOptions(
71                     'host=s'      => \@hosts,
72                     'hostfile=s'  => \$hostfile,
73                     'out=s'       => \$outfile,
74                     'discout=s'   => \$globalParams{'output-file'},
75                     'domain=s'    => \$globalParams{'domain-name'},
76                     'version=s'   => \$globalParams{'snmp-version'},
77                     'community=s' => \$globalParams{'snmp-community'},
78                     'port=i'      => \$globalParams{'snmp-port'},
79                     'timeout=i'   => \$globalParams{'snmp-timeout'},
80                     'retries=i'   => \$globalParams{'snmp-retries'},
81                     'subtree=s'   => \$globalParams{'host-subtree'},
82                     'holtwinters' => \$globalParams{'rrd-hwpredict'},
83                     'datadir=s'   => \$globalParams{'data-dir'},
84                     );
85
86 if( not $ok or
87     ( not $hostfile and scalar(@hosts) == 0 ) or
88     scalar( @ARGV ) > 0 )
89 {
90     print STDERR "Generate devdiscover XML configuration\n";
91
92     print STDERR "Usage: $0 options...\n",
93     "Options:\n",
94     " --host=hostname         router hostname\n",
95     " --hostfile=filename     space-separated router hostnames file\n",
96     " --out=filename          output file             [".$outfile."]\n",
97
98     " --discout=filename      discovery output file\n",
99     "      [", $globalParams{'output-file'}, "]\n",
100
101     " --domain=domain         optional DNS domain name\n",
102
103     " --version=v             SNMP version         [",
104     $globalParams{'snmp-version'}, "]\n",
105
106     " --community=string      SNMP read community  [",
107     $globalParams{'snmp-community'}, "]\n",
108
109     " --port=number           SNMP port            [",
110     $globalParams{'snmp-port'}, "]\n",
111
112     " --retries=number        SNMP retries         [",
113     $globalParams{'snmp-retries'}, "]\n",
114
115     " --timeout=number        SNMP timeout         [",
116     $globalParams{'snmp-timeout'}, "]\n",
117
118     " --subtree=string        Subtree name         [",
119     $globalParams{'host-subtree'}, "]\n",
120
121     " --datadir=path          data-dir parameter   [",
122     $globalParams{'data-dir'}, "]\n",
123
124     " --holtwinters           Enable Holt-Winters analysis\n",
125     "\n",
126     "Host names may be of form \"host:devname\" where devname is a symbolic\n",
127     "device name.\n",
128     "Output file is placed into " . $Torrus::Global::discoveryDir,
129     "\n if no path is given.\n";
130     exit 1;
131 }
132
133 # Place the output file in discovery directory if the path is not given
134 if( $outfile !~ /\// )
135 {
136     $outfile = $Torrus::Global::discoveryDir . '/' . $outfile;
137 }
138
139 # Convert flags from true/false to yes/no
140 foreach my $param ( 'rrd-hwpredict' )
141 {
142     if( $globalParams{$param} )
143     {
144         $globalParams{$param} = 'yes';
145     }
146     else
147     {
148         $globalParams{$param} = 'no';
149     }
150 }
151
152 if( $globalParams{'host-subtree'} !~ /^\/[0-9A-Za-z_\-\.\/]*$/ or
153     $globalParams{'host-subtree'} =~ /\.\./ )
154 {
155     Error("Invalid format for subtree name: " . $globalParams{'host-subtree'});
156     exit 1;
157 }
158
159 if( defined $hostfile )
160 {
161     if( not open(HOSTS, $hostfile) )
162     {
163         print STDERR "Cannot open $hostfile: $!";
164         exit 1;
165     }
166     while(<HOSTS>)
167     {
168         s/^\s+//;
169         s/\s+$//;
170         push( @hosts, split( /\s+/ ) );
171     }
172 }
173
174 # Create XML DOM
175
176 my $doc = XML::LibXML->createDocument( "1.0", "UTF-8" );
177 my $root = $doc->createElement('snmp-discovery');
178 $doc->setDocumentElement( $root );
179
180 {
181     my $fileInfoNode = $doc->createElement('file-info');
182     $root->appendChild( $fileInfoNode );
183
184     my $formatNode = $doc->createElement('format-version');
185     $formatNode->appendText( $outFormatVersion );
186     $fileInfoNode->appendChild( $formatNode );
187 }
188
189 {
190     my $creatorNode = $doc->createElement('creator-info');
191     $creatorNode->appendText( $creator );
192     $root->appendChild( $creatorNode );
193 }
194
195 createParamsDom( \%globalParams, $doc, $root );
196
197
198 foreach my $host ( @hosts )
199 {
200     my $devname = $host;
201     if( $host =~ /([^:]+):(.+)/ )
202     {
203         $host = $1;
204         $devname = $2;
205     }
206
207     my $hostNode = $doc->createElement('host');
208     $root->appendChild( $hostNode );
209
210     my %hostParams = ( 'snmp-host'      => $host );
211     if( $devname ne $host )
212     {
213         $hostParams{'symbolic-name'} = $devname;
214     }
215
216     createParamsDom( \%hostParams, $doc, $hostNode );
217 }
218
219 my $ok = $doc->toFile( $outfile, 2 );
220 if( $ok )
221 {
222     print STDERR ("Wrote $outfile\n");
223 }
224 else
225 {
226     print STDERR ("Cannot write $outfile: $!\n");
227 }
228
229
230 exit($ok ? 0:1);
231
232
233 sub createParamsDom
234 {
235     my $params = shift;
236     my $doc = shift;
237     my $parentNode = shift;
238
239     foreach my $param ( sort keys %{$params} )
240     {
241         my $paramNode = $doc->createElement('param');
242         $paramNode->setAttribute( 'name', $param );
243         $paramNode->setAttribute( 'value', $params->{$param} );
244         $parentNode->appendChild( $paramNode );
245     }
246 }
247
248
249
250
251 # Local Variables:
252 # mode: perl
253 # indent-tabs-mode: nil
254 # perl-indent-level: 4
255 # End: