2 * collectd - src/snmp.c
3 * Copyright (C) 2007-2012 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils/common/common.h"
31 #include "utils/ignorelist/ignorelist.h"
32 #include "utils_complain.h"
34 #include <net-snmp/net-snmp-config.h>
35 #include <net-snmp/net-snmp-includes.h>
40 * Private data structes
46 typedef struct oid_s oid_t;
54 typedef struct instance_s instance_t;
56 struct data_definition_s {
57 char *name; /* used to reference this from the `Collect' option */
58 char *type; /* used to find the data_set */
60 instance_t type_instance;
61 instance_t plugin_instance;
64 ignorelist_t *ignorelist;
70 struct data_definition_s *next;
75 typedef struct data_definition_s data_definition_t;
77 struct host_definition_s {
84 /* snmpv1/2 options */
87 /* snmpv3 security options */
90 size_t auth_protocol_len;
91 char *auth_passphrase;
93 size_t priv_protocol_len;
94 char *priv_passphrase;
99 c_complain_t complaint;
100 data_definition_t **data_list;
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 ssnprintf(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 ssnprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
876 status = plugin_register_complex_read(
877 /* group = */ NULL, cb_name, csnmp_read_host, interval,
880 .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 = ssnprintf(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 ssnprintf(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 ssnprintf(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));
1319 bool suffix_skipped = 0;
1321 /* Determine next suffix to handle. */
1322 if (type_instance_cells != NULL) {
1323 if (type_instance_cell_ptr == NULL) {
1328 memcpy(¤t_suffix, &type_instance_cell_ptr->suffix,
1329 sizeof(current_suffix));
1331 /* no instance configured */
1332 csnmp_cell_value_t *ptr = value_cell_ptr[0];
1338 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1342 char oid_buffer[1024] = {0};
1343 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, current_suffix.oid,
1344 current_suffix.oid_len);
1345 DEBUG("SNMP PLUGIN: SUFFIX %s", oid_buffer);
1348 /* Update plugin_instance_cell_ptr to point expected suffix */
1349 if (plugin_instance_cells != NULL) {
1350 while ((plugin_instance_cell_ptr != NULL) &&
1351 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1352 ¤t_suffix) < 0))
1353 plugin_instance_cell_ptr = plugin_instance_cell_ptr->next;
1355 if (plugin_instance_cell_ptr == NULL) {
1360 if (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1361 ¤t_suffix) > 0) {
1362 /* This suffix is missing in the subtree. Indicate this with the
1363 * "suffix_skipped" flag and try the next instance / suffix. */
1368 /* Update hostname_cell_ptr to point expected suffix */
1369 if (hostname_cells != NULL) {
1371 (hostname_cell_ptr != NULL) &&
1372 (csnmp_oid_compare(&hostname_cell_ptr->suffix, ¤t_suffix) < 0))
1373 hostname_cell_ptr = hostname_cell_ptr->next;
1375 if (hostname_cell_ptr == NULL) {
1380 if (csnmp_oid_compare(&hostname_cell_ptr->suffix, ¤t_suffix) > 0) {
1381 /* This suffix is missing in the subtree. Indicate this with the
1382 * "suffix_skipped" flag and try the next instance / suffix. */
1387 /* Update filter_cell_ptr to point expected suffix */
1388 if (filter_cells != NULL) {
1389 while ((filter_cell_ptr != NULL) &&
1390 (csnmp_oid_compare(&filter_cell_ptr->suffix, ¤t_suffix) < 0))
1391 filter_cell_ptr = filter_cell_ptr->next;
1393 if (filter_cell_ptr == NULL) {
1398 if (csnmp_oid_compare(&filter_cell_ptr->suffix, ¤t_suffix) > 0) {
1399 /* This suffix is missing in the subtree. Indicate this with the
1400 * "suffix_skipped" flag and try the next instance / suffix. */
1405 /* Update all the value_cell_ptr to point at the entry with the same
1406 * trailing partial OID */
1407 for (i = 0; i < data->values_len; i++) {
1409 (value_cell_ptr[i] != NULL) &&
1410 (csnmp_oid_compare(&value_cell_ptr[i]->suffix, ¤t_suffix) < 0))
1411 value_cell_ptr[i] = value_cell_ptr[i]->next;
1413 if (value_cell_ptr[i] == NULL) {
1416 } else if (csnmp_oid_compare(&value_cell_ptr[i]->suffix,
1417 ¤t_suffix) > 0) {
1418 /* This suffix is missing in the subtree. Indicate this with the
1419 * "suffix_skipped" flag and try the next instance / suffix. */
1423 } /* for (i = 0; i < columns; i++) */
1428 /* Matching the values failed. Start from the beginning again. */
1429 if (suffix_skipped) {
1430 if (type_instance_cells != NULL)
1431 type_instance_cell_ptr = type_instance_cell_ptr->next;
1433 value_cell_ptr[0] = value_cell_ptr[0]->next;
1438 /* if we reach this line, all value_cell_ptr[i] are non-NULL and are set
1439 * to the same subid. type_instance_cell_ptr is either NULL or points to the
1440 * same subid, too. */
1442 for (i = 1; i < data->values_len; i++) {
1443 assert(value_cell_ptr[i] != NULL);
1444 assert(csnmp_oid_compare(&value_cell_ptr[i - 1]->suffix,
1445 &value_cell_ptr[i]->suffix) == 0);
1447 assert((type_instance_cell_ptr == NULL) ||
1448 (csnmp_oid_compare(&type_instance_cell_ptr->suffix,
1449 &value_cell_ptr[0]->suffix) == 0));
1450 assert((plugin_instance_cell_ptr == NULL) ||
1451 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1452 &value_cell_ptr[0]->suffix) == 0));
1453 assert((hostname_cell_ptr == NULL) ||
1454 (csnmp_oid_compare(&hostname_cell_ptr->suffix,
1455 &value_cell_ptr[0]->suffix) == 0));
1456 assert((filter_cell_ptr == NULL) ||
1457 (csnmp_oid_compare(&filter_cell_ptr->suffix,
1458 &value_cell_ptr[0]->suffix) == 0));
1461 /* Check the value in filter column */
1462 if (filter_cell_ptr &&
1463 ignorelist_match(data->ignorelist, filter_cell_ptr->value) != 0) {
1464 if (type_instance_cells != NULL)
1465 type_instance_cell_ptr = type_instance_cell_ptr->next;
1467 value_cell_ptr[0] = value_cell_ptr[0]->next;
1473 if (data->host.configured) {
1474 char temp[DATA_MAX_NAME_LEN];
1475 if (hostname_cell_ptr == NULL)
1476 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1478 sstrncpy(temp, hostname_cell_ptr->value, sizeof(temp));
1480 if (data->host.prefix == NULL)
1481 sstrncpy(vl.host, temp, sizeof(vl.host));
1483 ssnprintf(vl.host, sizeof(vl.host), "%s%s", data->host.prefix, temp);
1485 sstrncpy(vl.host, host->name, sizeof(vl.host));
1488 /* set vl.type_instance */
1489 if (data->type_instance.configured) {
1490 char temp[DATA_MAX_NAME_LEN];
1491 if (type_instance_cell_ptr == NULL)
1492 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1494 sstrncpy(temp, type_instance_cell_ptr->value, sizeof(temp));
1496 if (data->type_instance.prefix == NULL)
1497 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1499 ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1500 data->type_instance.prefix, temp);
1501 } else if (data->type_instance.value) {
1502 sstrncpy(vl.type_instance, data->type_instance.value,
1503 sizeof(vl.type_instance));
1506 /* set vl.plugin_instance */
1507 if (data->plugin_instance.configured) {
1508 char temp[DATA_MAX_NAME_LEN];
1509 if (plugin_instance_cell_ptr == NULL)
1510 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1512 sstrncpy(temp, plugin_instance_cell_ptr->value, sizeof(temp));
1514 if (data->plugin_instance.prefix == NULL)
1515 sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
1517 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
1518 data->plugin_instance.prefix, temp);
1519 } else if (data->plugin_instance.value) {
1520 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
1521 sizeof(vl.plugin_instance));
1524 vl.values_len = data->values_len;
1525 value_t values[vl.values_len];
1528 for (i = 0; i < data->values_len; i++)
1529 vl.values[i] = value_cell_ptr[i]->value;
1531 plugin_dispatch_values(&vl);
1533 /* prevent leakage of pointer to local variable. */
1537 if (type_instance_cells != NULL)
1538 type_instance_cell_ptr = type_instance_cell_ptr->next;
1540 value_cell_ptr[0] = value_cell_ptr[0]->next;
1541 } /* while (have_more) */
1544 } /* int csnmp_dispatch_table */
1546 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1547 struct snmp_pdu *req;
1548 struct snmp_pdu *res = NULL;
1549 struct variable_list *vb;
1551 const data_set_t *ds;
1553 size_t oid_list_len = data->values_len;
1555 if (data->type_instance.oid.oid_len > 0)
1558 if (data->plugin_instance.oid.oid_len > 0)
1561 if (data->host.oid.oid_len > 0)
1564 if (data->filter_oid.oid_len > 0)
1567 /* Holds the last OID returned by the device. We use this in the GETNEXT
1568 * request to proceed. */
1569 oid_t oid_list[oid_list_len];
1570 /* Set to false when an OID has left its subtree so we don't re-request it
1572 csnmp_oid_type_t oid_list_todo[oid_list_len];
1577 /* `value_list_head' and `value_cells_tail' implement a linked list for each
1578 * value. `instance_cells_head' and `instance_cells_tail' implement a linked
1579 * list of instance names. This is used to jump gaps in the table. */
1580 csnmp_cell_char_t *type_instance_cells_head = NULL;
1581 csnmp_cell_char_t *type_instance_cells_tail = NULL;
1582 csnmp_cell_char_t *plugin_instance_cells_head = NULL;
1583 csnmp_cell_char_t *plugin_instance_cells_tail = NULL;
1584 csnmp_cell_char_t *hostname_cells_head = NULL;
1585 csnmp_cell_char_t *hostname_cells_tail = NULL;
1586 csnmp_cell_char_t *filter_cells_head = NULL;
1587 csnmp_cell_char_t *filter_cells_tail = NULL;
1588 csnmp_cell_value_t **value_cells_head;
1589 csnmp_cell_value_t **value_cells_tail;
1591 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1594 if (host->sess_handle == NULL) {
1595 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1599 ds = plugin_get_ds(data->type);
1601 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1605 if (ds->ds_num != data->values_len) {
1606 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1607 " values, but config talks "
1609 data->type, ds->ds_num, data->values_len);
1612 assert(data->values_len > 0);
1614 for (i = 0; i < data->values_len; i++)
1615 oid_list_todo[i] = OID_TYPE_VARIABLE;
1617 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1618 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1620 if (data->type_instance.oid.oid_len > 0) {
1621 memcpy(oid_list + i, &data->type_instance.oid, sizeof(oid_t));
1622 oid_list_todo[i] = OID_TYPE_TYPEINSTANCE;
1626 if (data->plugin_instance.oid.oid_len > 0) {
1627 memcpy(oid_list + i, &data->plugin_instance.oid, sizeof(oid_t));
1628 oid_list_todo[i] = OID_TYPE_PLUGININSTANCE;
1632 if (data->host.oid.oid_len > 0) {
1633 memcpy(oid_list + i, &data->host.oid, sizeof(oid_t));
1634 oid_list_todo[i] = OID_TYPE_HOST;
1638 if (data->filter_oid.oid_len > 0) {
1639 memcpy(oid_list + i, &data->filter_oid, sizeof(oid_t));
1640 oid_list_todo[i] = OID_TYPE_FILTER;
1644 /* We're going to construct n linked lists, one for each "value".
1645 * value_cells_head will contain pointers to the heads of these linked lists,
1646 * value_cells_tail will contain pointers to the tail of the lists. */
1647 value_cells_head = calloc(data->values_len, sizeof(*value_cells_head));
1648 value_cells_tail = calloc(data->values_len, sizeof(*value_cells_tail));
1649 if ((value_cells_head == NULL) || (value_cells_tail == NULL)) {
1650 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1651 sfree(value_cells_head);
1652 sfree(value_cells_tail);
1657 while (status == 0) {
1658 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1660 ERROR("snmp plugin: snmp_pdu_create failed.");
1665 size_t oid_list_todo_num = 0;
1666 size_t var_idx[oid_list_len];
1667 memset(var_idx, 0, sizeof(var_idx));
1669 for (i = 0; i < oid_list_len; i++) {
1670 /* Do not rerequest already finished OIDs */
1671 if (!oid_list_todo[i])
1673 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1674 var_idx[oid_list_todo_num] = i;
1675 oid_list_todo_num++;
1678 if (oid_list_todo_num == 0) {
1679 /* The request is still empty - so we are finished */
1680 DEBUG("snmp plugin: all variables have left their subtree");
1687 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1689 /* snmp_sess_synch_response always frees our req PDU */
1692 if ((status != STAT_SUCCESS) || (res == NULL)) {
1693 char *errstr = NULL;
1695 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1697 c_complain(LOG_ERR, &host->complaint,
1698 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1699 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1706 csnmp_host_close_session(host);
1713 assert(res != NULL);
1714 c_release(LOG_INFO, &host->complaint,
1715 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1718 vb = res->variables;
1724 if (res->errstat != SNMP_ERR_NOERROR) {
1725 if (res->errindex != 0) {
1726 /* Find the OID which caused error */
1727 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1728 vb = vb->next_variable, i++)
1732 if ((res->errindex == 0) || (vb == NULL)) {
1733 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1734 host->name, data->name, snmp_errstring(res->errstat),
1740 char oid_buffer[1024] = {0};
1741 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1743 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1744 data->name, oid_buffer, snmp_errstring(res->errstat));
1746 /* Get value index from todo list and skip OID found */
1747 assert(res->errindex <= oid_list_todo_num);
1748 i = var_idx[res->errindex - 1];
1749 assert(i < oid_list_len);
1750 oid_list_todo[i] = 0;
1757 for (vb = res->variables, i = 0; (vb != NULL);
1758 vb = vb->next_variable, i++) {
1759 /* Calculate value index from todo list */
1760 while ((i < oid_list_len) && !oid_list_todo[i]) {
1763 if (i >= oid_list_len) {
1767 /* An instance is configured and the res variable we process is the
1769 if (oid_list_todo[i] == OID_TYPE_TYPEINSTANCE) {
1770 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1771 (snmp_oid_ncompare(data->type_instance.oid.oid,
1772 data->type_instance.oid.oid_len, vb->name,
1774 data->type_instance.oid.oid_len) != 0)) {
1775 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1777 host->name, data->name);
1778 oid_list_todo[i] = 0;
1782 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1783 * add it to the list */
1784 csnmp_cell_char_t *cell =
1785 csnmp_get_char_cell(vb, &data->type_instance.oid, host, data);
1787 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1793 if (csnmp_ignore_instance(cell, data)) {
1796 csnmp_cell_replace_reserved_chars(cell);
1798 DEBUG("snmp plugin: il->type_instance = `%s';", cell->value);
1799 csnmp_cells_append(&type_instance_cells_head,
1800 &type_instance_cells_tail, cell);
1802 } else if (oid_list_todo[i] == OID_TYPE_PLUGININSTANCE) {
1803 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1804 (snmp_oid_ncompare(data->plugin_instance.oid.oid,
1805 data->plugin_instance.oid.oid_len, vb->name,
1807 data->plugin_instance.oid.oid_len) != 0)) {
1808 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1810 host->name, data->name);
1811 oid_list_todo[i] = 0;
1815 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1816 * add it to the list */
1817 csnmp_cell_char_t *cell =
1818 csnmp_get_char_cell(vb, &data->plugin_instance.oid, host, data);
1820 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1826 csnmp_cell_replace_reserved_chars(cell);
1828 DEBUG("snmp plugin: il->plugin_instance = `%s';", cell->value);
1829 csnmp_cells_append(&plugin_instance_cells_head,
1830 &plugin_instance_cells_tail, cell);
1831 } else if (oid_list_todo[i] == OID_TYPE_HOST) {
1832 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1833 (snmp_oid_ncompare(data->host.oid.oid, data->host.oid.oid_len,
1834 vb->name, vb->name_length,
1835 data->host.oid.oid_len) != 0)) {
1836 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1837 host->name, data->name);
1838 oid_list_todo[i] = 0;
1842 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1843 * add it to the list */
1844 csnmp_cell_char_t *cell =
1845 csnmp_get_char_cell(vb, &data->host.oid, host, data);
1847 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1853 csnmp_cell_replace_reserved_chars(cell);
1855 DEBUG("snmp plugin: il->hostname = `%s';", cell->value);
1856 csnmp_cells_append(&hostname_cells_head, &hostname_cells_tail, cell);
1857 } else if (oid_list_todo[i] == OID_TYPE_FILTER) {
1858 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1859 (snmp_oid_ncompare(data->filter_oid.oid, data->filter_oid.oid_len,
1860 vb->name, vb->name_length,
1861 data->filter_oid.oid_len) != 0)) {
1862 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1863 host->name, data->name);
1864 oid_list_todo[i] = 0;
1868 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1869 * add it to the list */
1870 csnmp_cell_char_t *cell =
1871 csnmp_get_char_cell(vb, &data->filter_oid, host, data);
1873 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1879 csnmp_cell_replace_reserved_chars(cell);
1881 DEBUG("snmp plugin: il->filter = `%s';", cell->value);
1882 csnmp_cells_append(&filter_cells_head, &filter_cells_tail, cell);
1883 } else /* The variable we are processing is a normal value */
1885 assert(oid_list_todo[i] == OID_TYPE_VARIABLE);
1887 csnmp_cell_value_t *vt;
1892 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1894 /* Calculate the current suffix. This is later used to check that the
1895 * suffix is increasing. This also checks if we left the subtree */
1896 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1898 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1899 "Value probably left its subtree.",
1900 host->name, data->name, i);
1901 oid_list_todo[i] = 0;
1905 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1906 * our table matching algorithm will get confused. */
1907 if ((value_cells_tail[i] != NULL) &&
1908 (csnmp_oid_compare(&suffix, &value_cells_tail[i]->suffix) <= 0)) {
1909 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1910 "Suffix is not increasing.",
1911 host->name, data->name, i);
1912 oid_list_todo[i] = 0;
1916 vt = calloc(1, sizeof(*vt));
1918 ERROR("snmp plugin: calloc failed.");
1924 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1925 data->shift, host->name, data->name);
1926 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1929 if (value_cells_tail[i] == NULL)
1930 value_cells_head[i] = vt;
1932 value_cells_tail[i]->next = vt;
1933 value_cells_tail[i] = vt;
1936 /* Copy OID to oid_list[i] */
1937 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1938 oid_list[i].oid_len = vb->name_length;
1940 } /* for (vb = res->variables ...) */
1945 } /* while (status == 0) */
1952 csnmp_dispatch_table(host, data, type_instance_cells_head,
1953 plugin_instance_cells_head, hostname_cells_head,
1954 filter_cells_head, value_cells_head);
1956 /* Free all allocated variables here */
1957 while (type_instance_cells_head != NULL) {
1958 csnmp_cell_char_t *next = type_instance_cells_head->next;
1959 sfree(type_instance_cells_head);
1960 type_instance_cells_head = next;
1963 while (plugin_instance_cells_head != NULL) {
1964 csnmp_cell_char_t *next = plugin_instance_cells_head->next;
1965 sfree(plugin_instance_cells_head);
1966 plugin_instance_cells_head = next;
1969 while (hostname_cells_head != NULL) {
1970 csnmp_cell_char_t *next = hostname_cells_head->next;
1971 sfree(hostname_cells_head);
1972 hostname_cells_head = next;
1975 while (filter_cells_head != NULL) {
1976 csnmp_cell_char_t *next = filter_cells_head->next;
1977 sfree(filter_cells_head);
1978 filter_cells_head = next;
1981 for (i = 0; i < data->values_len; i++) {
1982 while (value_cells_head[i] != NULL) {
1983 csnmp_cell_value_t *next = value_cells_head[i]->next;
1984 sfree(value_cells_head[i]);
1985 value_cells_head[i] = next;
1989 sfree(value_cells_head);
1990 sfree(value_cells_tail);
1993 } /* int csnmp_read_table */
1995 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1996 struct snmp_pdu *req;
1997 struct snmp_pdu *res = NULL;
1998 struct variable_list *vb;
2000 const data_set_t *ds;
2001 value_list_t vl = VALUE_LIST_INIT;
2006 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
2009 if (host->sess_handle == NULL) {
2010 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
2014 ds = plugin_get_ds(data->type);
2016 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
2020 if (ds->ds_num != data->values_len) {
2021 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
2022 " values, but config talks "
2024 data->type, ds->ds_num, data->values_len);
2028 vl.values_len = ds->ds_num;
2029 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
2030 if (vl.values == NULL)
2032 for (i = 0; i < vl.values_len; i++) {
2033 if (ds->ds[i].type == DS_TYPE_COUNTER)
2034 vl.values[i].counter = 0;
2036 vl.values[i].gauge = NAN;
2039 sstrncpy(vl.host, host->name, sizeof(vl.host));
2040 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
2041 sstrncpy(vl.type, data->type, sizeof(vl.type));
2042 if (data->type_instance.value)
2043 sstrncpy(vl.type_instance, data->type_instance.value,
2044 sizeof(vl.type_instance));
2045 if (data->plugin_instance.value)
2046 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
2047 sizeof(vl.plugin_instance));
2049 req = snmp_pdu_create(SNMP_MSG_GET);
2051 ERROR("snmp plugin: snmp_pdu_create failed.");
2056 for (i = 0; i < data->values_len; i++)
2057 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
2059 status = snmp_sess_synch_response(host->sess_handle, req, &res);
2061 if ((status != STAT_SUCCESS) || (res == NULL)) {
2062 char *errstr = NULL;
2064 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
2065 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
2066 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
2073 csnmp_host_close_session(host);
2078 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
2081 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
2082 DEBUG("snmp plugin: Got this variable: %s", buffer);
2083 #endif /* COLLECT_DEBUG */
2085 for (i = 0; i < data->values_len; i++)
2086 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
2087 vb->name, vb->name_length) == 0)
2089 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
2090 data->shift, host->name, data->name);
2091 } /* for (res->variables) */
2095 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
2096 plugin_dispatch_values(&vl);
2100 } /* int csnmp_read_value */
2102 static int csnmp_read_host(user_data_t *ud) {
2103 host_definition_t *host;
2110 if (host->sess_handle == NULL)
2111 csnmp_host_open_session(host);
2113 if (host->sess_handle == NULL)
2117 for (i = 0; i < host->data_list_len; i++) {
2118 data_definition_t *data = host->data_list[i];
2121 status = csnmp_read_table(host, data);
2123 status = csnmp_read_value(host, data);
2133 } /* int csnmp_read_host */
2135 static int csnmp_init(void) {
2136 call_snmp_init_once();
2139 } /* int csnmp_init */
2141 static int csnmp_shutdown(void) {
2142 data_definition_t *data_this;
2143 data_definition_t *data_next;
2145 /* When we get here, the read threads have been stopped and all the
2146 * `host_definition_t' will be freed. */
2147 DEBUG("snmp plugin: Destroying all data definitions.");
2149 data_this = data_head;
2151 while (data_this != NULL) {
2152 data_next = data_this->next;
2154 csnmp_data_definition_destroy(data_this);
2156 data_this = data_next;
2160 } /* int csnmp_shutdown */
2162 void module_register(void) {
2163 plugin_register_complex_config("snmp", csnmp_config);
2164 plugin_register_init("snmp", csnmp_init);
2165 plugin_register_shutdown("snmp", csnmp_shutdown);
2166 } /* void module_register */