forked from ssinyagin/tp-spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectrumDevices.pm
322 lines (259 loc) · 9.49 KB
/
SpectrumDevices.pm
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
# $Id: M_net.pm 871 2010-08-13 20:52:20Z ssinyagin $
# Stanislav Sinyagin <[email protected]>
# CA Spectrum integration
package Torrus::DevDiscover::SpectrumDevices;
use strict;
use JSON;
use IO::File;
use Net::hostent;
use Socket;
use Torrus::Log;
$Torrus::DevDiscover::registry{'SpectrumDevices'} = {
'sequence' => 600,
'checkdevtype' => \&checkdevtype,
'discover' => \&discover,
'buildConfig' => \&buildConfig
};
# CA Spectrum-specific nodeid values are only assinged to IF-MIB interfaces
# of specific ifType. This is related to the fact that some Cisco virtual
# interfaces (such as MPLS layer) have non-unique ifName, and Spectrum uses
# ifName for interface reference.
# spectrum-ddcfg.pl applies default values, and additional
# values can be added in devdiscover-siteconfig.pl
# see IANAifType-MIB.my for values
our %onlyIfTypes;
# Blacklist of devtypes where Spectrum plugin would automatically skip
# its processing. spectrum-ddcfg.pl applies default values, and additional
# values can be added in devdiscover-siteconfig.pl
our %excludeDevtypes;
sub checkdevtype
{
my $dd = shift;
my $devdetails = shift;
if( $devdetails->param('SpectrumDevices::manage') eq 'yes' )
{
foreach my $devtype (keys %excludeDevtypes)
{
if( $devdetails->isDevType($devtype) )
{
Info($devdetails->param('snmp-host') .
': Device is of type ' . $devtype . ', which is listed ' .
'in Spectrum plugin blacklist. Skipping Spectrum ' .
'processing');
return 0;
}
}
my $data = $devdetails->data();
if( $devdetails->hasCap('nodeidReferenceManaged') )
{
Error($devdetails->param('snmp-host') .
': SpectrumDevices conflicts with ' .
$data->{'nodeidManagedBy'} . ' in nodeid management. ' .
'Modify the discovery instructions to enable only one of ' .
'the modules to manage nodeid.');
return 0;
}
$devdetails->setCap('nodeidReferenceManaged');
$data->{'nodeidManagedBy'} = 'SpectrumDevices';
return 1;
}
return 0;
}
# We only read every export file once
my %export_cache;
my %cache_by_ip;
sub discover
{
my $dd = shift;
my $devdetails = shift;
my $session = $dd->session();
my $data = $devdetails->data();
my $export_file = $devdetails->param('SpectrumDevices::export-file');
if( not defined($export_file) )
{
Error('Mandatory parameter SpectrumDevices::export-file is not ' .
' defined for ' . $devdetails->param('snmp-host'));
return 0;
}
if( not defined($export_cache{$export_file}) )
{
my $fh = new IO::File( $export_file, 'r' );
if( not defined($fh) )
{
Error('Torrus::DevDiscover::SpectrumDevices cannot read ' .
$export_file . ': ' . $!);
return 0;
}
my $json = new JSON;
local $/;
my $json_data = <$fh>;
$fh->close();
my $result = eval { $json->decode( $json_data ) };
if( not defined( $result ) )
{
Error('Error reading JSON contrent from ' . $export_file . ': ' .
$@);
return 0;
}
$export_cache{$export_file} = $result;
foreach my $mh ( keys %{$result} )
{
foreach my $attr ( 'IfModelNameOption',
'IfModelNameOptionSecondary',
'MName',
'Network_Address' )
{
if( not defined( $result->{$mh}->{$attr} ) )
{
Error('Mandatory attribute ' . $attr . ' is not defined ' .
'in ' . $export_file . ' for ' . $mh);
return 0;
}
}
my $ipaddr = $result->{$mh}->{'Network_Address'};
$cache_by_ip{$export_file}{$ipaddr} = $mh;
}
}
my $spectrum_data = $export_cache{$export_file};
# We've got the Spectrum database export. Now find our device in Spectrum
my $mh;
# first check if any of these parameters matches MH
foreach my $param ( 'SpectrumDevices::device-mh',
'nodeid-device',
'system-id' )
{
my $val = $devdetails->param( $param );
if( defined( $val ) and defined $spectrum_data->{$val} )
{
$mh = $val;
Debug('Found the exact match of MH in ' . $param . ' for ' .
$devdetails->param('snmp-host') . ' in Spectrum data: ' .
'mh=' . $mh);
last;
}
}
if( not defined($mh) )
{
Debug('Trying to match the device IP address against Spectrum data');
# now try to match the IP address
# if SNMP host starts with a nondigit, consider it a DNS name
my $hostname = $devdetails->param('snmp-host');
if( $hostname =~ /^\d/o )
{
$mh = $cache_by_ip{$export_file}{$hostname};
}
else
{
my $domain = $devdetails->param('domain-name');
if( $domain and index($hostname, '.') < 0 and
index($hostname, ':') < 0 )
{
$hostname .= '.' . $domain;
}
my $h = gethost($hostname);
if( not defined( $h ) )
{
Error('Cannot resolve DNS name: ' . $hostname);
return 0;
}
foreach my $addr ( @{$h->addr_list()} )
{
my $ipaddr = inet_ntoa($addr);
Debug('Resolved hostname ' . $hostname . ' into ' . $ipaddr);
$mh = $cache_by_ip{$export_file}{$ipaddr};
if( defined( $mh ) )
{
Debug('IP address ' . $ipaddr . ' matched mh=' . $mh);
last;
}
}
}
}
if( not defined($mh) )
{
Error('Cannot find ' . $devdetails->param('snmp-host') .
' in Spectrum export file ' . $export_file . '. Falling back' .
' to the original nodeid scheme');
return 1;
}
# Now we matched the current device with the Spectrum MH
Debug('Found Spectrum MH: ' . $mh);
# Copy the old nodeid values into a new reference map
my $orig_nameref_ifNodeidPrefix =
$data->{'nameref'}{'ifNodeidPrefix'};
my $orig_nameref_ifNodeid =
$data->{'nameref'}{'ifNodeid'};
$data->{'nameref'}{'ifNodeidPrefix'} = 'Spectrum_ifNodeidPrefix';
$data->{'nameref'}{'ifNodeid'} = 'Spectrum_ifNodeid';
foreach my $ifIndex ( keys %{$data->{'interfaces'}} )
{
my $interface = $data->{'interfaces'}{$ifIndex};
next if $interface->{'excluded'};
$interface->{$data->{'nameref'}{'ifNodeidPrefix'}} =
$interface->{$orig_nameref_ifNodeidPrefix};
$interface->{$data->{'nameref'}{'ifNodeid'}} =
$interface->{$orig_nameref_ifNodeid};
}
my $ifnameref_primary = $spectrum_data->{$mh}{'IfModelNameOption'};
my $ifnameref_secondary =
$spectrum_data->{$mh}{'IfModelNameOptionSecondary'};
my $mname = $spectrum_data->{$mh}{'MName'};
$data->{'param'}{'nodeid-device'} = $mname;
$data->{'param'}{'spectrum-device-mh'} = $mh;
foreach my $ifIndex ( keys %{$data->{'interfaces'}} )
{
my $interface = $data->{'interfaces'}{$ifIndex};
next if $interface->{'excluded'};
next unless $onlyIfTypes{$interface->{'ifType'}};
$interface->{$data->{'nameref'}{'ifNodeidPrefix'}} =
'spec-if//' . $mname . '_';
my $ifname = $interface->{$ifnameref_primary};
if( not defined( $ifname ) )
{
Error('Cannot find ' . $ifnameref_primary .
' for ifIndex=' . $ifIndex);
next;
}
if( length( $ifnameref_secondary ) )
{
my $ifname_sec = $interface->{$ifnameref_secondary};
if( not defined( $ifname_sec ) )
{
Error('Cannot find ' . $ifnameref_secondary .
' for ifIndex=' . $ifIndex);
return 0;
}
$ifname .= '_' . $ifname_sec;
}
$interface->{$data->{'nameref'}{'ifNodeid'}} = $ifname;
}
return 1;
}
sub buildConfig
{
my $devdetails = shift;
my $cb = shift;
my $devNode = shift;
my $data = $devdetails->data();
}
1;
# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# perl-indent-level: 4
# End: