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;
48 char string[DATA_MAX_NAME_LEN];
51 typedef union instance_u instance_t;
53 struct data_definition_s {
54 char *name; /* used to reference this from the `Collect' option */
55 char *type; /* used to find the data_set */
58 char *instance_prefix;
63 struct data_definition_s *next;
68 typedef struct data_definition_s data_definition_t;
70 struct host_definition_s {
75 /* snmpv1/2 options */
78 /* snmpv3 security options */
81 size_t auth_protocol_len;
82 char *auth_passphrase;
84 size_t priv_protocol_len;
85 char *priv_passphrase;
90 c_complain_t complaint;
92 data_definition_t **data_list;
95 typedef struct host_definition_s host_definition_t;
97 /* These two types are used to cache values in `csnmp_read_table' to handle
99 struct csnmp_list_instances_s {
101 char instance[DATA_MAX_NAME_LEN];
102 struct csnmp_list_instances_s *next;
104 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
106 struct csnmp_table_values_s {
109 struct csnmp_table_values_s *next;
111 typedef struct csnmp_table_values_s csnmp_table_values_t;
116 static data_definition_t *data_head = NULL;
121 static int csnmp_read_host(user_data_t *ud);
126 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
127 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
128 memcpy(dst->oid, src, sizeof(*src) * n);
132 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
133 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
136 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
137 /* Make sure "src" is in "root"s subtree. */
138 if (src->oid_len <= root->oid_len)
140 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
141 /* n = */ root->oid_len) != 0)
144 memset(dst, 0, sizeof(*dst));
145 dst->oid_len = src->oid_len - root->oid_len;
146 memcpy(dst->oid, &src->oid[root->oid_len],
147 dst->oid_len * sizeof(dst->oid[0]));
151 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
153 char oid_str[MAX_OID_LEN][16];
154 char *oid_str_ptr[MAX_OID_LEN];
156 for (size_t i = 0; i < o->oid_len; i++) {
157 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
158 oid_str_ptr[i] = oid_str[i];
161 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
164 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
166 if (host->sess_handle == NULL)
169 snmp_sess_close(host->sess_handle);
170 host->sess_handle = NULL;
171 } /* }}} void csnmp_host_close_session */
173 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
175 host_definition_t *hd;
182 if (hd->name != NULL) {
183 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
186 csnmp_host_close_session(hd);
190 sfree(hd->community);
192 sfree(hd->auth_passphrase);
193 sfree(hd->priv_passphrase);
195 sfree(hd->data_list);
198 } /* }}} void csnmp_host_definition_destroy */
200 /* Many functions to handle the configuration. {{{ */
201 /* First there are many functions which do configuration stuff. It's a big
202 * bloated and messy, I'm afraid. */
205 * Callgraph for the config stuff:
207 * +-> call_snmp_init_once
208 * +-> csnmp_config_add_data
209 * ! +-> csnmp_config_add_data_instance
210 * ! +-> csnmp_config_add_data_instance_prefix
211 * ! +-> csnmp_config_add_data_values
212 * +-> csnmp_config_add_host
213 * +-> csnmp_config_add_host_version
214 * +-> csnmp_config_add_host_collect
215 * +-> csnmp_config_add_host_auth_protocol
216 * +-> csnmp_config_add_host_priv_protocol
217 * +-> csnmp_config_add_host_security_level
219 static void call_snmp_init_once(void) {
220 static int have_init = 0;
223 init_snmp(PACKAGE_NAME);
225 } /* void call_snmp_init_once */
227 static int csnmp_config_add_data_instance(data_definition_t *dd,
228 oconfig_item_t *ci) {
229 char buffer[DATA_MAX_NAME_LEN];
232 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
237 /* Instance is an OID */
238 dd->instance.oid.oid_len = MAX_OID_LEN;
240 if (!read_objid(buffer, dd->instance.oid.oid, &dd->instance.oid.oid_len)) {
241 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
245 /* Instance is a simple string */
246 sstrncpy(dd->instance.string, buffer, sizeof(dd->instance.string));
250 } /* int csnmp_config_add_data_instance */
252 static int csnmp_config_add_data_instance_prefix(data_definition_t *dd,
253 oconfig_item_t *ci) {
257 WARNING("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
258 "is set to `false'.",
263 status = cf_util_get_string(ci, &dd->instance_prefix);
265 } /* int csnmp_config_add_data_instance_prefix */
267 static int csnmp_config_add_data_values(data_definition_t *dd,
268 oconfig_item_t *ci) {
269 if (ci->values_num < 1) {
270 WARNING("snmp plugin: `Values' needs at least one argument.");
274 for (int i = 0; i < ci->values_num; i++)
275 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
276 WARNING("snmp plugin: `Values' needs only string argument.");
282 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
283 if (dd->values == NULL)
285 dd->values_len = (size_t)ci->values_num;
287 for (int i = 0; i < ci->values_num; i++) {
288 dd->values[i].oid_len = MAX_OID_LEN;
290 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
291 &dd->values[i].oid_len)) {
292 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
293 ci->values[i].value.string);
302 } /* int csnmp_config_add_data_instance */
304 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
305 oconfig_item_t *ci) {
306 if (ci->values_num < 1)
309 for (int i = 0; i < ci->values_num; i++) {
310 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
311 WARNING("snmp plugin: `Ignore' needs only string argument.");
319 for (int i = 0; i < ci->values_num; ++i) {
320 if (strarray_add(&(dd->ignores), &(dd->ignores_len),
321 ci->values[i].value.string) != 0) {
322 ERROR("snmp plugin: Can't allocate memory");
323 strarray_free(dd->ignores, dd->ignores_len);
328 } /* int csnmp_config_add_data_blacklist */
330 static int csnmp_config_add_data_blacklist_match_inverted(data_definition_t *dd,
331 oconfig_item_t *ci) {
332 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
333 WARNING("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
337 dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
340 } /* int csnmp_config_add_data_blacklist_match_inverted */
342 static int csnmp_config_add_data(oconfig_item_t *ci) {
343 data_definition_t *dd;
346 dd = calloc(1, sizeof(*dd));
350 status = cf_util_get_string(ci, &dd->name);
359 for (int i = 0; i < ci->children_num; i++) {
360 oconfig_item_t *option = ci->children + i;
362 if (strcasecmp("Type", option->key) == 0)
363 status = cf_util_get_string(option, &dd->type);
364 else if (strcasecmp("Table", option->key) == 0)
365 status = cf_util_get_boolean(option, &dd->is_table);
366 else if (strcasecmp("Instance", option->key) == 0)
367 status = csnmp_config_add_data_instance(dd, option);
368 else if (strcasecmp("InstancePrefix", option->key) == 0)
369 status = csnmp_config_add_data_instance_prefix(dd, option);
370 else if (strcasecmp("Values", option->key) == 0)
371 status = csnmp_config_add_data_values(dd, option);
372 else if (strcasecmp("Shift", option->key) == 0)
373 status = cf_util_get_double(option, &dd->shift);
374 else if (strcasecmp("Scale", option->key) == 0)
375 status = cf_util_get_double(option, &dd->scale);
376 else if (strcasecmp("Ignore", option->key) == 0)
377 status = csnmp_config_add_data_blacklist(dd, option);
378 else if (strcasecmp("InvertMatch", option->key) == 0)
379 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
381 WARNING("snmp plugin: Option `%s' not allowed here.", option->key);
387 } /* for (ci->children) */
389 while (status == 0) {
390 if (dd->type == NULL) {
391 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
395 if (dd->values == NULL) {
396 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
402 } /* while (status == 0) */
406 sfree(dd->instance_prefix);
413 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
415 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false",
418 if (data_head == NULL)
421 data_definition_t *last;
423 while (last->next != NULL)
429 } /* int csnmp_config_add_data */
431 static int csnmp_config_add_host_version(host_definition_t *hd,
432 oconfig_item_t *ci) {
435 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
436 WARNING("snmp plugin: The `Version' config option needs exactly one number "
441 version = (int)ci->values[0].value.number;
442 if ((version < 1) || (version > 3)) {
443 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
447 hd->version = version;
450 } /* int csnmp_config_add_host_address */
452 static int csnmp_config_add_host_collect(host_definition_t *host,
453 oconfig_item_t *ci) {
454 data_definition_t *data;
455 data_definition_t **data_list;
458 if (ci->values_num < 1) {
459 WARNING("snmp plugin: `Collect' needs at least one argument.");
463 for (int i = 0; i < ci->values_num; i++)
464 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
465 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
469 data_list_len = host->data_list_len + ci->values_num;
471 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
472 if (data_list == NULL)
474 host->data_list = data_list;
476 for (int i = 0; i < ci->values_num; i++) {
477 for (data = data_head; data != NULL; data = data->next)
478 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
482 WARNING("snmp plugin: No such data configured: `%s'",
483 ci->values[i].value.string);
487 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
488 host->data_list_len, data->name);
490 host->data_list[host->data_list_len] = data;
491 host->data_list_len++;
492 } /* for (values_num) */
495 } /* int csnmp_config_add_host_collect */
497 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
498 oconfig_item_t *ci) {
502 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
506 if (strcasecmp("MD5", buffer) == 0) {
507 hd->auth_protocol = usmHMACMD5AuthProtocol;
508 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
509 } else if (strcasecmp("SHA", buffer) == 0) {
510 hd->auth_protocol = usmHMACSHA1AuthProtocol;
511 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
513 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
518 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
519 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
522 } /* int csnmp_config_add_host_auth_protocol */
524 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
525 oconfig_item_t *ci) {
529 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
533 if (strcasecmp("AES", buffer) == 0) {
534 hd->priv_protocol = usmAESPrivProtocol;
535 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
536 } else if (strcasecmp("DES", buffer) == 0) {
537 hd->priv_protocol = usmDESPrivProtocol;
538 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
540 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
545 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
546 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
549 } /* int csnmp_config_add_host_priv_protocol */
551 static int csnmp_config_add_host_security_level(host_definition_t *hd,
552 oconfig_item_t *ci) {
556 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
560 if (strcasecmp("noAuthNoPriv", buffer) == 0)
561 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
562 else if (strcasecmp("authNoPriv", buffer) == 0)
563 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
564 else if (strcasecmp("authPriv", buffer) == 0)
565 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
567 WARNING("snmp plugin: The `SecurityLevel' config option must be "
568 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
572 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
576 } /* int csnmp_config_add_host_security_level */
578 static int csnmp_config_add_host(oconfig_item_t *ci) {
579 host_definition_t *hd;
582 /* Registration stuff. */
583 char cb_name[DATA_MAX_NAME_LEN];
585 hd = calloc(1, sizeof(*hd));
589 C_COMPLAIN_INIT(&hd->complaint);
591 status = cf_util_get_string(ci, &hd->name);
597 hd->sess_handle = NULL;
600 for (int i = 0; i < ci->children_num; i++) {
601 oconfig_item_t *option = ci->children + i;
604 if (strcasecmp("Address", option->key) == 0)
605 status = cf_util_get_string(option, &hd->address);
606 else if (strcasecmp("Community", option->key) == 0)
607 status = cf_util_get_string(option, &hd->community);
608 else if (strcasecmp("Version", option->key) == 0)
609 status = csnmp_config_add_host_version(hd, option);
610 else if (strcasecmp("Collect", option->key) == 0)
611 csnmp_config_add_host_collect(hd, option);
612 else if (strcasecmp("Interval", option->key) == 0)
613 cf_util_get_cdtime(option, &hd->interval);
614 else if (strcasecmp("Username", option->key) == 0)
615 status = cf_util_get_string(option, &hd->username);
616 else if (strcasecmp("AuthProtocol", option->key) == 0)
617 status = csnmp_config_add_host_auth_protocol(hd, option);
618 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
619 status = csnmp_config_add_host_priv_protocol(hd, option);
620 else if (strcasecmp("AuthPassphrase", option->key) == 0)
621 status = cf_util_get_string(option, &hd->auth_passphrase);
622 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
623 status = cf_util_get_string(option, &hd->priv_passphrase);
624 else if (strcasecmp("SecurityLevel", option->key) == 0)
625 status = csnmp_config_add_host_security_level(hd, option);
626 else if (strcasecmp("Context", option->key) == 0)
627 status = cf_util_get_string(option, &hd->context);
630 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
637 } /* for (ci->children) */
639 while (status == 0) {
640 if (hd->address == NULL) {
641 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
645 if (hd->community == NULL && hd->version < 3) {
646 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
650 if (hd->version == 3) {
651 if (hd->username == NULL) {
652 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
656 if (hd->security_level == 0) {
657 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
662 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
663 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
664 if (hd->auth_protocol == NULL) {
665 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
670 if (hd->auth_passphrase == NULL) {
671 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
677 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
678 if (hd->priv_protocol == NULL) {
679 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
684 if (hd->priv_passphrase == NULL) {
685 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
694 } /* while (status == 0) */
697 csnmp_host_definition_destroy(hd);
701 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
703 hd->name, hd->address, hd->community, hd->version);
705 snprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
707 status = plugin_register_complex_read(
708 /* group = */ NULL, cb_name, csnmp_read_host, hd->interval,
710 .data = hd, .free_func = csnmp_host_definition_destroy,
713 ERROR("snmp plugin: Registering complex read function failed.");
714 csnmp_host_definition_destroy(hd);
719 } /* int csnmp_config_add_host */
721 static int csnmp_config(oconfig_item_t *ci) {
722 call_snmp_init_once();
724 for (int i = 0; i < ci->children_num; i++) {
725 oconfig_item_t *child = ci->children + i;
726 if (strcasecmp("Data", child->key) == 0)
727 csnmp_config_add_data(child);
728 else if (strcasecmp("Host", child->key) == 0)
729 csnmp_config_add_host(child);
731 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
733 } /* for (ci->children) */
736 } /* int csnmp_config */
738 /* }}} End of the config stuff. Now the interesting part begins */
740 static void csnmp_host_open_session(host_definition_t *host) {
741 struct snmp_session sess;
744 if (host->sess_handle != NULL)
745 csnmp_host_close_session(host);
747 snmp_sess_init(&sess);
748 sess.peername = host->address;
749 switch (host->version) {
751 sess.version = SNMP_VERSION_1;
754 sess.version = SNMP_VERSION_3;
757 sess.version = SNMP_VERSION_2c;
761 if (host->version == 3) {
762 sess.securityName = host->username;
763 sess.securityNameLen = strlen(host->username);
764 sess.securityLevel = host->security_level;
766 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
767 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
768 sess.securityAuthProto = host->auth_protocol;
769 sess.securityAuthProtoLen = host->auth_protocol_len;
770 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
771 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
772 (u_char *)host->auth_passphrase,
773 strlen(host->auth_passphrase), sess.securityAuthKey,
774 &sess.securityAuthKeyLen);
775 if (error != SNMPERR_SUCCESS) {
776 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
782 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
783 sess.securityPrivProto = host->priv_protocol;
784 sess.securityPrivProtoLen = host->priv_protocol_len;
785 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
786 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
787 (u_char *)host->priv_passphrase,
788 strlen(host->priv_passphrase), sess.securityPrivKey,
789 &sess.securityPrivKeyLen);
790 if (error != SNMPERR_SUCCESS) {
791 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
797 if (host->context != NULL) {
798 sess.contextName = host->context;
799 sess.contextNameLen = strlen(host->context);
801 } else /* SNMPv1/2 "authenticates" with community string */
803 sess.community = (u_char *)host->community;
804 sess.community_len = strlen(host->community);
807 /* snmp_sess_open will copy the `struct snmp_session *'. */
808 host->sess_handle = snmp_sess_open(&sess);
810 if (host->sess_handle == NULL) {
813 snmp_error(&sess, NULL, NULL, &errstr);
815 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
816 (errstr == NULL) ? "Unknown problem" : errstr);
819 } /* void csnmp_host_open_session */
821 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
823 static value_t csnmp_value_list_to_value(struct variable_list *vl, int type,
824 double scale, double shift,
825 const char *host_name,
826 const char *data_name) {
828 uint64_t tmp_unsigned = 0;
829 int64_t tmp_signed = 0;
831 /* Set to true when the original SNMP type appears to have been signed. */
832 _Bool prefer_signed = 0;
834 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
835 (vl->type == ASN_COUNTER)
837 || (vl->type == ASN_TIMETICKS)
839 || (vl->type == ASN_GAUGE)) {
840 tmp_unsigned = (uint32_t)*vl->val.integer;
841 tmp_signed = (int32_t)*vl->val.integer;
843 if (vl->type == ASN_INTEGER)
846 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
847 } else if (vl->type == ASN_COUNTER64) {
848 tmp_unsigned = (uint32_t)vl->val.counter64->high;
849 tmp_unsigned = tmp_unsigned << 32;
850 tmp_unsigned += (uint32_t)vl->val.counter64->low;
851 tmp_signed = (int64_t)tmp_unsigned;
852 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
853 } else if (vl->type == ASN_OCTET_STR) {
854 /* We'll handle this later.. */
856 char oid_buffer[1024] = {0};
858 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
862 if (vl->type == ASN_NULL)
863 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
866 WARNING("snmp plugin: I don't know the ASN type #%i "
867 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
868 (int)vl->type, oid_buffer,
869 (data_name != NULL) ? data_name : "UNKNOWN",
870 (host_name != NULL) ? host_name : "UNKNOWN");
875 if (vl->type == ASN_OCTET_STR) {
878 if (vl->val.string != NULL) {
880 size_t string_length;
882 string_length = sizeof(string) - 1;
883 if (vl->val_len < string_length)
884 string_length = vl->val_len;
886 /* The strings we get from the Net-SNMP library may not be null
887 * terminated. That is why we're using `memcpy' here and not `strcpy'.
888 * `string_length' is set to `vl->val_len' which holds the length of the
890 memcpy(string, vl->val.string, string_length);
891 string[string_length] = 0;
893 status = parse_value(string, &ret, type);
895 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
897 (host_name != NULL) ? host_name : "UNKNOWN",
898 DS_TYPE_TO_STRING(type), string);
904 case DS_TYPE_COUNTER:
906 case DS_TYPE_ABSOLUTE:
907 memset(&ret, 0, sizeof(ret));
915 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
916 "data source type: %i.",
921 } /* if (vl->type == ASN_OCTET_STR) */
922 else if (type == DS_TYPE_COUNTER) {
923 ret.counter = tmp_unsigned;
924 } else if (type == DS_TYPE_GAUGE) {
927 else if (prefer_signed)
928 ret.gauge = (scale * tmp_signed) + shift;
930 ret.gauge = (scale * tmp_unsigned) + shift;
931 } else if (type == DS_TYPE_DERIVE) {
933 ret.derive = (derive_t)tmp_signed;
935 ret.derive = (derive_t)tmp_unsigned;
936 } else if (type == DS_TYPE_ABSOLUTE) {
937 ret.absolute = (absolute_t)tmp_unsigned;
939 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
946 } /* value_t csnmp_value_list_to_value */
948 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
949 * representation and writes it to dst. Returns zero on success and ENOMEM if
950 * dst is not large enough to hold the string. dst is guaranteed to be
952 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
953 const struct variable_list *vb,
961 buffer_free = dst_size;
963 for (size_t i = 0; i < vb->val_len; i++) {
966 status = snprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
967 (unsigned int)vb->val.bitstring[i]);
970 if (((size_t)status) >= buffer_free) /* truncated */
972 dst[dst_size - 1] = 0;
974 } else /* if (status < buffer_free) */
976 buffer_ptr += (size_t)status;
977 buffer_free -= (size_t)status;
982 } /* }}} int csnmp_strvbcopy_hexstring */
984 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
985 * dst. If non-printable characters are detected, it will switch to a hex
986 * representation of the string. Returns zero on success, EINVAL if vb does not
987 * contain a string and ENOMEM if dst is not large enough to contain the
989 static int csnmp_strvbcopy(char *dst, /* {{{ */
990 const struct variable_list *vb, size_t dst_size) {
994 if (vb->type == ASN_OCTET_STR)
995 src = (char *)vb->val.string;
996 else if (vb->type == ASN_BIT_STR)
997 src = (char *)vb->val.bitstring;
998 else if (vb->type == ASN_IPADDRESS) {
999 return snprintf(dst, dst_size,
1000 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1001 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1002 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1008 num_chars = dst_size - 1;
1009 if (num_chars > vb->val_len)
1010 num_chars = vb->val_len;
1012 for (size_t i = 0; i < num_chars; i++) {
1013 /* Check for control characters. */
1014 if ((unsigned char)src[i] < 32)
1015 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1019 dst[dst_size - 1] = 0;
1021 if (dst_size <= vb->val_len)
1025 } /* }}} int csnmp_strvbcopy */
1027 static int csnmp_instance_list_add(csnmp_list_instances_t **head,
1028 csnmp_list_instances_t **tail,
1029 const struct snmp_pdu *res,
1030 const host_definition_t *hd,
1031 const data_definition_t *dd) {
1032 csnmp_list_instances_t *il;
1033 struct variable_list *vb;
1036 uint32_t is_matched;
1038 /* Set vb on the last variable */
1039 for (vb = res->variables; (vb != NULL) && (vb->next_variable != NULL);
1040 vb = vb->next_variable)
1045 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1047 il = calloc(1, sizeof(*il));
1049 ERROR("snmp plugin: calloc failed.");
1054 status = csnmp_oid_suffix(&il->suffix, &vb_name, &dd->instance.oid);
1060 /* Get instance name */
1061 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1062 (vb->type == ASN_IPADDRESS)) {
1065 csnmp_strvbcopy(il->instance, vb, sizeof(il->instance));
1067 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1068 status = fnmatch(dd->ignores[i], il->instance, 0);
1070 if (dd->invert_match == 0) {
1079 if (dd->invert_match != 0 && is_matched == 0) {
1083 for (ptr = il->instance; *ptr != '\0'; ptr++) {
1084 if ((*ptr > 0) && (*ptr < 32))
1086 else if (*ptr == '/')
1089 DEBUG("snmp plugin: il->instance = `%s';", il->instance);
1091 value_t val = csnmp_value_list_to_value(
1092 vb, DS_TYPE_COUNTER,
1093 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1094 snprintf(il->instance, sizeof(il->instance), "%llu", val.counter);
1097 /* TODO: Debugging output */
1106 } /* int csnmp_instance_list_add */
1108 static int csnmp_dispatch_table(host_definition_t *host,
1109 data_definition_t *data,
1110 csnmp_list_instances_t *instance_list,
1111 csnmp_table_values_t **value_table) {
1112 const data_set_t *ds;
1113 value_list_t vl = VALUE_LIST_INIT;
1115 csnmp_list_instances_t *instance_list_ptr;
1116 csnmp_table_values_t *value_table_ptr[data->values_len];
1120 oid_t current_suffix;
1122 ds = plugin_get_ds(data->type);
1124 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1127 assert(ds->ds_num == data->values_len);
1128 assert(data->values_len > 0);
1130 instance_list_ptr = instance_list;
1132 for (i = 0; i < data->values_len; i++)
1133 value_table_ptr[i] = value_table[i];
1135 sstrncpy(vl.host, host->name, sizeof(vl.host));
1136 sstrncpy(vl.plugin, "snmp", sizeof(vl.plugin));
1138 vl.interval = host->interval;
1142 _Bool suffix_skipped = 0;
1144 /* Determine next suffix to handle. */
1145 if (instance_list != NULL) {
1146 if (instance_list_ptr == NULL) {
1151 memcpy(¤t_suffix, &instance_list_ptr->suffix,
1152 sizeof(current_suffix));
1154 /* no instance configured */
1155 csnmp_table_values_t *ptr = value_table_ptr[0];
1161 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1164 /* Update all the value_table_ptr to point at the entry with the same
1165 * trailing partial OID */
1166 for (i = 0; i < data->values_len; i++) {
1168 (value_table_ptr[i] != NULL) &&
1169 (csnmp_oid_compare(&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1170 value_table_ptr[i] = value_table_ptr[i]->next;
1172 if (value_table_ptr[i] == NULL) {
1175 } else if (csnmp_oid_compare(&value_table_ptr[i]->suffix,
1176 ¤t_suffix) > 0) {
1177 /* This suffix is missing in the subtree. Indicate this with the
1178 * "suffix_skipped" flag and try the next instance / suffix. */
1182 } /* for (i = 0; i < columns; i++) */
1187 /* Matching the values failed. Start from the beginning again. */
1188 if (suffix_skipped) {
1189 if (instance_list != NULL)
1190 instance_list_ptr = instance_list_ptr->next;
1192 value_table_ptr[0] = value_table_ptr[0]->next;
1197 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1198 * to the same subid. instance_list_ptr is either NULL or points to the
1199 * same subid, too. */
1201 for (i = 1; i < data->values_len; i++) {
1202 assert(value_table_ptr[i] != NULL);
1203 assert(csnmp_oid_compare(&value_table_ptr[i - 1]->suffix,
1204 &value_table_ptr[i]->suffix) == 0);
1206 assert((instance_list_ptr == NULL) ||
1207 (csnmp_oid_compare(&instance_list_ptr->suffix,
1208 &value_table_ptr[0]->suffix) == 0));
1211 sstrncpy(vl.type, data->type, sizeof(vl.type));
1214 char temp[DATA_MAX_NAME_LEN];
1216 if (instance_list_ptr == NULL)
1217 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1219 sstrncpy(temp, instance_list_ptr->instance, sizeof(temp));
1221 if (data->instance_prefix == NULL)
1222 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1224 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1225 data->instance_prefix, temp);
1228 vl.values_len = data->values_len;
1229 value_t values[vl.values_len];
1232 for (i = 0; i < data->values_len; i++)
1233 vl.values[i] = value_table_ptr[i]->value;
1235 /* If we get here `vl.type_instance' and all `vl.values' have been set
1236 * vl.type_instance can be empty, i.e. a blank port description on a
1237 * switch if you're using IF-MIB::ifDescr as Instance.
1239 if (vl.type_instance[0] != '\0')
1240 plugin_dispatch_values(&vl);
1242 /* prevent leakage of pointer to local variable. */
1246 if (instance_list != NULL)
1247 instance_list_ptr = instance_list_ptr->next;
1249 value_table_ptr[0] = value_table_ptr[0]->next;
1250 } /* while (have_more) */
1253 } /* int csnmp_dispatch_table */
1255 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1256 struct snmp_pdu *req;
1257 struct snmp_pdu *res = NULL;
1258 struct variable_list *vb;
1260 const data_set_t *ds;
1262 size_t oid_list_len = data->values_len + 1;
1263 /* Holds the last OID returned by the device. We use this in the GETNEXT
1264 * request to proceed. */
1265 oid_t oid_list[oid_list_len];
1266 /* Set to false when an OID has left its subtree so we don't re-request it
1268 _Bool oid_list_todo[oid_list_len];
1273 /* `value_list_head' and `value_list_tail' implement a linked list for each
1274 * value. `instance_list_head' and `instance_list_tail' implement a linked
1276 * instance names. This is used to jump gaps in the table. */
1277 csnmp_list_instances_t *instance_list_head;
1278 csnmp_list_instances_t *instance_list_tail;
1279 csnmp_table_values_t **value_list_head;
1280 csnmp_table_values_t **value_list_tail;
1282 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1285 if (host->sess_handle == NULL) {
1286 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1290 ds = plugin_get_ds(data->type);
1292 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1296 if (ds->ds_num != data->values_len) {
1297 ERROR("snmp plugin: DataSet `%s' requires %zu values, but config talks "
1299 data->type, ds->ds_num, data->values_len);
1302 assert(data->values_len > 0);
1304 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1305 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1306 if (data->instance.oid.oid_len > 0)
1307 memcpy(oid_list + data->values_len, &data->instance.oid, sizeof(oid_t));
1308 else /* no InstanceFrom option specified. */
1311 for (i = 0; i < oid_list_len; i++)
1312 oid_list_todo[i] = 1;
1314 /* We're going to construct n linked lists, one for each "value".
1315 * value_list_head will contain pointers to the heads of these linked lists,
1316 * value_list_tail will contain pointers to the tail of the lists. */
1317 value_list_head = calloc(data->values_len, sizeof(*value_list_head));
1318 value_list_tail = calloc(data->values_len, sizeof(*value_list_tail));
1319 if ((value_list_head == NULL) || (value_list_tail == NULL)) {
1320 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1321 sfree(value_list_head);
1322 sfree(value_list_tail);
1326 instance_list_head = NULL;
1327 instance_list_tail = NULL;
1330 while (status == 0) {
1331 int oid_list_todo_num;
1333 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1335 ERROR("snmp plugin: snmp_pdu_create failed.");
1340 oid_list_todo_num = 0;
1341 for (i = 0; i < oid_list_len; i++) {
1342 /* Do not rerequest already finished OIDs */
1343 if (!oid_list_todo[i])
1345 oid_list_todo_num++;
1346 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1349 if (oid_list_todo_num == 0) {
1350 /* The request is still empty - so we are finished */
1351 DEBUG("snmp plugin: all variables have left their subtree");
1357 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1358 if ((status != STAT_SUCCESS) || (res == NULL)) {
1359 char *errstr = NULL;
1361 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1363 c_complain(LOG_ERR, &host->complaint,
1364 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1365 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1371 /* snmp_synch_response already freed our PDU */
1374 csnmp_host_close_session(host);
1381 assert(res != NULL);
1382 c_release(LOG_INFO, &host->complaint,
1383 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1386 vb = res->variables;
1392 for (vb = res->variables, i = 0; (vb != NULL);
1393 vb = vb->next_variable, i++) {
1394 /* Calculate value index from todo list */
1395 while ((i < oid_list_len) && !oid_list_todo[i])
1398 /* An instance is configured and the res variable we process is the
1399 * instance value (last index) */
1400 if ((data->instance.oid.oid_len > 0) && (i == data->values_len)) {
1401 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1403 data->instance.oid.oid, data->instance.oid.oid_len, vb->name,
1404 vb->name_length, data->instance.oid.oid_len) != 0)) {
1405 DEBUG("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1406 host->name, data->name);
1407 oid_list_todo[i] = 0;
1411 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1412 * add it to the list */
1413 if (csnmp_instance_list_add(&instance_list_head, &instance_list_tail,
1414 res, host, data) != 0) {
1415 ERROR("snmp plugin: host %s: csnmp_instance_list_add failed.",
1420 } else /* The variable we are processing is a normal value */
1422 csnmp_table_values_t *vt;
1427 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1429 /* Calculate the current suffix. This is later used to check that the
1430 * suffix is increasing. This also checks if we left the subtree */
1431 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1433 DEBUG("snmp plugin: host = %s; data = %s; i = %zu; "
1434 "Value probably left its subtree.",
1435 host->name, data->name, i);
1436 oid_list_todo[i] = 0;
1440 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1442 * table matching algorithm will get confused. */
1443 if ((value_list_tail[i] != NULL) &&
1444 (csnmp_oid_compare(&suffix, &value_list_tail[i]->suffix) <= 0)) {
1445 DEBUG("snmp plugin: host = %s; data = %s; i = %zu; "
1446 "Suffix is not increasing.",
1447 host->name, data->name, i);
1448 oid_list_todo[i] = 0;
1452 vt = calloc(1, sizeof(*vt));
1454 ERROR("snmp plugin: calloc failed.");
1460 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1461 data->shift, host->name, data->name);
1462 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1465 if (value_list_tail[i] == NULL)
1466 value_list_head[i] = vt;
1468 value_list_tail[i]->next = vt;
1469 value_list_tail[i] = vt;
1472 /* Copy OID to oid_list[i] */
1473 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1474 oid_list[i].oid_len = vb->name_length;
1476 } /* for (vb = res->variables ...) */
1481 } /* while (status == 0) */
1492 csnmp_dispatch_table(host, data, instance_list_head, value_list_head);
1494 /* Free all allocated variables here */
1495 while (instance_list_head != NULL) {
1496 csnmp_list_instances_t *next = instance_list_head->next;
1497 sfree(instance_list_head);
1498 instance_list_head = next;
1501 for (i = 0; i < data->values_len; i++) {
1502 while (value_list_head[i] != NULL) {
1503 csnmp_table_values_t *next = value_list_head[i]->next;
1504 sfree(value_list_head[i]);
1505 value_list_head[i] = next;
1509 sfree(value_list_head);
1510 sfree(value_list_tail);
1513 } /* int csnmp_read_table */
1515 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1516 struct snmp_pdu *req;
1517 struct snmp_pdu *res = NULL;
1518 struct variable_list *vb;
1520 const data_set_t *ds;
1521 value_list_t vl = VALUE_LIST_INIT;
1526 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
1529 if (host->sess_handle == NULL) {
1530 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1534 ds = plugin_get_ds(data->type);
1536 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1540 if (ds->ds_num != data->values_len) {
1541 ERROR("snmp plugin: DataSet `%s' requires %zu values, but config talks "
1543 data->type, ds->ds_num, data->values_len);
1547 vl.values_len = ds->ds_num;
1548 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
1549 if (vl.values == NULL)
1551 for (i = 0; i < vl.values_len; i++) {
1552 if (ds->ds[i].type == DS_TYPE_COUNTER)
1553 vl.values[i].counter = 0;
1555 vl.values[i].gauge = NAN;
1558 sstrncpy(vl.host, host->name, sizeof(vl.host));
1559 sstrncpy(vl.plugin, "snmp", sizeof(vl.plugin));
1560 sstrncpy(vl.type, data->type, sizeof(vl.type));
1561 sstrncpy(vl.type_instance, data->instance.string, sizeof(vl.type_instance));
1563 vl.interval = host->interval;
1565 req = snmp_pdu_create(SNMP_MSG_GET);
1567 ERROR("snmp plugin: snmp_pdu_create failed.");
1572 for (i = 0; i < data->values_len; i++)
1573 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
1575 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1577 if ((status != STAT_SUCCESS) || (res == NULL)) {
1578 char *errstr = NULL;
1580 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1581 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1582 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1589 csnmp_host_close_session(host);
1594 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
1597 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
1598 DEBUG("snmp plugin: Got this variable: %s", buffer);
1599 #endif /* COLLECT_DEBUG */
1601 for (i = 0; i < data->values_len; i++)
1602 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
1603 vb->name, vb->name_length) == 0)
1605 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1606 data->shift, host->name, data->name);
1607 } /* for (res->variables) */
1611 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
1612 plugin_dispatch_values(&vl);
1616 } /* int csnmp_read_value */
1618 static int csnmp_read_host(user_data_t *ud) {
1619 host_definition_t *host;
1626 if (host->interval == 0)
1627 host->interval = plugin_get_interval();
1629 if (host->sess_handle == NULL)
1630 csnmp_host_open_session(host);
1632 if (host->sess_handle == NULL)
1636 for (i = 0; i < host->data_list_len; i++) {
1637 data_definition_t *data = host->data_list[i];
1640 status = csnmp_read_table(host, data);
1642 status = csnmp_read_value(host, data);
1652 } /* int csnmp_read_host */
1654 static int csnmp_init(void) {
1655 call_snmp_init_once();
1658 } /* int csnmp_init */
1660 static int csnmp_shutdown(void) {
1661 data_definition_t *data_this;
1662 data_definition_t *data_next;
1664 /* When we get here, the read threads have been stopped and all the
1665 * `host_definition_t' will be freed. */
1666 DEBUG("snmp plugin: Destroying all data definitions.");
1668 data_this = data_head;
1670 while (data_this != NULL) {
1671 data_next = data_this->next;
1673 sfree(data_this->name);
1674 sfree(data_this->type);
1675 sfree(data_this->values);
1676 sfree(data_this->ignores);
1679 data_this = data_next;
1683 } /* int csnmp_shutdown */
1685 void module_register(void) {
1686 plugin_register_complex_config("snmp", csnmp_config);
1687 plugin_register_init("snmp", csnmp_init);
1688 plugin_register_shutdown("snmp", csnmp_shutdown);
1689 } /* void module_register */