Info/Layer2/Ubiquiti.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 279 CC 27 SCAR 42.3
CC
27
Cov
0%
model
182.0
os_ver
30.0
os
20.0
CRAP 68.8
1 # SNMP::Info::Layer2::Ubiquiti - SNMP Interface to Ubiquiti Devices
2 #
3 # Copyright (c) 2019 by The Netdisco Developer Team.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the University of California, Santa Cruz nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28  
29 package SNMP::Info::Layer2::Ubiquiti;
30  
31 use strict;
32 use warnings;
33 use Exporter;
34 use SNMP::Info::IEEE802dot11;
35 use SNMP::Info::Layer2;
36 use SNMP::Info::Layer3; # only used in sub mac()
37  
38  
39  
40 @SNMP::Info::Layer2::Ubiquiti::ISA
41 = qw/SNMP::Info::IEEE802dot11 SNMP::Info::Layer2 Exporter/;
42 @SNMP::Info::Layer2::Ubiquiti::EXPORT_OK = qw//;
43  
44 our ($VERSION, %FUNCS, %GLOBALS, %MIBS, %MUNGE);
45  
46 $VERSION = '3.985003';
47  
48 %MIBS = (
49 %SNMP::Info::Layer2::MIBS,
50 %SNMP::Info::IEEE802dot11::MIBS,
51 );
52  
53 %GLOBALS = (
54 %SNMP::Info::Layer2::GLOBALS,
55 %SNMP::Info::IEEE802dot11::GLOBALS,
56 );
57  
58 %FUNCS = (
59 %SNMP::Info::Layer2::FUNCS,
60 %SNMP::Info::IEEE802dot11::FUNCS,
61 );
62  
63 %MUNGE = ( %SNMP::Info::Layer2::MUNGE, %SNMP::Info::IEEE802dot11::MUNGE, );
64  
65 sub os {
66 my $ubnt = shift;
67  
68 my $names = $ubnt->dot11_prod_name();
69  
70 foreach my $iid ( keys %$names ) {
71 my $prod = $names->{$iid};
72 next unless defined $prod;
73 # Product names that match AirOS products
74 if((lc $prod) =~ /station/ or (lc $prod) =~ /beam/ or (lc $prod) =~ /grid/){
75 return 'AirOS';
76 # Product names that match UAP
77 }elsif((lc $prod) =~ /uap/){
78 return 'UniFi';
79 }else{
80 # Continue below to find OS name
81 }
82 }
83  
84 ## EdgeMAX OS (EdgeSwitch and EdgeRouter) name is first field split by space
85 my $ver = $ubnt->description() || '';
86  
87 my @myver = split(/ /, $ver);
88  
89 return $myver[0];
90 }
91  
92 sub os_ver {
93 my $ubnt = shift;
94  
95 my $versions = $ubnt->dot11_prod_ver();
96  
97 foreach my $iid ( keys %$versions ) {
98 my $ver = $versions->{$iid};
99 next unless defined $ver;
100 my $os = $ubnt->os;
101 if($os == 'AirOS'){
102 ## pretty up the version reporting for AirOS to include hardware (XW, XM, etc) and three major groups of firmware
103 my @firmware = split(/v/, $ver);
104 my @firmwareSplit = split(/\./, $firmware[1]);
105 my @prefix = split(/\./, $ver);
106 $ver = $prefix[0] . '-v' . join('.', @firmwareSplit[0,1,2]);
107 }
108 return $ver;
109 ## Not sure what this function does, it seems to be extraneous being in the same code block after a return statement?
110 #if ( $ver =~ /([\d\.]+)/ ) {
111 # return $1;
112 #}
113 }
114  
115 my $ver = $ubnt->description() || '';
116 if((lc $ver) =~ /^edgeswitch/){
117 ## EdgeSwitch OS version is second field split by comma and bootcode version is last
118 my @myver = split(/, /, $ver);
119 my @firmware = split(/\./, $myver[1]);
120 my @bootcode = split(/\./, $myver[-1]);
121  
122 ## Return only three major version groupings and include bootcode version
123 return join('.', @firmware[0,1,2]) . '.-b' . join('.', @bootcode[0,1,2]);
124  
125 }
126  
127 ## EdgeRouter OS version is second field split by space
128 my @myver = split(/ /, $ver);
129 my @firmware = split(/\./, $myver[1]);
130  
131 if($firmware[2] =~ /hotfix$/){
132 # edge case where EdgeOS has hotfix versions in format "EdgeOS v1.9.7+hotfix.4.5024004.171005.0403"
133 $firmware[2] = $firmware[2] . '.' . $firmware[3]
134 }
135 ## Return only three major version groupings
136 return join('.', @firmware[0,1,2]);
137 }
138  
139 sub vendor {
140 return 'ubiquiti';
141 }
142  
143 sub model {
144 my $ubnt = shift;
145  
146 my $names = $ubnt->dot11_prod_name();
147  
148 foreach my $iid ( keys %$names ) {
149 my $prod = $names->{$iid};
150 next unless defined $prod;
151 return $prod;
152 }
153  
154 my $desc = $ubnt->description() || '';
155  
156 ## Pull Model from beginning of description, separated by comma (EdgeSwitch)
157 if((lc $desc) =~ /^edgeswitch/){
158 my @mydesc = split(/, /, $desc);
159 return $mydesc[0];
160 }
161  
162 if(!((lc $desc) =~ /edgeos/)){
163 # Not sure what type of device this is to get Model
164 # Wireless devices report dot11_prod_name
165 # EdgeSwitch includes model directly and edgeos logic is in else statement
166 return ;
167 }else{
168 ## do some logic to determine ER model based on tech specs from ubnt:
169 ## https://help.ubnt.com/hc/en-us/articles/219652227--EdgeRouter-Which-EdgeRouter-Should-I-Use-#tech%20specs
170 ## Would be nice if UBNT simply adds the model string to their SNMP daemon directly
171 my $ethCount = 0;
172 my $switchCount = 0;
173 #my $sfpCount = 0;
174 #my $poeCount = 0;
175 my $memTotalReal = $ubnt->memTotalReal;
176 my $cpuLoad = $ubnt->hrProcessorLoad;
177 my $cpuCount = 0;
178 ## My perl is lacking. Not sure if there's a more efficient way to find the cpu count
179 foreach my $iid ( keys %$cpuLoad ) {
180 $cpuCount++;
181 }
182  
183 my $ifDescs = $ubnt->ifDescr;
184 foreach my $iid ( keys %$ifDescs ) {
185 my $ifDesc = $ifDescs->{$iid};
186 next unless defined $ifDesc;
187  
188 if((lc $ifDesc) =~ /^eth\d+$/){ # exclude vlan interfaces. Ex: eth1.5
189 $ethCount++;
190 }elsif((lc $ifDesc) =~ /^switch/){
191 $switchCount++;
192 }
193 }
194  
195 ## If people have other models to further fine-tune this logic that would be great.
196 if($ethCount eq 9){
197 ## Should be ER Infinity
198 return "EdgeRouter Infinity"
199 }if($ethCount eq 8){
200 ## Could be ER-8 Pro, ER-8, or EP-R8
201 return "EdgeRouter 8-Port"
202 }if($ethCount eq 12){
203 ## ER-12
204 return "EdgeRouter 12-Port"
205 }elsif($ethCount eq 5 and $cpuCount eq 4){
206 ## Could be ER-X or ER-X-SFP
207 return "EdgeRouter X 5-Port"
208 }elsif($ethCount eq 5){
209 return "EdgeRouter PoE 5-Port"
210 }elsif($ethCount eq 3 and $cpuCount eq 2){
211 return "EdgeRouter LITE 3-Port"
212 }else{
213 ## failback string
214 return "EdgeRouter eth-$ethCount switch-$switchCount mem-$memTotalReal cpuNum-$cpuCount";
215 }
216  
217 }
218 }
219  
220 ## simply take the MAC and clean it up
221 sub serial {
222 my $ubnt = shift;
223  
224 my $serial = $ubnt->mac();
225 if($serial){
226 $serial =~ s/://g;
227 return uc $serial;
228 }
229 return ;
230 }
231  
232 ## UBNT doesn't put the primary-mac interface at index 1
233 sub mac {
234 my $ubnt = shift;
235 my $ifDescs = $ubnt->ifDescr;
236 my $erModel = $ubnt->model;
237  
238 foreach my $iid ( keys %$ifDescs ) {
239 my $ifDesc = $ifDescs->{$iid};
240 next unless defined $ifDesc;
241 ## CPU Interface will have the primary MAC for EdgeSwitch
242 ## eth0 will have primary MAC for linux-based UBNT devices, except ER-12 which is eth11
243 if($ifDesc =~ /CPU/ or ($ifDesc eq 'eth0' and !($erModel =~ /EdgeRouter 12/)) or ($ifDesc eq 'eth11' and ($erModel =~ /EdgeRouter 12/))){
244 my $mac = $ubnt->ifPhysAddress->{$iid};
245  
246 # syntax stolen from sub munge_mac in SNMP::Info
247 $mac = lc join( ':', map { sprintf "%02x", $_ } unpack( 'C*', $mac ) );
248 return $mac if $mac =~ /^([0-9A-F][0-9A-F]:){5}[0-9A-F][0-9A-F]$/i;
249 }
250 }
251  
252 # MAC malformed or missing
253 return;
254 }
255  
256 sub interfaces {
257 my $ubnt = shift;
258 my $partial = shift;
259  
260 my $interfaces = $ubnt->i_index($partial) || {};
261 my $i_descr = $ubnt->i_description($partial) || {};
262 my $return = {};
263  
264 foreach my $iid ( keys %$i_descr ) {
265 # Slot: 0 Port: 4 Gigabit - Level
266 if ($i_descr->{$iid} =~ m/([0-9]+)[^0-9]+([0-9]+)/) {
267 $return->{$iid} = $1 .'/'. $2;
268 next;
269 }
270 # Link Aggregate 4
271 if ($i_descr->{$iid} =~ m/Link Aggregate (\d+)/) {
272 $return->{$iid} = '3/'. $1;
273 next;
274 }
275 # else
276 $return->{$iid} = $i_descr->{$iid};
277 }
278  
279 return $return;
280 }
281  
282 sub i_ignore {
283 my $l2 = shift;
284 my $partial = shift;
285  
286 my $interfaces = $l2->interfaces($partial) || {};
287 my $i_descr = $l2->i_description($partial) || {};
288  
289 my %i_ignore;
290 foreach my $if ( keys %$interfaces ) {
291  
292 # CPU Interface
293 if ( $i_descr->{$if} =~ /CPU Interface/i ) {
294 $i_ignore{$if}++;
295 }
296 }
297 return \%i_ignore;
298 }
299  
300 1;
301 __END__