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 {
77 /* snmpv1/2 options */
80 /* snmpv3 security options */
83 size_t auth_protocol_len;
84 char *auth_passphrase;
86 size_t priv_protocol_len;
87 char *priv_passphrase;
92 c_complain_t complaint;
94 data_definition_t **data_list;
97 typedef struct host_definition_s host_definition_t;
99 /* These two types are used to cache values in `csnmp_read_table' to handle
101 struct csnmp_list_instances_s {
103 char instance[DATA_MAX_NAME_LEN];
104 struct csnmp_list_instances_s *next;
106 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
108 struct csnmp_table_values_s {
111 struct csnmp_table_values_s *next;
113 typedef struct csnmp_table_values_s csnmp_table_values_t;
118 static data_definition_t *data_head = NULL;
123 static int csnmp_read_host(user_data_t *ud);
128 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
129 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
130 memcpy(dst->oid, src, sizeof(*src) * n);
134 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
135 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
138 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
139 /* Make sure "src" is in "root"s subtree. */
140 if (src->oid_len <= root->oid_len)
142 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
143 /* n = */ root->oid_len) != 0)
146 memset(dst, 0, sizeof(*dst));
147 dst->oid_len = src->oid_len - root->oid_len;
148 memcpy(dst->oid, &src->oid[root->oid_len],
149 dst->oid_len * sizeof(dst->oid[0]));
153 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
155 char oid_str[MAX_OID_LEN][16];
156 char *oid_str_ptr[MAX_OID_LEN];
158 for (size_t i = 0; i < o->oid_len; i++) {
159 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
160 oid_str_ptr[i] = oid_str[i];
163 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
166 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
168 if (host->sess_handle == NULL)
171 snmp_sess_close(host->sess_handle);
172 host->sess_handle = NULL;
173 } /* }}} void csnmp_host_close_session */
175 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
177 host_definition_t *hd;
184 if (hd->name != NULL) {
185 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
188 csnmp_host_close_session(hd);
192 sfree(hd->community);
194 sfree(hd->auth_passphrase);
195 sfree(hd->priv_passphrase);
197 sfree(hd->data_list);
200 } /* }}} void csnmp_host_definition_destroy */
202 /* Many functions to handle the configuration. {{{ */
203 /* First there are many functions which do configuration stuff. It's a big
204 * bloated and messy, I'm afraid. */
207 * Callgraph for the config stuff:
209 * +-> call_snmp_init_once
210 * +-> csnmp_config_add_data
211 * ! +-> csnmp_config_add_data_instance
212 * ! +-> csnmp_config_add_data_instance_prefix
213 * ! +-> csnmp_config_add_data_values
214 * +-> csnmp_config_add_host
215 * +-> csnmp_config_add_host_version
216 * +-> csnmp_config_add_host_collect
217 * +-> csnmp_config_add_host_auth_protocol
218 * +-> csnmp_config_add_host_priv_protocol
219 * +-> csnmp_config_add_host_security_level
221 static void call_snmp_init_once(void) {
222 static int have_init = 0;
225 init_snmp(PACKAGE_NAME);
227 } /* void call_snmp_init_once */
229 static int csnmp_config_add_data_instance(data_definition_t *dd,
230 oconfig_item_t *ci) {
231 char buffer[DATA_MAX_NAME_LEN];
234 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
239 /* Instance is an OID */
240 dd->instance.oid.oid_len = MAX_OID_LEN;
242 if (!read_objid(buffer, dd->instance.oid.oid, &dd->instance.oid.oid_len)) {
243 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
247 /* Instance is a simple string */
248 sstrncpy(dd->instance.string, buffer, sizeof(dd->instance.string));
252 } /* int csnmp_config_add_data_instance */
254 static int csnmp_config_add_data_instance_prefix(data_definition_t *dd,
255 oconfig_item_t *ci) {
259 WARNING("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
260 "is set to `false'.",
265 status = cf_util_get_string(ci, &dd->instance_prefix);
267 } /* int csnmp_config_add_data_instance_prefix */
269 static int csnmp_config_add_data_values(data_definition_t *dd,
270 oconfig_item_t *ci) {
271 if (ci->values_num < 1) {
272 WARNING("snmp plugin: `Values' needs at least one argument.");
276 for (int i = 0; i < ci->values_num; i++)
277 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
278 WARNING("snmp plugin: `Values' needs only string argument.");
284 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
285 if (dd->values == NULL)
287 dd->values_len = (size_t)ci->values_num;
289 for (int i = 0; i < ci->values_num; i++) {
290 dd->values[i].oid_len = MAX_OID_LEN;
292 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
293 &dd->values[i].oid_len)) {
294 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
295 ci->values[i].value.string);
304 } /* int csnmp_config_add_data_instance */
306 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
307 oconfig_item_t *ci) {
308 if (ci->values_num < 1)
311 for (int i = 0; i < ci->values_num; i++) {
312 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
313 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_blacklist_match_inverted(data_definition_t *dd,
333 oconfig_item_t *ci) {
334 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
335 WARNING("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
339 dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
342 } /* int csnmp_config_add_data_blacklist_match_inverted */
344 static int csnmp_config_add_data(oconfig_item_t *ci) {
345 data_definition_t *dd;
348 dd = calloc(1, sizeof(*dd));
352 status = cf_util_get_string(ci, &dd->name);
361 for (int i = 0; i < ci->children_num; i++) {
362 oconfig_item_t *option = ci->children + i;
364 if (strcasecmp("Type", option->key) == 0)
365 status = cf_util_get_string(option, &dd->type);
366 else if (strcasecmp("Table", option->key) == 0)
367 status = cf_util_get_boolean(option, &dd->is_table);
368 else if (strcasecmp("Instance", option->key) == 0)
369 status = csnmp_config_add_data_instance(dd, option);
370 else if (strcasecmp("InstancePrefix", option->key) == 0)
371 status = csnmp_config_add_data_instance_prefix(dd, option);
372 else if (strcasecmp("Values", option->key) == 0)
373 status = csnmp_config_add_data_values(dd, option);
374 else if (strcasecmp("Shift", option->key) == 0)
375 status = cf_util_get_double(option, &dd->shift);
376 else if (strcasecmp("Scale", option->key) == 0)
377 status = cf_util_get_double(option, &dd->scale);
378 else if (strcasecmp("Ignore", option->key) == 0)
379 status = csnmp_config_add_data_blacklist(dd, option);
380 else if (strcasecmp("InvertMatch", option->key) == 0)
381 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
383 WARNING("snmp plugin: Option `%s' not allowed here.", option->key);
389 } /* for (ci->children) */
391 while (status == 0) {
392 if (dd->type == NULL) {
393 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
397 if (dd->values == NULL) {
398 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
404 } /* while (status == 0) */
408 sfree(dd->instance_prefix);
415 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
417 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false",
420 if (data_head == NULL)
423 data_definition_t *last;
425 while (last->next != NULL)
431 } /* int csnmp_config_add_data */
433 static int csnmp_config_add_host_version(host_definition_t *hd,
434 oconfig_item_t *ci) {
437 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
438 WARNING("snmp plugin: The `Version' config option needs exactly one number "
443 version = (int)ci->values[0].value.number;
444 if ((version < 1) || (version > 3)) {
445 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
449 hd->version = version;
452 } /* int csnmp_config_add_host_address */
454 static int csnmp_config_add_host_collect(host_definition_t *host,
455 oconfig_item_t *ci) {
456 data_definition_t *data;
457 data_definition_t **data_list;
460 if (ci->values_num < 1) {
461 WARNING("snmp plugin: `Collect' needs at least one argument.");
465 for (int i = 0; i < ci->values_num; i++)
466 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
467 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
471 data_list_len = host->data_list_len + ci->values_num;
473 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
474 if (data_list == NULL)
476 host->data_list = data_list;
478 for (int i = 0; i < ci->values_num; i++) {
479 for (data = data_head; data != NULL; data = data->next)
480 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
484 WARNING("snmp plugin: No such data configured: `%s'",
485 ci->values[i].value.string);
489 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
490 host->data_list_len, data->name);
492 host->data_list[host->data_list_len] = data;
493 host->data_list_len++;
494 } /* for (values_num) */
497 } /* int csnmp_config_add_host_collect */
499 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
500 oconfig_item_t *ci) {
504 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
508 if (strcasecmp("MD5", buffer) == 0) {
509 hd->auth_protocol = usmHMACMD5AuthProtocol;
510 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
511 } else if (strcasecmp("SHA", buffer) == 0) {
512 hd->auth_protocol = usmHMACSHA1AuthProtocol;
513 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
515 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
520 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
521 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
524 } /* int csnmp_config_add_host_auth_protocol */
526 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
527 oconfig_item_t *ci) {
531 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
535 if (strcasecmp("AES", buffer) == 0) {
536 hd->priv_protocol = usmAESPrivProtocol;
537 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
538 } else if (strcasecmp("DES", buffer) == 0) {
539 hd->priv_protocol = usmDESPrivProtocol;
540 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
542 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
547 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
548 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
551 } /* int csnmp_config_add_host_priv_protocol */
553 static int csnmp_config_add_host_security_level(host_definition_t *hd,
554 oconfig_item_t *ci) {
558 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
562 if (strcasecmp("noAuthNoPriv", buffer) == 0)
563 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
564 else if (strcasecmp("authNoPriv", buffer) == 0)
565 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
566 else if (strcasecmp("authPriv", buffer) == 0)
567 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
569 WARNING("snmp plugin: The `SecurityLevel' config option must be "
570 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
574 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
578 } /* int csnmp_config_add_host_security_level */
580 static int csnmp_config_add_host(oconfig_item_t *ci) {
581 host_definition_t *hd;
584 /* Registration stuff. */
585 char cb_name[DATA_MAX_NAME_LEN];
587 hd = calloc(1, sizeof(*hd));
591 C_COMPLAIN_INIT(&hd->complaint);
593 status = cf_util_get_string(ci, &hd->name);
599 hd->sess_handle = NULL;
602 /* These mean that we have not set a timeout or retry value */
606 for (int i = 0; i < ci->children_num; i++) {
607 oconfig_item_t *option = ci->children + i;
610 if (strcasecmp("Address", option->key) == 0)
611 status = cf_util_get_string(option, &hd->address);
612 else if (strcasecmp("Community", option->key) == 0)
613 status = cf_util_get_string(option, &hd->community);
614 else if (strcasecmp("Version", option->key) == 0)
615 status = csnmp_config_add_host_version(hd, option);
616 else if (strcasecmp("Timeout", option->key) == 0)
617 cf_util_get_cdtime(option, &hd->timeout);
618 else if (strcasecmp("Retries", option->key) == 0)
619 cf_util_get_int(option, &hd->retries);
620 else if (strcasecmp("Collect", option->key) == 0)
621 csnmp_config_add_host_collect(hd, option);
622 else if (strcasecmp("Interval", option->key) == 0)
623 cf_util_get_cdtime(option, &hd->interval);
624 else if (strcasecmp("Username", option->key) == 0)
625 status = cf_util_get_string(option, &hd->username);
626 else if (strcasecmp("AuthProtocol", option->key) == 0)
627 status = csnmp_config_add_host_auth_protocol(hd, option);
628 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
629 status = csnmp_config_add_host_priv_protocol(hd, option);
630 else if (strcasecmp("AuthPassphrase", option->key) == 0)
631 status = cf_util_get_string(option, &hd->auth_passphrase);
632 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
633 status = cf_util_get_string(option, &hd->priv_passphrase);
634 else if (strcasecmp("SecurityLevel", option->key) == 0)
635 status = csnmp_config_add_host_security_level(hd, option);
636 else if (strcasecmp("Context", option->key) == 0)
637 status = cf_util_get_string(option, &hd->context);
640 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
647 } /* for (ci->children) */
649 while (status == 0) {
650 if (hd->address == NULL) {
651 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
655 if (hd->community == NULL && hd->version < 3) {
656 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
660 if (hd->version == 3) {
661 if (hd->username == NULL) {
662 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
666 if (hd->security_level == 0) {
667 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
672 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
673 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
674 if (hd->auth_protocol == NULL) {
675 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
680 if (hd->auth_passphrase == NULL) {
681 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
687 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
688 if (hd->priv_protocol == NULL) {
689 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
694 if (hd->priv_passphrase == NULL) {
695 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
704 } /* while (status == 0) */
707 csnmp_host_definition_destroy(hd);
711 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
713 hd->name, hd->address, hd->community, hd->version);
715 snprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
717 status = plugin_register_complex_read(
718 /* group = */ NULL, cb_name, csnmp_read_host, hd->interval,
720 .data = hd, .free_func = csnmp_host_definition_destroy,
723 ERROR("snmp plugin: Registering complex read function failed.");
728 } /* int csnmp_config_add_host */
730 static int csnmp_config(oconfig_item_t *ci) {
731 call_snmp_init_once();
733 for (int i = 0; i < ci->children_num; i++) {
734 oconfig_item_t *child = ci->children + i;
735 if (strcasecmp("Data", child->key) == 0)
736 csnmp_config_add_data(child);
737 else if (strcasecmp("Host", child->key) == 0)
738 csnmp_config_add_host(child);
740 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
742 } /* for (ci->children) */
745 } /* int csnmp_config */
747 /* }}} End of the config stuff. Now the interesting part begins */
749 static void csnmp_host_open_session(host_definition_t *host) {
750 struct snmp_session sess;
753 if (host->sess_handle != NULL)
754 csnmp_host_close_session(host);
756 snmp_sess_init(&sess);
757 sess.peername = host->address;
758 switch (host->version) {
760 sess.version = SNMP_VERSION_1;
763 sess.version = SNMP_VERSION_3;
766 sess.version = SNMP_VERSION_2c;
770 if (host->version == 3) {
771 sess.securityName = host->username;
772 sess.securityNameLen = strlen(host->username);
773 sess.securityLevel = host->security_level;
775 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
776 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
777 sess.securityAuthProto = host->auth_protocol;
778 sess.securityAuthProtoLen = host->auth_protocol_len;
779 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
780 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
781 (u_char *)host->auth_passphrase,
782 strlen(host->auth_passphrase), sess.securityAuthKey,
783 &sess.securityAuthKeyLen);
784 if (error != SNMPERR_SUCCESS) {
785 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
791 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
792 sess.securityPrivProto = host->priv_protocol;
793 sess.securityPrivProtoLen = host->priv_protocol_len;
794 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
795 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
796 (u_char *)host->priv_passphrase,
797 strlen(host->priv_passphrase), sess.securityPrivKey,
798 &sess.securityPrivKeyLen);
799 if (error != SNMPERR_SUCCESS) {
800 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
806 if (host->context != NULL) {
807 sess.contextName = host->context;
808 sess.contextNameLen = strlen(host->context);
810 } else /* SNMPv1/2 "authenticates" with community string */
812 sess.community = (u_char *)host->community;
813 sess.community_len = strlen(host->community);
816 /* Set timeout & retries, if they have been changed from the default */
817 if (host->timeout != 0) {
818 /* net-snmp expects microseconds */
819 sess.timeout = CDTIME_T_TO_US(host->timeout);
821 if (host->retries >= 0) {
822 sess.retries = host->retries;
825 /* snmp_sess_open will copy the `struct snmp_session *'. */
826 host->sess_handle = snmp_sess_open(&sess);
828 if (host->sess_handle == NULL) {
831 snmp_error(&sess, NULL, NULL, &errstr);
833 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
834 (errstr == NULL) ? "Unknown problem" : errstr);
837 } /* void csnmp_host_open_session */
839 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
841 static value_t csnmp_value_list_to_value(struct variable_list *vl, int type,
842 double scale, double shift,
843 const char *host_name,
844 const char *data_name) {
846 uint64_t tmp_unsigned = 0;
847 int64_t tmp_signed = 0;
849 /* Set to true when the original SNMP type appears to have been signed. */
850 _Bool prefer_signed = 0;
852 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
853 (vl->type == ASN_COUNTER)
855 || (vl->type == ASN_TIMETICKS)
857 || (vl->type == ASN_GAUGE)) {
858 tmp_unsigned = (uint32_t)*vl->val.integer;
859 tmp_signed = (int32_t)*vl->val.integer;
861 if (vl->type == ASN_INTEGER)
864 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
865 } else if (vl->type == ASN_COUNTER64) {
866 tmp_unsigned = (uint32_t)vl->val.counter64->high;
867 tmp_unsigned = tmp_unsigned << 32;
868 tmp_unsigned += (uint32_t)vl->val.counter64->low;
869 tmp_signed = (int64_t)tmp_unsigned;
870 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
871 } else if (vl->type == ASN_OCTET_STR) {
872 /* We'll handle this later.. */
874 char oid_buffer[1024] = {0};
876 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
880 if (vl->type == ASN_NULL)
881 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
884 WARNING("snmp plugin: I don't know the ASN type #%i "
885 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
886 (int)vl->type, oid_buffer,
887 (data_name != NULL) ? data_name : "UNKNOWN",
888 (host_name != NULL) ? host_name : "UNKNOWN");
893 if (vl->type == ASN_OCTET_STR) {
896 if (vl->val.string != NULL) {
898 size_t string_length;
900 string_length = sizeof(string) - 1;
901 if (vl->val_len < string_length)
902 string_length = vl->val_len;
904 /* The strings we get from the Net-SNMP library may not be null
905 * terminated. That is why we're using `memcpy' here and not `strcpy'.
906 * `string_length' is set to `vl->val_len' which holds the length of the
908 memcpy(string, vl->val.string, string_length);
909 string[string_length] = 0;
911 status = parse_value(string, &ret, type);
913 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
915 (host_name != NULL) ? host_name : "UNKNOWN",
916 DS_TYPE_TO_STRING(type), string);
922 case DS_TYPE_COUNTER:
924 case DS_TYPE_ABSOLUTE:
925 memset(&ret, 0, sizeof(ret));
933 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
934 "data source type: %i.",
939 } /* if (vl->type == ASN_OCTET_STR) */
940 else if (type == DS_TYPE_COUNTER) {
941 ret.counter = tmp_unsigned;
942 } else if (type == DS_TYPE_GAUGE) {
945 else if (prefer_signed)
946 ret.gauge = (scale * tmp_signed) + shift;
948 ret.gauge = (scale * tmp_unsigned) + shift;
949 } else if (type == DS_TYPE_DERIVE) {
951 ret.derive = (derive_t)tmp_signed;
953 ret.derive = (derive_t)tmp_unsigned;
954 } else if (type == DS_TYPE_ABSOLUTE) {
955 ret.absolute = (absolute_t)tmp_unsigned;
957 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
964 } /* value_t csnmp_value_list_to_value */
966 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
967 * representation and writes it to dst. Returns zero on success and ENOMEM if
968 * dst is not large enough to hold the string. dst is guaranteed to be
970 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
971 const struct variable_list *vb,
979 buffer_free = dst_size;
981 for (size_t i = 0; i < vb->val_len; i++) {
984 status = snprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
985 (unsigned int)vb->val.bitstring[i]);
988 if (((size_t)status) >= buffer_free) /* truncated */
990 dst[dst_size - 1] = 0;
992 } else /* if (status < buffer_free) */
994 buffer_ptr += (size_t)status;
995 buffer_free -= (size_t)status;
1000 } /* }}} int csnmp_strvbcopy_hexstring */
1002 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1003 * dst. If non-printable characters are detected, it will switch to a hex
1004 * representation of the string. Returns zero on success, EINVAL if vb does not
1005 * contain a string and ENOMEM if dst is not large enough to contain the
1007 static int csnmp_strvbcopy(char *dst, /* {{{ */
1008 const struct variable_list *vb, size_t dst_size) {
1012 if (vb->type == ASN_OCTET_STR)
1013 src = (char *)vb->val.string;
1014 else if (vb->type == ASN_BIT_STR)
1015 src = (char *)vb->val.bitstring;
1016 else if (vb->type == ASN_IPADDRESS) {
1017 return snprintf(dst, dst_size,
1018 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1019 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1020 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1026 num_chars = dst_size - 1;
1027 if (num_chars > vb->val_len)
1028 num_chars = vb->val_len;
1030 for (size_t i = 0; i < num_chars; i++) {
1031 /* Check for control characters. */
1032 if ((unsigned char)src[i] < 32)
1033 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1037 dst[dst_size - 1] = 0;
1039 if (dst_size <= vb->val_len)
1043 } /* }}} int csnmp_strvbcopy */
1045 static int csnmp_instance_list_add(csnmp_list_instances_t **head,
1046 csnmp_list_instances_t **tail,
1047 const struct snmp_pdu *res,
1048 const host_definition_t *hd,
1049 const data_definition_t *dd) {
1050 csnmp_list_instances_t *il;
1051 struct variable_list *vb;
1054 uint32_t is_matched;
1056 /* Set vb on the last variable */
1057 for (vb = res->variables; (vb != NULL) && (vb->next_variable != NULL);
1058 vb = vb->next_variable)
1063 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1065 il = calloc(1, sizeof(*il));
1067 ERROR("snmp plugin: calloc failed.");
1072 status = csnmp_oid_suffix(&il->suffix, &vb_name, &dd->instance.oid);
1078 /* Get instance name */
1079 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1080 (vb->type == ASN_IPADDRESS)) {
1083 csnmp_strvbcopy(il->instance, vb, sizeof(il->instance));
1085 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1086 status = fnmatch(dd->ignores[i], il->instance, 0);
1088 if (dd->invert_match == 0) {
1097 if (dd->invert_match != 0 && is_matched == 0) {
1101 for (ptr = il->instance; *ptr != '\0'; ptr++) {
1102 if ((*ptr > 0) && (*ptr < 32))
1104 else if (*ptr == '/')
1107 DEBUG("snmp plugin: il->instance = `%s';", il->instance);
1109 value_t val = csnmp_value_list_to_value(
1110 vb, DS_TYPE_COUNTER,
1111 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1112 snprintf(il->instance, sizeof(il->instance), "%llu", val.counter);
1115 /* TODO: Debugging output */
1124 } /* int csnmp_instance_list_add */
1126 static int csnmp_dispatch_table(host_definition_t *host,
1127 data_definition_t *data,
1128 csnmp_list_instances_t *instance_list,
1129 csnmp_table_values_t **value_table) {
1130 const data_set_t *ds;
1131 value_list_t vl = VALUE_LIST_INIT;
1133 csnmp_list_instances_t *instance_list_ptr;
1134 csnmp_table_values_t *value_table_ptr[data->values_len];
1138 oid_t current_suffix;
1140 ds = plugin_get_ds(data->type);
1142 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1145 assert(ds->ds_num == data->values_len);
1146 assert(data->values_len > 0);
1148 instance_list_ptr = instance_list;
1150 for (i = 0; i < data->values_len; i++)
1151 value_table_ptr[i] = value_table[i];
1153 sstrncpy(vl.host, host->name, sizeof(vl.host));
1154 sstrncpy(vl.plugin, "snmp", sizeof(vl.plugin));
1156 vl.interval = host->interval;
1160 _Bool suffix_skipped = 0;
1162 /* Determine next suffix to handle. */
1163 if (instance_list != NULL) {
1164 if (instance_list_ptr == NULL) {
1169 memcpy(¤t_suffix, &instance_list_ptr->suffix,
1170 sizeof(current_suffix));
1172 /* no instance configured */
1173 csnmp_table_values_t *ptr = value_table_ptr[0];
1179 memcpy(¤t_suffix, &ptr->suffix, sizeof(current_suffix));
1182 /* Update all the value_table_ptr to point at the entry with the same
1183 * trailing partial OID */
1184 for (i = 0; i < data->values_len; i++) {
1186 (value_table_ptr[i] != NULL) &&
1187 (csnmp_oid_compare(&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1188 value_table_ptr[i] = value_table_ptr[i]->next;
1190 if (value_table_ptr[i] == NULL) {
1193 } else if (csnmp_oid_compare(&value_table_ptr[i]->suffix,
1194 ¤t_suffix) > 0) {
1195 /* This suffix is missing in the subtree. Indicate this with the
1196 * "suffix_skipped" flag and try the next instance / suffix. */
1200 } /* for (i = 0; i < columns; i++) */
1205 /* Matching the values failed. Start from the beginning again. */
1206 if (suffix_skipped) {
1207 if (instance_list != NULL)
1208 instance_list_ptr = instance_list_ptr->next;
1210 value_table_ptr[0] = value_table_ptr[0]->next;
1215 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1216 * to the same subid. instance_list_ptr is either NULL or points to the
1217 * same subid, too. */
1219 for (i = 1; i < data->values_len; i++) {
1220 assert(value_table_ptr[i] != NULL);
1221 assert(csnmp_oid_compare(&value_table_ptr[i - 1]->suffix,
1222 &value_table_ptr[i]->suffix) == 0);
1224 assert((instance_list_ptr == NULL) ||
1225 (csnmp_oid_compare(&instance_list_ptr->suffix,
1226 &value_table_ptr[0]->suffix) == 0));
1229 sstrncpy(vl.type, data->type, sizeof(vl.type));
1232 char temp[DATA_MAX_NAME_LEN];
1234 if (instance_list_ptr == NULL)
1235 csnmp_oid_to_string(temp, sizeof(temp), ¤t_suffix);
1237 sstrncpy(temp, instance_list_ptr->instance, sizeof(temp));
1239 if (data->instance_prefix == NULL)
1240 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1242 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1243 data->instance_prefix, temp);
1246 vl.values_len = data->values_len;
1247 value_t values[vl.values_len];
1250 for (i = 0; i < data->values_len; i++)
1251 vl.values[i] = value_table_ptr[i]->value;
1253 /* If we get here `vl.type_instance' and all `vl.values' have been set
1254 * vl.type_instance can be empty, i.e. a blank port description on a
1255 * switch if you're using IF-MIB::ifDescr as Instance.
1257 if (vl.type_instance[0] != '\0')
1258 plugin_dispatch_values(&vl);
1260 /* prevent leakage of pointer to local variable. */
1264 if (instance_list != NULL)
1265 instance_list_ptr = instance_list_ptr->next;
1267 value_table_ptr[0] = value_table_ptr[0]->next;
1268 } /* while (have_more) */
1271 } /* int csnmp_dispatch_table */
1273 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1274 struct snmp_pdu *req;
1275 struct snmp_pdu *res = NULL;
1276 struct variable_list *vb;
1278 const data_set_t *ds;
1280 size_t oid_list_len = data->values_len + 1;
1281 /* Holds the last OID returned by the device. We use this in the GETNEXT
1282 * request to proceed. */
1283 oid_t oid_list[oid_list_len];
1284 /* Set to false when an OID has left its subtree so we don't re-request it
1286 _Bool oid_list_todo[oid_list_len];
1291 /* `value_list_head' and `value_list_tail' implement a linked list for each
1292 * value. `instance_list_head' and `instance_list_tail' implement a linked
1294 * instance names. This is used to jump gaps in the table. */
1295 csnmp_list_instances_t *instance_list_head;
1296 csnmp_list_instances_t *instance_list_tail;
1297 csnmp_table_values_t **value_list_head;
1298 csnmp_table_values_t **value_list_tail;
1300 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1303 if (host->sess_handle == NULL) {
1304 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1308 ds = plugin_get_ds(data->type);
1310 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1314 if (ds->ds_num != data->values_len) {
1315 ERROR("snmp plugin: DataSet `%s' requires %zu values, but config talks "
1317 data->type, ds->ds_num, data->values_len);
1320 assert(data->values_len > 0);
1322 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1323 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1324 if (data->instance.oid.oid_len > 0)
1325 memcpy(oid_list + data->values_len, &data->instance.oid, sizeof(oid_t));
1326 else /* no InstanceFrom option specified. */
1329 for (i = 0; i < oid_list_len; i++)
1330 oid_list_todo[i] = 1;
1332 /* We're going to construct n linked lists, one for each "value".
1333 * value_list_head will contain pointers to the heads of these linked lists,
1334 * value_list_tail will contain pointers to the tail of the lists. */
1335 value_list_head = calloc(data->values_len, sizeof(*value_list_head));
1336 value_list_tail = calloc(data->values_len, sizeof(*value_list_tail));
1337 if ((value_list_head == NULL) || (value_list_tail == NULL)) {
1338 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1339 sfree(value_list_head);
1340 sfree(value_list_tail);
1344 instance_list_head = NULL;
1345 instance_list_tail = NULL;
1348 while (status == 0) {
1349 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1351 ERROR("snmp plugin: snmp_pdu_create failed.");
1356 size_t oid_list_todo_num = 0;
1357 size_t var_idx[oid_list_len];
1358 memset(var_idx, 0, sizeof(var_idx));
1360 for (i = 0; i < oid_list_len; i++) {
1361 /* Do not rerequest already finished OIDs */
1362 if (!oid_list_todo[i])
1364 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1365 var_idx[oid_list_todo_num] = i;
1366 oid_list_todo_num++;
1369 if (oid_list_todo_num == 0) {
1370 /* The request is still empty - so we are finished */
1371 DEBUG("snmp plugin: all variables have left their subtree");
1378 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1380 /* snmp_sess_synch_response always frees our req PDU */
1383 if ((status != STAT_SUCCESS) || (res == NULL)) {
1384 char *errstr = NULL;
1386 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1388 c_complain(LOG_ERR, &host->complaint,
1389 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1390 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1397 csnmp_host_close_session(host);
1404 assert(res != NULL);
1405 c_release(LOG_INFO, &host->complaint,
1406 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1409 vb = res->variables;
1415 if (res->errstat != SNMP_ERR_NOERROR) {
1416 if (res->errindex != 0) {
1417 /* Find the OID which caused error */
1418 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1419 vb = vb->next_variable, i++)
1423 if ((res->errindex == 0) || (vb == NULL)) {
1424 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1425 host->name, data->name, snmp_errstring(res->errstat),
1431 char oid_buffer[1024] = {0};
1432 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1434 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1435 data->name, oid_buffer, snmp_errstring(res->errstat));
1437 /* Get value index from todo list and skip OID found */
1438 assert(res->errindex <= oid_list_todo_num);
1439 i = var_idx[res->errindex - 1];
1440 assert(i < oid_list_len);
1441 oid_list_todo[i] = 0;
1448 for (vb = res->variables, i = 0; (vb != NULL);
1449 vb = vb->next_variable, i++) {
1450 /* Calculate value index from todo list */
1451 while ((i < oid_list_len) && !oid_list_todo[i])
1454 /* An instance is configured and the res variable we process is the
1455 * instance value (last index) */
1456 if ((data->instance.oid.oid_len > 0) && (i == data->values_len)) {
1457 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1459 data->instance.oid.oid, data->instance.oid.oid_len, vb->name,
1460 vb->name_length, data->instance.oid.oid_len) != 0)) {
1461 DEBUG("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1462 host->name, data->name);
1463 oid_list_todo[i] = 0;
1467 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1468 * add it to the list */
1469 if (csnmp_instance_list_add(&instance_list_head, &instance_list_tail,
1470 res, host, data) != 0) {
1471 ERROR("snmp plugin: host %s: csnmp_instance_list_add failed.",
1476 } else /* The variable we are processing is a normal value */
1478 csnmp_table_values_t *vt;
1483 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1485 /* Calculate the current suffix. This is later used to check that the
1486 * suffix is increasing. This also checks if we left the subtree */
1487 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1489 DEBUG("snmp plugin: host = %s; data = %s; i = %zu; "
1490 "Value probably left its subtree.",
1491 host->name, data->name, i);
1492 oid_list_todo[i] = 0;
1496 /* Make sure the OIDs returned by the agent are increasing. Otherwise
1498 * table matching algorithm will get confused. */
1499 if ((value_list_tail[i] != NULL) &&
1500 (csnmp_oid_compare(&suffix, &value_list_tail[i]->suffix) <= 0)) {
1501 DEBUG("snmp plugin: host = %s; data = %s; i = %zu; "
1502 "Suffix is not increasing.",
1503 host->name, data->name, i);
1504 oid_list_todo[i] = 0;
1508 vt = calloc(1, sizeof(*vt));
1510 ERROR("snmp plugin: calloc failed.");
1516 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1517 data->shift, host->name, data->name);
1518 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
1521 if (value_list_tail[i] == NULL)
1522 value_list_head[i] = vt;
1524 value_list_tail[i]->next = vt;
1525 value_list_tail[i] = vt;
1528 /* Copy OID to oid_list[i] */
1529 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
1530 oid_list[i].oid_len = vb->name_length;
1532 } /* for (vb = res->variables ...) */
1537 } /* while (status == 0) */
1545 csnmp_dispatch_table(host, data, instance_list_head, value_list_head);
1547 /* Free all allocated variables here */
1548 while (instance_list_head != NULL) {
1549 csnmp_list_instances_t *next = instance_list_head->next;
1550 sfree(instance_list_head);
1551 instance_list_head = next;
1554 for (i = 0; i < data->values_len; i++) {
1555 while (value_list_head[i] != NULL) {
1556 csnmp_table_values_t *next = value_list_head[i]->next;
1557 sfree(value_list_head[i]);
1558 value_list_head[i] = next;
1562 sfree(value_list_head);
1563 sfree(value_list_tail);
1566 } /* int csnmp_read_table */
1568 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
1569 struct snmp_pdu *req;
1570 struct snmp_pdu *res = NULL;
1571 struct variable_list *vb;
1573 const data_set_t *ds;
1574 value_list_t vl = VALUE_LIST_INIT;
1579 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
1582 if (host->sess_handle == NULL) {
1583 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1587 ds = plugin_get_ds(data->type);
1589 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1593 if (ds->ds_num != data->values_len) {
1594 ERROR("snmp plugin: DataSet `%s' requires %zu values, but config talks "
1596 data->type, ds->ds_num, data->values_len);
1600 vl.values_len = ds->ds_num;
1601 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
1602 if (vl.values == NULL)
1604 for (i = 0; i < vl.values_len; i++) {
1605 if (ds->ds[i].type == DS_TYPE_COUNTER)
1606 vl.values[i].counter = 0;
1608 vl.values[i].gauge = NAN;
1611 sstrncpy(vl.host, host->name, sizeof(vl.host));
1612 sstrncpy(vl.plugin, "snmp", sizeof(vl.plugin));
1613 sstrncpy(vl.type, data->type, sizeof(vl.type));
1614 sstrncpy(vl.type_instance, data->instance.string, sizeof(vl.type_instance));
1616 vl.interval = host->interval;
1618 req = snmp_pdu_create(SNMP_MSG_GET);
1620 ERROR("snmp plugin: snmp_pdu_create failed.");
1625 for (i = 0; i < data->values_len; i++)
1626 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
1628 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1630 if ((status != STAT_SUCCESS) || (res == NULL)) {
1631 char *errstr = NULL;
1633 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1634 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1635 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1642 csnmp_host_close_session(host);
1647 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
1650 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
1651 DEBUG("snmp plugin: Got this variable: %s", buffer);
1652 #endif /* COLLECT_DEBUG */
1654 for (i = 0; i < data->values_len; i++)
1655 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
1656 vb->name, vb->name_length) == 0)
1658 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
1659 data->shift, host->name, data->name);
1660 } /* for (res->variables) */
1664 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
1665 plugin_dispatch_values(&vl);
1669 } /* int csnmp_read_value */
1671 static int csnmp_read_host(user_data_t *ud) {
1672 host_definition_t *host;
1679 if (host->interval == 0)
1680 host->interval = plugin_get_interval();
1682 if (host->sess_handle == NULL)
1683 csnmp_host_open_session(host);
1685 if (host->sess_handle == NULL)
1689 for (i = 0; i < host->data_list_len; i++) {
1690 data_definition_t *data = host->data_list[i];
1693 status = csnmp_read_table(host, data);
1695 status = csnmp_read_value(host, data);
1705 } /* int csnmp_read_host */
1707 static int csnmp_init(void) {
1708 call_snmp_init_once();
1711 } /* int csnmp_init */
1713 static int csnmp_shutdown(void) {
1714 data_definition_t *data_this;
1715 data_definition_t *data_next;
1717 /* When we get here, the read threads have been stopped and all the
1718 * `host_definition_t' will be freed. */
1719 DEBUG("snmp plugin: Destroying all data definitions.");
1721 data_this = data_head;
1723 while (data_this != NULL) {
1724 data_next = data_this->next;
1726 sfree(data_this->name);
1727 sfree(data_this->type);
1728 sfree(data_this->values);
1729 sfree(data_this->ignores);
1732 data_this = data_next;
1736 } /* int csnmp_shutdown */
1738 void module_register(void) {
1739 plugin_register_complex_config("snmp", csnmp_config);
1740 plugin_register_init("snmp", csnmp_init);
1741 plugin_register_shutdown("snmp", csnmp_shutdown);
1742 } /* void module_register */