2 # Copyright (C) 2002 Stanislav Sinyagin
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.
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.
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.
18 # $Id: genddx.in,v 1.1 2010-12-27 00:04:00 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 # Generate the SNMP discovery instructions XML file out of plaintext
24 BEGIN { require '@devdiscover_config_pl@'; }
32 our $outFormatVersion = '1.0';
39 'output-file' => 'routers.xml',
41 'host-subtree' => '/Routers',
43 'snmp-community' => 'public',
44 'snmp-version' => '2c',
48 'data-dir' => '@defrrddir@',
51 our $outfile = 'routers.ddx';
54 my $creator = "Torrus version @VERSION@\n" .
55 "This file was generated by command:\n" .
57 foreach my $arg ( @ARGV )
61 $creator .= ' ' . $arg . ' ';
65 $creator .= "\'" . $arg . "\'\\\n";
68 $creator .= "\nOn " . scalar(localtime(time));
72 'hostfile=s' => \$hostfile,
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'},
87 ( not $hostfile and scalar(@hosts) == 0 ) or
90 print STDERR "Generate devdiscover XML configuration\n";
92 print STDERR "Usage: $0 options...\n",
94 " --host=hostname router hostname\n",
95 " --hostfile=filename space-separated router hostnames file\n",
96 " --out=filename output file [".$outfile."]\n",
98 " --discout=filename discovery output file\n",
99 " [", $globalParams{'output-file'}, "]\n",
101 " --domain=domain optional DNS domain name\n",
103 " --version=v SNMP version [",
104 $globalParams{'snmp-version'}, "]\n",
106 " --community=string SNMP read community [",
107 $globalParams{'snmp-community'}, "]\n",
109 " --port=number SNMP port [",
110 $globalParams{'snmp-port'}, "]\n",
112 " --retries=number SNMP retries [",
113 $globalParams{'snmp-retries'}, "]\n",
115 " --timeout=number SNMP timeout [",
116 $globalParams{'snmp-timeout'}, "]\n",
118 " --subtree=string Subtree name [",
119 $globalParams{'host-subtree'}, "]\n",
121 " --datadir=path data-dir parameter [",
122 $globalParams{'data-dir'}, "]\n",
124 " --holtwinters Enable Holt-Winters analysis\n",
126 "Host names may be of form \"host:devname\" where devname is a symbolic\n",
128 "Output file is placed into " . $Torrus::Global::discoveryDir,
129 "\n if no path is given.\n";
133 # Place the output file in discovery directory if the path is not given
134 if( $outfile !~ /\// )
136 $outfile = $Torrus::Global::discoveryDir . '/' . $outfile;
139 # Convert flags from true/false to yes/no
140 foreach my $param ( 'rrd-hwpredict' )
142 if( $globalParams{$param} )
144 $globalParams{$param} = 'yes';
148 $globalParams{$param} = 'no';
152 if( $globalParams{'host-subtree'} !~ /^\/[0-9A-Za-z_\-\.\/]*$/ or
153 $globalParams{'host-subtree'} =~ /\.\./ )
155 Error("Invalid format for subtree name: " . $globalParams{'host-subtree'});
159 if( defined $hostfile )
161 if( not open(HOSTS, $hostfile) )
163 print STDERR "Cannot open $hostfile: $!";
170 push( @hosts, split( /\s+/ ) );
176 my $doc = XML::LibXML->createDocument( "1.0", "UTF-8" );
177 my $root = $doc->createElement('snmp-discovery');
178 $doc->setDocumentElement( $root );
181 my $fileInfoNode = $doc->createElement('file-info');
182 $root->appendChild( $fileInfoNode );
184 my $formatNode = $doc->createElement('format-version');
185 $formatNode->appendText( $outFormatVersion );
186 $fileInfoNode->appendChild( $formatNode );
190 my $creatorNode = $doc->createElement('creator-info');
191 $creatorNode->appendText( $creator );
192 $root->appendChild( $creatorNode );
195 createParamsDom( \%globalParams, $doc, $root );
198 foreach my $host ( @hosts )
201 if( $host =~ /([^:]+):(.+)/ )
207 my $hostNode = $doc->createElement('host');
208 $root->appendChild( $hostNode );
210 my %hostParams = ( 'snmp-host' => $host );
211 if( $devname ne $host )
213 $hostParams{'symbolic-name'} = $devname;
216 createParamsDom( \%hostParams, $doc, $hostNode );
219 my $ok = $doc->toFile( $outfile, 2 );
222 print STDERR ("Wrote $outfile\n");
226 print STDERR ("Cannot write $outfile: $!\n");
237 my $parentNode = shift;
239 foreach my $param ( sort keys %{$params} )
241 my $paramNode = $doc->createElement('param');
242 $paramNode->setAttribute( 'name', $param );
243 $paramNode->setAttribute( 'value', $params->{$param} );
244 $parentNode->appendChild( $paramNode );
253 # indent-tabs-mode: nil
254 # perl-indent-level: 4