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>
30 #include "utils_complain.h"
34 #include <net-snmp/net-snmp-config.h>
35 #include <net-snmp/net-snmp-includes.h>
40 * Private data structes
47 typedef struct oid_s oid_t;
51 char string[DATA_MAX_NAME_LEN];
54 typedef union instance_u instance_t;
56 struct data_definition_s
58 char *name; /* used to reference this from the `Collect' option */
59 char *type; /* used to find the data_set */
62 char *instance_prefix;
67 struct data_definition_s *next;
72 typedef struct data_definition_s data_definition_t;
74 struct host_definition_s
80 /* snmpv1/2 options */
83 /* snmpv3 security options */
86 size_t auth_protocol_len;
87 char *auth_passphrase;
89 size_t priv_protocol_len;
90 char *priv_passphrase;
95 c_complain_t complaint;
97 data_definition_t **data_list;
100 typedef struct host_definition_s host_definition_t;
102 /* These two types are used to cache values in `csnmp_read_table' to handle
104 struct csnmp_list_instances_s
107 char instance[DATA_MAX_NAME_LEN];
108 struct csnmp_list_instances_s *next;
110 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
112 struct csnmp_table_values_s
116 struct csnmp_table_values_s *next;
118 typedef struct csnmp_table_values_s csnmp_table_values_t;
123 static data_definition_t *data_head = NULL;
128 static int csnmp_read_host (user_data_t *ud);
133 static void csnmp_oid_init (oid_t *dst, oid const *src, size_t n)
135 assert (n <= STATIC_ARRAY_SIZE (dst->oid));
136 memcpy (dst->oid, src, sizeof (*src) * n);
140 static int csnmp_oid_compare (oid_t const *left, oid_t const *right)
142 return (snmp_oid_compare (left->oid, left->oid_len,
143 right->oid, right->oid_len));
146 static int csnmp_oid_suffix (oid_t *dst, oid_t const *src,
149 /* Make sure "src" is in "root"s subtree. */
150 if (src->oid_len <= root->oid_len)
152 if (snmp_oid_ncompare (root->oid, root->oid_len,
153 src->oid, src->oid_len,
154 /* n = */ root->oid_len) != 0)
157 memset (dst, 0, sizeof (*dst));
158 dst->oid_len = src->oid_len - root->oid_len;
159 memcpy (dst->oid, &src->oid[root->oid_len],
160 dst->oid_len * sizeof (dst->oid[0]));
164 static int csnmp_oid_to_string (char *buffer, size_t buffer_size,
167 char oid_str[MAX_OID_LEN][16];
168 char *oid_str_ptr[MAX_OID_LEN];
171 for (i = 0; i < o->oid_len; i++)
173 ssnprintf (oid_str[i], sizeof (oid_str[i]), "%lu", (unsigned long) o->oid[i]);
174 oid_str_ptr[i] = oid_str[i];
177 return (strjoin (buffer, buffer_size,
178 oid_str_ptr, o->oid_len, /* separator = */ "."));
181 static void csnmp_host_close_session (host_definition_t *host) /* {{{ */
183 if (host->sess_handle == NULL)
186 snmp_sess_close (host->sess_handle);
187 host->sess_handle = NULL;
188 } /* }}} void csnmp_host_close_session */
190 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
192 host_definition_t *hd;
199 if (hd->name != NULL)
201 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
205 csnmp_host_close_session (hd);
209 sfree (hd->community);
210 sfree (hd->username);
211 sfree (hd->auth_passphrase);
212 sfree (hd->priv_passphrase);
214 sfree (hd->data_list);
217 } /* }}} void csnmp_host_definition_destroy */
219 /* Many functions to handle the configuration. {{{ */
220 /* First there are many functions which do configuration stuff. It's a big
221 * bloated and messy, I'm afraid. */
224 * Callgraph for the config stuff:
226 * +-> call_snmp_init_once
227 * +-> csnmp_config_add_data
228 * ! +-> csnmp_config_add_data_instance
229 * ! +-> csnmp_config_add_data_instance_prefix
230 * ! +-> csnmp_config_add_data_values
231 * +-> csnmp_config_add_host
232 * +-> csnmp_config_add_host_version
233 * +-> csnmp_config_add_host_collect
234 * +-> csnmp_config_add_host_auth_protocol
235 * +-> csnmp_config_add_host_priv_protocol
236 * +-> csnmp_config_add_host_security_level
238 static void call_snmp_init_once (void)
240 static int have_init = 0;
243 init_snmp (PACKAGE_NAME);
245 } /* void call_snmp_init_once */
247 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
249 char buffer[DATA_MAX_NAME_LEN];
252 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
258 /* Instance is an OID */
259 dd->instance.oid.oid_len = MAX_OID_LEN;
261 if (!read_objid (buffer,
262 dd->instance.oid.oid, &dd->instance.oid.oid_len))
264 ERROR ("snmp plugin: read_objid (%s) failed.", buffer);
270 /* Instance is a simple string */
271 sstrncpy (dd->instance.string, buffer,
272 sizeof (dd->instance.string));
276 } /* int csnmp_config_add_data_instance */
278 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
285 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
286 "is set to `false'.", dd->name);
290 status = cf_util_get_string(ci, &dd->instance_prefix);
292 } /* int csnmp_config_add_data_instance_prefix */
294 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
298 if (ci->values_num < 1)
300 WARNING ("snmp plugin: `Values' needs at least one argument.");
304 for (i = 0; i < ci->values_num; i++)
305 if (ci->values[i].type != OCONFIG_TYPE_STRING)
307 WARNING ("snmp plugin: `Values' needs only string argument.");
313 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
314 if (dd->values == NULL)
316 dd->values_len = (size_t) ci->values_num;
318 for (i = 0; i < ci->values_num; i++)
320 dd->values[i].oid_len = MAX_OID_LEN;
322 if (NULL == snmp_parse_oid (ci->values[i].value.string,
323 dd->values[i].oid, &dd->values[i].oid_len))
325 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
326 ci->values[i].value.string);
335 } /* int csnmp_config_add_data_instance */
337 static int csnmp_config_add_data_blacklist(data_definition_t *dd, oconfig_item_t *ci)
341 if (ci->values_num < 1)
344 for (i = 0; i < ci->values_num; i++)
346 if (ci->values[i].type != OCONFIG_TYPE_STRING)
348 WARNING ("snmp plugin: `Ignore' needs only string argument.");
356 for (i = 0; i < ci->values_num; ++i)
358 if (strarray_add(&(dd->ignores), &(dd->ignores_len), ci->values[i].value.string) != 0)
360 ERROR("snmp plugin: Can't allocate memory");
361 strarray_free(dd->ignores, dd->ignores_len);
366 } /* int csnmp_config_add_data_blacklist */
368 static int csnmp_config_add_data_blacklist_match_inverted(data_definition_t *dd, oconfig_item_t *ci)
370 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
372 WARNING ("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
376 dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
379 } /* int csnmp_config_add_data_blacklist_match_inverted */
381 static int csnmp_config_add_data (oconfig_item_t *ci)
383 data_definition_t *dd;
387 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
390 memset (dd, '\0', sizeof (data_definition_t));
392 status = cf_util_get_string(ci, &dd->name);
402 for (i = 0; i < ci->children_num; i++)
404 oconfig_item_t *option = ci->children + i;
406 if (strcasecmp ("Type", option->key) == 0)
407 status = cf_util_get_string(option, &dd->type);
408 else if (strcasecmp ("Table", option->key) == 0)
409 status = cf_util_get_boolean(option, &dd->is_table);
410 else if (strcasecmp ("Instance", option->key) == 0)
411 status = csnmp_config_add_data_instance (dd, option);
412 else if (strcasecmp ("InstancePrefix", option->key) == 0)
413 status = csnmp_config_add_data_instance_prefix (dd, option);
414 else if (strcasecmp ("Values", option->key) == 0)
415 status = csnmp_config_add_data_values (dd, option);
416 else if (strcasecmp ("Shift", option->key) == 0)
417 status = cf_util_get_double(option, &dd->shift);
418 else if (strcasecmp ("Scale", option->key) == 0)
419 status = cf_util_get_double(option, &dd->scale);
420 else if (strcasecmp ("Ignore", option->key) == 0)
421 status = csnmp_config_add_data_blacklist(dd, option);
422 else if (strcasecmp ("InvertMatch", option->key) == 0)
423 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
426 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
432 } /* for (ci->children) */
436 if (dd->type == NULL)
438 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
442 if (dd->values == NULL)
444 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
450 } /* while (status == 0) */
455 sfree (dd->instance_prefix);
462 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %zu }",
463 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
465 if (data_head == NULL)
469 data_definition_t *last;
471 while (last->next != NULL)
477 } /* int csnmp_config_add_data */
479 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
483 if ((ci->values_num != 1)
484 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
486 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
490 version = (int) ci->values[0].value.number;
491 if ((version < 1) || (version > 3))
493 WARNING ("snmp plugin: `Version' must either be `1', `2', or `3'.");
497 hd->version = version;
500 } /* int csnmp_config_add_host_address */
502 static int csnmp_config_add_host_collect (host_definition_t *host,
505 data_definition_t *data;
506 data_definition_t **data_list;
510 if (ci->values_num < 1)
512 WARNING ("snmp plugin: `Collect' needs at least one argument.");
516 for (i = 0; i < ci->values_num; i++)
517 if (ci->values[i].type != OCONFIG_TYPE_STRING)
519 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
523 data_list_len = host->data_list_len + ci->values_num;
524 data_list = (data_definition_t **) realloc (host->data_list,
525 sizeof (data_definition_t *) * data_list_len);
526 if (data_list == NULL)
528 host->data_list = data_list;
530 for (i = 0; i < ci->values_num; i++)
532 for (data = data_head; data != NULL; data = data->next)
533 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
538 WARNING ("snmp plugin: No such data configured: `%s'",
539 ci->values[i].value.string);
543 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
544 host->name, host->data_list_len, data->name);
546 host->data_list[host->data_list_len] = data;
547 host->data_list_len++;
548 } /* for (values_num) */
551 } /* int csnmp_config_add_host_collect */
553 static int csnmp_config_add_host_auth_protocol (host_definition_t *hd, oconfig_item_t *ci)
558 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
562 if (strcasecmp("MD5", buffer) == 0) {
563 hd->auth_protocol = usmHMACMD5AuthProtocol;
564 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
566 else if (strcasecmp("SHA", buffer) == 0) {
567 hd->auth_protocol = usmHMACSHA1AuthProtocol;
568 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol)/sizeof(oid);
572 WARNING ("snmp plugin: The `AuthProtocol' config option must be `MD5' or `SHA'.");
576 DEBUG ("snmp plugin: host = %s; host->auth_protocol = %s;",
577 hd->name, hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
580 } /* int csnmp_config_add_host_auth_protocol */
582 static int csnmp_config_add_host_priv_protocol (host_definition_t *hd, oconfig_item_t *ci)
587 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
591 if (strcasecmp("AES", buffer) == 0)
593 hd->priv_protocol = usmAESPrivProtocol;
594 hd->priv_protocol_len = sizeof(usmAESPrivProtocol)/sizeof(oid);
596 else if (strcasecmp("DES", buffer) == 0) {
597 hd->priv_protocol = usmDESPrivProtocol;
598 hd->priv_protocol_len = sizeof(usmDESPrivProtocol)/sizeof(oid);
602 WARNING ("snmp plugin: The `PrivProtocol' config option must be `AES' or `DES'.");
606 DEBUG ("snmp plugin: host = %s; host->priv_protocol = %s;",
607 hd->name, hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
610 } /* int csnmp_config_add_host_priv_protocol */
612 static int csnmp_config_add_host_security_level (host_definition_t *hd, oconfig_item_t *ci)
617 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
621 if (strcasecmp("noAuthNoPriv", buffer) == 0)
622 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
623 else if (strcasecmp("authNoPriv", buffer) == 0)
624 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
625 else if (strcasecmp("authPriv", buffer) == 0)
626 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
629 WARNING ("snmp plugin: The `SecurityLevel' config option must be `noAuthNoPriv', `authNoPriv', or `authPriv'.");
633 DEBUG ("snmp plugin: host = %s; host->security_level = %d;",
634 hd->name, hd->security_level);
637 } /* int csnmp_config_add_host_security_level */
639 static int csnmp_config_add_host (oconfig_item_t *ci)
641 host_definition_t *hd;
645 /* Registration stuff. */
646 char cb_name[DATA_MAX_NAME_LEN];
649 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
652 memset (hd, '\0', sizeof (host_definition_t));
654 C_COMPLAIN_INIT (&hd->complaint);
656 status = cf_util_get_string(ci, &hd->name);
663 hd->sess_handle = NULL;
666 for (i = 0; i < ci->children_num; i++)
668 oconfig_item_t *option = ci->children + i;
671 if (strcasecmp ("Address", option->key) == 0)
672 status = cf_util_get_string(option, &hd->address);
673 else if (strcasecmp ("Community", option->key) == 0)
674 status = cf_util_get_string(option, &hd->community);
675 else if (strcasecmp ("Version", option->key) == 0)
676 status = csnmp_config_add_host_version (hd, option);
677 else if (strcasecmp ("Collect", option->key) == 0)
678 csnmp_config_add_host_collect (hd, option);
679 else if (strcasecmp ("Interval", option->key) == 0)
680 cf_util_get_cdtime (option, &hd->interval);
681 else if (strcasecmp ("Username", option->key) == 0)
682 status = cf_util_get_string(option, &hd->username);
683 else if (strcasecmp ("AuthProtocol", option->key) == 0)
684 status = csnmp_config_add_host_auth_protocol (hd, option);
685 else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
686 status = csnmp_config_add_host_priv_protocol (hd, option);
687 else if (strcasecmp ("AuthPassphrase", option->key) == 0)
688 status = cf_util_get_string(option, &hd->auth_passphrase);
689 else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
690 status = cf_util_get_string(option, &hd->priv_passphrase);
691 else if (strcasecmp ("SecurityLevel", option->key) == 0)
692 status = csnmp_config_add_host_security_level (hd, option);
693 else if (strcasecmp ("Context", option->key) == 0)
694 status = cf_util_get_string(option, &hd->context);
697 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
703 } /* for (ci->children) */
707 if (hd->address == NULL)
709 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
713 if (hd->community == NULL && hd->version < 3)
715 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
719 if (hd->version == 3)
721 if (hd->username == NULL)
723 WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
727 if (hd->security_level == 0)
729 WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
733 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
735 if (hd->auth_protocol == NULL)
737 WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
741 if (hd->auth_passphrase == NULL)
743 WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
748 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
750 if (hd->priv_protocol == NULL)
752 WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
756 if (hd->priv_passphrase == NULL)
758 WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
766 } /* while (status == 0) */
770 csnmp_host_definition_destroy (hd);
774 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
775 hd->name, hd->address, hd->community, hd->version);
777 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
779 memset (&cb_data, 0, sizeof (cb_data));
781 cb_data.free_func = csnmp_host_definition_destroy;
783 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
784 csnmp_read_host, hd->interval, /* user_data = */ &cb_data);
787 ERROR ("snmp plugin: Registering complex read function failed.");
788 csnmp_host_definition_destroy (hd);
793 } /* int csnmp_config_add_host */
795 static int csnmp_config (oconfig_item_t *ci)
799 call_snmp_init_once ();
801 for (i = 0; i < ci->children_num; i++)
803 oconfig_item_t *child = ci->children + i;
804 if (strcasecmp ("Data", child->key) == 0)
805 csnmp_config_add_data (child);
806 else if (strcasecmp ("Host", child->key) == 0)
807 csnmp_config_add_host (child);
810 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
812 } /* for (ci->children) */
815 } /* int csnmp_config */
817 /* }}} End of the config stuff. Now the interesting part begins */
819 static void csnmp_host_open_session (host_definition_t *host)
821 struct snmp_session sess;
824 if (host->sess_handle != NULL)
825 csnmp_host_close_session (host);
827 snmp_sess_init (&sess);
828 sess.peername = host->address;
829 switch (host->version)
832 sess.version = SNMP_VERSION_1;
835 sess.version = SNMP_VERSION_3;
838 sess.version = SNMP_VERSION_2c;
842 if (host->version == 3)
844 sess.securityName = host->username;
845 sess.securityNameLen = strlen (host->username);
846 sess.securityLevel = host->security_level;
848 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
850 sess.securityAuthProto = host->auth_protocol;
851 sess.securityAuthProtoLen = host->auth_protocol_len;
852 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
853 error = generate_Ku (sess.securityAuthProto,
854 sess.securityAuthProtoLen,
855 (u_char *) host->auth_passphrase,
856 strlen(host->auth_passphrase),
857 sess.securityAuthKey,
858 &sess.securityAuthKeyLen);
859 if (error != SNMPERR_SUCCESS) {
860 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
864 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
866 sess.securityPrivProto = host->priv_protocol;
867 sess.securityPrivProtoLen = host->priv_protocol_len;
868 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
869 error = generate_Ku (sess.securityAuthProto,
870 sess.securityAuthProtoLen,
871 (u_char *) host->priv_passphrase,
872 strlen(host->priv_passphrase),
873 sess.securityPrivKey,
874 &sess.securityPrivKeyLen);
875 if (error != SNMPERR_SUCCESS) {
876 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
880 if (host->context != NULL)
882 sess.contextName = host->context;
883 sess.contextNameLen = strlen (host->context);
886 else /* SNMPv1/2 "authenticates" with community string */
888 sess.community = (u_char *) host->community;
889 sess.community_len = strlen (host->community);
892 /* snmp_sess_open will copy the `struct snmp_session *'. */
893 host->sess_handle = snmp_sess_open (&sess);
895 if (host->sess_handle == NULL)
899 snmp_error (&sess, NULL, NULL, &errstr);
901 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
902 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
905 } /* void csnmp_host_open_session */
907 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
908 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
909 double scale, double shift,
910 const char *host_name, const char *data_name)
913 uint64_t tmp_unsigned = 0;
914 int64_t tmp_signed = 0;
916 /* Set to true when the original SNMP type appears to have been signed. */
917 _Bool prefer_signed = 0;
919 if ((vl->type == ASN_INTEGER)
920 || (vl->type == ASN_UINTEGER)
921 || (vl->type == ASN_COUNTER)
923 || (vl->type == ASN_TIMETICKS)
925 || (vl->type == ASN_GAUGE))
927 tmp_unsigned = (uint32_t) *vl->val.integer;
928 tmp_signed = (int32_t) *vl->val.integer;
930 if (vl->type == ASN_INTEGER)
933 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
935 else if (vl->type == ASN_COUNTER64)
937 tmp_unsigned = (uint32_t) vl->val.counter64->high;
938 tmp_unsigned = tmp_unsigned << 32;
939 tmp_unsigned += (uint32_t) vl->val.counter64->low;
940 tmp_signed = (int64_t) tmp_unsigned;
941 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
943 else if (vl->type == ASN_OCTET_STR)
945 /* We'll handle this later.. */
949 char oid_buffer[1024];
951 memset (oid_buffer, 0, sizeof (oid_buffer));
952 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
953 vl->name, vl->name_length);
956 if (vl->type == ASN_NULL)
957 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
961 WARNING ("snmp plugin: I don't know the ASN type #%i "
962 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
963 (int) vl->type, oid_buffer,
964 (data_name != NULL) ? data_name : "UNKNOWN",
965 (host_name != NULL) ? host_name : "UNKNOWN");
970 if (vl->type == ASN_OCTET_STR)
974 if (vl->val.string != NULL)
977 size_t string_length;
979 string_length = sizeof (string) - 1;
980 if (vl->val_len < string_length)
981 string_length = vl->val_len;
983 /* The strings we get from the Net-SNMP library may not be null
984 * terminated. That is why we're using `memcpy' here and not `strcpy'.
985 * `string_length' is set to `vl->val_len' which holds the length of the
987 memcpy (string, vl->val.string, string_length);
988 string[string_length] = 0;
990 status = parse_value (string, &ret, type);
993 ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
994 (host_name != NULL) ? host_name : "UNKNOWN",
995 DS_TYPE_TO_STRING (type), string);
1003 case DS_TYPE_COUNTER:
1004 case DS_TYPE_DERIVE:
1005 case DS_TYPE_ABSOLUTE:
1006 memset (&ret, 0, sizeof (ret));
1014 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
1015 "data source type: %i.", type);
1019 } /* if (vl->type == ASN_OCTET_STR) */
1020 else if (type == DS_TYPE_COUNTER)
1022 ret.counter = tmp_unsigned;
1024 else if (type == DS_TYPE_GAUGE)
1028 else if (prefer_signed)
1029 ret.gauge = (scale * tmp_signed) + shift;
1031 ret.gauge = (scale * tmp_unsigned) + shift;
1033 else if (type == DS_TYPE_DERIVE)
1036 ret.derive = (derive_t) tmp_signed;
1038 ret.derive = (derive_t) tmp_unsigned;
1040 else if (type == DS_TYPE_ABSOLUTE)
1042 ret.absolute = (absolute_t) tmp_unsigned;
1046 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1052 } /* value_t csnmp_value_list_to_value */
1054 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1055 * representation and writes it to dst. Returns zero on success and ENOMEM if
1056 * dst is not large enough to hold the string. dst is guaranteed to be
1057 * nul-terminated. */
1058 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
1059 const struct variable_list *vb, size_t dst_size)
1068 buffer_free = dst_size;
1070 for (i = 0; i < vb->val_len; i++)
1074 status = snprintf (buffer_ptr, buffer_free,
1075 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1076 assert (status >= 0);
1078 if (((size_t) status) >= buffer_free) /* truncated */
1080 dst[dst_size - 1] = 0;
1083 else /* if (status < buffer_free) */
1085 buffer_ptr += (size_t) status;
1086 buffer_free -= (size_t) status;
1091 } /* }}} int csnmp_strvbcopy_hexstring */
1093 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1094 * dst. If non-printable characters are detected, it will switch to a hex
1095 * representation of the string. Returns zero on success, EINVAL if vb does not
1096 * contain a string and ENOMEM if dst is not large enough to contain the
1098 static int csnmp_strvbcopy (char *dst, /* {{{ */
1099 const struct variable_list *vb, size_t dst_size)
1105 if (vb->type == ASN_OCTET_STR)
1106 src = (char *) vb->val.string;
1107 else if (vb->type == ASN_BIT_STR)
1108 src = (char *) vb->val.bitstring;
1115 num_chars = dst_size - 1;
1116 if (num_chars > vb->val_len)
1117 num_chars = vb->val_len;
1119 for (i = 0; i < num_chars; i++)
1121 /* Check for control characters. */
1122 if ((unsigned char)src[i] < 32)
1123 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1127 dst[dst_size - 1] = 0;
1129 if (dst_size <= vb->val_len)
1133 } /* }}} int csnmp_strvbcopy */
1135 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1136 csnmp_list_instances_t **tail,
1137 const struct snmp_pdu *res,
1138 const host_definition_t *hd, const data_definition_t *dd)
1140 csnmp_list_instances_t *il;
1141 struct variable_list *vb;
1145 uint32_t is_matched;
1147 /* Set vb on the last variable */
1148 for (vb = res->variables;
1149 (vb != NULL) && (vb->next_variable != NULL);
1150 vb = vb->next_variable)
1155 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1157 il = malloc (sizeof (*il));
1160 ERROR ("snmp plugin: malloc failed.");
1163 memset (il, 0, sizeof (*il));
1166 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1173 /* Get instance name */
1174 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1178 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1180 for (i = 0; i < dd->ignores_len; i++)
1182 status = fnmatch(dd->ignores[i], il->instance, 0);
1185 if (dd->invert_match == 0)
1197 if (dd->invert_match != 0 && is_matched == 0)
1202 for (ptr = il->instance; *ptr != '\0'; ptr++)
1204 if ((*ptr > 0) && (*ptr < 32))
1206 else if (*ptr == '/')
1209 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1213 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1214 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1215 ssnprintf (il->instance, sizeof (il->instance),
1216 "%llu", val.counter);
1219 /* TODO: Debugging output */
1228 } /* int csnmp_instance_list_add */
1230 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1231 csnmp_list_instances_t *instance_list,
1232 csnmp_table_values_t **value_table)
1234 const data_set_t *ds;
1235 value_list_t vl = VALUE_LIST_INIT;
1237 csnmp_list_instances_t *instance_list_ptr;
1238 csnmp_table_values_t **value_table_ptr;
1242 oid_t current_suffix;
1244 ds = plugin_get_ds (data->type);
1247 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1250 assert (ds->ds_num == data->values_len);
1251 assert (data->values_len > 0);
1253 instance_list_ptr = instance_list;
1255 value_table_ptr = calloc (data->values_len, sizeof (*value_table_ptr));
1256 if (value_table_ptr == NULL)
1258 for (i = 0; i < data->values_len; i++)
1259 value_table_ptr[i] = value_table[i];
1261 vl.values_len = data->values_len;
1262 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1263 if (vl.values == NULL)
1265 ERROR ("snmp plugin: malloc failed.");
1266 sfree (value_table_ptr);
1270 sstrncpy (vl.host, host->name, sizeof (vl.host));
1271 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1273 vl.interval = host->interval;
1276 memset (¤t_suffix, 0, sizeof (current_suffix));
1279 _Bool suffix_skipped = 0;
1281 /* Determine next suffix to handle. */
1282 if (instance_list != NULL)
1284 if (instance_list_ptr == NULL)
1290 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1292 else /* no instance configured */
1294 csnmp_table_values_t *ptr = value_table_ptr[0];
1301 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1304 /* Update all the value_table_ptr to point at the entry with the same
1305 * trailing partial OID */
1306 for (i = 0; i < data->values_len; i++)
1308 while ((value_table_ptr[i] != NULL)
1309 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1310 value_table_ptr[i] = value_table_ptr[i]->next;
1312 if (value_table_ptr[i] == NULL)
1317 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1319 /* This suffix is missing in the subtree. Indicate this with the
1320 * "suffix_skipped" flag and try the next instance / suffix. */
1324 } /* for (i = 0; i < columns; i++) */
1329 /* Matching the values failed. Start from the beginning again. */
1332 if (instance_list != NULL)
1333 instance_list_ptr = instance_list_ptr->next;
1335 value_table_ptr[0] = value_table_ptr[0]->next;
1340 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1341 * to the same subid. instance_list_ptr is either NULL or points to the
1342 * same subid, too. */
1344 for (i = 1; i < data->values_len; i++)
1346 assert (value_table_ptr[i] != NULL);
1347 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1348 &value_table_ptr[i]->suffix) == 0);
1350 assert ((instance_list_ptr == NULL)
1351 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1352 &value_table_ptr[0]->suffix) == 0));
1355 sstrncpy (vl.type, data->type, sizeof (vl.type));
1358 char temp[DATA_MAX_NAME_LEN];
1360 if (instance_list_ptr == NULL)
1361 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1363 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1365 if (data->instance_prefix == NULL)
1366 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1368 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1369 data->instance_prefix, temp);
1372 for (i = 0; i < data->values_len; i++)
1373 vl.values[i] = value_table_ptr[i]->value;
1375 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1376 plugin_dispatch_values (&vl);
1378 if (instance_list != NULL)
1379 instance_list_ptr = instance_list_ptr->next;
1381 value_table_ptr[0] = value_table_ptr[0]->next;
1382 } /* while (have_more) */
1385 sfree (value_table_ptr);
1388 } /* int csnmp_dispatch_table */
1390 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1392 struct snmp_pdu *req;
1393 struct snmp_pdu *res;
1394 struct variable_list *vb;
1396 const data_set_t *ds;
1398 size_t oid_list_len = data->values_len + 1;
1399 /* Holds the last OID returned by the device. We use this in the GETNEXT
1400 * request to proceed. */
1401 oid_t oid_list[oid_list_len];
1402 /* Set to false when an OID has left its subtree so we don't re-request it
1404 _Bool oid_list_todo[oid_list_len];
1409 /* `value_list_head' and `value_list_tail' implement a linked list for each
1410 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1411 * instance names. This is used to jump gaps in the table. */
1412 csnmp_list_instances_t *instance_list_head;
1413 csnmp_list_instances_t *instance_list_tail;
1414 csnmp_table_values_t **value_list_head;
1415 csnmp_table_values_t **value_list_tail;
1417 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1418 host->name, data->name);
1420 if (host->sess_handle == NULL)
1422 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1426 ds = plugin_get_ds (data->type);
1429 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1433 if (ds->ds_num != data->values_len)
1435 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1436 data->type, ds->ds_num, data->values_len);
1439 assert (data->values_len > 0);
1441 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1442 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1443 if (data->instance.oid.oid_len > 0)
1444 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1445 else /* no InstanceFrom option specified. */
1448 for (i = 0; i < oid_list_len; i++)
1449 oid_list_todo[i] = 1;
1451 /* We're going to construct n linked lists, one for each "value".
1452 * value_list_head will contain pointers to the heads of these linked lists,
1453 * value_list_tail will contain pointers to the tail of the lists. */
1454 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1455 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1456 if ((value_list_head == NULL) || (value_list_tail == NULL))
1458 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1459 sfree (value_list_head);
1460 sfree (value_list_tail);
1464 instance_list_head = NULL;
1465 instance_list_tail = NULL;
1470 int oid_list_todo_num;
1472 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1475 ERROR ("snmp plugin: snmp_pdu_create failed.");
1480 oid_list_todo_num = 0;
1481 for (i = 0; i < oid_list_len; i++)
1483 /* Do not rerequest already finished OIDs */
1484 if (!oid_list_todo[i])
1486 oid_list_todo_num++;
1487 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1490 if (oid_list_todo_num == 0)
1492 /* The request is still empty - so we are finished */
1493 DEBUG ("snmp plugin: all variables have left their subtree");
1499 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1500 if ((status != STAT_SUCCESS) || (res == NULL))
1502 char *errstr = NULL;
1504 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1506 c_complain (LOG_ERR, &host->complaint,
1507 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1508 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1511 snmp_free_pdu (res);
1514 /* snmp_synch_response already freed our PDU */
1517 csnmp_host_close_session (host);
1524 assert (res != NULL);
1525 c_release (LOG_INFO, &host->complaint,
1526 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1529 vb = res->variables;
1536 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1538 /* Calculate value index from todo list */
1539 while ((i < oid_list_len) && !oid_list_todo[i])
1542 /* An instance is configured and the res variable we process is the
1543 * instance value (last index) */
1544 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1546 if ((vb->type == SNMP_ENDOFMIBVIEW)
1547 || (snmp_oid_ncompare (data->instance.oid.oid,
1548 data->instance.oid.oid_len,
1549 vb->name, vb->name_length,
1550 data->instance.oid.oid_len) != 0))
1552 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1553 host->name, data->name);
1554 oid_list_todo[i] = 0;
1558 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1559 * add it to the list */
1560 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1561 res, host, data) != 0)
1563 ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
1569 else /* The variable we are processing is a normal value */
1571 csnmp_table_values_t *vt;
1576 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1578 /* Calculate the current suffix. This is later used to check that the
1579 * suffix is increasing. This also checks if we left the subtree */
1580 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1583 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1584 "Value probably left its subtree.",
1585 host->name, data->name, i);
1586 oid_list_todo[i] = 0;
1590 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1591 * table matching algorithm will get confused. */
1592 if ((value_list_tail[i] != NULL)
1593 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1595 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1596 "Suffix is not increasing.",
1597 host->name, data->name, i);
1598 oid_list_todo[i] = 0;
1602 vt = malloc (sizeof (*vt));
1605 ERROR ("snmp plugin: malloc failed.");
1609 memset (vt, 0, sizeof (*vt));
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;
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 = (value_t *) malloc (sizeof (value_t) * 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);
1735 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1737 if ((status != STAT_SUCCESS) || (res == NULL))
1739 char *errstr = NULL;
1741 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1742 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1743 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1746 snmp_free_pdu (res);
1750 csnmp_host_close_session (host);
1756 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1760 snprint_variable (buffer, sizeof (buffer),
1761 vb->name, vb->name_length, vb);
1762 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1763 #endif /* COLLECT_DEBUG */
1765 for (i = 0; i < data->values_len; i++)
1766 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1767 vb->name, vb->name_length) == 0)
1768 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1769 data->scale, data->shift, host->name, data->name);
1770 } /* for (res->variables) */
1773 snmp_free_pdu (res);
1776 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1777 plugin_dispatch_values (&vl);
1781 } /* int csnmp_read_value */
1783 static int csnmp_read_host (user_data_t *ud)
1785 host_definition_t *host;
1792 if (host->interval == 0)
1793 host->interval = plugin_get_interval ();
1795 if (host->sess_handle == NULL)
1796 csnmp_host_open_session (host);
1798 if (host->sess_handle == NULL)
1802 for (i = 0; i < host->data_list_len; i++)
1804 data_definition_t *data = host->data_list[i];
1807 status = csnmp_read_table (host, data);
1809 status = csnmp_read_value (host, data);
1819 } /* int csnmp_read_host */
1821 static int csnmp_init (void)
1823 call_snmp_init_once ();
1826 } /* int csnmp_init */
1828 static int csnmp_shutdown (void)
1830 data_definition_t *data_this;
1831 data_definition_t *data_next;
1833 /* When we get here, the read threads have been stopped and all the
1834 * `host_definition_t' will be freed. */
1835 DEBUG ("snmp plugin: Destroying all data definitions.");
1837 data_this = data_head;
1839 while (data_this != NULL)
1841 data_next = data_this->next;
1843 sfree (data_this->name);
1844 sfree (data_this->type);
1845 sfree (data_this->values);
1846 sfree (data_this->ignores);
1849 data_this = data_next;
1853 } /* int csnmp_shutdown */
1855 void module_register (void)
1857 plugin_register_complex_config ("snmp", csnmp_config);
1858 plugin_register_init ("snmp", csnmp_init);
1859 plugin_register_shutdown ("snmp", csnmp_shutdown);
1860 } /* void module_register */
1863 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker