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_cell_char_s {
107 char value[DATA_MAX_NAME_LEN];
108 struct csnmp_cell_char_s *next;
110 typedef struct csnmp_cell_char_s csnmp_cell_char_t;
112 struct csnmp_cell_value_s {
115 struct csnmp_cell_value_s *next;
117 typedef struct csnmp_cell_value_s csnmp_cell_value_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(const struct variable_list *vl,
900 int type, 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 csnmp_cell_char_t *csnmp_get_char_cell(const struct variable_list *vb,
1104 const host_definition_t *hd,
1105 const data_definition_t *dd) {
1110 csnmp_cell_char_t *il = calloc(1, sizeof(*il));
1112 ERROR("snmp plugin: calloc failed.");
1118 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1120 if (csnmp_oid_suffix(&il->suffix, &vb_name, &dd->instance.oid) != 0) {
1126 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1127 (vb->type == ASN_IPADDRESS)) {
1129 csnmp_strvbcopy(il->value, vb, sizeof(il->value));
1132 value_t val = csnmp_value_list_to_value(
1133 vb, DS_TYPE_COUNTER,
1134 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1135 snprintf(il->value, sizeof(il->value), "%" PRIu64, (uint64_t)val.counter);
1139 } /* csnmp_cell_char_t csnmp_get_char_cell */
1141 static void csnmp_cells_append(csnmp_cell_char_t **head,
1142 csnmp_cell_char_t **tail,
1143 csnmp_cell_char_t *il) {
1149 } /* void csnmp_cells_append */
1151 static bool csnmp_ignore_instance(csnmp_cell_char_t *cell,
1152 const data_definition_t *dd) {
1153 bool is_matched = 0;
1154 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1155 int status = fnmatch(dd->ignores[i], cell->value, 0);
1157 if (!dd->invert_match) {
1165 if (dd->invert_match && !is_matched) {
1169 } /* bool csnmp_ignore_instance */
1171 static int csnmp_dispatch_table(host_definition_t *host,
1172 data_definition_t *data,
1173 csnmp_cell_char_t *instance_cells,
1174 csnmp_cell_value_t **value_cells) {
1175 const data_set_t *ds;
1176 value_list_t vl = VALUE_LIST_INIT;
1178 csnmp_cell_char_t *instance_cell_ptr;
1179 csnmp_cell_value_t *value_cell_ptr[data->values_len];
1183 oid_t current_suffix;
1185 ds = plugin_get_ds(data->type);
1187 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1190 assert(ds->ds_num == data->values_len);
1191 assert(data->values_len > 0);
1193 instance_cell_ptr = instance_cells;
1195 for (i = 0; i < data->values_len; i++)
1196 value_cell_ptr[i] = value_cells[i];
1198 sstrncpy(vl.host, host->name, sizeof(vl.host));
1199 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1201 vl.interval = host->interval;
1205 bool suffix_skipped = 0;
1207 /* Determine next suffix to handle. */
1208 if (instance_cells != NULL) {
1209 if (instance_cell_ptr == NULL) {
1214 memcpy(¤t_suffix, &instance_cell_ptr->suffix,
1215 sizeof(current_suffix));
1217 /* no instance configured */
1218 csnmp_cell_value_t *ptr = value_cell_ptr[0];
1224 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1227 /* Update all the value_cell_ptr to point at the entry with the same
1228 * trailing partial OID */
1229 for (i = 0; i < data->values_len; i++) {
1231 (value_cell_ptr[i] != NULL) &&
1232 (csnmp_oid_compare(&value_cell_ptr[i]->suffix, ¤t_suffix) < 0))
1233 value_cell_ptr[i] = value_cell_ptr[i]->next;
1235 if (value_cell_ptr[i] == NULL) {
1238 } else if (csnmp_oid_compare(&value_cell_ptr[i]->suffix,
1239 ¤t_suffix) > 0) {
1240 /* This suffix is missing in the subtree. Indicate this with the
1241 * "suffix_skipped" flag and try the next instance / suffix. */
1245 } /* for (i = 0; i < columns; i++) */
1250 /* Matching the values failed. Start from the beginning again. */
1251 if (suffix_skipped) {
1252 if (instance_cells != NULL)
1253 instance_cell_ptr = instance_cell_ptr->next;
1255 value_cell_ptr[0] = value_cell_ptr[0]->next;
1260 /* if we reach this line, all value_cell_ptr[i] are non-NULL and are set
1261 * to the same subid. instance_cell_ptr is either NULL or points to the
1262 * same subid, too. */
1264 for (i = 1; i < data->values_len; i++) {
1265 assert(value_cell_ptr[i] != NULL);
1266 assert(csnmp_oid_compare(&value_cell_ptr[i - 1]->suffix,
1267 &value_cell_ptr[i]->suffix) == 0);
1269 assert((instance_cell_ptr == NULL) ||
1270 (csnmp_oid_compare(&instance_cell_ptr->suffix,
1271 &value_cell_ptr[0]->suffix) == 0));
1274 sstrncpy(vl.type, data->type, sizeof(vl.type));
1277 char temp[DATA_MAX_NAME_LEN];
1279 if (instance_cell_ptr == NULL)
1280 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1282 sstrncpy(temp, instance_cell_ptr->value, sizeof(temp));
1284 if (data->instance.is_plugin) {
1285 if (data->instance_prefix == NULL)
1286 sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
1288 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
1289 data->instance_prefix, temp);
1291 if (data->type_instance)
1292 sstrncpy(vl.type_instance, data->type_instance,
1293 sizeof(vl.type_instance));
1295 if (data->instance_prefix == NULL)
1296 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1298 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1299 data->instance_prefix, temp);
1301 if (data->plugin_instance)
1302 sstrncpy(vl.plugin_instance, data->plugin_instance,
1303 sizeof(vl.plugin_instance));
1307 vl.values_len = data->values_len;
1308 value_t values[vl.values_len];
1311 for (i = 0; i < data->values_len; i++)
1312 vl.values[i] = value_cell_ptr[i]->value;
1314 plugin_dispatch_values(&vl);
1316 /* prevent leakage of pointer to local variable. */
1320 if (instance_cells != NULL)
1321 instance_cell_ptr = instance_cell_ptr->next;
1323 value_cell_ptr[0] = value_cell_ptr[0]->next;
1324 } /* while (have_more) */
1327 } /* int csnmp_dispatch_table */
1329 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1330 struct snmp_pdu *req;
1331 struct snmp_pdu *res = NULL;
1332 struct variable_list *vb;
1334 const data_set_t *ds;
1336 size_t oid_list_len = data->values_len;
1338 if (data->instance.oid.oid_len > 0)
1341 /* Holds the last OID returned by the device. We use this in the GETNEXT
1342 * request to proceed. */
1343 oid_t oid_list[oid_list_len];
1344 /* Set to false when an OID has left its subtree so we don't re-request it
1346 csnmp_oid_type_t oid_list_todo[oid_list_len];
1351 /* `value_list_head' and `value_cells_tail' implement a linked list for each
1352 * value. `instance_cells_head' and `instance_cells_tail' implement a linked
1354 * instance names. This is used to jump gaps in the table. */
1355 csnmp_cell_char_t *instance_cells_head;
1356 csnmp_cell_char_t *instance_cells_tail;
1357 csnmp_cell_value_t **value_cells_head;
1358 csnmp_cell_value_t **value_cells_tail;
1360 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1363 if (host->sess_handle == NULL) {
1364 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1368 ds = plugin_get_ds(data->type);
1370 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1374 if (ds->ds_num != data->values_len) {
1375 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1376 " values, but config talks "
1378 data->type, ds->ds_num, data->values_len);
1381 assert(data->values_len > 0);
1383 for (i = 0; i < data->values_len; i++)
1384 oid_list_todo[i] = OID_TYPE_VARIABLE;
1386 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1387 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1389 if (data->instance.oid.oid_len > 0) {
1390 memcpy(oid_list + i, &data->instance.oid, sizeof(oid_t));
1391 oid_list_todo[i] = OID_TYPE_INSTANCE;
1394 /* We're going to construct n linked lists, one for each "value".
1395 * value_cells_head will contain pointers to the heads of these linked lists,
1396 * value_cells_tail will contain pointers to the tail of the lists. */
1397 value_cells_head = calloc(data->values_len, sizeof(*value_cells_head));
1398 value_cells_tail = calloc(data->values_len, sizeof(*value_cells_tail));
1399 if ((value_cells_head == NULL) || (value_cells_tail == NULL)) {
1400 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1401 sfree(value_cells_head);
1402 sfree(value_cells_tail);
1406 instance_cells_head = NULL;
1407 instance_cells_tail = NULL;
1410 while (status == 0) {
1411 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1413 ERROR("snmp plugin: snmp_pdu_create failed.");
1418 size_t oid_list_todo_num = 0;
1419 size_t var_idx[oid_list_len];
1420 memset(var_idx, 0, sizeof(var_idx));
1422 for (i = 0; i < oid_list_len; i++) {
1423 /* Do not rerequest already finished OIDs */
1424 if (!oid_list_todo[i])
1426 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1427 var_idx[oid_list_todo_num] = i;
1428 oid_list_todo_num++;
1431 if (oid_list_todo_num == 0) {
1432 /* The request is still empty - so we are finished */
1433 DEBUG("snmp plugin: all variables have left their subtree");
1440 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1442 /* snmp_sess_synch_response always frees our req PDU */
1445 if ((status != STAT_SUCCESS) || (res == NULL)) {
1446 char *errstr = NULL;
1448 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1450 c_complain(LOG_ERR, &host->complaint,
1451 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1452 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1459 csnmp_host_close_session(host);
1466 assert(res != NULL);
1467 c_release(LOG_INFO, &host->complaint,
1468 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1471 vb = res->variables;
1477 if (res->errstat != SNMP_ERR_NOERROR) {
1478 if (res->errindex != 0) {
1479 /* Find the OID which caused error */
1480 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1481 vb = vb->next_variable, i++)
1485 if ((res->errindex == 0) || (vb == NULL)) {
1486 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1487 host->name, data->name, snmp_errstring(res->errstat),
1493 char oid_buffer[1024] = {0};
1494 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1496 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1497 data->name, oid_buffer, snmp_errstring(res->errstat));
1499 /* Get value index from todo list and skip OID found */
1500 assert(res->errindex <= oid_list_todo_num);
1501 i = var_idx[res->errindex - 1];
1502 assert(i < oid_list_len);
1503 oid_list_todo[i] = 0;
1510 for (vb = res->variables, i = 0; (vb != NULL);
1511 vb = vb->next_variable, i++) {
1512 /* Calculate value index from todo list */
1513 while ((i < oid_list_len) && !oid_list_todo[i]) {
1516 if (i >= oid_list_len) {
1520 /* An instance is configured and the res variable we process is the
1522 if (oid_list_todo[i] == OID_TYPE_INSTANCE) {
1523 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1525 data->instance.oid.oid, data->instance.oid.oid_len, vb->name,
1526 vb->name_length, data->instance.oid.oid_len) != 0)) {
1527 DEBUG("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1528 host->name, data->name);
1529 oid_list_todo[i] = 0;
1533 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1534 * add it to the list */
1535 csnmp_cell_char_t *cell = csnmp_get_char_cell(vb, host, data);
1537 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1543 if (csnmp_ignore_instance(cell, data)) {
1547 for (char *ptr = cell->value; *ptr != '\0'; ptr++) {
1548 if ((*ptr > 0) && (*ptr < 32))
1550 else if (*ptr == '/')
1554 DEBUG("snmp plugin: il->instance = `%s';", cell->value);
1555 csnmp_cells_append(&instance_cells_head, &instance_cells_tail, cell);
1557 } else if (oid_list_todo[i] == OID_TYPE_HOST) {
1560 } else /* The variable we are processing is a normal value */
1562 assert(oid_list_todo[i] == OID_TYPE_VARIABLE);
1564 csnmp_cell_value_t *vt;
1569 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1572 "snmp plugin: src.oid_len = %d root.oid_len = %d is_endofmib = %s",
1573 vb_name.oid_len, (data->values + i)->oid_len,
1574 (vb->type == SNMP_ENDOFMIBVIEW) ? "true" : "false");
1576 /* Calculate the current suffix. This is later used to check that the
1577 * suffix is increasing. This also checks if we left the subtree */
1578 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1580 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1581 "Value probably left its subtree.",
1582 host->name, data->name, i);
1583 oid_list_todo[i] = 0;
1587 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1588 * our table matching algorithm will get confused. */
1589 if ((value_cells_tail[i] != NULL) &&
1590 (csnmp_oid_compare(&suffix, &value_cells_tail[i]->suffix) <= 0)) {
1591 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1592 "Suffix is not increasing.",
1593 host->name, data->name, i);
1594 oid_list_todo[i] = 0;
1598 vt = calloc(1, sizeof(*vt));
1600 ERROR("snmp plugin: calloc failed.");
1606 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1607 data->shift, host->name, data->name);
1608 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1611 if (value_cells_tail[i] == NULL)
1612 value_cells_head[i] = vt;
1614 value_cells_tail[i]->next = vt;
1615 value_cells_tail[i] = vt;
1618 /* Copy OID to oid_list[i] */
1619 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1620 oid_list[i].oid_len = vb->name_length;
1622 } /* for (vb = res->variables ...) */
1627 } /* while (status == 0) */
1634 csnmp_dispatch_table(host, data, instance_cells_head, value_cells_head);
1636 /* Free all allocated variables here */
1637 while (instance_cells_head != NULL) {
1638 csnmp_cell_char_t *next = instance_cells_head->next;
1639 sfree(instance_cells_head);
1640 instance_cells_head = next;
1643 for (i = 0; i < data->values_len; i++) {
1644 while (value_cells_head[i] != NULL) {
1645 csnmp_cell_value_t *next = value_cells_head[i]->next;
1646 sfree(value_cells_head[i]);
1647 value_cells_head[i] = next;
1651 sfree(value_cells_head);
1652 sfree(value_cells_tail);
1655 } /* int csnmp_read_table */
1657 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1658 struct snmp_pdu *req;
1659 struct snmp_pdu *res = NULL;
1660 struct variable_list *vb;
1662 const data_set_t *ds;
1663 value_list_t vl = VALUE_LIST_INIT;
1668 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
1671 if (host->sess_handle == NULL) {
1672 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1676 ds = plugin_get_ds(data->type);
1678 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1682 if (ds->ds_num != data->values_len) {
1683 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1684 " values, but config talks "
1686 data->type, ds->ds_num, data->values_len);
1690 vl.values_len = ds->ds_num;
1691 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
1692 if (vl.values == NULL)
1694 for (i = 0; i < vl.values_len; i++) {
1695 if (ds->ds[i].type == DS_TYPE_COUNTER)
1696 vl.values[i].counter = 0;
1698 vl.values[i].gauge = NAN;
1701 sstrncpy(vl.host, host->name, sizeof(vl.host));
1702 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1703 sstrncpy(vl.type, data->type, sizeof(vl.type));
1704 if (data->type_instance)
1705 sstrncpy(vl.type_instance, data->type_instance, sizeof(vl.type_instance));
1706 if (data->plugin_instance)
1707 sstrncpy(vl.plugin_instance, data->plugin_instance,
1708 sizeof(vl.plugin_instance));
1710 vl.interval = host->interval;
1712 req = snmp_pdu_create(SNMP_MSG_GET);
1714 ERROR("snmp plugin: snmp_pdu_create failed.");
1719 for (i = 0; i < data->values_len; i++)
1720 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
1722 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1724 if ((status != STAT_SUCCESS) || (res == NULL)) {
1725 char *errstr = NULL;
1727 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1728 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1729 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1736 csnmp_host_close_session(host);
1741 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
1744 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
1745 DEBUG("snmp plugin: Got this variable: %s", buffer);
1746 #endif /* COLLECT_DEBUG */
1748 for (i = 0; i < data->values_len; i++)
1749 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
1750 vb->name, vb->name_length) == 0)
1752 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1753 data->shift, host->name, data->name);
1754 } /* for (res->variables) */
1758 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
1759 plugin_dispatch_values(&vl);
1763 } /* int csnmp_read_value */
1765 static int csnmp_read_host(user_data_t *ud) {
1766 host_definition_t *host;
1773 if (host->interval == 0)
1774 host->interval = plugin_get_interval();
1776 if (host->sess_handle == NULL)
1777 csnmp_host_open_session(host);
1779 if (host->sess_handle == NULL)
1783 for (i = 0; i < host->data_list_len; i++) {
1784 data_definition_t *data = host->data_list[i];
1787 status = csnmp_read_table(host, data);
1789 status = csnmp_read_value(host, data);
1799 } /* int csnmp_read_host */
1801 static int csnmp_init(void) {
1802 call_snmp_init_once();
1805 } /* int csnmp_init */
1807 static int csnmp_shutdown(void) {
1808 data_definition_t *data_this;
1809 data_definition_t *data_next;
1811 /* When we get here, the read threads have been stopped and all the
1812 * `host_definition_t' will be freed. */
1813 DEBUG("snmp plugin: Destroying all data definitions.");
1815 data_this = data_head;
1817 while (data_this != NULL) {
1818 data_next = data_this->next;
1820 sfree(data_this->name);
1821 sfree(data_this->type);
1822 sfree(data_this->plugin_name);
1823 sfree(data_this->plugin_instance);
1824 sfree(data_this->type_instance);
1825 sfree(data_this->instance_prefix);
1826 sfree(data_this->values);
1827 sfree(data_this->ignores);
1830 data_this = data_next;
1834 } /* int csnmp_shutdown */
1836 void module_register(void) {
1837 plugin_register_complex_config("snmp", csnmp_config);
1838 plugin_register_init("snmp", csnmp_init);
1839 plugin_register_shutdown("snmp", csnmp_shutdown);
1840 } /* void module_register */