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"
33 #include <net-snmp/net-snmp-config.h>
34 #include <net-snmp/net-snmp-includes.h>
39 * Private data structes
45 typedef struct oid_s oid_t;
52 typedef struct instance_s instance_t;
54 struct data_definition_s {
55 char *name; /* used to reference this from the `Collect' option */
56 char *type; /* used to find the data_set */
60 char *plugin_instance;
62 char *instance_prefix;
67 struct data_definition_s *next;
72 typedef struct data_definition_s data_definition_t;
74 struct host_definition_s {
81 /* snmpv1/2 options */
84 /* snmpv3 security options */
87 size_t auth_protocol_len;
88 char *auth_passphrase;
90 size_t priv_protocol_len;
91 char *priv_passphrase;
96 c_complain_t complaint;
98 data_definition_t **data_list;
101 typedef struct host_definition_s host_definition_t;
103 /* These two types are used to cache values in `csnmp_read_table' to handle
105 struct csnmp_list_instances_s {
107 char instance[DATA_MAX_NAME_LEN];
108 struct csnmp_list_instances_s *next;
110 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
112 struct csnmp_table_values_s {
115 struct csnmp_table_values_s *next;
117 typedef struct csnmp_table_values_s csnmp_table_values_t;
122 static data_definition_t *data_head;
127 static int csnmp_read_host(user_data_t *ud);
132 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
133 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
134 memcpy(dst->oid, src, sizeof(*src) * n);
138 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
139 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
142 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
143 /* Make sure "src" is in "root"s subtree. */
144 if (src->oid_len <= root->oid_len)
146 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
147 /* n = */ root->oid_len) != 0)
150 memset(dst, 0, sizeof(*dst));
151 dst->oid_len = src->oid_len - root->oid_len;
152 memcpy(dst->oid, &src->oid[root->oid_len],
153 dst->oid_len * sizeof(dst->oid[0]));
157 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
159 char oid_str[MAX_OID_LEN][16];
160 char *oid_str_ptr[MAX_OID_LEN];
162 for (size_t i = 0; i < o->oid_len; i++) {
163 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
164 oid_str_ptr[i] = oid_str[i];
167 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
170 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
172 if (host->sess_handle == NULL)
175 snmp_sess_close(host->sess_handle);
176 host->sess_handle = NULL;
177 } /* }}} void csnmp_host_close_session */
179 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
181 host_definition_t *hd;
188 if (hd->name != NULL) {
189 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
192 csnmp_host_close_session(hd);
196 sfree(hd->community);
198 sfree(hd->auth_passphrase);
199 sfree(hd->priv_passphrase);
201 sfree(hd->data_list);
204 } /* }}} void csnmp_host_definition_destroy */
206 /* Many functions to handle the configuration. {{{ */
207 /* First there are many functions which do configuration stuff. It's a big
208 * bloated and messy, I'm afraid. */
211 * Callgraph for the config stuff:
213 * +-> call_snmp_init_once
214 * +-> csnmp_config_add_data
215 * ! +-> csnmp_config_add_data_instance
216 * ! +-> csnmp_config_add_data_values
217 * +-> csnmp_config_add_host
218 * +-> csnmp_config_add_host_version
219 * +-> csnmp_config_add_host_collect
220 * +-> csnmp_config_add_host_auth_protocol
221 * +-> csnmp_config_add_host_priv_protocol
222 * +-> csnmp_config_add_host_security_level
224 static void call_snmp_init_once(void) {
225 static int have_init;
228 init_snmp(PACKAGE_NAME);
230 } /* void call_snmp_init_once */
232 static int csnmp_config_add_data_instance_oid(data_definition_t *dd,
235 char buffer[DATA_MAX_NAME_LEN];
238 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
242 if (dd->instance.configured) {
243 ERROR("snmp plugin: Only one of options `TypeInstanceOID', "
244 "`PluginInstanceOID' or `Instance' can be used in `Data' block.");
248 dd->instance.is_plugin = is_plugin;
249 dd->instance.configured = true;
251 if (strlen(buffer) == 0) {
255 dd->instance.oid.oid_len = MAX_OID_LEN;
257 if (!read_objid(buffer, dd->instance.oid.oid, &dd->instance.oid.oid_len)) {
258 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
263 } /* int csnmp_config_add_data_instance_oid */
265 static int csnmp_config_add_data_values(data_definition_t *dd,
266 oconfig_item_t *ci) {
267 if (ci->values_num < 1) {
268 WARNING("snmp plugin: `Values' needs at least one argument.");
272 for (int i = 0; i < ci->values_num; i++)
273 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
274 WARNING("snmp plugin: `Values' needs only string argument.");
280 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
281 if (dd->values == NULL)
283 dd->values_len = (size_t)ci->values_num;
285 for (int i = 0; i < ci->values_num; i++) {
286 dd->values[i].oid_len = MAX_OID_LEN;
288 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
289 &dd->values[i].oid_len)) {
290 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
291 ci->values[i].value.string);
300 } /* int csnmp_config_add_data_instance */
302 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
303 oconfig_item_t *ci) {
304 if (ci->values_num < 1)
307 for (int i = 0; i < ci->values_num; i++) {
308 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
309 WARNING("snmp plugin: `Ignore' needs only string argument.");
314 for (int i = 0; i < ci->values_num; ++i) {
315 if (strarray_add(&(dd->ignores), &(dd->ignores_len),
316 ci->values[i].value.string) != 0) {
317 ERROR("snmp plugin: Can't allocate memory");
318 strarray_free(dd->ignores, dd->ignores_len);
323 } /* int csnmp_config_add_data_blacklist */
325 static int csnmp_config_add_data(oconfig_item_t *ci) {
326 data_definition_t *dd = calloc(1, sizeof(*dd));
330 int status = cf_util_get_string(ci, &dd->name);
341 dd->plugin_name = strdup("snmp");
342 if (dd->plugin_name == NULL) {
343 ERROR("snmp plugin: Can't allocate memory");
347 for (int i = 0; i < ci->children_num; i++) {
348 oconfig_item_t *option = ci->children + i;
350 if (strcasecmp("Type", option->key) == 0)
351 status = cf_util_get_string(option, &dd->type);
352 else if (strcasecmp("Table", option->key) == 0)
353 status = cf_util_get_boolean(option, &dd->is_table);
354 else if (strcasecmp("Plugin", option->key) == 0)
355 status = cf_util_get_string(option, &dd->plugin_name);
356 else if (strcasecmp("Instance", option->key) == 0) {
358 /* Instance is OID */
359 WARNING("snmp plugin: Option `Instance' is deprecated, please update "
360 "Data \"%s\" block to use option `TypeInstanceOID'.",
362 status = csnmp_config_add_data_instance_oid(dd, option,
363 false /* type instance */);
365 /* Instance is a simple string */
366 WARNING("snmp plugin: Option `Instance' is deprecated, please update "
367 "Data \"%s\" block to use option `TypeInstance'.",
369 status = cf_util_get_string(option, &dd->type_instance);
371 } else if (strcasecmp("PluginInstance", option->key) == 0)
372 status = cf_util_get_string(option, &dd->plugin_instance);
373 else if (strcasecmp("TypeInstance", option->key) == 0)
374 status = cf_util_get_string(option, &dd->type_instance);
375 else if (strcasecmp("PluginInstanceOID", option->key) == 0)
376 status = csnmp_config_add_data_instance_oid(dd, option,
377 true /* plugin instance */);
378 else if (strcasecmp("TypeInstanceOID", option->key) == 0)
379 status = csnmp_config_add_data_instance_oid(dd, option,
380 false /* type instance */);
381 else if (strcasecmp("InstancePrefix", option->key) == 0)
382 status = cf_util_get_string(option, &dd->instance_prefix);
383 else if (strcasecmp("Values", option->key) == 0)
384 status = csnmp_config_add_data_values(dd, option);
385 else if (strcasecmp("Shift", option->key) == 0)
386 status = cf_util_get_double(option, &dd->shift);
387 else if (strcasecmp("Scale", option->key) == 0)
388 status = cf_util_get_double(option, &dd->scale);
389 else if (strcasecmp("Ignore", option->key) == 0)
390 status = csnmp_config_add_data_blacklist(dd, option);
391 else if (strcasecmp("InvertMatch", option->key) == 0)
392 status = cf_util_get_boolean(option, &dd->invert_match);
394 WARNING("snmp plugin: Option `%s' not allowed here.", option->key);
400 } /* for (ci->children) */
402 while (status == 0) {
404 if (dd->plugin_instance && dd->instance.is_plugin) {
405 WARNING("snmp plugin: Option `PluginInstance' will be ignored for "
409 if (dd->type_instance && !dd->instance.is_plugin) {
410 WARNING("snmp plugin: Option `TypeInstance' will be ignored for Data "
415 if (dd->instance.configured) {
416 if (dd->instance.is_plugin) {
417 WARNING("snmp plugin: Option `PluginInstanceOID' will be ignored for "
421 WARNING("snmp plugin: Option `TypeInstanceOID' will be ignored for "
427 if (dd->instance_prefix) {
428 WARNING("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
429 "is set to `false'.",
434 if (dd->type == NULL) {
435 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
439 if (dd->values == NULL) {
440 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
446 } /* while (status == 0) */
451 sfree(dd->plugin_name);
452 sfree(dd->plugin_instance);
453 sfree(dd->type_instance);
454 sfree(dd->instance_prefix);
461 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
463 dd->name, dd->type, (dd->is_table) ? "true" : "false", dd->values_len);
465 DEBUG("snmp plugin: plugin_instance = %s, type_instance = %s,",
466 dd->plugin_instance, dd->type_instance);
468 DEBUG("snmp plugin: instance_by_oid = %s, to_plugin_instance = %s }",
469 (dd->instance.oid.oid_len > 0) ? "true" : "SUBID",
470 (dd->instance.is_plugin) ? "true" : "false");
472 if (data_head == NULL)
475 data_definition_t *last;
477 while (last->next != NULL)
483 } /* int csnmp_config_add_data */
485 static int csnmp_config_add_host_version(host_definition_t *hd,
486 oconfig_item_t *ci) {
489 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
490 WARNING("snmp plugin: The `Version' config option needs exactly one number "
495 version = (int)ci->values[0].value.number;
496 if ((version < 1) || (version > 3)) {
497 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
501 hd->version = version;
504 } /* int csnmp_config_add_host_address */
506 static int csnmp_config_add_host_collect(host_definition_t *host,
507 oconfig_item_t *ci) {
508 data_definition_t *data;
509 data_definition_t **data_list;
512 if (ci->values_num < 1) {
513 WARNING("snmp plugin: `Collect' needs at least one argument.");
517 for (int i = 0; i < ci->values_num; i++)
518 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
519 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
523 data_list_len = host->data_list_len + ci->values_num;
525 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
526 if (data_list == NULL)
528 host->data_list = data_list;
530 for (int i = 0; i < ci->values_num; i++) {
531 for (data = data_head; data != NULL; data = data->next)
532 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
536 WARNING("snmp plugin: No such data configured: `%s'",
537 ci->values[i].value.string);
541 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
542 host->data_list_len, data->name);
544 host->data_list[host->data_list_len] = data;
545 host->data_list_len++;
546 } /* for (values_num) */
549 } /* int csnmp_config_add_host_collect */
551 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
552 oconfig_item_t *ci) {
556 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
560 if (strcasecmp("MD5", buffer) == 0) {
561 hd->auth_protocol = usmHMACMD5AuthProtocol;
562 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
563 } else if (strcasecmp("SHA", buffer) == 0) {
564 hd->auth_protocol = usmHMACSHA1AuthProtocol;
565 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
567 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
572 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
573 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
576 } /* int csnmp_config_add_host_auth_protocol */
578 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
579 oconfig_item_t *ci) {
583 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
587 if (strcasecmp("AES", buffer) == 0) {
588 hd->priv_protocol = usmAESPrivProtocol;
589 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
590 } else if (strcasecmp("DES", buffer) == 0) {
591 hd->priv_protocol = usmDESPrivProtocol;
592 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
594 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
599 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
600 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
603 } /* int csnmp_config_add_host_priv_protocol */
605 static int csnmp_config_add_host_security_level(host_definition_t *hd,
606 oconfig_item_t *ci) {
610 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
614 if (strcasecmp("noAuthNoPriv", buffer) == 0)
615 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
616 else if (strcasecmp("authNoPriv", buffer) == 0)
617 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
618 else if (strcasecmp("authPriv", buffer) == 0)
619 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
621 WARNING("snmp plugin: The `SecurityLevel' config option must be "
622 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
626 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
630 } /* int csnmp_config_add_host_security_level */
632 static int csnmp_config_add_host(oconfig_item_t *ci) {
633 host_definition_t *hd;
636 /* Registration stuff. */
637 char cb_name[DATA_MAX_NAME_LEN];
639 hd = calloc(1, sizeof(*hd));
643 C_COMPLAIN_INIT(&hd->complaint);
645 status = cf_util_get_string(ci, &hd->name);
651 hd->sess_handle = NULL;
654 /* These mean that we have not set a timeout or retry value */
658 for (int i = 0; i < ci->children_num; i++) {
659 oconfig_item_t *option = ci->children + i;
661 if (strcasecmp("Address", option->key) == 0)
662 status = cf_util_get_string(option, &hd->address);
663 else if (strcasecmp("Community", option->key) == 0)
664 status = cf_util_get_string(option, &hd->community);
665 else if (strcasecmp("Version", option->key) == 0)
666 status = csnmp_config_add_host_version(hd, option);
667 else if (strcasecmp("Timeout", option->key) == 0)
668 status = cf_util_get_cdtime(option, &hd->timeout);
669 else if (strcasecmp("Retries", option->key) == 0)
670 status = cf_util_get_int(option, &hd->retries);
671 else if (strcasecmp("Collect", option->key) == 0)
672 status = csnmp_config_add_host_collect(hd, option);
673 else if (strcasecmp("Interval", option->key) == 0)
674 status = cf_util_get_cdtime(option, &hd->interval);
675 else if (strcasecmp("Username", option->key) == 0)
676 status = cf_util_get_string(option, &hd->username);
677 else if (strcasecmp("AuthProtocol", option->key) == 0)
678 status = csnmp_config_add_host_auth_protocol(hd, option);
679 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
680 status = csnmp_config_add_host_priv_protocol(hd, option);
681 else if (strcasecmp("AuthPassphrase", option->key) == 0)
682 status = cf_util_get_string(option, &hd->auth_passphrase);
683 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
684 status = cf_util_get_string(option, &hd->priv_passphrase);
685 else if (strcasecmp("SecurityLevel", option->key) == 0)
686 status = csnmp_config_add_host_security_level(hd, option);
687 else if (strcasecmp("Context", option->key) == 0)
688 status = cf_util_get_string(option, &hd->context);
691 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
698 } /* for (ci->children) */
700 while (status == 0) {
701 if (hd->address == NULL) {
702 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
706 if (hd->community == NULL && hd->version < 3) {
707 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
711 if (hd->version == 3) {
712 if (hd->username == NULL) {
713 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
717 if (hd->security_level == 0) {
718 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
723 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
724 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
725 if (hd->auth_protocol == NULL) {
726 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
731 if (hd->auth_passphrase == NULL) {
732 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
738 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
739 if (hd->priv_protocol == NULL) {
740 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
745 if (hd->priv_passphrase == NULL) {
746 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
755 } /* while (status == 0) */
758 csnmp_host_definition_destroy(hd);
762 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
764 hd->name, hd->address, hd->community, hd->version);
766 snprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
768 status = plugin_register_complex_read(
769 /* group = */ NULL, cb_name, csnmp_read_host, hd->interval,
771 .data = hd, .free_func = csnmp_host_definition_destroy,
774 ERROR("snmp plugin: Registering complex read function failed.");
779 } /* int csnmp_config_add_host */
781 static int csnmp_config(oconfig_item_t *ci) {
782 call_snmp_init_once();
784 for (int i = 0; i < ci->children_num; i++) {
785 oconfig_item_t *child = ci->children + i;
786 if (strcasecmp("Data", child->key) == 0)
787 csnmp_config_add_data(child);
788 else if (strcasecmp("Host", child->key) == 0)
789 csnmp_config_add_host(child);
791 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
793 } /* for (ci->children) */
796 } /* int csnmp_config */
798 /* }}} End of the config stuff. Now the interesting part begins */
800 static void csnmp_host_open_session(host_definition_t *host) {
801 struct snmp_session sess;
804 if (host->sess_handle != NULL)
805 csnmp_host_close_session(host);
807 snmp_sess_init(&sess);
808 sess.peername = host->address;
809 switch (host->version) {
811 sess.version = SNMP_VERSION_1;
814 sess.version = SNMP_VERSION_3;
817 sess.version = SNMP_VERSION_2c;
821 if (host->version == 3) {
822 sess.securityName = host->username;
823 sess.securityNameLen = strlen(host->username);
824 sess.securityLevel = host->security_level;
826 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
827 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
828 sess.securityAuthProto = host->auth_protocol;
829 sess.securityAuthProtoLen = host->auth_protocol_len;
830 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
831 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
832 (u_char *)host->auth_passphrase,
833 strlen(host->auth_passphrase), sess.securityAuthKey,
834 &sess.securityAuthKeyLen);
835 if (error != SNMPERR_SUCCESS) {
836 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
842 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
843 sess.securityPrivProto = host->priv_protocol;
844 sess.securityPrivProtoLen = host->priv_protocol_len;
845 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
846 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
847 (u_char *)host->priv_passphrase,
848 strlen(host->priv_passphrase), sess.securityPrivKey,
849 &sess.securityPrivKeyLen);
850 if (error != SNMPERR_SUCCESS) {
851 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
857 if (host->context != NULL) {
858 sess.contextName = host->context;
859 sess.contextNameLen = strlen(host->context);
861 } else /* SNMPv1/2 "authenticates" with community string */
863 sess.community = (u_char *)host->community;
864 sess.community_len = strlen(host->community);
867 /* Set timeout & retries, if they have been changed from the default */
868 if (host->timeout != 0) {
869 /* net-snmp expects microseconds */
870 sess.timeout = CDTIME_T_TO_US(host->timeout);
872 if (host->retries >= 0) {
873 sess.retries = host->retries;
876 /* snmp_sess_open will copy the `struct snmp_session *'. */
877 host->sess_handle = snmp_sess_open(&sess);
879 if (host->sess_handle == NULL) {
882 snmp_error(&sess, NULL, NULL, &errstr);
884 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
885 (errstr == NULL) ? "Unknown problem" : errstr);
888 } /* void csnmp_host_open_session */
890 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
892 static value_t csnmp_value_list_to_value(struct variable_list *vl, int type,
893 double scale, double shift,
894 const char *host_name,
895 const char *data_name) {
897 uint64_t tmp_unsigned = 0;
898 int64_t tmp_signed = 0;
900 /* Set to true when the original SNMP type appears to have been signed. */
901 bool prefer_signed = 0;
903 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
904 (vl->type == ASN_COUNTER)
906 || (vl->type == ASN_TIMETICKS)
908 || (vl->type == ASN_GAUGE)) {
909 tmp_unsigned = (uint32_t)*vl->val.integer;
910 tmp_signed = (int32_t)*vl->val.integer;
912 if (vl->type == ASN_INTEGER)
915 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
916 } else if (vl->type == ASN_COUNTER64) {
917 tmp_unsigned = (uint32_t)vl->val.counter64->high;
918 tmp_unsigned = tmp_unsigned << 32;
919 tmp_unsigned += (uint32_t)vl->val.counter64->low;
920 tmp_signed = (int64_t)tmp_unsigned;
921 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
922 } else if (vl->type == ASN_OCTET_STR) {
923 /* We'll handle this later.. */
925 char oid_buffer[1024] = {0};
927 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
931 if (vl->type == ASN_NULL)
932 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
935 WARNING("snmp plugin: I don't know the ASN type #%i "
936 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
937 (int)vl->type, oid_buffer,
938 (data_name != NULL) ? data_name : "UNKNOWN",
939 (host_name != NULL) ? host_name : "UNKNOWN");
944 if (vl->type == ASN_OCTET_STR) {
947 if (vl->val.string != NULL) {
949 size_t string_length;
951 string_length = sizeof(string) - 1;
952 if (vl->val_len < string_length)
953 string_length = vl->val_len;
955 /* The strings we get from the Net-SNMP library may not be null
956 * terminated. That is why we're using `memcpy' here and not `strcpy'.
957 * `string_length' is set to `vl->val_len' which holds the length of the
959 memcpy(string, vl->val.string, string_length);
960 string[string_length] = 0;
962 status = parse_value(string, &ret, type);
964 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
966 (host_name != NULL) ? host_name : "UNKNOWN",
967 DS_TYPE_TO_STRING(type), string);
973 case DS_TYPE_COUNTER:
975 case DS_TYPE_ABSOLUTE:
976 memset(&ret, 0, sizeof(ret));
984 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
985 "data source type: %i.",
990 } /* if (vl->type == ASN_OCTET_STR) */
991 else if (type == DS_TYPE_COUNTER) {
992 ret.counter = tmp_unsigned;
993 } else if (type == DS_TYPE_GAUGE) {
996 else if (prefer_signed)
997 ret.gauge = (scale * tmp_signed) + shift;
999 ret.gauge = (scale * tmp_unsigned) + shift;
1000 } else if (type == DS_TYPE_DERIVE) {
1002 ret.derive = (derive_t)tmp_signed;
1004 ret.derive = (derive_t)tmp_unsigned;
1005 } else if (type == DS_TYPE_ABSOLUTE) {
1006 ret.absolute = (absolute_t)tmp_unsigned;
1008 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1015 } /* value_t csnmp_value_list_to_value */
1017 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1018 * representation and writes it to dst. Returns zero on success and ENOMEM if
1019 * dst is not large enough to hold the string. dst is guaranteed to be
1020 * nul-terminated. */
1021 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
1022 const struct variable_list *vb,
1030 buffer_free = dst_size;
1032 for (size_t i = 0; i < vb->val_len; i++) {
1035 status = snprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
1036 (unsigned int)vb->val.bitstring[i]);
1037 assert(status >= 0);
1039 if (((size_t)status) >= buffer_free) /* truncated */
1041 dst[dst_size - 1] = 0;
1043 } else /* if (status < buffer_free) */
1045 buffer_ptr += (size_t)status;
1046 buffer_free -= (size_t)status;
1051 } /* }}} int csnmp_strvbcopy_hexstring */
1053 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1054 * dst. If non-printable characters are detected, it will switch to a hex
1055 * representation of the string. Returns zero on success, EINVAL if vb does not
1056 * contain a string and ENOMEM if dst is not large enough to contain the
1058 static int csnmp_strvbcopy(char *dst, /* {{{ */
1059 const struct variable_list *vb, size_t dst_size) {
1063 if (vb->type == ASN_OCTET_STR)
1064 src = (char *)vb->val.string;
1065 else if (vb->type == ASN_BIT_STR)
1066 src = (char *)vb->val.bitstring;
1067 else if (vb->type == ASN_IPADDRESS) {
1068 return snprintf(dst, dst_size,
1069 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1070 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1071 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1077 num_chars = dst_size - 1;
1078 if (num_chars > vb->val_len)
1079 num_chars = vb->val_len;
1081 for (size_t i = 0; i < num_chars; i++) {
1082 /* Check for control characters. */
1083 if ((unsigned char)src[i] < 32)
1084 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1088 dst[dst_size - 1] = 0;
1090 if (dst_size <= vb->val_len)
1094 } /* }}} int csnmp_strvbcopy */
1096 static int csnmp_instance_list_add(csnmp_list_instances_t **head,
1097 csnmp_list_instances_t **tail,
1098 const struct snmp_pdu *res,
1099 const host_definition_t *hd,
1100 const data_definition_t *dd) {
1101 csnmp_list_instances_t *il;
1102 struct variable_list *vb;
1106 /* Set vb on the last variable */
1107 for (vb = res->variables; (vb != NULL) && (vb->next_variable != NULL);
1108 vb = vb->next_variable)
1113 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1115 il = calloc(1, sizeof(*il));
1117 ERROR("snmp plugin: calloc failed.");
1122 status = csnmp_oid_suffix(&il->suffix, &vb_name, &dd->instance.oid);
1128 /* Get instance name */
1129 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1130 (vb->type == ASN_IPADDRESS)) {
1133 csnmp_strvbcopy(il->instance, vb, sizeof(il->instance));
1134 bool is_matched = 0;
1135 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1136 status = fnmatch(dd->ignores[i], il->instance, 0);
1138 if (!dd->invert_match) {
1147 if (dd->invert_match && !is_matched) {
1151 for (ptr = il->instance; *ptr != '\0'; ptr++) {
1152 if ((*ptr > 0) && (*ptr < 32))
1154 else if (*ptr == '/')
1157 DEBUG("snmp plugin: il->instance = `%s';", il->instance);
1159 value_t val = csnmp_value_list_to_value(
1160 vb, DS_TYPE_COUNTER,
1161 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1162 snprintf(il->instance, sizeof(il->instance), "%" PRIu64,
1163 (uint64_t)val.counter);
1166 /* TODO: Debugging output */
1175 } /* int csnmp_instance_list_add */
1177 static int csnmp_dispatch_table(host_definition_t *host,
1178 data_definition_t *data,
1179 csnmp_list_instances_t *instance_list,
1180 csnmp_table_values_t **value_table) {
1181 const data_set_t *ds;
1182 value_list_t vl = VALUE_LIST_INIT;
1184 csnmp_list_instances_t *instance_list_ptr;
1185 csnmp_table_values_t *value_table_ptr[data->values_len];
1189 oid_t current_suffix;
1191 ds = plugin_get_ds(data->type);
1193 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1196 assert(ds->ds_num == data->values_len);
1197 assert(data->values_len > 0);
1199 instance_list_ptr = instance_list;
1201 for (i = 0; i < data->values_len; i++)
1202 value_table_ptr[i] = value_table[i];
1204 sstrncpy(vl.host, host->name, sizeof(vl.host));
1205 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1207 vl.interval = host->interval;
1211 bool suffix_skipped = 0;
1213 /* Determine next suffix to handle. */
1214 if (instance_list != NULL) {
1215 if (instance_list_ptr == NULL) {
1220 memcpy(¤t_suffix, &instance_list_ptr->suffix,
1221 sizeof(current_suffix));
1223 /* no instance configured */
1224 csnmp_table_values_t *ptr = value_table_ptr[0];
1230 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1233 /* Update all the value_table_ptr to point at the entry with the same
1234 * trailing partial OID */
1235 for (i = 0; i < data->values_len; i++) {
1237 (value_table_ptr[i] != NULL) &&
1238 (csnmp_oid_compare(&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1239 value_table_ptr[i] = value_table_ptr[i]->next;
1241 if (value_table_ptr[i] == NULL) {
1244 } else if (csnmp_oid_compare(&value_table_ptr[i]->suffix,
1245 ¤t_suffix) > 0) {
1246 /* This suffix is missing in the subtree. Indicate this with the
1247 * "suffix_skipped" flag and try the next instance / suffix. */
1251 } /* for (i = 0; i < columns; i++) */
1256 /* Matching the values failed. Start from the beginning again. */
1257 if (suffix_skipped) {
1258 if (instance_list != NULL)
1259 instance_list_ptr = instance_list_ptr->next;
1261 value_table_ptr[0] = value_table_ptr[0]->next;
1266 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1267 * to the same subid. instance_list_ptr is either NULL or points to the
1268 * same subid, too. */
1270 for (i = 1; i < data->values_len; i++) {
1271 assert(value_table_ptr[i] != NULL);
1272 assert(csnmp_oid_compare(&value_table_ptr[i - 1]->suffix,
1273 &value_table_ptr[i]->suffix) == 0);
1275 assert((instance_list_ptr == NULL) ||
1276 (csnmp_oid_compare(&instance_list_ptr->suffix,
1277 &value_table_ptr[0]->suffix) == 0));
1280 sstrncpy(vl.type, data->type, sizeof(vl.type));
1283 char temp[DATA_MAX_NAME_LEN];
1285 if (instance_list_ptr == NULL)
1286 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1288 sstrncpy(temp, instance_list_ptr->instance, sizeof(temp));
1290 if (data->instance.is_plugin) {
1291 if (data->instance_prefix == NULL)
1292 sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
1294 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
1295 data->instance_prefix, temp);
1297 if (data->type_instance)
1298 sstrncpy(vl.type_instance, data->type_instance,
1299 sizeof(vl.type_instance));
1301 if (data->instance_prefix == NULL)
1302 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1304 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1305 data->instance_prefix, temp);
1307 if (data->plugin_instance)
1308 sstrncpy(vl.plugin_instance, data->plugin_instance,
1309 sizeof(vl.plugin_instance));
1313 vl.values_len = data->values_len;
1314 value_t values[vl.values_len];
1317 for (i = 0; i < data->values_len; i++)
1318 vl.values[i] = value_table_ptr[i]->value;
1320 plugin_dispatch_values(&vl);
1322 /* prevent leakage of pointer to local variable. */
1326 if (instance_list != NULL)
1327 instance_list_ptr = instance_list_ptr->next;
1329 value_table_ptr[0] = value_table_ptr[0]->next;
1330 } /* while (have_more) */
1333 } /* int csnmp_dispatch_table */
1335 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1336 struct snmp_pdu *req;
1337 struct snmp_pdu *res = NULL;
1338 struct variable_list *vb;
1340 const data_set_t *ds;
1342 size_t oid_list_len = data->values_len + 1;
1343 /* Holds the last OID returned by the device. We use this in the GETNEXT
1344 * request to proceed. */
1345 oid_t oid_list[oid_list_len];
1346 /* Set to false when an OID has left its subtree so we don't re-request it
1348 bool oid_list_todo[oid_list_len];
1353 /* `value_list_head' and `value_list_tail' implement a linked list for each
1354 * value. `instance_list_head' and `instance_list_tail' implement a linked
1356 * instance names. This is used to jump gaps in the table. */
1357 csnmp_list_instances_t *instance_list_head;
1358 csnmp_list_instances_t *instance_list_tail;
1359 csnmp_table_values_t **value_list_head;
1360 csnmp_table_values_t **value_list_tail;
1362 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1365 if (host->sess_handle == NULL) {
1366 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1370 ds = plugin_get_ds(data->type);
1372 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1376 if (ds->ds_num != data->values_len) {
1377 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1378 " values, but config talks "
1380 data->type, ds->ds_num, data->values_len);
1383 assert(data->values_len > 0);
1385 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1386 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1387 if (data->instance.oid.oid_len > 0)
1388 memcpy(oid_list + data->values_len, &data->instance.oid, sizeof(oid_t));
1389 else /* no InstanceFrom option specified. */
1392 for (i = 0; i < oid_list_len; i++)
1393 oid_list_todo[i] = 1;
1395 /* We're going to construct n linked lists, one for each "value".
1396 * value_list_head will contain pointers to the heads of these linked lists,
1397 * value_list_tail will contain pointers to the tail of the lists. */
1398 value_list_head = calloc(data->values_len, sizeof(*value_list_head));
1399 value_list_tail = calloc(data->values_len, sizeof(*value_list_tail));
1400 if ((value_list_head == NULL) || (value_list_tail == NULL)) {
1401 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1402 sfree(value_list_head);
1403 sfree(value_list_tail);
1407 instance_list_head = NULL;
1408 instance_list_tail = NULL;
1411 while (status == 0) {
1412 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1414 ERROR("snmp plugin: snmp_pdu_create failed.");
1419 size_t oid_list_todo_num = 0;
1420 size_t var_idx[oid_list_len];
1421 memset(var_idx, 0, sizeof(var_idx));
1423 for (i = 0; i < oid_list_len; i++) {
1424 /* Do not rerequest already finished OIDs */
1425 if (!oid_list_todo[i])
1427 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1428 var_idx[oid_list_todo_num] = i;
1429 oid_list_todo_num++;
1432 if (oid_list_todo_num == 0) {
1433 /* The request is still empty - so we are finished */
1434 DEBUG("snmp plugin: all variables have left their subtree");
1441 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1443 /* snmp_sess_synch_response always frees our req PDU */
1446 if ((status != STAT_SUCCESS) || (res == NULL)) {
1447 char *errstr = NULL;
1449 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1451 c_complain(LOG_ERR, &host->complaint,
1452 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1453 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1460 csnmp_host_close_session(host);
1467 assert(res != NULL);
1468 c_release(LOG_INFO, &host->complaint,
1469 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1472 vb = res->variables;
1478 if (res->errstat != SNMP_ERR_NOERROR) {
1479 if (res->errindex != 0) {
1480 /* Find the OID which caused error */
1481 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1482 vb = vb->next_variable, i++)
1486 if ((res->errindex == 0) || (vb == NULL)) {
1487 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1488 host->name, data->name, snmp_errstring(res->errstat),
1494 char oid_buffer[1024] = {0};
1495 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1497 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1498 data->name, oid_buffer, snmp_errstring(res->errstat));
1500 /* Get value index from todo list and skip OID found */
1501 assert(res->errindex <= oid_list_todo_num);
1502 i = var_idx[res->errindex - 1];
1503 assert(i < oid_list_len);
1504 oid_list_todo[i] = 0;
1511 for (vb = res->variables, i = 0; (vb != NULL);
1512 vb = vb->next_variable, i++) {
1513 /* Calculate value index from todo list */
1514 while ((i < oid_list_len) && !oid_list_todo[i]) {
1517 if (i >= oid_list_len) {
1521 /* An instance is configured and the res variable we process is the
1522 * instance value (last index) */
1523 if ((data->instance.oid.oid_len > 0) && (i == data->values_len)) {
1524 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1526 data->instance.oid.oid, data->instance.oid.oid_len, vb->name,
1527 vb->name_length, data->instance.oid.oid_len) != 0)) {
1528 DEBUG("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1529 host->name, data->name);
1530 oid_list_todo[i] = 0;
1534 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1535 * add it to the list */
1536 if (csnmp_instance_list_add(&instance_list_head, &instance_list_tail,
1537 res, host, data) != 0) {
1538 ERROR("snmp plugin: host %s: csnmp_instance_list_add failed.",
1543 } else /* The variable we are processing is a normal value */
1545 csnmp_table_values_t *vt;
1550 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1552 /* Calculate the current suffix. This is later used to check that the
1553 * suffix is increasing. This also checks if we left the subtree */
1554 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1556 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1557 "Value probably left its subtree.",
1558 host->name, data->name, i);
1559 oid_list_todo[i] = 0;
1563 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1565 * table matching algorithm will get confused. */
1566 if ((value_list_tail[i] != NULL) &&
1567 (csnmp_oid_compare(&suffix, &value_list_tail[i]->suffix) <= 0)) {
1568 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1569 "Suffix is not increasing.",
1570 host->name, data->name, i);
1571 oid_list_todo[i] = 0;
1575 vt = calloc(1, sizeof(*vt));
1577 ERROR("snmp plugin: calloc failed.");
1583 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1584 data->shift, host->name, data->name);
1585 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1588 if (value_list_tail[i] == NULL)
1589 value_list_head[i] = vt;
1591 value_list_tail[i]->next = vt;
1592 value_list_tail[i] = vt;
1595 /* Copy OID to oid_list[i] */
1596 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1597 oid_list[i].oid_len = vb->name_length;
1599 } /* for (vb = res->variables ...) */
1604 } /* while (status == 0) */
1611 csnmp_dispatch_table(host, data, instance_list_head, value_list_head);
1613 /* Free all allocated variables here */
1614 while (instance_list_head != NULL) {
1615 csnmp_list_instances_t *next = instance_list_head->next;
1616 sfree(instance_list_head);
1617 instance_list_head = next;
1620 for (i = 0; i < data->values_len; i++) {
1621 while (value_list_head[i] != NULL) {
1622 csnmp_table_values_t *next = value_list_head[i]->next;
1623 sfree(value_list_head[i]);
1624 value_list_head[i] = next;
1628 sfree(value_list_head);
1629 sfree(value_list_tail);
1632 } /* int csnmp_read_table */
1634 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1635 struct snmp_pdu *req;
1636 struct snmp_pdu *res = NULL;
1637 struct variable_list *vb;
1639 const data_set_t *ds;
1640 value_list_t vl = VALUE_LIST_INIT;
1645 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
1648 if (host->sess_handle == NULL) {
1649 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1653 ds = plugin_get_ds(data->type);
1655 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1659 if (ds->ds_num != data->values_len) {
1660 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1661 " values, but config talks "
1663 data->type, ds->ds_num, data->values_len);
1667 vl.values_len = ds->ds_num;
1668 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
1669 if (vl.values == NULL)
1671 for (i = 0; i < vl.values_len; i++) {
1672 if (ds->ds[i].type == DS_TYPE_COUNTER)
1673 vl.values[i].counter = 0;
1675 vl.values[i].gauge = NAN;
1678 sstrncpy(vl.host, host->name, sizeof(vl.host));
1679 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1680 sstrncpy(vl.type, data->type, sizeof(vl.type));
1681 if (data->type_instance)
1682 sstrncpy(vl.type_instance, data->type_instance, sizeof(vl.type_instance));
1683 if (data->plugin_instance)
1684 sstrncpy(vl.plugin_instance, data->plugin_instance,
1685 sizeof(vl.plugin_instance));
1687 vl.interval = host->interval;
1689 req = snmp_pdu_create(SNMP_MSG_GET);
1691 ERROR("snmp plugin: snmp_pdu_create failed.");
1696 for (i = 0; i < data->values_len; i++)
1697 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
1699 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1701 if ((status != STAT_SUCCESS) || (res == NULL)) {
1702 char *errstr = NULL;
1704 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1705 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1706 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1713 csnmp_host_close_session(host);
1718 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
1721 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
1722 DEBUG("snmp plugin: Got this variable: %s", buffer);
1723 #endif /* COLLECT_DEBUG */
1725 for (i = 0; i < data->values_len; i++)
1726 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
1727 vb->name, vb->name_length) == 0)
1729 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1730 data->shift, host->name, data->name);
1731 } /* for (res->variables) */
1735 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
1736 plugin_dispatch_values(&vl);
1740 } /* int csnmp_read_value */
1742 static int csnmp_read_host(user_data_t *ud) {
1743 host_definition_t *host;
1750 if (host->interval == 0)
1751 host->interval = plugin_get_interval();
1753 if (host->sess_handle == NULL)
1754 csnmp_host_open_session(host);
1756 if (host->sess_handle == NULL)
1760 for (i = 0; i < host->data_list_len; i++) {
1761 data_definition_t *data = host->data_list[i];
1764 status = csnmp_read_table(host, data);
1766 status = csnmp_read_value(host, data);
1776 } /* int csnmp_read_host */
1778 static int csnmp_init(void) {
1779 call_snmp_init_once();
1782 } /* int csnmp_init */
1784 static int csnmp_shutdown(void) {
1785 data_definition_t *data_this;
1786 data_definition_t *data_next;
1788 /* When we get here, the read threads have been stopped and all the
1789 * `host_definition_t' will be freed. */
1790 DEBUG("snmp plugin: Destroying all data definitions.");
1792 data_this = data_head;
1794 while (data_this != NULL) {
1795 data_next = data_this->next;
1797 sfree(data_this->name);
1798 sfree(data_this->type);
1799 sfree(data_this->plugin_name);
1800 sfree(data_this->plugin_instance);
1801 sfree(data_this->type_instance);
1802 sfree(data_this->instance_prefix);
1803 sfree(data_this->values);
1804 sfree(data_this->ignores);
1807 data_this = data_next;
1811 } /* int csnmp_shutdown */
1813 void module_register(void) {
1814 plugin_register_complex_config("snmp", csnmp_config);
1815 plugin_register_init("snmp", csnmp_init);
1816 plugin_register_shutdown("snmp", csnmp_shutdown);
1817 } /* void module_register */