ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-SNMP/SNMP.pm
(Generate patch)

Comparing cvsroot/AnyEvent-SNMP/SNMP.pm (file contents):
Revision 1.2 by root, Fri Apr 10 06:50:16 2009 UTC vs.
Revision 1.9 by root, Wed Jan 6 10:43:20 2010 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::SNMP - adaptor to integrate Net::SNMP into Anyevent. 3AnyEvent::SNMP - adaptor to integrate Net::SNMP into AnyEvent.
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent::SNMP; 7 use AnyEvent::SNMP;
8 use Net::SNMP; 8 use Net::SNMP;
43This module does not export anything and does not require you to do 43This module does not export anything and does not require you to do
44anything special apart from loading it I<before doing any non-blocking 44anything special apart from loading it I<before doing any non-blocking
45requests with Net::SNMP>. It is recommended but not required to load this 45requests with Net::SNMP>. It is recommended but not required to load this
46module before C<Net::SNMP>. 46module before C<Net::SNMP>.
47 47
48=head1 GLOBAL VARIABLES
49
50=over 4
51
52=item $AnyEvent::SNMP::MAX_OUTSTANDING (default: C<50>, dynamic)
53
54=item AnyEvent::SNMP::set_max_outstanding $new_value
55
56Use this package variable to restrict the number of outstanding SNMP
57requests at any point in time.
58
59Net::SNMP is very fast at creating and sending SNMP requests, but much
60slower at parsing (big, bulk) responses. This makes it easy to request a
61lot of data that can take many seconds to parse.
62
63In the best case, this can lead to unnecessary delays (and even time-outs,
64as the data has been received but not yet processed) and in the worst
65case, this can lead to packet loss, when the receive queue overflows and
66the kernel can no longer accept new packets.
67
68To avoid this, you can (and should) limit the number of outstanding
69requests to a number low enough so that parsing time doesn't introduce
70noticable delays.
71
72Unfortunately, this number depends not only on processing speed and load
73of the machine running Net::SNMP, but also on the network latency and the
74speed of your SNMP agents.
75
76AnyEvent::SNMP tries to dynamically adjust this number dynamically upwards
77and downwards.
78
79Increasing C<$MAX_OUTSTANDING> will not automatically use the
80extra request slots. To increase C<$MAX_OUTSTANDING> and make
81C<AnyEvent::SNMP> make use of the extra paralellity, call
82C<AnyEvent::SNMP::set_max_outstanding> with the new value, e.g.:
83
84 AnyEvent::SNMP::set_max_outstanding 500;
85
86Although due to the dynamic adjustment, this might have little lasting
87effect.
88
89Note that you can use L<Net::SNMP::XS> to speed up parsing of responses
90considerably.
91
92=item $AnyEvent::SNMP::MIN_RECVQUEUE (default: C<8>)
93
94=item $AnyEvent::SNMP::MAX_RECVQUEUE (default: C<64>)
95
96These values specify the minimum and maximum receive queue length (in
97units of one response packet).
98
99When AnyEvent::SNMP handles $MAX_RECVQUEUE or more packets per iteration
100it will reduce $MAX_OUTSTANDING. If it handles less than $MIN_RECVQUEUE,
101it increases $MAX_OUTSTANDING.
102
103This has the result of adjusting the number of outstanding requests so that
104the recv queue is between the minimum and maximu, usually.
105
106This algorithm works reasonably well as long as the responses, response
107latencies and processing times are the same size per packet on average.
108
109=back
110
111=head1 COMPATIBILITY
112
113This module may be used as a drop in replacement for the
114Net::SNMP::Dispatcher in existing programs. You can still call
115C<snmp_dispatcher> to start the event-loop, but then you loose the benefit
116of mixing Net::SNMP events with other events.
117
118 use AnyEvent::SNMP;
119 use Net::SNMP;
120
121 # just use Net::SNMP as before
122
123 # ... start non-blocking snmp request(s)...
124 Net::SNMP->session (
125 -hostname => "127.0.0.1",
126 -community => "public",
127 -nonblocking => 1,
128 )->get_request (-callback => sub { ... });
129
130 snmp_dispatcher;
131
48=cut 132=cut
49 133
50package AnyEvent::SNMP; 134package AnyEvent::SNMP;
51 135
52no warnings; 136no warnings;
61} 145}
62 146
63use Net::SNMP (); 147use Net::SNMP ();
64use AnyEvent (); 148use AnyEvent ();
65 149
66our $VERSION = '0.11'; 150our $VERSION = '1.0';
67 151
68$Net::SNMP::DISPATCHER = instance Net::SNMP::Dispatcher; 152$Net::SNMP::DISPATCHER = instance Net::SNMP::Dispatcher;
69 153
70our $MESSAGE_PROCESSING = $Net::SNMP::Dispatcher::MESSAGE_PROCESSING; 154our $MESSAGE_PROCESSING = $Net::SNMP::Dispatcher::MESSAGE_PROCESSING;
71 155
72# avoid the method call
73my $timer = sub { shift->timer (@_) };
74AnyEvent::post_detect { $timer = AnyEvent->can ("timer") };
75
76our $BUSY; 156our $BUSY;
157our $DONE; # finished all jobs
77our %TRANSPORT; # address => [count, watcher] 158our @TRANSPORT; # fileno => [count, watcher]
159our @QUEUE;
160our $MAX_OUTSTANDING = 50;
161our $MIN_RECVQUEUE = 8;
162our $MAX_RECVQUEUE = 64;
163
164sub kick_job;
78 165
79sub _send_pdu { 166sub _send_pdu {
80 my ($pdu, $retries) = @_; 167 my ($pdu, $retries) = @_;
81 168
82 # mostly copied from Net::SNMP::Dispatch 169 # mostly copied from Net::SNMP::Dispatch
85 # create the new outgoing message. 172 # create the new outgoing message.
86 my $msg = $MESSAGE_PROCESSING->prepare_outgoing_msg ($pdu); 173 my $msg = $MESSAGE_PROCESSING->prepare_outgoing_msg ($pdu);
87 174
88 if (!defined $msg) { 175 if (!defined $msg) {
89 --$BUSY; 176 --$BUSY;
177 kick_job;
90 # Inform the command generator about the Message Processing error. 178 # Inform the command generator about the Message Processing error.
91 $pdu->status_information ($MESSAGE_PROCESSING->error); 179 $pdu->status_information ($MESSAGE_PROCESSING->error);
92 return; 180 return;
93 } 181 }
94 182
97 $MESSAGE_PROCESSING->msg_handle_delete ($pdu->msg_id) 185 $MESSAGE_PROCESSING->msg_handle_delete ($pdu->msg_id)
98 if $pdu->expect_response; 186 if $pdu->expect_response;
99 187
100 # A crude attempt to recover from temporary failures. 188 # A crude attempt to recover from temporary failures.
101 if ($retries-- > 0 && ($!{EAGAIN} || $!{EWOULDBLOCK} || $!{ENOSPC})) { 189 if ($retries-- > 0 && ($!{EAGAIN} || $!{EWOULDBLOCK} || $!{ENOSPC})) {
102 my $retry_w; $retry_w = AnyEvent->$timer (after => $pdu->timeout, cb => sub { 190 my $retry_w; $retry_w = AE::timer $pdu->timeout, 0, sub {
103 undef $retry_w; 191 undef $retry_w;
104 _send_pdu ($pdu, $retries); 192 _send_pdu ($pdu, $retries);
105 }); 193 };
106 } else { 194 } else {
107 --$BUSY; 195 --$BUSY;
196 kick_job;
108 } 197 }
109 198
110 # Inform the command generator about the send() error. 199 # Inform the command generator about the send() error.
111 $pdu->status_information ($msg->error); 200 $pdu->status_information ($msg->error);
112 return; 201 return;
113 } 202 }
114 203
115 # Schedule the timeout handler if the message expects a response. 204 # Schedule the timeout handler if the message expects a response.
116 if ($pdu->expect_response) { 205 if ($pdu->expect_response) {
117 my $transport = $msg->transport; 206 my $transport = $msg->transport;
207 my $fileno = $transport->fileno;
118 208
119 # register the transport 209 # register the transport
120 unless ($TRANSPORT{$transport+0}[0]++) { 210 unless ($TRANSPORT[$fileno][0]++) {
121 $TRANSPORT{$transport+0}[1] = AnyEvent->io (fh => $transport->socket, poll => 'r', cb => sub { 211 $TRANSPORT[$fileno][1] = AE::io $transport->socket, 0, sub {
212 for my $count (1..$MAX_RECVQUEUE) { # handle up to this many requests in one go
122 # Create a new Message object to receive the response 213 # Create a new Message object to receive the response
123 my ($msg, $error) = Net::SNMP::Message->new (-transport => $transport); 214 my ($msg, $error) = Net::SNMP::Message->new (-transport => $transport);
124 215
125 if (!defined $msg) { 216 if (!defined $msg) {
126 die sprintf 'Failed to create Message object [%s]', $error; 217 die sprintf 'Failed to create Message object [%s]', $error;
218 }
219
220 # Read the message from the Transport Layer
221 if (!defined $msg->recv) {
222 if ($transport->connectionless) {
223 # if we handled very few replies and we have queued work, try
224 # to increase the parallelity as we probably can handle more.
225 if ($count < $MIN_RECVQUEUE && @QUEUE) {
226 ++$MAX_OUTSTANDING;
227 kick_job;
228 }
229 } else {
230 # for some reason, connected-oriented transports seem to need this
231 delete $TRANSPORT[$fileno]
232 unless --$TRANSPORT[$fileno][0];
233 }
234
235 $msg->error;
236 return;
237 }
238
239 # For connection-oriented Transport Domains, it is possible to
240 # "recv" an empty buffer if reassembly is required.
241 if (!$msg->length) {
242 return;
243 }
244
245 # Hand the message over to Message Processing.
246 if (!defined $MESSAGE_PROCESSING->prepare_data_elements ($msg)) {
247 $MESSAGE_PROCESSING->error;
248 return;
249 }
250
251 # Set the error if applicable.
252 $msg->error ($MESSAGE_PROCESSING->error) if $MESSAGE_PROCESSING->error;
253
254 # Notify the command generator to process the response.
255 $msg->process_response_pdu;
256
257 # Cancel the timeout.
258 my $rtimeout_w = $msg->timeout_id;
259 if ($$rtimeout_w) {
260 undef $$rtimeout_w;
261
262 --$BUSY;
263 kick_job;
264
265 unless (--$TRANSPORT[$fileno][0]) {
266 delete $TRANSPORT[$fileno];
267 return;
268 }
269 }
127 } 270 }
128 271
129 # Read the message from the Transport Layer 272 # when we end up here, we successfully handled $MAX_RECVQUEUE
130 if (!defined $msg->recv) { 273 # replies in one iteration, so assume we are overloaded
131 # for some reason, connected-oriented transports seem to need this 274 # and reduce the amount of parallelity.
132 unless ($transport->connectionless) { 275 $MAX_OUTSTANDING = (int $MAX_OUTSTANDING * 0.95) || 1;
133 delete $TRANSPORT{$transport+0}
134 unless --$TRANSPORT{$transport+0}[0];
135 }
136
137 $msg->error;
138 return;
139 } 276 };
277 }
140 278
141 # For connection-oriented Transport Domains, it is possible to 279 $msg->timeout_id (\(my $rtimeout_w =
142 # "recv" an empty buffer if reassembly is required. 280 AE::timer $pdu->timeout, 0, sub {
143 if (!$msg->length) {
144 return;
145 }
146
147 # Hand the message over to Message Processing.
148 if (!defined $MESSAGE_PROCESSING->prepare_data_elements ($msg)) {
149 $MESSAGE_PROCESSING->error;
150 return;
151 }
152
153 # Set the error if applicable.
154 $msg->error ($MESSAGE_PROCESSING->error) if $MESSAGE_PROCESSING->error;
155
156 # Cancel the timeout.
157 my $rtimeout_w = $msg->timeout_id; 281 my $rtimeout_w = $msg->timeout_id;
158 if ($$rtimeout_w) { 282 if ($$rtimeout_w) {
159 undef $$rtimeout_w; 283 undef $$rtimeout_w;
160 delete $TRANSPORT{$transport+0} 284 delete $TRANSPORT[$fileno]
161 unless --$TRANSPORT{$transport+0}[0]; 285 unless --$TRANSPORT[$fileno][0];
162
163 --$BUSY;
164 }
165
166 # Notify the command generator to process the response.
167 $msg->process_response_pdu;
168 });
169 }
170
171 #####d# timeout_id, wtf?
172 $msg->timeout_id (\(my $rtimeout_w =
173 AnyEvent->$timer (after => $pdu->timeout, cb => sub {
174 my $rtimeout_w = $msg->timeout_id;
175 if ($$rtimeout_w) {
176 undef $$rtimeout_w;
177 delete $TRANSPORT{$transport+0}
178 unless --$TRANSPORT{$transport+0}[0];
179 } 286 }
180 287
181 if ($retries--) { 288 if ($retries--) {
182 _send_pdu ($pdu, $retries); 289 _send_pdu ($pdu, $retries);
183 } else { 290 } else {
184 --$BUSY;
185 $MESSAGE_PROCESSING->msg_handle_delete ($pdu->msg_id); 291 $MESSAGE_PROCESSING->msg_handle_delete ($pdu->msg_id);
186 $pdu->status_information ("No response from remote host '%s'", $pdu->hostname); 292 $pdu->status_information ("No response from remote host '%s'", $pdu->hostname);
293
294 --$BUSY;
295 kick_job;
187 } 296 }
188 }) 297 })
189 )); 298 );
190 } else { 299 } else {
191 --$BUSY; 300 --$BUSY;
301 kick_job;
192 } 302 }
303}
304
305sub kick_job {
306 while ($BUSY < $MAX_OUTSTANDING) {
307 my $pdu = shift @QUEUE
308 or last;
309
310 ++$BUSY;
311 _send_pdu $pdu, $pdu->retries;
312 }
313
314 $DONE and $DONE->() unless $BUSY;
193} 315}
194 316
195sub send_pdu($$$) { 317sub send_pdu($$$) {
196 my (undef, $pdu, $delay) = @_; 318 my (undef, $pdu, $delay) = @_;
197 319
198 ++$BUSY; 320 # $delay is not very sensibly implemented by AnyEvent::SNMP,
199 321 # but apparently it is not a very sensible feature.
200 if ($delay > 0) { 322 if ($delay > 0) {
323 ++$BUSY;
201 my $delay_w; $delay_w = AnyEvent->$timer (after => $delay, cb => sub { 324 my $delay_w; $delay_w = AE::timer $delay, 0, sub {
202 undef $delay_w; 325 undef $delay_w;
203 _send_pdu ($pdu, $pdu->retries); 326 push @QUEUE, $pdu;
327 --$BUSY;
328 kick_job;
204 }); 329 };
205 return 1; 330 return 1;
206 } 331 }
207 332
208 _send_pdu $pdu, $pdu->retries; 333 push @QUEUE, $pdu;
334 kick_job;
335
209 1 336 1
210} 337}
211 338
212sub activate($) { 339sub activate($) {
213 AnyEvent->one_event while $BUSY; 340 while ($BUSY) {
341 $DONE = AE::cv;
342 $DONE->recv;
343 undef $DONE;
344 }
214} 345}
215 346
216sub one_event($) { 347sub one_event($) {
217 die; 348 # should not ever be used
349 AnyEvent->one_event; #d# todo
350}
351
352sub set_max_outstanding($) {
353 $MAX_OUTSTANDING = $_[0];
354 kick_job;
218} 355}
219 356
220=head1 SEE ALSO 357=head1 SEE ALSO
221 358
222L<AnyEvent>, L<Net::SNMP>, L<Net::SNMP::EV>. 359L<AnyEvent>, L<Net::SNMP>, L<Net::SNMP::XS>, L<Net::SNMP::EV>.
223 360
224=head1 AUTHOR 361=head1 AUTHOR
225 362
226 Marc Lehmann <schmorp@schmorp.de> 363 Marc Lehmann <schmorp@schmorp.de>
227 http://home.schmorp.de/ 364 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines