2 * collectd - src/snmp.c
3 * Copyright (C) 2007-2012 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils/common/common.h"
31 #include "utils/ignorelist/ignorelist.h"
32 #include "utils_complain.h"
34 #include <net-snmp/net-snmp-config.h>
35 #include <net-snmp/net-snmp-includes.h>
40 * Private data structes
46 typedef struct oid_s oid_t;
54 typedef struct instance_s instance_t;
56 struct data_definition_s {
57 char *name; /* used to reference this from the `Collect' option */
58 char *type; /* used to find the data_set */
60 instance_t type_instance;
61 instance_t plugin_instance;
64 ignorelist_t *ignorelist;
70 struct data_definition_s *next;
75 typedef struct data_definition_s data_definition_t;
77 struct host_definition_s {
84 /* snmpv1/2 options */
87 /* snmpv3 security options */
90 size_t auth_protocol_len;
91 char *auth_passphrase;
93 size_t priv_protocol_len;
94 char *priv_passphrase;
99 c_complain_t complaint;
100 data_definition_t **data_list;
105 typedef struct host_definition_s host_definition_t;
107 /* These two types are used to cache values in `csnmp_read_table' to handle
109 struct csnmp_cell_char_s {
111 char value[DATA_MAX_NAME_LEN];
112 struct csnmp_cell_char_s *next;
114 typedef struct csnmp_cell_char_s csnmp_cell_char_t;
116 struct csnmp_cell_value_s {
119 struct csnmp_cell_value_s *next;
121 typedef struct csnmp_cell_value_s csnmp_cell_value_t;
126 OID_TYPE_TYPEINSTANCE,
127 OID_TYPE_PLUGININSTANCE,
135 static data_definition_t *data_head;
140 static int csnmp_read_host(user_data_t *ud);
145 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
146 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
147 memcpy(dst->oid, src, sizeof(*src) * n);
151 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
152 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
155 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
156 /* Make sure "src" is in "root"s subtree. */
157 if (src->oid_len <= root->oid_len)
159 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
160 /* n = */ root->oid_len) != 0)
163 memset(dst, 0, sizeof(*dst));
164 dst->oid_len = src->oid_len - root->oid_len;
165 memcpy(dst->oid, &src->oid[root->oid_len],
166 dst->oid_len * sizeof(dst->oid[0]));
170 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
172 char oid_str[MAX_OID_LEN][16];
173 char *oid_str_ptr[MAX_OID_LEN];
175 for (size_t i = 0; i < o->oid_len; i++) {
176 ssnprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
177 oid_str_ptr[i] = oid_str[i];
180 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
183 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
185 if (host->sess_handle == NULL)
188 snmp_sess_close(host->sess_handle);
189 host->sess_handle = NULL;
190 } /* }}} void csnmp_host_close_session */
192 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
194 host_definition_t *hd;
201 if (hd->name != NULL) {
202 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
205 csnmp_host_close_session(hd);
209 sfree(hd->community);
211 sfree(hd->auth_passphrase);
212 sfree(hd->priv_passphrase);
214 sfree(hd->data_list);
217 } /* }}} void csnmp_host_definition_destroy */
219 static void csnmp_data_definition_destroy(data_definition_t *dd) {
222 sfree(dd->plugin_name);
223 sfree(dd->plugin_instance.prefix);
224 sfree(dd->plugin_instance.value);
225 sfree(dd->type_instance.prefix);
226 sfree(dd->type_instance.value);
227 sfree(dd->host.prefix);
228 sfree(dd->host.value);
231 ignorelist_free(dd->ignorelist);
233 } /* void csnmp_data_definition_destroy */
235 /* Many functions to handle the configuration. {{{ */
236 /* First there are many functions which do configuration stuff. It's a big
237 * bloated and messy, I'm afraid. */
240 * Callgraph for the config stuff:
242 * +-> call_snmp_init_once
243 * +-> csnmp_config_add_data
244 * ! +-> csnmp_config_configure_data_instance
245 * ! +-> csnmp_config_add_data_values
246 * +-> csnmp_config_add_host
247 * +-> csnmp_config_add_host_version
248 * +-> csnmp_config_add_host_collect
249 * +-> csnmp_config_add_host_auth_protocol
250 * +-> csnmp_config_add_host_priv_protocol
251 * +-> csnmp_config_add_host_security_level
253 static void call_snmp_init_once(void) {
254 static int have_init;
257 init_snmp(PACKAGE_NAME);
259 } /* void call_snmp_init_once */
261 static int csnmp_config_configure_data_instance(instance_t *instance,
262 oconfig_item_t *ci) {
263 char buffer[DATA_MAX_NAME_LEN];
265 int status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
269 instance->configured = true;
271 if (strlen(buffer) == 0) {
275 instance->oid.oid_len = MAX_OID_LEN;
277 if (!read_objid(buffer, instance->oid.oid, &instance->oid.oid_len)) {
278 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
283 } /* int csnmp_config_configure_data_instance */
285 static int csnmp_config_add_data_values(data_definition_t *dd,
286 oconfig_item_t *ci) {
287 if (ci->values_num < 1) {
288 WARNING("snmp plugin: `Values' needs at least one argument.");
292 for (int i = 0; i < ci->values_num; i++)
293 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
294 WARNING("snmp plugin: `Values' needs only string argument.");
300 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
301 if (dd->values == NULL)
303 dd->values_len = (size_t)ci->values_num;
305 for (int i = 0; i < ci->values_num; i++) {
306 dd->values[i].oid_len = MAX_OID_LEN;
308 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
309 &dd->values[i].oid_len)) {
310 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
311 ci->values[i].value.string);
320 } /* int csnmp_config_configure_data_instance */
322 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
323 oconfig_item_t *ci) {
324 if (ci->values_num < 1)
327 for (int i = 0; i < ci->values_num; i++) {
328 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
329 WARNING("snmp plugin: `Ignore' needs only string argument.");
334 for (int i = 0; i < ci->values_num; ++i) {
335 if (strarray_add(&(dd->ignores), &(dd->ignores_len),
336 ci->values[i].value.string) != 0) {
337 ERROR("snmp plugin: Can't allocate memory");
338 strarray_free(dd->ignores, dd->ignores_len);
343 } /* int csnmp_config_add_data_blacklist */
345 static int csnmp_config_add_data_filter_values(data_definition_t *data,
346 oconfig_item_t *ci) {
347 if (ci->values_num < 1) {
348 WARNING("snmp plugin: `FilterValues' needs at least one argument.");
352 for (int i = 0; i < ci->values_num; i++) {
353 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
354 WARNING("snmp plugin: All arguments to `FilterValues' must be strings.");
357 ignorelist_add(data->ignorelist, ci->values[i].value.string);
361 } /* int csnmp_config_add_data_filter_values */
363 static int csnmp_config_add_data_filter_oid(data_definition_t *data,
364 oconfig_item_t *ci) {
366 char buffer[DATA_MAX_NAME_LEN];
367 int status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
371 data->filter_oid.oid_len = MAX_OID_LEN;
373 if (!read_objid(buffer, data->filter_oid.oid, &data->filter_oid.oid_len)) {
374 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
378 } /* int csnmp_config_add_data_filter_oid */
380 static int csnmp_config_add_data(oconfig_item_t *ci) {
381 data_definition_t *dd = calloc(1, sizeof(*dd));
385 int status = cf_util_get_string(ci, &dd->name);
396 dd->ignorelist = ignorelist_create(/* invert = */ 1);
397 if (dd->ignorelist == NULL) {
400 ERROR("snmp plugin: ignorelist_create() failed.");
404 dd->plugin_name = strdup("snmp");
405 if (dd->plugin_name == NULL) {
406 ERROR("snmp plugin: Can't allocate memory");
410 for (int i = 0; i < ci->children_num; i++) {
411 oconfig_item_t *option = ci->children + i;
413 if (strcasecmp("Type", option->key) == 0)
414 status = cf_util_get_string(option, &dd->type);
415 else if (strcasecmp("Table", option->key) == 0)
416 status = cf_util_get_boolean(option, &dd->is_table);
417 else if (strcasecmp("Plugin", option->key) == 0)
418 status = cf_util_get_string(option, &dd->plugin_name);
419 else if (strcasecmp("Instance", option->key) == 0) {
421 /* Instance is OID */
423 "snmp plugin: data %s: Option `Instance' is deprecated, please use "
424 "option `TypeInstanceOID'.",
427 csnmp_config_configure_data_instance(&dd->type_instance, option);
429 /* Instance is a simple string */
431 "snmp plugin: data %s: Option `Instance' is deprecated, please use "
432 "option `TypeInstance'.",
434 status = cf_util_get_string(option, &dd->type_instance.value);
436 } else if (strcasecmp("InstancePrefix", option->key) == 0) {
437 WARNING("snmp plugin: data %s: Option `InstancePrefix' is deprecated, "
438 "please use option `TypeInstancePrefix'.",
440 status = cf_util_get_string(option, &dd->type_instance.prefix);
441 } else if (strcasecmp("PluginInstance", option->key) == 0)
442 status = cf_util_get_string(option, &dd->plugin_instance.value);
443 else if (strcasecmp("TypeInstance", option->key) == 0)
444 status = cf_util_get_string(option, &dd->type_instance.value);
445 else if (strcasecmp("PluginInstanceOID", option->key) == 0)
447 csnmp_config_configure_data_instance(&dd->plugin_instance, option);
448 else if (strcasecmp("PluginInstancePrefix", option->key) == 0)
449 status = cf_util_get_string(option, &dd->plugin_instance.prefix);
450 else if (strcasecmp("TypeInstanceOID", option->key) == 0)
451 status = csnmp_config_configure_data_instance(&dd->type_instance, option);
452 else if (strcasecmp("TypeInstancePrefix", option->key) == 0)
453 status = cf_util_get_string(option, &dd->type_instance.prefix);
454 else if (strcasecmp("HostOID", option->key) == 0)
455 status = csnmp_config_configure_data_instance(&dd->host, option);
456 else if (strcasecmp("HostPrefix", option->key) == 0)
457 status = cf_util_get_string(option, &dd->host.prefix);
458 else if (strcasecmp("Values", option->key) == 0)
459 status = csnmp_config_add_data_values(dd, option);
460 else if (strcasecmp("Shift", option->key) == 0)
461 status = cf_util_get_double(option, &dd->shift);
462 else if (strcasecmp("Scale", option->key) == 0)
463 status = cf_util_get_double(option, &dd->scale);
464 else if (strcasecmp("Ignore", option->key) == 0)
465 status = csnmp_config_add_data_blacklist(dd, option);
466 else if (strcasecmp("InvertMatch", option->key) == 0)
467 status = cf_util_get_boolean(option, &dd->invert_match);
468 else if (strcasecmp("FilterOID", option->key) == 0) {
469 status = csnmp_config_add_data_filter_oid(dd, option);
470 } else if (strcasecmp("FilterValues", option->key) == 0) {
471 status = csnmp_config_add_data_filter_values(dd, option);
472 } else if (strcasecmp("FilterIgnoreSelected", option->key) == 0) {
474 status = cf_util_get_boolean(option, &t);
476 ignorelist_set_invert(dd->ignorelist, /* invert = */ !t);
478 WARNING("snmp plugin: data %s: Option `%s' not allowed here.", dd->name,
485 } /* for (ci->children) */
487 while (status == 0) {
489 /* Set type_instance to SUBID by default */
490 if (!dd->plugin_instance.configured && !dd->host.configured)
491 dd->type_instance.configured = true;
493 if (dd->plugin_instance.value && dd->plugin_instance.configured) {
495 "snmp plugin: data %s: Option `PluginInstance' will be ignored.",
498 if (dd->type_instance.value && dd->type_instance.configured) {
499 WARNING("snmp plugin: data %s: Option `TypeInstance' will be ignored.",
502 if (dd->type_instance.prefix && !dd->type_instance.configured) {
503 WARNING("snmp plugin: data %s: Option `TypeInstancePrefix' will be "
507 if (dd->plugin_instance.prefix && !dd->plugin_instance.configured) {
508 WARNING("snmp plugin: data %s: Option `PluginInstancePrefix' will be "
512 if (dd->host.prefix && !dd->host.configured) {
513 WARNING("snmp plugin: data %s: Option `HostPrefix' will be ignored.",
517 if (dd->plugin_instance.oid.oid_len > 0) {
518 WARNING("snmp plugin: data %s: Option `PluginInstanceOID' will be "
522 if (dd->type_instance.oid.oid_len > 0) {
524 "snmp plugin: data %s: Option `TypeInstanceOID' will be ignored.",
527 if (dd->type_instance.prefix) {
528 WARNING("snmp plugin: data %s: Option `TypeInstancePrefix' is ignored "
533 if (dd->plugin_instance.prefix) {
534 WARNING("snmp plugin: data %s: Option `PluginInstancePrefix' is "
536 "`Table' set to `false'.",
539 if (dd->host.prefix) {
541 "snmp plugin: data %s: Option `HostPrefix' is ignored when `Table' "
547 if (dd->type == NULL) {
548 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
552 if (dd->values == NULL) {
553 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
559 } /* while (status == 0) */
562 csnmp_data_definition_destroy(dd);
566 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
568 dd->name, dd->type, (dd->is_table) ? "true" : "false", dd->values_len);
570 DEBUG("snmp plugin: plugin_instance = %s, type_instance = %s,",
571 dd->plugin_instance.value, dd->type_instance.value);
573 DEBUG("snmp plugin: type_instance_by_oid = %s, plugin_instance_by_oid "
575 (dd->type_instance.oid.oid_len > 0)
577 : ((dd->type_instance.configured) ? "SUBID" : "false"),
578 (dd->plugin_instance.oid.oid_len > 0)
580 : ((dd->plugin_instance.configured) ? "SUBID" : "false"));
582 if (data_head == NULL)
585 data_definition_t *last;
587 while (last->next != NULL)
593 } /* int csnmp_config_add_data */
595 static int csnmp_config_add_host_version(host_definition_t *hd,
596 oconfig_item_t *ci) {
599 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
600 WARNING("snmp plugin: The `Version' config option needs exactly one number "
605 version = (int)ci->values[0].value.number;
606 if ((version < 1) || (version > 3)) {
607 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
611 hd->version = version;
614 } /* int csnmp_config_add_host_version */
616 static int csnmp_config_add_host_collect(host_definition_t *host,
617 oconfig_item_t *ci) {
618 data_definition_t *data;
619 data_definition_t **data_list;
622 if (ci->values_num < 1) {
623 WARNING("snmp plugin: `Collect' needs at least one argument.");
627 for (int i = 0; i < ci->values_num; i++)
628 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
629 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
633 data_list_len = host->data_list_len + ci->values_num;
635 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
636 if (data_list == NULL)
638 host->data_list = data_list;
640 for (int i = 0; i < ci->values_num; i++) {
641 for (data = data_head; data != NULL; data = data->next)
642 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
646 WARNING("snmp plugin: No such data configured: `%s'",
647 ci->values[i].value.string);
651 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
652 host->data_list_len, data->name);
654 host->data_list[host->data_list_len] = data;
655 host->data_list_len++;
656 } /* for (values_num) */
659 } /* int csnmp_config_add_host_collect */
661 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
662 oconfig_item_t *ci) {
666 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
670 if (strcasecmp("MD5", buffer) == 0) {
671 hd->auth_protocol = usmHMACMD5AuthProtocol;
672 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
673 } else if (strcasecmp("SHA", buffer) == 0) {
674 hd->auth_protocol = usmHMACSHA1AuthProtocol;
675 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
677 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
682 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
683 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
686 } /* int csnmp_config_add_host_auth_protocol */
688 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
689 oconfig_item_t *ci) {
693 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
697 if (strcasecmp("AES", buffer) == 0) {
698 hd->priv_protocol = usmAESPrivProtocol;
699 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
700 } else if (strcasecmp("DES", buffer) == 0) {
701 hd->priv_protocol = usmDESPrivProtocol;
702 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
704 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
709 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
710 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
713 } /* int csnmp_config_add_host_priv_protocol */
715 static int csnmp_config_add_host_security_level(host_definition_t *hd,
716 oconfig_item_t *ci) {
720 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
724 if (strcasecmp("noAuthNoPriv", buffer) == 0)
725 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
726 else if (strcasecmp("authNoPriv", buffer) == 0)
727 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
728 else if (strcasecmp("authPriv", buffer) == 0)
729 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
731 WARNING("snmp plugin: The `SecurityLevel' config option must be "
732 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
736 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
740 } /* int csnmp_config_add_host_security_level */
742 static int csnmp_config_add_host(oconfig_item_t *ci) {
743 host_definition_t *hd;
746 /* Registration stuff. */
747 cdtime_t interval = 0;
748 char cb_name[DATA_MAX_NAME_LEN];
750 hd = calloc(1, sizeof(*hd));
754 C_COMPLAIN_INIT(&hd->complaint);
756 status = cf_util_get_string(ci, &hd->name);
762 hd->sess_handle = NULL;
764 /* These mean that we have not set a timeout or retry value */
769 for (int i = 0; i < ci->children_num; i++) {
770 oconfig_item_t *option = ci->children + i;
772 if (strcasecmp("Address", option->key) == 0)
773 status = cf_util_get_string(option, &hd->address);
774 else if (strcasecmp("Community", option->key) == 0)
775 status = cf_util_get_string(option, &hd->community);
776 else if (strcasecmp("Version", option->key) == 0)
777 status = csnmp_config_add_host_version(hd, option);
778 else if (strcasecmp("Timeout", option->key) == 0)
779 status = cf_util_get_cdtime(option, &hd->timeout);
780 else if (strcasecmp("Retries", option->key) == 0)
781 status = cf_util_get_int(option, &hd->retries);
782 else if (strcasecmp("Collect", option->key) == 0)
783 status = csnmp_config_add_host_collect(hd, option);
784 else if (strcasecmp("Interval", option->key) == 0)
785 status = cf_util_get_cdtime(option, &interval);
786 else if (strcasecmp("Username", option->key) == 0)
787 status = cf_util_get_string(option, &hd->username);
788 else if (strcasecmp("AuthProtocol", option->key) == 0)
789 status = csnmp_config_add_host_auth_protocol(hd, option);
790 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
791 status = csnmp_config_add_host_priv_protocol(hd, option);
792 else if (strcasecmp("AuthPassphrase", option->key) == 0)
793 status = cf_util_get_string(option, &hd->auth_passphrase);
794 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
795 status = cf_util_get_string(option, &hd->priv_passphrase);
796 else if (strcasecmp("SecurityLevel", option->key) == 0)
797 status = csnmp_config_add_host_security_level(hd, option);
798 else if (strcasecmp("Context", option->key) == 0)
799 status = cf_util_get_string(option, &hd->context);
800 else if (strcasecmp("BulkSize", option->key) == 0)
801 status = cf_util_get_int(option, &hd->bulk_size);
804 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
811 } /* for (ci->children) */
813 while (status == 0) {
814 if (hd->address == NULL) {
815 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
819 if (hd->community == NULL && hd->version < 3) {
820 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
824 if (hd->bulk_size > 0 && hd->version < 2) {
825 WARNING("snmp plugin: Bulk transferts is only available for snmp v2 and later, host '%s' is configured as version '%d'", hd->name, hd->version );
827 if (hd->version == 3) {
828 if (hd->username == NULL) {
829 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
833 if (hd->security_level == 0) {
834 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
839 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
840 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
841 if (hd->auth_protocol == NULL) {
842 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
847 if (hd->auth_passphrase == NULL) {
848 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
854 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
855 if (hd->priv_protocol == NULL) {
856 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
861 if (hd->priv_passphrase == NULL) {
862 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
871 } /* while (status == 0) */
874 csnmp_host_definition_destroy(hd);
878 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
880 hd->name, hd->address, hd->community, hd->version);
882 ssnprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
884 status = plugin_register_complex_read(
885 /* group = */ NULL, cb_name, csnmp_read_host, interval,
888 .free_func = csnmp_host_definition_destroy,
891 ERROR("snmp plugin: Registering complex read function failed.");
896 } /* int csnmp_config_add_host */
898 static int csnmp_config(oconfig_item_t *ci) {
899 call_snmp_init_once();
901 for (int i = 0; i < ci->children_num; i++) {
902 oconfig_item_t *child = ci->children + i;
903 if (strcasecmp("Data", child->key) == 0)
904 csnmp_config_add_data(child);
905 else if (strcasecmp("Host", child->key) == 0)
906 csnmp_config_add_host(child);
908 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
910 } /* for (ci->children) */
913 } /* int csnmp_config */
915 /* }}} End of the config stuff. Now the interesting part begins */
917 static void csnmp_host_open_session(host_definition_t *host) {
918 struct snmp_session sess;
921 if (host->sess_handle != NULL)
922 csnmp_host_close_session(host);
924 snmp_sess_init(&sess);
925 sess.peername = host->address;
926 switch (host->version) {
928 sess.version = SNMP_VERSION_1;
931 sess.version = SNMP_VERSION_3;
934 sess.version = SNMP_VERSION_2c;
938 if (host->version == 3) {
939 sess.securityName = host->username;
940 sess.securityNameLen = strlen(host->username);
941 sess.securityLevel = host->security_level;
943 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
944 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
945 sess.securityAuthProto = host->auth_protocol;
946 sess.securityAuthProtoLen = host->auth_protocol_len;
947 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
948 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
949 (u_char *)host->auth_passphrase,
950 strlen(host->auth_passphrase), sess.securityAuthKey,
951 &sess.securityAuthKeyLen);
952 if (error != SNMPERR_SUCCESS) {
953 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
959 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
960 sess.securityPrivProto = host->priv_protocol;
961 sess.securityPrivProtoLen = host->priv_protocol_len;
962 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
963 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
964 (u_char *)host->priv_passphrase,
965 strlen(host->priv_passphrase), sess.securityPrivKey,
966 &sess.securityPrivKeyLen);
967 if (error != SNMPERR_SUCCESS) {
968 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
974 if (host->context != NULL) {
975 sess.contextName = host->context;
976 sess.contextNameLen = strlen(host->context);
978 } else /* SNMPv1/2 "authenticates" with community string */
980 sess.community = (u_char *)host->community;
981 sess.community_len = strlen(host->community);
984 /* Set timeout & retries, if they have been changed from the default */
985 if (host->timeout != 0) {
986 /* net-snmp expects microseconds */
987 sess.timeout = CDTIME_T_TO_US(host->timeout);
989 if (host->retries >= 0) {
990 sess.retries = host->retries;
993 /* snmp_sess_open will copy the `struct snmp_session *'. */
994 host->sess_handle = snmp_sess_open(&sess);
996 if (host->sess_handle == NULL) {
999 snmp_error(&sess, NULL, NULL, &errstr);
1001 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
1002 (errstr == NULL) ? "Unknown problem" : errstr);
1005 } /* void csnmp_host_open_session */
1007 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
1009 static value_t csnmp_value_list_to_value(const struct variable_list *vl,
1010 int type, double scale, double shift,
1011 const char *host_name,
1012 const char *data_name) {
1014 uint64_t tmp_unsigned = 0;
1015 int64_t tmp_signed = 0;
1017 /* Set to true when the original SNMP type appears to have been signed. */
1018 bool prefer_signed = 0;
1020 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
1021 (vl->type == ASN_COUNTER)
1022 #ifdef ASN_TIMETICKS
1023 || (vl->type == ASN_TIMETICKS)
1025 || (vl->type == ASN_GAUGE)) {
1026 tmp_unsigned = (uint32_t)*vl->val.integer;
1027 tmp_signed = (int32_t)*vl->val.integer;
1029 if (vl->type == ASN_INTEGER)
1032 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
1033 } else if (vl->type == ASN_COUNTER64) {
1034 tmp_unsigned = (uint32_t)vl->val.counter64->high;
1035 tmp_unsigned = tmp_unsigned << 32;
1036 tmp_unsigned += (uint32_t)vl->val.counter64->low;
1037 tmp_signed = (int64_t)tmp_unsigned;
1038 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
1039 } else if (vl->type == ASN_OCTET_STR) {
1040 /* We'll handle this later.. */
1042 char oid_buffer[1024] = {0};
1044 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
1048 if (vl->type == ASN_NULL)
1049 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
1052 WARNING("snmp plugin: I don't know the ASN type #%i "
1053 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
1054 (int)vl->type, oid_buffer,
1055 (data_name != NULL) ? data_name : "UNKNOWN",
1056 (host_name != NULL) ? host_name : "UNKNOWN");
1061 if (vl->type == ASN_OCTET_STR) {
1064 if (vl->val.string != NULL) {
1066 size_t string_length;
1068 string_length = sizeof(string) - 1;
1069 if (vl->val_len < string_length)
1070 string_length = vl->val_len;
1072 /* The strings we get from the Net-SNMP library may not be null
1073 * terminated. That is why we're using `memcpy' here and not `strcpy'.
1074 * `string_length' is set to `vl->val_len' which holds the length of the
1076 memcpy(string, vl->val.string, string_length);
1077 string[string_length] = 0;
1079 status = parse_value(string, &ret, type);
1081 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
1083 (host_name != NULL) ? host_name : "UNKNOWN",
1084 DS_TYPE_TO_STRING(type), string);
1090 case DS_TYPE_COUNTER:
1091 case DS_TYPE_DERIVE:
1092 case DS_TYPE_ABSOLUTE:
1093 memset(&ret, 0, sizeof(ret));
1101 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
1102 "data source type: %i.",
1107 } /* if (vl->type == ASN_OCTET_STR) */
1108 else if (type == DS_TYPE_COUNTER) {
1109 ret.counter = tmp_unsigned;
1110 } else if (type == DS_TYPE_GAUGE) {
1113 else if (prefer_signed)
1114 ret.gauge = (scale * tmp_signed) + shift;
1116 ret.gauge = (scale * tmp_unsigned) + shift;
1117 } else if (type == DS_TYPE_DERIVE) {
1119 ret.derive = (derive_t)tmp_signed;
1121 ret.derive = (derive_t)tmp_unsigned;
1122 } else if (type == DS_TYPE_ABSOLUTE) {
1123 ret.absolute = (absolute_t)tmp_unsigned;
1125 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1132 } /* value_t csnmp_value_list_to_value */
1134 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1135 * representation and writes it to dst. Returns zero on success and ENOMEM if
1136 * dst is not large enough to hold the string. dst is guaranteed to be
1137 * nul-terminated. */
1138 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
1139 const struct variable_list *vb,
1147 buffer_free = dst_size;
1149 for (size_t i = 0; i < vb->val_len; i++) {
1152 status = ssnprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
1153 (unsigned int)vb->val.bitstring[i]);
1154 assert(status >= 0);
1156 if (((size_t)status) >= buffer_free) /* truncated */
1158 dst[dst_size - 1] = '\0';
1160 } else /* if (status < buffer_free) */
1162 buffer_ptr += (size_t)status;
1163 buffer_free -= (size_t)status;
1168 } /* }}} int csnmp_strvbcopy_hexstring */
1170 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1171 * dst. If non-printable characters are detected, it will switch to a hex
1172 * representation of the string. Returns zero on success, EINVAL if vb does not
1173 * contain a string and ENOMEM if dst is not large enough to contain the
1175 static int csnmp_strvbcopy(char *dst, /* {{{ */
1176 const struct variable_list *vb, size_t dst_size) {
1180 if (vb->type == ASN_OCTET_STR)
1181 src = (char *)vb->val.string;
1182 else if (vb->type == ASN_BIT_STR)
1183 src = (char *)vb->val.bitstring;
1184 else if (vb->type == ASN_IPADDRESS) {
1185 return ssnprintf(dst, dst_size,
1186 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1187 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1188 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1194 num_chars = dst_size - 1;
1195 if (num_chars > vb->val_len)
1196 num_chars = vb->val_len;
1198 for (size_t i = 0; i < num_chars; i++) {
1199 /* Check for control characters. */
1200 if ((unsigned char)src[i] < 32)
1201 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1205 dst[dst_size - 1] = '\0';
1207 if (dst_size <= vb->val_len)
1211 } /* }}} int csnmp_strvbcopy */
1213 static csnmp_cell_char_t *csnmp_get_char_cell(const struct variable_list *vb,
1214 const oid_t *root_oid,
1215 const host_definition_t *hd,
1216 const data_definition_t *dd) {
1221 csnmp_cell_char_t *il = calloc(1, sizeof(*il));
1223 ERROR("snmp plugin: calloc failed.");
1229 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1231 if (csnmp_oid_suffix(&il->suffix, &vb_name, root_oid) != 0) {
1237 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1238 (vb->type == ASN_IPADDRESS)) {
1240 csnmp_strvbcopy(il->value, vb, sizeof(il->value));
1243 value_t val = csnmp_value_list_to_value(
1244 vb, DS_TYPE_COUNTER,
1245 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1246 ssnprintf(il->value, sizeof(il->value), "%" PRIu64, (uint64_t)val.counter);
1250 } /* csnmp_cell_char_t csnmp_get_char_cell */
1252 static void csnmp_cells_append(csnmp_cell_char_t **head,
1253 csnmp_cell_char_t **tail,
1254 csnmp_cell_char_t *il) {
1260 } /* void csnmp_cells_append */
1262 static bool csnmp_ignore_instance(csnmp_cell_char_t *cell,
1263 const data_definition_t *dd) {
1264 bool is_matched = 0;
1265 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1266 int status = fnmatch(dd->ignores[i], cell->value, 0);
1268 if (!dd->invert_match) {
1276 if (dd->invert_match && !is_matched) {
1280 } /* bool csnmp_ignore_instance */
1282 static void csnmp_cell_replace_reserved_chars(csnmp_cell_char_t *cell) {
1283 for (char *ptr = cell->value; *ptr != '\0'; ptr++) {
1284 if ((*ptr > 0) && (*ptr < 32))
1286 else if (*ptr == '/')
1289 } /* void csnmp_cell_replace_reserved_chars */
1291 static int csnmp_dispatch_table(host_definition_t *host,
1292 data_definition_t *data,
1293 csnmp_cell_char_t *type_instance_cells,
1294 csnmp_cell_char_t *plugin_instance_cells,
1295 csnmp_cell_char_t *hostname_cells,
1296 csnmp_cell_char_t *filter_cells,
1297 csnmp_cell_value_t **value_cells) {
1298 const data_set_t *ds;
1299 value_list_t vl = VALUE_LIST_INIT;
1301 csnmp_cell_char_t *type_instance_cell_ptr = type_instance_cells;
1302 csnmp_cell_char_t *plugin_instance_cell_ptr = plugin_instance_cells;
1303 csnmp_cell_char_t *hostname_cell_ptr = hostname_cells;
1304 csnmp_cell_char_t *filter_cell_ptr = filter_cells;
1305 csnmp_cell_value_t *value_cell_ptr[data->values_len];
1309 oid_t current_suffix;
1311 ds = plugin_get_ds(data->type);
1313 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1316 assert(ds->ds_num == data->values_len);
1317 assert(data->values_len > 0);
1319 for (i = 0; i < data->values_len; i++)
1320 value_cell_ptr[i] = value_cells[i];
1322 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1323 sstrncpy(vl.type, data->type, sizeof(vl.type));
1327 bool suffix_skipped = 0;
1329 /* Determine next suffix to handle. */
1330 if (type_instance_cells != NULL) {
1331 if (type_instance_cell_ptr == NULL) {
1336 memcpy(¤t_suffix, &type_instance_cell_ptr->suffix,
1337 sizeof(current_suffix));
1339 /* no instance configured */
1340 csnmp_cell_value_t *ptr = value_cell_ptr[0];
1346 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1350 char oid_buffer[1024] = {0};
1351 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, current_suffix.oid,
1352 current_suffix.oid_len);
1353 DEBUG("SNMP PLUGIN: SUFFIX %s", oid_buffer);
1356 /* Update plugin_instance_cell_ptr to point expected suffix */
1357 if (plugin_instance_cells != NULL) {
1358 while ((plugin_instance_cell_ptr != NULL) &&
1359 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1360 ¤t_suffix) < 0))
1361 plugin_instance_cell_ptr = plugin_instance_cell_ptr->next;
1363 if (plugin_instance_cell_ptr == NULL) {
1368 if (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1369 ¤t_suffix) > 0) {
1370 /* This suffix is missing in the subtree. Indicate this with the
1371 * "suffix_skipped" flag and try the next instance / suffix. */
1376 /* Update hostname_cell_ptr to point expected suffix */
1377 if (hostname_cells != NULL) {
1379 (hostname_cell_ptr != NULL) &&
1380 (csnmp_oid_compare(&hostname_cell_ptr->suffix, ¤t_suffix) < 0))
1381 hostname_cell_ptr = hostname_cell_ptr->next;
1383 if (hostname_cell_ptr == NULL) {
1388 if (csnmp_oid_compare(&hostname_cell_ptr->suffix, ¤t_suffix) > 0) {
1389 /* This suffix is missing in the subtree. Indicate this with the
1390 * "suffix_skipped" flag and try the next instance / suffix. */
1395 /* Update filter_cell_ptr to point expected suffix */
1396 if (filter_cells != NULL) {
1397 while ((filter_cell_ptr != NULL) &&
1398 (csnmp_oid_compare(&filter_cell_ptr->suffix, ¤t_suffix) < 0))
1399 filter_cell_ptr = filter_cell_ptr->next;
1401 if (filter_cell_ptr == NULL) {
1406 if (csnmp_oid_compare(&filter_cell_ptr->suffix, ¤t_suffix) > 0) {
1407 /* This suffix is missing in the subtree. Indicate this with the
1408 * "suffix_skipped" flag and try the next instance / suffix. */
1413 /* Update all the value_cell_ptr to point at the entry with the same
1414 * trailing partial OID */
1415 for (i = 0; i < data->values_len; i++) {
1417 (value_cell_ptr[i] != NULL) &&
1418 (csnmp_oid_compare(&value_cell_ptr[i]->suffix, ¤t_suffix) < 0))
1419 value_cell_ptr[i] = value_cell_ptr[i]->next;
1421 if (value_cell_ptr[i] == NULL) {
1424 } else if (csnmp_oid_compare(&value_cell_ptr[i]->suffix,
1425 ¤t_suffix) > 0) {
1426 /* This suffix is missing in the subtree. Indicate this with the
1427 * "suffix_skipped" flag and try the next instance / suffix. */
1431 } /* for (i = 0; i < columns; i++) */
1436 /* Matching the values failed. Start from the beginning again. */
1437 if (suffix_skipped) {
1438 if (type_instance_cells != NULL)
1439 type_instance_cell_ptr = type_instance_cell_ptr->next;
1441 value_cell_ptr[0] = value_cell_ptr[0]->next;
1446 /* if we reach this line, all value_cell_ptr[i] are non-NULL and are set
1447 * to the same subid. type_instance_cell_ptr is either NULL or points to the
1448 * same subid, too. */
1450 for (i = 1; i < data->values_len; i++) {
1451 assert(value_cell_ptr[i] != NULL);
1452 assert(csnmp_oid_compare(&value_cell_ptr[i - 1]->suffix,
1453 &value_cell_ptr[i]->suffix) == 0);
1455 assert((type_instance_cell_ptr == NULL) ||
1456 (csnmp_oid_compare(&type_instance_cell_ptr->suffix,
1457 &value_cell_ptr[0]->suffix) == 0));
1458 assert((plugin_instance_cell_ptr == NULL) ||
1459 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1460 &value_cell_ptr[0]->suffix) == 0));
1461 assert((hostname_cell_ptr == NULL) ||
1462 (csnmp_oid_compare(&hostname_cell_ptr->suffix,
1463 &value_cell_ptr[0]->suffix) == 0));
1464 assert((filter_cell_ptr == NULL) ||
1465 (csnmp_oid_compare(&filter_cell_ptr->suffix,
1466 &value_cell_ptr[0]->suffix) == 0));
1469 /* Check the value in filter column */
1470 if (filter_cell_ptr &&
1471 ignorelist_match(data->ignorelist, filter_cell_ptr->value) != 0) {
1472 if (type_instance_cells != NULL)
1473 type_instance_cell_ptr = type_instance_cell_ptr->next;
1475 value_cell_ptr[0] = value_cell_ptr[0]->next;
1481 if (data->host.configured) {
1482 char temp[DATA_MAX_NAME_LEN];
1483 if (hostname_cell_ptr == NULL)
1484 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1486 sstrncpy(temp, hostname_cell_ptr->value, sizeof(temp));
1488 if (data->host.prefix == NULL)
1489 sstrncpy(vl.host, temp, sizeof(vl.host));
1491 ssnprintf(vl.host, sizeof(vl.host), "%s%s", data->host.prefix, temp);
1493 sstrncpy(vl.host, host->name, sizeof(vl.host));
1496 /* set vl.type_instance */
1497 if (data->type_instance.configured) {
1498 char temp[DATA_MAX_NAME_LEN];
1499 if (type_instance_cell_ptr == NULL)
1500 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1502 sstrncpy(temp, type_instance_cell_ptr->value, sizeof(temp));
1504 if (data->type_instance.prefix == NULL)
1505 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1507 ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1508 data->type_instance.prefix, temp);
1509 } else if (data->type_instance.value) {
1510 sstrncpy(vl.type_instance, data->type_instance.value,
1511 sizeof(vl.type_instance));
1514 /* set vl.plugin_instance */
1515 if (data->plugin_instance.configured) {
1516 char temp[DATA_MAX_NAME_LEN];
1517 if (plugin_instance_cell_ptr == NULL)
1518 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1520 sstrncpy(temp, plugin_instance_cell_ptr->value, sizeof(temp));
1522 if (data->plugin_instance.prefix == NULL)
1523 sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
1525 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
1526 data->plugin_instance.prefix, temp);
1527 } else if (data->plugin_instance.value) {
1528 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
1529 sizeof(vl.plugin_instance));
1532 vl.values_len = data->values_len;
1533 value_t values[vl.values_len];
1536 for (i = 0; i < data->values_len; i++)
1537 vl.values[i] = value_cell_ptr[i]->value;
1539 plugin_dispatch_values(&vl);
1541 /* prevent leakage of pointer to local variable. */
1545 if (type_instance_cells != NULL)
1546 type_instance_cell_ptr = type_instance_cell_ptr->next;
1548 value_cell_ptr[0] = value_cell_ptr[0]->next;
1549 } /* while (have_more) */
1552 } /* int csnmp_dispatch_table */
1554 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1555 struct snmp_pdu *req;
1556 struct snmp_pdu *res = NULL;
1557 struct variable_list *vb;
1559 const data_set_t *ds;
1561 size_t oid_list_len = data->values_len;
1563 if (data->type_instance.oid.oid_len > 0)
1566 if (data->plugin_instance.oid.oid_len > 0)
1569 if (data->host.oid.oid_len > 0)
1572 if (data->filter_oid.oid_len > 0)
1575 /* Holds the last OID returned by the device. We use this in the GETNEXT
1576 * request to proceed. */
1577 oid_t oid_list[oid_list_len];
1578 /* Set to false when an OID has left its subtree so we don't re-request it
1580 csnmp_oid_type_t oid_list_todo[oid_list_len];
1585 /* `value_list_head' and `value_cells_tail' implement a linked list for each
1586 * value. `instance_cells_head' and `instance_cells_tail' implement a linked
1587 * list of instance names. This is used to jump gaps in the table. */
1588 csnmp_cell_char_t *type_instance_cells_head = NULL;
1589 csnmp_cell_char_t *type_instance_cells_tail = NULL;
1590 csnmp_cell_char_t *plugin_instance_cells_head = NULL;
1591 csnmp_cell_char_t *plugin_instance_cells_tail = NULL;
1592 csnmp_cell_char_t *hostname_cells_head = NULL;
1593 csnmp_cell_char_t *hostname_cells_tail = NULL;
1594 csnmp_cell_char_t *filter_cells_head = NULL;
1595 csnmp_cell_char_t *filter_cells_tail = NULL;
1596 csnmp_cell_value_t **value_cells_head;
1597 csnmp_cell_value_t **value_cells_tail;
1599 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1602 if (host->sess_handle == NULL) {
1603 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1607 ds = plugin_get_ds(data->type);
1609 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1613 if (ds->ds_num != data->values_len) {
1614 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1615 " values, but config talks "
1617 data->type, ds->ds_num, data->values_len);
1620 assert(data->values_len > 0);
1622 for (i = 0; i < data->values_len; i++)
1623 oid_list_todo[i] = OID_TYPE_VARIABLE;
1625 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1626 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1628 if (data->type_instance.oid.oid_len > 0) {
1629 memcpy(oid_list + i, &data->type_instance.oid, sizeof(oid_t));
1630 oid_list_todo[i] = OID_TYPE_TYPEINSTANCE;
1634 if (data->plugin_instance.oid.oid_len > 0) {
1635 memcpy(oid_list + i, &data->plugin_instance.oid, sizeof(oid_t));
1636 oid_list_todo[i] = OID_TYPE_PLUGININSTANCE;
1640 if (data->host.oid.oid_len > 0) {
1641 memcpy(oid_list + i, &data->host.oid, sizeof(oid_t));
1642 oid_list_todo[i] = OID_TYPE_HOST;
1646 if (data->filter_oid.oid_len > 0) {
1647 memcpy(oid_list + i, &data->filter_oid, sizeof(oid_t));
1648 oid_list_todo[i] = OID_TYPE_FILTER;
1652 /* We're going to construct n linked lists, one for each "value".
1653 * value_cells_head will contain pointers to the heads of these linked lists,
1654 * value_cells_tail will contain pointers to the tail of the lists. */
1655 value_cells_head = calloc(data->values_len, sizeof(*value_cells_head));
1656 value_cells_tail = calloc(data->values_len, sizeof(*value_cells_tail));
1657 if ((value_cells_head == NULL) || (value_cells_tail == NULL)) {
1658 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1659 sfree(value_cells_head);
1660 sfree(value_cells_tail);
1665 while (status == 0) {
1666 /* If SNMP v2 and later and bulk transfert enabled, use GETBULK PDU */
1667 if (host->version > 1 && host->bulk_size > 0) {
1668 req = snmp_pdu_create(SNMP_MSG_GETBULK);
1669 req->non_repeaters = 0;
1670 req->max_repetitions = host->bulk_size;
1672 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1675 ERROR("snmp plugin: snmp_pdu_create failed.");
1680 size_t oid_list_todo_num = 0;
1681 size_t var_idx[oid_list_len];
1682 memset(var_idx, 0, sizeof(var_idx));
1684 for (i = 0; i < oid_list_len; i++) {
1685 /* Do not rerequest already finished OIDs */
1686 if (!oid_list_todo[i])
1688 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1689 var_idx[oid_list_todo_num] = i;
1690 oid_list_todo_num++;
1693 if (oid_list_todo_num == 0) {
1694 /* The request is still empty - so we are finished */
1695 DEBUG("snmp plugin: all variables have left their subtree");
1701 if (req->command == SNMP_MSG_GETBULK) {
1702 /* In bulk mode the host will send 'max_repetitions' values per
1703 requested variable, so we need to split it per number of variable
1704 to stay 'in budget' */
1705 req->max_repetitions = floor(host->bulk_size / oid_list_todo_num);
1709 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1711 /* snmp_sess_synch_response always frees our req PDU */
1714 if ((status != STAT_SUCCESS) || (res == NULL)) {
1715 char *errstr = NULL;
1717 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1719 c_complain(LOG_ERR, &host->complaint,
1720 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1721 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1728 csnmp_host_close_session(host);
1735 assert(res != NULL);
1736 c_release(LOG_INFO, &host->complaint,
1737 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1740 vb = res->variables;
1746 if (res->errstat != SNMP_ERR_NOERROR) {
1747 if (res->errindex != 0) {
1748 /* Find the OID which caused error */
1749 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1750 vb = vb->next_variable, i++)
1754 if ((res->errindex == 0) || (vb == NULL)) {
1755 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1756 host->name, data->name, snmp_errstring(res->errstat),
1762 char oid_buffer[1024] = {0};
1763 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1765 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1766 data->name, oid_buffer, snmp_errstring(res->errstat));
1768 /* Get value index from todo list and skip OID found */
1769 assert(res->errindex <= oid_list_todo_num);
1770 i = var_idx[res->errindex - 1];
1771 assert(i < oid_list_len);
1772 oid_list_todo[i] = 0;
1779 for (vb = res->variables, j = 0; (vb != NULL);
1780 vb = vb->next_variable, j++) {
1781 /* If bulk request is active convert value index of the extra value */
1782 if (host->version > 1 && host->bulk_size > 0) {
1783 i = j % oid_list_todo_num;
1787 /* Calculate value index from todo list */
1788 while ((i < oid_list_len) && !oid_list_todo[i]) {
1792 if (i >= oid_list_len) {
1796 /* An instance is configured and the res variable we process is the
1798 if (oid_list_todo[i] == OID_TYPE_TYPEINSTANCE) {
1799 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1800 (snmp_oid_ncompare(data->type_instance.oid.oid,
1801 data->type_instance.oid.oid_len, vb->name,
1803 data->type_instance.oid.oid_len) != 0)) {
1804 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1806 host->name, data->name);
1807 oid_list_todo[i] = 0;
1811 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1812 * add it to the list */
1813 csnmp_cell_char_t *cell =
1814 csnmp_get_char_cell(vb, &data->type_instance.oid, host, data);
1816 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1822 if (csnmp_ignore_instance(cell, data)) {
1825 csnmp_cell_replace_reserved_chars(cell);
1827 DEBUG("snmp plugin: il->type_instance = `%s';", cell->value);
1828 csnmp_cells_append(&type_instance_cells_head,
1829 &type_instance_cells_tail, cell);
1831 } else if (oid_list_todo[i] == OID_TYPE_PLUGININSTANCE) {
1832 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1833 (snmp_oid_ncompare(data->plugin_instance.oid.oid,
1834 data->plugin_instance.oid.oid_len, vb->name,
1836 data->plugin_instance.oid.oid_len) != 0)) {
1837 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1839 host->name, data->name);
1840 oid_list_todo[i] = 0;
1844 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1845 * add it to the list */
1846 csnmp_cell_char_t *cell =
1847 csnmp_get_char_cell(vb, &data->plugin_instance.oid, host, data);
1849 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1855 csnmp_cell_replace_reserved_chars(cell);
1857 DEBUG("snmp plugin: il->plugin_instance = `%s';", cell->value);
1858 csnmp_cells_append(&plugin_instance_cells_head,
1859 &plugin_instance_cells_tail, cell);
1860 } else if (oid_list_todo[i] == OID_TYPE_HOST) {
1861 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1862 (snmp_oid_ncompare(data->host.oid.oid, data->host.oid.oid_len,
1863 vb->name, vb->name_length,
1864 data->host.oid.oid_len) != 0)) {
1865 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1866 host->name, data->name);
1867 oid_list_todo[i] = 0;
1871 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1872 * add it to the list */
1873 csnmp_cell_char_t *cell =
1874 csnmp_get_char_cell(vb, &data->host.oid, host, data);
1876 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1882 csnmp_cell_replace_reserved_chars(cell);
1884 DEBUG("snmp plugin: il->hostname = `%s';", cell->value);
1885 csnmp_cells_append(&hostname_cells_head, &hostname_cells_tail, cell);
1886 } else if (oid_list_todo[i] == OID_TYPE_FILTER) {
1887 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1888 (snmp_oid_ncompare(data->filter_oid.oid, data->filter_oid.oid_len,
1889 vb->name, vb->name_length,
1890 data->filter_oid.oid_len) != 0)) {
1891 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1892 host->name, data->name);
1893 oid_list_todo[i] = 0;
1897 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1898 * add it to the list */
1899 csnmp_cell_char_t *cell =
1900 csnmp_get_char_cell(vb, &data->filter_oid, host, data);
1902 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1908 csnmp_cell_replace_reserved_chars(cell);
1910 DEBUG("snmp plugin: il->filter = `%s';", cell->value);
1911 csnmp_cells_append(&filter_cells_head, &filter_cells_tail, cell);
1912 } else /* The variable we are processing is a normal value */
1914 assert(oid_list_todo[i] == OID_TYPE_VARIABLE);
1916 csnmp_cell_value_t *vt;
1921 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1923 /* Calculate the current suffix. This is later used to check that the
1924 * suffix is increasing. This also checks if we left the subtree */
1925 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1927 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1928 "Value probably left its subtree.",
1929 host->name, data->name, i);
1930 oid_list_todo[i] = 0;
1934 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1935 * our table matching algorithm will get confused. */
1936 if ((value_cells_tail[i] != NULL) &&
1937 (csnmp_oid_compare(&suffix, &value_cells_tail[i]->suffix) <= 0)) {
1938 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1939 "Suffix is not increasing.",
1940 host->name, data->name, i);
1941 oid_list_todo[i] = 0;
1945 vt = calloc(1, sizeof(*vt));
1947 ERROR("snmp plugin: calloc failed.");
1953 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1954 data->shift, host->name, data->name);
1955 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1958 if (value_cells_tail[i] == NULL)
1959 value_cells_head[i] = vt;
1961 value_cells_tail[i]->next = vt;
1962 value_cells_tail[i] = vt;
1965 /* Copy OID to oid_list[i] */
1966 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1967 oid_list[i].oid_len = vb->name_length;
1969 } /* for (vb = res->variables ...) */
1974 } /* while (status == 0) */
1981 csnmp_dispatch_table(host, data, type_instance_cells_head,
1982 plugin_instance_cells_head, hostname_cells_head,
1983 filter_cells_head, value_cells_head);
1985 /* Free all allocated variables here */
1986 while (type_instance_cells_head != NULL) {
1987 csnmp_cell_char_t *next = type_instance_cells_head->next;
1988 sfree(type_instance_cells_head);
1989 type_instance_cells_head = next;
1992 while (plugin_instance_cells_head != NULL) {
1993 csnmp_cell_char_t *next = plugin_instance_cells_head->next;
1994 sfree(plugin_instance_cells_head);
1995 plugin_instance_cells_head = next;
1998 while (hostname_cells_head != NULL) {
1999 csnmp_cell_char_t *next = hostname_cells_head->next;
2000 sfree(hostname_cells_head);
2001 hostname_cells_head = next;
2004 while (filter_cells_head != NULL) {
2005 csnmp_cell_char_t *next = filter_cells_head->next;
2006 sfree(filter_cells_head);
2007 filter_cells_head = next;
2010 for (i = 0; i < data->values_len; i++) {
2011 while (value_cells_head[i] != NULL) {
2012 csnmp_cell_value_t *next = value_cells_head[i]->next;
2013 sfree(value_cells_head[i]);
2014 value_cells_head[i] = next;
2018 sfree(value_cells_head);
2019 sfree(value_cells_tail);
2022 } /* int csnmp_read_table */
2024 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
2025 struct snmp_pdu *req;
2026 struct snmp_pdu *res = NULL;
2027 struct variable_list *vb;
2029 const data_set_t *ds;
2030 value_list_t vl = VALUE_LIST_INIT;
2035 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
2038 if (host->sess_handle == NULL) {
2039 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
2043 ds = plugin_get_ds(data->type);
2045 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
2049 if (ds->ds_num != data->values_len) {
2050 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
2051 " values, but config talks "
2053 data->type, ds->ds_num, data->values_len);
2057 vl.values_len = ds->ds_num;
2058 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
2059 if (vl.values == NULL)
2061 for (i = 0; i < vl.values_len; i++) {
2062 if (ds->ds[i].type == DS_TYPE_COUNTER)
2063 vl.values[i].counter = 0;
2065 vl.values[i].gauge = NAN;
2068 sstrncpy(vl.host, host->name, sizeof(vl.host));
2069 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
2070 sstrncpy(vl.type, data->type, sizeof(vl.type));
2071 if (data->type_instance.value)
2072 sstrncpy(vl.type_instance, data->type_instance.value,
2073 sizeof(vl.type_instance));
2074 if (data->plugin_instance.value)
2075 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
2076 sizeof(vl.plugin_instance));
2078 req = snmp_pdu_create(SNMP_MSG_GET);
2080 ERROR("snmp plugin: snmp_pdu_create failed.");
2085 for (i = 0; i < data->values_len; i++)
2086 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
2088 status = snmp_sess_synch_response(host->sess_handle, req, &res);
2090 if ((status != STAT_SUCCESS) || (res == NULL)) {
2091 char *errstr = NULL;
2093 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
2094 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
2095 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
2102 csnmp_host_close_session(host);
2107 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
2110 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
2111 DEBUG("snmp plugin: Got this variable: %s", buffer);
2112 #endif /* COLLECT_DEBUG */
2114 for (i = 0; i < data->values_len; i++)
2115 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
2116 vb->name, vb->name_length) == 0)
2118 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
2119 data->shift, host->name, data->name);
2120 } /* for (res->variables) */
2124 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
2125 plugin_dispatch_values(&vl);
2129 } /* int csnmp_read_value */
2131 static int csnmp_read_host(user_data_t *ud) {
2132 host_definition_t *host;
2139 if (host->sess_handle == NULL)
2140 csnmp_host_open_session(host);
2142 if (host->sess_handle == NULL)
2146 for (i = 0; i < host->data_list_len; i++) {
2147 data_definition_t *data = host->data_list[i];
2150 status = csnmp_read_table(host, data);
2152 status = csnmp_read_value(host, data);
2162 } /* int csnmp_read_host */
2164 static int csnmp_init(void) {
2165 call_snmp_init_once();
2168 } /* int csnmp_init */
2170 static int csnmp_shutdown(void) {
2171 data_definition_t *data_this;
2172 data_definition_t *data_next;
2174 /* When we get here, the read threads have been stopped and all the
2175 * `host_definition_t' will be freed. */
2176 DEBUG("snmp plugin: Destroying all data definitions.");
2178 data_this = data_head;
2180 while (data_this != NULL) {
2181 data_next = data_this->next;
2183 csnmp_data_definition_destroy(data_this);
2185 data_this = data_next;
2189 } /* int csnmp_shutdown */
2191 void module_register(void) {
2192 plugin_register_complex_config("snmp", csnmp_config);
2193 plugin_register_init("snmp", csnmp_init);
2194 plugin_register_shutdown("snmp", csnmp_shutdown);
2195 } /* void module_register */