blib/lib/SNMP/Effective/Dispatch.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 196 CC 23 SCAR 46.5
CC
23
Cov
0%
dispatch
210.0
_walk
42.0
_set
6.0
CRAP 104.7
1 package SNMP::Effective::Dispatch;
2  
3 =head1 NAME
4  
5 SNMP::Effective::Dispatch - Base class for SNMP::Effective
6  
7 =head1 DESCRIPTION
8  
9 L<SNMP::Effective> inherit from this class. The methods here are
10 separated out just for convenience.
11  
12 =head1 PACKAGE VARIABLES
13  
14 =head2 %METHOD
15  
16 This hash contains a mapping between
17  
18 $effective->add($key => [...]);
19 SNMP::Effective::Dispatch::_$key();
20 SNMP::$value();
21  
22 This means that you can add your custom SNMP method if you like.
23  
24 The C<SNMP::Effective::Dispatch::_walk()> method, is a working example on this,
25 since it's actually a series of getnext, seen from L<SNMP>'s perspective.
26  
27 Example:
28  
29 $SNMP::Effective::Dispatch::METHOD{'foo'} = 'get';
30 *SNMP::Effective::Dispatch::_foo = sub {
31 my($self, $host) = @_;
32  
33 # do stuff...
34  
35 return $self->_end($host);
36 };
37  
38 my $effective = SNMP::Effective->new(
39 foo => [$oid],
40 # ...
41 );
42  
43 # execute() will then call $effective->_foo($host) when
44 # $host answer with data
45 $effective->execute;
46  
47 =cut
48  
49 use strict;
50 use warnings;
51 use constant DEBUG => $ENV{'SNMP_EFFECTIVE_DEBUG'} ? 1 : 0;
52  
53 our %METHOD = (
54 get => 'get',
55 getnext => 'getnext',
56 walk => 'getnext',
57 set => 'set',
58 );
59  
60 =head1 METHODS
61  
62 =head2 dispatch
63  
64 This method does the actual fetching, and is called by
65 L<SNMP::Effective/execute>.
66  
67 =cut
68  
69 sub dispatch {
70 my $self = shift;
71 my $host = shift;
72 my $hostlist = $self->hostlist;
73 my($request, $req_id, $snmp_method);
74  
75 $self->_wait_for_lock;
76  
77 HOST:
78 while($self->{'_sessions'} < $self->max_sessions or $host) {
79  
80 unless($host) {
81 $host = $hostlist->shift or last HOST;
82 $host->pre_collect_callback->($host, $self);
83 }
84  
85 $request = shift @$host or next HOST;
86 $snmp_method = $METHOD{ $request->[0] };
87 $req_id = undef;
88  
89 # fetch or create snmp session
90 unless($$host) {
91 next HOST unless($$host = $self->_create_session($host));
92 $self->{'_sessions'}++;
93 }
94  
95 # ready request
96 if($$host->can($snmp_method) and $self->can("_$request->[0]")) {
97 $req_id = $$host->$snmp_method(
98 $request->[1],
99 [ "_$request->[0]", $self, $host, $request->[1] ]
100 );
101  
102 warn "\$self->_$request->[0]( ${host}->$snmp_method(...) )" if DEBUG;
103 }
104  
105 # something went wrong
106 unless($req_id) {
107 warn "Method $request->[0] failed \@ $host" if DEBUG;
108 next HOST;
109 }
110 }
111 continue {
112 if(ref $$host and !ref $request) {
113 warn "Completed $host" if DEBUG;
114 $self->{'_sessions'}--;
115 }
116 if($req_id or @$host == 0) {
117 $host->post_collect_callback->($host, $self);
118 $host = undef;
119 }
120 }
121  
122 warn sprintf "Sessions/max-sessions: %i<%i", $self->{'_sessions'}, $self->max_sessions if DEBUG;
123  
124 unless($hostlist->length or $self->{'_sessions'}) {
125 warn "SNMP::finish() is next up" if DEBUG;
126 SNMP::finish();
127 }
128  
129 $self->_unlock;
130  
131 return $hostlist->length || $self->{'_sessions'};
132 }
133  
134 sub _set {
135 my $self = shift;
136 my $host = shift;
137 my $request = shift;
138 my $response = shift;
139  
140 return $self->_end($host, 'Timeout') unless(ref $response);
141  
142 for my $r (grep { ref $_ } @$response) {
143 my $cur_oid = SNMP::Effective::make_numeric_oid($r->name);
144 $host->data($r, $cur_oid);
145 }
146  
147 return $self->_end($host);
148 }
149  
150 sub _get {
151 my $self = shift;
152 my $host = shift;
153 my $request = shift;
154 my $response = shift;
155  
156 return $self->_end($host, 'Timeout') unless(ref $response);
157  
158 for my $r (grep { ref $_ } @$response) {
159 my $cur_oid = SNMP::Effective::make_numeric_oid($r->name);
160 $host->data($r, $cur_oid);
161 }
162  
163 return $self->_end($host);
164 }
165  
166 sub _getnext {
167 my $self = shift;
168 my $host = shift;
169 my $request = shift;
170 my $response = shift;
171  
172 return $self->_end($host, 'Timeout') unless(ref $response);
173  
174 for my $r (grep { ref $_ } @$response) {
175 my $cur_oid = SNMP::Effective::make_numeric_oid($r->name);
176 $host->data($r, $cur_oid);
177 }
178  
179 return $self->_end($host);
180 }
181  
182 sub _walk {
183 my $self = shift;
184 my $host = shift;
185 my $request = shift;
186 my $response = shift;
187 my $i = 0;
188  
189 return $self->_end($host, 'Timeout') unless(ref $response);
190  
191 while($i < @$response) {
192 my $splice = 2;
193  
194 if(my $r = $response->[$i]) {
195 my($cur_oid, $ref_oid) = SNMP::Effective::make_numeric_oid($r->name, $request->[$i]->name);
196 my $oid_match = SNMP::Effective::match_oid($cur_oid, $ref_oid);
197 my $data_type = $r->[3] || 'UNDEF';
198  
199 $r->[0] = $cur_oid;
200 $splice--;
201  
202 # valid oid
203 if(defined $oid_match and $data_type ne 'NULL') {
204 $host->data($r, $ref_oid);
205 $splice--;
206 $i++;
207 }
208 }
209  
210 if($splice) {
211 splice @$request, $i, 1;
212 splice @$response, $i, 1;
213 }
214 }
215  
216 if(@$response) {
217 $$host->getnext($response, [ \&_walk, $self, $host, $request ]);
218 return;
219 }
220 else {
221 return $self->_end($host);
222 }
223 }
224  
225 sub _end {
226 my $self = shift;
227 my $host = shift;
228 my $error = shift;
229  
230 warn "Calling callback for $host..." if DEBUG;
231  
232 $host->callback->($host, $error);
233 $host->clear_data;
234  
235 return $self->dispatch($host)
236 }
237  
238 =head1 AUTHOR
239  
240 =head1 ACKNOWLEDGEMENTS
241  
242 =head1 COPYRIGHT & LICENSE
243  
244 See L<SNMP::Effective>
245  
246 =cut
247  
248 1;