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>
31 #include "utils_complain.h"
32 #include "utils_ignorelist.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;
101 data_definition_t **data_list;
104 typedef struct host_definition_s host_definition_t;
106 /* These two types are used to cache values in `csnmp_read_table' to handle
108 struct csnmp_cell_char_s {
110 char value[DATA_MAX_NAME_LEN];
111 struct csnmp_cell_char_s *next;
113 typedef struct csnmp_cell_char_s csnmp_cell_char_t;
115 struct csnmp_cell_value_s {
118 struct csnmp_cell_value_s *next;
120 typedef struct csnmp_cell_value_s csnmp_cell_value_t;
125 OID_TYPE_TYPEINSTANCE,
126 OID_TYPE_PLUGININSTANCE,
134 static data_definition_t *data_head;
139 static int csnmp_read_host(user_data_t *ud);
144 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
145 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
146 memcpy(dst->oid, src, sizeof(*src) * n);
150 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
151 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
154 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
155 /* Make sure "src" is in "root"s subtree. */
156 if (src->oid_len <= root->oid_len)
158 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
159 /* n = */ root->oid_len) != 0)
162 memset(dst, 0, sizeof(*dst));
163 dst->oid_len = src->oid_len - root->oid_len;
164 memcpy(dst->oid, &src->oid[root->oid_len],
165 dst->oid_len * sizeof(dst->oid[0]));
169 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
171 char oid_str[MAX_OID_LEN][16];
172 char *oid_str_ptr[MAX_OID_LEN];
174 for (size_t i = 0; i < o->oid_len; i++) {
175 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
176 oid_str_ptr[i] = oid_str[i];
179 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
182 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
184 if (host->sess_handle == NULL)
187 snmp_sess_close(host->sess_handle);
188 host->sess_handle = NULL;
189 } /* }}} void csnmp_host_close_session */
191 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
193 host_definition_t *hd;
200 if (hd->name != NULL) {
201 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
204 csnmp_host_close_session(hd);
208 sfree(hd->community);
210 sfree(hd->auth_passphrase);
211 sfree(hd->priv_passphrase);
213 sfree(hd->data_list);
216 } /* }}} void csnmp_host_definition_destroy */
218 static void csnmp_data_definition_destroy(data_definition_t *dd) {
221 sfree(dd->plugin_name);
222 sfree(dd->plugin_instance.prefix);
223 sfree(dd->plugin_instance.value);
224 sfree(dd->type_instance.prefix);
225 sfree(dd->type_instance.value);
226 sfree(dd->host.prefix);
227 sfree(dd->host.value);
230 ignorelist_free(dd->ignorelist);
232 } /* void csnmp_data_definition_destroy */
234 /* Many functions to handle the configuration. {{{ */
235 /* First there are many functions which do configuration stuff. It's a big
236 * bloated and messy, I'm afraid. */
239 * Callgraph for the config stuff:
241 * +-> call_snmp_init_once
242 * +-> csnmp_config_add_data
243 * ! +-> csnmp_config_configure_data_instance
244 * ! +-> csnmp_config_add_data_values
245 * +-> csnmp_config_add_host
246 * +-> csnmp_config_add_host_version
247 * +-> csnmp_config_add_host_collect
248 * +-> csnmp_config_add_host_auth_protocol
249 * +-> csnmp_config_add_host_priv_protocol
250 * +-> csnmp_config_add_host_security_level
252 static void call_snmp_init_once(void) {
253 static int have_init;
256 init_snmp(PACKAGE_NAME);
258 } /* void call_snmp_init_once */
260 static int csnmp_config_configure_data_instance(instance_t *instance,
261 oconfig_item_t *ci) {
262 char buffer[DATA_MAX_NAME_LEN];
264 int status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
268 instance->configured = true;
270 if (strlen(buffer) == 0) {
274 instance->oid.oid_len = MAX_OID_LEN;
276 if (!read_objid(buffer, instance->oid.oid, &instance->oid.oid_len)) {
277 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
282 } /* int csnmp_config_configure_data_instance */
284 static int csnmp_config_add_data_values(data_definition_t *dd,
285 oconfig_item_t *ci) {
286 if (ci->values_num < 1) {
287 WARNING("snmp plugin: `Values' needs at least one argument.");
291 for (int i = 0; i < ci->values_num; i++)
292 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
293 WARNING("snmp plugin: `Values' needs only string argument.");
299 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
300 if (dd->values == NULL)
302 dd->values_len = (size_t)ci->values_num;
304 for (int i = 0; i < ci->values_num; i++) {
305 dd->values[i].oid_len = MAX_OID_LEN;
307 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
308 &dd->values[i].oid_len)) {
309 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
310 ci->values[i].value.string);
319 } /* int csnmp_config_configure_data_instance */
321 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
322 oconfig_item_t *ci) {
323 if (ci->values_num < 1)
326 for (int i = 0; i < ci->values_num; i++) {
327 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
328 WARNING("snmp plugin: `Ignore' needs only string argument.");
333 for (int i = 0; i < ci->values_num; ++i) {
334 if (strarray_add(&(dd->ignores), &(dd->ignores_len),
335 ci->values[i].value.string) != 0) {
336 ERROR("snmp plugin: Can't allocate memory");
337 strarray_free(dd->ignores, dd->ignores_len);
342 } /* int csnmp_config_add_data_blacklist */
344 static int csnmp_config_add_data_filter_values(data_definition_t *data,
345 oconfig_item_t *ci) {
346 if (ci->values_num < 1) {
347 WARNING("snmp plugin: `FilterValues' needs at least one argument.");
351 for (int i = 0; i < ci->values_num; i++) {
352 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
353 WARNING("snmp plugin: All arguments to `FilterValues' must be strings.");
356 ignorelist_add(data->ignorelist, ci->values[i].value.string);
360 } /* int csnmp_config_add_data_filter_values */
362 static int csnmp_config_add_data_filter_oid(data_definition_t *data,
363 oconfig_item_t *ci) {
365 char buffer[DATA_MAX_NAME_LEN];
366 int status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
370 data->filter_oid.oid_len = MAX_OID_LEN;
372 if (!read_objid(buffer, data->filter_oid.oid, &data->filter_oid.oid_len)) {
373 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
377 } /* int csnmp_config_add_data_filter_oid */
379 static int csnmp_config_add_data(oconfig_item_t *ci) {
380 data_definition_t *dd = calloc(1, sizeof(*dd));
384 int status = cf_util_get_string(ci, &dd->name);
395 dd->ignorelist = ignorelist_create(/* invert = */ 1);
396 if (dd->ignorelist == NULL) {
399 ERROR("snmp plugin: ignorelist_create() failed.");
403 dd->plugin_name = strdup("snmp");
404 if (dd->plugin_name == NULL) {
405 ERROR("snmp plugin: Can't allocate memory");
409 for (int i = 0; i < ci->children_num; i++) {
410 oconfig_item_t *option = ci->children + i;
412 if (strcasecmp("Type", option->key) == 0)
413 status = cf_util_get_string(option, &dd->type);
414 else if (strcasecmp("Table", option->key) == 0)
415 status = cf_util_get_boolean(option, &dd->is_table);
416 else if (strcasecmp("Plugin", option->key) == 0)
417 status = cf_util_get_string(option, &dd->plugin_name);
418 else if (strcasecmp("Instance", option->key) == 0) {
420 /* Instance is OID */
422 "snmp plugin: data %s: Option `Instance' is deprecated, please use "
423 "option `TypeInstanceOID'.",
426 csnmp_config_configure_data_instance(&dd->type_instance, option);
428 /* Instance is a simple string */
430 "snmp plugin: data %s: Option `Instance' is deprecated, please use "
431 "option `TypeInstance'.",
433 status = cf_util_get_string(option, &dd->type_instance.value);
435 } else if (strcasecmp("InstancePrefix", option->key) == 0) {
436 WARNING("snmp plugin: data %s: Option `InstancePrefix' is deprecated, "
437 "please use option `TypeInstancePrefix'.",
439 status = cf_util_get_string(option, &dd->type_instance.prefix);
440 } else if (strcasecmp("PluginInstance", option->key) == 0)
441 status = cf_util_get_string(option, &dd->plugin_instance.value);
442 else if (strcasecmp("TypeInstance", option->key) == 0)
443 status = cf_util_get_string(option, &dd->type_instance.value);
444 else if (strcasecmp("PluginInstanceOID", option->key) == 0)
446 csnmp_config_configure_data_instance(&dd->plugin_instance, option);
447 else if (strcasecmp("PluginInstancePrefix", option->key) == 0)
448 status = cf_util_get_string(option, &dd->plugin_instance.prefix);
449 else if (strcasecmp("TypeInstanceOID", option->key) == 0)
450 status = csnmp_config_configure_data_instance(&dd->type_instance, option);
451 else if (strcasecmp("TypeInstancePrefix", option->key) == 0)
452 status = cf_util_get_string(option, &dd->type_instance.prefix);
453 else if (strcasecmp("HostOID", option->key) == 0)
454 status = csnmp_config_configure_data_instance(&dd->host, option);
455 else if (strcasecmp("HostPrefix", option->key) == 0)
456 status = cf_util_get_string(option, &dd->host.prefix);
457 else if (strcasecmp("Values", option->key) == 0)
458 status = csnmp_config_add_data_values(dd, option);
459 else if (strcasecmp("Shift", option->key) == 0)
460 status = cf_util_get_double(option, &dd->shift);
461 else if (strcasecmp("Scale", option->key) == 0)
462 status = cf_util_get_double(option, &dd->scale);
463 else if (strcasecmp("Ignore", option->key) == 0)
464 status = csnmp_config_add_data_blacklist(dd, option);
465 else if (strcasecmp("InvertMatch", option->key) == 0)
466 status = cf_util_get_boolean(option, &dd->invert_match);
467 else if (strcasecmp("FilterOID", option->key) == 0) {
468 status = csnmp_config_add_data_filter_oid(dd, option);
469 } else if (strcasecmp("FilterValues", option->key) == 0) {
470 status = csnmp_config_add_data_filter_values(dd, option);
471 } else if (strcasecmp("FilterIgnoreSelected", option->key) == 0) {
473 status = cf_util_get_boolean(option, &t);
475 ignorelist_set_invert(dd->ignorelist, /* invert = */ !t);
477 WARNING("snmp plugin: data %s: Option `%s' not allowed here.", dd->name,
484 } /* for (ci->children) */
486 while (status == 0) {
488 /* Set type_instance to SUBID by default */
489 if (!dd->plugin_instance.configured && !dd->host.configured)
490 dd->type_instance.configured = true;
492 if (dd->plugin_instance.value && dd->plugin_instance.configured) {
494 "snmp plugin: data %s: Option `PluginInstance' will be ignored.",
497 if (dd->type_instance.value && dd->type_instance.configured) {
498 WARNING("snmp plugin: data %s: Option `TypeInstance' will be ignored.",
501 if (dd->type_instance.prefix && !dd->type_instance.configured) {
502 WARNING("snmp plugin: data %s: Option `TypeInstancePrefix' will be "
506 if (dd->plugin_instance.prefix && !dd->plugin_instance.configured) {
507 WARNING("snmp plugin: data %s: Option `PluginInstancePrefix' will be "
511 if (dd->host.prefix && !dd->host.configured) {
512 WARNING("snmp plugin: data %s: Option `HostPrefix' will be ignored.",
516 if (dd->plugin_instance.oid.oid_len > 0) {
517 WARNING("snmp plugin: data %s: Option `PluginInstanceOID' will be "
521 if (dd->type_instance.oid.oid_len > 0) {
523 "snmp plugin: data %s: Option `TypeInstanceOID' will be ignored.",
526 if (dd->type_instance.prefix) {
527 WARNING("snmp plugin: data %s: Option `TypeInstancePrefix' is ignored "
532 if (dd->plugin_instance.prefix) {
533 WARNING("snmp plugin: data %s: Option `PluginInstancePrefix' is "
535 "`Table' set to `false'.",
538 if (dd->host.prefix) {
540 "snmp plugin: data %s: Option `HostPrefix' is ignored when `Table' "
546 if (dd->type == NULL) {
547 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
551 if (dd->values == NULL) {
552 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
558 } /* while (status == 0) */
561 csnmp_data_definition_destroy(dd);
565 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
567 dd->name, dd->type, (dd->is_table) ? "true" : "false", dd->values_len);
569 DEBUG("snmp plugin: plugin_instance = %s, type_instance = %s,",
570 dd->plugin_instance.value, dd->type_instance.value);
572 DEBUG("snmp plugin: type_instance_by_oid = %s, plugin_instance_by_oid "
574 (dd->type_instance.oid.oid_len > 0)
576 : ((dd->type_instance.configured) ? "SUBID" : "false"),
577 (dd->plugin_instance.oid.oid_len > 0)
579 : ((dd->plugin_instance.configured) ? "SUBID" : "false"));
581 if (data_head == NULL)
584 data_definition_t *last;
586 while (last->next != NULL)
592 } /* int csnmp_config_add_data */
594 static int csnmp_config_add_host_version(host_definition_t *hd,
595 oconfig_item_t *ci) {
598 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
599 WARNING("snmp plugin: The `Version' config option needs exactly one number "
604 version = (int)ci->values[0].value.number;
605 if ((version < 1) || (version > 3)) {
606 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
610 hd->version = version;
613 } /* int csnmp_config_add_host_version */
615 static int csnmp_config_add_host_collect(host_definition_t *host,
616 oconfig_item_t *ci) {
617 data_definition_t *data;
618 data_definition_t **data_list;
621 if (ci->values_num < 1) {
622 WARNING("snmp plugin: `Collect' needs at least one argument.");
626 for (int i = 0; i < ci->values_num; i++)
627 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
628 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
632 data_list_len = host->data_list_len + ci->values_num;
634 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
635 if (data_list == NULL)
637 host->data_list = data_list;
639 for (int i = 0; i < ci->values_num; i++) {
640 for (data = data_head; data != NULL; data = data->next)
641 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
645 WARNING("snmp plugin: No such data configured: `%s'",
646 ci->values[i].value.string);
650 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
651 host->data_list_len, data->name);
653 host->data_list[host->data_list_len] = data;
654 host->data_list_len++;
655 } /* for (values_num) */
658 } /* int csnmp_config_add_host_collect */
660 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
661 oconfig_item_t *ci) {
665 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
669 if (strcasecmp("MD5", buffer) == 0) {
670 hd->auth_protocol = usmHMACMD5AuthProtocol;
671 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
672 } else if (strcasecmp("SHA", buffer) == 0) {
673 hd->auth_protocol = usmHMACSHA1AuthProtocol;
674 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
676 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
681 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
682 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
685 } /* int csnmp_config_add_host_auth_protocol */
687 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
688 oconfig_item_t *ci) {
692 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
696 if (strcasecmp("AES", buffer) == 0) {
697 hd->priv_protocol = usmAESPrivProtocol;
698 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
699 } else if (strcasecmp("DES", buffer) == 0) {
700 hd->priv_protocol = usmDESPrivProtocol;
701 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
703 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
708 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
709 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
712 } /* int csnmp_config_add_host_priv_protocol */
714 static int csnmp_config_add_host_security_level(host_definition_t *hd,
715 oconfig_item_t *ci) {
719 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
723 if (strcasecmp("noAuthNoPriv", buffer) == 0)
724 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
725 else if (strcasecmp("authNoPriv", buffer) == 0)
726 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
727 else if (strcasecmp("authPriv", buffer) == 0)
728 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
730 WARNING("snmp plugin: The `SecurityLevel' config option must be "
731 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
735 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
739 } /* int csnmp_config_add_host_security_level */
741 static int csnmp_config_add_host(oconfig_item_t *ci) {
742 host_definition_t *hd;
745 /* Registration stuff. */
746 char cb_name[DATA_MAX_NAME_LEN];
748 hd = calloc(1, sizeof(*hd));
752 C_COMPLAIN_INIT(&hd->complaint);
754 status = cf_util_get_string(ci, &hd->name);
760 hd->sess_handle = NULL;
763 /* These mean that we have not set a timeout or retry value */
767 for (int i = 0; i < ci->children_num; i++) {
768 oconfig_item_t *option = ci->children + i;
770 if (strcasecmp("Address", option->key) == 0)
771 status = cf_util_get_string(option, &hd->address);
772 else if (strcasecmp("Community", option->key) == 0)
773 status = cf_util_get_string(option, &hd->community);
774 else if (strcasecmp("Version", option->key) == 0)
775 status = csnmp_config_add_host_version(hd, option);
776 else if (strcasecmp("Timeout", option->key) == 0)
777 status = cf_util_get_cdtime(option, &hd->timeout);
778 else if (strcasecmp("Retries", option->key) == 0)
779 status = cf_util_get_int(option, &hd->retries);
780 else if (strcasecmp("Collect", option->key) == 0)
781 status = csnmp_config_add_host_collect(hd, option);
782 else if (strcasecmp("Interval", option->key) == 0)
783 status = cf_util_get_cdtime(option, &hd->interval);
784 else if (strcasecmp("Username", option->key) == 0)
785 status = cf_util_get_string(option, &hd->username);
786 else if (strcasecmp("AuthProtocol", option->key) == 0)
787 status = csnmp_config_add_host_auth_protocol(hd, option);
788 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
789 status = csnmp_config_add_host_priv_protocol(hd, option);
790 else if (strcasecmp("AuthPassphrase", option->key) == 0)
791 status = cf_util_get_string(option, &hd->auth_passphrase);
792 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
793 status = cf_util_get_string(option, &hd->priv_passphrase);
794 else if (strcasecmp("SecurityLevel", option->key) == 0)
795 status = csnmp_config_add_host_security_level(hd, option);
796 else if (strcasecmp("Context", option->key) == 0)
797 status = cf_util_get_string(option, &hd->context);
800 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
807 } /* for (ci->children) */
809 while (status == 0) {
810 if (hd->address == NULL) {
811 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
815 if (hd->community == NULL && hd->version < 3) {
816 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
820 if (hd->version == 3) {
821 if (hd->username == NULL) {
822 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
826 if (hd->security_level == 0) {
827 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
832 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
833 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
834 if (hd->auth_protocol == NULL) {
835 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
840 if (hd->auth_passphrase == NULL) {
841 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
847 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
848 if (hd->priv_protocol == NULL) {
849 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
854 if (hd->priv_passphrase == NULL) {
855 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
864 } /* while (status == 0) */
867 csnmp_host_definition_destroy(hd);
871 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
873 hd->name, hd->address, hd->community, hd->version);
875 snprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
877 status = plugin_register_complex_read(
878 /* group = */ NULL, cb_name, csnmp_read_host, hd->interval,
880 .data = hd, .free_func = csnmp_host_definition_destroy,
883 ERROR("snmp plugin: Registering complex read function failed.");
888 } /* int csnmp_config_add_host */
890 static int csnmp_config(oconfig_item_t *ci) {
891 call_snmp_init_once();
893 for (int i = 0; i < ci->children_num; i++) {
894 oconfig_item_t *child = ci->children + i;
895 if (strcasecmp("Data", child->key) == 0)
896 csnmp_config_add_data(child);
897 else if (strcasecmp("Host", child->key) == 0)
898 csnmp_config_add_host(child);
900 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
902 } /* for (ci->children) */
905 } /* int csnmp_config */
907 /* }}} End of the config stuff. Now the interesting part begins */
909 static void csnmp_host_open_session(host_definition_t *host) {
910 struct snmp_session sess;
913 if (host->sess_handle != NULL)
914 csnmp_host_close_session(host);
916 snmp_sess_init(&sess);
917 sess.peername = host->address;
918 switch (host->version) {
920 sess.version = SNMP_VERSION_1;
923 sess.version = SNMP_VERSION_3;
926 sess.version = SNMP_VERSION_2c;
930 if (host->version == 3) {
931 sess.securityName = host->username;
932 sess.securityNameLen = strlen(host->username);
933 sess.securityLevel = host->security_level;
935 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
936 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
937 sess.securityAuthProto = host->auth_protocol;
938 sess.securityAuthProtoLen = host->auth_protocol_len;
939 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
940 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
941 (u_char *)host->auth_passphrase,
942 strlen(host->auth_passphrase), sess.securityAuthKey,
943 &sess.securityAuthKeyLen);
944 if (error != SNMPERR_SUCCESS) {
945 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
951 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
952 sess.securityPrivProto = host->priv_protocol;
953 sess.securityPrivProtoLen = host->priv_protocol_len;
954 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
955 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
956 (u_char *)host->priv_passphrase,
957 strlen(host->priv_passphrase), sess.securityPrivKey,
958 &sess.securityPrivKeyLen);
959 if (error != SNMPERR_SUCCESS) {
960 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
966 if (host->context != NULL) {
967 sess.contextName = host->context;
968 sess.contextNameLen = strlen(host->context);
970 } else /* SNMPv1/2 "authenticates" with community string */
972 sess.community = (u_char *)host->community;
973 sess.community_len = strlen(host->community);
976 /* Set timeout & retries, if they have been changed from the default */
977 if (host->timeout != 0) {
978 /* net-snmp expects microseconds */
979 sess.timeout = CDTIME_T_TO_US(host->timeout);
981 if (host->retries >= 0) {
982 sess.retries = host->retries;
985 /* snmp_sess_open will copy the `struct snmp_session *'. */
986 host->sess_handle = snmp_sess_open(&sess);
988 if (host->sess_handle == NULL) {
991 snmp_error(&sess, NULL, NULL, &errstr);
993 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
994 (errstr == NULL) ? "Unknown problem" : errstr);
997 } /* void csnmp_host_open_session */
999 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
1001 static value_t csnmp_value_list_to_value(const struct variable_list *vl,
1002 int type, double scale, double shift,
1003 const char *host_name,
1004 const char *data_name) {
1006 uint64_t tmp_unsigned = 0;
1007 int64_t tmp_signed = 0;
1009 /* Set to true when the original SNMP type appears to have been signed. */
1010 bool prefer_signed = 0;
1012 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
1013 (vl->type == ASN_COUNTER)
1014 #ifdef ASN_TIMETICKS
1015 || (vl->type == ASN_TIMETICKS)
1017 || (vl->type == ASN_GAUGE)) {
1018 tmp_unsigned = (uint32_t)*vl->val.integer;
1019 tmp_signed = (int32_t)*vl->val.integer;
1021 if (vl->type == ASN_INTEGER)
1024 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
1025 } else if (vl->type == ASN_COUNTER64) {
1026 tmp_unsigned = (uint32_t)vl->val.counter64->high;
1027 tmp_unsigned = tmp_unsigned << 32;
1028 tmp_unsigned += (uint32_t)vl->val.counter64->low;
1029 tmp_signed = (int64_t)tmp_unsigned;
1030 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
1031 } else if (vl->type == ASN_OCTET_STR) {
1032 /* We'll handle this later.. */
1034 char oid_buffer[1024] = {0};
1036 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
1040 if (vl->type == ASN_NULL)
1041 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
1044 WARNING("snmp plugin: I don't know the ASN type #%i "
1045 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
1046 (int)vl->type, oid_buffer,
1047 (data_name != NULL) ? data_name : "UNKNOWN",
1048 (host_name != NULL) ? host_name : "UNKNOWN");
1053 if (vl->type == ASN_OCTET_STR) {
1056 if (vl->val.string != NULL) {
1058 size_t string_length;
1060 string_length = sizeof(string) - 1;
1061 if (vl->val_len < string_length)
1062 string_length = vl->val_len;
1064 /* The strings we get from the Net-SNMP library may not be null
1065 * terminated. That is why we're using `memcpy' here and not `strcpy'.
1066 * `string_length' is set to `vl->val_len' which holds the length of the
1068 memcpy(string, vl->val.string, string_length);
1069 string[string_length] = 0;
1071 status = parse_value(string, &ret, type);
1073 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
1075 (host_name != NULL) ? host_name : "UNKNOWN",
1076 DS_TYPE_TO_STRING(type), string);
1082 case DS_TYPE_COUNTER:
1083 case DS_TYPE_DERIVE:
1084 case DS_TYPE_ABSOLUTE:
1085 memset(&ret, 0, sizeof(ret));
1093 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
1094 "data source type: %i.",
1099 } /* if (vl->type == ASN_OCTET_STR) */
1100 else if (type == DS_TYPE_COUNTER) {
1101 ret.counter = tmp_unsigned;
1102 } else if (type == DS_TYPE_GAUGE) {
1105 else if (prefer_signed)
1106 ret.gauge = (scale * tmp_signed) + shift;
1108 ret.gauge = (scale * tmp_unsigned) + shift;
1109 } else if (type == DS_TYPE_DERIVE) {
1111 ret.derive = (derive_t)tmp_signed;
1113 ret.derive = (derive_t)tmp_unsigned;
1114 } else if (type == DS_TYPE_ABSOLUTE) {
1115 ret.absolute = (absolute_t)tmp_unsigned;
1117 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1124 } /* value_t csnmp_value_list_to_value */
1126 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1127 * representation and writes it to dst. Returns zero on success and ENOMEM if
1128 * dst is not large enough to hold the string. dst is guaranteed to be
1129 * nul-terminated. */
1130 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
1131 const struct variable_list *vb,
1139 buffer_free = dst_size;
1141 for (size_t i = 0; i < vb->val_len; i++) {
1144 status = snprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
1145 (unsigned int)vb->val.bitstring[i]);
1146 assert(status >= 0);
1148 if (((size_t)status) >= buffer_free) /* truncated */
1150 dst[dst_size - 1] = 0;
1152 } else /* if (status < buffer_free) */
1154 buffer_ptr += (size_t)status;
1155 buffer_free -= (size_t)status;
1160 } /* }}} int csnmp_strvbcopy_hexstring */
1162 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1163 * dst. If non-printable characters are detected, it will switch to a hex
1164 * representation of the string. Returns zero on success, EINVAL if vb does not
1165 * contain a string and ENOMEM if dst is not large enough to contain the
1167 static int csnmp_strvbcopy(char *dst, /* {{{ */
1168 const struct variable_list *vb, size_t dst_size) {
1172 if (vb->type == ASN_OCTET_STR)
1173 src = (char *)vb->val.string;
1174 else if (vb->type == ASN_BIT_STR)
1175 src = (char *)vb->val.bitstring;
1176 else if (vb->type == ASN_IPADDRESS) {
1177 return snprintf(dst, dst_size,
1178 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1179 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1180 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1186 num_chars = dst_size - 1;
1187 if (num_chars > vb->val_len)
1188 num_chars = vb->val_len;
1190 for (size_t i = 0; i < num_chars; i++) {
1191 /* Check for control characters. */
1192 if ((unsigned char)src[i] < 32)
1193 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1197 dst[dst_size - 1] = 0;
1199 if (dst_size <= vb->val_len)
1203 } /* }}} int csnmp_strvbcopy */
1205 static csnmp_cell_char_t *csnmp_get_char_cell(const struct variable_list *vb,
1206 const oid_t *root_oid,
1207 const host_definition_t *hd,
1208 const data_definition_t *dd) {
1213 csnmp_cell_char_t *il = calloc(1, sizeof(*il));
1215 ERROR("snmp plugin: calloc failed.");
1221 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1223 if (csnmp_oid_suffix(&il->suffix, &vb_name, root_oid) != 0) {
1229 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1230 (vb->type == ASN_IPADDRESS)) {
1232 csnmp_strvbcopy(il->value, vb, sizeof(il->value));
1235 value_t val = csnmp_value_list_to_value(
1236 vb, DS_TYPE_COUNTER,
1237 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1238 snprintf(il->value, sizeof(il->value), "%" PRIu64, (uint64_t)val.counter);
1242 } /* csnmp_cell_char_t csnmp_get_char_cell */
1244 static void csnmp_cells_append(csnmp_cell_char_t **head,
1245 csnmp_cell_char_t **tail,
1246 csnmp_cell_char_t *il) {
1252 } /* void csnmp_cells_append */
1254 static bool csnmp_ignore_instance(csnmp_cell_char_t *cell,
1255 const data_definition_t *dd) {
1256 bool is_matched = 0;
1257 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1258 int status = fnmatch(dd->ignores[i], cell->value, 0);
1260 if (!dd->invert_match) {
1268 if (dd->invert_match && !is_matched) {
1272 } /* bool csnmp_ignore_instance */
1274 static void csnmp_cell_replace_reserved_chars(csnmp_cell_char_t *cell) {
1275 for (char *ptr = cell->value; *ptr != '\0'; ptr++) {
1276 if ((*ptr > 0) && (*ptr < 32))
1278 else if (*ptr == '/')
1281 } /* void csnmp_cell_replace_reserved_chars */
1283 static int csnmp_dispatch_table(host_definition_t *host,
1284 data_definition_t *data,
1285 csnmp_cell_char_t *type_instance_cells,
1286 csnmp_cell_char_t *plugin_instance_cells,
1287 csnmp_cell_char_t *hostname_cells,
1288 csnmp_cell_char_t *filter_cells,
1289 csnmp_cell_value_t **value_cells) {
1290 const data_set_t *ds;
1291 value_list_t vl = VALUE_LIST_INIT;
1293 csnmp_cell_char_t *type_instance_cell_ptr = type_instance_cells;
1294 csnmp_cell_char_t *plugin_instance_cell_ptr = plugin_instance_cells;
1295 csnmp_cell_char_t *hostname_cell_ptr = hostname_cells;
1296 csnmp_cell_char_t *filter_cell_ptr = filter_cells;
1297 csnmp_cell_value_t *value_cell_ptr[data->values_len];
1301 oid_t current_suffix;
1303 ds = plugin_get_ds(data->type);
1305 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1308 assert(ds->ds_num == data->values_len);
1309 assert(data->values_len > 0);
1311 for (i = 0; i < data->values_len; i++)
1312 value_cell_ptr[i] = value_cells[i];
1314 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1315 sstrncpy(vl.type, data->type, sizeof(vl.type));
1317 vl.interval = host->interval;
1321 bool suffix_skipped = 0;
1323 /* Determine next suffix to handle. */
1324 if (type_instance_cells != NULL) {
1325 if (type_instance_cell_ptr == NULL) {
1330 memcpy(¤t_suffix, &type_instance_cell_ptr->suffix,
1331 sizeof(current_suffix));
1333 /* no instance configured */
1334 csnmp_cell_value_t *ptr = value_cell_ptr[0];
1340 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1344 char oid_buffer[1024] = {0};
1345 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, current_suffix.oid,
1346 current_suffix.oid_len);
1347 DEBUG("SNMP PLUGIN: SUFFIX %s", oid_buffer);
1350 /* Update plugin_instance_cell_ptr to point expected suffix */
1351 if (plugin_instance_cells != NULL) {
1352 while ((plugin_instance_cell_ptr != NULL) &&
1353 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1354 ¤t_suffix) < 0))
1355 plugin_instance_cell_ptr = plugin_instance_cell_ptr->next;
1357 if (plugin_instance_cell_ptr == NULL) {
1362 if (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1363 ¤t_suffix) > 0) {
1364 /* This suffix is missing in the subtree. Indicate this with the
1365 * "suffix_skipped" flag and try the next instance / suffix. */
1370 /* Update hostname_cell_ptr to point expected suffix */
1371 if (hostname_cells != NULL) {
1373 (hostname_cell_ptr != NULL) &&
1374 (csnmp_oid_compare(&hostname_cell_ptr->suffix, ¤t_suffix) < 0))
1375 hostname_cell_ptr = hostname_cell_ptr->next;
1377 if (hostname_cell_ptr == NULL) {
1382 if (csnmp_oid_compare(&hostname_cell_ptr->suffix, ¤t_suffix) > 0) {
1383 /* This suffix is missing in the subtree. Indicate this with the
1384 * "suffix_skipped" flag and try the next instance / suffix. */
1389 /* Update filter_cell_ptr to point expected suffix */
1390 if (filter_cells != NULL) {
1391 while ((filter_cell_ptr != NULL) &&
1392 (csnmp_oid_compare(&filter_cell_ptr->suffix, ¤t_suffix) < 0))
1393 filter_cell_ptr = filter_cell_ptr->next;
1395 if (filter_cell_ptr == NULL) {
1400 if (csnmp_oid_compare(&filter_cell_ptr->suffix, ¤t_suffix) > 0) {
1401 /* This suffix is missing in the subtree. Indicate this with the
1402 * "suffix_skipped" flag and try the next instance / suffix. */
1407 /* Update all the value_cell_ptr to point at the entry with the same
1408 * trailing partial OID */
1409 for (i = 0; i < data->values_len; i++) {
1411 (value_cell_ptr[i] != NULL) &&
1412 (csnmp_oid_compare(&value_cell_ptr[i]->suffix, ¤t_suffix) < 0))
1413 value_cell_ptr[i] = value_cell_ptr[i]->next;
1415 if (value_cell_ptr[i] == NULL) {
1418 } else if (csnmp_oid_compare(&value_cell_ptr[i]->suffix,
1419 ¤t_suffix) > 0) {
1420 /* This suffix is missing in the subtree. Indicate this with the
1421 * "suffix_skipped" flag and try the next instance / suffix. */
1425 } /* for (i = 0; i < columns; i++) */
1430 /* Matching the values failed. Start from the beginning again. */
1431 if (suffix_skipped) {
1432 if (type_instance_cells != NULL)
1433 type_instance_cell_ptr = type_instance_cell_ptr->next;
1435 value_cell_ptr[0] = value_cell_ptr[0]->next;
1440 /* if we reach this line, all value_cell_ptr[i] are non-NULL and are set
1441 * to the same subid. type_instance_cell_ptr is either NULL or points to the
1442 * same subid, too. */
1444 for (i = 1; i < data->values_len; i++) {
1445 assert(value_cell_ptr[i] != NULL);
1446 assert(csnmp_oid_compare(&value_cell_ptr[i - 1]->suffix,
1447 &value_cell_ptr[i]->suffix) == 0);
1449 assert((type_instance_cell_ptr == NULL) ||
1450 (csnmp_oid_compare(&type_instance_cell_ptr->suffix,
1451 &value_cell_ptr[0]->suffix) == 0));
1452 assert((plugin_instance_cell_ptr == NULL) ||
1453 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1454 &value_cell_ptr[0]->suffix) == 0));
1455 assert((hostname_cell_ptr == NULL) ||
1456 (csnmp_oid_compare(&hostname_cell_ptr->suffix,
1457 &value_cell_ptr[0]->suffix) == 0));
1458 assert((filter_cell_ptr == NULL) ||
1459 (csnmp_oid_compare(&filter_cell_ptr->suffix,
1460 &value_cell_ptr[0]->suffix) == 0));
1463 /* Check the value in filter column */
1464 if (filter_cell_ptr &&
1465 ignorelist_match(data->ignorelist, filter_cell_ptr->value) != 0) {
1466 if (type_instance_cells != NULL)
1467 type_instance_cell_ptr = type_instance_cell_ptr->next;
1469 value_cell_ptr[0] = value_cell_ptr[0]->next;
1475 if (data->host.configured) {
1476 char temp[DATA_MAX_NAME_LEN];
1477 if (hostname_cell_ptr == NULL)
1478 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1480 sstrncpy(temp, hostname_cell_ptr->value, sizeof(temp));
1482 if (data->host.prefix == NULL)
1483 sstrncpy(vl.host, temp, sizeof(vl.host));
1485 snprintf(vl.host, sizeof(vl.host), "%s%s", data->host.prefix, temp);
1487 sstrncpy(vl.host, host->name, sizeof(vl.host));
1490 /* set vl.type_instance */
1491 if (data->type_instance.configured) {
1492 char temp[DATA_MAX_NAME_LEN];
1493 if (type_instance_cell_ptr == NULL)
1494 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1496 sstrncpy(temp, type_instance_cell_ptr->value, sizeof(temp));
1498 if (data->type_instance.prefix == NULL)
1499 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1501 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1502 data->type_instance.prefix, temp);
1503 } else if (data->type_instance.value) {
1504 sstrncpy(vl.type_instance, data->type_instance.value,
1505 sizeof(vl.type_instance));
1508 /* set vl.plugin_instance */
1509 if (data->plugin_instance.configured) {
1510 char temp[DATA_MAX_NAME_LEN];
1511 if (plugin_instance_cell_ptr == NULL)
1512 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1514 sstrncpy(temp, plugin_instance_cell_ptr->value, sizeof(temp));
1516 if (data->plugin_instance.prefix == NULL)
1517 sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
1519 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
1520 data->plugin_instance.prefix, temp);
1521 } else if (data->plugin_instance.value) {
1522 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
1523 sizeof(vl.plugin_instance));
1526 vl.values_len = data->values_len;
1527 value_t values[vl.values_len];
1530 for (i = 0; i < data->values_len; i++)
1531 vl.values[i] = value_cell_ptr[i]->value;
1533 plugin_dispatch_values(&vl);
1535 /* prevent leakage of pointer to local variable. */
1539 if (type_instance_cells != NULL)
1540 type_instance_cell_ptr = type_instance_cell_ptr->next;
1542 value_cell_ptr[0] = value_cell_ptr[0]->next;
1543 } /* while (have_more) */
1546 } /* int csnmp_dispatch_table */
1548 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1549 struct snmp_pdu *req;
1550 struct snmp_pdu *res = NULL;
1551 struct variable_list *vb;
1553 const data_set_t *ds;
1555 size_t oid_list_len = data->values_len;
1557 if (data->type_instance.oid.oid_len > 0)
1560 if (data->plugin_instance.oid.oid_len > 0)
1563 if (data->host.oid.oid_len > 0)
1566 if (data->filter_oid.oid_len > 0)
1569 /* Holds the last OID returned by the device. We use this in the GETNEXT
1570 * request to proceed. */
1571 oid_t oid_list[oid_list_len];
1572 /* Set to false when an OID has left its subtree so we don't re-request it
1574 csnmp_oid_type_t oid_list_todo[oid_list_len];
1579 /* `value_list_head' and `value_cells_tail' implement a linked list for each
1580 * value. `instance_cells_head' and `instance_cells_tail' implement a linked
1581 * list of instance names. This is used to jump gaps in the table. */
1582 csnmp_cell_char_t *type_instance_cells_head = NULL;
1583 csnmp_cell_char_t *type_instance_cells_tail = NULL;
1584 csnmp_cell_char_t *plugin_instance_cells_head = NULL;
1585 csnmp_cell_char_t *plugin_instance_cells_tail = NULL;
1586 csnmp_cell_char_t *hostname_cells_head = NULL;
1587 csnmp_cell_char_t *hostname_cells_tail = NULL;
1588 csnmp_cell_char_t *filter_cells_head = NULL;
1589 csnmp_cell_char_t *filter_cells_tail = NULL;
1590 csnmp_cell_value_t **value_cells_head;
1591 csnmp_cell_value_t **value_cells_tail;
1593 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1596 if (host->sess_handle == NULL) {
1597 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1601 ds = plugin_get_ds(data->type);
1603 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1607 if (ds->ds_num != data->values_len) {
1608 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1609 " values, but config talks "
1611 data->type, ds->ds_num, data->values_len);
1614 assert(data->values_len > 0);
1616 for (i = 0; i < data->values_len; i++)
1617 oid_list_todo[i] = OID_TYPE_VARIABLE;
1619 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1620 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1622 if (data->type_instance.oid.oid_len > 0) {
1623 memcpy(oid_list + i, &data->type_instance.oid, sizeof(oid_t));
1624 oid_list_todo[i] = OID_TYPE_TYPEINSTANCE;
1628 if (data->plugin_instance.oid.oid_len > 0) {
1629 memcpy(oid_list + i, &data->plugin_instance.oid, sizeof(oid_t));
1630 oid_list_todo[i] = OID_TYPE_PLUGININSTANCE;
1634 if (data->host.oid.oid_len > 0) {
1635 memcpy(oid_list + i, &data->host.oid, sizeof(oid_t));
1636 oid_list_todo[i] = OID_TYPE_HOST;
1640 if (data->filter_oid.oid_len > 0) {
1641 memcpy(oid_list + i, &data->filter_oid, sizeof(oid_t));
1642 oid_list_todo[i] = OID_TYPE_FILTER;
1646 /* We're going to construct n linked lists, one for each "value".
1647 * value_cells_head will contain pointers to the heads of these linked lists,
1648 * value_cells_tail will contain pointers to the tail of the lists. */
1649 value_cells_head = calloc(data->values_len, sizeof(*value_cells_head));
1650 value_cells_tail = calloc(data->values_len, sizeof(*value_cells_tail));
1651 if ((value_cells_head == NULL) || (value_cells_tail == NULL)) {
1652 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1653 sfree(value_cells_head);
1654 sfree(value_cells_tail);
1659 while (status == 0) {
1660 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1662 ERROR("snmp plugin: snmp_pdu_create failed.");
1667 size_t oid_list_todo_num = 0;
1668 size_t var_idx[oid_list_len];
1669 memset(var_idx, 0, sizeof(var_idx));
1671 for (i = 0; i < oid_list_len; i++) {
1672 /* Do not rerequest already finished OIDs */
1673 if (!oid_list_todo[i])
1675 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1676 var_idx[oid_list_todo_num] = i;
1677 oid_list_todo_num++;
1680 if (oid_list_todo_num == 0) {
1681 /* The request is still empty - so we are finished */
1682 DEBUG("snmp plugin: all variables have left their subtree");
1689 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1691 /* snmp_sess_synch_response always frees our req PDU */
1694 if ((status != STAT_SUCCESS) || (res == NULL)) {
1695 char *errstr = NULL;
1697 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1699 c_complain(LOG_ERR, &host->complaint,
1700 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1701 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1708 csnmp_host_close_session(host);
1715 assert(res != NULL);
1716 c_release(LOG_INFO, &host->complaint,
1717 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1720 vb = res->variables;
1726 if (res->errstat != SNMP_ERR_NOERROR) {
1727 if (res->errindex != 0) {
1728 /* Find the OID which caused error */
1729 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1730 vb = vb->next_variable, i++)
1734 if ((res->errindex == 0) || (vb == NULL)) {
1735 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1736 host->name, data->name, snmp_errstring(res->errstat),
1742 char oid_buffer[1024] = {0};
1743 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1745 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1746 data->name, oid_buffer, snmp_errstring(res->errstat));
1748 /* Get value index from todo list and skip OID found */
1749 assert(res->errindex <= oid_list_todo_num);
1750 i = var_idx[res->errindex - 1];
1751 assert(i < oid_list_len);
1752 oid_list_todo[i] = 0;
1759 for (vb = res->variables, i = 0; (vb != NULL);
1760 vb = vb->next_variable, i++) {
1761 /* Calculate value index from todo list */
1762 while ((i < oid_list_len) && !oid_list_todo[i]) {
1765 if (i >= oid_list_len) {
1769 /* An instance is configured and the res variable we process is the
1771 if (oid_list_todo[i] == OID_TYPE_TYPEINSTANCE) {
1772 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1773 (snmp_oid_ncompare(data->type_instance.oid.oid,
1774 data->type_instance.oid.oid_len, vb->name,
1776 data->type_instance.oid.oid_len) != 0)) {
1777 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1779 host->name, data->name);
1780 oid_list_todo[i] = 0;
1784 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1785 * add it to the list */
1786 csnmp_cell_char_t *cell =
1787 csnmp_get_char_cell(vb, &data->type_instance.oid, host, data);
1789 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1795 if (csnmp_ignore_instance(cell, data)) {
1798 csnmp_cell_replace_reserved_chars(cell);
1800 DEBUG("snmp plugin: il->type_instance = `%s';", cell->value);
1801 csnmp_cells_append(&type_instance_cells_head,
1802 &type_instance_cells_tail, cell);
1804 } else if (oid_list_todo[i] == OID_TYPE_PLUGININSTANCE) {
1805 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1806 (snmp_oid_ncompare(data->plugin_instance.oid.oid,
1807 data->plugin_instance.oid.oid_len, vb->name,
1809 data->plugin_instance.oid.oid_len) != 0)) {
1810 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1812 host->name, data->name);
1813 oid_list_todo[i] = 0;
1817 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1818 * add it to the list */
1819 csnmp_cell_char_t *cell =
1820 csnmp_get_char_cell(vb, &data->plugin_instance.oid, host, data);
1822 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1828 csnmp_cell_replace_reserved_chars(cell);
1830 DEBUG("snmp plugin: il->plugin_instance = `%s';", cell->value);
1831 csnmp_cells_append(&plugin_instance_cells_head,
1832 &plugin_instance_cells_tail, cell);
1833 } else if (oid_list_todo[i] == OID_TYPE_HOST) {
1834 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1835 (snmp_oid_ncompare(data->host.oid.oid, data->host.oid.oid_len,
1836 vb->name, vb->name_length,
1837 data->host.oid.oid_len) != 0)) {
1838 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
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->host.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->hostname = `%s';", cell->value);
1858 csnmp_cells_append(&hostname_cells_head, &hostname_cells_tail, cell);
1859 } else if (oid_list_todo[i] == OID_TYPE_FILTER) {
1860 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1861 (snmp_oid_ncompare(data->filter_oid.oid, data->filter_oid.oid_len,
1862 vb->name, vb->name_length,
1863 data->filter_oid.oid_len) != 0)) {
1864 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1865 host->name, data->name);
1866 oid_list_todo[i] = 0;
1870 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1871 * add it to the list */
1872 csnmp_cell_char_t *cell =
1873 csnmp_get_char_cell(vb, &data->filter_oid, host, data);
1875 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1881 csnmp_cell_replace_reserved_chars(cell);
1883 DEBUG("snmp plugin: il->filter = `%s';", cell->value);
1884 csnmp_cells_append(&filter_cells_head, &filter_cells_tail, cell);
1885 } else /* The variable we are processing is a normal value */
1887 assert(oid_list_todo[i] == OID_TYPE_VARIABLE);
1889 csnmp_cell_value_t *vt;
1894 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1896 /* Calculate the current suffix. This is later used to check that the
1897 * suffix is increasing. This also checks if we left the subtree */
1898 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1900 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1901 "Value probably left its subtree.",
1902 host->name, data->name, i);
1903 oid_list_todo[i] = 0;
1907 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1908 * our table matching algorithm will get confused. */
1909 if ((value_cells_tail[i] != NULL) &&
1910 (csnmp_oid_compare(&suffix, &value_cells_tail[i]->suffix) <= 0)) {
1911 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1912 "Suffix is not increasing.",
1913 host->name, data->name, i);
1914 oid_list_todo[i] = 0;
1918 vt = calloc(1, sizeof(*vt));
1920 ERROR("snmp plugin: calloc failed.");
1926 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1927 data->shift, host->name, data->name);
1928 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1931 if (value_cells_tail[i] == NULL)
1932 value_cells_head[i] = vt;
1934 value_cells_tail[i]->next = vt;
1935 value_cells_tail[i] = vt;
1938 /* Copy OID to oid_list[i] */
1939 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1940 oid_list[i].oid_len = vb->name_length;
1942 } /* for (vb = res->variables ...) */
1947 } /* while (status == 0) */
1954 csnmp_dispatch_table(host, data, type_instance_cells_head,
1955 plugin_instance_cells_head, hostname_cells_head,
1956 filter_cells_head, value_cells_head);
1958 /* Free all allocated variables here */
1959 while (type_instance_cells_head != NULL) {
1960 csnmp_cell_char_t *next = type_instance_cells_head->next;
1961 sfree(type_instance_cells_head);
1962 type_instance_cells_head = next;
1965 while (plugin_instance_cells_head != NULL) {
1966 csnmp_cell_char_t *next = plugin_instance_cells_head->next;
1967 sfree(plugin_instance_cells_head);
1968 plugin_instance_cells_head = next;
1971 while (hostname_cells_head != NULL) {
1972 csnmp_cell_char_t *next = hostname_cells_head->next;
1973 sfree(hostname_cells_head);
1974 hostname_cells_head = next;
1977 while (filter_cells_head != NULL) {
1978 csnmp_cell_char_t *next = filter_cells_head->next;
1979 sfree(filter_cells_head);
1980 filter_cells_head = next;
1983 for (i = 0; i < data->values_len; i++) {
1984 while (value_cells_head[i] != NULL) {
1985 csnmp_cell_value_t *next = value_cells_head[i]->next;
1986 sfree(value_cells_head[i]);
1987 value_cells_head[i] = next;
1991 sfree(value_cells_head);
1992 sfree(value_cells_tail);
1995 } /* int csnmp_read_table */
1997 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1998 struct snmp_pdu *req;
1999 struct snmp_pdu *res = NULL;
2000 struct variable_list *vb;
2002 const data_set_t *ds;
2003 value_list_t vl = VALUE_LIST_INIT;
2008 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
2011 if (host->sess_handle == NULL) {
2012 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
2016 ds = plugin_get_ds(data->type);
2018 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
2022 if (ds->ds_num != data->values_len) {
2023 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
2024 " values, but config talks "
2026 data->type, ds->ds_num, data->values_len);
2030 vl.values_len = ds->ds_num;
2031 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
2032 if (vl.values == NULL)
2034 for (i = 0; i < vl.values_len; i++) {
2035 if (ds->ds[i].type == DS_TYPE_COUNTER)
2036 vl.values[i].counter = 0;
2038 vl.values[i].gauge = NAN;
2041 sstrncpy(vl.host, host->name, sizeof(vl.host));
2042 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
2043 sstrncpy(vl.type, data->type, sizeof(vl.type));
2044 if (data->type_instance.value)
2045 sstrncpy(vl.type_instance, data->type_instance.value,
2046 sizeof(vl.type_instance));
2047 if (data->plugin_instance.value)
2048 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
2049 sizeof(vl.plugin_instance));
2051 vl.interval = host->interval;
2053 req = snmp_pdu_create(SNMP_MSG_GET);
2055 ERROR("snmp plugin: snmp_pdu_create failed.");
2060 for (i = 0; i < data->values_len; i++)
2061 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
2063 status = snmp_sess_synch_response(host->sess_handle, req, &res);
2065 if ((status != STAT_SUCCESS) || (res == NULL)) {
2066 char *errstr = NULL;
2068 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
2069 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
2070 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
2077 csnmp_host_close_session(host);
2082 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
2085 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
2086 DEBUG("snmp plugin: Got this variable: %s", buffer);
2087 #endif /* COLLECT_DEBUG */
2089 for (i = 0; i < data->values_len; i++)
2090 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
2091 vb->name, vb->name_length) == 0)
2093 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
2094 data->shift, host->name, data->name);
2095 } /* for (res->variables) */
2099 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
2100 plugin_dispatch_values(&vl);
2104 } /* int csnmp_read_value */
2106 static int csnmp_read_host(user_data_t *ud) {
2107 host_definition_t *host;
2114 if (host->interval == 0)
2115 host->interval = plugin_get_interval();
2117 if (host->sess_handle == NULL)
2118 csnmp_host_open_session(host);
2120 if (host->sess_handle == NULL)
2124 for (i = 0; i < host->data_list_len; i++) {
2125 data_definition_t *data = host->data_list[i];
2128 status = csnmp_read_table(host, data);
2130 status = csnmp_read_value(host, data);
2140 } /* int csnmp_read_host */
2142 static int csnmp_init(void) {
2143 call_snmp_init_once();
2146 } /* int csnmp_init */
2148 static int csnmp_shutdown(void) {
2149 data_definition_t *data_this;
2150 data_definition_t *data_next;
2152 /* When we get here, the read threads have been stopped and all the
2153 * `host_definition_t' will be freed. */
2154 DEBUG("snmp plugin: Destroying all data definitions.");
2156 data_this = data_head;
2158 while (data_this != NULL) {
2159 data_next = data_this->next;
2161 csnmp_data_definition_destroy(data_this);
2163 data_this = data_next;
2167 } /* int csnmp_shutdown */
2169 void module_register(void) {
2170 plugin_register_complex_config("snmp", csnmp_config);
2171 plugin_register_init("snmp", csnmp_init);
2172 plugin_register_shutdown("snmp", csnmp_shutdown);
2173 } /* void module_register */