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"
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_complain.h"
33 #include <net-snmp/net-snmp-config.h>
34 #include <net-snmp/net-snmp-includes.h>
36 #include <fnmatch.h>
38 /*
39 * Private data structes
40 */
41 struct oid_s {
42 oid oid[MAX_OID_LEN];
43 size_t oid_len;
44 };
45 typedef struct oid_s oid_t;
47 union instance_u {
48 char string[DATA_MAX_NAME_LEN];
49 oid_t oid;
50 };
51 typedef union instance_u instance_t;
53 struct data_definition_s {
54 char *name; /* used to reference this from the `Collect' option */
55 char *type; /* used to find the data_set */
56 _Bool is_table;
57 instance_t instance;
58 char *instance_prefix;
59 oid_t *values;
60 size_t values_len;
61 double scale;
62 double shift;
63 struct data_definition_s *next;
64 char **ignores;
65 size_t ignores_len;
66 int invert_match;
67 };
68 typedef struct data_definition_s data_definition_t;
70 struct host_definition_s {
71 char *name;
72 char *address;
73 int version;
75 /* snmpv1/2 options */
76 char *community;
78 /* snmpv3 security options */
79 char *username;
80 oid *auth_protocol;
81 size_t auth_protocol_len;
82 char *auth_passphrase;
83 oid *priv_protocol;
84 size_t priv_protocol_len;
85 char *priv_passphrase;
86 int security_level;
87 char *context;
89 void *sess_handle;
90 c_complain_t complaint;
91 cdtime_t interval;
92 data_definition_t **data_list;
93 int data_list_len;
94 };
95 typedef struct host_definition_s host_definition_t;
97 /* These two types are used to cache values in `csnmp_read_table' to handle
98 * gaps in tables. */
99 struct csnmp_list_instances_s {
100 oid_t suffix;
101 char instance[DATA_MAX_NAME_LEN];
102 struct csnmp_list_instances_s *next;
103 };
104 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
106 struct csnmp_table_values_s {
107 oid_t suffix;
108 value_t value;
109 struct csnmp_table_values_s *next;
110 };
111 typedef struct csnmp_table_values_s csnmp_table_values_t;
113 /*
114 * Private variables
115 */
116 static data_definition_t *data_head = NULL;
118 /*
119 * Prototypes
120 */
121 static int csnmp_read_host(user_data_t *ud);
123 /*
124 * Private functions
125 */
126 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
127 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
128 memcpy(dst->oid, src, sizeof(*src) * n);
129 dst->oid_len = n;
130 }
132 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
133 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
134 }
136 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
137 /* Make sure "src" is in "root"s subtree. */
138 if (src->oid_len <= root->oid_len)
139 return EINVAL;
140 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
141 /* n = */ root->oid_len) != 0)
142 return EINVAL;
144 memset(dst, 0, sizeof(*dst));
145 dst->oid_len = src->oid_len - root->oid_len;
146 memcpy(dst->oid, &src->oid[root->oid_len],
147 dst->oid_len * sizeof(dst->oid[0]));
148 return 0;
149 }
151 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
152 oid_t const *o) {
153 char oid_str[MAX_OID_LEN][16];
154 char *oid_str_ptr[MAX_OID_LEN];
156 for (size_t i = 0; i < o->oid_len; i++) {
157 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
158 oid_str_ptr[i] = oid_str[i];
159 }
161 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
162 }
164 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
165 {
166 if (host->sess_handle == NULL)
167 return;
169 snmp_sess_close(host->sess_handle);
170 host->sess_handle = NULL;
171 } /* }}} void csnmp_host_close_session */
173 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
174 {
175 host_definition_t *hd;
177 hd = arg;
179 if (hd == NULL)
180 return;
182 if (hd->name != NULL) {
183 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
184 }
186 csnmp_host_close_session(hd);
188 sfree(hd->name);
189 sfree(hd->address);
190 sfree(hd->community);
191 sfree(hd->username);
192 sfree(hd->auth_passphrase);
193 sfree(hd->priv_passphrase);
194 sfree(hd->context);
195 sfree(hd->data_list);
197 sfree(hd);
198 } /* }}} void csnmp_host_definition_destroy */
200 /* Many functions to handle the configuration. {{{ */
201 /* First there are many functions which do configuration stuff. It's a big
202 * bloated and messy, I'm afraid. */
204 /*
205 * Callgraph for the config stuff:
206 * csnmp_config
207 * +-> call_snmp_init_once
208 * +-> csnmp_config_add_data
209 * ! +-> csnmp_config_add_data_instance
210 * ! +-> csnmp_config_add_data_instance_prefix
211 * ! +-> csnmp_config_add_data_values
212 * +-> csnmp_config_add_host
213 * +-> csnmp_config_add_host_version
214 * +-> csnmp_config_add_host_collect
215 * +-> csnmp_config_add_host_auth_protocol
216 * +-> csnmp_config_add_host_priv_protocol
217 * +-> csnmp_config_add_host_security_level
218 */
219 static void call_snmp_init_once(void) {
220 static int have_init = 0;
222 if (have_init == 0)
223 init_snmp(PACKAGE_NAME);
224 have_init = 1;
225 } /* void call_snmp_init_once */
227 static int csnmp_config_add_data_instance(data_definition_t *dd,
228 oconfig_item_t *ci) {
229 char buffer[DATA_MAX_NAME_LEN];
230 int status;
232 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
233 if (status != 0)
234 return status;
236 if (dd->is_table) {
237 /* Instance is an OID */
238 dd->instance.oid.oid_len = MAX_OID_LEN;
240 if (!read_objid(buffer, dd->instance.oid.oid, &dd->instance.oid.oid_len)) {
241 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
242 return -1;
243 }
244 } else {
245 /* Instance is a simple string */
246 sstrncpy(dd->instance.string, buffer, sizeof(dd->instance.string));
247 }
249 return 0;
250 } /* int csnmp_config_add_data_instance */
252 static int csnmp_config_add_data_instance_prefix(data_definition_t *dd,
253 oconfig_item_t *ci) {
254 int status;
256 if (!dd->is_table) {
257 WARNING("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
258 "is set to `false'.",
259 dd->name);
260 return -1;
261 }
263 status = cf_util_get_string(ci, &dd->instance_prefix);
264 return status;
265 } /* int csnmp_config_add_data_instance_prefix */
267 static int csnmp_config_add_data_values(data_definition_t *dd,
268 oconfig_item_t *ci) {
269 if (ci->values_num < 1) {
270 WARNING("snmp plugin: `Values' needs at least one argument.");
271 return -1;
272 }
274 for (int i = 0; i < ci->values_num; i++)
275 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
276 WARNING("snmp plugin: `Values' needs only string argument.");
277 return -1;
278 }
280 sfree(dd->values);
281 dd->values_len = 0;
282 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
283 if (dd->values == NULL)
284 return -1;
285 dd->values_len = (size_t)ci->values_num;
287 for (int i = 0; i < ci->values_num; i++) {
288 dd->values[i].oid_len = MAX_OID_LEN;
290 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
291 &dd->values[i].oid_len)) {
292 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
293 ci->values[i].value.string);
294 free(dd->values);
295 dd->values = NULL;
296 dd->values_len = 0;
297 return -1;
298 }
299 }
301 return 0;
302 } /* int csnmp_config_add_data_instance */
304 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
305 oconfig_item_t *ci) {
306 if (ci->values_num < 1)
307 return 0;
309 for (int i = 0; i < ci->values_num; i++) {
310 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
311 WARNING("snmp plugin: `Ignore' needs only string argument.");
312 return -1;
313 }
314 }
316 dd->ignores_len = 0;
317 dd->ignores = NULL;
319 for (int i = 0; i < ci->values_num; ++i) {
320 if (strarray_add(&(dd->ignores), &(dd->ignores_len),
321 ci->values[i].value.string) != 0) {
322 ERROR("snmp plugin: Can't allocate memory");
323 strarray_free(dd->ignores, dd->ignores_len);
324 return ENOMEM;
325 }
326 }
327 return 0;
328 } /* int csnmp_config_add_data_blacklist */
330 static int csnmp_config_add_data_blacklist_match_inverted(data_definition_t *dd,
331 oconfig_item_t *ci) {
332 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
333 WARNING("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
334 return -1;
335 }
337 dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
339 return 0;
340 } /* int csnmp_config_add_data_blacklist_match_inverted */
342 static int csnmp_config_add_data(oconfig_item_t *ci) {
343 data_definition_t *dd;
344 int status = 0;
346 dd = calloc(1, sizeof(*dd));
347 if (dd == NULL)
348 return -1;
350 status = cf_util_get_string(ci, &dd->name);
351 if (status != 0) {
352 free(dd);
353 return -1;
354 }
356 dd->scale = 1.0;
357 dd->shift = 0.0;
359 for (int i = 0; i < ci->children_num; i++) {
360 oconfig_item_t *option = ci->children + i;
362 if (strcasecmp("Type", option->key) == 0)
363 status = cf_util_get_string(option, &dd->type);
364 else if (strcasecmp("Table", option->key) == 0)
365 status = cf_util_get_boolean(option, &dd->is_table);
366 else if (strcasecmp("Instance", option->key) == 0)
367 status = csnmp_config_add_data_instance(dd, option);
368 else if (strcasecmp("InstancePrefix", option->key) == 0)
369 status = csnmp_config_add_data_instance_prefix(dd, option);
370 else if (strcasecmp("Values", option->key) == 0)
371 status = csnmp_config_add_data_values(dd, option);
372 else if (strcasecmp("Shift", option->key) == 0)
373 status = cf_util_get_double(option, &dd->shift);
374 else if (strcasecmp("Scale", option->key) == 0)
375 status = cf_util_get_double(option, &dd->scale);
376 else if (strcasecmp("Ignore", option->key) == 0)
377 status = csnmp_config_add_data_blacklist(dd, option);
378 else if (strcasecmp("InvertMatch", option->key) == 0)
379 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
380 else {
381 WARNING("snmp plugin: Option `%s' not allowed here.", option->key);
382 status = -1;
383 }
385 if (status != 0)
386 break;
387 } /* for (ci->children) */
389 while (status == 0) {
390 if (dd->type == NULL) {
391 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
392 status = -1;
393 break;
394 }
395 if (dd->values == NULL) {
396 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
397 status = -1;
398 break;
399 }
401 break;
402 } /* while (status == 0) */
404 if (status != 0) {
405 sfree(dd->name);
406 sfree(dd->instance_prefix);
407 sfree(dd->values);
408 sfree(dd->ignores);
409 sfree(dd);
410 return -1;
411 }
413 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
414 "%zu }",
415 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false",
416 dd->values_len);
418 if (data_head == NULL)
419 data_head = dd;
420 else {
421 data_definition_t *last;
422 last = data_head;
423 while (last->next != NULL)
424 last = last->next;
425 last->next = dd;
426 }
428 return 0;
429 } /* int csnmp_config_add_data */
431 static int csnmp_config_add_host_version(host_definition_t *hd,
432 oconfig_item_t *ci) {
433 int version;
435 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
436 WARNING("snmp plugin: The `Version' config option needs exactly one number "
437 "argument.");
438 return -1;
439 }
441 version = (int)ci->values[0].value.number;
442 if ((version < 1) || (version > 3)) {
443 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
444 return -1;
445 }
447 hd->version = version;
449 return 0;
450 } /* int csnmp_config_add_host_address */
452 static int csnmp_config_add_host_collect(host_definition_t *host,
453 oconfig_item_t *ci) {
454 data_definition_t *data;
455 data_definition_t **data_list;
456 int data_list_len;
458 if (ci->values_num < 1) {
459 WARNING("snmp plugin: `Collect' needs at least one argument.");
460 return -1;
461 }
463 for (int i = 0; i < ci->values_num; i++)
464 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
465 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
466 return -1;
467 }
469 data_list_len = host->data_list_len + ci->values_num;
470 data_list =
471 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
472 if (data_list == NULL)
473 return -1;
474 host->data_list = data_list;
476 for (int i = 0; i < ci->values_num; i++) {
477 for (data = data_head; data != NULL; data = data->next)
478 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
479 break;
481 if (data == NULL) {
482 WARNING("snmp plugin: No such data configured: `%s'",
483 ci->values[i].value.string);
484 continue;
485 }
487 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
488 host->data_list_len, data->name);
490 host->data_list[host->data_list_len] = data;
491 host->data_list_len++;
492 } /* for (values_num) */
494 return 0;
495 } /* int csnmp_config_add_host_collect */
497 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
498 oconfig_item_t *ci) {
499 char buffer[4];
500 int status;
502 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
503 if (status != 0)
504 return status;
506 if (strcasecmp("MD5", buffer) == 0) {
507 hd->auth_protocol = usmHMACMD5AuthProtocol;
508 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
509 } else if (strcasecmp("SHA", buffer) == 0) {
510 hd->auth_protocol = usmHMACSHA1AuthProtocol;
511 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
512 } else {
513 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
514 "`SHA'.");
515 return -1;
516 }
518 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
519 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
521 return 0;
522 } /* int csnmp_config_add_host_auth_protocol */
524 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
525 oconfig_item_t *ci) {
526 char buffer[4];
527 int status;
529 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
530 if (status != 0)
531 return status;
533 if (strcasecmp("AES", buffer) == 0) {
534 hd->priv_protocol = usmAESPrivProtocol;
535 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
536 } else if (strcasecmp("DES", buffer) == 0) {
537 hd->priv_protocol = usmDESPrivProtocol;
538 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
539 } else {
540 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
541 "`DES'.");
542 return -1;
543 }
545 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
546 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
548 return 0;
549 } /* int csnmp_config_add_host_priv_protocol */
551 static int csnmp_config_add_host_security_level(host_definition_t *hd,
552 oconfig_item_t *ci) {
553 char buffer[16];
554 int status;
556 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
557 if (status != 0)
558 return status;
560 if (strcasecmp("noAuthNoPriv", buffer) == 0)
561 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
562 else if (strcasecmp("authNoPriv", buffer) == 0)
563 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
564 else if (strcasecmp("authPriv", buffer) == 0)
565 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
566 else {
567 WARNING("snmp plugin: The `SecurityLevel' config option must be "
568 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
569 return -1;
570 }
572 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
573 hd->security_level);
575 return 0;
576 } /* int csnmp_config_add_host_security_level */
578 static int csnmp_config_add_host(oconfig_item_t *ci) {
579 host_definition_t *hd;
580 int status = 0;
582 /* Registration stuff. */
583 char cb_name[DATA_MAX_NAME_LEN];
585 hd = calloc(1, sizeof(*hd));
586 if (hd == NULL)
587 return -1;
588 hd->version = 2;
589 C_COMPLAIN_INIT(&hd->complaint);
591 status = cf_util_get_string(ci, &hd->name);
592 if (status != 0) {
593 sfree(hd);
594 return status;
595 }
597 hd->sess_handle = NULL;
598 hd->interval = 0;
600 for (int i = 0; i < ci->children_num; i++) {
601 oconfig_item_t *option = ci->children + i;
602 status = 0;
604 if (strcasecmp("Address", option->key) == 0)
605 status = cf_util_get_string(option, &hd->address);
606 else if (strcasecmp("Community", option->key) == 0)
607 status = cf_util_get_string(option, &hd->community);
608 else if (strcasecmp("Version", option->key) == 0)
609 status = csnmp_config_add_host_version(hd, option);
610 else if (strcasecmp("Collect", option->key) == 0)
611 csnmp_config_add_host_collect(hd, option);
612 else if (strcasecmp("Interval", option->key) == 0)
613 cf_util_get_cdtime(option, &hd->interval);
614 else if (strcasecmp("Username", option->key) == 0)
615 status = cf_util_get_string(option, &hd->username);
616 else if (strcasecmp("AuthProtocol", option->key) == 0)
617 status = csnmp_config_add_host_auth_protocol(hd, option);
618 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
619 status = csnmp_config_add_host_priv_protocol(hd, option);
620 else if (strcasecmp("AuthPassphrase", option->key) == 0)
621 status = cf_util_get_string(option, &hd->auth_passphrase);
622 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
623 status = cf_util_get_string(option, &hd->priv_passphrase);
624 else if (strcasecmp("SecurityLevel", option->key) == 0)
625 status = csnmp_config_add_host_security_level(hd, option);
626 else if (strcasecmp("Context", option->key) == 0)
627 status = cf_util_get_string(option, &hd->context);
628 else {
629 WARNING(
630 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
631 option->key);
632 status = -1;
633 }
635 if (status != 0)
636 break;
637 } /* for (ci->children) */
639 while (status == 0) {
640 if (hd->address == NULL) {
641 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
642 status = -1;
643 break;
644 }
645 if (hd->community == NULL && hd->version < 3) {
646 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
647 status = -1;
648 break;
649 }
650 if (hd->version == 3) {
651 if (hd->username == NULL) {
652 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
653 status = -1;
654 break;
655 }
656 if (hd->security_level == 0) {
657 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
658 hd->name);
659 status = -1;
660 break;
661 }
662 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
663 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
664 if (hd->auth_protocol == NULL) {
665 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
666 hd->name);
667 status = -1;
668 break;
669 }
670 if (hd->auth_passphrase == NULL) {
671 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
672 hd->name);
673 status = -1;
674 break;
675 }
676 }
677 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
678 if (hd->priv_protocol == NULL) {
679 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
680 hd->name);
681 status = -1;
682 break;
683 }
684 if (hd->priv_passphrase == NULL) {
685 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
686 hd->name);
687 status = -1;
688 break;
689 }
690 }
691 }
693 break;
694 } /* while (status == 0) */
696 if (status != 0) {
697 csnmp_host_definition_destroy(hd);
698 return -1;
699 }
701 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
702 "= %i }",
703 hd->name, hd->address, hd->community, hd->version);
705 snprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
707 status = plugin_register_complex_read(
708 /* group = */ NULL, cb_name, csnmp_read_host, hd->interval,
709 &(user_data_t){
710 .data = hd, .free_func = csnmp_host_definition_destroy,
711 });
712 if (status != 0) {
713 ERROR("snmp plugin: Registering complex read function failed.");
714 return -1;
715 }
717 return 0;
718 } /* int csnmp_config_add_host */
720 static int csnmp_config(oconfig_item_t *ci) {
721 call_snmp_init_once();
723 for (int i = 0; i < ci->children_num; i++) {
724 oconfig_item_t *child = ci->children + i;
725 if (strcasecmp("Data", child->key) == 0)
726 csnmp_config_add_data(child);
727 else if (strcasecmp("Host", child->key) == 0)
728 csnmp_config_add_host(child);
729 else {
730 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
731 }
732 } /* for (ci->children) */
734 return 0;
735 } /* int csnmp_config */
737 /* }}} End of the config stuff. Now the interesting part begins */
739 static void csnmp_host_open_session(host_definition_t *host) {
740 struct snmp_session sess;
741 int error;
743 if (host->sess_handle != NULL)
744 csnmp_host_close_session(host);
746 snmp_sess_init(&sess);
747 sess.peername = host->address;
748 switch (host->version) {
749 case 1:
750 sess.version = SNMP_VERSION_1;
751 break;
752 case 3:
753 sess.version = SNMP_VERSION_3;
754 break;
755 default:
756 sess.version = SNMP_VERSION_2c;
757 break;
758 }
760 if (host->version == 3) {
761 sess.securityName = host->username;
762 sess.securityNameLen = strlen(host->username);
763 sess.securityLevel = host->security_level;
765 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
766 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
767 sess.securityAuthProto = host->auth_protocol;
768 sess.securityAuthProtoLen = host->auth_protocol_len;
769 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
770 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
771 (u_char *)host->auth_passphrase,
772 strlen(host->auth_passphrase), sess.securityAuthKey,
773 &sess.securityAuthKeyLen);
774 if (error != SNMPERR_SUCCESS) {
775 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
776 "(Error %d)",
777 host->name, error);
778 }
779 }
781 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
782 sess.securityPrivProto = host->priv_protocol;
783 sess.securityPrivProtoLen = host->priv_protocol_len;
784 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
785 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
786 (u_char *)host->priv_passphrase,
787 strlen(host->priv_passphrase), sess.securityPrivKey,
788 &sess.securityPrivKeyLen);
789 if (error != SNMPERR_SUCCESS) {
790 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
791 "(Error %d)",
792 host->name, error);
793 }
794 }
796 if (host->context != NULL) {
797 sess.contextName = host->context;
798 sess.contextNameLen = strlen(host->context);
799 }
800 } else /* SNMPv1/2 "authenticates" with community string */
801 {
802 sess.community = (u_char *)host->community;
803 sess.community_len = strlen(host->community);
804 }
806 /* snmp_sess_open will copy the `struct snmp_session *'. */
807 host->sess_handle = snmp_sess_open(&sess);
809 if (host->sess_handle == NULL) {
810 char *errstr = NULL;
812 snmp_error(&sess, NULL, NULL, &errstr);
814 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
815 (errstr == NULL) ? "Unknown problem" : errstr);
816 sfree(errstr);
817 }
818 } /* void csnmp_host_open_session */
820 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
821 */
822 static value_t csnmp_value_list_to_value(struct variable_list *vl, int type,
823 double scale, double shift,
824 const char *host_name,
825 const char *data_name) {
826 value_t ret;
827 uint64_t tmp_unsigned = 0;
828 int64_t tmp_signed = 0;
829 _Bool defined = 1;
830 /* Set to true when the original SNMP type appears to have been signed. */
831 _Bool prefer_signed = 0;
833 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
834 (vl->type == ASN_COUNTER)
835 #ifdef ASN_TIMETICKS
836 || (vl->type == ASN_TIMETICKS)
837 #endif
838 || (vl->type == ASN_GAUGE)) {
839 tmp_unsigned = (uint32_t)*vl->val.integer;
840 tmp_signed = (int32_t)*vl->val.integer;
842 if (vl->type == ASN_INTEGER)
843 prefer_signed = 1;
845 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
846 } else if (vl->type == ASN_COUNTER64) {
847 tmp_unsigned = (uint32_t)vl->val.counter64->high;
848 tmp_unsigned = tmp_unsigned << 32;
849 tmp_unsigned += (uint32_t)vl->val.counter64->low;
850 tmp_signed = (int64_t)tmp_unsigned;
851 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
852 } else if (vl->type == ASN_OCTET_STR) {
853 /* We'll handle this later.. */
854 } else {
855 char oid_buffer[1024] = {0};
857 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
858 vl->name_length);
860 #ifdef ASN_NULL
861 if (vl->type == ASN_NULL)
862 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
863 else
864 #endif
865 WARNING("snmp plugin: I don't know the ASN type #%i "
866 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
867 (int)vl->type, oid_buffer,
868 (data_name != NULL) ? data_name : "UNKNOWN",
869 (host_name != NULL) ? host_name : "UNKNOWN");
871 defined = 0;
872 }
874 if (vl->type == ASN_OCTET_STR) {
875 int status = -1;
877 if (vl->val.string != NULL) {
878 char string[64];
879 size_t string_length;
881 string_length = sizeof(string) - 1;
882 if (vl->val_len < string_length)
883 string_length = vl->val_len;
885 /* The strings we get from the Net-SNMP library may not be null
886 * terminated. That is why we're using `memcpy' here and not `strcpy'.
887 * `string_length' is set to `vl->val_len' which holds the length of the
888 * string. -octo */
889 memcpy(string, vl->val.string, string_length);
890 string[string_length] = 0;
892 status = parse_value(string, &ret, type);
893 if (status != 0) {
894 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
895 "as %s failed: %s",
896 (host_name != NULL) ? host_name : "UNKNOWN",
897 DS_TYPE_TO_STRING(type), string);
898 }
899 }
901 if (status != 0) {
902 switch (type) {
903 case DS_TYPE_COUNTER:
904 case DS_TYPE_DERIVE:
905 case DS_TYPE_ABSOLUTE:
906 memset(&ret, 0, sizeof(ret));
907 break;
909 case DS_TYPE_GAUGE:
910 ret.gauge = NAN;
911 break;
913 default:
914 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
915 "data source type: %i.",
916 type);
917 ret.gauge = NAN;
918 }
919 }
920 } /* if (vl->type == ASN_OCTET_STR) */
921 else if (type == DS_TYPE_COUNTER) {
922 ret.counter = tmp_unsigned;
923 } else if (type == DS_TYPE_GAUGE) {
924 if (!defined)
925 ret.gauge = NAN;
926 else if (prefer_signed)
927 ret.gauge = (scale * tmp_signed) + shift;
928 else
929 ret.gauge = (scale * tmp_unsigned) + shift;
930 } else if (type == DS_TYPE_DERIVE) {
931 if (prefer_signed)
932 ret.derive = (derive_t)tmp_signed;
933 else
934 ret.derive = (derive_t)tmp_unsigned;
935 } else if (type == DS_TYPE_ABSOLUTE) {
936 ret.absolute = (absolute_t)tmp_unsigned;
937 } else {
938 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
939 "type: %i.",
940 type);
941 ret.gauge = NAN;
942 }
944 return ret;
945 } /* value_t csnmp_value_list_to_value */
947 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
948 * representation and writes it to dst. Returns zero on success and ENOMEM if
949 * dst is not large enough to hold the string. dst is guaranteed to be
950 * nul-terminated. */
951 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
952 const struct variable_list *vb,
953 size_t dst_size) {
954 char *buffer_ptr;
955 size_t buffer_free;
957 dst[0] = 0;
959 buffer_ptr = dst;
960 buffer_free = dst_size;
962 for (size_t i = 0; i < vb->val_len; i++) {
963 int status;
965 status = snprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
966 (unsigned int)vb->val.bitstring[i]);
967 assert(status >= 0);
969 if (((size_t)status) >= buffer_free) /* truncated */
970 {
971 dst[dst_size - 1] = 0;
972 return ENOMEM;
973 } else /* if (status < buffer_free) */
974 {
975 buffer_ptr += (size_t)status;
976 buffer_free -= (size_t)status;
977 }
978 }
980 return 0;
981 } /* }}} int csnmp_strvbcopy_hexstring */
983 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
984 * dst. If non-printable characters are detected, it will switch to a hex
985 * representation of the string. Returns zero on success, EINVAL if vb does not
986 * contain a string and ENOMEM if dst is not large enough to contain the
987 * string. */
988 static int csnmp_strvbcopy(char *dst, /* {{{ */
989 const struct variable_list *vb, size_t dst_size) {
990 char *src;
991 size_t num_chars;
993 if (vb->type == ASN_OCTET_STR)
994 src = (char *)vb->val.string;
995 else if (vb->type == ASN_BIT_STR)
996 src = (char *)vb->val.bitstring;
997 else if (vb->type == ASN_IPADDRESS) {
998 return snprintf(dst, dst_size,
999 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1000 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1001 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1002 } else {
1003 dst[0] = 0;
1004 return EINVAL;
1005 }
1007 num_chars = dst_size - 1;
1008 if (num_chars > vb->val_len)
1009 num_chars = vb->val_len;
1011 for (size_t i = 0; i < num_chars; i++) {
1012 /* Check for control characters. */
1013 if ((unsigned char)src[i] < 32)
1014 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1015 dst[i] = src[i];
1016 }
1017 dst[num_chars] = 0;
1018 dst[dst_size - 1] = 0;
1020 if (dst_size <= vb->val_len)
1021 return ENOMEM;
1023 return 0;
1024 } /* }}} int csnmp_strvbcopy */
1026 static int csnmp_instance_list_add(csnmp_list_instances_t **head,
1027 csnmp_list_instances_t **tail,
1028 const struct snmp_pdu *res,
1029 const host_definition_t *hd,
1030 const data_definition_t *dd) {
1031 csnmp_list_instances_t *il;
1032 struct variable_list *vb;
1033 oid_t vb_name;
1034 int status;
1035 uint32_t is_matched;
1037 /* Set vb on the last variable */
1038 for (vb = res->variables; (vb != NULL) && (vb->next_variable != NULL);
1039 vb = vb->next_variable)
1040 /* do nothing */;
1041 if (vb == NULL)
1042 return -1;
1044 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1046 il = calloc(1, sizeof(*il));
1047 if (il == NULL) {
1048 ERROR("snmp plugin: calloc failed.");
1049 return -1;
1050 }
1051 il->next = NULL;
1053 status = csnmp_oid_suffix(&il->suffix, &vb_name, &dd->instance.oid);
1054 if (status != 0) {
1055 sfree(il);
1056 return status;
1057 }
1059 /* Get instance name */
1060 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1061 (vb->type == ASN_IPADDRESS)) {
1062 char *ptr;
1064 csnmp_strvbcopy(il->instance, vb, sizeof(il->instance));
1065 is_matched = 0;
1066 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1067 status = fnmatch(dd->ignores[i], il->instance, 0);
1068 if (status == 0) {
1069 if (dd->invert_match == 0) {
1070 sfree(il);
1071 return 0;
1072 } else {
1073 is_matched = 1;
1074 break;
1075 }
1076 }
1077 }
1078 if (dd->invert_match != 0 && is_matched == 0) {
1079 sfree(il);
1080 return 0;
1081 }
1082 for (ptr = il->instance; *ptr != '\0'; ptr++) {
1083 if ((*ptr > 0) && (*ptr < 32))
1084 *ptr = ' ';
1085 else if (*ptr == '/')
1086 *ptr = '_';
1087 }
1088 DEBUG("snmp plugin: il->instance = `%s';", il->instance);
1089 } else {
1090 value_t val = csnmp_value_list_to_value(
1091 vb, DS_TYPE_COUNTER,
1092 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1093 snprintf(il->instance, sizeof(il->instance), "%llu", val.counter);
1094 }
1096 /* TODO: Debugging output */
1098 if (*head == NULL)
1099 *head = il;
1100 else
1101 (*tail)->next = il;
1102 *tail = il;
1104 return 0;
1105 } /* int csnmp_instance_list_add */
1107 static int csnmp_dispatch_table(host_definition_t *host,
1108 data_definition_t *data,
1109 csnmp_list_instances_t *instance_list,
1110 csnmp_table_values_t **value_table) {
1111 const data_set_t *ds;
1112 value_list_t vl = VALUE_LIST_INIT;
1114 csnmp_list_instances_t *instance_list_ptr;
1115 csnmp_table_values_t *value_table_ptr[data->values_len];
1117 size_t i;
1118 _Bool have_more;
1119 oid_t current_suffix;
1121 ds = plugin_get_ds(data->type);
1122 if (!ds) {
1123 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1124 return -1;
1125 }
1126 assert(ds->ds_num == data->values_len);
1127 assert(data->values_len > 0);
1129 instance_list_ptr = instance_list;
1131 for (i = 0; i < data->values_len; i++)
1132 value_table_ptr[i] = value_table[i];
1134 sstrncpy(vl.host, host->name, sizeof(vl.host));
1135 sstrncpy(vl.plugin, "snmp", sizeof(vl.plugin));
1137 vl.interval = host->interval;
1139 have_more = 1;
1140 while (have_more) {
1141 _Bool suffix_skipped = 0;
1143 /* Determine next suffix to handle. */
1144 if (instance_list != NULL) {
1145 if (instance_list_ptr == NULL) {
1146 have_more = 0;
1147 continue;
1148 }
1150 memcpy(¤t_suffix, &instance_list_ptr->suffix,
1151 sizeof(current_suffix));
1152 } else {
1153 /* no instance configured */
1154 csnmp_table_values_t *ptr = value_table_ptr[0];
1155 if (ptr == NULL) {
1156 have_more = 0;
1157 continue;
1158 }
1160 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1161 }
1163 /* Update all the value_table_ptr to point at the entry with the same
1164 * trailing partial OID */
1165 for (i = 0; i < data->values_len; i++) {
1166 while (
1167 (value_table_ptr[i] != NULL) &&
1168 (csnmp_oid_compare(&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1169 value_table_ptr[i] = value_table_ptr[i]->next;
1171 if (value_table_ptr[i] == NULL) {
1172 have_more = 0;
1173 break;
1174 } else if (csnmp_oid_compare(&value_table_ptr[i]->suffix,
1175 ¤t_suffix) > 0) {
1176 /* This suffix is missing in the subtree. Indicate this with the
1177 * "suffix_skipped" flag and try the next instance / suffix. */
1178 suffix_skipped = 1;
1179 break;
1180 }
1181 } /* for (i = 0; i < columns; i++) */
1183 if (!have_more)
1184 break;
1186 /* Matching the values failed. Start from the beginning again. */
1187 if (suffix_skipped) {
1188 if (instance_list != NULL)
1189 instance_list_ptr = instance_list_ptr->next;
1190 else
1191 value_table_ptr[0] = value_table_ptr[0]->next;
1193 continue;
1194 }
1196 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1197 * to the same subid. instance_list_ptr is either NULL or points to the
1198 * same subid, too. */
1199 #if COLLECT_DEBUG
1200 for (i = 1; i < data->values_len; i++) {
1201 assert(value_table_ptr[i] != NULL);
1202 assert(csnmp_oid_compare(&value_table_ptr[i - 1]->suffix,
1203 &value_table_ptr[i]->suffix) == 0);
1204 }
1205 assert((instance_list_ptr == NULL) ||
1206 (csnmp_oid_compare(&instance_list_ptr->suffix,
1207 &value_table_ptr[0]->suffix) == 0));
1208 #endif
1210 sstrncpy(vl.type, data->type, sizeof(vl.type));
1212 {
1213 char temp[DATA_MAX_NAME_LEN];
1215 if (instance_list_ptr == NULL)
1216 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1217 else
1218 sstrncpy(temp, instance_list_ptr->instance, sizeof(temp));
1220 if (data->instance_prefix == NULL)
1221 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1222 else
1223 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1224 data->instance_prefix, temp);
1225 }
1227 vl.values_len = data->values_len;
1228 value_t values[vl.values_len];
1229 vl.values = values;
1231 for (i = 0; i < data->values_len; i++)
1232 vl.values[i] = value_table_ptr[i]->value;
1234 /* If we get here `vl.type_instance' and all `vl.values' have been set
1235 * vl.type_instance can be empty, i.e. a blank port description on a
1236 * switch if you're using IF-MIB::ifDescr as Instance.
1237 */
1238 if (vl.type_instance[0] != '\0')
1239 plugin_dispatch_values(&vl);
1241 /* prevent leakage of pointer to local variable. */
1242 vl.values_len = 0;
1243 vl.values = NULL;
1245 if (instance_list != NULL)
1246 instance_list_ptr = instance_list_ptr->next;
1247 else
1248 value_table_ptr[0] = value_table_ptr[0]->next;
1249 } /* while (have_more) */
1251 return (0);
1252 } /* int csnmp_dispatch_table */
1254 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1255 struct snmp_pdu *req;
1256 struct snmp_pdu *res = NULL;
1257 struct variable_list *vb;
1259 const data_set_t *ds;
1261 size_t oid_list_len = data->values_len + 1;
1262 /* Holds the last OID returned by the device. We use this in the GETNEXT
1263 * request to proceed. */
1264 oid_t oid_list[oid_list_len];
1265 /* Set to false when an OID has left its subtree so we don't re-request it
1266 * again. */
1267 _Bool oid_list_todo[oid_list_len];
1269 int status;
1270 size_t i;
1272 /* `value_list_head' and `value_list_tail' implement a linked list for each
1273 * value. `instance_list_head' and `instance_list_tail' implement a linked
1274 * list of
1275 * instance names. This is used to jump gaps in the table. */
1276 csnmp_list_instances_t *instance_list_head;
1277 csnmp_list_instances_t *instance_list_tail;
1278 csnmp_table_values_t **value_list_head;
1279 csnmp_table_values_t **value_list_tail;
1281 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1282 data->name);
1284 if (host->sess_handle == NULL) {
1285 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1286 return -1;
1287 }
1289 ds = plugin_get_ds(data->type);
1290 if (!ds) {
1291 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1292 return -1;
1293 }
1295 if (ds->ds_num != data->values_len) {
1296 ERROR("snmp plugin: DataSet `%s' requires %zu values, but config talks "
1297 "about %zu",
1298 data->type, ds->ds_num, data->values_len);
1299 return -1;
1300 }
1301 assert(data->values_len > 0);
1303 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1304 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1305 if (data->instance.oid.oid_len > 0)
1306 memcpy(oid_list + data->values_len, &data->instance.oid, sizeof(oid_t));
1307 else /* no InstanceFrom option specified. */
1308 oid_list_len--;
1310 for (i = 0; i < oid_list_len; i++)
1311 oid_list_todo[i] = 1;
1313 /* We're going to construct n linked lists, one for each "value".
1314 * value_list_head will contain pointers to the heads of these linked lists,
1315 * value_list_tail will contain pointers to the tail of the lists. */
1316 value_list_head = calloc(data->values_len, sizeof(*value_list_head));
1317 value_list_tail = calloc(data->values_len, sizeof(*value_list_tail));
1318 if ((value_list_head == NULL) || (value_list_tail == NULL)) {
1319 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1320 sfree(value_list_head);
1321 sfree(value_list_tail);
1322 return -1;
1323 }
1325 instance_list_head = NULL;
1326 instance_list_tail = NULL;
1328 status = 0;
1329 while (status == 0) {
1330 int oid_list_todo_num;
1332 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1333 if (req == NULL) {
1334 ERROR("snmp plugin: snmp_pdu_create failed.");
1335 status = -1;
1336 break;
1337 }
1339 oid_list_todo_num = 0;
1340 for (i = 0; i < oid_list_len; i++) {
1341 /* Do not rerequest already finished OIDs */
1342 if (!oid_list_todo[i])
1343 continue;
1344 oid_list_todo_num++;
1345 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1346 }
1348 if (oid_list_todo_num == 0) {
1349 /* The request is still empty - so we are finished */
1350 DEBUG("snmp plugin: all variables have left their subtree");
1351 status = 0;
1352 break;
1353 }
1355 res = NULL;
1356 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1357 if ((status != STAT_SUCCESS) || (res == NULL)) {
1358 char *errstr = NULL;
1360 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1362 c_complain(LOG_ERR, &host->complaint,
1363 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1364 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1366 if (res != NULL)
1367 snmp_free_pdu(res);
1368 res = NULL;
1370 /* snmp_synch_response already freed our PDU */
1371 req = NULL;
1372 sfree(errstr);
1373 csnmp_host_close_session(host);
1375 status = -1;
1376 break;
1377 }
1379 status = 0;
1380 assert(res != NULL);
1381 c_release(LOG_INFO, &host->complaint,
1382 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1383 host->name);
1385 vb = res->variables;
1386 if (vb == NULL) {
1387 status = -1;
1388 break;
1389 }
1391 if (res->errstat != SNMP_ERR_NOERROR) {
1392 if (res->errindex != 0) {
1393 /* Find the OID which caused error */
1394 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1395 vb = vb->next_variable, i++)
1396 /* do nothing */;
1397 }
1399 if ((res->errindex == 0) || (vb == NULL)) {
1400 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1401 host->name, data->name, snmp_errstring(res->errstat),
1402 res->errstat);
1403 status = -1;
1404 break;
1405 }
1407 char oid_buffer[1024] = {0};
1408 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1409 vb->name_length);
1410 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1411 data->name, oid_buffer, snmp_errstring(res->errstat));
1413 /* Calculate value index from todo list and skip OID found */
1414 i = 0;
1415 size_t j = 1;
1416 for (;;) {
1417 while ((i < oid_list_len) && !oid_list_todo[i])
1418 i++;
1420 if (j == res->errindex)
1421 break;
1423 i++;
1424 j++;
1425 }
1427 assert(i < oid_list_len);
1428 oid_list_todo[i] = 0;
1430 snmp_free_pdu(res);
1431 res = NULL;
1432 continue;
1433 }
1435 for (vb = res->variables, i = 0; (vb != NULL);
1436 vb = vb->next_variable, i++) {
1437 /* Calculate value index from todo list */
1438 while ((i < oid_list_len) && !oid_list_todo[i])
1439 i++;
1441 /* An instance is configured and the res variable we process is the
1442 * instance value (last index) */
1443 if ((data->instance.oid.oid_len > 0) && (i == data->values_len)) {
1444 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1445 (snmp_oid_ncompare(
1446 data->instance.oid.oid, data->instance.oid.oid_len, vb->name,
1447 vb->name_length, data->instance.oid.oid_len) != 0)) {
1448 DEBUG("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1449 host->name, data->name);
1450 oid_list_todo[i] = 0;
1451 continue;
1452 }
1454 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1455 * add it to the list */
1456 if (csnmp_instance_list_add(&instance_list_head, &instance_list_tail,
1457 res, host, data) != 0) {
1458 ERROR("snmp plugin: host %s: csnmp_instance_list_add failed.",
1459 host->name);
1460 status = -1;
1461 break;
1462 }
1463 } else /* The variable we are processing is a normal value */
1464 {
1465 csnmp_table_values_t *vt;
1466 oid_t vb_name;
1467 oid_t suffix;
1468 int ret;
1470 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1472 /* Calculate the current suffix. This is later used to check that the
1473 * suffix is increasing. This also checks if we left the subtree */
1474 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1475 if (ret != 0) {
1476 DEBUG("snmp plugin: host = %s; data = %s; i = %zu; "
1477 "Value probably left its subtree.",
1478 host->name, data->name, i);
1479 oid_list_todo[i] = 0;
1480 continue;
1481 }
1483 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1484 * our
1485 * table matching algorithm will get confused. */
1486 if ((value_list_tail[i] != NULL) &&
1487 (csnmp_oid_compare(&suffix, &value_list_tail[i]->suffix) <= 0)) {
1488 DEBUG("snmp plugin: host = %s; data = %s; i = %zu; "
1489 "Suffix is not increasing.",
1490 host->name, data->name, i);
1491 oid_list_todo[i] = 0;
1492 continue;
1493 }
1495 vt = calloc(1, sizeof(*vt));
1496 if (vt == NULL) {
1497 ERROR("snmp plugin: calloc failed.");
1498 status = -1;
1499 break;
1500 }
1502 vt->value =
1503 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1504 data->shift, host->name, data->name);
1505 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1506 vt->next = NULL;
1508 if (value_list_tail[i] == NULL)
1509 value_list_head[i] = vt;
1510 else
1511 value_list_tail[i]->next = vt;
1512 value_list_tail[i] = vt;
1513 }
1515 /* Copy OID to oid_list[i] */
1516 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1517 oid_list[i].oid_len = vb->name_length;
1519 } /* for (vb = res->variables ...) */
1521 if (res != NULL)
1522 snmp_free_pdu(res);
1523 res = NULL;
1524 } /* while (status == 0) */
1526 if (res != NULL)
1527 snmp_free_pdu(res);
1528 res = NULL;
1530 if (req != NULL)
1531 snmp_free_pdu(req);
1532 req = NULL;
1534 if (status == 0)
1535 csnmp_dispatch_table(host, data, instance_list_head, value_list_head);
1537 /* Free all allocated variables here */
1538 while (instance_list_head != NULL) {
1539 csnmp_list_instances_t *next = instance_list_head->next;
1540 sfree(instance_list_head);
1541 instance_list_head = next;
1542 }
1544 for (i = 0; i < data->values_len; i++) {
1545 while (value_list_head[i] != NULL) {
1546 csnmp_table_values_t *next = value_list_head[i]->next;
1547 sfree(value_list_head[i]);
1548 value_list_head[i] = next;
1549 }
1550 }
1552 sfree(value_list_head);
1553 sfree(value_list_tail);
1555 return 0;
1556 } /* int csnmp_read_table */
1558 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1559 struct snmp_pdu *req;
1560 struct snmp_pdu *res = NULL;
1561 struct variable_list *vb;
1563 const data_set_t *ds;
1564 value_list_t vl = VALUE_LIST_INIT;
1566 int status;
1567 size_t i;
1569 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
1570 data->name);
1572 if (host->sess_handle == NULL) {
1573 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1574 return -1;
1575 }
1577 ds = plugin_get_ds(data->type);
1578 if (!ds) {
1579 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1580 return -1;
1581 }
1583 if (ds->ds_num != data->values_len) {
1584 ERROR("snmp plugin: DataSet `%s' requires %zu values, but config talks "
1585 "about %zu",
1586 data->type, ds->ds_num, data->values_len);
1587 return -1;
1588 }
1590 vl.values_len = ds->ds_num;
1591 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
1592 if (vl.values == NULL)
1593 return -1;
1594 for (i = 0; i < vl.values_len; i++) {
1595 if (ds->ds[i].type == DS_TYPE_COUNTER)
1596 vl.values[i].counter = 0;
1597 else
1598 vl.values[i].gauge = NAN;
1599 }
1601 sstrncpy(vl.host, host->name, sizeof(vl.host));
1602 sstrncpy(vl.plugin, "snmp", sizeof(vl.plugin));
1603 sstrncpy(vl.type, data->type, sizeof(vl.type));
1604 sstrncpy(vl.type_instance, data->instance.string, sizeof(vl.type_instance));
1606 vl.interval = host->interval;
1608 req = snmp_pdu_create(SNMP_MSG_GET);
1609 if (req == NULL) {
1610 ERROR("snmp plugin: snmp_pdu_create failed.");
1611 sfree(vl.values);
1612 return -1;
1613 }
1615 for (i = 0; i < data->values_len; i++)
1616 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
1618 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1620 if ((status != STAT_SUCCESS) || (res == NULL)) {
1621 char *errstr = NULL;
1623 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1624 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1625 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1627 if (res != NULL)
1628 snmp_free_pdu(res);
1630 sfree(errstr);
1631 sfree(vl.values);
1632 csnmp_host_close_session(host);
1634 return -1;
1635 }
1637 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
1638 #if COLLECT_DEBUG
1639 char buffer[1024];
1640 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
1641 DEBUG("snmp plugin: Got this variable: %s", buffer);
1642 #endif /* COLLECT_DEBUG */
1644 for (i = 0; i < data->values_len; i++)
1645 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
1646 vb->name, vb->name_length) == 0)
1647 vl.values[i] =
1648 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1649 data->shift, host->name, data->name);
1650 } /* for (res->variables) */
1652 snmp_free_pdu(res);
1654 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
1655 plugin_dispatch_values(&vl);
1656 sfree(vl.values);
1658 return 0;
1659 } /* int csnmp_read_value */
1661 static int csnmp_read_host(user_data_t *ud) {
1662 host_definition_t *host;
1663 int status;
1664 int success;
1665 int i;
1667 host = ud->data;
1669 if (host->interval == 0)
1670 host->interval = plugin_get_interval();
1672 if (host->sess_handle == NULL)
1673 csnmp_host_open_session(host);
1675 if (host->sess_handle == NULL)
1676 return -1;
1678 success = 0;
1679 for (i = 0; i < host->data_list_len; i++) {
1680 data_definition_t *data = host->data_list[i];
1682 if (data->is_table)
1683 status = csnmp_read_table(host, data);
1684 else
1685 status = csnmp_read_value(host, data);
1687 if (status == 0)
1688 success++;
1689 }
1691 if (success == 0)
1692 return -1;
1694 return 0;
1695 } /* int csnmp_read_host */
1697 static int csnmp_init(void) {
1698 call_snmp_init_once();
1700 return 0;
1701 } /* int csnmp_init */
1703 static int csnmp_shutdown(void) {
1704 data_definition_t *data_this;
1705 data_definition_t *data_next;
1707 /* When we get here, the read threads have been stopped and all the
1708 * `host_definition_t' will be freed. */
1709 DEBUG("snmp plugin: Destroying all data definitions.");
1711 data_this = data_head;
1712 data_head = NULL;
1713 while (data_this != NULL) {
1714 data_next = data_this->next;
1716 sfree(data_this->name);
1717 sfree(data_this->type);
1718 sfree(data_this->values);
1719 sfree(data_this->ignores);
1720 sfree(data_this);
1722 data_this = data_next;
1723 }
1725 return 0;
1726 } /* int csnmp_shutdown */
1728 void module_register(void) {
1729 plugin_register_complex_config("snmp", csnmp_config);
1730 plugin_register_init("snmp", csnmp_init);
1731 plugin_register_shutdown("snmp", csnmp_shutdown);
1732 } /* void module_register */