1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
/*
* Copyright (c) 2008, Stefan Walter
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*
* CONTRIBUTORS
* Stef Walter <stef@memberwebs.com>
*
*/
#include "usuals.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <err.h>
#include <bsnmp/asn1.h>
#include <bsnmp/snmp.h>
#include "log.h"
#include "rrdbotd.h"
#include "server-mainloop.h"
#include "snmp-engine.h"
/* -----------------------------------------------------------------------------
* PACKET HANDLING
*/
static void
complete_requests (rb_item *item, int code)
{
int host;
ASSERT (item);
if (item->field_request)
snmp_engine_cancel (item->field_request);
item->field_request = 0;
if (item->query_request)
snmp_engine_cancel (item->query_request);
item->query_request = 0;
/* If we have multiple host names then try the next host */
if (code != SNMP_ERR_NOERROR) {
host = (item->hostindex + 1) % item->n_hostnames;
if (host != item->hostindex) {
log_debug ("request failed, trying new host: %s", item->hostnames[host]);
item->hostindex = host;
}
}
}
static void
cancel_requests (rb_item *item, const char *reason)
{
ASSERT (item);
ASSERT (reason);
ASSERT (item->field_request || item->query_request);
log_debug ("value for field '%s': %s", item->field, reason);
item->vtype = VALUE_UNSET;
complete_requests (item, -1);
}
static void
force_poll (rb_poller *poll, mstime when, const char *reason)
{
rb_item *item;
int forced = 0;
ASSERT (poll);
ASSERT (reason);
/* Now see if the all the requests are done */
for (item = poll->items; item; item = item->next) {
if (item->field_request || item->query_request) {
cancel_requests (item, reason);
forced = 1;
}
ASSERT (!item->field_request);
ASSERT (!item->query_request);
}
if (!forced && !poll->polling)
return;
/* Mark any non-matched queries as unset */
for (item = poll->items; item; item = item->next) {
if (item->has_query && !item->query_matched)
item->vtype = VALUE_UNSET;
}
/*
* We note the failure has having taken place halfway between
* the request and the current time.
*/
poll->last_polled = poll->last_request + ((when - poll->last_request) / 2);
/* And send off our collection of values */
rb_rrd_update (poll);
/* This polling cycle is no longer active */
poll->polling = 0;
}
static void
finish_poll (rb_poller *poll, mstime when)
{
rb_item *item;
ASSERT (poll);
ASSERT (poll->polling);
/* See if the all the requests are done */
for (item = poll->items; item; item = item->next) {
if (item->field_request || item->query_request)
return;
}
/* Mark any non-matched queries as unset */
for (item = poll->items; item; item = item->next) {
if (item->has_query && !item->query_matched)
item->vtype = VALUE_UNSET;
}
/* Update the book-keeping */
poll->last_polled = when;
/* And send off our collection of values */
rb_rrd_update (poll);
/* This polling cycle is no longer active */
poll->polling = 0;
}
static void
field_response (int request, int code, struct snmp_value *value, void *arg)
{
rb_item *item = arg;
const char *msg = NULL;
ASSERT (item->field_request == request);
/* Mark this item as done */
item->field_request = 0;
/* Errors result in us writing U */
if (code != SNMP_ERR_NOERROR) {
item->vtype = VALUE_UNSET;
/* Parse the value from server */
} else {
switch(value->syntax)
{
case SNMP_SYNTAX_NULL:
item->vtype = VALUE_UNSET;
break;
case SNMP_SYNTAX_INTEGER:
item->v.i_value = value->v.integer;
item->vtype = VALUE_REAL;
break;
case SNMP_SYNTAX_COUNTER:
case SNMP_SYNTAX_GAUGE:
case SNMP_SYNTAX_TIMETICKS:
item->v.i_value = value->v.uint32;
item->vtype = VALUE_REAL;
break;
case SNMP_SYNTAX_COUNTER64:
item->v.i_value = value->v.counter64;
item->vtype = VALUE_REAL;
break;
case SNMP_SYNTAX_OCTETSTRING:
case SNMP_SYNTAX_OID:
case SNMP_SYNTAX_IPADDRESS:
msg = "snmp server returned non numeric value for field: %s";
break;
case SNMP_SYNTAX_NOSUCHOBJECT:
case SNMP_SYNTAX_NOSUCHINSTANCE:
case SNMP_SYNTAX_ENDOFMIBVIEW:
msg = "field not available on snmp server: %s";
break;
default:
msg = "snmp server returned invalid or unsupported value for field: %s";
break;
};
if (msg)
log_warnx (msg, item->field);
else if (item->vtype == VALUE_REAL)
log_debug ("got value for field '%s': %lld",
item->field, item->v.i_value);
else if (item->vtype == VALUE_FLOAT)
log_debug ("got value for field '%s': %.4lf",
item->field, item->v.f_value);
else
log_debug ("got value for field '%s': U",
item->field);
}
complete_requests (item, code);
/* If the entire poll is done, then complete it */
finish_poll (item->poller, server_get_time ());
}
static void
field_request (rb_item *item)
{
int req;
ASSERT (item);
ASSERT (!item->field_request);
item->vtype = VALUE_UNSET;
req = snmp_engine_request (item->hostnames[item->hostindex], item->community,
item->version, item->poller->interval, item->poller->timeout,
SNMP_PDU_GET, &item->field_oid, field_response, item);
item->field_request = req;
}
/* Forward declaration */
static void query_search_request (rb_item *item);
static void
query_value_request (rb_item *item, asn_subid_t subid)
{
struct asn_oid oid;
int req;
ASSERT (item);
ASSERT (item->has_query);
ASSERT (!item->query_request);
ASSERT (!item->field_request);
item->vtype = VALUE_UNSET;
/* OID for the actual value */
oid = item->field_oid;
ASSERT (oid.len < ASN_MAXOIDLEN);
oid.subs[oid.len] = subid;
++oid.len;
log_debug ("query requesting value for table index: %u", subid);
req = snmp_engine_request (item->hostnames[item->hostindex], item->community,
item->version, item->poller->interval, item->poller->timeout,
SNMP_PDU_GET, &oid, field_response, item);
/* Value retrieval is active */
item->field_request = req;
}
static void
query_next_response (int request, int code, struct snmp_value *value, void *arg)
{
rb_item *item = arg;
asn_subid_t subid;
int matched;
/*
* Called when we get the next OID in a table
*/
ASSERT (request == item->query_request);
ASSERT (!item->field_request);
item->query_request = 0;
if (code == SNMP_ERR_NOERROR) {
ASSERT (value);
/* Convert these result codes into 'not found' */
switch (value->syntax) {
case SNMP_SYNTAX_NOSUCHOBJECT:
case SNMP_SYNTAX_NOSUCHINSTANCE:
case SNMP_SYNTAX_ENDOFMIBVIEW:
code = SNMP_ERR_NOSUCHNAME;
break;
/*
* Make sure that we haven't gone past the end. For it to
* be in the table it must be exactly one longer (the table index)
* and otherwise identical.
*/
default:
if (item->query_oid.len + 1 != value->var.len ||
!asn_is_suboid (&item->query_oid, &value->var))
code = SNMP_ERR_NOSUCHNAME;
break;
};
}
if (code == SNMP_ERR_NOSUCHNAME)
log_debug ("query couldn't find table index that matches: %s",
item->query_match ? item->query_match : "[null]");
/* Problems communicating with the server, or not found */
if (code != SNMP_ERR_NOERROR) {
memset (&item->query_last, 0, sizeof (item->query_last));
complete_requests (item, code);
return;
}
/* Save away the last OID we've seen */
item->query_last = value->var;
item->query_searched = 1;
ASSERT (value);
/* Match the query valu ereceived */
if (item->query_match)
matched = snmp_engine_match (value, item->query_match);
/* When query match is null, anything matches */
else
matched = 1;
item->query_matched = matched;
item->vtype = VALUE_UNSET;
if (matched) {
/* Do a query for the field value with this sub id */
subid = value->var.subs[value->var.len - 1];
query_value_request (item, subid);
} else {
/* Look for the next table index */
query_search_request (item);
}
}
static void
query_search_request (rb_item *item)
{
struct asn_oid *oid;
int req;
ASSERT (item);
ASSERT (item->has_query);
ASSERT (!item->query_request);
ASSERT (!item->field_request);
item->query_matched = 0;
item->vtype = VALUE_UNSET;
/* Start with the OID without any table index */
if (!item->query_searched) {
oid = &item->query_oid;
memset (&item->query_last, 0, sizeof (item->query_last));
log_debug ("query looking for first table index");
/* Go for the next one in the search */
} else {
ASSERT (item->query_last.len > 0);
oid = &item->query_last;
log_debug ("query looking for next table index");
}
req = snmp_engine_request (item->hostnames[item->hostindex], item->community,
item->version, item->poller->interval, item->poller->timeout,
SNMP_PDU_GETNEXT, oid, query_next_response, item);
item->query_request = req;
}
static void
query_match_response (int request, int code, struct snmp_value *value, void *arg)
{
rb_item *item = arg;
int matched;
/*
* Callback when SNMP request in query_request() completes.
*
* We receive a value back from the server when querying the table match OID,
* whenever we queried it directly (without the search).
*/
ASSERT (request == item->query_request);
item->query_request = 0;
/* Problems communicating with the server? */
if (code != SNMP_ERR_NOERROR && code != SNMP_ERR_NOSUCHNAME) {
complete_requests (item, code);
return;
}
matched = 0;
if (code == SNMP_ERR_NOERROR) {
ASSERT (value);
/* These all signify 'not found' in our book */
switch (value->syntax) {
case SNMP_SYNTAX_NOSUCHOBJECT:
case SNMP_SYNTAX_NOSUCHINSTANCE:
case SNMP_SYNTAX_ENDOFMIBVIEW:
break;
/* See if we have a match */
default:
if (item->query_match)
matched = snmp_engine_match (value, item->query_match);
/* When query match is null, anything matches */
else
matched = 1;
break;
};
}
item->query_matched = matched;
if (matched)
return;
log_debug ("query previous index did not match: %s",
item->query_match ? item->query_match : "[null]");
/*
* When it doesn't match cancel any pending value request, and
* start a search for a match.
*/
if (item->field_request)
snmp_engine_cancel (item->field_request);
item->field_request = 0;
query_search_request (item);
}
static void
query_pair_request (rb_item *item, asn_subid_t subid)
{
struct asn_oid oid;
int req;
ASSERT (item);
ASSERT (item->has_query);
ASSERT (!item->query_request);
ASSERT (!item->field_request);
log_debug ("query requesting match and value pair for index: %u", subid);
item->vtype = VALUE_UNSET;
item->query_matched = 0;
/* OID for the value to match */
oid = item->query_oid;
ASSERT (oid.len < ASN_MAXOIDLEN);
oid.subs[oid.len] = subid;
++oid.len;
req = snmp_engine_request (item->hostnames[item->hostindex], item->community,
item->version, item->poller->interval, item->poller->timeout,
SNMP_PDU_GET, &oid, query_match_response, item);
/* Query is active */
item->query_request = req;
/* OID for the actual value */
oid = item->field_oid;
ASSERT (oid.len < ASN_MAXOIDLEN);
oid.subs[oid.len] = subid;
++oid.len;
req = snmp_engine_request (item->hostnames[item->hostindex], item->community,
item->version, item->poller->interval, item->poller->timeout,
SNMP_PDU_GET, &oid, field_response, item);
/* Value retrieval is active */
item->field_request = req;
}
static void
query_request (rb_item *item)
{
ASSERT (item);
ASSERT (!item->query_request);
ASSERT (!item->field_request);
item->query_searched = 0;
item->query_matched = 0;
item->vtype = VALUE_UNSET;
if (item->query_last.len) {
/*
* If we've done this query before, then we know the last matching table
* index. We build a two part request that gets the match value for the
* table index it was last seen on, and the actual value that we want.
*
* Doing this in one request is more efficient, then we check if the
* match value matches the query in the response.
*/
query_pair_request (item, item->query_last.subs[item->query_last.len - 1]);
} else {
/*
* We don't have a last matching table index, so start the search.
* For indexes. We'll then query each of those indexes with the two
* part request, as above.
*/
query_search_request (item);
}
}
static int
poller_timer (mstime when, void *arg)
{
rb_poller *poll = (rb_poller*)arg;
rb_item *item;
/*
* If the previous poll has not completed, then we count it
* as a timeout.
*/
force_poll (poll, when, "timed out");
/* Mark this poller as starting requests now */
poll->last_request = when;
ASSERT (!poll->polling);
poll->polling = 1;
/*
* Send off the next query. This needs to be done after
* all the timeouts above, as the above could write to RRD.
*/
for (item = poll->items; item; item = item->next) {
if (item->has_query)
query_request (item);
else
field_request (item);
}
snmp_engine_flush ();
return 1;
}
static int
prep_timer (mstime when, void* arg)
{
/*
* We don't prepare all timers at exactly the same time
* but we sort of randomly start the various timers. We're
* going to be hitting these over and over again, so there's
* lots of benefits to spreading them out randomly over a
* few seconds.
*/
rb_poller* poll;
int next;
/* All done? */
if(!arg)
return 0;
poll = (rb_poller*)arg;
if (server_timer (poll->interval, poller_timer, poll) == -1)
log_error ("couldn't setup poller timer");
/* Run the poll the first time */
poller_timer (when, poll);
/* Setup the next poller anywhere between 0 and 750 ms */
next = rand () % 750;
server_oneshot (next, prep_timer, poll->next);
return 0;
}
void
rb_poll_engine_init (void)
{
/* Start the preparation timers for setting up randomly */
if (server_oneshot (100, prep_timer, g_state.polls) == -1)
err(1, "couldn't setup timer");
}
void
rb_poll_engine_uninit (void)
{
}
|