forked from ssinyagin/tp-spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrumdevs.in
245 lines (186 loc) · 5.71 KB
/
spectrumdevs.in
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
#!@PERL@
# Copyright (C) 2010 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$
# Stanislav Sinyagin <[email protected]>
BEGIN { require '@torrus_config_pl@'; }
use strict;
use Getopt::Long;
use Spectrum::CLI;
use JSON;
use IO::File;
use Torrus::Log;
$ENV{'SPECROOT'} = '@specroot@';
my $outfile = '-';
my $server;
my $debug = 0;
my $verbose = 0;
my $ok = GetOptions( 'out=s' => \$outfile,
'server=s' => \$server,
'verbose' => \$verbose,
'debug' => \$debug );
if( not $ok or scalar( @ARGV ) > 0 )
{
print STDERR "Usage: $0 --out=FILE [options...]\n",
"Options:\n",
" --out=FILE Spectrum data used by DevDiscover\n",
" --server=NAME Spectrum server name if different from default\n",
" --verbose print extra information\n",
" --debug print debugging information\n";
exit 1;
}
if( $debug )
{
Torrus::Log::setLevel('debug');
}
elsif( $verbose )
{
Torrus::Log::setLevel('verbose');
}
Verbose('Connecting to Spectrum server');
my @specargs;
if( defined($server) )
{
push( @specargs, $server );
}
push( @specargs, { verbose => 1, Verbose => 1 } );
my $spec = new Spectrum::CLI( @specargs );
if( not defined($spec) )
{
Error('Failed connecting to Spectrum server: ' . $!);
exit(1);
}
# Spectrum attribute names<->numbers mapping
my %attr_name_id =
('Network_Address' => '0x12d7f',
'IfModelNameOption' => '0x12a1e',
'IfModelNameOptionSecondary' => '0x12d7b',
'ifIndex' => '0x11348',
'ifDescr' => '0x1134b',
'ifType' => '0x1134c',
'ifName' => '0x11f6f',
'ifAlias' => '0x11f84',
);
my %attr_id_name;
while( my ($key, $val) = each %attr_name_id )
{
$attr_id_name{$val} = $key;
}
my %model_type_blacklist =
('0x10290' => 1, # Pingable
'0x100ae' => 1, # Fanout
'0x1048f' => 1, # Provider_Cloud
);
# get the list of devices
my $devices = $spec->show_devices();
# returned hash of mh => attributes
my $ret = {};
# verify uniqueness of hostnames and IP addresses
my $known_names = {};
my $known_ipaddr = {};
foreach my $device ( @{$devices} )
{
next if $model_type_blacklist{$device->{'MTypeHnd'}};
my $devmh = $device->{'MHandle'};
### Retrieve device properties
retrieve_properties( $spec, $devmh, $device,
['Network_Address', 'IfModelNameOption',
'IfModelNameOptionSecondary'] );
next unless defined( $device->{'IfModelNameOption'} );
if( defined($known_names->{$device->{'MName'}}) )
{
Error('Duplicate model name: ' . $device->{'MName'} . ' in ' .
$devmh . ' and ' . $known_names->{$device->{'MName'}});
next;
}
else
{
$known_names->{$device->{'MName'}} = $devmh;
}
if( defined($known_ipaddr->{$device->{'Network_Address'}}) )
{
Error('Duplicate IP address: ' . $device->{'Network_Address'} .
' in ' . $devmh . ' and ' .
$known_ipaddr->{$device->{'Network_Address'}});
next;
}
else
{
$known_ipaddr->{$device->{'Network_Address'}} = $devmh;
}
$ret->{$devmh} = $device;
# 'IfModelNameOption' and 'IfModelNameOptionSecondary'
# refer to attribute IDs which are used for composing the interface name in
# Spectrum. By default, IfModelNameOption is set to 0x11f6f (ifName),
# and IfModelNameOptionSecondary is usually zero
foreach my $attr ('IfModelNameOption', 'IfModelNameOptionSecondary')
{
my $val = $device->{$attr};
if( defined( $attr_id_name{$val} ) )
{
$val = $attr_id_name{$val};
}
elsif( $val eq '0x0' )
{
$val = '';
}
else
{
Error('Unknown attribute ID in ' . $attr . ' for device mh=' .
$devmh . ' (' . $device->{'MName'} . ')');
$ok = 0;
}
$device->{$attr} = $val;
}
}
$spec->disconnect();
my $json = new JSON;
$json->canonical;
$json->pretty;
my $fh = new IO::File( $outfile, 'w' );
if( not defined($fh) )
{
Error('Error opening ' . $outfile . ' for writing: ' . $!);
exit(1);
}
print $fh $json->encode( $ret );
exit($ok?0:1);
sub retrieve_properties
{
my $spec = shift;
my $mh = shift;
my $object = shift;
my $propnames = shift;
my @cmdargs;
foreach my $propname ( @{$propnames} )
{
push(@cmdargs, 'attr=' . $attr_name_id{$propname});
}
push(@cmdargs, 'mh=' . $mh);
my $properties = $spec->show_attributes(@cmdargs);
foreach my $prop ( @{$properties} )
{
if( defined( $attr_id_name{$prop->{'Id'}} ) )
{
$object->{$attr_id_name{$prop->{'Id'}}} = $prop->{'Value'};
}
}
}
# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# perl-indent-level: 4
# End: