1 /**
2 * collectd - src/snmp.c
3 * Copyright (C) 2007-2012 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_complain.h"
32 #include <pthread.h>
34 #include <net-snmp/net-snmp-config.h>
35 #include <net-snmp/net-snmp-includes.h>
37 #include <fnmatch.h>
39 /*
40 * Private data structes
41 */
42 struct oid_s
43 {
44 oid oid[MAX_OID_LEN];
45 size_t oid_len;
46 };
47 typedef struct oid_s oid_t;
49 union instance_u
50 {
51 char string[DATA_MAX_NAME_LEN];
52 oid_t oid;
53 };
54 typedef union instance_u instance_t;
56 struct data_definition_s
57 {
58 char *name; /* used to reference this from the `Collect' option */
59 char *type; /* used to find the data_set */
60 _Bool is_table;
61 instance_t instance;
62 char *instance_prefix;
63 oid_t *values;
64 size_t values_len;
65 double scale;
66 double shift;
67 struct data_definition_s *next;
68 char **ignores;
69 size_t ignores_len;
70 int invert_match;
71 };
72 typedef struct data_definition_s data_definition_t;
74 struct host_definition_s
75 {
76 char *name;
77 char *address;
78 int version;
80 /* snmpv1/2 options */
81 char *community;
83 /* snmpv3 security options */
84 char *username;
85 oid *auth_protocol;
86 size_t auth_protocol_len;
87 char *auth_passphrase;
88 oid *priv_protocol;
89 size_t priv_protocol_len;
90 char *priv_passphrase;
91 int security_level;
92 char *context;
94 void *sess_handle;
95 c_complain_t complaint;
96 cdtime_t interval;
97 data_definition_t **data_list;
98 int data_list_len;
99 };
100 typedef struct host_definition_s host_definition_t;
102 /* These two types are used to cache values in `csnmp_read_table' to handle
103 * gaps in tables. */
104 struct csnmp_list_instances_s
105 {
106 oid_t suffix;
107 char instance[DATA_MAX_NAME_LEN];
108 struct csnmp_list_instances_s *next;
109 };
110 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
112 struct csnmp_table_values_s
113 {
114 oid_t suffix;
115 value_t value;
116 struct csnmp_table_values_s *next;
117 };
118 typedef struct csnmp_table_values_s csnmp_table_values_t;
120 /*
121 * Private variables
122 */
123 static data_definition_t *data_head = NULL;
125 /*
126 * Prototypes
127 */
128 static int csnmp_read_host (user_data_t *ud);
130 /*
131 * Private functions
132 */
133 static void csnmp_oid_init (oid_t *dst, oid const *src, size_t n)
134 {
135 assert (n <= STATIC_ARRAY_SIZE (dst->oid));
136 memcpy (dst->oid, src, sizeof (*src) * n);
137 dst->oid_len = n;
138 }
140 static int csnmp_oid_compare (oid_t const *left, oid_t const *right)
141 {
142 return (snmp_oid_compare (left->oid, left->oid_len,
143 right->oid, right->oid_len));
144 }
146 static int csnmp_oid_suffix (oid_t *dst, oid_t const *src,
147 oid_t const *root)
148 {
149 /* Make sure "src" is in "root"s subtree. */
150 if (src->oid_len <= root->oid_len)
151 return (EINVAL);
152 if (snmp_oid_ncompare (root->oid, root->oid_len,
153 src->oid, src->oid_len,
154 /* n = */ root->oid_len) != 0)
155 return (EINVAL);
157 memset (dst, 0, sizeof (*dst));
158 dst->oid_len = src->oid_len - root->oid_len;
159 memcpy (dst->oid, &src->oid[root->oid_len],
160 dst->oid_len * sizeof (dst->oid[0]));
161 return (0);
162 }
164 static int csnmp_oid_to_string (char *buffer, size_t buffer_size,
165 oid_t const *o)
166 {
167 char oid_str[MAX_OID_LEN][16];
168 char *oid_str_ptr[MAX_OID_LEN];
169 size_t i;
171 for (i = 0; i < o->oid_len; i++)
172 {
173 ssnprintf (oid_str[i], sizeof (oid_str[i]), "%lu", (unsigned long) o->oid[i]);
174 oid_str_ptr[i] = oid_str[i];
175 }
177 return (strjoin (buffer, buffer_size,
178 oid_str_ptr, o->oid_len, /* separator = */ "."));
179 }
181 static void csnmp_host_close_session (host_definition_t *host) /* {{{ */
182 {
183 if (host->sess_handle == NULL)
184 return;
186 snmp_sess_close (host->sess_handle);
187 host->sess_handle = NULL;
188 } /* }}} void csnmp_host_close_session */
190 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
191 {
192 host_definition_t *hd;
194 hd = arg;
196 if (hd == NULL)
197 return;
199 if (hd->name != NULL)
200 {
201 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
202 hd->name);
203 }
205 csnmp_host_close_session (hd);
207 sfree (hd->name);
208 sfree (hd->address);
209 sfree (hd->community);
210 sfree (hd->username);
211 sfree (hd->auth_passphrase);
212 sfree (hd->priv_passphrase);
213 sfree (hd->context);
214 sfree (hd->data_list);
216 sfree (hd);
217 } /* }}} void csnmp_host_definition_destroy */
219 /* Many functions to handle the configuration. {{{ */
220 /* First there are many functions which do configuration stuff. It's a big
221 * bloated and messy, I'm afraid. */
223 /*
224 * Callgraph for the config stuff:
225 * csnmp_config
226 * +-> call_snmp_init_once
227 * +-> csnmp_config_add_data
228 * ! +-> csnmp_config_add_data_instance
229 * ! +-> csnmp_config_add_data_instance_prefix
230 * ! +-> csnmp_config_add_data_values
231 * +-> csnmp_config_add_host
232 * +-> csnmp_config_add_host_version
233 * +-> csnmp_config_add_host_collect
234 * +-> csnmp_config_add_host_auth_protocol
235 * +-> csnmp_config_add_host_priv_protocol
236 * +-> csnmp_config_add_host_security_level
237 */
238 static void call_snmp_init_once (void)
239 {
240 static int have_init = 0;
242 if (have_init == 0)
243 init_snmp (PACKAGE_NAME);
244 have_init = 1;
245 } /* void call_snmp_init_once */
247 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
248 {
249 char buffer[DATA_MAX_NAME_LEN];
250 int status;
252 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
253 if (status != 0)
254 return status;
256 if (dd->is_table)
257 {
258 /* Instance is an OID */
259 dd->instance.oid.oid_len = MAX_OID_LEN;
261 if (!read_objid (buffer,
262 dd->instance.oid.oid, &dd->instance.oid.oid_len))
263 {
264 ERROR ("snmp plugin: read_objid (%s) failed.", buffer);
265 return (-1);
266 }
267 }
268 else
269 {
270 /* Instance is a simple string */
271 sstrncpy (dd->instance.string, buffer,
272 sizeof (dd->instance.string));
273 }
275 return (0);
276 } /* int csnmp_config_add_data_instance */
278 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
279 oconfig_item_t *ci)
280 {
281 int status;
283 if (!dd->is_table)
284 {
285 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
286 "is set to `false'.", dd->name);
287 return (-1);
288 }
290 status = cf_util_get_string(ci, &dd->instance_prefix);
291 return status;
292 } /* int csnmp_config_add_data_instance_prefix */
294 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
295 {
296 int i;
298 if (ci->values_num < 1)
299 {
300 WARNING ("snmp plugin: `Values' needs at least one argument.");
301 return (-1);
302 }
304 for (i = 0; i < ci->values_num; i++)
305 if (ci->values[i].type != OCONFIG_TYPE_STRING)
306 {
307 WARNING ("snmp plugin: `Values' needs only string argument.");
308 return (-1);
309 }
311 sfree (dd->values);
312 dd->values_len = 0;
313 dd->values = malloc (sizeof (*dd->values) * ci->values_num);
314 if (dd->values == NULL)
315 return (-1);
316 dd->values_len = (size_t) ci->values_num;
318 for (i = 0; i < ci->values_num; i++)
319 {
320 dd->values[i].oid_len = MAX_OID_LEN;
322 if (NULL == snmp_parse_oid (ci->values[i].value.string,
323 dd->values[i].oid, &dd->values[i].oid_len))
324 {
325 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
326 ci->values[i].value.string);
327 free (dd->values);
328 dd->values = NULL;
329 dd->values_len = 0;
330 return (-1);
331 }
332 }
334 return (0);
335 } /* int csnmp_config_add_data_instance */
337 static int csnmp_config_add_data_blacklist(data_definition_t *dd, oconfig_item_t *ci)
338 {
339 int i;
341 if (ci->values_num < 1)
342 return (0);
344 for (i = 0; i < ci->values_num; i++)
345 {
346 if (ci->values[i].type != OCONFIG_TYPE_STRING)
347 {
348 WARNING ("snmp plugin: `Ignore' needs only string argument.");
349 return (-1);
350 }
351 }
353 dd->ignores_len = 0;
354 dd->ignores = NULL;
356 for (i = 0; i < ci->values_num; ++i)
357 {
358 if (strarray_add(&(dd->ignores), &(dd->ignores_len), ci->values[i].value.string) != 0)
359 {
360 ERROR("snmp plugin: Can't allocate memory");
361 strarray_free(dd->ignores, dd->ignores_len);
362 return (ENOMEM);
363 }
364 }
365 return 0;
366 } /* int csnmp_config_add_data_blacklist */
368 static int csnmp_config_add_data_blacklist_match_inverted(data_definition_t *dd, oconfig_item_t *ci)
369 {
370 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
371 {
372 WARNING ("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
373 return (-1);
374 }
376 dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
378 return (0);
379 } /* int csnmp_config_add_data_blacklist_match_inverted */
381 static int csnmp_config_add_data (oconfig_item_t *ci)
382 {
383 data_definition_t *dd;
384 int status = 0;
385 int i;
387 dd = calloc (1, sizeof (*dd));
388 if (dd == NULL)
389 return (-1);
391 status = cf_util_get_string(ci, &dd->name);
392 if (status != 0)
393 {
394 free (dd);
395 return (-1);
396 }
398 dd->scale = 1.0;
399 dd->shift = 0.0;
401 for (i = 0; i < ci->children_num; i++)
402 {
403 oconfig_item_t *option = ci->children + i;
405 if (strcasecmp ("Type", option->key) == 0)
406 status = cf_util_get_string(option, &dd->type);
407 else if (strcasecmp ("Table", option->key) == 0)
408 status = cf_util_get_boolean(option, &dd->is_table);
409 else if (strcasecmp ("Instance", option->key) == 0)
410 status = csnmp_config_add_data_instance (dd, option);
411 else if (strcasecmp ("InstancePrefix", option->key) == 0)
412 status = csnmp_config_add_data_instance_prefix (dd, option);
413 else if (strcasecmp ("Values", option->key) == 0)
414 status = csnmp_config_add_data_values (dd, option);
415 else if (strcasecmp ("Shift", option->key) == 0)
416 status = cf_util_get_double(option, &dd->shift);
417 else if (strcasecmp ("Scale", option->key) == 0)
418 status = cf_util_get_double(option, &dd->scale);
419 else if (strcasecmp ("Ignore", option->key) == 0)
420 status = csnmp_config_add_data_blacklist(dd, option);
421 else if (strcasecmp ("InvertMatch", option->key) == 0)
422 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
423 else
424 {
425 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
426 status = -1;
427 }
429 if (status != 0)
430 break;
431 } /* for (ci->children) */
433 while (status == 0)
434 {
435 if (dd->type == NULL)
436 {
437 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
438 status = -1;
439 break;
440 }
441 if (dd->values == NULL)
442 {
443 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
444 status = -1;
445 break;
446 }
448 break;
449 } /* while (status == 0) */
451 if (status != 0)
452 {
453 sfree (dd->name);
454 sfree (dd->instance_prefix);
455 sfree (dd->values);
456 sfree (dd->ignores);
457 sfree (dd);
458 return (-1);
459 }
461 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %zu }",
462 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
464 if (data_head == NULL)
465 data_head = dd;
466 else
467 {
468 data_definition_t *last;
469 last = data_head;
470 while (last->next != NULL)
471 last = last->next;
472 last->next = dd;
473 }
475 return (0);
476 } /* int csnmp_config_add_data */
478 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
479 {
480 int version;
482 if ((ci->values_num != 1)
483 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
484 {
485 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
486 return (-1);
487 }
489 version = (int) ci->values[0].value.number;
490 if ((version < 1) || (version > 3))
491 {
492 WARNING ("snmp plugin: `Version' must either be `1', `2', or `3'.");
493 return (-1);
494 }
496 hd->version = version;
498 return (0);
499 } /* int csnmp_config_add_host_address */
501 static int csnmp_config_add_host_collect (host_definition_t *host,
502 oconfig_item_t *ci)
503 {
504 data_definition_t *data;
505 data_definition_t **data_list;
506 int data_list_len;
507 int i;
509 if (ci->values_num < 1)
510 {
511 WARNING ("snmp plugin: `Collect' needs at least one argument.");
512 return (-1);
513 }
515 for (i = 0; i < ci->values_num; i++)
516 if (ci->values[i].type != OCONFIG_TYPE_STRING)
517 {
518 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
519 return (-1);
520 }
522 data_list_len = host->data_list_len + ci->values_num;
523 data_list = (data_definition_t **) realloc (host->data_list,
524 sizeof (data_definition_t *) * data_list_len);
525 if (data_list == NULL)
526 return (-1);
527 host->data_list = data_list;
529 for (i = 0; i < ci->values_num; i++)
530 {
531 for (data = data_head; data != NULL; data = data->next)
532 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
533 break;
535 if (data == NULL)
536 {
537 WARNING ("snmp plugin: No such data configured: `%s'",
538 ci->values[i].value.string);
539 continue;
540 }
542 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
543 host->name, host->data_list_len, data->name);
545 host->data_list[host->data_list_len] = data;
546 host->data_list_len++;
547 } /* for (values_num) */
549 return (0);
550 } /* int csnmp_config_add_host_collect */
552 static int csnmp_config_add_host_auth_protocol (host_definition_t *hd, oconfig_item_t *ci)
553 {
554 char buffer[4];
555 int status;
557 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
558 if (status != 0)
559 return status;
561 if (strcasecmp("MD5", buffer) == 0) {
562 hd->auth_protocol = usmHMACMD5AuthProtocol;
563 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
564 }
565 else if (strcasecmp("SHA", buffer) == 0) {
566 hd->auth_protocol = usmHMACSHA1AuthProtocol;
567 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol)/sizeof(oid);
568 }
569 else
570 {
571 WARNING ("snmp plugin: The `AuthProtocol' config option must be `MD5' or `SHA'.");
572 return (-1);
573 }
575 DEBUG ("snmp plugin: host = %s; host->auth_protocol = %s;",
576 hd->name, hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
578 return (0);
579 } /* int csnmp_config_add_host_auth_protocol */
581 static int csnmp_config_add_host_priv_protocol (host_definition_t *hd, oconfig_item_t *ci)
582 {
583 char buffer[4];
584 int status;
586 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
587 if (status != 0)
588 return status;
590 if (strcasecmp("AES", buffer) == 0)
591 {
592 hd->priv_protocol = usmAESPrivProtocol;
593 hd->priv_protocol_len = sizeof(usmAESPrivProtocol)/sizeof(oid);
594 }
595 else if (strcasecmp("DES", buffer) == 0) {
596 hd->priv_protocol = usmDESPrivProtocol;
597 hd->priv_protocol_len = sizeof(usmDESPrivProtocol)/sizeof(oid);
598 }
599 else
600 {
601 WARNING ("snmp plugin: The `PrivProtocol' config option must be `AES' or `DES'.");
602 return (-1);
603 }
605 DEBUG ("snmp plugin: host = %s; host->priv_protocol = %s;",
606 hd->name, hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
608 return (0);
609 } /* int csnmp_config_add_host_priv_protocol */
611 static int csnmp_config_add_host_security_level (host_definition_t *hd, oconfig_item_t *ci)
612 {
613 char buffer[16];
614 int status;
616 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
617 if (status != 0)
618 return status;
620 if (strcasecmp("noAuthNoPriv", buffer) == 0)
621 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
622 else if (strcasecmp("authNoPriv", buffer) == 0)
623 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
624 else if (strcasecmp("authPriv", buffer) == 0)
625 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
626 else
627 {
628 WARNING ("snmp plugin: The `SecurityLevel' config option must be `noAuthNoPriv', `authNoPriv', or `authPriv'.");
629 return (-1);
630 }
632 DEBUG ("snmp plugin: host = %s; host->security_level = %d;",
633 hd->name, hd->security_level);
635 return (0);
636 } /* int csnmp_config_add_host_security_level */
638 static int csnmp_config_add_host (oconfig_item_t *ci)
639 {
640 host_definition_t *hd;
641 int status = 0;
642 int i;
644 /* Registration stuff. */
645 char cb_name[DATA_MAX_NAME_LEN];
646 user_data_t cb_data;
648 hd = calloc (1, sizeof (*hd));
649 if (hd == NULL)
650 return (-1);
651 hd->version = 2;
652 C_COMPLAIN_INIT (&hd->complaint);
654 status = cf_util_get_string(ci, &hd->name);
655 if (status != 0)
656 {
657 sfree (hd);
658 return status;
659 }
661 hd->sess_handle = NULL;
662 hd->interval = 0;
664 for (i = 0; i < ci->children_num; i++)
665 {
666 oconfig_item_t *option = ci->children + i;
667 status = 0;
669 if (strcasecmp ("Address", option->key) == 0)
670 status = cf_util_get_string(option, &hd->address);
671 else if (strcasecmp ("Community", option->key) == 0)
672 status = cf_util_get_string(option, &hd->community);
673 else if (strcasecmp ("Version", option->key) == 0)
674 status = csnmp_config_add_host_version (hd, option);
675 else if (strcasecmp ("Collect", option->key) == 0)
676 csnmp_config_add_host_collect (hd, option);
677 else if (strcasecmp ("Interval", option->key) == 0)
678 cf_util_get_cdtime (option, &hd->interval);
679 else if (strcasecmp ("Username", option->key) == 0)
680 status = cf_util_get_string(option, &hd->username);
681 else if (strcasecmp ("AuthProtocol", option->key) == 0)
682 status = csnmp_config_add_host_auth_protocol (hd, option);
683 else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
684 status = csnmp_config_add_host_priv_protocol (hd, option);
685 else if (strcasecmp ("AuthPassphrase", option->key) == 0)
686 status = cf_util_get_string(option, &hd->auth_passphrase);
687 else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
688 status = cf_util_get_string(option, &hd->priv_passphrase);
689 else if (strcasecmp ("SecurityLevel", option->key) == 0)
690 status = csnmp_config_add_host_security_level (hd, option);
691 else if (strcasecmp ("Context", option->key) == 0)
692 status = cf_util_get_string(option, &hd->context);
693 else
694 {
695 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
696 status = -1;
697 }
699 if (status != 0)
700 break;
701 } /* for (ci->children) */
703 while (status == 0)
704 {
705 if (hd->address == NULL)
706 {
707 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
708 status = -1;
709 break;
710 }
711 if (hd->community == NULL && hd->version < 3)
712 {
713 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
714 status = -1;
715 break;
716 }
717 if (hd->version == 3)
718 {
719 if (hd->username == NULL)
720 {
721 WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
722 status = -1;
723 break;
724 }
725 if (hd->security_level == 0)
726 {
727 WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
728 status = -1;
729 break;
730 }
731 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
732 {
733 if (hd->auth_protocol == NULL)
734 {
735 WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
736 status = -1;
737 break;
738 }
739 if (hd->auth_passphrase == NULL)
740 {
741 WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
742 status = -1;
743 break;
744 }
745 }
746 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
747 {
748 if (hd->priv_protocol == NULL)
749 {
750 WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
751 status = -1;
752 break;
753 }
754 if (hd->priv_passphrase == NULL)
755 {
756 WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
757 status = -1;
758 break;
759 }
760 }
761 }
763 break;
764 } /* while (status == 0) */
766 if (status != 0)
767 {
768 csnmp_host_definition_destroy (hd);
769 return (-1);
770 }
772 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
773 hd->name, hd->address, hd->community, hd->version);
775 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
777 memset (&cb_data, 0, sizeof (cb_data));
778 cb_data.data = hd;
779 cb_data.free_func = csnmp_host_definition_destroy;
781 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
782 csnmp_read_host, hd->interval, /* user_data = */ &cb_data);
783 if (status != 0)
784 {
785 ERROR ("snmp plugin: Registering complex read function failed.");
786 csnmp_host_definition_destroy (hd);
787 return (-1);
788 }
790 return (0);
791 } /* int csnmp_config_add_host */
793 static int csnmp_config (oconfig_item_t *ci)
794 {
795 int i;
797 call_snmp_init_once ();
799 for (i = 0; i < ci->children_num; i++)
800 {
801 oconfig_item_t *child = ci->children + i;
802 if (strcasecmp ("Data", child->key) == 0)
803 csnmp_config_add_data (child);
804 else if (strcasecmp ("Host", child->key) == 0)
805 csnmp_config_add_host (child);
806 else
807 {
808 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
809 }
810 } /* for (ci->children) */
812 return (0);
813 } /* int csnmp_config */
815 /* }}} End of the config stuff. Now the interesting part begins */
817 static void csnmp_host_open_session (host_definition_t *host)
818 {
819 struct snmp_session sess;
820 int error;
822 if (host->sess_handle != NULL)
823 csnmp_host_close_session (host);
825 snmp_sess_init (&sess);
826 sess.peername = host->address;
827 switch (host->version)
828 {
829 case 1:
830 sess.version = SNMP_VERSION_1;
831 break;
832 case 3:
833 sess.version = SNMP_VERSION_3;
834 break;
835 default:
836 sess.version = SNMP_VERSION_2c;
837 break;
838 }
840 if (host->version == 3)
841 {
842 sess.securityName = host->username;
843 sess.securityNameLen = strlen (host->username);
844 sess.securityLevel = host->security_level;
846 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
847 {
848 sess.securityAuthProto = host->auth_protocol;
849 sess.securityAuthProtoLen = host->auth_protocol_len;
850 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
851 error = generate_Ku (sess.securityAuthProto,
852 sess.securityAuthProtoLen,
853 (u_char *) host->auth_passphrase,
854 strlen(host->auth_passphrase),
855 sess.securityAuthKey,
856 &sess.securityAuthKeyLen);
857 if (error != SNMPERR_SUCCESS) {
858 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
859 }
860 }
862 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
863 {
864 sess.securityPrivProto = host->priv_protocol;
865 sess.securityPrivProtoLen = host->priv_protocol_len;
866 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
867 error = generate_Ku (sess.securityAuthProto,
868 sess.securityAuthProtoLen,
869 (u_char *) host->priv_passphrase,
870 strlen(host->priv_passphrase),
871 sess.securityPrivKey,
872 &sess.securityPrivKeyLen);
873 if (error != SNMPERR_SUCCESS) {
874 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
875 }
876 }
878 if (host->context != NULL)
879 {
880 sess.contextName = host->context;
881 sess.contextNameLen = strlen (host->context);
882 }
883 }
884 else /* SNMPv1/2 "authenticates" with community string */
885 {
886 sess.community = (u_char *) host->community;
887 sess.community_len = strlen (host->community);
888 }
890 /* snmp_sess_open will copy the `struct snmp_session *'. */
891 host->sess_handle = snmp_sess_open (&sess);
893 if (host->sess_handle == NULL)
894 {
895 char *errstr = NULL;
897 snmp_error (&sess, NULL, NULL, &errstr);
899 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
900 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
901 sfree (errstr);
902 }
903 } /* void csnmp_host_open_session */
905 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
906 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
907 double scale, double shift,
908 const char *host_name, const char *data_name)
909 {
910 value_t ret;
911 uint64_t tmp_unsigned = 0;
912 int64_t tmp_signed = 0;
913 _Bool defined = 1;
914 /* Set to true when the original SNMP type appears to have been signed. */
915 _Bool prefer_signed = 0;
917 if ((vl->type == ASN_INTEGER)
918 || (vl->type == ASN_UINTEGER)
919 || (vl->type == ASN_COUNTER)
920 #ifdef ASN_TIMETICKS
921 || (vl->type == ASN_TIMETICKS)
922 #endif
923 || (vl->type == ASN_GAUGE))
924 {
925 tmp_unsigned = (uint32_t) *vl->val.integer;
926 tmp_signed = (int32_t) *vl->val.integer;
928 if (vl->type == ASN_INTEGER)
929 prefer_signed = 1;
931 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
932 }
933 else if (vl->type == ASN_COUNTER64)
934 {
935 tmp_unsigned = (uint32_t) vl->val.counter64->high;
936 tmp_unsigned = tmp_unsigned << 32;
937 tmp_unsigned += (uint32_t) vl->val.counter64->low;
938 tmp_signed = (int64_t) tmp_unsigned;
939 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
940 }
941 else if (vl->type == ASN_OCTET_STR)
942 {
943 /* We'll handle this later.. */
944 }
945 else
946 {
947 char oid_buffer[1024];
949 memset (oid_buffer, 0, sizeof (oid_buffer));
950 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
951 vl->name, vl->name_length);
953 #ifdef ASN_NULL
954 if (vl->type == ASN_NULL)
955 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
956 oid_buffer);
957 else
958 #endif
959 WARNING ("snmp plugin: I don't know the ASN type #%i "
960 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
961 (int) vl->type, oid_buffer,
962 (data_name != NULL) ? data_name : "UNKNOWN",
963 (host_name != NULL) ? host_name : "UNKNOWN");
965 defined = 0;
966 }
968 if (vl->type == ASN_OCTET_STR)
969 {
970 int status = -1;
972 if (vl->val.string != NULL)
973 {
974 char string[64];
975 size_t string_length;
977 string_length = sizeof (string) - 1;
978 if (vl->val_len < string_length)
979 string_length = vl->val_len;
981 /* The strings we get from the Net-SNMP library may not be null
982 * terminated. That is why we're using `memcpy' here and not `strcpy'.
983 * `string_length' is set to `vl->val_len' which holds the length of the
984 * string. -octo */
985 memcpy (string, vl->val.string, string_length);
986 string[string_length] = 0;
988 status = parse_value (string, &ret, type);
989 if (status != 0)
990 {
991 ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
992 (host_name != NULL) ? host_name : "UNKNOWN",
993 DS_TYPE_TO_STRING (type), string);
994 }
995 }
997 if (status != 0)
998 {
999 switch (type)
1000 {
1001 case DS_TYPE_COUNTER:
1002 case DS_TYPE_DERIVE:
1003 case DS_TYPE_ABSOLUTE:
1004 memset (&ret, 0, sizeof (ret));
1005 break;
1007 case DS_TYPE_GAUGE:
1008 ret.gauge = NAN;
1009 break;
1011 default:
1012 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
1013 "data source type: %i.", type);
1014 ret.gauge = NAN;
1015 }
1016 }
1017 } /* if (vl->type == ASN_OCTET_STR) */
1018 else if (type == DS_TYPE_COUNTER)
1019 {
1020 ret.counter = tmp_unsigned;
1021 }
1022 else if (type == DS_TYPE_GAUGE)
1023 {
1024 if (!defined)
1025 ret.gauge = NAN;
1026 else if (prefer_signed)
1027 ret.gauge = (scale * tmp_signed) + shift;
1028 else
1029 ret.gauge = (scale * tmp_unsigned) + shift;
1030 }
1031 else if (type == DS_TYPE_DERIVE)
1032 {
1033 if (prefer_signed)
1034 ret.derive = (derive_t) tmp_signed;
1035 else
1036 ret.derive = (derive_t) tmp_unsigned;
1037 }
1038 else if (type == DS_TYPE_ABSOLUTE)
1039 {
1040 ret.absolute = (absolute_t) tmp_unsigned;
1041 }
1042 else
1043 {
1044 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1045 "type: %i.", type);
1046 ret.gauge = NAN;
1047 }
1049 return (ret);
1050 } /* value_t csnmp_value_list_to_value */
1052 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1053 * representation and writes it to dst. Returns zero on success and ENOMEM if
1054 * dst is not large enough to hold the string. dst is guaranteed to be
1055 * nul-terminated. */
1056 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
1057 const struct variable_list *vb, size_t dst_size)
1058 {
1059 char *buffer_ptr;
1060 size_t buffer_free;
1061 size_t i;
1063 dst[0] = 0;
1065 buffer_ptr = dst;
1066 buffer_free = dst_size;
1068 for (i = 0; i < vb->val_len; i++)
1069 {
1070 int status;
1072 status = snprintf (buffer_ptr, buffer_free,
1073 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1074 assert (status >= 0);
1076 if (((size_t) status) >= buffer_free) /* truncated */
1077 {
1078 dst[dst_size - 1] = 0;
1079 return ENOMEM;
1080 }
1081 else /* if (status < buffer_free) */
1082 {
1083 buffer_ptr += (size_t) status;
1084 buffer_free -= (size_t) status;
1085 }
1086 }
1088 return 0;
1089 } /* }}} int csnmp_strvbcopy_hexstring */
1091 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1092 * dst. If non-printable characters are detected, it will switch to a hex
1093 * representation of the string. Returns zero on success, EINVAL if vb does not
1094 * contain a string and ENOMEM if dst is not large enough to contain the
1095 * string. */
1096 static int csnmp_strvbcopy (char *dst, /* {{{ */
1097 const struct variable_list *vb, size_t dst_size)
1098 {
1099 char *src;
1100 size_t num_chars;
1101 size_t i;
1103 if (vb->type == ASN_OCTET_STR)
1104 src = (char *) vb->val.string;
1105 else if (vb->type == ASN_BIT_STR)
1106 src = (char *) vb->val.bitstring;
1107 else if (vb->type == ASN_IPADDRESS)
1108 {
1109 return ssnprintf (dst, dst_size, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
1110 (uint8_t) vb->val.string[0],
1111 (uint8_t) vb->val.string[1],
1112 (uint8_t) vb->val.string[2],
1113 (uint8_t) vb->val.string[3]);
1114 }
1115 else
1116 {
1117 dst[0] = 0;
1118 return (EINVAL);
1119 }
1121 num_chars = dst_size - 1;
1122 if (num_chars > vb->val_len)
1123 num_chars = vb->val_len;
1125 for (i = 0; i < num_chars; i++)
1126 {
1127 /* Check for control characters. */
1128 if ((unsigned char)src[i] < 32)
1129 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1130 dst[i] = src[i];
1131 }
1132 dst[num_chars] = 0;
1133 dst[dst_size - 1] = 0;
1135 if (dst_size <= vb->val_len)
1136 return ENOMEM;
1138 return 0;
1139 } /* }}} int csnmp_strvbcopy */
1141 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1142 csnmp_list_instances_t **tail,
1143 const struct snmp_pdu *res,
1144 const host_definition_t *hd, const data_definition_t *dd)
1145 {
1146 csnmp_list_instances_t *il;
1147 struct variable_list *vb;
1148 oid_t vb_name;
1149 int status;
1150 uint32_t i;
1151 uint32_t is_matched;
1153 /* Set vb on the last variable */
1154 for (vb = res->variables;
1155 (vb != NULL) && (vb->next_variable != NULL);
1156 vb = vb->next_variable)
1157 /* do nothing */;
1158 if (vb == NULL)
1159 return (-1);
1161 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1163 il = calloc (1, sizeof (*il));
1164 if (il == NULL)
1165 {
1166 ERROR ("snmp plugin: calloc failed.");
1167 return (-1);
1168 }
1169 il->next = NULL;
1171 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1172 if (status != 0)
1173 {
1174 sfree (il);
1175 return (status);
1176 }
1178 /* Get instance name */
1179 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) || (vb->type == ASN_IPADDRESS))
1180 {
1181 char *ptr;
1183 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1184 is_matched = 0;
1185 for (i = 0; i < dd->ignores_len; i++)
1186 {
1187 status = fnmatch(dd->ignores[i], il->instance, 0);
1188 if (status == 0)
1189 {
1190 if (dd->invert_match == 0)
1191 {
1192 sfree(il);
1193 return 0;
1194 }
1195 else
1196 {
1197 is_matched = 1;
1198 break;
1199 }
1200 }
1201 }
1202 if (dd->invert_match != 0 && is_matched == 0)
1203 {
1204 sfree(il);
1205 return 0;
1206 }
1207 for (ptr = il->instance; *ptr != '\0'; ptr++)
1208 {
1209 if ((*ptr > 0) && (*ptr < 32))
1210 *ptr = ' ';
1211 else if (*ptr == '/')
1212 *ptr = '_';
1213 }
1214 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1215 }
1216 else
1217 {
1218 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1219 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1220 ssnprintf (il->instance, sizeof (il->instance),
1221 "%llu", val.counter);
1222 }
1224 /* TODO: Debugging output */
1226 if (*head == NULL)
1227 *head = il;
1228 else
1229 (*tail)->next = il;
1230 *tail = il;
1232 return (0);
1233 } /* int csnmp_instance_list_add */
1235 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1236 csnmp_list_instances_t *instance_list,
1237 csnmp_table_values_t **value_table)
1238 {
1239 const data_set_t *ds;
1240 value_list_t vl = VALUE_LIST_INIT;
1242 csnmp_list_instances_t *instance_list_ptr;
1243 csnmp_table_values_t **value_table_ptr;
1245 size_t i;
1246 _Bool have_more;
1247 oid_t current_suffix;
1249 ds = plugin_get_ds (data->type);
1250 if (!ds)
1251 {
1252 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1253 return (-1);
1254 }
1255 assert (ds->ds_num == data->values_len);
1256 assert (data->values_len > 0);
1258 instance_list_ptr = instance_list;
1260 value_table_ptr = calloc (data->values_len, sizeof (*value_table_ptr));
1261 if (value_table_ptr == NULL)
1262 return (-1);
1263 for (i = 0; i < data->values_len; i++)
1264 value_table_ptr[i] = value_table[i];
1266 vl.values_len = data->values_len;
1267 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1268 if (vl.values == NULL)
1269 {
1270 ERROR ("snmp plugin: malloc failed.");
1271 sfree (value_table_ptr);
1272 return (-1);
1273 }
1275 sstrncpy (vl.host, host->name, sizeof (vl.host));
1276 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1278 vl.interval = host->interval;
1280 have_more = 1;
1281 memset (¤t_suffix, 0, sizeof (current_suffix));
1282 while (have_more)
1283 {
1284 _Bool suffix_skipped = 0;
1286 /* Determine next suffix to handle. */
1287 if (instance_list != NULL)
1288 {
1289 if (instance_list_ptr == NULL)
1290 {
1291 have_more = 0;
1292 continue;
1293 }
1295 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1296 }
1297 else /* no instance configured */
1298 {
1299 csnmp_table_values_t *ptr = value_table_ptr[0];
1300 if (ptr == NULL)
1301 {
1302 have_more = 0;
1303 continue;
1304 }
1306 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1307 }
1309 /* Update all the value_table_ptr to point at the entry with the same
1310 * trailing partial OID */
1311 for (i = 0; i < data->values_len; i++)
1312 {
1313 while ((value_table_ptr[i] != NULL)
1314 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1315 value_table_ptr[i] = value_table_ptr[i]->next;
1317 if (value_table_ptr[i] == NULL)
1318 {
1319 have_more = 0;
1320 break;
1321 }
1322 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1323 {
1324 /* This suffix is missing in the subtree. Indicate this with the
1325 * "suffix_skipped" flag and try the next instance / suffix. */
1326 suffix_skipped = 1;
1327 break;
1328 }
1329 } /* for (i = 0; i < columns; i++) */
1331 if (!have_more)
1332 break;
1334 /* Matching the values failed. Start from the beginning again. */
1335 if (suffix_skipped)
1336 {
1337 if (instance_list != NULL)
1338 instance_list_ptr = instance_list_ptr->next;
1339 else
1340 value_table_ptr[0] = value_table_ptr[0]->next;
1342 continue;
1343 }
1345 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1346 * to the same subid. instance_list_ptr is either NULL or points to the
1347 * same subid, too. */
1348 #if COLLECT_DEBUG
1349 for (i = 1; i < data->values_len; i++)
1350 {
1351 assert (value_table_ptr[i] != NULL);
1352 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1353 &value_table_ptr[i]->suffix) == 0);
1354 }
1355 assert ((instance_list_ptr == NULL)
1356 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1357 &value_table_ptr[0]->suffix) == 0));
1358 #endif
1360 sstrncpy (vl.type, data->type, sizeof (vl.type));
1362 {
1363 char temp[DATA_MAX_NAME_LEN];
1365 if (instance_list_ptr == NULL)
1366 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1367 else
1368 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1370 if (data->instance_prefix == NULL)
1371 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1372 else
1373 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1374 data->instance_prefix, temp);
1375 }
1377 for (i = 0; i < data->values_len; i++)
1378 vl.values[i] = value_table_ptr[i]->value;
1380 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1381 plugin_dispatch_values (&vl);
1383 if (instance_list != NULL)
1384 instance_list_ptr = instance_list_ptr->next;
1385 else
1386 value_table_ptr[0] = value_table_ptr[0]->next;
1387 } /* while (have_more) */
1389 sfree (vl.values);
1390 sfree (value_table_ptr);
1392 return (0);
1393 } /* int csnmp_dispatch_table */
1395 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1396 {
1397 struct snmp_pdu *req;
1398 struct snmp_pdu *res = NULL;
1399 struct variable_list *vb;
1401 const data_set_t *ds;
1403 size_t oid_list_len = data->values_len + 1;
1404 /* Holds the last OID returned by the device. We use this in the GETNEXT
1405 * request to proceed. */
1406 oid_t oid_list[oid_list_len];
1407 /* Set to false when an OID has left its subtree so we don't re-request it
1408 * again. */
1409 _Bool oid_list_todo[oid_list_len];
1411 int status;
1412 size_t i;
1414 /* `value_list_head' and `value_list_tail' implement a linked list for each
1415 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1416 * instance names. This is used to jump gaps in the table. */
1417 csnmp_list_instances_t *instance_list_head;
1418 csnmp_list_instances_t *instance_list_tail;
1419 csnmp_table_values_t **value_list_head;
1420 csnmp_table_values_t **value_list_tail;
1422 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1423 host->name, data->name);
1425 if (host->sess_handle == NULL)
1426 {
1427 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1428 return (-1);
1429 }
1431 ds = plugin_get_ds (data->type);
1432 if (!ds)
1433 {
1434 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1435 return (-1);
1436 }
1438 if (ds->ds_num != data->values_len)
1439 {
1440 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1441 data->type, ds->ds_num, data->values_len);
1442 return (-1);
1443 }
1444 assert (data->values_len > 0);
1446 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1447 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1448 if (data->instance.oid.oid_len > 0)
1449 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1450 else /* no InstanceFrom option specified. */
1451 oid_list_len--;
1453 for (i = 0; i < oid_list_len; i++)
1454 oid_list_todo[i] = 1;
1456 /* We're going to construct n linked lists, one for each "value".
1457 * value_list_head will contain pointers to the heads of these linked lists,
1458 * value_list_tail will contain pointers to the tail of the lists. */
1459 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1460 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1461 if ((value_list_head == NULL) || (value_list_tail == NULL))
1462 {
1463 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1464 sfree (value_list_head);
1465 sfree (value_list_tail);
1466 return (-1);
1467 }
1469 instance_list_head = NULL;
1470 instance_list_tail = NULL;
1472 status = 0;
1473 while (status == 0)
1474 {
1475 int oid_list_todo_num;
1477 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1478 if (req == NULL)
1479 {
1480 ERROR ("snmp plugin: snmp_pdu_create failed.");
1481 status = -1;
1482 break;
1483 }
1485 oid_list_todo_num = 0;
1486 for (i = 0; i < oid_list_len; i++)
1487 {
1488 /* Do not rerequest already finished OIDs */
1489 if (!oid_list_todo[i])
1490 continue;
1491 oid_list_todo_num++;
1492 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1493 }
1495 if (oid_list_todo_num == 0)
1496 {
1497 /* The request is still empty - so we are finished */
1498 DEBUG ("snmp plugin: all variables have left their subtree");
1499 status = 0;
1500 break;
1501 }
1503 res = NULL;
1504 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1505 if ((status != STAT_SUCCESS) || (res == NULL))
1506 {
1507 char *errstr = NULL;
1509 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1511 c_complain (LOG_ERR, &host->complaint,
1512 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1513 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1515 if (res != NULL)
1516 snmp_free_pdu (res);
1517 res = NULL;
1519 /* snmp_synch_response already freed our PDU */
1520 req = NULL;
1521 sfree (errstr);
1522 csnmp_host_close_session (host);
1524 status = -1;
1525 break;
1526 }
1528 status = 0;
1529 assert (res != NULL);
1530 c_release (LOG_INFO, &host->complaint,
1531 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1532 host->name);
1534 vb = res->variables;
1535 if (vb == NULL)
1536 {
1537 status = -1;
1538 break;
1539 }
1541 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1542 {
1543 /* Calculate value index from todo list */
1544 while ((i < oid_list_len) && !oid_list_todo[i])
1545 i++;
1547 /* An instance is configured and the res variable we process is the
1548 * instance value (last index) */
1549 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1550 {
1551 if ((vb->type == SNMP_ENDOFMIBVIEW)
1552 || (snmp_oid_ncompare (data->instance.oid.oid,
1553 data->instance.oid.oid_len,
1554 vb->name, vb->name_length,
1555 data->instance.oid.oid_len) != 0))
1556 {
1557 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1558 host->name, data->name);
1559 oid_list_todo[i] = 0;
1560 continue;
1561 }
1563 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1564 * add it to the list */
1565 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1566 res, host, data) != 0)
1567 {
1568 ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
1569 host->name);
1570 status = -1;
1571 break;
1572 }
1573 }
1574 else /* The variable we are processing is a normal value */
1575 {
1576 csnmp_table_values_t *vt;
1577 oid_t vb_name;
1578 oid_t suffix;
1579 int ret;
1581 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1583 /* Calculate the current suffix. This is later used to check that the
1584 * suffix is increasing. This also checks if we left the subtree */
1585 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1586 if (ret != 0)
1587 {
1588 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1589 "Value probably left its subtree.",
1590 host->name, data->name, i);
1591 oid_list_todo[i] = 0;
1592 continue;
1593 }
1595 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1596 * table matching algorithm will get confused. */
1597 if ((value_list_tail[i] != NULL)
1598 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1599 {
1600 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1601 "Suffix is not increasing.",
1602 host->name, data->name, i);
1603 oid_list_todo[i] = 0;
1604 continue;
1605 }
1607 vt = calloc (1, sizeof (*vt));
1608 if (vt == NULL)
1609 {
1610 ERROR ("snmp plugin: calloc failed.");
1611 status = -1;
1612 break;
1613 }
1615 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1616 data->scale, data->shift, host->name, data->name);
1617 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1618 vt->next = NULL;
1620 if (value_list_tail[i] == NULL)
1621 value_list_head[i] = vt;
1622 else
1623 value_list_tail[i]->next = vt;
1624 value_list_tail[i] = vt;
1625 }
1627 /* Copy OID to oid_list[i] */
1628 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1629 oid_list[i].oid_len = vb->name_length;
1631 } /* for (vb = res->variables ...) */
1633 if (res != NULL)
1634 snmp_free_pdu (res);
1635 res = NULL;
1636 } /* while (status == 0) */
1638 if (res != NULL)
1639 snmp_free_pdu (res);
1640 res = NULL;
1642 if (req != NULL)
1643 snmp_free_pdu (req);
1644 req = NULL;
1646 if (status == 0)
1647 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1649 /* Free all allocated variables here */
1650 while (instance_list_head != NULL)
1651 {
1652 csnmp_list_instances_t *next = instance_list_head->next;
1653 sfree (instance_list_head);
1654 instance_list_head = next;
1655 }
1657 for (i = 0; i < data->values_len; i++)
1658 {
1659 while (value_list_head[i] != NULL)
1660 {
1661 csnmp_table_values_t *next = value_list_head[i]->next;
1662 sfree (value_list_head[i]);
1663 value_list_head[i] = next;
1664 }
1665 }
1667 sfree (value_list_head);
1668 sfree (value_list_tail);
1670 return (0);
1671 } /* int csnmp_read_table */
1673 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1674 {
1675 struct snmp_pdu *req;
1676 struct snmp_pdu *res = NULL;
1677 struct variable_list *vb;
1679 const data_set_t *ds;
1680 value_list_t vl = VALUE_LIST_INIT;
1682 int status;
1683 size_t i;
1685 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1686 host->name, data->name);
1688 if (host->sess_handle == NULL)
1689 {
1690 DEBUG ("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1691 return (-1);
1692 }
1694 ds = plugin_get_ds (data->type);
1695 if (!ds)
1696 {
1697 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1698 return (-1);
1699 }
1701 if (ds->ds_num != data->values_len)
1702 {
1703 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1704 data->type, ds->ds_num, data->values_len);
1705 return (-1);
1706 }
1708 vl.values_len = ds->ds_num;
1709 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1710 if (vl.values == NULL)
1711 return (-1);
1712 for (i = 0; i < vl.values_len; i++)
1713 {
1714 if (ds->ds[i].type == DS_TYPE_COUNTER)
1715 vl.values[i].counter = 0;
1716 else
1717 vl.values[i].gauge = NAN;
1718 }
1720 sstrncpy (vl.host, host->name, sizeof (vl.host));
1721 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1722 sstrncpy (vl.type, data->type, sizeof (vl.type));
1723 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1725 vl.interval = host->interval;
1727 req = snmp_pdu_create (SNMP_MSG_GET);
1728 if (req == NULL)
1729 {
1730 ERROR ("snmp plugin: snmp_pdu_create failed.");
1731 sfree (vl.values);
1732 return (-1);
1733 }
1735 for (i = 0; i < data->values_len; i++)
1736 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1738 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1740 if ((status != STAT_SUCCESS) || (res == NULL))
1741 {
1742 char *errstr = NULL;
1744 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1745 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1746 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1748 if (res != NULL)
1749 snmp_free_pdu (res);
1751 sfree (errstr);
1752 sfree (vl.values);
1753 csnmp_host_close_session (host);
1755 return (-1);
1756 }
1759 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1760 {
1761 #if COLLECT_DEBUG
1762 char buffer[1024];
1763 snprint_variable (buffer, sizeof (buffer),
1764 vb->name, vb->name_length, vb);
1765 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1766 #endif /* COLLECT_DEBUG */
1768 for (i = 0; i < data->values_len; i++)
1769 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1770 vb->name, vb->name_length) == 0)
1771 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1772 data->scale, data->shift, host->name, data->name);
1773 } /* for (res->variables) */
1775 snmp_free_pdu (res);
1777 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1778 plugin_dispatch_values (&vl);
1779 sfree (vl.values);
1781 return (0);
1782 } /* int csnmp_read_value */
1784 static int csnmp_read_host (user_data_t *ud)
1785 {
1786 host_definition_t *host;
1787 int status;
1788 int success;
1789 int i;
1791 host = ud->data;
1793 if (host->interval == 0)
1794 host->interval = plugin_get_interval ();
1796 if (host->sess_handle == NULL)
1797 csnmp_host_open_session (host);
1799 if (host->sess_handle == NULL)
1800 return (-1);
1802 success = 0;
1803 for (i = 0; i < host->data_list_len; i++)
1804 {
1805 data_definition_t *data = host->data_list[i];
1807 if (data->is_table)
1808 status = csnmp_read_table (host, data);
1809 else
1810 status = csnmp_read_value (host, data);
1812 if (status == 0)
1813 success++;
1814 }
1816 if (success == 0)
1817 return (-1);
1819 return (0);
1820 } /* int csnmp_read_host */
1822 static int csnmp_init (void)
1823 {
1824 call_snmp_init_once ();
1826 return (0);
1827 } /* int csnmp_init */
1829 static int csnmp_shutdown (void)
1830 {
1831 data_definition_t *data_this;
1832 data_definition_t *data_next;
1834 /* When we get here, the read threads have been stopped and all the
1835 * `host_definition_t' will be freed. */
1836 DEBUG ("snmp plugin: Destroying all data definitions.");
1838 data_this = data_head;
1839 data_head = NULL;
1840 while (data_this != NULL)
1841 {
1842 data_next = data_this->next;
1844 sfree (data_this->name);
1845 sfree (data_this->type);
1846 sfree (data_this->values);
1847 sfree (data_this->ignores);
1848 sfree (data_this);
1850 data_this = data_next;
1851 }
1853 return (0);
1854 } /* int csnmp_shutdown */
1856 void module_register (void)
1857 {
1858 plugin_register_complex_config ("snmp", csnmp_config);
1859 plugin_register_init ("snmp", csnmp_init);
1860 plugin_register_shutdown ("snmp", csnmp_shutdown);
1861 } /* void module_register */
1863 /*
1864 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1865 */