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;
129 static data_definition_t *data_head;
134 static int csnmp_read_host(user_data_t *ud);
139 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
140 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
141 memcpy(dst->oid, src, sizeof(*src) * n);
145 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
146 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
149 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
150 /* Make sure "src" is in "root"s subtree. */
151 if (src->oid_len <= root->oid_len)
153 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
154 /* n = */ root->oid_len) != 0)
157 memset(dst, 0, sizeof(*dst));
158 dst->oid_len = src->oid_len - root->oid_len;
159 memcpy(dst->oid, &src->oid[root->oid_len],
160 dst->oid_len * sizeof(dst->oid[0]));
164 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
166 char oid_str[MAX_OID_LEN][16];
167 char *oid_str_ptr[MAX_OID_LEN];
169 for (size_t i = 0; i < o->oid_len; i++) {
170 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
171 oid_str_ptr[i] = oid_str[i];
174 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
177 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
179 if (host->sess_handle == NULL)
182 snmp_sess_close(host->sess_handle);
183 host->sess_handle = NULL;
184 } /* }}} void csnmp_host_close_session */
186 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
188 host_definition_t *hd;
195 if (hd->name != NULL) {
196 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
199 csnmp_host_close_session(hd);
203 sfree(hd->community);
205 sfree(hd->auth_passphrase);
206 sfree(hd->priv_passphrase);
208 sfree(hd->data_list);
211 } /* }}} void csnmp_host_definition_destroy */
213 /* Many functions to handle the configuration. {{{ */
214 /* First there are many functions which do configuration stuff. It's a big
215 * bloated and messy, I'm afraid. */
218 * Callgraph for the config stuff:
220 * +-> call_snmp_init_once
221 * +-> csnmp_config_add_data
222 * ! +-> csnmp_config_add_data_instance
223 * ! +-> csnmp_config_add_data_values
224 * +-> csnmp_config_add_host
225 * +-> csnmp_config_add_host_version
226 * +-> csnmp_config_add_host_collect
227 * +-> csnmp_config_add_host_auth_protocol
228 * +-> csnmp_config_add_host_priv_protocol
229 * +-> csnmp_config_add_host_security_level
231 static void call_snmp_init_once(void) {
232 static int have_init;
235 init_snmp(PACKAGE_NAME);
237 } /* void call_snmp_init_once */
239 static int csnmp_config_add_data_instance_oid(data_definition_t *dd,
242 char buffer[DATA_MAX_NAME_LEN];
245 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
249 if (dd->instance.configured) {
250 ERROR("snmp plugin: Only one of options `TypeInstanceOID', "
251 "`PluginInstanceOID' or `Instance' can be used in `Data' block.");
255 dd->instance.is_plugin = is_plugin;
256 dd->instance.configured = true;
258 if (strlen(buffer) == 0) {
262 dd->instance.oid.oid_len = MAX_OID_LEN;
264 if (!read_objid(buffer, dd->instance.oid.oid, &dd->instance.oid.oid_len)) {
265 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
270 } /* int csnmp_config_add_data_instance_oid */
272 static int csnmp_config_add_data_values(data_definition_t *dd,
273 oconfig_item_t *ci) {
274 if (ci->values_num < 1) {
275 WARNING("snmp plugin: `Values' needs at least one argument.");
279 for (int i = 0; i < ci->values_num; i++)
280 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
281 WARNING("snmp plugin: `Values' needs only string argument.");
287 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
288 if (dd->values == NULL)
290 dd->values_len = (size_t)ci->values_num;
292 for (int i = 0; i < ci->values_num; i++) {
293 dd->values[i].oid_len = MAX_OID_LEN;
295 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
296 &dd->values[i].oid_len)) {
297 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
298 ci->values[i].value.string);
307 } /* int csnmp_config_add_data_instance */
309 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
310 oconfig_item_t *ci) {
311 if (ci->values_num < 1)
314 for (int i = 0; i < ci->values_num; i++) {
315 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
316 WARNING("snmp plugin: `Ignore' needs only string argument.");
321 for (int i = 0; i < ci->values_num; ++i) {
322 if (strarray_add(&(dd->ignores), &(dd->ignores_len),
323 ci->values[i].value.string) != 0) {
324 ERROR("snmp plugin: Can't allocate memory");
325 strarray_free(dd->ignores, dd->ignores_len);
330 } /* int csnmp_config_add_data_blacklist */
332 static int csnmp_config_add_data(oconfig_item_t *ci) {
333 data_definition_t *dd = calloc(1, sizeof(*dd));
337 int status = cf_util_get_string(ci, &dd->name);
348 dd->plugin_name = strdup("snmp");
349 if (dd->plugin_name == NULL) {
350 ERROR("snmp plugin: Can't allocate memory");
354 for (int i = 0; i < ci->children_num; i++) {
355 oconfig_item_t *option = ci->children + i;
357 if (strcasecmp("Type", option->key) == 0)
358 status = cf_util_get_string(option, &dd->type);
359 else if (strcasecmp("Table", option->key) == 0)
360 status = cf_util_get_boolean(option, &dd->is_table);
361 else if (strcasecmp("Plugin", option->key) == 0)
362 status = cf_util_get_string(option, &dd->plugin_name);
363 else if (strcasecmp("Instance", option->key) == 0) {
365 /* Instance is OID */
366 WARNING("snmp plugin: Option `Instance' is deprecated, please update "
367 "Data \"%s\" block to use option `TypeInstanceOID'.",
369 status = csnmp_config_add_data_instance_oid(dd, option,
370 false /* type instance */);
372 /* Instance is a simple string */
373 WARNING("snmp plugin: Option `Instance' is deprecated, please update "
374 "Data \"%s\" block to use option `TypeInstance'.",
376 status = cf_util_get_string(option, &dd->type_instance);
378 } else if (strcasecmp("PluginInstance", option->key) == 0)
379 status = cf_util_get_string(option, &dd->plugin_instance);
380 else if (strcasecmp("TypeInstance", option->key) == 0)
381 status = cf_util_get_string(option, &dd->type_instance);
382 else if (strcasecmp("PluginInstanceOID", option->key) == 0)
383 status = csnmp_config_add_data_instance_oid(dd, option,
384 true /* plugin instance */);
385 else if (strcasecmp("TypeInstanceOID", option->key) == 0)
386 status = csnmp_config_add_data_instance_oid(dd, option,
387 false /* type instance */);
388 else if (strcasecmp("InstancePrefix", option->key) == 0)
389 status = cf_util_get_string(option, &dd->instance_prefix);
390 else if (strcasecmp("Values", option->key) == 0)
391 status = csnmp_config_add_data_values(dd, option);
392 else if (strcasecmp("Shift", option->key) == 0)
393 status = cf_util_get_double(option, &dd->shift);
394 else if (strcasecmp("Scale", option->key) == 0)
395 status = cf_util_get_double(option, &dd->scale);
396 else if (strcasecmp("Ignore", option->key) == 0)
397 status = csnmp_config_add_data_blacklist(dd, option);
398 else if (strcasecmp("InvertMatch", option->key) == 0)
399 status = cf_util_get_boolean(option, &dd->invert_match);
401 WARNING("snmp plugin: Option `%s' not allowed here.", option->key);
407 } /* for (ci->children) */
409 while (status == 0) {
411 if (dd->plugin_instance && dd->instance.is_plugin) {
412 WARNING("snmp plugin: Option `PluginInstance' will be ignored for "
416 if (dd->type_instance && !dd->instance.is_plugin) {
417 WARNING("snmp plugin: Option `TypeInstance' will be ignored for Data "
422 if (dd->instance.configured) {
423 if (dd->instance.is_plugin) {
424 WARNING("snmp plugin: Option `PluginInstanceOID' will be ignored for "
428 WARNING("snmp plugin: Option `TypeInstanceOID' will be ignored for "
434 if (dd->instance_prefix) {
435 WARNING("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
436 "is set to `false'.",
441 if (dd->type == NULL) {
442 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
446 if (dd->values == NULL) {
447 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
453 } /* while (status == 0) */
458 sfree(dd->plugin_name);
459 sfree(dd->plugin_instance);
460 sfree(dd->type_instance);
461 sfree(dd->instance_prefix);
468 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
470 dd->name, dd->type, (dd->is_table) ? "true" : "false", dd->values_len);
472 DEBUG("snmp plugin: plugin_instance = %s, type_instance = %s,",
473 dd->plugin_instance, dd->type_instance);
475 DEBUG("snmp plugin: instance_by_oid = %s, to_plugin_instance = %s }",
476 (dd->instance.oid.oid_len > 0) ? "true" : "SUBID",
477 (dd->instance.is_plugin) ? "true" : "false");
479 if (data_head == NULL)
482 data_definition_t *last;
484 while (last->next != NULL)
490 } /* int csnmp_config_add_data */
492 static int csnmp_config_add_host_version(host_definition_t *hd,
493 oconfig_item_t *ci) {
496 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
497 WARNING("snmp plugin: The `Version' config option needs exactly one number "
502 version = (int)ci->values[0].value.number;
503 if ((version < 1) || (version > 3)) {
504 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
508 hd->version = version;
511 } /* int csnmp_config_add_host_address */
513 static int csnmp_config_add_host_collect(host_definition_t *host,
514 oconfig_item_t *ci) {
515 data_definition_t *data;
516 data_definition_t **data_list;
519 if (ci->values_num < 1) {
520 WARNING("snmp plugin: `Collect' needs at least one argument.");
524 for (int i = 0; i < ci->values_num; i++)
525 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
526 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
530 data_list_len = host->data_list_len + ci->values_num;
532 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
533 if (data_list == NULL)
535 host->data_list = data_list;
537 for (int i = 0; i < ci->values_num; i++) {
538 for (data = data_head; data != NULL; data = data->next)
539 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
543 WARNING("snmp plugin: No such data configured: `%s'",
544 ci->values[i].value.string);
548 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
549 host->data_list_len, data->name);
551 host->data_list[host->data_list_len] = data;
552 host->data_list_len++;
553 } /* for (values_num) */
556 } /* int csnmp_config_add_host_collect */
558 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
559 oconfig_item_t *ci) {
563 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
567 if (strcasecmp("MD5", buffer) == 0) {
568 hd->auth_protocol = usmHMACMD5AuthProtocol;
569 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
570 } else if (strcasecmp("SHA", buffer) == 0) {
571 hd->auth_protocol = usmHMACSHA1AuthProtocol;
572 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
574 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
579 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
580 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
583 } /* int csnmp_config_add_host_auth_protocol */
585 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
586 oconfig_item_t *ci) {
590 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
594 if (strcasecmp("AES", buffer) == 0) {
595 hd->priv_protocol = usmAESPrivProtocol;
596 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
597 } else if (strcasecmp("DES", buffer) == 0) {
598 hd->priv_protocol = usmDESPrivProtocol;
599 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
601 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
606 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
607 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
610 } /* int csnmp_config_add_host_priv_protocol */
612 static int csnmp_config_add_host_security_level(host_definition_t *hd,
613 oconfig_item_t *ci) {
617 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
621 if (strcasecmp("noAuthNoPriv", buffer) == 0)
622 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
623 else if (strcasecmp("authNoPriv", buffer) == 0)
624 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
625 else if (strcasecmp("authPriv", buffer) == 0)
626 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
628 WARNING("snmp plugin: The `SecurityLevel' config option must be "
629 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
633 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
637 } /* int csnmp_config_add_host_security_level */
639 static int csnmp_config_add_host(oconfig_item_t *ci) {
640 host_definition_t *hd;
643 /* Registration stuff. */
644 char cb_name[DATA_MAX_NAME_LEN];
646 hd = calloc(1, sizeof(*hd));
650 C_COMPLAIN_INIT(&hd->complaint);
652 status = cf_util_get_string(ci, &hd->name);
658 hd->sess_handle = NULL;
661 /* These mean that we have not set a timeout or retry value */
665 for (int i = 0; i < ci->children_num; i++) {
666 oconfig_item_t *option = ci->children + i;
668 if (strcasecmp("Address", option->key) == 0)
669 status = cf_util_get_string(option, &hd->address);
670 else if (strcasecmp("Community", option->key) == 0)
671 status = cf_util_get_string(option, &hd->community);
672 else if (strcasecmp("Version", option->key) == 0)
673 status = csnmp_config_add_host_version(hd, option);
674 else if (strcasecmp("Timeout", option->key) == 0)
675 status = cf_util_get_cdtime(option, &hd->timeout);
676 else if (strcasecmp("Retries", option->key) == 0)
677 status = cf_util_get_int(option, &hd->retries);
678 else if (strcasecmp("Collect", option->key) == 0)
679 status = csnmp_config_add_host_collect(hd, option);
680 else if (strcasecmp("Interval", option->key) == 0)
681 status = cf_util_get_cdtime(option, &hd->interval);
682 else if (strcasecmp("Username", option->key) == 0)
683 status = cf_util_get_string(option, &hd->username);
684 else if (strcasecmp("AuthProtocol", option->key) == 0)
685 status = csnmp_config_add_host_auth_protocol(hd, option);
686 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
687 status = csnmp_config_add_host_priv_protocol(hd, option);
688 else if (strcasecmp("AuthPassphrase", option->key) == 0)
689 status = cf_util_get_string(option, &hd->auth_passphrase);
690 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
691 status = cf_util_get_string(option, &hd->priv_passphrase);
692 else if (strcasecmp("SecurityLevel", option->key) == 0)
693 status = csnmp_config_add_host_security_level(hd, option);
694 else if (strcasecmp("Context", option->key) == 0)
695 status = cf_util_get_string(option, &hd->context);
698 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
705 } /* for (ci->children) */
707 while (status == 0) {
708 if (hd->address == NULL) {
709 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
713 if (hd->community == NULL && hd->version < 3) {
714 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
718 if (hd->version == 3) {
719 if (hd->username == NULL) {
720 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
724 if (hd->security_level == 0) {
725 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
730 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
731 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
732 if (hd->auth_protocol == NULL) {
733 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
738 if (hd->auth_passphrase == NULL) {
739 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
745 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
746 if (hd->priv_protocol == NULL) {
747 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
752 if (hd->priv_passphrase == NULL) {
753 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
762 } /* while (status == 0) */
765 csnmp_host_definition_destroy(hd);
769 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
771 hd->name, hd->address, hd->community, hd->version);
773 snprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
775 status = plugin_register_complex_read(
776 /* group = */ NULL, cb_name, csnmp_read_host, hd->interval,
778 .data = hd, .free_func = csnmp_host_definition_destroy,
781 ERROR("snmp plugin: Registering complex read function failed.");
786 } /* int csnmp_config_add_host */
788 static int csnmp_config(oconfig_item_t *ci) {
789 call_snmp_init_once();
791 for (int i = 0; i < ci->children_num; i++) {
792 oconfig_item_t *child = ci->children + i;
793 if (strcasecmp("Data", child->key) == 0)
794 csnmp_config_add_data(child);
795 else if (strcasecmp("Host", child->key) == 0)
796 csnmp_config_add_host(child);
798 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
800 } /* for (ci->children) */
803 } /* int csnmp_config */
805 /* }}} End of the config stuff. Now the interesting part begins */
807 static void csnmp_host_open_session(host_definition_t *host) {
808 struct snmp_session sess;
811 if (host->sess_handle != NULL)
812 csnmp_host_close_session(host);
814 snmp_sess_init(&sess);
815 sess.peername = host->address;
816 switch (host->version) {
818 sess.version = SNMP_VERSION_1;
821 sess.version = SNMP_VERSION_3;
824 sess.version = SNMP_VERSION_2c;
828 if (host->version == 3) {
829 sess.securityName = host->username;
830 sess.securityNameLen = strlen(host->username);
831 sess.securityLevel = host->security_level;
833 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
834 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
835 sess.securityAuthProto = host->auth_protocol;
836 sess.securityAuthProtoLen = host->auth_protocol_len;
837 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
838 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
839 (u_char *)host->auth_passphrase,
840 strlen(host->auth_passphrase), sess.securityAuthKey,
841 &sess.securityAuthKeyLen);
842 if (error != SNMPERR_SUCCESS) {
843 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
849 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
850 sess.securityPrivProto = host->priv_protocol;
851 sess.securityPrivProtoLen = host->priv_protocol_len;
852 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
853 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
854 (u_char *)host->priv_passphrase,
855 strlen(host->priv_passphrase), sess.securityPrivKey,
856 &sess.securityPrivKeyLen);
857 if (error != SNMPERR_SUCCESS) {
858 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
864 if (host->context != NULL) {
865 sess.contextName = host->context;
866 sess.contextNameLen = strlen(host->context);
868 } else /* SNMPv1/2 "authenticates" with community string */
870 sess.community = (u_char *)host->community;
871 sess.community_len = strlen(host->community);
874 /* Set timeout & retries, if they have been changed from the default */
875 if (host->timeout != 0) {
876 /* net-snmp expects microseconds */
877 sess.timeout = CDTIME_T_TO_US(host->timeout);
879 if (host->retries >= 0) {
880 sess.retries = host->retries;
883 /* snmp_sess_open will copy the `struct snmp_session *'. */
884 host->sess_handle = snmp_sess_open(&sess);
886 if (host->sess_handle == NULL) {
889 snmp_error(&sess, NULL, NULL, &errstr);
891 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
892 (errstr == NULL) ? "Unknown problem" : errstr);
895 } /* void csnmp_host_open_session */
897 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
899 static value_t csnmp_value_list_to_value(struct variable_list *vl, int type,
900 double scale, double shift,
901 const char *host_name,
902 const char *data_name) {
904 uint64_t tmp_unsigned = 0;
905 int64_t tmp_signed = 0;
907 /* Set to true when the original SNMP type appears to have been signed. */
908 bool prefer_signed = 0;
910 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
911 (vl->type == ASN_COUNTER)
913 || (vl->type == ASN_TIMETICKS)
915 || (vl->type == ASN_GAUGE)) {
916 tmp_unsigned = (uint32_t)*vl->val.integer;
917 tmp_signed = (int32_t)*vl->val.integer;
919 if (vl->type == ASN_INTEGER)
922 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
923 } else if (vl->type == ASN_COUNTER64) {
924 tmp_unsigned = (uint32_t)vl->val.counter64->high;
925 tmp_unsigned = tmp_unsigned << 32;
926 tmp_unsigned += (uint32_t)vl->val.counter64->low;
927 tmp_signed = (int64_t)tmp_unsigned;
928 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
929 } else if (vl->type == ASN_OCTET_STR) {
930 /* We'll handle this later.. */
932 char oid_buffer[1024] = {0};
934 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
938 if (vl->type == ASN_NULL)
939 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
942 WARNING("snmp plugin: I don't know the ASN type #%i "
943 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
944 (int)vl->type, oid_buffer,
945 (data_name != NULL) ? data_name : "UNKNOWN",
946 (host_name != NULL) ? host_name : "UNKNOWN");
951 if (vl->type == ASN_OCTET_STR) {
954 if (vl->val.string != NULL) {
956 size_t string_length;
958 string_length = sizeof(string) - 1;
959 if (vl->val_len < string_length)
960 string_length = vl->val_len;
962 /* The strings we get from the Net-SNMP library may not be null
963 * terminated. That is why we're using `memcpy' here and not `strcpy'.
964 * `string_length' is set to `vl->val_len' which holds the length of the
966 memcpy(string, vl->val.string, string_length);
967 string[string_length] = 0;
969 status = parse_value(string, &ret, type);
971 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
973 (host_name != NULL) ? host_name : "UNKNOWN",
974 DS_TYPE_TO_STRING(type), string);
980 case DS_TYPE_COUNTER:
982 case DS_TYPE_ABSOLUTE:
983 memset(&ret, 0, sizeof(ret));
991 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
992 "data source type: %i.",
997 } /* if (vl->type == ASN_OCTET_STR) */
998 else if (type == DS_TYPE_COUNTER) {
999 ret.counter = tmp_unsigned;
1000 } else if (type == DS_TYPE_GAUGE) {
1003 else if (prefer_signed)
1004 ret.gauge = (scale * tmp_signed) + shift;
1006 ret.gauge = (scale * tmp_unsigned) + shift;
1007 } else if (type == DS_TYPE_DERIVE) {
1009 ret.derive = (derive_t)tmp_signed;
1011 ret.derive = (derive_t)tmp_unsigned;
1012 } else if (type == DS_TYPE_ABSOLUTE) {
1013 ret.absolute = (absolute_t)tmp_unsigned;
1015 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1022 } /* value_t csnmp_value_list_to_value */
1024 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1025 * representation and writes it to dst. Returns zero on success and ENOMEM if
1026 * dst is not large enough to hold the string. dst is guaranteed to be
1027 * nul-terminated. */
1028 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
1029 const struct variable_list *vb,
1037 buffer_free = dst_size;
1039 for (size_t i = 0; i < vb->val_len; i++) {
1042 status = snprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
1043 (unsigned int)vb->val.bitstring[i]);
1044 assert(status >= 0);
1046 if (((size_t)status) >= buffer_free) /* truncated */
1048 dst[dst_size - 1] = 0;
1050 } else /* if (status < buffer_free) */
1052 buffer_ptr += (size_t)status;
1053 buffer_free -= (size_t)status;
1058 } /* }}} int csnmp_strvbcopy_hexstring */
1060 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1061 * dst. If non-printable characters are detected, it will switch to a hex
1062 * representation of the string. Returns zero on success, EINVAL if vb does not
1063 * contain a string and ENOMEM if dst is not large enough to contain the
1065 static int csnmp_strvbcopy(char *dst, /* {{{ */
1066 const struct variable_list *vb, size_t dst_size) {
1070 if (vb->type == ASN_OCTET_STR)
1071 src = (char *)vb->val.string;
1072 else if (vb->type == ASN_BIT_STR)
1073 src = (char *)vb->val.bitstring;
1074 else if (vb->type == ASN_IPADDRESS) {
1075 return snprintf(dst, dst_size,
1076 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1077 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1078 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1084 num_chars = dst_size - 1;
1085 if (num_chars > vb->val_len)
1086 num_chars = vb->val_len;
1088 for (size_t i = 0; i < num_chars; i++) {
1089 /* Check for control characters. */
1090 if ((unsigned char)src[i] < 32)
1091 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1095 dst[dst_size - 1] = 0;
1097 if (dst_size <= vb->val_len)
1101 } /* }}} int csnmp_strvbcopy */
1103 static int csnmp_instance_list_add(csnmp_list_instances_t **head,
1104 csnmp_list_instances_t **tail,
1105 const struct snmp_pdu *res,
1106 const host_definition_t *hd,
1107 const data_definition_t *dd) {
1108 csnmp_list_instances_t *il;
1109 struct variable_list *vb;
1113 /* Set vb on the last variable */
1114 for (vb = res->variables; (vb != NULL) && (vb->next_variable != NULL);
1115 vb = vb->next_variable)
1120 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1122 il = calloc(1, sizeof(*il));
1124 ERROR("snmp plugin: calloc failed.");
1129 status = csnmp_oid_suffix(&il->suffix, &vb_name, &dd->instance.oid);
1135 /* Get instance name */
1136 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1137 (vb->type == ASN_IPADDRESS)) {
1140 csnmp_strvbcopy(il->instance, vb, sizeof(il->instance));
1141 bool is_matched = 0;
1142 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1143 status = fnmatch(dd->ignores[i], il->instance, 0);
1145 if (!dd->invert_match) {
1154 if (dd->invert_match && !is_matched) {
1158 for (ptr = il->instance; *ptr != '\0'; ptr++) {
1159 if ((*ptr > 0) && (*ptr < 32))
1161 else if (*ptr == '/')
1164 DEBUG("snmp plugin: il->instance = `%s';", il->instance);
1166 value_t val = csnmp_value_list_to_value(
1167 vb, DS_TYPE_COUNTER,
1168 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1169 snprintf(il->instance, sizeof(il->instance), "%" PRIu64,
1170 (uint64_t)val.counter);
1173 /* TODO: Debugging output */
1182 } /* int csnmp_instance_list_add */
1184 static int csnmp_dispatch_table(host_definition_t *host,
1185 data_definition_t *data,
1186 csnmp_list_instances_t *instance_list,
1187 csnmp_table_values_t **value_table) {
1188 const data_set_t *ds;
1189 value_list_t vl = VALUE_LIST_INIT;
1191 csnmp_list_instances_t *instance_list_ptr;
1192 csnmp_table_values_t *value_table_ptr[data->values_len];
1196 oid_t current_suffix;
1198 ds = plugin_get_ds(data->type);
1200 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1203 assert(ds->ds_num == data->values_len);
1204 assert(data->values_len > 0);
1206 instance_list_ptr = instance_list;
1208 for (i = 0; i < data->values_len; i++)
1209 value_table_ptr[i] = value_table[i];
1211 sstrncpy(vl.host, host->name, sizeof(vl.host));
1212 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1214 vl.interval = host->interval;
1218 bool suffix_skipped = 0;
1220 /* Determine next suffix to handle. */
1221 if (instance_list != NULL) {
1222 if (instance_list_ptr == NULL) {
1227 memcpy(¤t_suffix, &instance_list_ptr->suffix,
1228 sizeof(current_suffix));
1230 /* no instance configured */
1231 csnmp_table_values_t *ptr = value_table_ptr[0];
1237 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1240 /* Update all the value_table_ptr to point at the entry with the same
1241 * trailing partial OID */
1242 for (i = 0; i < data->values_len; i++) {
1244 (value_table_ptr[i] != NULL) &&
1245 (csnmp_oid_compare(&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1246 value_table_ptr[i] = value_table_ptr[i]->next;
1248 if (value_table_ptr[i] == NULL) {
1251 } else if (csnmp_oid_compare(&value_table_ptr[i]->suffix,
1252 ¤t_suffix) > 0) {
1253 /* This suffix is missing in the subtree. Indicate this with the
1254 * "suffix_skipped" flag and try the next instance / suffix. */
1258 } /* for (i = 0; i < columns; i++) */
1263 /* Matching the values failed. Start from the beginning again. */
1264 if (suffix_skipped) {
1265 if (instance_list != NULL)
1266 instance_list_ptr = instance_list_ptr->next;
1268 value_table_ptr[0] = value_table_ptr[0]->next;
1273 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1274 * to the same subid. instance_list_ptr is either NULL or points to the
1275 * same subid, too. */
1277 for (i = 1; i < data->values_len; i++) {
1278 assert(value_table_ptr[i] != NULL);
1279 assert(csnmp_oid_compare(&value_table_ptr[i - 1]->suffix,
1280 &value_table_ptr[i]->suffix) == 0);
1282 assert((instance_list_ptr == NULL) ||
1283 (csnmp_oid_compare(&instance_list_ptr->suffix,
1284 &value_table_ptr[0]->suffix) == 0));
1287 sstrncpy(vl.type, data->type, sizeof(vl.type));
1290 char temp[DATA_MAX_NAME_LEN];
1292 if (instance_list_ptr == NULL)
1293 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1295 sstrncpy(temp, instance_list_ptr->instance, sizeof(temp));
1297 if (data->instance.is_plugin) {
1298 if (data->instance_prefix == NULL)
1299 sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
1301 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
1302 data->instance_prefix, temp);
1304 if (data->type_instance)
1305 sstrncpy(vl.type_instance, data->type_instance,
1306 sizeof(vl.type_instance));
1308 if (data->instance_prefix == NULL)
1309 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1311 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1312 data->instance_prefix, temp);
1314 if (data->plugin_instance)
1315 sstrncpy(vl.plugin_instance, data->plugin_instance,
1316 sizeof(vl.plugin_instance));
1320 vl.values_len = data->values_len;
1321 value_t values[vl.values_len];
1324 for (i = 0; i < data->values_len; i++)
1325 vl.values[i] = value_table_ptr[i]->value;
1327 plugin_dispatch_values(&vl);
1329 /* prevent leakage of pointer to local variable. */
1333 if (instance_list != NULL)
1334 instance_list_ptr = instance_list_ptr->next;
1336 value_table_ptr[0] = value_table_ptr[0]->next;
1337 } /* while (have_more) */
1340 } /* int csnmp_dispatch_table */
1342 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1343 struct snmp_pdu *req;
1344 struct snmp_pdu *res = NULL;
1345 struct variable_list *vb;
1347 const data_set_t *ds;
1349 size_t oid_list_len = data->values_len;
1351 if (data->instance.oid.oid_len > 0)
1354 /* Holds the last OID returned by the device. We use this in the GETNEXT
1355 * request to proceed. */
1356 oid_t oid_list[oid_list_len];
1357 /* Set to false when an OID has left its subtree so we don't re-request it
1359 csnmp_oid_type_t oid_list_todo[oid_list_len];
1364 /* `value_list_head' and `value_list_tail' implement a linked list for each
1365 * value. `instance_list_head' and `instance_list_tail' implement a linked
1367 * instance names. This is used to jump gaps in the table. */
1368 csnmp_list_instances_t *instance_list_head;
1369 csnmp_list_instances_t *instance_list_tail;
1370 csnmp_table_values_t **value_list_head;
1371 csnmp_table_values_t **value_list_tail;
1373 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1376 if (host->sess_handle == NULL) {
1377 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1381 ds = plugin_get_ds(data->type);
1383 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1387 if (ds->ds_num != data->values_len) {
1388 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1389 " values, but config talks "
1391 data->type, ds->ds_num, data->values_len);
1394 assert(data->values_len > 0);
1396 for (i = 0; i < data->values_len; i++)
1397 oid_list_todo[i] = OID_TYPE_VARIABLE;
1399 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1400 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1402 if (data->instance.oid.oid_len > 0) {
1403 memcpy(oid_list + i, &data->instance.oid, sizeof(oid_t));
1404 oid_list_todo[i] = OID_TYPE_INSTANCE;
1407 /* We're going to construct n linked lists, one for each "value".
1408 * value_list_head will contain pointers to the heads of these linked lists,
1409 * value_list_tail will contain pointers to the tail of the lists. */
1410 value_list_head = calloc(data->values_len, sizeof(*value_list_head));
1411 value_list_tail = calloc(data->values_len, sizeof(*value_list_tail));
1412 if ((value_list_head == NULL) || (value_list_tail == NULL)) {
1413 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1414 sfree(value_list_head);
1415 sfree(value_list_tail);
1419 instance_list_head = NULL;
1420 instance_list_tail = NULL;
1423 while (status == 0) {
1424 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1426 ERROR("snmp plugin: snmp_pdu_create failed.");
1431 size_t oid_list_todo_num = 0;
1432 size_t var_idx[oid_list_len];
1433 memset(var_idx, 0, sizeof(var_idx));
1435 for (i = 0; i < oid_list_len; i++) {
1436 /* Do not rerequest already finished OIDs */
1437 if (!oid_list_todo[i])
1439 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1440 var_idx[oid_list_todo_num] = i;
1441 oid_list_todo_num++;
1444 if (oid_list_todo_num == 0) {
1445 /* The request is still empty - so we are finished */
1446 DEBUG("snmp plugin: all variables have left their subtree");
1453 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1455 /* snmp_sess_synch_response always frees our req PDU */
1458 if ((status != STAT_SUCCESS) || (res == NULL)) {
1459 char *errstr = NULL;
1461 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1463 c_complain(LOG_ERR, &host->complaint,
1464 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1465 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1472 csnmp_host_close_session(host);
1479 assert(res != NULL);
1480 c_release(LOG_INFO, &host->complaint,
1481 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1484 vb = res->variables;
1490 if (res->errstat != SNMP_ERR_NOERROR) {
1491 if (res->errindex != 0) {
1492 /* Find the OID which caused error */
1493 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1494 vb = vb->next_variable, i++)
1498 if ((res->errindex == 0) || (vb == NULL)) {
1499 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1500 host->name, data->name, snmp_errstring(res->errstat),
1506 char oid_buffer[1024] = {0};
1507 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1509 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1510 data->name, oid_buffer, snmp_errstring(res->errstat));
1512 /* Get value index from todo list and skip OID found */
1513 assert(res->errindex <= oid_list_todo_num);
1514 i = var_idx[res->errindex - 1];
1515 assert(i < oid_list_len);
1516 oid_list_todo[i] = 0;
1523 for (vb = res->variables, i = 0; (vb != NULL);
1524 vb = vb->next_variable, i++) {
1525 /* Calculate value index from todo list */
1526 while ((i < oid_list_len) && !oid_list_todo[i]) {
1529 if (i >= oid_list_len) {
1533 /* An instance is configured and the res variable we process is the
1535 if (oid_list_todo[i] == OID_TYPE_INSTANCE) {
1536 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1538 data->instance.oid.oid, data->instance.oid.oid_len, vb->name,
1539 vb->name_length, data->instance.oid.oid_len) != 0)) {
1540 DEBUG("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1541 host->name, data->name);
1542 oid_list_todo[i] = 0;
1546 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1547 * add it to the list */
1548 if (csnmp_instance_list_add(&instance_list_head, &instance_list_tail,
1549 res, host, data) != 0) {
1550 ERROR("snmp plugin: host %s: csnmp_instance_list_add failed.",
1555 } else if (oid_list_todo[i] == OID_TYPE_HOST) {
1558 } else /* The variable we are processing is a normal value */
1560 assert(oid_list_todo[i] == OID_TYPE_VARIABLE);
1562 csnmp_table_values_t *vt;
1567 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1570 "snmp plugin: src.oid_len = %d root.oid_len = %d is_endofmib = %s",
1571 vb_name.oid_len, (data->values + i)->oid_len,
1572 (vb->type == SNMP_ENDOFMIBVIEW) ? "true" : "false");
1574 /* Calculate the current suffix. This is later used to check that the
1575 * suffix is increasing. This also checks if we left the subtree */
1576 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1578 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1579 "Value probably left its subtree.",
1580 host->name, data->name, i);
1581 oid_list_todo[i] = 0;
1585 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1587 * table matching algorithm will get confused. */
1588 if ((value_list_tail[i] != NULL) &&
1589 (csnmp_oid_compare(&suffix, &value_list_tail[i]->suffix) <= 0)) {
1590 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1591 "Suffix is not increasing.",
1592 host->name, data->name, i);
1593 oid_list_todo[i] = 0;
1597 vt = calloc(1, sizeof(*vt));
1599 ERROR("snmp plugin: calloc failed.");
1605 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1606 data->shift, host->name, data->name);
1607 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1610 if (value_list_tail[i] == NULL)
1611 value_list_head[i] = vt;
1613 value_list_tail[i]->next = vt;
1614 value_list_tail[i] = vt;
1617 /* Copy OID to oid_list[i] */
1618 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1619 oid_list[i].oid_len = vb->name_length;
1621 } /* for (vb = res->variables ...) */
1626 } /* while (status == 0) */
1633 csnmp_dispatch_table(host, data, instance_list_head, value_list_head);
1635 /* Free all allocated variables here */
1636 while (instance_list_head != NULL) {
1637 csnmp_list_instances_t *next = instance_list_head->next;
1638 sfree(instance_list_head);
1639 instance_list_head = next;
1642 for (i = 0; i < data->values_len; i++) {
1643 while (value_list_head[i] != NULL) {
1644 csnmp_table_values_t *next = value_list_head[i]->next;
1645 sfree(value_list_head[i]);
1646 value_list_head[i] = next;
1650 sfree(value_list_head);
1651 sfree(value_list_tail);
1654 } /* int csnmp_read_table */
1656 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1657 struct snmp_pdu *req;
1658 struct snmp_pdu *res = NULL;
1659 struct variable_list *vb;
1661 const data_set_t *ds;
1662 value_list_t vl = VALUE_LIST_INIT;
1667 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
1670 if (host->sess_handle == NULL) {
1671 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1675 ds = plugin_get_ds(data->type);
1677 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1681 if (ds->ds_num != data->values_len) {
1682 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1683 " values, but config talks "
1685 data->type, ds->ds_num, data->values_len);
1689 vl.values_len = ds->ds_num;
1690 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
1691 if (vl.values == NULL)
1693 for (i = 0; i < vl.values_len; i++) {
1694 if (ds->ds[i].type == DS_TYPE_COUNTER)
1695 vl.values[i].counter = 0;
1697 vl.values[i].gauge = NAN;
1700 sstrncpy(vl.host, host->name, sizeof(vl.host));
1701 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1702 sstrncpy(vl.type, data->type, sizeof(vl.type));
1703 if (data->type_instance)
1704 sstrncpy(vl.type_instance, data->type_instance, sizeof(vl.type_instance));
1705 if (data->plugin_instance)
1706 sstrncpy(vl.plugin_instance, data->plugin_instance,
1707 sizeof(vl.plugin_instance));
1709 vl.interval = host->interval;
1711 req = snmp_pdu_create(SNMP_MSG_GET);
1713 ERROR("snmp plugin: snmp_pdu_create failed.");
1718 for (i = 0; i < data->values_len; i++)
1719 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
1721 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1723 if ((status != STAT_SUCCESS) || (res == NULL)) {
1724 char *errstr = NULL;
1726 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1727 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1728 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1735 csnmp_host_close_session(host);
1740 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
1743 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
1744 DEBUG("snmp plugin: Got this variable: %s", buffer);
1745 #endif /* COLLECT_DEBUG */
1747 for (i = 0; i < data->values_len; i++)
1748 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
1749 vb->name, vb->name_length) == 0)
1751 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1752 data->shift, host->name, data->name);
1753 } /* for (res->variables) */
1757 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
1758 plugin_dispatch_values(&vl);
1762 } /* int csnmp_read_value */
1764 static int csnmp_read_host(user_data_t *ud) {
1765 host_definition_t *host;
1772 if (host->interval == 0)
1773 host->interval = plugin_get_interval();
1775 if (host->sess_handle == NULL)
1776 csnmp_host_open_session(host);
1778 if (host->sess_handle == NULL)
1782 for (i = 0; i < host->data_list_len; i++) {
1783 data_definition_t *data = host->data_list[i];
1786 status = csnmp_read_table(host, data);
1788 status = csnmp_read_value(host, data);
1798 } /* int csnmp_read_host */
1800 static int csnmp_init(void) {
1801 call_snmp_init_once();
1804 } /* int csnmp_init */
1806 static int csnmp_shutdown(void) {
1807 data_definition_t *data_this;
1808 data_definition_t *data_next;
1810 /* When we get here, the read threads have been stopped and all the
1811 * `host_definition_t' will be freed. */
1812 DEBUG("snmp plugin: Destroying all data definitions.");
1814 data_this = data_head;
1816 while (data_this != NULL) {
1817 data_next = data_this->next;
1819 sfree(data_this->name);
1820 sfree(data_this->type);
1821 sfree(data_this->plugin_name);
1822 sfree(data_this->plugin_instance);
1823 sfree(data_this->type_instance);
1824 sfree(data_this->instance_prefix);
1825 sfree(data_this->values);
1826 sfree(data_this->ignores);
1829 data_this = data_next;
1833 } /* int csnmp_shutdown */
1835 void module_register(void) {
1836 plugin_register_complex_config("snmp", csnmp_config);
1837 plugin_register_init("snmp", csnmp_init);
1838 plugin_register_shutdown("snmp", csnmp_shutdown);
1839 } /* void module_register */