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
46 typedef struct oid_s oid_t;
50 char string[DATA_MAX_NAME_LEN];
53 typedef union instance_u instance_t;
55 struct data_definition_s
57 char *name; /* used to reference this from the `Collect' option */
58 char *type; /* used to find the data_set */
61 char *instance_prefix;
66 struct data_definition_s *next;
71 typedef struct data_definition_s data_definition_t;
73 struct host_definition_s
79 /* snmpv1/2 options */
82 /* snmpv3 security options */
85 size_t auth_protocol_len;
86 char *auth_passphrase;
88 size_t priv_protocol_len;
89 char *priv_passphrase;
94 c_complain_t complaint;
96 data_definition_t **data_list;
99 typedef struct host_definition_s host_definition_t;
101 /* These two types are used to cache values in `csnmp_read_table' to handle
103 struct csnmp_list_instances_s
106 char instance[DATA_MAX_NAME_LEN];
107 struct csnmp_list_instances_s *next;
109 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
111 struct csnmp_table_values_s
115 struct csnmp_table_values_s *next;
117 typedef struct csnmp_table_values_s csnmp_table_values_t;
122 static data_definition_t *data_head = NULL;
127 static int csnmp_read_host (user_data_t *ud);
132 static void csnmp_oid_init (oid_t *dst, oid const *src, size_t n)
134 assert (n <= STATIC_ARRAY_SIZE (dst->oid));
135 memcpy (dst->oid, src, sizeof (*src) * n);
139 static int csnmp_oid_compare (oid_t const *left, oid_t const *right)
141 return (snmp_oid_compare (left->oid, left->oid_len,
142 right->oid, right->oid_len));
145 static int csnmp_oid_suffix (oid_t *dst, oid_t const *src,
148 /* Make sure "src" is in "root"s subtree. */
149 if (src->oid_len <= root->oid_len)
151 if (snmp_oid_ncompare (root->oid, root->oid_len,
152 src->oid, src->oid_len,
153 /* n = */ root->oid_len) != 0)
156 memset (dst, 0, sizeof (*dst));
157 dst->oid_len = src->oid_len - root->oid_len;
158 memcpy (dst->oid, &src->oid[root->oid_len],
159 dst->oid_len * sizeof (dst->oid[0]));
163 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];
170 for (i = 0; i < o->oid_len; i++)
172 ssnprintf (oid_str[i], sizeof (oid_str[i]), "%lu", (unsigned long) o->oid[i]);
173 oid_str_ptr[i] = oid_str[i];
176 return (strjoin (buffer, buffer_size,
177 oid_str_ptr, o->oid_len, /* separator = */ "."));
180 static void csnmp_host_close_session (host_definition_t *host) /* {{{ */
182 if (host->sess_handle == NULL)
185 snmp_sess_close (host->sess_handle);
186 host->sess_handle = NULL;
187 } /* }}} void csnmp_host_close_session */
189 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
191 host_definition_t *hd;
198 if (hd->name != NULL)
200 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
204 csnmp_host_close_session (hd);
208 sfree (hd->community);
209 sfree (hd->username);
210 sfree (hd->auth_passphrase);
211 sfree (hd->priv_passphrase);
213 sfree (hd->data_list);
216 } /* }}} void csnmp_host_definition_destroy */
218 /* Many functions to handle the configuration. {{{ */
219 /* First there are many functions which do configuration stuff. It's a big
220 * bloated and messy, I'm afraid. */
223 * Callgraph for the config stuff:
225 * +-> call_snmp_init_once
226 * +-> csnmp_config_add_data
227 * ! +-> csnmp_config_add_data_instance
228 * ! +-> csnmp_config_add_data_instance_prefix
229 * ! +-> csnmp_config_add_data_values
230 * +-> csnmp_config_add_host
231 * +-> csnmp_config_add_host_version
232 * +-> csnmp_config_add_host_collect
233 * +-> csnmp_config_add_host_auth_protocol
234 * +-> csnmp_config_add_host_priv_protocol
235 * +-> csnmp_config_add_host_security_level
237 static void call_snmp_init_once (void)
239 static int have_init = 0;
242 init_snmp (PACKAGE_NAME);
244 } /* void call_snmp_init_once */
246 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
248 char buffer[DATA_MAX_NAME_LEN];
251 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
257 /* Instance is an OID */
258 dd->instance.oid.oid_len = MAX_OID_LEN;
260 if (!read_objid (buffer,
261 dd->instance.oid.oid, &dd->instance.oid.oid_len))
263 ERROR ("snmp plugin: read_objid (%s) failed.", buffer);
269 /* Instance is a simple string */
270 sstrncpy (dd->instance.string, buffer,
271 sizeof (dd->instance.string));
275 } /* int csnmp_config_add_data_instance */
277 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
284 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
285 "is set to `false'.", dd->name);
289 status = cf_util_get_string(ci, &dd->instance_prefix);
291 } /* int csnmp_config_add_data_instance_prefix */
293 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
297 if (ci->values_num < 1)
299 WARNING ("snmp plugin: `Values' needs at least one argument.");
303 for (i = 0; i < ci->values_num; i++)
304 if (ci->values[i].type != OCONFIG_TYPE_STRING)
306 WARNING ("snmp plugin: `Values' needs only string argument.");
312 dd->values = malloc (sizeof (*dd->values) * ci->values_num);
313 if (dd->values == NULL)
315 dd->values_len = (size_t) ci->values_num;
317 for (i = 0; i < ci->values_num; i++)
319 dd->values[i].oid_len = MAX_OID_LEN;
321 if (NULL == snmp_parse_oid (ci->values[i].value.string,
322 dd->values[i].oid, &dd->values[i].oid_len))
324 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
325 ci->values[i].value.string);
334 } /* int csnmp_config_add_data_instance */
336 static int csnmp_config_add_data_blacklist(data_definition_t *dd, oconfig_item_t *ci)
340 if (ci->values_num < 1)
343 for (i = 0; i < ci->values_num; i++)
345 if (ci->values[i].type != OCONFIG_TYPE_STRING)
347 WARNING ("snmp plugin: `Ignore' needs only string argument.");
355 for (i = 0; i < ci->values_num; ++i)
357 if (strarray_add(&(dd->ignores), &(dd->ignores_len), ci->values[i].value.string) != 0)
359 ERROR("snmp plugin: Can't allocate memory");
360 strarray_free(dd->ignores, dd->ignores_len);
365 } /* int csnmp_config_add_data_blacklist */
367 static int csnmp_config_add_data_blacklist_match_inverted(data_definition_t *dd, oconfig_item_t *ci)
369 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
371 WARNING ("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
375 dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
378 } /* int csnmp_config_add_data_blacklist_match_inverted */
380 static int csnmp_config_add_data (oconfig_item_t *ci)
382 data_definition_t *dd;
386 dd = calloc (1, sizeof (*dd));
390 status = cf_util_get_string(ci, &dd->name);
400 for (i = 0; i < ci->children_num; i++)
402 oconfig_item_t *option = ci->children + i;
404 if (strcasecmp ("Type", option->key) == 0)
405 status = cf_util_get_string(option, &dd->type);
406 else if (strcasecmp ("Table", option->key) == 0)
407 status = cf_util_get_boolean(option, &dd->is_table);
408 else if (strcasecmp ("Instance", option->key) == 0)
409 status = csnmp_config_add_data_instance (dd, option);
410 else if (strcasecmp ("InstancePrefix", option->key) == 0)
411 status = csnmp_config_add_data_instance_prefix (dd, option);
412 else if (strcasecmp ("Values", option->key) == 0)
413 status = csnmp_config_add_data_values (dd, option);
414 else if (strcasecmp ("Shift", option->key) == 0)
415 status = cf_util_get_double(option, &dd->shift);
416 else if (strcasecmp ("Scale", option->key) == 0)
417 status = cf_util_get_double(option, &dd->scale);
418 else if (strcasecmp ("Ignore", option->key) == 0)
419 status = csnmp_config_add_data_blacklist(dd, option);
420 else if (strcasecmp ("InvertMatch", option->key) == 0)
421 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
424 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
430 } /* for (ci->children) */
434 if (dd->type == NULL)
436 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
440 if (dd->values == NULL)
442 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
448 } /* while (status == 0) */
453 sfree (dd->instance_prefix);
460 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %zu }",
461 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
463 if (data_head == NULL)
467 data_definition_t *last;
469 while (last->next != NULL)
475 } /* int csnmp_config_add_data */
477 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
481 if ((ci->values_num != 1)
482 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
484 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
488 version = (int) ci->values[0].value.number;
489 if ((version < 1) || (version > 3))
491 WARNING ("snmp plugin: `Version' must either be `1', `2', or `3'.");
495 hd->version = version;
498 } /* int csnmp_config_add_host_address */
500 static int csnmp_config_add_host_collect (host_definition_t *host,
503 data_definition_t *data;
504 data_definition_t **data_list;
508 if (ci->values_num < 1)
510 WARNING ("snmp plugin: `Collect' needs at least one argument.");
514 for (i = 0; i < ci->values_num; i++)
515 if (ci->values[i].type != OCONFIG_TYPE_STRING)
517 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
521 data_list_len = host->data_list_len + ci->values_num;
522 data_list = realloc (host->data_list,
523 sizeof (data_definition_t *) * data_list_len);
524 if (data_list == NULL)
526 host->data_list = data_list;
528 for (i = 0; i < ci->values_num; i++)
530 for (data = data_head; data != NULL; data = data->next)
531 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
536 WARNING ("snmp plugin: No such data configured: `%s'",
537 ci->values[i].value.string);
541 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
542 host->name, host->data_list_len, data->name);
544 host->data_list[host->data_list_len] = data;
545 host->data_list_len++;
546 } /* for (values_num) */
549 } /* int csnmp_config_add_host_collect */
551 static int csnmp_config_add_host_auth_protocol (host_definition_t *hd, oconfig_item_t *ci)
556 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
560 if (strcasecmp("MD5", buffer) == 0) {
561 hd->auth_protocol = usmHMACMD5AuthProtocol;
562 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
564 else if (strcasecmp("SHA", buffer) == 0) {
565 hd->auth_protocol = usmHMACSHA1AuthProtocol;
566 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol)/sizeof(oid);
570 WARNING ("snmp plugin: The `AuthProtocol' config option must be `MD5' or `SHA'.");
574 DEBUG ("snmp plugin: host = %s; host->auth_protocol = %s;",
575 hd->name, hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
578 } /* int csnmp_config_add_host_auth_protocol */
580 static int csnmp_config_add_host_priv_protocol (host_definition_t *hd, oconfig_item_t *ci)
585 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
589 if (strcasecmp("AES", buffer) == 0)
591 hd->priv_protocol = usmAESPrivProtocol;
592 hd->priv_protocol_len = sizeof(usmAESPrivProtocol)/sizeof(oid);
594 else if (strcasecmp("DES", buffer) == 0) {
595 hd->priv_protocol = usmDESPrivProtocol;
596 hd->priv_protocol_len = sizeof(usmDESPrivProtocol)/sizeof(oid);
600 WARNING ("snmp plugin: The `PrivProtocol' config option must be `AES' or `DES'.");
604 DEBUG ("snmp plugin: host = %s; host->priv_protocol = %s;",
605 hd->name, hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
608 } /* int csnmp_config_add_host_priv_protocol */
610 static int csnmp_config_add_host_security_level (host_definition_t *hd, oconfig_item_t *ci)
615 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
619 if (strcasecmp("noAuthNoPriv", buffer) == 0)
620 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
621 else if (strcasecmp("authNoPriv", buffer) == 0)
622 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
623 else if (strcasecmp("authPriv", buffer) == 0)
624 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
627 WARNING ("snmp plugin: The `SecurityLevel' config option must be `noAuthNoPriv', `authNoPriv', or `authPriv'.");
631 DEBUG ("snmp plugin: host = %s; host->security_level = %d;",
632 hd->name, hd->security_level);
635 } /* int csnmp_config_add_host_security_level */
637 static int csnmp_config_add_host (oconfig_item_t *ci)
639 host_definition_t *hd;
643 /* Registration stuff. */
644 char cb_name[DATA_MAX_NAME_LEN];
645 user_data_t cb_data = { 0 };
647 hd = calloc (1, sizeof (*hd));
651 C_COMPLAIN_INIT (&hd->complaint);
653 status = cf_util_get_string(ci, &hd->name);
660 hd->sess_handle = NULL;
663 for (i = 0; i < ci->children_num; i++)
665 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 ("Collect", option->key) == 0)
675 csnmp_config_add_host_collect (hd, option);
676 else if (strcasecmp ("Interval", option->key) == 0)
677 cf_util_get_cdtime (option, &hd->interval);
678 else if (strcasecmp ("Username", option->key) == 0)
679 status = cf_util_get_string(option, &hd->username);
680 else if (strcasecmp ("AuthProtocol", option->key) == 0)
681 status = csnmp_config_add_host_auth_protocol (hd, option);
682 else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
683 status = csnmp_config_add_host_priv_protocol (hd, option);
684 else if (strcasecmp ("AuthPassphrase", option->key) == 0)
685 status = cf_util_get_string(option, &hd->auth_passphrase);
686 else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
687 status = cf_util_get_string(option, &hd->priv_passphrase);
688 else if (strcasecmp ("SecurityLevel", option->key) == 0)
689 status = csnmp_config_add_host_security_level (hd, option);
690 else if (strcasecmp ("Context", option->key) == 0)
691 status = cf_util_get_string(option, &hd->context);
694 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
700 } /* for (ci->children) */
704 if (hd->address == NULL)
706 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
710 if (hd->community == NULL && hd->version < 3)
712 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
716 if (hd->version == 3)
718 if (hd->username == NULL)
720 WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
724 if (hd->security_level == 0)
726 WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
730 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
732 if (hd->auth_protocol == NULL)
734 WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
738 if (hd->auth_passphrase == NULL)
740 WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
745 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
747 if (hd->priv_protocol == NULL)
749 WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
753 if (hd->priv_passphrase == NULL)
755 WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
763 } /* while (status == 0) */
767 csnmp_host_definition_destroy (hd);
771 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
772 hd->name, hd->address, hd->community, hd->version);
774 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
777 cb_data.free_func = csnmp_host_definition_destroy;
779 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
780 csnmp_read_host, hd->interval, /* user_data = */ &cb_data);
783 ERROR ("snmp plugin: Registering complex read function failed.");
784 csnmp_host_definition_destroy (hd);
789 } /* int csnmp_config_add_host */
791 static int csnmp_config (oconfig_item_t *ci)
795 call_snmp_init_once ();
797 for (i = 0; i < ci->children_num; i++)
799 oconfig_item_t *child = ci->children + i;
800 if (strcasecmp ("Data", child->key) == 0)
801 csnmp_config_add_data (child);
802 else if (strcasecmp ("Host", child->key) == 0)
803 csnmp_config_add_host (child);
806 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
808 } /* for (ci->children) */
811 } /* int csnmp_config */
813 /* }}} End of the config stuff. Now the interesting part begins */
815 static void csnmp_host_open_session (host_definition_t *host)
817 struct snmp_session sess;
820 if (host->sess_handle != NULL)
821 csnmp_host_close_session (host);
823 snmp_sess_init (&sess);
824 sess.peername = host->address;
825 switch (host->version)
828 sess.version = SNMP_VERSION_1;
831 sess.version = SNMP_VERSION_3;
834 sess.version = SNMP_VERSION_2c;
838 if (host->version == 3)
840 sess.securityName = host->username;
841 sess.securityNameLen = strlen (host->username);
842 sess.securityLevel = host->security_level;
844 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
846 sess.securityAuthProto = host->auth_protocol;
847 sess.securityAuthProtoLen = host->auth_protocol_len;
848 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
849 error = generate_Ku (sess.securityAuthProto,
850 sess.securityAuthProtoLen,
851 (u_char *) host->auth_passphrase,
852 strlen(host->auth_passphrase),
853 sess.securityAuthKey,
854 &sess.securityAuthKeyLen);
855 if (error != SNMPERR_SUCCESS) {
856 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
860 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
862 sess.securityPrivProto = host->priv_protocol;
863 sess.securityPrivProtoLen = host->priv_protocol_len;
864 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
865 error = generate_Ku (sess.securityAuthProto,
866 sess.securityAuthProtoLen,
867 (u_char *) host->priv_passphrase,
868 strlen(host->priv_passphrase),
869 sess.securityPrivKey,
870 &sess.securityPrivKeyLen);
871 if (error != SNMPERR_SUCCESS) {
872 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
876 if (host->context != NULL)
878 sess.contextName = host->context;
879 sess.contextNameLen = strlen (host->context);
882 else /* SNMPv1/2 "authenticates" with community string */
884 sess.community = (u_char *) host->community;
885 sess.community_len = strlen (host->community);
888 /* snmp_sess_open will copy the `struct snmp_session *'. */
889 host->sess_handle = snmp_sess_open (&sess);
891 if (host->sess_handle == NULL)
895 snmp_error (&sess, NULL, NULL, &errstr);
897 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
898 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
901 } /* void csnmp_host_open_session */
903 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
904 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
905 double scale, double shift,
906 const char *host_name, const char *data_name)
909 uint64_t tmp_unsigned = 0;
910 int64_t tmp_signed = 0;
912 /* Set to true when the original SNMP type appears to have been signed. */
913 _Bool prefer_signed = 0;
915 if ((vl->type == ASN_INTEGER)
916 || (vl->type == ASN_UINTEGER)
917 || (vl->type == ASN_COUNTER)
919 || (vl->type == ASN_TIMETICKS)
921 || (vl->type == ASN_GAUGE))
923 tmp_unsigned = (uint32_t) *vl->val.integer;
924 tmp_signed = (int32_t) *vl->val.integer;
926 if (vl->type == ASN_INTEGER)
929 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
931 else if (vl->type == ASN_COUNTER64)
933 tmp_unsigned = (uint32_t) vl->val.counter64->high;
934 tmp_unsigned = tmp_unsigned << 32;
935 tmp_unsigned += (uint32_t) vl->val.counter64->low;
936 tmp_signed = (int64_t) tmp_unsigned;
937 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
939 else if (vl->type == ASN_OCTET_STR)
941 /* We'll handle this later.. */
945 char oid_buffer[1024] = { 0 };
947 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
948 vl->name, vl->name_length);
951 if (vl->type == ASN_NULL)
952 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
956 WARNING ("snmp plugin: I don't know the ASN type #%i "
957 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
958 (int) vl->type, oid_buffer,
959 (data_name != NULL) ? data_name : "UNKNOWN",
960 (host_name != NULL) ? host_name : "UNKNOWN");
965 if (vl->type == ASN_OCTET_STR)
969 if (vl->val.string != NULL)
972 size_t string_length;
974 string_length = sizeof (string) - 1;
975 if (vl->val_len < string_length)
976 string_length = vl->val_len;
978 /* The strings we get from the Net-SNMP library may not be null
979 * terminated. That is why we're using `memcpy' here and not `strcpy'.
980 * `string_length' is set to `vl->val_len' which holds the length of the
982 memcpy (string, vl->val.string, string_length);
983 string[string_length] = 0;
985 status = parse_value (string, &ret, type);
988 ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
989 (host_name != NULL) ? host_name : "UNKNOWN",
990 DS_TYPE_TO_STRING (type), string);
998 case DS_TYPE_COUNTER:
1000 case DS_TYPE_ABSOLUTE:
1001 memset (&ret, 0, sizeof (ret));
1009 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
1010 "data source type: %i.", type);
1014 } /* if (vl->type == ASN_OCTET_STR) */
1015 else if (type == DS_TYPE_COUNTER)
1017 ret.counter = tmp_unsigned;
1019 else if (type == DS_TYPE_GAUGE)
1023 else if (prefer_signed)
1024 ret.gauge = (scale * tmp_signed) + shift;
1026 ret.gauge = (scale * tmp_unsigned) + shift;
1028 else if (type == DS_TYPE_DERIVE)
1031 ret.derive = (derive_t) tmp_signed;
1033 ret.derive = (derive_t) tmp_unsigned;
1035 else if (type == DS_TYPE_ABSOLUTE)
1037 ret.absolute = (absolute_t) tmp_unsigned;
1041 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1047 } /* value_t csnmp_value_list_to_value */
1049 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1050 * representation and writes it to dst. Returns zero on success and ENOMEM if
1051 * dst is not large enough to hold the string. dst is guaranteed to be
1052 * nul-terminated. */
1053 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
1054 const struct variable_list *vb, size_t dst_size)
1063 buffer_free = dst_size;
1065 for (i = 0; i < vb->val_len; i++)
1069 status = snprintf (buffer_ptr, buffer_free,
1070 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1071 assert (status >= 0);
1073 if (((size_t) status) >= buffer_free) /* truncated */
1075 dst[dst_size - 1] = 0;
1078 else /* if (status < buffer_free) */
1080 buffer_ptr += (size_t) status;
1081 buffer_free -= (size_t) status;
1086 } /* }}} int csnmp_strvbcopy_hexstring */
1088 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1089 * dst. If non-printable characters are detected, it will switch to a hex
1090 * representation of the string. Returns zero on success, EINVAL if vb does not
1091 * contain a string and ENOMEM if dst is not large enough to contain the
1093 static int csnmp_strvbcopy (char *dst, /* {{{ */
1094 const struct variable_list *vb, size_t dst_size)
1100 if (vb->type == ASN_OCTET_STR)
1101 src = (char *) vb->val.string;
1102 else if (vb->type == ASN_BIT_STR)
1103 src = (char *) vb->val.bitstring;
1104 else if (vb->type == ASN_IPADDRESS)
1106 return ssnprintf (dst, dst_size, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
1107 (uint8_t) vb->val.string[0],
1108 (uint8_t) vb->val.string[1],
1109 (uint8_t) vb->val.string[2],
1110 (uint8_t) vb->val.string[3]);
1118 num_chars = dst_size - 1;
1119 if (num_chars > vb->val_len)
1120 num_chars = vb->val_len;
1122 for (i = 0; i < num_chars; i++)
1124 /* Check for control characters. */
1125 if ((unsigned char)src[i] < 32)
1126 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1130 dst[dst_size - 1] = 0;
1132 if (dst_size <= vb->val_len)
1136 } /* }}} int csnmp_strvbcopy */
1138 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1139 csnmp_list_instances_t **tail,
1140 const struct snmp_pdu *res,
1141 const host_definition_t *hd, const data_definition_t *dd)
1143 csnmp_list_instances_t *il;
1144 struct variable_list *vb;
1148 uint32_t is_matched;
1150 /* Set vb on the last variable */
1151 for (vb = res->variables;
1152 (vb != NULL) && (vb->next_variable != NULL);
1153 vb = vb->next_variable)
1158 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1160 il = calloc (1, sizeof (*il));
1163 ERROR ("snmp plugin: calloc failed.");
1168 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1175 /* Get instance name */
1176 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) || (vb->type == ASN_IPADDRESS))
1180 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1182 for (i = 0; i < dd->ignores_len; i++)
1184 status = fnmatch(dd->ignores[i], il->instance, 0);
1187 if (dd->invert_match == 0)
1199 if (dd->invert_match != 0 && is_matched == 0)
1204 for (ptr = il->instance; *ptr != '\0'; ptr++)
1206 if ((*ptr > 0) && (*ptr < 32))
1208 else if (*ptr == '/')
1211 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1215 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1216 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1217 ssnprintf (il->instance, sizeof (il->instance),
1218 "%llu", val.counter);
1221 /* TODO: Debugging output */
1230 } /* int csnmp_instance_list_add */
1232 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1233 csnmp_list_instances_t *instance_list,
1234 csnmp_table_values_t **value_table)
1236 const data_set_t *ds;
1237 value_list_t vl = VALUE_LIST_INIT;
1239 csnmp_list_instances_t *instance_list_ptr;
1240 csnmp_table_values_t **value_table_ptr;
1244 oid_t current_suffix;
1246 ds = plugin_get_ds (data->type);
1249 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1252 assert (ds->ds_num == data->values_len);
1253 assert (data->values_len > 0);
1255 instance_list_ptr = instance_list;
1257 value_table_ptr = calloc (data->values_len, sizeof (*value_table_ptr));
1258 if (value_table_ptr == NULL)
1260 for (i = 0; i < data->values_len; i++)
1261 value_table_ptr[i] = value_table[i];
1263 vl.values_len = data->values_len;
1264 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1265 if (vl.values == NULL)
1267 ERROR ("snmp plugin: malloc failed.");
1268 sfree (value_table_ptr);
1272 sstrncpy (vl.host, host->name, sizeof (vl.host));
1273 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1275 vl.interval = host->interval;
1280 _Bool suffix_skipped = 0;
1282 /* Determine next suffix to handle. */
1283 if (instance_list != NULL)
1285 if (instance_list_ptr == NULL)
1291 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1293 else /* no instance configured */
1295 csnmp_table_values_t *ptr = value_table_ptr[0];
1302 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1305 /* Update all the value_table_ptr to point at the entry with the same
1306 * trailing partial OID */
1307 for (i = 0; i < data->values_len; i++)
1309 while ((value_table_ptr[i] != NULL)
1310 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1311 value_table_ptr[i] = value_table_ptr[i]->next;
1313 if (value_table_ptr[i] == NULL)
1318 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1320 /* This suffix is missing in the subtree. Indicate this with the
1321 * "suffix_skipped" flag and try the next instance / suffix. */
1325 } /* for (i = 0; i < columns; i++) */
1330 /* Matching the values failed. Start from the beginning again. */
1333 if (instance_list != NULL)
1334 instance_list_ptr = instance_list_ptr->next;
1336 value_table_ptr[0] = value_table_ptr[0]->next;
1341 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1342 * to the same subid. instance_list_ptr is either NULL or points to the
1343 * same subid, too. */
1345 for (i = 1; i < data->values_len; i++)
1347 assert (value_table_ptr[i] != NULL);
1348 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1349 &value_table_ptr[i]->suffix) == 0);
1351 assert ((instance_list_ptr == NULL)
1352 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1353 &value_table_ptr[0]->suffix) == 0));
1356 sstrncpy (vl.type, data->type, sizeof (vl.type));
1359 char temp[DATA_MAX_NAME_LEN];
1361 if (instance_list_ptr == NULL)
1362 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1364 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1366 if (data->instance_prefix == NULL)
1367 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1369 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1370 data->instance_prefix, temp);
1373 for (i = 0; i < data->values_len; i++)
1374 vl.values[i] = value_table_ptr[i]->value;
1376 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1377 plugin_dispatch_values (&vl);
1379 if (instance_list != NULL)
1380 instance_list_ptr = instance_list_ptr->next;
1382 value_table_ptr[0] = value_table_ptr[0]->next;
1383 } /* while (have_more) */
1386 sfree (value_table_ptr);
1389 } /* int csnmp_dispatch_table */
1391 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1393 struct snmp_pdu *req;
1394 struct snmp_pdu *res = NULL;
1395 struct variable_list *vb;
1397 const data_set_t *ds;
1399 size_t oid_list_len = data->values_len + 1;
1400 /* Holds the last OID returned by the device. We use this in the GETNEXT
1401 * request to proceed. */
1402 oid_t oid_list[oid_list_len];
1403 /* Set to false when an OID has left its subtree so we don't re-request it
1405 _Bool oid_list_todo[oid_list_len];
1410 /* `value_list_head' and `value_list_tail' implement a linked list for each
1411 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1412 * instance names. This is used to jump gaps in the table. */
1413 csnmp_list_instances_t *instance_list_head;
1414 csnmp_list_instances_t *instance_list_tail;
1415 csnmp_table_values_t **value_list_head;
1416 csnmp_table_values_t **value_list_tail;
1418 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1419 host->name, data->name);
1421 if (host->sess_handle == NULL)
1423 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1427 ds = plugin_get_ds (data->type);
1430 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1434 if (ds->ds_num != data->values_len)
1436 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1437 data->type, ds->ds_num, data->values_len);
1440 assert (data->values_len > 0);
1442 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1443 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1444 if (data->instance.oid.oid_len > 0)
1445 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1446 else /* no InstanceFrom option specified. */
1449 for (i = 0; i < oid_list_len; i++)
1450 oid_list_todo[i] = 1;
1452 /* We're going to construct n linked lists, one for each "value".
1453 * value_list_head will contain pointers to the heads of these linked lists,
1454 * value_list_tail will contain pointers to the tail of the lists. */
1455 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1456 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1457 if ((value_list_head == NULL) || (value_list_tail == NULL))
1459 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1460 sfree (value_list_head);
1461 sfree (value_list_tail);
1465 instance_list_head = NULL;
1466 instance_list_tail = NULL;
1471 int oid_list_todo_num;
1473 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1476 ERROR ("snmp plugin: snmp_pdu_create failed.");
1481 oid_list_todo_num = 0;
1482 for (i = 0; i < oid_list_len; i++)
1484 /* Do not rerequest already finished OIDs */
1485 if (!oid_list_todo[i])
1487 oid_list_todo_num++;
1488 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1491 if (oid_list_todo_num == 0)
1493 /* The request is still empty - so we are finished */
1494 DEBUG ("snmp plugin: all variables have left their subtree");
1500 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1501 if ((status != STAT_SUCCESS) || (res == NULL))
1503 char *errstr = NULL;
1505 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1507 c_complain (LOG_ERR, &host->complaint,
1508 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1509 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1512 snmp_free_pdu (res);
1515 /* snmp_synch_response already freed our PDU */
1518 csnmp_host_close_session (host);
1525 assert (res != NULL);
1526 c_release (LOG_INFO, &host->complaint,
1527 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1530 vb = res->variables;
1537 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1539 /* Calculate value index from todo list */
1540 while ((i < oid_list_len) && !oid_list_todo[i])
1543 /* An instance is configured and the res variable we process is the
1544 * instance value (last index) */
1545 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1547 if ((vb->type == SNMP_ENDOFMIBVIEW)
1548 || (snmp_oid_ncompare (data->instance.oid.oid,
1549 data->instance.oid.oid_len,
1550 vb->name, vb->name_length,
1551 data->instance.oid.oid_len) != 0))
1553 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1554 host->name, data->name);
1555 oid_list_todo[i] = 0;
1559 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1560 * add it to the list */
1561 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1562 res, host, data) != 0)
1564 ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
1570 else /* The variable we are processing is a normal value */
1572 csnmp_table_values_t *vt;
1577 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1579 /* Calculate the current suffix. This is later used to check that the
1580 * suffix is increasing. This also checks if we left the subtree */
1581 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1584 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1585 "Value probably left its subtree.",
1586 host->name, data->name, i);
1587 oid_list_todo[i] = 0;
1591 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1592 * table matching algorithm will get confused. */
1593 if ((value_list_tail[i] != NULL)
1594 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1596 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1597 "Suffix is not increasing.",
1598 host->name, data->name, i);
1599 oid_list_todo[i] = 0;
1603 vt = calloc (1, sizeof (*vt));
1606 ERROR ("snmp plugin: calloc failed.");
1611 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1612 data->scale, data->shift, host->name, data->name);
1613 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1616 if (value_list_tail[i] == NULL)
1617 value_list_head[i] = vt;
1619 value_list_tail[i]->next = vt;
1620 value_list_tail[i] = vt;
1623 /* Copy OID to oid_list[i] */
1624 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1625 oid_list[i].oid_len = vb->name_length;
1627 } /* for (vb = res->variables ...) */
1630 snmp_free_pdu (res);
1632 } /* while (status == 0) */
1635 snmp_free_pdu (res);
1639 snmp_free_pdu (req);
1643 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1645 /* Free all allocated variables here */
1646 while (instance_list_head != NULL)
1648 csnmp_list_instances_t *next = instance_list_head->next;
1649 sfree (instance_list_head);
1650 instance_list_head = next;
1653 for (i = 0; i < data->values_len; i++)
1655 while (value_list_head[i] != NULL)
1657 csnmp_table_values_t *next = value_list_head[i]->next;
1658 sfree (value_list_head[i]);
1659 value_list_head[i] = next;
1663 sfree (value_list_head);
1664 sfree (value_list_tail);
1667 } /* int csnmp_read_table */
1669 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1671 struct snmp_pdu *req;
1672 struct snmp_pdu *res = NULL;
1673 struct variable_list *vb;
1675 const data_set_t *ds;
1676 value_list_t vl = VALUE_LIST_INIT;
1681 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1682 host->name, data->name);
1684 if (host->sess_handle == NULL)
1686 DEBUG ("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1690 ds = plugin_get_ds (data->type);
1693 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1697 if (ds->ds_num != data->values_len)
1699 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1700 data->type, ds->ds_num, data->values_len);
1704 vl.values_len = ds->ds_num;
1705 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1706 if (vl.values == NULL)
1708 for (i = 0; i < vl.values_len; i++)
1710 if (ds->ds[i].type == DS_TYPE_COUNTER)
1711 vl.values[i].counter = 0;
1713 vl.values[i].gauge = NAN;
1716 sstrncpy (vl.host, host->name, sizeof (vl.host));
1717 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1718 sstrncpy (vl.type, data->type, sizeof (vl.type));
1719 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1721 vl.interval = host->interval;
1723 req = snmp_pdu_create (SNMP_MSG_GET);
1726 ERROR ("snmp plugin: snmp_pdu_create failed.");
1731 for (i = 0; i < data->values_len; i++)
1732 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1734 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1736 if ((status != STAT_SUCCESS) || (res == NULL))
1738 char *errstr = NULL;
1740 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1741 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1742 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1745 snmp_free_pdu (res);
1749 csnmp_host_close_session (host);
1755 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1759 snprint_variable (buffer, sizeof (buffer),
1760 vb->name, vb->name_length, vb);
1761 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1762 #endif /* COLLECT_DEBUG */
1764 for (i = 0; i < data->values_len; i++)
1765 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1766 vb->name, vb->name_length) == 0)
1767 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1768 data->scale, data->shift, host->name, data->name);
1769 } /* for (res->variables) */
1771 snmp_free_pdu (res);
1773 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1774 plugin_dispatch_values (&vl);
1778 } /* int csnmp_read_value */
1780 static int csnmp_read_host (user_data_t *ud)
1782 host_definition_t *host;
1789 if (host->interval == 0)
1790 host->interval = plugin_get_interval ();
1792 if (host->sess_handle == NULL)
1793 csnmp_host_open_session (host);
1795 if (host->sess_handle == NULL)
1799 for (i = 0; i < host->data_list_len; i++)
1801 data_definition_t *data = host->data_list[i];
1804 status = csnmp_read_table (host, data);
1806 status = csnmp_read_value (host, data);
1816 } /* int csnmp_read_host */
1818 static int csnmp_init (void)
1820 call_snmp_init_once ();
1823 } /* int csnmp_init */
1825 static int csnmp_shutdown (void)
1827 data_definition_t *data_this;
1828 data_definition_t *data_next;
1830 /* When we get here, the read threads have been stopped and all the
1831 * `host_definition_t' will be freed. */
1832 DEBUG ("snmp plugin: Destroying all data definitions.");
1834 data_this = data_head;
1836 while (data_this != NULL)
1838 data_next = data_this->next;
1840 sfree (data_this->name);
1841 sfree (data_this->type);
1842 sfree (data_this->values);
1843 sfree (data_this->ignores);
1846 data_this = data_next;
1850 } /* int csnmp_shutdown */
1852 void module_register (void)
1854 plugin_register_complex_config ("snmp", csnmp_config);
1855 plugin_register_init ("snmp", csnmp_init);
1856 plugin_register_shutdown ("snmp", csnmp_shutdown);
1857 } /* void module_register */
1860 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker