File Coverage

blib/lib/SNMP/Insight/Classifier.pm
Criterion Covered Total %
statement 80 82 97.5
branch 52 104 50.0
condition 13 36 36.1
subroutine 6 6 100.0
pod 3 3 100.0
total 154 231 66.6


line stmt bran cond sub pod time code
1             package SNMP::Insight::Classifier;
2              
3             #ABSTRACT: Guess SNMP Agent from description
4              
5             # Portions based on SNMP::Info.
6             # Copyright (c) 2003-2012 Max Baker and SNMP::Info Developers
7             # All rights reserved.
8              
9 2     2   18775 use Moose;
  2         4  
  2         15  
10 2     2   10871 use namespace::autoclean;
  2         4  
  2         22  
11              
12             our $VERSION = '0.002'; #TRIAL VERSION:
13              
14 2     2   532 use SNMP::Insight::Utils qw( _debug );
  2         3  
  2         2921  
15              
16             has device => (
17             is => 'ro',
18             isa => 'SNMP::Insight::Device',
19             );
20              
21             has desc => (
22             is => 'ro',
23             isa => 'Str',
24             lazy => 1,
25             default => sub {
26             my $desc = $_[0]->device->sysDescr or return '';
27             $desc =~ s/[\r\n\l]+/ /g;
28             return $desc;
29             }
30             );
31              
32             sub classify {
33 3     3 1 1347 my $self = shift;
34              
35 3         82 my $device = $self->device;
36              
37 3         9 my $id = $device->sysObjectID;
38 3         87 my $vendor = $device->vendor;
39 3         11 my $services = $device->sysServices;
40 3         101 my $desc = $self->desc;
41              
42 3   50     36 _debug(
43             "SNMP::Insight::classifier services: ", $services || 'UNDEF',
44             "id:$id sysDescr:\"$desc\" vendor: $vendor\n"
45             );
46              
47             # Some devices don't implement sysServices, but do return a description.
48             # In that case, log a warning and continue.
49 3 50 33     13 if ( !defined($services) && !defined($desc) ) {
50 0         0 _debug("No sysServices nor sysDescr, giving up");
51 0         0 return;
52             }
53              
54 3         4 my $device_type;
55              
56 3         10 $device_type = $self->guess_by_desc($desc);
57 3   50     22 _debug(
58             "SNMP::Insight::classifier by description %s\n",
59             $device_type || 'undef'
60             );
61              
62 3   33     14 $device_type ||= $self->guess_by_vendor();
63 3   50     13 _debug(
64             "SNMP::Insight::classifier by vendor %s\n",
65             $device_type || 'undef'
66             );
67              
68 3         24 return $device_type;
69             }
70              
71             sub guess_by_vendor {
72 3     3 1 4 my $self = shift;
73              
74 3         91 my $device = $self->device;
75 3         10 my $id = $device->sysObjectID;
76 3         138 my $vendor = $device->vendor;
77              
78             # Cisco Small Business (300 500) series override
79             # This is for enterprises(1).cisco(9).otherEnterprises(6).ciscosb(1)
80 3 50       12 return 'CiscoSB'
81             if ( $id =~ /^\.1\.3\.6\.1\.4\.1\.9\.6\.1/ );
82              
83 3 50       16 return 'NetSNMP' if $vendor eq 'NetSNMP';
84             }
85              
86             sub guess_by_desc {
87 3     3 1 4 my $self = shift;
88              
89 3         76 my $desc = $self->desc;
90              
91 3 50       13 return unless $desc =~ /\S/o;
92              
93             #------------------------------------------------------------------#
94             # Cisco Devices #
95             #------------------------------------------------------------------#
96              
97             # Catalyst 1900
98 3 50 33     22 return 'Cisco::C1900'
99             if ( $desc =~ /catalyst/i and $desc =~ /\D19\d{2}/ );
100              
101             # Catalyst 2900 and 3500XL (IOS)
102 3 50       32 return 'Cisco::C2900'
103             if ( $desc =~ /(C2900XL|C2950|C3500XL|C2940|CGESM|CIGESM)/i );
104              
105             # Cisco Catalyst 3550 3560
106             # Catalyst 3550 / 3548 Layer2 only switches
107             # Cisco 3400 w/ MetroBase Image
108 3 50       13 return 'Cisco::C3550' if $desc =~ /(C3550|C3560|ME340x)/;
109              
110             # Cisco 4000-4500
111 3 50       9 return 'Cisco::C4000' if $desc =~ /Catalyst 4[05]00/;
112              
113             # Cat6k with older SUPs (hybrid CatOS/IOS?)
114 3 50       12 return 'Cisco::C6500' if $desc =~ /(c6sup2|c6sup1)/;
115              
116             # Cat6k with Sup720, Sup720 or Sup2T (and Sup2 running native IOS?)
117 3 50       12 return 'Cisco::C6500'
118             if $desc =~ /(s72033_rp|s3223_rp|s32p3_rp|s222_rp|s2t54)/;
119              
120             # IOS 15.x on Catalyst 3850
121 3 50 33     12 return 'Cisco::C6500'
122             if ( $desc =~ /cisco/i and $desc =~ /CAT3K/ );
123              
124             # Cisco 2970
125 3 50       9 return 'Cisco::C6500' if ( $desc =~ /(C2970|C2960)/ );
126              
127             # Various Cisco blade switches, CBS30x0 and CBS31x0 models
128 3 50 33     11 return 'Cisco::C6500'
129             if ( $desc =~ /cisco/i and $desc =~ /CBS3[0-9A-Za-z]{3}/ );
130              
131             # Cisco blade switches, CBS30x0 and CBS31x0 models with L2 only
132 3 50 33     10 return 'Cisco::C6500'
133             if ( $desc =~ /cisco/i and $desc =~ /CBS3[0-9A-Za-z]{3}/ );
134              
135             # Cisco 2970
136 3 50       9 return 'Cisco::C6500'
137             if ( $desc =~ /(C2970|C2960)/ );
138              
139             # Cisco 3750
140 3 50 33     10 return 'Cisco::C6500'
141             if ( $desc =~ /cisco/i and $desc =~ /3750/ );
142              
143             # Cisco Nexus running NX-OS
144 3 50       110 return 'Cisco::Nexus'
145             if ( $desc =~ /^Cisco\s+NX-OS/ );
146              
147             # Catalyst WS-C series override 2926,4k,5k,6k in Hybrid
148 3 50       15 return 'Cisco::Catalyst' if ( $desc =~ /WS-C\d{4}/ );
149              
150             # Aironet - IOS
151 3 50 33     24 return 'Cisco::AironetIOS'
152             if ( $desc
153             =~ /\b(C1100|C1130|C1140|AP1200|C350|C1200|C1240|C1250|C2700|C3700)\b/
154             && $desc =~ /\bIOS\b/ );
155              
156             # Aironet - non IOS
157 3 50 33     13 return 'Cisco::Aironet'
158             if ( $desc =~ /Cisco/
159             && $desc =~ /\D(BR500|CAP340|AP340|CAP350|350|1200)\D/ );
160              
161 3 50 33     9 return 'Cisco::Aironet'
162             if ( $desc =~ /Aironet/ && $desc =~ /\D(AP4800)\D/ );
163              
164             # Airespace (WLC) Module
165 3 50       10 return 'Cisco::Airespace'
166             if ( $desc =~ /^Cisco Controller$/ );
167              
168             # Cisco ASA, newer versions which report layer 3 functionality
169             # version >= 8.2 are known to do this
170 3 50       9 return 'Cisco::ASA'
171             if ( $desc =~ /Cisco Adaptive Security Appliance/i );
172              
173             # Cisco PIX
174 3 50       13 return 'Cisco::Generic'
175             if ( $desc =~ /Cisco PIX Security Appliance/i );
176              
177             # Cisco FWSM
178 3 50       9 return 'Cisco::FWSM'
179             if ( $desc =~ /Cisco Firewall Services Module/i );
180              
181             #------------------------------------------------------------------#
182             # HP Devices #
183             #------------------------------------------------------------------#
184              
185             # HP, older ProCurve models (1600, 2400, 2424m, 4000, 8000)
186 3 50       9 return 'HP::HP4000'
187             if $desc =~ /\b(J4093A|J4110A|J4120A|J4121A|J4122A|J4122B)\b/;
188              
189             # HP, Foundry OEM
190 3 50       7 return 'HP::HP9300'
191             if $desc =~ /\b(J4874A|J4138A|J4139A|J4840A|J4841A)\b/;
192              
193             # HP Virtual Connect blade switches
194 3 50       19 return 'HP::HPVC'
195             if $desc =~ /HP\sVC\s/;
196              
197             #------------------------------------------------------------------#
198             # Nortel Devices #
199             #------------------------------------------------------------------#
200              
201             # Nortel 2270
202 3 50       13 return 'Nortel::N2270'
203             if ( $desc =~ /Nortel\s+(Networks\s+)??WLAN\s+-\s+Security\s+Switch/ );
204              
205             # Nortel (Trapeze) WSS 2300 Series
206 3 50       8 return 'Nortel::WSS2300'
207             if ( $desc =~ /^(Nortel\s)??Wireless\sSecurity\sSwitch\s23[568][012]\b/ );
208              
209             # Nortel ERS (Passport) 1600 Series < version 2.1
210 3 50       9 return 'Nortel::ERS1600'
211             if $desc =~ /(Passport|Ethernet\s+Routing\s+Switch)-16/i;
212              
213             # Nortel Contivity
214 3 50       45 return 'NortelContivity' if $desc =~ /(\bCES\b|\bNVR\sV\d)/;
215              
216             # Nortel Business Ethernet Switch
217 3 50       8 return 'Nortel::Baystack'
218             if ( $desc =~ /^Business Ethernet Switch\s[12]\d\d/i );
219              
220             # ERS - BayStack Numbered
221 3 50       10 return 'Nortel::Baystack'
222             if $desc =~ /^(BayStack|Ethernet\s+Routing\s+Switch)\s[2345](\d){2,3}/i;
223              
224             # BPS
225 3 50       9 return 'Nortel::Baystack' if $desc =~ /Business\sPolicy\sSwitch/i;
226              
227             # Nortel AP 222X
228 3 50       8 return 'Nortel::AP222x'
229             if ( $desc =~ /Access\s+Point\s+222/ );
230              
231             #------------------------------------------------------------------#
232              
233             # Allied Telesis Layer2 managed switches.
234 3 50       9 return 'Allied'
235             if ( $desc =~ /Allied.*AT-80\d{2}\S*/i );
236              
237 3 50       13 return 'Allied' if ( $desc =~ /allied/i );
238              
239             # Aruba wireless switches
240 3 50       7 return 'Aruba'
241             if ( $desc =~ /(ArubaOS|AirOS)/ );
242              
243             # Alcatel-Lucent branded Aruba
244 3 50       11 return 'Aruba'
245             if ( $desc =~ /^AOS-W/ );
246              
247 3 50       9 return 'Asante' if ( $desc =~ /asante/i );
248              
249             # Bay Hub
250 3 50       8 return 'Bayhub'
251             if ( $desc =~ /\bNMM.*Agent/ );
252 3 50       17 return 'Bayhub'
253             if ( $desc =~ /\bBay\s*Stack.*Hub/i );
254              
255             # Centillion ATM
256 3 50       5 return 'Centillion' if ( $desc =~ /MCP/ );
257              
258             # IBM BladeCenter 4-Port GB Ethernet Switch Module
259 3 50       8 return 'Dell'
260             if ( $desc =~ /^IBM Gigabit Ethernet Switch Module$/ );
261              
262             # Linksys 2024/2048
263 3 50       6 return 'Dell'
264             if ( $desc
265             =~ /^(24|48)-Port 10\/100\/1000 Gigabit Switch (with |w\/)WebView$/ );
266              
267             # Foundry
268 3 50       9 return 'Foundry' if $desc =~ /foundry/i;
269              
270             # Kentrox DataSMART DSU/CSU
271 3 50       9 return 'Kentrox'
272             if ( $desc =~ /^DataSMART/i );
273              
274             # Orinoco
275 3 50       11 return 'Orinoco'
276             if ( $desc =~ /(AP-\d{3}|WavePOINT)/ );
277              
278             # Synoptics Hub
279 3 50       17 return 'S3000'
280             if ( $desc =~ /\bNMM\s+(281|3000|3030)/i );
281              
282             # SonicWALL
283 3 50       9 return 'SonicWALL' if $desc =~ /SonicWALL/i;
284              
285             # Avaya Secure Router
286 3 50       14 return 'Tasman'
287             if ( $desc =~ /^(avaya|nortel)\s+(SR|secure\srouter)\s+\d{4}/i );
288             }
289              
290             1;
291              
292             __END__
293              
294             =pod
295              
296             =head1 NAME
297              
298             SNMP::Insight::Classifier - Guess SNMP Agent from description
299              
300             =head1 VERSION
301              
302             version 0.002
303              
304             =head1 DESCRIPTION
305              
306             Autodiscovery of device type applying heuristics on SNMPv2 entities
307              
308             =head1 ATTRIBUTES
309              
310             =head2 device
311              
312             The instance of L<SNMP::Insight::Device> on which perform device type guessing.
313              
314             =head2 desc
315              
316             A cleaned sysDescr (no new lines nor special characters).
317              
318             =head1 METHODS
319              
320             =head2 classify
321              
322             Return a suitable device role for the associated device.
323              
324             =head2 guess_by_vendor
325              
326             Use sysObjectID to guess device type.
327              
328             =head2 guess_by_desc
329              
330             Use sysObjectID to guess device type.
331              
332             =head1 AUTHOR
333              
334             Gabriele Mambrini <g.mambrini@gmail.com>
335              
336             =head1 COPYRIGHT AND LICENSE
337              
338             This software is copyright (c) 2015 by Gabriele Mambrini.
339              
340             This is free software; you can redistribute it and/or modify it under
341             the same terms as the Perl 5 programming language system itself.
342              
343             =cut