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;
100 data_definition_t **data_list;
103 typedef struct host_definition_s host_definition_t;
105 /* These two types are used to cache values in `csnmp_read_table' to handle
107 struct csnmp_cell_char_s {
109 char value[DATA_MAX_NAME_LEN];
110 struct csnmp_cell_char_s *next;
112 typedef struct csnmp_cell_char_s csnmp_cell_char_t;
114 struct csnmp_cell_value_s {
117 struct csnmp_cell_value_s *next;
119 typedef struct csnmp_cell_value_s csnmp_cell_value_t;
124 OID_TYPE_TYPEINSTANCE,
125 OID_TYPE_PLUGININSTANCE,
133 static data_definition_t *data_head;
138 static int csnmp_read_host(user_data_t *ud);
143 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
144 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
145 memcpy(dst->oid, src, sizeof(*src) * n);
149 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
150 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
153 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
154 /* Make sure "src" is in "root"s subtree. */
155 if (src->oid_len <= root->oid_len)
157 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
158 /* n = */ root->oid_len) != 0)
161 memset(dst, 0, sizeof(*dst));
162 dst->oid_len = src->oid_len - root->oid_len;
163 memcpy(dst->oid, &src->oid[root->oid_len],
164 dst->oid_len * sizeof(dst->oid[0]));
168 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
170 char oid_str[MAX_OID_LEN][16];
171 char *oid_str_ptr[MAX_OID_LEN];
173 for (size_t i = 0; i < o->oid_len; i++) {
174 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
175 oid_str_ptr[i] = oid_str[i];
178 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
181 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
183 if (host->sess_handle == NULL)
186 snmp_sess_close(host->sess_handle);
187 host->sess_handle = NULL;
188 } /* }}} void csnmp_host_close_session */
190 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
192 host_definition_t *hd;
199 if (hd->name != NULL) {
200 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
203 csnmp_host_close_session(hd);
207 sfree(hd->community);
209 sfree(hd->auth_passphrase);
210 sfree(hd->priv_passphrase);
212 sfree(hd->data_list);
215 } /* }}} void csnmp_host_definition_destroy */
217 static void csnmp_data_definition_destroy(data_definition_t *dd) {
220 sfree(dd->plugin_name);
221 sfree(dd->plugin_instance.prefix);
222 sfree(dd->plugin_instance.value);
223 sfree(dd->type_instance.prefix);
224 sfree(dd->type_instance.value);
225 sfree(dd->host.prefix);
226 sfree(dd->host.value);
229 ignorelist_free(dd->ignorelist);
231 } /* void csnmp_data_definition_destroy */
233 /* Many functions to handle the configuration. {{{ */
234 /* First there are many functions which do configuration stuff. It's a big
235 * bloated and messy, I'm afraid. */
238 * Callgraph for the config stuff:
240 * +-> call_snmp_init_once
241 * +-> csnmp_config_add_data
242 * ! +-> csnmp_config_configure_data_instance
243 * ! +-> csnmp_config_add_data_values
244 * +-> csnmp_config_add_host
245 * +-> csnmp_config_add_host_version
246 * +-> csnmp_config_add_host_collect
247 * +-> csnmp_config_add_host_auth_protocol
248 * +-> csnmp_config_add_host_priv_protocol
249 * +-> csnmp_config_add_host_security_level
251 static void call_snmp_init_once(void) {
252 static int have_init;
255 init_snmp(PACKAGE_NAME);
257 } /* void call_snmp_init_once */
259 static int csnmp_config_configure_data_instance(instance_t *instance,
260 oconfig_item_t *ci) {
261 char buffer[DATA_MAX_NAME_LEN];
263 int status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
267 instance->configured = true;
269 if (strlen(buffer) == 0) {
273 instance->oid.oid_len = MAX_OID_LEN;
275 if (!read_objid(buffer, instance->oid.oid, &instance->oid.oid_len)) {
276 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
281 } /* int csnmp_config_configure_data_instance */
283 static int csnmp_config_add_data_values(data_definition_t *dd,
284 oconfig_item_t *ci) {
285 if (ci->values_num < 1) {
286 WARNING("snmp plugin: `Values' needs at least one argument.");
290 for (int i = 0; i < ci->values_num; i++)
291 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
292 WARNING("snmp plugin: `Values' needs only string argument.");
298 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
299 if (dd->values == NULL)
301 dd->values_len = (size_t)ci->values_num;
303 for (int i = 0; i < ci->values_num; i++) {
304 dd->values[i].oid_len = MAX_OID_LEN;
306 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
307 &dd->values[i].oid_len)) {
308 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
309 ci->values[i].value.string);
318 } /* int csnmp_config_configure_data_instance */
320 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
321 oconfig_item_t *ci) {
322 if (ci->values_num < 1)
325 for (int i = 0; i < ci->values_num; i++) {
326 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
327 WARNING("snmp plugin: `Ignore' needs only string argument.");
332 for (int i = 0; i < ci->values_num; ++i) {
333 if (strarray_add(&(dd->ignores), &(dd->ignores_len),
334 ci->values[i].value.string) != 0) {
335 ERROR("snmp plugin: Can't allocate memory");
336 strarray_free(dd->ignores, dd->ignores_len);
341 } /* int csnmp_config_add_data_blacklist */
343 static int csnmp_config_add_data_filter_values(data_definition_t *data,
344 oconfig_item_t *ci) {
345 if (ci->values_num < 1) {
346 WARNING("snmp plugin: `FilterValues' needs at least one argument.");
350 for (int i = 0; i < ci->values_num; i++) {
351 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
352 WARNING("snmp plugin: All arguments to `FilterValues' must be strings.");
355 ignorelist_add(data->ignorelist, ci->values[i].value.string);
359 } /* int csnmp_config_add_data_filter_values */
361 static int csnmp_config_add_data_filter_oid(data_definition_t *data,
362 oconfig_item_t *ci) {
364 char buffer[DATA_MAX_NAME_LEN];
365 int status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
369 data->filter_oid.oid_len = MAX_OID_LEN;
371 if (!read_objid(buffer, data->filter_oid.oid, &data->filter_oid.oid_len)) {
372 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
376 } /* int csnmp_config_add_data_filter_oid */
378 static int csnmp_config_add_data(oconfig_item_t *ci) {
379 data_definition_t *dd = calloc(1, sizeof(*dd));
383 int status = cf_util_get_string(ci, &dd->name);
394 dd->ignorelist = ignorelist_create(/* invert = */ 1);
395 if (dd->ignorelist == NULL) {
398 ERROR("snmp plugin: ignorelist_create() failed.");
402 dd->plugin_name = strdup("snmp");
403 if (dd->plugin_name == NULL) {
404 ERROR("snmp plugin: Can't allocate memory");
408 for (int i = 0; i < ci->children_num; i++) {
409 oconfig_item_t *option = ci->children + i;
411 if (strcasecmp("Type", option->key) == 0)
412 status = cf_util_get_string(option, &dd->type);
413 else if (strcasecmp("Table", option->key) == 0)
414 status = cf_util_get_boolean(option, &dd->is_table);
415 else if (strcasecmp("Plugin", option->key) == 0)
416 status = cf_util_get_string(option, &dd->plugin_name);
417 else if (strcasecmp("Instance", option->key) == 0) {
419 /* Instance is OID */
421 "snmp plugin: data %s: Option `Instance' is deprecated, please use "
422 "option `TypeInstanceOID'.",
425 csnmp_config_configure_data_instance(&dd->type_instance, option);
427 /* Instance is a simple string */
429 "snmp plugin: data %s: Option `Instance' is deprecated, please use "
430 "option `TypeInstance'.",
432 status = cf_util_get_string(option, &dd->type_instance.value);
434 } else if (strcasecmp("InstancePrefix", option->key) == 0) {
435 WARNING("snmp plugin: data %s: Option `InstancePrefix' is deprecated, "
436 "please use option `TypeInstancePrefix'.",
438 status = cf_util_get_string(option, &dd->type_instance.prefix);
439 } else if (strcasecmp("PluginInstance", option->key) == 0)
440 status = cf_util_get_string(option, &dd->plugin_instance.value);
441 else if (strcasecmp("TypeInstance", option->key) == 0)
442 status = cf_util_get_string(option, &dd->type_instance.value);
443 else if (strcasecmp("PluginInstanceOID", option->key) == 0)
445 csnmp_config_configure_data_instance(&dd->plugin_instance, option);
446 else if (strcasecmp("PluginInstancePrefix", option->key) == 0)
447 status = cf_util_get_string(option, &dd->plugin_instance.prefix);
448 else if (strcasecmp("TypeInstanceOID", option->key) == 0)
449 status = csnmp_config_configure_data_instance(&dd->type_instance, option);
450 else if (strcasecmp("TypeInstancePrefix", option->key) == 0)
451 status = cf_util_get_string(option, &dd->type_instance.prefix);
452 else if (strcasecmp("HostOID", option->key) == 0)
453 status = csnmp_config_configure_data_instance(&dd->host, option);
454 else if (strcasecmp("HostPrefix", option->key) == 0)
455 status = cf_util_get_string(option, &dd->host.prefix);
456 else if (strcasecmp("Values", option->key) == 0)
457 status = csnmp_config_add_data_values(dd, option);
458 else if (strcasecmp("Shift", option->key) == 0)
459 status = cf_util_get_double(option, &dd->shift);
460 else if (strcasecmp("Scale", option->key) == 0)
461 status = cf_util_get_double(option, &dd->scale);
462 else if (strcasecmp("Ignore", option->key) == 0)
463 status = csnmp_config_add_data_blacklist(dd, option);
464 else if (strcasecmp("InvertMatch", option->key) == 0)
465 status = cf_util_get_boolean(option, &dd->invert_match);
466 else if (strcasecmp("FilterOID", option->key) == 0) {
467 status = csnmp_config_add_data_filter_oid(dd, option);
468 } else if (strcasecmp("FilterValues", option->key) == 0) {
469 status = csnmp_config_add_data_filter_values(dd, option);
470 } else if (strcasecmp("FilterIgnoreSelected", option->key) == 0) {
472 status = cf_util_get_boolean(option, &t);
474 ignorelist_set_invert(dd->ignorelist, /* invert = */ !t);
476 WARNING("snmp plugin: data %s: Option `%s' not allowed here.", dd->name,
483 } /* for (ci->children) */
485 while (status == 0) {
487 /* Set type_instance to SUBID by default */
488 if (!dd->plugin_instance.configured && !dd->host.configured)
489 dd->type_instance.configured = true;
491 if (dd->plugin_instance.value && dd->plugin_instance.configured) {
493 "snmp plugin: data %s: Option `PluginInstance' will be ignored.",
496 if (dd->type_instance.value && dd->type_instance.configured) {
497 WARNING("snmp plugin: data %s: Option `TypeInstance' will be ignored.",
500 if (dd->type_instance.prefix && !dd->type_instance.configured) {
501 WARNING("snmp plugin: data %s: Option `TypeInstancePrefix' will be "
505 if (dd->plugin_instance.prefix && !dd->plugin_instance.configured) {
506 WARNING("snmp plugin: data %s: Option `PluginInstancePrefix' will be "
510 if (dd->host.prefix && !dd->host.configured) {
511 WARNING("snmp plugin: data %s: Option `HostPrefix' will be ignored.",
515 if (dd->plugin_instance.oid.oid_len > 0) {
516 WARNING("snmp plugin: data %s: Option `PluginInstanceOID' will be "
520 if (dd->type_instance.oid.oid_len > 0) {
522 "snmp plugin: data %s: Option `TypeInstanceOID' will be ignored.",
525 if (dd->type_instance.prefix) {
526 WARNING("snmp plugin: data %s: Option `TypeInstancePrefix' is ignored "
531 if (dd->plugin_instance.prefix) {
532 WARNING("snmp plugin: data %s: Option `PluginInstancePrefix' is "
534 "`Table' set to `false'.",
537 if (dd->host.prefix) {
539 "snmp plugin: data %s: Option `HostPrefix' is ignored when `Table' "
545 if (dd->type == NULL) {
546 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
550 if (dd->values == NULL) {
551 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
557 } /* while (status == 0) */
560 csnmp_data_definition_destroy(dd);
564 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
566 dd->name, dd->type, (dd->is_table) ? "true" : "false", dd->values_len);
568 DEBUG("snmp plugin: plugin_instance = %s, type_instance = %s,",
569 dd->plugin_instance.value, dd->type_instance.value);
571 DEBUG("snmp plugin: type_instance_by_oid = %s, plugin_instance_by_oid "
573 (dd->type_instance.oid.oid_len > 0)
575 : ((dd->type_instance.configured) ? "SUBID" : "false"),
576 (dd->plugin_instance.oid.oid_len > 0)
578 : ((dd->plugin_instance.configured) ? "SUBID" : "false"));
580 if (data_head == NULL)
583 data_definition_t *last;
585 while (last->next != NULL)
591 } /* int csnmp_config_add_data */
593 static int csnmp_config_add_host_version(host_definition_t *hd,
594 oconfig_item_t *ci) {
597 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
598 WARNING("snmp plugin: The `Version' config option needs exactly one number "
603 version = (int)ci->values[0].value.number;
604 if ((version < 1) || (version > 3)) {
605 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
609 hd->version = version;
612 } /* int csnmp_config_add_host_version */
614 static int csnmp_config_add_host_collect(host_definition_t *host,
615 oconfig_item_t *ci) {
616 data_definition_t *data;
617 data_definition_t **data_list;
620 if (ci->values_num < 1) {
621 WARNING("snmp plugin: `Collect' needs at least one argument.");
625 for (int i = 0; i < ci->values_num; i++)
626 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
627 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
631 data_list_len = host->data_list_len + ci->values_num;
633 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
634 if (data_list == NULL)
636 host->data_list = data_list;
638 for (int i = 0; i < ci->values_num; i++) {
639 for (data = data_head; data != NULL; data = data->next)
640 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
644 WARNING("snmp plugin: No such data configured: `%s'",
645 ci->values[i].value.string);
649 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
650 host->data_list_len, data->name);
652 host->data_list[host->data_list_len] = data;
653 host->data_list_len++;
654 } /* for (values_num) */
657 } /* int csnmp_config_add_host_collect */
659 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
660 oconfig_item_t *ci) {
664 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
668 if (strcasecmp("MD5", buffer) == 0) {
669 hd->auth_protocol = usmHMACMD5AuthProtocol;
670 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
671 } else if (strcasecmp("SHA", buffer) == 0) {
672 hd->auth_protocol = usmHMACSHA1AuthProtocol;
673 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
675 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
680 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
681 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
684 } /* int csnmp_config_add_host_auth_protocol */
686 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
687 oconfig_item_t *ci) {
691 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
695 if (strcasecmp("AES", buffer) == 0) {
696 hd->priv_protocol = usmAESPrivProtocol;
697 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
698 } else if (strcasecmp("DES", buffer) == 0) {
699 hd->priv_protocol = usmDESPrivProtocol;
700 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
702 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
707 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
708 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
711 } /* int csnmp_config_add_host_priv_protocol */
713 static int csnmp_config_add_host_security_level(host_definition_t *hd,
714 oconfig_item_t *ci) {
718 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
722 if (strcasecmp("noAuthNoPriv", buffer) == 0)
723 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
724 else if (strcasecmp("authNoPriv", buffer) == 0)
725 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
726 else if (strcasecmp("authPriv", buffer) == 0)
727 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
729 WARNING("snmp plugin: The `SecurityLevel' config option must be "
730 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
734 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
738 } /* int csnmp_config_add_host_security_level */
740 static int csnmp_config_add_host(oconfig_item_t *ci) {
741 host_definition_t *hd;
744 /* Registration stuff. */
745 cdtime_t interval = 0;
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;
762 /* These mean that we have not set a timeout or retry value */
766 for (int i = 0; i < ci->children_num; i++) {
767 oconfig_item_t *option = ci->children + i;
769 if (strcasecmp("Address", option->key) == 0)
770 status = cf_util_get_string(option, &hd->address);
771 else if (strcasecmp("Community", option->key) == 0)
772 status = cf_util_get_string(option, &hd->community);
773 else if (strcasecmp("Version", option->key) == 0)
774 status = csnmp_config_add_host_version(hd, option);
775 else if (strcasecmp("Timeout", option->key) == 0)
776 status = cf_util_get_cdtime(option, &hd->timeout);
777 else if (strcasecmp("Retries", option->key) == 0)
778 status = cf_util_get_int(option, &hd->retries);
779 else if (strcasecmp("Collect", option->key) == 0)
780 status = csnmp_config_add_host_collect(hd, option);
781 else if (strcasecmp("Interval", option->key) == 0)
782 status = cf_util_get_cdtime(option, &interval);
783 else if (strcasecmp("Username", option->key) == 0)
784 status = cf_util_get_string(option, &hd->username);
785 else if (strcasecmp("AuthProtocol", option->key) == 0)
786 status = csnmp_config_add_host_auth_protocol(hd, option);
787 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
788 status = csnmp_config_add_host_priv_protocol(hd, option);
789 else if (strcasecmp("AuthPassphrase", option->key) == 0)
790 status = cf_util_get_string(option, &hd->auth_passphrase);
791 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
792 status = cf_util_get_string(option, &hd->priv_passphrase);
793 else if (strcasecmp("SecurityLevel", option->key) == 0)
794 status = csnmp_config_add_host_security_level(hd, option);
795 else if (strcasecmp("Context", option->key) == 0)
796 status = cf_util_get_string(option, &hd->context);
799 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
806 } /* for (ci->children) */
808 while (status == 0) {
809 if (hd->address == NULL) {
810 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
814 if (hd->community == NULL && hd->version < 3) {
815 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
819 if (hd->version == 3) {
820 if (hd->username == NULL) {
821 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
825 if (hd->security_level == 0) {
826 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
831 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
832 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
833 if (hd->auth_protocol == NULL) {
834 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
839 if (hd->auth_passphrase == NULL) {
840 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
846 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
847 if (hd->priv_protocol == NULL) {
848 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
853 if (hd->priv_passphrase == NULL) {
854 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
863 } /* while (status == 0) */
866 csnmp_host_definition_destroy(hd);
870 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
872 hd->name, hd->address, hd->community, hd->version);
874 snprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
876 status = plugin_register_complex_read(
877 /* group = */ NULL, cb_name, csnmp_read_host, interval,
879 .data = hd, .free_func = csnmp_host_definition_destroy,
882 ERROR("snmp plugin: Registering complex read function failed.");
887 } /* int csnmp_config_add_host */
889 static int csnmp_config(oconfig_item_t *ci) {
890 call_snmp_init_once();
892 for (int i = 0; i < ci->children_num; i++) {
893 oconfig_item_t *child = ci->children + i;
894 if (strcasecmp("Data", child->key) == 0)
895 csnmp_config_add_data(child);
896 else if (strcasecmp("Host", child->key) == 0)
897 csnmp_config_add_host(child);
899 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
901 } /* for (ci->children) */
904 } /* int csnmp_config */
906 /* }}} End of the config stuff. Now the interesting part begins */
908 static void csnmp_host_open_session(host_definition_t *host) {
909 struct snmp_session sess;
912 if (host->sess_handle != NULL)
913 csnmp_host_close_session(host);
915 snmp_sess_init(&sess);
916 sess.peername = host->address;
917 switch (host->version) {
919 sess.version = SNMP_VERSION_1;
922 sess.version = SNMP_VERSION_3;
925 sess.version = SNMP_VERSION_2c;
929 if (host->version == 3) {
930 sess.securityName = host->username;
931 sess.securityNameLen = strlen(host->username);
932 sess.securityLevel = host->security_level;
934 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
935 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
936 sess.securityAuthProto = host->auth_protocol;
937 sess.securityAuthProtoLen = host->auth_protocol_len;
938 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
939 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
940 (u_char *)host->auth_passphrase,
941 strlen(host->auth_passphrase), sess.securityAuthKey,
942 &sess.securityAuthKeyLen);
943 if (error != SNMPERR_SUCCESS) {
944 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
950 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
951 sess.securityPrivProto = host->priv_protocol;
952 sess.securityPrivProtoLen = host->priv_protocol_len;
953 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
954 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
955 (u_char *)host->priv_passphrase,
956 strlen(host->priv_passphrase), sess.securityPrivKey,
957 &sess.securityPrivKeyLen);
958 if (error != SNMPERR_SUCCESS) {
959 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
965 if (host->context != NULL) {
966 sess.contextName = host->context;
967 sess.contextNameLen = strlen(host->context);
969 } else /* SNMPv1/2 "authenticates" with community string */
971 sess.community = (u_char *)host->community;
972 sess.community_len = strlen(host->community);
975 /* Set timeout & retries, if they have been changed from the default */
976 if (host->timeout != 0) {
977 /* net-snmp expects microseconds */
978 sess.timeout = CDTIME_T_TO_US(host->timeout);
980 if (host->retries >= 0) {
981 sess.retries = host->retries;
984 /* snmp_sess_open will copy the `struct snmp_session *'. */
985 host->sess_handle = snmp_sess_open(&sess);
987 if (host->sess_handle == NULL) {
990 snmp_error(&sess, NULL, NULL, &errstr);
992 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
993 (errstr == NULL) ? "Unknown problem" : errstr);
996 } /* void csnmp_host_open_session */
998 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
1000 static value_t csnmp_value_list_to_value(const struct variable_list *vl,
1001 int type, double scale, double shift,
1002 const char *host_name,
1003 const char *data_name) {
1005 uint64_t tmp_unsigned = 0;
1006 int64_t tmp_signed = 0;
1008 /* Set to true when the original SNMP type appears to have been signed. */
1009 bool prefer_signed = 0;
1011 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
1012 (vl->type == ASN_COUNTER)
1013 #ifdef ASN_TIMETICKS
1014 || (vl->type == ASN_TIMETICKS)
1016 || (vl->type == ASN_GAUGE)) {
1017 tmp_unsigned = (uint32_t)*vl->val.integer;
1018 tmp_signed = (int32_t)*vl->val.integer;
1020 if (vl->type == ASN_INTEGER)
1023 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
1024 } else if (vl->type == ASN_COUNTER64) {
1025 tmp_unsigned = (uint32_t)vl->val.counter64->high;
1026 tmp_unsigned = tmp_unsigned << 32;
1027 tmp_unsigned += (uint32_t)vl->val.counter64->low;
1028 tmp_signed = (int64_t)tmp_unsigned;
1029 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
1030 } else if (vl->type == ASN_OCTET_STR) {
1031 /* We'll handle this later.. */
1033 char oid_buffer[1024] = {0};
1035 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
1039 if (vl->type == ASN_NULL)
1040 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
1043 WARNING("snmp plugin: I don't know the ASN type #%i "
1044 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
1045 (int)vl->type, oid_buffer,
1046 (data_name != NULL) ? data_name : "UNKNOWN",
1047 (host_name != NULL) ? host_name : "UNKNOWN");
1052 if (vl->type == ASN_OCTET_STR) {
1055 if (vl->val.string != NULL) {
1057 size_t string_length;
1059 string_length = sizeof(string) - 1;
1060 if (vl->val_len < string_length)
1061 string_length = vl->val_len;
1063 /* The strings we get from the Net-SNMP library may not be null
1064 * terminated. That is why we're using `memcpy' here and not `strcpy'.
1065 * `string_length' is set to `vl->val_len' which holds the length of the
1067 memcpy(string, vl->val.string, string_length);
1068 string[string_length] = 0;
1070 status = parse_value(string, &ret, type);
1072 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
1074 (host_name != NULL) ? host_name : "UNKNOWN",
1075 DS_TYPE_TO_STRING(type), string);
1081 case DS_TYPE_COUNTER:
1082 case DS_TYPE_DERIVE:
1083 case DS_TYPE_ABSOLUTE:
1084 memset(&ret, 0, sizeof(ret));
1092 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
1093 "data source type: %i.",
1098 } /* if (vl->type == ASN_OCTET_STR) */
1099 else if (type == DS_TYPE_COUNTER) {
1100 ret.counter = tmp_unsigned;
1101 } else if (type == DS_TYPE_GAUGE) {
1104 else if (prefer_signed)
1105 ret.gauge = (scale * tmp_signed) + shift;
1107 ret.gauge = (scale * tmp_unsigned) + shift;
1108 } else if (type == DS_TYPE_DERIVE) {
1110 ret.derive = (derive_t)tmp_signed;
1112 ret.derive = (derive_t)tmp_unsigned;
1113 } else if (type == DS_TYPE_ABSOLUTE) {
1114 ret.absolute = (absolute_t)tmp_unsigned;
1116 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1123 } /* value_t csnmp_value_list_to_value */
1125 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1126 * representation and writes it to dst. Returns zero on success and ENOMEM if
1127 * dst is not large enough to hold the string. dst is guaranteed to be
1128 * nul-terminated. */
1129 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
1130 const struct variable_list *vb,
1138 buffer_free = dst_size;
1140 for (size_t i = 0; i < vb->val_len; i++) {
1143 status = snprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
1144 (unsigned int)vb->val.bitstring[i]);
1145 assert(status >= 0);
1147 if (((size_t)status) >= buffer_free) /* truncated */
1149 dst[dst_size - 1] = 0;
1151 } else /* if (status < buffer_free) */
1153 buffer_ptr += (size_t)status;
1154 buffer_free -= (size_t)status;
1159 } /* }}} int csnmp_strvbcopy_hexstring */
1161 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1162 * dst. If non-printable characters are detected, it will switch to a hex
1163 * representation of the string. Returns zero on success, EINVAL if vb does not
1164 * contain a string and ENOMEM if dst is not large enough to contain the
1166 static int csnmp_strvbcopy(char *dst, /* {{{ */
1167 const struct variable_list *vb, size_t dst_size) {
1171 if (vb->type == ASN_OCTET_STR)
1172 src = (char *)vb->val.string;
1173 else if (vb->type == ASN_BIT_STR)
1174 src = (char *)vb->val.bitstring;
1175 else if (vb->type == ASN_IPADDRESS) {
1176 return snprintf(dst, dst_size,
1177 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1178 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1179 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1185 num_chars = dst_size - 1;
1186 if (num_chars > vb->val_len)
1187 num_chars = vb->val_len;
1189 for (size_t i = 0; i < num_chars; i++) {
1190 /* Check for control characters. */
1191 if ((unsigned char)src[i] < 32)
1192 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1196 dst[dst_size - 1] = 0;
1198 if (dst_size <= vb->val_len)
1202 } /* }}} int csnmp_strvbcopy */
1204 static csnmp_cell_char_t *csnmp_get_char_cell(const struct variable_list *vb,
1205 const oid_t *root_oid,
1206 const host_definition_t *hd,
1207 const data_definition_t *dd) {
1212 csnmp_cell_char_t *il = calloc(1, sizeof(*il));
1214 ERROR("snmp plugin: calloc failed.");
1220 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1222 if (csnmp_oid_suffix(&il->suffix, &vb_name, root_oid) != 0) {
1228 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1229 (vb->type == ASN_IPADDRESS)) {
1231 csnmp_strvbcopy(il->value, vb, sizeof(il->value));
1234 value_t val = csnmp_value_list_to_value(
1235 vb, DS_TYPE_COUNTER,
1236 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1237 snprintf(il->value, sizeof(il->value), "%" PRIu64, (uint64_t)val.counter);
1241 } /* csnmp_cell_char_t csnmp_get_char_cell */
1243 static void csnmp_cells_append(csnmp_cell_char_t **head,
1244 csnmp_cell_char_t **tail,
1245 csnmp_cell_char_t *il) {
1251 } /* void csnmp_cells_append */
1253 static bool csnmp_ignore_instance(csnmp_cell_char_t *cell,
1254 const data_definition_t *dd) {
1255 bool is_matched = 0;
1256 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1257 int status = fnmatch(dd->ignores[i], cell->value, 0);
1259 if (!dd->invert_match) {
1267 if (dd->invert_match && !is_matched) {
1271 } /* bool csnmp_ignore_instance */
1273 static void csnmp_cell_replace_reserved_chars(csnmp_cell_char_t *cell) {
1274 for (char *ptr = cell->value; *ptr != '\0'; ptr++) {
1275 if ((*ptr > 0) && (*ptr < 32))
1277 else if (*ptr == '/')
1280 } /* void csnmp_cell_replace_reserved_chars */
1282 static int csnmp_dispatch_table(host_definition_t *host,
1283 data_definition_t *data,
1284 csnmp_cell_char_t *type_instance_cells,
1285 csnmp_cell_char_t *plugin_instance_cells,
1286 csnmp_cell_char_t *hostname_cells,
1287 csnmp_cell_char_t *filter_cells,
1288 csnmp_cell_value_t **value_cells) {
1289 const data_set_t *ds;
1290 value_list_t vl = VALUE_LIST_INIT;
1292 csnmp_cell_char_t *type_instance_cell_ptr = type_instance_cells;
1293 csnmp_cell_char_t *plugin_instance_cell_ptr = plugin_instance_cells;
1294 csnmp_cell_char_t *hostname_cell_ptr = hostname_cells;
1295 csnmp_cell_char_t *filter_cell_ptr = filter_cells;
1296 csnmp_cell_value_t *value_cell_ptr[data->values_len];
1300 oid_t current_suffix;
1302 ds = plugin_get_ds(data->type);
1304 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1307 assert(ds->ds_num == data->values_len);
1308 assert(data->values_len > 0);
1310 for (i = 0; i < data->values_len; i++)
1311 value_cell_ptr[i] = value_cells[i];
1313 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1314 sstrncpy(vl.type, data->type, sizeof(vl.type));
1318 bool suffix_skipped = 0;
1320 /* Determine next suffix to handle. */
1321 if (type_instance_cells != NULL) {
1322 if (type_instance_cell_ptr == NULL) {
1327 memcpy(¤t_suffix, &type_instance_cell_ptr->suffix,
1328 sizeof(current_suffix));
1330 /* no instance configured */
1331 csnmp_cell_value_t *ptr = value_cell_ptr[0];
1337 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1341 char oid_buffer[1024] = {0};
1342 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, current_suffix.oid,
1343 current_suffix.oid_len);
1344 DEBUG("SNMP PLUGIN: SUFFIX %s", oid_buffer);
1347 /* Update plugin_instance_cell_ptr to point expected suffix */
1348 if (plugin_instance_cells != NULL) {
1349 while ((plugin_instance_cell_ptr != NULL) &&
1350 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1351 ¤t_suffix) < 0))
1352 plugin_instance_cell_ptr = plugin_instance_cell_ptr->next;
1354 if (plugin_instance_cell_ptr == NULL) {
1359 if (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1360 ¤t_suffix) > 0) {
1361 /* This suffix is missing in the subtree. Indicate this with the
1362 * "suffix_skipped" flag and try the next instance / suffix. */
1367 /* Update hostname_cell_ptr to point expected suffix */
1368 if (hostname_cells != NULL) {
1370 (hostname_cell_ptr != NULL) &&
1371 (csnmp_oid_compare(&hostname_cell_ptr->suffix, ¤t_suffix) < 0))
1372 hostname_cell_ptr = hostname_cell_ptr->next;
1374 if (hostname_cell_ptr == NULL) {
1379 if (csnmp_oid_compare(&hostname_cell_ptr->suffix, ¤t_suffix) > 0) {
1380 /* This suffix is missing in the subtree. Indicate this with the
1381 * "suffix_skipped" flag and try the next instance / suffix. */
1386 /* Update filter_cell_ptr to point expected suffix */
1387 if (filter_cells != NULL) {
1388 while ((filter_cell_ptr != NULL) &&
1389 (csnmp_oid_compare(&filter_cell_ptr->suffix, ¤t_suffix) < 0))
1390 filter_cell_ptr = filter_cell_ptr->next;
1392 if (filter_cell_ptr == NULL) {
1397 if (csnmp_oid_compare(&filter_cell_ptr->suffix, ¤t_suffix) > 0) {
1398 /* This suffix is missing in the subtree. Indicate this with the
1399 * "suffix_skipped" flag and try the next instance / suffix. */
1404 /* Update all the value_cell_ptr to point at the entry with the same
1405 * trailing partial OID */
1406 for (i = 0; i < data->values_len; i++) {
1408 (value_cell_ptr[i] != NULL) &&
1409 (csnmp_oid_compare(&value_cell_ptr[i]->suffix, ¤t_suffix) < 0))
1410 value_cell_ptr[i] = value_cell_ptr[i]->next;
1412 if (value_cell_ptr[i] == NULL) {
1415 } else if (csnmp_oid_compare(&value_cell_ptr[i]->suffix,
1416 ¤t_suffix) > 0) {
1417 /* This suffix is missing in the subtree. Indicate this with the
1418 * "suffix_skipped" flag and try the next instance / suffix. */
1422 } /* for (i = 0; i < columns; i++) */
1427 /* Matching the values failed. Start from the beginning again. */
1428 if (suffix_skipped) {
1429 if (type_instance_cells != NULL)
1430 type_instance_cell_ptr = type_instance_cell_ptr->next;
1432 value_cell_ptr[0] = value_cell_ptr[0]->next;
1437 /* if we reach this line, all value_cell_ptr[i] are non-NULL and are set
1438 * to the same subid. type_instance_cell_ptr is either NULL or points to the
1439 * same subid, too. */
1441 for (i = 1; i < data->values_len; i++) {
1442 assert(value_cell_ptr[i] != NULL);
1443 assert(csnmp_oid_compare(&value_cell_ptr[i - 1]->suffix,
1444 &value_cell_ptr[i]->suffix) == 0);
1446 assert((type_instance_cell_ptr == NULL) ||
1447 (csnmp_oid_compare(&type_instance_cell_ptr->suffix,
1448 &value_cell_ptr[0]->suffix) == 0));
1449 assert((plugin_instance_cell_ptr == NULL) ||
1450 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1451 &value_cell_ptr[0]->suffix) == 0));
1452 assert((hostname_cell_ptr == NULL) ||
1453 (csnmp_oid_compare(&hostname_cell_ptr->suffix,
1454 &value_cell_ptr[0]->suffix) == 0));
1455 assert((filter_cell_ptr == NULL) ||
1456 (csnmp_oid_compare(&filter_cell_ptr->suffix,
1457 &value_cell_ptr[0]->suffix) == 0));
1460 /* Check the value in filter column */
1461 if (filter_cell_ptr &&
1462 ignorelist_match(data->ignorelist, filter_cell_ptr->value) != 0) {
1463 if (type_instance_cells != NULL)
1464 type_instance_cell_ptr = type_instance_cell_ptr->next;
1466 value_cell_ptr[0] = value_cell_ptr[0]->next;
1472 if (data->host.configured) {
1473 char temp[DATA_MAX_NAME_LEN];
1474 if (hostname_cell_ptr == NULL)
1475 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1477 sstrncpy(temp, hostname_cell_ptr->value, sizeof(temp));
1479 if (data->host.prefix == NULL)
1480 sstrncpy(vl.host, temp, sizeof(vl.host));
1482 snprintf(vl.host, sizeof(vl.host), "%s%s", data->host.prefix, temp);
1484 sstrncpy(vl.host, host->name, sizeof(vl.host));
1487 /* set vl.type_instance */
1488 if (data->type_instance.configured) {
1489 char temp[DATA_MAX_NAME_LEN];
1490 if (type_instance_cell_ptr == NULL)
1491 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1493 sstrncpy(temp, type_instance_cell_ptr->value, sizeof(temp));
1495 if (data->type_instance.prefix == NULL)
1496 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1498 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1499 data->type_instance.prefix, temp);
1500 } else if (data->type_instance.value) {
1501 sstrncpy(vl.type_instance, data->type_instance.value,
1502 sizeof(vl.type_instance));
1505 /* set vl.plugin_instance */
1506 if (data->plugin_instance.configured) {
1507 char temp[DATA_MAX_NAME_LEN];
1508 if (plugin_instance_cell_ptr == NULL)
1509 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1511 sstrncpy(temp, plugin_instance_cell_ptr->value, sizeof(temp));
1513 if (data->plugin_instance.prefix == NULL)
1514 sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
1516 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
1517 data->plugin_instance.prefix, temp);
1518 } else if (data->plugin_instance.value) {
1519 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
1520 sizeof(vl.plugin_instance));
1523 vl.values_len = data->values_len;
1524 value_t values[vl.values_len];
1527 for (i = 0; i < data->values_len; i++)
1528 vl.values[i] = value_cell_ptr[i]->value;
1530 plugin_dispatch_values(&vl);
1532 /* prevent leakage of pointer to local variable. */
1536 if (type_instance_cells != NULL)
1537 type_instance_cell_ptr = type_instance_cell_ptr->next;
1539 value_cell_ptr[0] = value_cell_ptr[0]->next;
1540 } /* while (have_more) */
1543 } /* int csnmp_dispatch_table */
1545 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1546 struct snmp_pdu *req;
1547 struct snmp_pdu *res = NULL;
1548 struct variable_list *vb;
1550 const data_set_t *ds;
1552 size_t oid_list_len = data->values_len;
1554 if (data->type_instance.oid.oid_len > 0)
1557 if (data->plugin_instance.oid.oid_len > 0)
1560 if (data->host.oid.oid_len > 0)
1563 if (data->filter_oid.oid_len > 0)
1566 /* Holds the last OID returned by the device. We use this in the GETNEXT
1567 * request to proceed. */
1568 oid_t oid_list[oid_list_len];
1569 /* Set to false when an OID has left its subtree so we don't re-request it
1571 csnmp_oid_type_t oid_list_todo[oid_list_len];
1576 /* `value_list_head' and `value_cells_tail' implement a linked list for each
1577 * value. `instance_cells_head' and `instance_cells_tail' implement a linked
1578 * list of instance names. This is used to jump gaps in the table. */
1579 csnmp_cell_char_t *type_instance_cells_head = NULL;
1580 csnmp_cell_char_t *type_instance_cells_tail = NULL;
1581 csnmp_cell_char_t *plugin_instance_cells_head = NULL;
1582 csnmp_cell_char_t *plugin_instance_cells_tail = NULL;
1583 csnmp_cell_char_t *hostname_cells_head = NULL;
1584 csnmp_cell_char_t *hostname_cells_tail = NULL;
1585 csnmp_cell_char_t *filter_cells_head = NULL;
1586 csnmp_cell_char_t *filter_cells_tail = NULL;
1587 csnmp_cell_value_t **value_cells_head;
1588 csnmp_cell_value_t **value_cells_tail;
1590 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1593 if (host->sess_handle == NULL) {
1594 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1598 ds = plugin_get_ds(data->type);
1600 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1604 if (ds->ds_num != data->values_len) {
1605 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1606 " values, but config talks "
1608 data->type, ds->ds_num, data->values_len);
1611 assert(data->values_len > 0);
1613 for (i = 0; i < data->values_len; i++)
1614 oid_list_todo[i] = OID_TYPE_VARIABLE;
1616 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1617 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1619 if (data->type_instance.oid.oid_len > 0) {
1620 memcpy(oid_list + i, &data->type_instance.oid, sizeof(oid_t));
1621 oid_list_todo[i] = OID_TYPE_TYPEINSTANCE;
1625 if (data->plugin_instance.oid.oid_len > 0) {
1626 memcpy(oid_list + i, &data->plugin_instance.oid, sizeof(oid_t));
1627 oid_list_todo[i] = OID_TYPE_PLUGININSTANCE;
1631 if (data->host.oid.oid_len > 0) {
1632 memcpy(oid_list + i, &data->host.oid, sizeof(oid_t));
1633 oid_list_todo[i] = OID_TYPE_HOST;
1637 if (data->filter_oid.oid_len > 0) {
1638 memcpy(oid_list + i, &data->filter_oid, sizeof(oid_t));
1639 oid_list_todo[i] = OID_TYPE_FILTER;
1643 /* We're going to construct n linked lists, one for each "value".
1644 * value_cells_head will contain pointers to the heads of these linked lists,
1645 * value_cells_tail will contain pointers to the tail of the lists. */
1646 value_cells_head = calloc(data->values_len, sizeof(*value_cells_head));
1647 value_cells_tail = calloc(data->values_len, sizeof(*value_cells_tail));
1648 if ((value_cells_head == NULL) || (value_cells_tail == NULL)) {
1649 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1650 sfree(value_cells_head);
1651 sfree(value_cells_tail);
1656 while (status == 0) {
1657 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1659 ERROR("snmp plugin: snmp_pdu_create failed.");
1664 size_t oid_list_todo_num = 0;
1665 size_t var_idx[oid_list_len];
1666 memset(var_idx, 0, sizeof(var_idx));
1668 for (i = 0; i < oid_list_len; i++) {
1669 /* Do not rerequest already finished OIDs */
1670 if (!oid_list_todo[i])
1672 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1673 var_idx[oid_list_todo_num] = i;
1674 oid_list_todo_num++;
1677 if (oid_list_todo_num == 0) {
1678 /* The request is still empty - so we are finished */
1679 DEBUG("snmp plugin: all variables have left their subtree");
1686 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1688 /* snmp_sess_synch_response always frees our req PDU */
1691 if ((status != STAT_SUCCESS) || (res == NULL)) {
1692 char *errstr = NULL;
1694 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1696 c_complain(LOG_ERR, &host->complaint,
1697 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1698 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1705 csnmp_host_close_session(host);
1712 assert(res != NULL);
1713 c_release(LOG_INFO, &host->complaint,
1714 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1717 vb = res->variables;
1723 if (res->errstat != SNMP_ERR_NOERROR) {
1724 if (res->errindex != 0) {
1725 /* Find the OID which caused error */
1726 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1727 vb = vb->next_variable, i++)
1731 if ((res->errindex == 0) || (vb == NULL)) {
1732 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1733 host->name, data->name, snmp_errstring(res->errstat),
1739 char oid_buffer[1024] = {0};
1740 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1742 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1743 data->name, oid_buffer, snmp_errstring(res->errstat));
1745 /* Get value index from todo list and skip OID found */
1746 assert(res->errindex <= oid_list_todo_num);
1747 i = var_idx[res->errindex - 1];
1748 assert(i < oid_list_len);
1749 oid_list_todo[i] = 0;
1756 for (vb = res->variables, i = 0; (vb != NULL);
1757 vb = vb->next_variable, i++) {
1758 /* Calculate value index from todo list */
1759 while ((i < oid_list_len) && !oid_list_todo[i]) {
1762 if (i >= oid_list_len) {
1766 /* An instance is configured and the res variable we process is the
1768 if (oid_list_todo[i] == OID_TYPE_TYPEINSTANCE) {
1769 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1770 (snmp_oid_ncompare(data->type_instance.oid.oid,
1771 data->type_instance.oid.oid_len, vb->name,
1773 data->type_instance.oid.oid_len) != 0)) {
1774 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1776 host->name, data->name);
1777 oid_list_todo[i] = 0;
1781 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1782 * add it to the list */
1783 csnmp_cell_char_t *cell =
1784 csnmp_get_char_cell(vb, &data->type_instance.oid, host, data);
1786 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1792 if (csnmp_ignore_instance(cell, data)) {
1795 csnmp_cell_replace_reserved_chars(cell);
1797 DEBUG("snmp plugin: il->type_instance = `%s';", cell->value);
1798 csnmp_cells_append(&type_instance_cells_head,
1799 &type_instance_cells_tail, cell);
1801 } else if (oid_list_todo[i] == OID_TYPE_PLUGININSTANCE) {
1802 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1803 (snmp_oid_ncompare(data->plugin_instance.oid.oid,
1804 data->plugin_instance.oid.oid_len, vb->name,
1806 data->plugin_instance.oid.oid_len) != 0)) {
1807 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1809 host->name, data->name);
1810 oid_list_todo[i] = 0;
1814 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1815 * add it to the list */
1816 csnmp_cell_char_t *cell =
1817 csnmp_get_char_cell(vb, &data->plugin_instance.oid, host, data);
1819 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1825 csnmp_cell_replace_reserved_chars(cell);
1827 DEBUG("snmp plugin: il->plugin_instance = `%s';", cell->value);
1828 csnmp_cells_append(&plugin_instance_cells_head,
1829 &plugin_instance_cells_tail, cell);
1830 } else if (oid_list_todo[i] == OID_TYPE_HOST) {
1831 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1832 (snmp_oid_ncompare(data->host.oid.oid, data->host.oid.oid_len,
1833 vb->name, vb->name_length,
1834 data->host.oid.oid_len) != 0)) {
1835 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1836 host->name, data->name);
1837 oid_list_todo[i] = 0;
1841 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1842 * add it to the list */
1843 csnmp_cell_char_t *cell =
1844 csnmp_get_char_cell(vb, &data->host.oid, host, data);
1846 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1852 csnmp_cell_replace_reserved_chars(cell);
1854 DEBUG("snmp plugin: il->hostname = `%s';", cell->value);
1855 csnmp_cells_append(&hostname_cells_head, &hostname_cells_tail, cell);
1856 } else if (oid_list_todo[i] == OID_TYPE_FILTER) {
1857 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1858 (snmp_oid_ncompare(data->filter_oid.oid, data->filter_oid.oid_len,
1859 vb->name, vb->name_length,
1860 data->filter_oid.oid_len) != 0)) {
1861 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1862 host->name, data->name);
1863 oid_list_todo[i] = 0;
1867 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1868 * add it to the list */
1869 csnmp_cell_char_t *cell =
1870 csnmp_get_char_cell(vb, &data->filter_oid, host, data);
1872 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1878 csnmp_cell_replace_reserved_chars(cell);
1880 DEBUG("snmp plugin: il->filter = `%s';", cell->value);
1881 csnmp_cells_append(&filter_cells_head, &filter_cells_tail, cell);
1882 } else /* The variable we are processing is a normal value */
1884 assert(oid_list_todo[i] == OID_TYPE_VARIABLE);
1886 csnmp_cell_value_t *vt;
1891 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1893 /* Calculate the current suffix. This is later used to check that the
1894 * suffix is increasing. This also checks if we left the subtree */
1895 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1897 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1898 "Value probably left its subtree.",
1899 host->name, data->name, i);
1900 oid_list_todo[i] = 0;
1904 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1905 * our table matching algorithm will get confused. */
1906 if ((value_cells_tail[i] != NULL) &&
1907 (csnmp_oid_compare(&suffix, &value_cells_tail[i]->suffix) <= 0)) {
1908 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1909 "Suffix is not increasing.",
1910 host->name, data->name, i);
1911 oid_list_todo[i] = 0;
1915 vt = calloc(1, sizeof(*vt));
1917 ERROR("snmp plugin: calloc failed.");
1923 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1924 data->shift, host->name, data->name);
1925 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1928 if (value_cells_tail[i] == NULL)
1929 value_cells_head[i] = vt;
1931 value_cells_tail[i]->next = vt;
1932 value_cells_tail[i] = vt;
1935 /* Copy OID to oid_list[i] */
1936 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1937 oid_list[i].oid_len = vb->name_length;
1939 } /* for (vb = res->variables ...) */
1944 } /* while (status == 0) */
1951 csnmp_dispatch_table(host, data, type_instance_cells_head,
1952 plugin_instance_cells_head, hostname_cells_head,
1953 filter_cells_head, value_cells_head);
1955 /* Free all allocated variables here */
1956 while (type_instance_cells_head != NULL) {
1957 csnmp_cell_char_t *next = type_instance_cells_head->next;
1958 sfree(type_instance_cells_head);
1959 type_instance_cells_head = next;
1962 while (plugin_instance_cells_head != NULL) {
1963 csnmp_cell_char_t *next = plugin_instance_cells_head->next;
1964 sfree(plugin_instance_cells_head);
1965 plugin_instance_cells_head = next;
1968 while (hostname_cells_head != NULL) {
1969 csnmp_cell_char_t *next = hostname_cells_head->next;
1970 sfree(hostname_cells_head);
1971 hostname_cells_head = next;
1974 while (filter_cells_head != NULL) {
1975 csnmp_cell_char_t *next = filter_cells_head->next;
1976 sfree(filter_cells_head);
1977 filter_cells_head = next;
1980 for (i = 0; i < data->values_len; i++) {
1981 while (value_cells_head[i] != NULL) {
1982 csnmp_cell_value_t *next = value_cells_head[i]->next;
1983 sfree(value_cells_head[i]);
1984 value_cells_head[i] = next;
1988 sfree(value_cells_head);
1989 sfree(value_cells_tail);
1992 } /* int csnmp_read_table */
1994 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1995 struct snmp_pdu *req;
1996 struct snmp_pdu *res = NULL;
1997 struct variable_list *vb;
1999 const data_set_t *ds;
2000 value_list_t vl = VALUE_LIST_INIT;
2005 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
2008 if (host->sess_handle == NULL) {
2009 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
2013 ds = plugin_get_ds(data->type);
2015 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
2019 if (ds->ds_num != data->values_len) {
2020 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
2021 " values, but config talks "
2023 data->type, ds->ds_num, data->values_len);
2027 vl.values_len = ds->ds_num;
2028 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
2029 if (vl.values == NULL)
2031 for (i = 0; i < vl.values_len; i++) {
2032 if (ds->ds[i].type == DS_TYPE_COUNTER)
2033 vl.values[i].counter = 0;
2035 vl.values[i].gauge = NAN;
2038 sstrncpy(vl.host, host->name, sizeof(vl.host));
2039 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
2040 sstrncpy(vl.type, data->type, sizeof(vl.type));
2041 if (data->type_instance.value)
2042 sstrncpy(vl.type_instance, data->type_instance.value,
2043 sizeof(vl.type_instance));
2044 if (data->plugin_instance.value)
2045 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
2046 sizeof(vl.plugin_instance));
2048 req = snmp_pdu_create(SNMP_MSG_GET);
2050 ERROR("snmp plugin: snmp_pdu_create failed.");
2055 for (i = 0; i < data->values_len; i++)
2056 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
2058 status = snmp_sess_synch_response(host->sess_handle, req, &res);
2060 if ((status != STAT_SUCCESS) || (res == NULL)) {
2061 char *errstr = NULL;
2063 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
2064 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
2065 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
2072 csnmp_host_close_session(host);
2077 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
2080 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
2081 DEBUG("snmp plugin: Got this variable: %s", buffer);
2082 #endif /* COLLECT_DEBUG */
2084 for (i = 0; i < data->values_len; i++)
2085 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
2086 vb->name, vb->name_length) == 0)
2088 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
2089 data->shift, host->name, data->name);
2090 } /* for (res->variables) */
2094 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
2095 plugin_dispatch_values(&vl);
2099 } /* int csnmp_read_value */
2101 static int csnmp_read_host(user_data_t *ud) {
2102 host_definition_t *host;
2109 if (host->sess_handle == NULL)
2110 csnmp_host_open_session(host);
2112 if (host->sess_handle == NULL)
2116 for (i = 0; i < host->data_list_len; i++) {
2117 data_definition_t *data = host->data_list[i];
2120 status = csnmp_read_table(host, data);
2122 status = csnmp_read_value(host, data);
2132 } /* int csnmp_read_host */
2134 static int csnmp_init(void) {
2135 call_snmp_init_once();
2138 } /* int csnmp_init */
2140 static int csnmp_shutdown(void) {
2141 data_definition_t *data_this;
2142 data_definition_t *data_next;
2144 /* When we get here, the read threads have been stopped and all the
2145 * `host_definition_t' will be freed. */
2146 DEBUG("snmp plugin: Destroying all data definitions.");
2148 data_this = data_head;
2150 while (data_this != NULL) {
2151 data_next = data_this->next;
2153 csnmp_data_definition_destroy(data_this);
2155 data_this = data_next;
2159 } /* int csnmp_shutdown */
2161 void module_register(void) {
2162 plugin_register_complex_config("snmp", csnmp_config);
2163 plugin_register_init("snmp", csnmp_init);
2164 plugin_register_shutdown("snmp", csnmp_shutdown);
2165 } /* void module_register */