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