SNMP.pm

filter: Statementstmt 48.3% 103 / 213 Branchbran 35.4% 34 / 96 Conditioncond 33.3% 12 / 36 MC/DCmcdc 9.5% 2 / 21 Subroutinesub 51.3% 19 / 37 Podpod 100% 9 / 9 totaltotal 43.4% 179 / 412 CC 69 SCAR 31.8
CC
69
Cov
43%
_prepare_request
79.8
__ANON__
56.0
__ANON__
42.0
CRAP 24.1
« SNMP/Dispatcher.pm ↑ Summary
1 package Mojo::SNMP;
2 12 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Mojo::SNMP::Dispatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Net::SNMP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use constant DEBUG => $ENV{MOJO_SNMP_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'MOJO_SNMP_DEBUG'} ? : 0 12 12
8 12 ▶use constant MAXREPETITIONS => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 our $VERSION = '0.13';
11  
12 12 my $DISPATCHER;
13 12 my @EXCLUDE_METHOD_ARGS = qw(maxrepetitions);
14 12 my %EXCLUDE = (
15 v1 => [qw(username authkey authpassword authprotocol privkey privpassword privprotocol)],
16 v2c => [qw(username authkey authpassword authprotocol privkey privpassword privprotocol)],
17 v3 => [qw(community)],
18 );
19  
20 12 my %SNMP_METHOD;
21 12 __PACKAGE__->add_custom_request_method(bulk_walk => \&_snmp_method_bulk_walk);
22 12 __PACKAGE__->add_custom_request_method(walk => \&_snmp_method_walk);
23  
24 12 $Net::SNMP::DISPATCHER = $Net::SNMP::DISPATCHER; # avoid warning
25  
26 12 has concurrent => 20;
27 2 ▶has defaults => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 12 has master_timeout => 0;
29 6 ▶has ioloop => sub { Mojo::IOLoop->singleton };
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 # these attributes are experimental and therefore not exposed. Let me know if
32 # you use them...
33 5 ▶has _dispatcher => sub { $DISPATCHER ||= Mojo::SNMP::Dispatcher->new(ioloop => shift->ioloop) };
Subroutine __ANON__: called CC=2 SCAR=8.1
Condition33%1/3
$DISPATCHER ||= "Mojo::SNMP::Dispatcher"->new("ioloop", (shift())->ioloop)
l!l&&r!l&&!r
050
MC/DC0%0/2
$DISPATCHER ||= "Mojo::SNMP::Dispatcher"->new("ioloop", (shift())->ioloop)
$DISPATCHER "Mojo::SNMP::Dispatcher"->new("ioloop", (shift())->ioloop)
Truth table33%1/3
A ||= B
A= $DISPATCHERB= "Mojo::SNMP::Dispatcher"->new("ioloop", (shift())->ioloop)
ABresult
1X1
011
000
34  
35 24 ▶sub add_custom_request_method {
Subroutine add_custom_request_method: called CC=1 SCAR=0.0
Pod documented
36 24 my ($class, $name, $cb) = @_;
37 24 $SNMP_METHOD{$name} = $cb;
38 24 $class;
39 }
40  
41 15 ▶sub prepare {
Subroutine prepare: called CC=22 SCAR=31.4
Pod documented
42 15 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef; # internal usage. might change
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 2 13 15
43 15 my $self = shift;
44 15 ▶ my $hosts = ref $_[0] eq 'ARRAY' ? shift : [shift];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 15 15
45 15 ▶ my $args = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 10 5 15
46 15 my %args = %$args;
47  
48 15 ▶ $hosts = [keys %{$self->{sessions} || {}}] if $hosts->[0] and $hosts->[0] eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $hosts->[0] and $hosts->[0] eq "*" 5 10 15
Condition60%3/5
$hosts->[0] and $hosts->[0] eq "*"
!ll&&!rl&&r
0105
$self->{'sessions'} || {}
l!l
50
MC/DC50%1/2
$hosts->[0] and $hosts->[0] eq "*"
$hosts->[0] $hosts->[0] eq "*"
MC/DC0%0/1
$self->{'sessions'} || {}
$self->{'sessions'}
Truth table66%2/3
A and B
A= $hosts->[0]B= $hosts->[0] eq "*"
ABresult
0X0
100
111
Truth table50%1/2
A
A= $self->{'sessions'}
Aresult
11
00
49  
50 15 ▶ defined $args{$_} or $args{$_} = $self->defaults->{$_} for keys %{$self->defaults};
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} or $args{$_} = $self->defaults->{$_} 2 37 39
51 15 ▶ $args{version} = $self->_normalize_version($args{version} || '');
Condition100%2/2
$args{'version'} || ''
l!l
87
MC/DC100%1/1
$args{'version'} || ''
$args{'version'}
Truth table100%2/2
A
A= $args{'version'}
Aresult
11
00
52 15 delete $args{$_} for @{$EXCLUDE{$args{version}}}, @EXCLUDE_METHOD_ARGS;
53 15 delete $args{stash};
54  
55 HOST:
56 15 for my $key (@$hosts) {
57 22 my ($host) = $key =~ /^([^|]+)/;
58 22 local $args{hostname} = $host;
59 22 ▶ my $key = $key eq $host ? $self->_calculate_pool_key(\%args) : $key;
Branch: 1 branch
BranchTrueFalseTotal
$key eq $host ? : 10 12 22
60 22 ▶ $self->{sessions}{$key} ||= $self->_new_session(\%args) or next HOST;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sessions"}{$key} ||= $self->_new_session(\%args) or next HOST 22 0 22
Condition66%2/3
$self->{"sessions"}{$key} ||= $self->_new_session(\%args)
l!l&&r!l&&!r
12100
MC/DC0%0/2
$self->{"sessions"}{$key} ||= $self->_new_session(\%args)
$self->{"sessions"}{$key} $self->_new_session(\%args)
Truth table66%2/3
A ||= B
A= $self->{"sessions"}{$key}B= $self->_new_session(\%args)
ABresult
1X1
011
000
61  
62 22 local @_ = @_;
63 22 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 22 37
64 15 my $method = shift;
65 15 ▶ my $oid = ref $_[0] eq 'ARRAY' ? shift : [shift];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 10 5 15
66 15 push @{$self->{queue}{$key}}, [$key, $method, $oid, $args, $cb];
67 }
68 }
69  
70 15 ▶ $self->{n_requests} ||= 0;
Condition50%1/2
$self->{"n_requests"} ||= 0
l!l
015
MC/DC0%0/1
$self->{"n_requests"} ||= 0
$self->{"n_requests"}
Truth table50%1/2
A
A= $self->{"n_requests"}
Aresult
11
00
71  
72 15 for ($self->{n_requests} .. $self->concurrent - 1) {
73 4 ▶ my $queue = $self->_dequeue or last;
Branch: 1 branch
BranchTrueFalseTotal
my $queue = $self->_dequeue or last 4 0 4
74 4 $self->_prepare_request($queue);
75 }
76  
77 11 ▶ $self->_setup if !$self->{_setup}++ and $self->ioloop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"_setup"}++ and $self->ioloop->is_running 0 11 11
Condition66%2/3
not $self->{"_setup"}++ and $self->ioloop->is_running
!ll&&!rl&&r
650
MC/DC0%0/2
not $self->{"_setup"}++ and $self->ioloop->is_running
not $self->{"_setup"}++ $self->ioloop->is_running
Truth table66%2/3
not(A) and B
A= not $self->{"_setup"}++B= $self->ioloop->is_running
ABresult
0X0
100
111
78 11 $self;
79 }
80  
81 0 ▶sub wait {
Subroutine wait: not called CC=2 SCAR=17.9
Pod documented
82 0 my $self = shift;
83 0 my $ioloop = $self->ioloop;
84 0 my $stop;
85  
86 0 ▶ $stop = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 $_[0]->unsubscribe(finish => $stop);
88 0 $_[0]->unsubscribe(timeout => $stop);
89 0 $ioloop->stop;
90 0 undef $stop;
91 0 };
92  
93 0 ▶ $self->_setup unless $self->{_setup}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_setup"}++ 0 0 0
94 0 $self->once(finish => $stop);
95 0 $self->once(timeout => $stop);
96 0 $ioloop->start;
97 0 $self;
98 }
99  
100 12 for my $method (qw(get get_bulk get_next set walk bulk_walk)) {
101 0 ▶ eval <<"HERE" or die $@;
Subroutine bulk_walk: not called CC=2 SCAR=12.1
Subroutine get: called CC=2 SCAR=12.1
Subroutine get_bulk: not called CC=2 SCAR=12.1
Subroutine get_next: called CC=2 SCAR=12.1
Subroutine set: not called CC=2 SCAR=12.1
Subroutine walk: not called CC=2 SCAR=12.1
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 7 branches
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
ref $_[0] eq 'HASH' ? : 1 0 1
ref $_[0] eq 'HASH' ? : 0 0 0
ref $_[0] eq 'HASH' ? : 0 1 1
ref $_[0] eq 'HASH' ? : 0 0 0
ref $_[0] eq 'HASH' ? : 0 0 0
eval " sub $method {\n my (\$self, \$host) = (shift, shift);\n my \$args = ref \$_[0] eq 'HASH' ? shift : {};\n \$self->prepare(\$host, \$args, $method => \@_);\n }\n 1;\n" or die $@ 72 0 72
102 sub $method {
103 my (\$self, \$host) = (shift, shift);
104 my \$args = ref \$_[0] eq 'HASH' ? shift : {};
105 \$self->prepare(\$host, \$args, $method => \@_);
106 }
107 1;
108 HERE
109 }
110  
111 10 ▶sub _calculate_pool_key {
Subroutine _calculate_pool_key: called CC=2 SCAR=6.9
112 10 ▶ join '|', map { defined $_[1]->{$_} ? $_[1]->{$_} : '' } qw(hostname version community username);
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1]{$_} ? : 28 12 40
113 }
114  
115 5 ▶sub _dequeue {
Subroutine _dequeue: called CC=3 SCAR=11.4
116 5 my $self = shift;
117 5 ▶ my $key = (keys %{$self->{queue} || {}})[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $key = (keys %{$self->{"queue"} or {};})[0] or return 5 0 5
Condition50%1/2
$self->{'queue'} || {}
l!l
50
MC/DC0%0/1
$self->{'queue'} || {}
$self->{'queue'}
Truth table50%1/2
A
A= $self->{'queue'}
Aresult
11
00
118 5 return delete $self->{queue}{$key};
119 }
120  
121 0 ▶sub _finish {
Subroutine _finish: not called CC=1 SCAR=6.9
122 0 warn "[Mojo::SNMP] Finish\n" if DEBUG;
123 0 $_[0]->emit('finish');
124 0 $_[0]->{_setup} = 0;
125 }
126  
127 10 ▶sub _new_session {
Subroutine _new_session: called CC=2 SCAR=7.2
128 10 my ($self, $args) = @_;
129 10 my ($session, $error) = Net::SNMP->new(%$args, nonblocking => 1);
130  
131 10 warn "[Mojo::SNMP] New session $args->{hostname}: ", ($error || 'OK'), "\n" if DEBUG;
132 0 ▶ Mojo::IOLoop->next_tick(sub { $self->emit(error => "$args->{hostname}: $error") }) if $error;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 10 10
133 10 $session;
134 }
135  
136 15 ▶sub _normalize_version {
Subroutine _normalize_version: called CC=3 SCAR=11.0
137 15 ▶ $_[1] =~ /1/ ? 'v1' : $_[1] =~ /3/ ? 'v3' : 'v2c';
Branch: 2 branches
BranchTrueFalseTotal
$_[1] =~ /1/u ? : 1 14 15
$_[1] =~ /3/u ? : 1 13 14
138 }
139  
140 5 ▶sub _prepare_request {
Subroutine _prepare_request: called CC=12 SCAR=43.8
141 5 my ($self, $queue) = @_;
142 5 my $item = shift @$queue;
143  
144 5 ▶ unless ($item) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item) 1 4 5
145 1 ▶ $queue = $self->_dequeue or return;
Branch: 1 branch
BranchTrueFalseTotal
$queue = $self->_dequeue or return 1 0 1
146 1 $item = shift @$queue;
147 }
148  
149 5 my ($key, $method, $list, $args, $cb) = @$item;
150 5 my $session = $self->{sessions}{$key};
151 5 my ($error, $success);
152  
153 # Dispatch to our mojo based dispatcher
154 5 $Net::SNMP::DISPATCHER = $self->_dispatcher;
155  
156 5 ▶ unless ($session->transport) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session->transport) 5 0 5
157 5 warn "[Mojo::SNMP] <<< open connection\n" if DEBUG;
158 5 ▶ unless ($session->open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session->open) 0 0 0
159 Mojo::IOLoop->next_tick(
160 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
161 0 ▶ return $self->$cb($session->error, undef) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
162 0 return $self->emit(error => $session->error, $session, $args);
163 },
164 0 );
165 0 ▶ return $self->{n_requests} || '0e0';
Condition0%0/2
$self->{'n_requests'} || '0e0'
l!l
00
MC/DC0%0/1
$self->{'n_requests'} || '0e0'
$self->{'n_requests'}
Truth table0%0/2
A
A= $self->{'n_requests'}
Aresult
11
00
166 }
167 }
168  
169 0 warn "[Mojo::SNMP] <<< $method $key @$list\n" if DEBUG;
170 0 Scalar::Util::weaken($self);
171 0 ▶ $method = $SNMP_METHOD{$method} || "$method\_request";
Condition0%0/2
$SNMP_METHOD{$method} || "${method}_request"
l!l
00
MC/DC0%0/1
$SNMP_METHOD{$method} || "${method}_request"
$SNMP_METHOD{$method}
Truth table0%0/2
A
A= $SNMP_METHOD{$method}
Aresult
11
00
172 $success = $session->$method(
173 $method =~ /bulk/ ? (maxrepetitions => $args->{maxrepetitions} || MAXREPETITIONS) : (),
174 ref $method ? (%$args) : (),
175 varbindlist => $list,
176 0 ▶ callback => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
177 0 my $session = shift;
178  
179 eval {
180 0 local @$args{qw(method request)} = @$item[1, 2];
181 0 $self->{n_requests}--;
182 0 ▶ if ($session->var_bind_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->var_bind_list) { } 0 0 0
183 0 warn "[Mojo::SNMP] >>> success: $method $key @$list\n" if DEBUG;
184 0 ▶ $cb ? $self->$cb('', $session) : $self->emit(response => $session, $args);
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
185 }
186 else {
187 0 warn "[Mojo::SNMP] >>> error: $method $key @{[$session->error]}\n" if DEBUG;
188 0 ▶ $cb ? $self->$cb($session->error, $session) : $self->emit(error => $session->error, $session, $args);
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
189 }
190 0 1;
191 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local(@$args{"method", "request"}) = @$item[1, 2]; --$self->{"n_requests"}; if ($session->var_bind_list) { '???'; $cb ? $self->$cb("", $session) : $self->emit("response", $session, $args); } else { '???'; $cb ? $self->$cb($session->error, $session) : $self->emit("error", $session->error, $session, $args); } ; 1 } } or do { $self->emit("error", $@) } 0 0 0
192 0 $self->emit(error => $@);
193 };
194  
195 0 warn "[Mojo::SNMP] n_requests: $self->{n_requests}\n" if DEBUG;
196 0 $self->_prepare_request($queue);
197 0 warn "[Mojo::SNMP] n_requests: $self->{n_requests}\n" if DEBUG;
198 0 ▶ $self->_finish unless $self->{n_requests};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"n_requests"} 0 0 0
199 },
200 0 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$method =~ /bulk/u ? : 0 0 0
ref $method ? : 0 0 0
Condition0%0/2
$args->{"maxrepetitions"} or 10
l!l
00
MC/DC0%0/1
$args->{"maxrepetitions"} or 10
$args->{"maxrepetitions"}
Truth table0%0/2
A
A= $args->{"maxrepetitions"}
Aresult
11
00
201  
202 0 ▶ return ++$self->{n_requests} if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 0 0 0
203 0 $self->emit(error => $session->error, $session);
204 0 ▶ return $self->{n_requests} || '0e0';
Condition0%0/2
$self->{'n_requests'} || '0e0'
l!l
00
MC/DC0%0/1
$self->{'n_requests'} || '0e0'
$self->{'n_requests'}
Truth table0%0/2
A
A= $self->{'n_requests'}
Aresult
11
00
205 }
206  
207 0 ▶sub _setup {
Subroutine _setup: not called CC=2 SCAR=17.9
208 0 my $self = shift;
209 0 ▶ my $timeout = $self->master_timeout or return;
Branch: 1 branch
BranchTrueFalseTotal
my $timeout = $self->master_timeout or return 0 0 0
210 0 my $tid;
211  
212 0 warn "[Mojo::SNMP] Timeout: $timeout\n" if DEBUG;
213 0 Scalar::Util::weaken($self);
214  
215 $tid = $self->ioloop->timer(
216 0 ▶ $timeout => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 warn "[Mojo::SNMP] Timeout\n" if DEBUG;
218 0 $self->ioloop->remove($tid);
219 0 $self->emit('timeout');
220 0 $self->{_setup} = 0;
221 }
222 0 );
223 }
224  
225 0 ▶sub _snmp_method_bulk_walk {
Subroutine _snmp_method_bulk_walk: not called CC=2 SCAR=17.9
226 0 my ($session, %args) = @_;
227 0 my $base_oid = $args{varbindlist}[0];
228 0 my $last = $args{callback};
229 0 ▶ my $maxrepetitions = $args{maxrepetitions} || MAXREPETITIONS;
Condition0%0/2
$args{'maxrepetitions'} || 10
l!l
00
MC/DC0%0/1
$args{'maxrepetitions'} || 10
$args{'maxrepetitions'}
Truth table0%0/2
A
A= $args{'maxrepetitions'}
Aresult
11
00
230 0 my ($callback, $end, %tree, %types);
231  
232 0 ▶ $end = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
233 0 ▶ if (scalar keys %tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %tree) { } 0 0 0
234 0 $session->pdu->var_bind_list(\%tree, \%types);
235 }
236 else {
237 0 $session->pdu->var_bind_list({$base_oid => 'noSuchObject'}, {$base_oid => Net::SNMP::NOSUCHOBJECT});
238 }
239 0 $session->$last;
240 0 $end = $callback = undef;
241 0 };
242  
243 0 ▶ $callback = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
244 0 my ($session) = @_;
245 0 ▶ my $res = $session->var_bind_list or return $end->();
Branch: 1 branch
BranchTrueFalseTotal
my $res = $session->var_bind_list or return &$end() 0 0 0
246 0 ▶ my @sortres = $session->var_bind_names() or return $end->();
Branch: 1 branch
BranchTrueFalseTotal
my(@sortres) = $session->var_bind_names or return &$end() 0 0 0
247 0 my $types = $session->var_bind_types;
248 0 my $next = $sortres[-1];
249  
250 0 for my $oid (@sortres) {
251 0 ▶ return $end->() if $types{$oid} or !Net::SNMP::oid_base_match($base_oid, $oid);
Branch: 1 branch
BranchTrueFalseTotal
if $types{$oid} or not Net::SNMP::oid_base_match($base_oid, $oid) 0 0 0
Condition0%0/3
$types{$oid} or not Net::SNMP::oid_base_match($base_oid, $oid)
l!l&&r!l&&!r
000
MC/DC0%0/2
$types{$oid} or not Net::SNMP::oid_base_match($base_oid, $oid)
$types{$oid} not Net::SNMP::oid_base_match($base_oid, $oid)
Truth table0%0/3
A or not(B)
A= $types{$oid}B= not Net::SNMP::oid_base_match($base_oid, $oid)
ABresult
1X1
011
000
252 0 $types{$oid} = $types->{$oid};
253 0 $tree{$oid} = $res->{$oid};
254 }
255  
256 0 ▶ return $end->() unless $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 0 0 0
257 0 return $session->get_bulk_request(maxrepetitions => $maxrepetitions, varbindlist => [$next], callback => $callback);
258 0 };
259  
260 0 $session->get_bulk_request(maxrepetitions => $maxrepetitions, varbindlist => [$base_oid], callback => $callback);
261 }
262  
263 0 ▶sub _snmp_method_walk {
Subroutine _snmp_method_walk: not called CC=1 SCAR=6.9
264 0 my ($session, %args) = @_;
265 0 my $base_oid = $args{varbindlist}[0];
266 0 my $last = $args{callback};
267 0 my ($callback, $end, %tree, %types);
268  
269 0 ▶ $end = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
270 0 ▶ $session->pdu->var_bind_list(\%tree, \%types) if %tree;
Branch: 1 branch
BranchTrueFalseTotal
if %tree 0 0 0
271 0 $session->$last;
272 0 $end = $callback = undef;
273 0 };
274  
275 0 ▶ $callback = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
276 0 my ($session) = @_;
277 0 ▶ my $res = $session->var_bind_list or return $end->();
Branch: 1 branch
BranchTrueFalseTotal
my $res = $session->var_bind_list or return &$end() 0 0 0
278 0 my $types = $session->var_bind_types;
279 0 my @next;
280  
281 0 for my $oid (keys %$res) {
282 0 ▶ if (!$types{$oid} and Net::SNMP::oid_base_match($base_oid, $oid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $types{$oid} and Net::SNMP::oid_base_match($base_oid, $oid)) 0 0 0
Condition0%0/3
not $types{$oid} and Net::SNMP::oid_base_match($base_oid, $oid)
!ll&&!rl&&r
000
MC/DC0%0/2
not $types{$oid} and Net::SNMP::oid_base_match($base_oid, $oid)
not $types{$oid} Net::SNMP::oid_base_match($base_oid, $oid)
Truth table0%0/3
not(A) and B
A= not $types{$oid}B= Net::SNMP::oid_base_match($base_oid, $oid)
ABresult
0X0
100
111
283 0 $types{$oid} = $types->{$oid};
284 0 $tree{$oid} = $res->{$oid};
285 0 push @next, $oid;
286 }
287 }
288  
289 0 ▶ return $end->() unless @next;
Branch: 1 branch
BranchTrueFalseTotal
unless @next 0 0 0
290 0 return $session->get_next_request(varbindlist => \@next, callback => $callback);
291 0 };
292  
293 0 $session->get_next_request(varbindlist => [$base_oid], callback => $callback);
294 }
295  
296 12 1;
297  
298 =encoding utf8
299  
300 =head1 NAME
301  
302 Mojo::SNMP - Run SNMP requests with Mojo::IOLoop
303  
304 =head1 VERSION
305  
306 0.13
307  
308 =head1 SYNOPSIS
309  
310 =head2 Using methods with callbacks
311  
312 use Mojo::SNMP;
313 my $snmp = Mojo::SNMP->new;
314  
315 $snmp->defaults({timeout => 3, community => "public"});
316  
317 $snmp->get_next(["10.0.0.1", "10.0.0.2"], ["1.3.6.1.2.1.1.3.0"], sub {
318 my ($self, $err, $session) = @_;
319 });
320  
321 $snmp->wait unless $snmp->ioloop->is_running;
322  
323 =head2 Using prepare
324  
325 use Mojo::SNMP;
326 my $snmp = Mojo::SNMP->new;
327 my @response;
328  
329 $snmp->on(response => sub {
330 my ($snmp, $session, $args) = @_;
331 warn "Got response from $args->{hostname} on $args->{method}(@{$args->{request}})...\n";
332 push @response, $session->var_bind_list;
333 });
334  
335 $snmp->defaults({
336 community => "public", # v1, v2c
337 username => "foo", # v3
338 version => "v2c", # v1, v2c or v3
339 });
340  
341 $snmp->prepare("127.0.0.1", get_next => ["1.3.6.1.2.1.1.3.0"]);
342 $snmp->prepare("localhost", {version => "v3"}, get => ["1.3.6.1.2.1.1.3.0"]);
343  
344 # start the IOLoop unless it is already running
345 $snmp->wait unless $snmp->ioloop->is_running;
346  
347 =head1 DESCRIPTION
348  
349 L<Mojo::SNMP> is an async library for fetching or writing data from/to many
350 SNMP agents. The module does its best to not get in your way, but rather
351 provide a simple API which allow you to extract information from multiple
352 servers at the same time.
353  
354 This module uses L<Net::SNMP> and L<Mojo::IOLoop> to fetch data from hosts
355 asynchronously. It does this by using a custom dispatcher,
356 L<Mojo::SNMP::Dispatcher>, which attach the sockets created by L<Net::SNMP>
357 directly into the ioloop reactor.
358  
359 If you want greater speed, you should check out L<Net::SNMP::XS> and make sure
360 L<Mojo::Reactor::EV> and L<EV> is installed.
361  
362 L<Mojo::SNMP> is supposed to be a replacement for a module I wrote earlier,
363 called L<SNMP::Effective>. Reason for the rewrite is that I'm using the
364 framework L<Mojolicious> which includes an awesome IO loop which allow me to
365 do cool stuff inside my web server.
366  
367 =head1 EVENTS
368  
369 =head2 error
370  
371 $self->on(error => sub { my ($self, $str, $session, $args) = @_; ... });
372  
373 Emitted on errors which may occur. C<$session> is a L<Net::SNMP> object and is
374 only available if the error is a result of a L<Net::SNMP> method, such as
375 L<get_request()|Net::SNMP/get_request>.
376  
377 See L</response> for C<$args> description.
378  
379 =head2 finish
380  
381 $self->on(finish => sub { my $self = shift; ... });
382  
383 Emitted when all requests have completed.
384  
385 =head2 response
386  
387 $self->on(response => sub { my ($self, $session, $args) = @_; ... });
388  
389 Called each time a host responds. The C<$session> is the current L<Net::SNMP>
390 object. C<$args> is a hash ref with the arguments given to L</prepare>, with
391 some additional information:
392  
393 {
394 method => $str, # get, get_next, ...
395 request => [$oid, ...],
396 ...
397 }
398  
399 =head2 timeout
400  
401 $self->on(timeout => sub { my $self = shift; ... })
402  
403 Emitted if L</wait> has been running for more than L</master_timeout> seconds.
404  
405 =head1 ATTRIBUTES
406  
407 =head2 concurrent
408  
409 $self = $self->concurrent(20);
410 $int = $self->concurrent;
411  
412 How many hosts to fetch data from at once. Default is 20. (The default may
413 change in later versions)
414  
415 =head2 defaults
416  
417 $self = $self->defaults({community => "public"});
418 $hash_ref = $self->community;
419  
420 This attribute holds a hash ref with default arguments which will be passed
421 on to L<Net::SNMP/session>. User-submitted C<%args> will be merged with the
422 defaults before being submitted to L</prepare>. C<prepare()> will filter out
423 and ignore arguments that don't work for the SNMP C<version>.
424  
425 NOTE: SNMP version will default to "v2c".
426  
427 =head2 master_timeout
428  
429 $self = $self->master_timeout(15);
430 $int = $self->master_timeout;
431  
432 How long to run in total before timeout. Note: This is NOT per host but for
433 the complete run. Default is 0, which means that it will never time out.
434  
435 If you want to set a timeout per request request to a host, then this need
436 to be set in L</defaults> or in C<$args> passed on to L</prepare> or one of
437 the other request methods.
438  
439 =head2 ioloop
440  
441 $self = $self->ioloop(Mojo::IOLoop->new);
442 $ioloop = $self->ioloop;
443  
444 Holds an instance of L<Mojo::IOLoop>.
445  
446 =head1 METHODS
447  
448 =head2 add_custom_request_method
449  
450 Mojo::SNMP->add_custom_request_method(my_custom_method => sub {
451 my ($session, %args) = @_;
452 # do custom stuff..
453 });
454  
455 L<Net::SNMP> has defined basic methods to write/retrieve data from/to the SNMP
456 agent. L</add_custom_request_method> allow you to add support for custom
457 methods, which can be useful if you find yourself doing the same complicated
458 logic over and over again. L</bulk_walk> and L</walk> are custom methods
459 bundled with this module.
460  
461 NOTE: This method will define the methods in a global scope, meaning the code
462 below will call the custom callback instead of L<Net::SNMP/get_next_request>
463 for all instances of L<Mojo::SNMP>:
464  
465 $self->add_custom_request_method(get_next => sub { ... });
466  
467 =head2 bulk_walk
468  
469 $self->bulk_walk($host, $args, \@oids, sub {
470 my ($self, $err, $session) = @_;
471 return warn $err if $err;
472 push @{$res{$host}}, $session->var_bind_list;
473 });
474  
475 This is a custom SNMP method added by L</add_custom_request_method>. See
476 L</prepare> for generic information about the variables associated with this
477 method.
478  
479 This method will run L<Net::SNMP/get_bulk_request> until it receives an OID
480 which does not match the base OID. C<maxrepetitions> in C<$args> will default
481 to 10, but could be overrided to potentially increase performance. Example:
482  
483 $self->bulk_walk("192.168.0.1" => {maxrepetitions => 25}, sub {
484 my ($self, $err, $session) = @_;
485 return warn $err if $err;
486 push @{$res{$host}}, $session->var_bind_list;
487 });
488  
489 =head2 get
490  
491 $self->get($host, \%args, \@oids, sub {
492 my ($self, $err, $session) = @_;
493 return warn $err if $err;
494 push @{$res{$host}}, $session->var_bind_list;
495 });
496  
497 Will send a SNMP get-request to the remote agent. See L<Net::SNMP/get_request>
498 for details on which C<%args> you can pass on. See L</prepare> for generic
499 information about the variables associated with this method.
500  
501 =head2 get_bulk
502  
503 $self->get_bulk($host, \%args, \@oids, sub {
504 my ($self, $err, $session) = @_;
505 return warn $err if $err;
506 push @{$res{$host}}, $session->var_bind_list;
507 });
508  
509 Will send a SNMP get-bulk-request to the remote agent. See
510 L<Net::SNMP/get_bulk_request> for details on which C<%args> you can pass on.
511 See L</prepare> for generic information about the variables associated with
512 this method.
513  
514 =head2 get_next
515  
516 $self->get_next($host, \%args, \@oids, sub {
517 my ($self, $err, $session) = @_;
518 return warn $err if $err;
519 push @{$res{$host}}, $session->var_bind_list;
520 });
521  
522 Will send a SNMP get-next-request to the remote agent. See
523 L<Net::SNMP/get_next_request> for details on which C<$args> you can pass on.
524 See L</prepare> for generic information about the variables associated with
525 this method.
526  
527 =head2 prepare
528  
529 $self = $self->prepare($host, \%args, ...);
530 $self = $self->prepare(\@hosts, \%args, ...);
531 $self = $self->prepare(\@hosts, ...);
532 $self = $self->prepare("*" => ...);
533  
534 =over 4
535  
536 =item * $host
537  
538 This can either be an array ref or a single host. The "host" can be whatever
539 L<Net::SNMP/session> can handle; generally a hostname or IP address.
540  
541 =item * \%args
542  
543 A hash ref of options which will be passed directly to L<Net::SNMP/session>.
544 This argument is optional. See also L</defaults>.
545  
546 =item * dot-dot-dot
547  
548 A list of key-value pairs of SNMP operations and bindlists which will be given
549 to L</prepare>. The operations are the same as the method names available in
550 L<Net::SNMP>, but without "_request" at end:
551  
552 get
553 get_next
554 set
555 get_bulk
556 inform
557 walk
558 bulk_walk
559 ...
560  
561 The special hostname "*" will apply the given operation to all previously
562 defined hosts.
563  
564 =back
565  
566 Examples:
567  
568 $self->prepare("192.168.0.1" => {version => "v2c"}, get_next => [$oid, ...]);
569 $self->prepare("192.168.0.1" => {version => "v3"}, get => [$oid, ...]);
570 $self->prepare(localhost => set => [$oid => OCTET_STRING, $value, ...]);
571 $self->prepare("*" => get => [$oid, ...]);
572  
573 Note: To get the C<OCTET_STRING> constant and friends you need to do:
574  
575 use Net::SNMP ":asn1";
576  
577 =head2 set
578  
579 use Net::SNMP ":asn1"; # Export OCTET_STRING
580  
581 $self->set($host, $args => [$oid, OCTET_STRING, $value, ...], sub {
582 my ($self, $err, $session) = @_;
583 return warn $err if $err;
584 push @{$res{$host}}, $session->var_bind_list;
585 });
586  
587 Will send a SNMP set-request to the remote agent. See L<Net::SNMP/set_request>
588 for details on which C<$args> you can pass on. See L</prepare> for generic
589 information about the variables associated with this method.
590  
591 =head2 walk
592  
593 $self->walk($host, $args, \@oids, sub {
594 my ($self, $err, $session) = @_;
595 return warn $err if $err;
596 push @{$res{$host}}, $session->var_bind_list;
597 });
598  
599 This is a custom SNMP method added by L</add_custom_request_method>. See
600 L</prepare> for generic information about the variables associated with this
601 method.
602  
603 This method will run L<Net::SNMP/get_next_request> until an oid retrieved does
604 not match the base OID, or if the tree is exhausted. You might want to use
605 L</bulk_walk> instead for better performance.
606  
607 =head2 wait
608  
609 $self->wait;
610  
611 This is useful if you want to block your code: C<wait()> starts the ioloop and
612 runs until L</master_timeout> or L</finish> is reached.
613  
614 my $snmp = Mojo::SNMP->new;
615 $snmp->prepare(...)->wait; # blocks while retrieving data
616 # ... your program continues after the SNMP operations have finished.
617  
618 =head1 AUTHOR
619  
620 Jan Henning Thorsen - C<jhthorsen@cpan.org>
621  
622 =head1 CONTRIBUTORS
623  
624 Espen Tallaksen - C<espen.tallaksen@telenor.com>
625  
626 Joshua Keroes - C<joshua.keroes@integratelecom.com>
627  
628 Oliver Gorwits - C<oliver@cpan.org>
629  
630 Per Carlson - C<per.carlson@broadnet.no>
631  
632 =head1 COPYRIGHT & LICENSE
633  
634 Copyright (C) 2012-2018, L</AUTHOR> and L</CONTRIBUTORS>.
635  
636 This library is free software. You can redistribute it and/or modify
637 it under the same terms as Perl itself.
638  
639 =cut
« SNMP/Dispatcher.pm ↑ Summary