lib/Cisco/SNMP/Interface.pm

filter: Statementstmt 9.5% 37 / 387 Branchbran 0.0% 0 / 174 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 34.7% 8 / 23 Podpod 100% 13 / 13 totaltotal 8.9% 58 / 652 CC 121 SCAR 64.3
CC
121
Cov
6%
interface_metrics
1560.0
interface_info
552.0
interface_utilization
420.0
CRAP 620.8
« lib/Cisco/SNMP/ProxyPing.pm ↑ Summary lib/Cisco/SNMP/CPU.pm »
1 package Cisco::SNMP::Interface;
2  
3 ##################################################
4 # AUTHOR = Michael Vincent
5 # www.VinsWorld.com
6 ##################################################
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Net::SNMP qw(:asn1);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Cisco::SNMP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = $Cisco::SNMP::VERSION;
15  
16 3 our @ISA = qw(Cisco::SNMP);
17  
18 ##################################################
19 # Start Public Module
20 ##################################################
21  
22 0 ▶sub _ifOID {
Subroutine _ifOID: not called CC=1 SCAR=6.9
23 0 return '1.3.6.1.2.1.2.2.1';
24 }
25  
26 6 ▶sub ifOIDs {
Subroutine ifOIDs: called CC=1 SCAR=0.0
Pod documented
27 6 return qw(Index Description Type MTU Speed PhysAddress AdminStatus
28 OperStatus LastChange Duplex Name Alias);
29 }
30  
31 0 ▶sub ipOIDs {
Subroutine ipOIDs: not called CC=1 SCAR=6.9
Pod documented
32 0 return qw(Addr NetMask);
33 }
34  
35 0 ▶sub ifMetricUserOIDs {
Subroutine ifMetricUserOIDs: not called CC=1 SCAR=6.9
Pod documented
36 0 return qw(Multicasts Broadcasts Octets Unicasts Discards Errors
37 Unknowns);
38 }
39  
40 3 ▶sub ifMetricOIDs {
Subroutine ifMetricOIDs: called CC=1 SCAR=0.0
Pod documented
41 3 return qw(InMulticasts OutMulticasts InBroadcasts OutBroadcasts
42 InOctets OutOctets InUnicasts OutUnicasts InDiscards OutDiscards
43 InErrors OutErrors InUnknowns);
44 }
45  
46 3 ▶sub _ifMetricOIDVals {
Subroutine _ifMetricOIDVals: called CC=1 SCAR=0.0
47 3 return qw(InOctets 10 OutOctets 16 InUnicasts 11 OutUnicasts 17
48 InDiscards 13 OutDiscards 19 InErrors 14 OutErrors 20 InUnknowns 15);
49 }
50  
51 0 ▶sub interface_getbyindex {
Subroutine interface_getbyindex: not called CC=11 SCAR=48.8
Pod documented
52 0 my $self = shift;
53  
54 0 my $session = $self->{_SESSION_};
55  
56 0 my $uIfx;
57 my %args;
58 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
59 0 ($uIfx) = @_;
60 0 ▶ if ( $uIfx !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uIfx =~ /^\d+$/) 0 0 0
61 0 $Cisco::SNMP::LASTERROR = "Invalid ifIndex `$uIfx'";
62 0 return undef;
63 }
64 } else {
65 0 %args = @_;
66 0 for ( keys(%args) ) {
67 0 ▶ if ( (/^-?interface$/i) || (/^-?index$/i) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-?interface$/i or /^-?index$/i) 0 0 0
Condition0%0/3
/^-?interface$/i or /^-?index$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
/^-?interface$/i or /^-?index$/i
/^-?interface$/i /^-?index$/i
Truth table0%0/3
A or B
A= /^-?interface$/iB= /^-?index$/i
ABresult
1X1
011
000
68 0 ▶ if ( $args{$_} =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$_} =~ /^\d+$/) { } 0 0 0
69 0 $uIfx = $args{$_};
70 } else {
71 0 $Cisco::SNMP::LASTERROR = "Invalid ifIndex `$args{$_}'";
72 0 return undef;
73 }
74 }
75 }
76 }
77 0 ▶ if ( not defined $uIfx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uIfx) 0 0 0
78 0 $Cisco::SNMP::LASTERROR = "No ifIndex provided";
79 0 return undef;
80 }
81 0 my $rIf = Cisco::SNMP::_snmpwalk( $session, _ifOID() . '.2' );
82 0 ▶ if ( not defined $rIf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rIf) 0 0 0
83 0 $Cisco::SNMP::LASTERROR = "Cannot get interface names from device";
84 0 return undef;
85 }
86 0 my $rIfx = Cisco::SNMP::_snmpwalk( $session, _ifOID() . '.1' );
87  
88 0 for ( 0 .. $#{$rIfx} ) {
89 0 ▶ if ( $rIfx->[$_] == $uIfx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rIfx->[$_] == $uIfx) 0 0 0
90 0 return $rIf->[$_];
91 }
92 }
93 0 $Cisco::SNMP::LASTERROR = "Cannot get interface for ifIndex `$uIfx'";
94 0 return undef;
95 }
96  
97 0 ▶sub interface_getbyname {
Subroutine interface_getbyname: not called CC=14 SCAR=53.5
Pod documented
98 0 my $self = shift;
99  
100 0 my $session = $self->{_SESSION_};
101  
102 0 my %params = ( 'index' => 0 );
103  
104 0 my %args;
105 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
106 0 ( $params{uIf} ) = @_;
107 } else {
108 0 %args = @_;
109 0 for ( keys(%args) ) {
110 0 ▶ if (/^-?interface$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^-?interface$/i) { } 0 0 0
elsif (/^-?index$/i) { } 0 0 0
111 0 $params{uIf} = $args{$_};
112 } elsif (/^-?index$/i) {
113 0 ▶ if ( $args{$_} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$_} == 1) 0 0 0
114 0 $params{index} = 1;
115 }
116 }
117 }
118 }
119 0 ▶ if ( !exists( $params{uIf} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{"uIf"}) 0 0 0
120 0 $Cisco::SNMP::LASTERROR = "No interface provided";
121 0 return undef;
122 }
123  
124 0 my $rIf = Cisco::SNMP::_snmpwalk( $session, _ifOID() . '.2' );
125 0 ▶ if ( not defined $rIf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rIf) 0 0 0
126 0 $Cisco::SNMP::LASTERROR = "Cannot get interface names from device";
127 0 return undef;
128 }
129 0 my $rIfx = Cisco::SNMP::_snmpwalk( $session, _ifOID() . '.1' );
130  
131 # user Provided
132 # split Gig0/1 into 'Gig' '0/1'
133 0 my @parts = split /([0-9])/, $params{uIf}, 2;
134 0 my $uIfNamePart = shift @parts;
135 0 my $uIfNumPart = "@parts";
136 0 $uIfNumPart =~ s/\s+//;
137  
138 0 my @matches;
139 my $idx;
140 0 for ( 0 .. $#{$rIf} ) {
141  
142 # Real Names
143 0 @parts = split /([0-9])/, $rIf->[$_], 2;
144 0 my $rIfNamePart = shift @parts;
145 0 my $rIfNumPart = "@parts";
146 0 $rIfNumPart =~ s/\s+//;
147 0 ▶ if ( ( $rIfNamePart =~ /^$uIfNamePart/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($rIfNamePart =~ /^$uIfNamePart/i and $rIfNumPart eq $uIfNumPart) 0 0 0
Condition0%0/3
$rIfNamePart =~ /^$uIfNamePart/i and $rIfNumPart eq $uIfNumPart
!ll&&!rl&&r
000
MC/DC0%0/2
$rIfNamePart =~ /^$uIfNamePart/i and $rIfNumPart eq $uIfNumPart
$rIfNamePart =~ /^$uIfNamePart/i $rIfNumPart eq $uIfNumPart
Truth table0%0/3
A and B
A= $rIfNamePart =~ /^$uIfNamePart/iB= $rIfNumPart eq $uIfNumPart
ABresult
0X0
100
111
148 && ( $rIfNumPart eq $uIfNumPart ) ) {
149 0 push @matches, $rIf->[$_];
150 0 $idx = $rIfx->[$_];
151 }
152 }
153 0 ▶ if ( @matches == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@matches == 1) { } 0 0 0
elsif (@matches == 0) { } 0 0 0
154 0 ▶ if ( $params{index} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'index'} == 0) { } 0 0 0
155 0 return "@matches";
156 } else {
157 0 return $idx;
158 }
159 } elsif ( @matches == 0 ) {
160 0 $Cisco::SNMP::LASTERROR = "Cannot find interface `$params{uIf}'";
161 0 return undef;
162 } else {
163 0 print "Interface `$params{uIf}' not specific enough - [@matches]";
164 0 return undef;
165 }
166 }
167  
168 0 ▶sub interface_info {
Subroutine interface_info: not called CC=23 SCAR=63.1
Pod documented
169 0 my $self = shift;
170 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
171  
172 0 my $session = $self->{_SESSION_};
173  
174 0 my %params = ( 'ifs' => [-1] );
175  
176 0 my %args;
177 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
178 0 ( $params{ifs} ) = @_;
179 0 ▶ if (not defined(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($params{"ifs"} = Cisco::SNMP::_get_range($params{"ifs"}))) 0 0 0
180 $params{ifs} = Cisco::SNMP::_get_range( $params{ifs} )
181 )
182 ) {
183 0 return undef;
184 }
185 } else {
186 0 %args = @_;
187 0 for ( keys(%args) ) {
188 0 ▶ if (/^-?interface(?:s)?$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-?interface(?:s)?$/i) 0 0 0
189 0 ▶ if (not defined(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($params{"ifs"} = Cisco::SNMP::_get_range($args{$_}))) 0 0 0
190 $params{ifs} = Cisco::SNMP::_get_range( $args{$_} )
191 )
192 ) {
193 0 return undef;
194 }
195 }
196 }
197 }
198  
199 0 my %IfInfo;
200 0 for my $ifs ( @{$params{ifs}} ) {
201  
202 0 my $interface;
203 0 ▶ if ( $ifs == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ifs == -1) { } 0 0 0
204 0 $interface = '';
205 } else {
206 0 $interface = '.' . $ifs;
207 }
208  
209 0 my %ret;
210 0 my @IFKEYS = ifOIDs();
211  
212 # -3 because last key (Duplex Name Alias) are different OID
213 0 for my $oid ( 0 .. $#IFKEYS - 3 ) {
214 0 $ret{$IFKEYS[$oid]} = Cisco::SNMP::_snmpwalk( $session,
215 _ifOID() . '.' . ( $oid + 1 ) . $interface );
216 0 ▶ if ( not defined $ret{$IFKEYS[$oid]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFKEYS[$oid]}) 0 0 0
217 0 $Cisco::SNMP::LASTERROR
218 = "Cannot get interface `$interface' `$IFKEYS[$oid]'";
219 0 return undef;
220 }
221 }
222  
223 # Name Alias are different OID
224 0 $ret{Name} = Cisco::SNMP::_snmpwalk( $session,
225 '.1.3.6.1.2.1.31.1.1.1.1' . $interface );
226 0 $ret{Alias} = Cisco::SNMP::_snmpwalk( $session,
227 '.1.3.6.1.2.1.31.1.1.1.18' . $interface );
228  
229 # Duplex is different OID
230 0 my $OIDS;
231 0 ( $OIDS, $ret{$IFKEYS[9]} )
232 = Cisco::SNMP::_snmpwalk( $session,
233 '1.3.6.1.2.1.10.7.2.1.19' . $interface );
234 0 my %duplexIfs;
235 0 for ( 0 .. $#{$OIDS} ) {
236  
237 # split the OID at dots
238 0 my @if = split /\./, $OIDS->[$_];
239  
240 # take the last value, which is the ifIndex equal to value returned
241 0 $duplexIfs{$if[$#if]} = $ret{$IFKEYS[9]}->[$_];
242 }
243  
244 0 my %UpDownStatus = (
245 1 => 'UP',
246 2 => 'DOWN',
247 3 => 'TEST',
248 4 => 'UNKNOWN',
249 5 => 'DORMANT',
250 6 => 'NOTPRESENT',
251 7 => 'LOWLAYERDOWN'
252 );
253 0 my %DuplexType = (
254 1 => 'UNKNOWN',
255 2 => 'HALF',
256 3 => 'FULL'
257 );
258 0 for my $idx ( 0 .. $#{$ret{$IFKEYS[0]}} ) {
259 0 my %IfInfoHash;
260 0 for ( 0 .. $#IFKEYS ) {
261 0 ▶ if ( $_ == 5 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_ == 5) { } 0 0 0
elsif ($_ == 6 or $_ == 7) { } 0 0 0
elsif ($_ == 9) { } 0 0 0
262 $IfInfoHash{$IFKEYS[$_]}
263 = ( $ret{$IFKEYS[$_]}->[$idx] =~ /^\0/ )
264 ? unpack( 'H12', $ret{$IFKEYS[$_]}->[$idx] )
265 : ( ( $ret{$IFKEYS[$_]}->[$idx] =~ /^0x/ )
266 ? substr( $ret{$IFKEYS[$_]}->[$idx], 2 )
267 0 ▶ : $ret{$IFKEYS[$_]}->[$idx] );
Branch: 2 branches
BranchTrueFalseTotal
$ret{$IFKEYS[$_]}[$idx] =~ /^\0/ ? : 0 0 0
$ret{$IFKEYS[$_]}[$idx] =~ /^0x/ ? : 0 0 0
268 ▶ } elsif ( ( $_ == 6 ) || ( $_ == 7 ) ) {
Condition0%0/3
$_ == 6 or $_ == 7
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ == 6 or $_ == 7
$_ == 6 $_ == 7
Truth table0%0/3
A or B
A= $_ == 6B= $_ == 7
ABresult
1X1
011
000
269 $IfInfoHash{$IFKEYS[$_]}
270 = exists( $UpDownStatus{$ret{$IFKEYS[$_]}->[$idx]} )
271 ? $UpDownStatus{$ret{$IFKEYS[$_]}->[$idx]}
272 0 ▶ : $ret{$IFKEYS[$_]}->[$idx];
Branch: 1 branch
BranchTrueFalseTotal
exists $UpDownStatus{$ret{$IFKEYS[$_]}[$idx]} ? : 0 0 0
273 } elsif ( $_ == 9 ) {
274  
275 # if $duplexIfs{ifIndex}, not necessarily the current array index
276 0 ▶ if ( exists $duplexIfs{$ret{$IFKEYS[0]}->[$idx]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $duplexIfs{$ret{$IFKEYS[0]}[$idx]}) { } 0 0 0
277 $IfInfoHash{$IFKEYS[$_]}
278 = exists(
279 $DuplexType{$duplexIfs{$ret{$IFKEYS[0]}->[$idx]}}
280 )
281 ? $DuplexType{$duplexIfs{$ret{$IFKEYS[0]}->[$idx]}}
282 0 ▶ : $duplexIfs{$ret{$IFKEYS[0]}->[$idx]};
Branch: 1 branch
BranchTrueFalseTotal
exists $DuplexType{$duplexIfs{$ret{$IFKEYS[0]}[$idx]}} ? : 0 0 0
283 } else {
284 0 $IfInfoHash{$IFKEYS[$_]} = '';
285 }
286 } else {
287 0 $IfInfoHash{$IFKEYS[$_]} = $ret{$IFKEYS[$_]}->[$idx];
288 }
289 }
290 0 $IfInfo{$ret{$IFKEYS[0]}->[$idx]} = \%IfInfoHash;
291 }
292 }
293 0 return bless \%IfInfo, $class;
294 }
295  
296 3 for ( ifOIDs() ) {
297 36 Cisco::SNMP::_mk_accessors_hash_1( 'if', $_ );
298 }
299  
300 0 ▶sub interface_metrics {
Subroutine interface_metrics: not called CC=39 SCAR=73.5
Pod documented
301 0 my $self = shift;
302 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
303  
304 0 my $session = $self->{_SESSION_};
305  
306 0 my %params = ( 'ifs' => [-1], );
307  
308 # assume all metrics
309 0 my @IFMETRICUSERKEYS = ifMetricUserOIDs();
310 0 for (@IFMETRICUSERKEYS) {
311 0 $params{$_} = 1;
312 }
313  
314 0 my %args;
315 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
316 0 ( $params{ifs} ) = @_;
317 0 ▶ if (not defined(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($params{"ifs"} = Cisco::SNMP::_get_range($params{"ifs"}))) 0 0 0
318 $params{ifs} = Cisco::SNMP::_get_range( $params{ifs} )
319 )
320 ) {
321 0 return undef;
322 }
323 } else {
324 0 %args = @_;
325 0 for ( keys(%args) ) {
326 0 ▶ if (/^-?interface(?:s)?$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^-?interface(?:s)?$/i) { } 0 0 0
elsif (/^-?metric(?:s)?$/i) { } 0 0 0
327 0 ▶ if (not defined(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($params{"ifs"} = Cisco::SNMP::_get_range($args{$_}))) 0 0 0
328 $params{ifs} = Cisco::SNMP::_get_range( $args{$_} )
329 )
330 ) {
331 0 return undef;
332 }
333 } elsif (/^-?metric(?:s)?$/i) {
334  
335 # metrics provided - only use provided
336 0 for (@IFMETRICUSERKEYS) {
337 0 $params{$_} = 0;
338 }
339 0 ▶ if ( ref( $args{$_} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{$_} eq 'ARRAY') { } 0 0 0
340 0 $params{oids} = '';
341 0 for my $mets ( @{$args{$_}} ) {
342 0 ▶ if ( exists( $params{ucfirst( lc($mets) )} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{ucfirst lc $mets}) { } 0 0 0
343 0 $params{ucfirst( lc($mets) )} = 1;
344 } else {
345 0 $Cisco::SNMP::LASTERROR
346 = "Invalid metric `$mets'";
347 0 return undef;
348 }
349 }
350 } else {
351 0 $params{oids} = '';
352 0 ▶ if ( exists( $params{ucfirst( lc( $args{$_} ) )} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{ucfirst lc $args{$_}}) { } 0 0 0
353 0 $params{ucfirst( lc( $args{$_} ) )} = 1;
354 } else {
355 0 $Cisco::SNMP::LASTERROR
356 = "Invalid metric `$args{$_}'";
357 0 return undef;
358 }
359 }
360 }
361 }
362 }
363  
364 0 my %IfMetric;
365 0 my @IFMETRICKEYS = ifMetricOIDs();
366 0 for my $ifs ( @{$params{ifs}} ) {
367  
368 0 my $interface;
369 0 ▶ if ( $ifs == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ifs == -1) { } 0 0 0
370 0 $interface = '';
371 } else {
372 0 $interface = '.' . $ifs;
373 }
374  
375 0 my %ret;
376 $ret{Index}
377 0 = Cisco::SNMP::_snmpwalk( $session, _ifOID() . '.1' . $interface );
378 0 ▶ if ( not defined $ret{Index} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{"Index"}) 0 0 0
379 0 $Cisco::SNMP::LASTERROR = "Cannot get ifIndex `$interface'";
380 0 return undef;
381 }
382  
383 # multicasts
384 0 ▶ if ( $params{$IFMETRICUSERKEYS[0]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$IFMETRICUSERKEYS[0]}) 0 0 0
385  
386 # In
387 0 $ret{$IFMETRICKEYS[0]} = Cisco::SNMP::_snmpwalk( $session,
388 '1.3.6.1.2.1.31.1.1.1.2' . $interface );
389 0 ▶ if ( not defined $ret{$IFMETRICKEYS[0]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[0]}) 0 0 0
390 0 $Cisco::SNMP::LASTERROR
391 = "Cannot get interface `$interface' `$IFMETRICKEYS[0]'";
392 0 return undef;
393 }
394  
395 # Out
396 0 $ret{$IFMETRICKEYS[1]} = Cisco::SNMP::_snmpwalk( $session,
397 '1.3.6.1.2.1.31.1.1.1.4' . $interface );
398 0 ▶ if ( not defined $ret{$IFMETRICKEYS[1]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[1]}) 0 0 0
399 0 $Cisco::SNMP::LASTERROR
400 = "Cannot get interface `$interface' `$IFMETRICKEYS[1]'";
401 0 return undef;
402 }
403 }
404  
405 # broadcasts
406 0 ▶ if ( $params{$IFMETRICUSERKEYS[1]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$IFMETRICUSERKEYS[1]}) 0 0 0
407  
408 # In
409 0 $ret{$IFMETRICKEYS[2]} = Cisco::SNMP::_snmpwalk( $session,
410 '1.3.6.1.2.1.31.1.1.1.3' . $interface );
411 0 ▶ if ( not defined $ret{$IFMETRICKEYS[2]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[2]}) 0 0 0
412 0 $Cisco::SNMP::LASTERROR
413 = "Cannot get interface `$interface' `$IFMETRICKEYS[2]'";
414 0 return undef;
415 }
416  
417 # Out
418 0 $ret{$IFMETRICKEYS[3]} = Cisco::SNMP::_snmpwalk( $session,
419 '1.3.6.1.2.1.31.1.1.1.5' . $interface );
420 0 ▶ if ( not defined $ret{$IFMETRICKEYS[3]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[3]}) 0 0 0
421 0 $Cisco::SNMP::LASTERROR
422 = "Cannot get interface `$interface' `$IFMETRICKEYS[3]'";
423 0 return undef;
424 }
425 }
426  
427 # octets
428 0 ▶ if ( $params{$IFMETRICUSERKEYS[2]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$IFMETRICUSERKEYS[2]}) 0 0 0
429  
430 # In
431 0 $ret{$IFMETRICKEYS[4]} = Cisco::SNMP::_snmpwalk( $session,
432 _ifOID() . '.10' . $interface );
433 0 ▶ if ( not defined $ret{$IFMETRICKEYS[4]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[4]}) 0 0 0
434 0 $Cisco::SNMP::LASTERROR
435 = "Cannot get interface `$interface' `$IFMETRICKEYS[4]'";
436 0 return undef;
437 }
438  
439 # Out
440 0 $ret{$IFMETRICKEYS[5]} = Cisco::SNMP::_snmpwalk( $session,
441 _ifOID() . '.16' . $interface );
442 0 ▶ if ( not defined $ret{$IFMETRICKEYS[5]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[5]}) 0 0 0
443 0 $Cisco::SNMP::LASTERROR
444 = "Cannot get interface `$interface' `$IFMETRICKEYS[5]'";
445 0 return undef;
446 }
447 }
448  
449 # unicasts
450 0 ▶ if ( $params{$IFMETRICUSERKEYS[3]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$IFMETRICUSERKEYS[3]}) 0 0 0
451  
452 # In
453 0 $ret{$IFMETRICKEYS[6]} = Cisco::SNMP::_snmpwalk( $session,
454 _ifOID() . '.11' . $interface );
455 0 ▶ if ( not defined $ret{$IFMETRICKEYS[6]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[6]}) 0 0 0
456 0 $Cisco::SNMP::LASTERROR
457 = "Cannot get interface `$interface' `$IFMETRICKEYS[6]'";
458 0 return undef;
459 }
460  
461 # Out
462 0 $ret{$IFMETRICKEYS[7]} = Cisco::SNMP::_snmpwalk( $session,
463 _ifOID() . '.17' . $interface );
464 0 ▶ if ( not defined $ret{$IFMETRICKEYS[7]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[7]}) 0 0 0
465 0 $Cisco::SNMP::LASTERROR
466 = "Cannot get interface `$interface' `$IFMETRICKEYS[7]'";
467 0 return undef;
468 }
469 }
470  
471 # discards
472 0 ▶ if ( $params{$IFMETRICUSERKEYS[4]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$IFMETRICUSERKEYS[4]}) 0 0 0
473  
474 # In
475 0 $ret{$IFMETRICKEYS[8]} = Cisco::SNMP::_snmpwalk( $session,
476 _ifOID() . '.13' . $interface );
477 0 ▶ if ( not defined $ret{$IFMETRICKEYS[8]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[8]}) 0 0 0
478 0 $Cisco::SNMP::LASTERROR
479 = "Cannot get interface `$interface' `$IFMETRICKEYS[8]'";
480 0 return undef;
481 }
482  
483 # Out
484 0 $ret{$IFMETRICKEYS[9]} = Cisco::SNMP::_snmpwalk( $session,
485 _ifOID() . '.19' . $interface );
486 0 ▶ if ( not defined $ret{$IFMETRICKEYS[9]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[9]}) 0 0 0
487 0 $Cisco::SNMP::LASTERROR
488 = "Cannot get interface `$interface' `$IFMETRICKEYS[9]'";
489 0 return undef;
490 }
491 }
492  
493 # errors
494 0 ▶ if ( $params{$IFMETRICUSERKEYS[5]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$IFMETRICUSERKEYS[5]}) 0 0 0
495  
496 # In
497 0 $ret{$IFMETRICKEYS[10]} = Cisco::SNMP::_snmpwalk( $session,
498 _ifOID() . '.14' . $interface );
499 0 ▶ if ( not defined $ret{$IFMETRICKEYS[10]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[10]}) 0 0 0
500 0 $Cisco::SNMP::LASTERROR
501 = "Cannot get interface `$interface' `$IFMETRICKEYS[10]'";
502 0 return undef;
503 }
504  
505 # Out
506 0 $ret{$IFMETRICKEYS[11]} = Cisco::SNMP::_snmpwalk( $session,
507 _ifOID() . '.20' . $interface );
508 0 ▶ if ( not defined $ret{$IFMETRICKEYS[11]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[11]}) 0 0 0
509 0 $Cisco::SNMP::LASTERROR
510 = "Cannot get interface `$interface' `$IFMETRICKEYS[11]'";
511 0 return undef;
512 }
513 }
514  
515 # unknowns
516 0 ▶ if ( $params{$IFMETRICUSERKEYS[6]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$IFMETRICUSERKEYS[6]}) 0 0 0
517  
518 # In
519 0 $ret{$IFMETRICKEYS[12]} = Cisco::SNMP::_snmpwalk( $session,
520 _ifOID() . '.15' . $interface );
521 0 ▶ if ( not defined $ret{$IFMETRICKEYS[12]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret{$IFMETRICKEYS[12]}) 0 0 0
522 0 $Cisco::SNMP::LASTERROR
523 = "Cannot get interface `$interface' `$IFMETRICKEYS[12]'";
524 0 return undef;
525 }
526 }
527  
528 0 for my $idx ( 0 .. $#{$ret{Index}} ) {
529 0 my %IfMetricHash;
530 0 for ( 0 .. $#IFMETRICKEYS ) {
531 $IfMetricHash{$IFMETRICKEYS[$_]}
532 0 = $ret{$IFMETRICKEYS[$_]}->[$idx];
533 }
534 0 $IfMetric{$ret{Index}->[$idx]} = \%IfMetricHash;
535 }
536 }
537 0 return bless \%IfMetric, $class;
538 }
539  
540 3 for ( ifMetricOIDs() ) {
541 39 Cisco::SNMP::_mk_accessors_hash_1( 'if', $_ );
542 }
543  
544 0 ▶sub interface_utilization {
Subroutine interface_utilization: not called CC=20 SCAR=60.4
Pod documented
545 0 my $self = shift;
546 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
547  
548 0 my $session = $self->{_SESSION_};
549  
550 0 my %params = ( 'polling' => 10 );
551  
552 0 my %args;
553 0 ▶ if ( @_ != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 1) 0 0 0
554 0 %args = @_;
555 0 for ( keys(%args) ) {
556 0 ▶ if ( (/^-?polling$/i) || (/^-?interval$/i) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^-?polling$/i or /^-?interval$/i) { } 0 0 0
elsif (/^-?recursive$/i) { } 0 0 0
Condition0%0/3
/^-?polling$/i or /^-?interval$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
/^-?polling$/i or /^-?interval$/i
/^-?polling$/i /^-?interval$/i
Truth table0%0/3
A or B
A= /^-?polling$/iB= /^-?interval$/i
ABresult
1X1
011
000
557 0 ▶ if ( ( $args{$_} =~ /^\d+$/ ) && ( $args{$_} > 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$_} =~ /^\d+$/ and $args{$_} > 0) { } 0 0 0
Condition0%0/3
$args{$_} =~ /^\d+$/ and $args{$_} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$args{$_} =~ /^\d+$/ and $args{$_} > 0
$args{$_} =~ /^\d+$/ $args{$_} > 0
Truth table0%0/3
A and B
A= $args{$_} =~ /^\d+$/B= $args{$_} > 0
ABresult
0X0
100
111
558 0 $params{polling} = $args{$_};
559 } else {
560 0 $Cisco::SNMP::LASTERROR
561 = "Invalid polling interval `$args{$_}'";
562 0 return undef;
563 }
564 } elsif (/^-?recursive$/i) {
565 0 $params{recur} = $args{$_};
566 }
567 }
568 }
569  
570 0 my $prev;
571 0 ▶ if ( exists( $params{recur} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{'recur'} and ref $params{'recur'} eq __PACKAGE__) { } 0 0 0
Condition0%0/3
exists $params{"recur"} and ref $params{"recur"} eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params{"recur"} and ref $params{"recur"} eq __PACKAGE__
exists $params{"recur"} ref $params{"recur"} eq __PACKAGE__
Truth table0%0/3
A and B
A= exists $params{"recur"}B= ref $params{"recur"} eq __PACKAGE__
ABresult
0X0
100
111
572 && ( ref( $params{recur} ) eq __PACKAGE__ ) ) {
573 0 $prev = $params{recur};
574 } else {
575 0 ▶ if ( not defined( $prev = $self->interface_metrics(@_) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($prev = $self->interface_metrics(@_))) 0 0 0
576 0 $Cisco::SNMP::LASTERROR
577 = "Cannot get initial utilization: " . $Cisco::SNMP::LASTERROR;
578 0 return undef;
579 }
580 }
581 0 sleep $params{polling};
582 0 my $curr;
583 0 ▶ if ( not defined( $curr = $self->interface_metrics(@_) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($curr = $self->interface_metrics(@_))) 0 0 0
584 0 $Cisco::SNMP::LASTERROR
585 = "Cannot get current utilization: " . $Cisco::SNMP::LASTERROR;
586 0 return undef;
587 }
588  
589 0 my %IfUtil;
590 0 my @IFMETRICKEYS = ifMetricOIDs();
591 0 for my $ifs ( sort { $a <=> $b } ( keys( %{$prev} ) ) ) {
592 0 my %IfUtilHash;
593 0 for ( 0 .. $#IFMETRICKEYS ) {
594 0 ▶ if ( ( $_ == 4 ) || ( $_ == 5 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ == 4 or $_ == 5) { } 0 0 0
Condition0%0/3
$_ == 4 or $_ == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ == 4 or $_ == 5
$_ == 4 $_ == 5
Truth table0%0/3
A or B
A= $_ == 4B= $_ == 5
ABresult
1X1
011
000
595 $IfUtilHash{$IFMETRICKEYS[$_]}
596 = defined( $curr->{$ifs}->{$IFMETRICKEYS[$_]} )
597 ? (
598 ( $curr->{$ifs}->{$IFMETRICKEYS[$_]}
599 - $prev->{$ifs}->{$IFMETRICKEYS[$_]}
600 ) * 8
601 ) / $params{polling}
602 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $curr->{$ifs}{$IFMETRICKEYS[$_]} ? : 0 0 0
603 } else {
604 $IfUtilHash{$IFMETRICKEYS[$_]}
605 = defined( $curr->{$ifs}->{$IFMETRICKEYS[$_]} )
606 ? ( $curr->{$ifs}->{$IFMETRICKEYS[$_]}
607 - $prev->{$ifs}->{$IFMETRICKEYS[$_]} )
608 / $params{polling}
609 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $curr->{$ifs}{$IFMETRICKEYS[$_]} ? : 0 0 0
610 }
611 }
612 0 $IfUtil{$ifs} = \%IfUtilHash;
613 }
614 0 $prev = bless \%IfUtil, $class;
615 0 ▶ return wantarray ? ( $prev, $curr ) : $prev;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
616 }
617  
618 0 ▶sub interface_updown {
Subroutine interface_updown: not called CC=13 SCAR=52.0
Pod documented
619 0 my $self = shift;
620  
621 0 my $session = $self->{_SESSION_};
622  
623 0 my %op = (
624 'UP' => 1,
625 'DOWN' => 2
626 );
627 0 my %params = ( 'oper' => $op{UP} );
628  
629 0 my %args;
630 0 my $oper = 'UP';
631 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
632 0 ( $params{ifs} ) = @_;
633 0 ▶ if (not defined(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($params{"ifs"} = Cisco::SNMP::_get_range($params{"ifs"}))) 0 0 0
634 $params{ifs} = Cisco::SNMP::_get_range( $params{ifs} )
635 )
636 ) {
637 0 return undef;
638 }
639 } else {
640 0 %args = @_;
641 0 for ( keys(%args) ) {
642 0 ▶ if (/^-?interface(?:s)?$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^-?interface(?:s)?$/i) { } 0 0 0
elsif (/^-?operation$/i or /^-?command$/i) { } 0 0 0
643 0 ▶ if (not defined(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($params{"ifs"} = Cisco::SNMP::_get_range($args{$_}))) 0 0 0
644 $params{ifs} = Cisco::SNMP::_get_range( $args{$_} )
645 )
646 ) {
647 0 return undef;
648 }
649 ▶ } elsif ( (/^-?operation$/i) || (/^-?command$/i) ) {
Condition0%0/3
/^-?operation$/i or /^-?command$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
/^-?operation$/i or /^-?command$/i
/^-?operation$/i /^-?command$/i
Truth table0%0/3
A or B
A= /^-?operation$/iB= /^-?command$/i
ABresult
1X1
011
000
650 0 ▶ if ( exists( $op{uc( $args{$_} )} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $op{uc $args{$_}}) { } 0 0 0
651 0 $params{oper} = $op{uc( $args{$_} )};
652 0 $oper = uc( $args{$_} );
653 } else {
654 0 $Cisco::SNMP::LASTERROR = "Invalid operation `$args{$_}'";
655 0 return undef;
656 }
657 }
658 }
659 }
660  
661 0 ▶ if ( not defined $params{ifs} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"ifs"}) 0 0 0
662 0 $params{ifs} = Cisco::SNMP::_snmpwalk( $session, _ifOID() . '.1' );
663 0 ▶ if ( not defined $params{ifs} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"ifs"}) 0 0 0
664 0 $Cisco::SNMP::LASTERROR = "Cannot get interfaces to $oper";
665 0 return undef;
666 }
667 }
668  
669 0 my @intf;
670 0 for ( @{$params{ifs}} ) {
671 0 ▶ if (defined $session->set_request(
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session->set_request(_ifOID() . '.7.' . $_, INTEGER(), $params{'oper'})) { } 0 0 0
672 _ifOID() . '.7.' . $_,
673 INTEGER, $params{oper}
674 )
675 ) {
676 0 push @intf, $_;
677 } else {
678 0 $Cisco::SNMP::LASTERROR = "Failed to $oper interface $_";
679 0 return undef;
680 }
681 }
682 0 return \@intf;
683 }
684  
685 3 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
686  
687 # get_ direct
688 3 my @OIDS = ifOIDs();
689  
690 # -3 because last keys (Duplex Name Alias) are different OID
691 3 for my $o ( 0 .. $#OIDS - 3 ) {
692 27 ▶ *{"get_if" . $OIDS[$o]} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.9
693 0 my $self = shift;
694 0 my ($val) = @_;
695  
696 0 ▶ if ( not defined $val ) { $val = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
697 0 my $s = $self->session;
698 0 my $r = $s->get_request(
699 varbindlist => [_ifOID() . '.' . ( $o + 1 ) . '.' . $val] );
700 0 return $r->{_ifOID() . '.' . ( $o + 1 ) . '.' . $val};
701 }
702 27 }
703  
704 0 ▶sub get_ifDuplex {
Subroutine get_ifDuplex: not called CC=2 SCAR=17.9
Pod documented
705 0 my $self = shift;
706 0 my ($val) = @_;
707  
708 0 ▶ if ( not defined $val ) { $val = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
709 0 my $s = $self->session;
710 0 my $r
711 = $s->get_request( varbindlist => ['1.3.6.1.2.1.10.7.2.1.19.' . $val] );
712 0 return $r->{'1.3.6.1.2.1.10.7.2.1.19.' . $val};
713 }
714  
715 0 ▶sub get_ifName {
Subroutine get_ifName: not called CC=2 SCAR=17.9
Pod documented
716 0 my $self = shift;
717 0 my ($val) = @_;
718  
719 0 ▶ if ( not defined $val ) { $val = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
720 0 my $s = $self->session;
721 0 my $r
722 = $s->get_request( varbindlist => ['1.3.6.1.2.1.31.1.1.1.1.' . $val] );
723 0 return $r->{'1.3.6.1.2.1.31.1.1.1.1.' . $val};
724 }
725  
726 0 ▶sub get_ifAlias {
Subroutine get_ifAlias: not called CC=2 SCAR=17.9
Pod documented
727 0 my $self = shift;
728 0 my ($val) = @_;
729  
730 0 ▶ if ( not defined $val ) { $val = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
731 0 my $s = $self->session;
732 0 my $r
733 = $s->get_request( varbindlist => ['1.3.6.1.2.1.31.1.1.1.18.' . $val] );
734 0 return $r->{'1.3.6.1.2.1.31.1.1.1.18.' . $val};
735 }
736  
737 3 my %OIDS = _ifMetricOIDVals();
738 3 for my $o ( keys(%OIDS) ) {
739 27 ▶ *{"get_if" . $o} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.9
740 0 my $self = shift;
741 0 my ($val) = @_;
742  
743 0 ▶ if ( not defined $val ) { $val = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
744 0 my $s = $self->session;
745 my $r = $s->get_request(
746 0 varbindlist => [_ifOID() . '.' . $OIDS{$o} . '.' . $val] );
747 0 return $r->{_ifOID() . '.' . $OIDS{$o} . '.' . $val};
748 }
749 27 }
750  
751 3 @OIDS = qw(InMulticasts InBroadcasts OutMulticasts OutBroadcasts);
752 3 for my $o ( 0 .. 3 ) {
753 12 ▶ *{"get_if" . $OIDS[$o]} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.9
754 0 my $self = shift;
755 0 my ($val) = @_;
756  
757 0 ▶ if ( not defined $val ) { $val = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
758 0 my $s = $self->session;
759 0 my $r = $s->get_request( varbindlist =>
760 ['1.3.6.1.2.1.31.1.1.1.' . ( $o + 2 ) . '.' . $val] );
761 0 return $r->{'1.3.6.1.2.1.31.1.1.1.' . ( $o + 2 ) . '.' . $val};
762 }
763 12 }
764  
765 ##################################################
766 # End Public Module
767 ##################################################
768  
769 3 1;
770  
771 __END__
« lib/Cisco/SNMP/ProxyPing.pm ↑ Summary lib/Cisco/SNMP/CPU.pm »