Insight/Session/NetSNMP.pm

filter: Statementstmt 19.7% 13 / 66 Branchbran 0.0% 0 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 3 / 6 Podpod 100% 2 / 2 totaltotal 15.7% 18 / 114 CC 22 SCAR 47.1
CC
22
Cov
12%
_build_driver
156.0
get_subtree
110.0
get_scalar
6.0
CRAP 110.6
« Insight/Classifier.pm ↑ Summary Insight/Device/Cisco/Catalyst.pm »
1 package SNMP::Insight::Session::NetSNMP;
2  
3 #ABSTRACT: Net::SNMP based implementation for SNMP::Insight::Session
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.002'; #TRIAL VERSION:
7  
8 1 with 'SNMP::Insight::Session';
9  
10 1 ▶use Net::SNMP 6.0 qw( :snmp DEBUG_ALL ENDOFMIBVIEW );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use SNMP::Insight::Utils qw( _debug_level _debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 has '_driver' => (
14 is => 'ro',
15 isa => 'Object',
16 lazy => 1,
17 builder => '_build_driver',
18 );
19  
20 0 ▶sub _build_driver {
Subroutine _build_driver: not called CC=12 SCAR=50.5
21 0 my $self = shift;
22  
23 0 my %options;
24 0 $options{-hostname} = $self->hostname;
25 0 $options{-port} = $self->port;
26  
27 0 my %version_map = ( 1 => 'snmpv1', '2c' => 'snmpv2c', 3 => 'snmpv3' );
28 0 $options{-version} = $version_map{ $self->version };
29  
30 # $options{-domain} = $self->domain;
31 0 $options{-timeout} = $self->timeout;
32 0 $options{-retries} = $self->retries;
33  
34 0 ▶ $options{-debug} = DEBUG_ALL if _debug_level() > 1;
Branch: 1 branch
BranchTrueFalseTotal
if _debug_level() > 1 0 0 0
35  
36 0 ▶ $options{-localaddr} = $self->localaddr if $self->localaddr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->localaddr 0 0 0
37 0 ▶ $options{-localport} = $self->localport if $self->localport;
Branch: 1 branch
BranchTrueFalseTotal
if $self->localport 0 0 0
38 0 ▶ $options{-community} = $self->community if $self->community;
Branch: 1 branch
BranchTrueFalseTotal
if $self->community 0 0 0
39 0 ▶ $options{-username} = $self->username if $self->username;
Branch: 1 branch
BranchTrueFalseTotal
if $self->username 0 0 0
40 0 ▶ $options{-authkey} = $self->authkey if $self->authkey;
Branch: 1 branch
BranchTrueFalseTotal
if $self->authkey 0 0 0
41 0 ▶ $options{-authpassword} = $self->authpassword if $self->authpassword;
Branch: 1 branch
BranchTrueFalseTotal
if $self->authpassword 0 0 0
42 0 ▶ $options{-authprotocol} = $self->authprotocol if $self->authprotocol;
Branch: 1 branch
BranchTrueFalseTotal
if $self->authprotocol 0 0 0
43 0 ▶ $options{-privkey} = $self->privkey if $self->privkey;
Branch: 1 branch
BranchTrueFalseTotal
if $self->privkey 0 0 0
44 0 ▶ $options{-privpassword} = $self->privpassword if $self->privpassword;
Branch: 1 branch
BranchTrueFalseTotal
if $self->privpassword 0 0 0
45 0 ▶ $options{-privprotocol} = $self->privprotocol if $self->privprotocol;
Branch: 1 branch
BranchTrueFalseTotal
if $self->privprotocol 0 0 0
46  
47 $options{-translate} = [
48 0 '-all' => 1,
49 '-octetstring' => 0,
50 '-null' => 1,
51 '-timeticks' => 0,
52 '-opaque' => 1,
53 '-nosuchobject' => 1,
54 '-nosuchinstance' => 1,
55 '-endofmibview' => 1,
56 '-unsigned' => 1,
57 ];
58  
59 0 return Net::SNMP->session(%options);
60  
61 }
62  
63 0 ▶sub get_scalar {
Subroutine get_scalar: not called CC=2 SCAR=17.9
Pod documented
64 0 my ( $self, $oid ) = @_;
65  
66 0 my $session = $self->_driver;
67  
68 #add istance number to the oid
69 0 $oid .= '.0';
70  
71 0 _debug("SNMP::Insight fetching scalar $oid\n");
72  
73 0 my $result = $session->get_request( '-varbindlist' => [$oid] );
74 0 ▶ $result or die "SNMP error " . $session->error();
Branch: 1 branch
BranchTrueFalseTotal
$result or die 'SNMP error ' . $session->error 0 0 0
75  
76 0 return $result->{$oid};
77 }
78  
79 0 ▶sub get_subtree {
Subroutine get_subtree: not called CC=10 SCAR=47.0
Pod documented
80 0 my ( $self, $oid ) = @_;
81  
82 0 my @result;
83  
84 0 my $s = $self->_driver;
85 0 ▶ $oid eq '.' and $oid = '0';
Branch: 1 branch
BranchTrueFalseTotal
$oid eq "." and $oid = 0 0 0 0
86  
87 0 _debug("SNMP::Insight fetching subtree $oid\n");
88  
89 0 my $last_oid = $oid;
90  
91 0 ▶ if ( $s->version() == SNMP_VERSION_1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->version == SNMP_VERSION_1()) { } 0 0 0
92  
93 0 ▶ while ( defined $s->get_next_request( -varbindlist => [$last_oid] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->get_next_request("-varbindlist", [$last_oid])) 0 0 0
94 0 my $returned_oid = ( $s->var_bind_names() )[0];
95 0 ▶ if ( !oid_base_match( $last_oid, $returned_oid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (oid_base_match($last_oid, $returned_oid)) 0 0 0
96 0 last;
97 }
98  
99 # store into result
100 push @result,
101 0 [ $returned_oid, $s->var_bind_list()->{$returned_oid} ];
102  
103 0 $last_oid = $returned_oid;
104 }
105  
106 }
107 else {
108  
109 GET_BULK:
110 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->get_bulk_request("-maxrepetitions", 25, "-varbindlist", [$last_oid])) 0 0 0
111 defined $s->get_bulk_request(
112 -maxrepetitions => 25,
113 -varbindlist => [$last_oid]
114 )
115 )
116 {
117 0 my @oids = $s->var_bind_names();
118  
119 0 ▶ if ( !scalar @oids ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @oids) 0 0 0
120 0 die('Received an empty varBindList');
121 }
122  
123 0 foreach my $returned_oid (@oids) {
124  
125 0 ▶ if ( !oid_base_match( $oid, $returned_oid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (oid_base_match($oid, $returned_oid)) 0 0 0
126 0 last GET_BULK;
127 }
128  
129 # Make sure we have not hit the end of the MIB.
130 0 ▶ if ( $s->var_bind_types()->{$returned_oid} == ENDOFMIBVIEW ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->var_bind_types->{$returned_oid} == ENDOFMIBVIEW()) 0 0 0
131 0 last GET_BULK;
132 }
133  
134 push @result,
135 0 [ $returned_oid, $s->var_bind_list()->{$returned_oid} ];
136  
137 0 $last_oid = $returned_oid;
138 }
139 }
140  
141 }
142  
143 0 return \@result;
144 }
145  
146 1 1;
147  
148 # Local Variables:
149 # mode: cperl
150 # indent-tabs-mode: nil
151 # cperl-indent-level: 4
152 # cperl-indent-parens-as-block: t
153 # End:
154  
155 __END__
« Insight/Classifier.pm ↑ Summary Insight/Device/Cisco/Catalyst.pm »