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 = 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;
407 if (strcasecmp ("Type", option->key) == 0)
408 status = cf_util_get_string(option, &dd->type);
409 else if (strcasecmp ("Table", option->key) == 0)
410 status = cf_util_get_boolean(option, &dd->is_table);
411 else if (strcasecmp ("Instance", option->key) == 0)
412 status = csnmp_config_add_data_instance (dd, option);
413 else if (strcasecmp ("InstancePrefix", option->key) == 0)
414 status = csnmp_config_add_data_instance_prefix (dd, option);
415 else if (strcasecmp ("Values", option->key) == 0)
416 status = csnmp_config_add_data_values (dd, option);
417 else if (strcasecmp ("Shift", option->key) == 0)
418 status = cf_util_get_double(option, &dd->shift);
419 else if (strcasecmp ("Scale", option->key) == 0)
420 status = cf_util_get_double(option, &dd->scale);
421 else if (strcasecmp ("Ignore", option->key) == 0)
422 status = csnmp_config_add_data_blacklist(dd, option);
423 else if (strcasecmp ("InvertMatch", option->key) == 0)
424 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
427 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
433 } /* for (ci->children) */
437 if (dd->type == NULL)
439 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
443 if (dd->values == NULL)
445 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
451 } /* while (status == 0) */
456 sfree (dd->instance_prefix);
463 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
464 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
466 if (data_head == NULL)
470 data_definition_t *last;
472 while (last->next != NULL)
478 } /* int csnmp_config_add_data */
480 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
484 if ((ci->values_num != 1)
485 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
487 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
491 version = (int) ci->values[0].value.number;
492 if ((version < 1) || (version > 3))
494 WARNING ("snmp plugin: `Version' must either be `1', `2', or `3'.");
498 hd->version = version;
501 } /* int csnmp_config_add_host_address */
503 static int csnmp_config_add_host_collect (host_definition_t *host,
506 data_definition_t *data;
507 data_definition_t **data_list;
511 if (ci->values_num < 1)
513 WARNING ("snmp plugin: `Collect' needs at least one argument.");
517 for (i = 0; i < ci->values_num; i++)
518 if (ci->values[i].type != OCONFIG_TYPE_STRING)
520 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
524 data_list_len = host->data_list_len + ci->values_num;
525 data_list = (data_definition_t **) realloc (host->data_list,
526 sizeof (data_definition_t *) * data_list_len);
527 if (data_list == NULL)
529 host->data_list = data_list;
531 for (i = 0; i < ci->values_num; i++)
533 for (data = data_head; data != NULL; data = data->next)
534 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
539 WARNING ("snmp plugin: No such data configured: `%s'",
540 ci->values[i].value.string);
544 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
545 host->name, host->data_list_len, data->name);
547 host->data_list[host->data_list_len] = data;
548 host->data_list_len++;
549 } /* for (values_num) */
552 } /* int csnmp_config_add_host_collect */
554 static int csnmp_config_add_host_auth_protocol (host_definition_t *hd, oconfig_item_t *ci)
559 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
563 if (strcasecmp("MD5", buffer) == 0) {
564 hd->auth_protocol = usmHMACMD5AuthProtocol;
565 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
567 else if (strcasecmp("SHA", buffer) == 0) {
568 hd->auth_protocol = usmHMACSHA1AuthProtocol;
569 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol)/sizeof(oid);
573 WARNING ("snmp plugin: The `AuthProtocol' config option must be `MD5' or `SHA'.");
577 DEBUG ("snmp plugin: host = %s; host->auth_protocol = %s;",
578 hd->name, hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
581 } /* int csnmp_config_add_host_auth_protocol */
583 static int csnmp_config_add_host_priv_protocol (host_definition_t *hd, oconfig_item_t *ci)
588 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
592 if (strcasecmp("AES", buffer) == 0)
594 hd->priv_protocol = usmAESPrivProtocol;
595 hd->priv_protocol_len = sizeof(usmAESPrivProtocol)/sizeof(oid);
597 else if (strcasecmp("DES", buffer) == 0) {
598 hd->priv_protocol = usmDESPrivProtocol;
599 hd->priv_protocol_len = sizeof(usmDESPrivProtocol)/sizeof(oid);
603 WARNING ("snmp plugin: The `PrivProtocol' config option must be `AES' or `DES'.");
607 DEBUG ("snmp plugin: host = %s; host->priv_protocol = %s;",
608 hd->name, hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
611 } /* int csnmp_config_add_host_priv_protocol */
613 static int csnmp_config_add_host_security_level (host_definition_t *hd, oconfig_item_t *ci)
618 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
622 if (strcasecmp("noAuthNoPriv", buffer) == 0)
623 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
624 else if (strcasecmp("authNoPriv", buffer) == 0)
625 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
626 else if (strcasecmp("authPriv", buffer) == 0)
627 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
630 WARNING ("snmp plugin: The `SecurityLevel' config option must be `noAuthNoPriv', `authNoPriv', or `authPriv'.");
634 DEBUG ("snmp plugin: host = %s; host->security_level = %d;",
635 hd->name, hd->security_level);
638 } /* int csnmp_config_add_host_security_level */
640 static int csnmp_config_add_host (oconfig_item_t *ci)
642 host_definition_t *hd;
646 /* Registration stuff. */
647 char cb_name[DATA_MAX_NAME_LEN];
650 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
653 memset (hd, '\0', sizeof (host_definition_t));
655 C_COMPLAIN_INIT (&hd->complaint);
657 status = cf_util_get_string(ci, &hd->name);
661 hd->sess_handle = NULL;
664 for (i = 0; i < ci->children_num; i++)
666 oconfig_item_t *option = ci->children + i;
669 if (strcasecmp ("Address", option->key) == 0)
670 status = cf_util_get_string(option, &hd->address);
671 else if (strcasecmp ("Community", option->key) == 0)
672 status = cf_util_get_string(option, &hd->community);
673 else if (strcasecmp ("Version", option->key) == 0)
674 status = csnmp_config_add_host_version (hd, option);
675 else if (strcasecmp ("Collect", option->key) == 0)
676 csnmp_config_add_host_collect (hd, option);
677 else if (strcasecmp ("Interval", option->key) == 0)
678 cf_util_get_cdtime (option, &hd->interval);
679 else if (strcasecmp ("Username", option->key) == 0)
680 status = cf_util_get_string(option, &hd->username);
681 else if (strcasecmp ("AuthProtocol", option->key) == 0)
682 status = csnmp_config_add_host_auth_protocol (hd, option);
683 else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
684 status = csnmp_config_add_host_priv_protocol (hd, option);
685 else if (strcasecmp ("AuthPassphrase", option->key) == 0)
686 status = cf_util_get_string(option, &hd->auth_passphrase);
687 else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
688 status = cf_util_get_string(option, &hd->priv_passphrase);
689 else if (strcasecmp ("SecurityLevel", option->key) == 0)
690 status = csnmp_config_add_host_security_level (hd, option);
691 else if (strcasecmp ("Context", option->key) == 0)
692 status = cf_util_get_string(option, &hd->context);
695 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
701 } /* for (ci->children) */
705 if (hd->address == NULL)
707 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
711 if (hd->community == NULL && hd->version < 3)
713 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
717 if (hd->version == 3)
719 if (hd->username == NULL)
721 WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
725 if (hd->security_level == 0)
727 WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
731 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
733 if (hd->auth_protocol == NULL)
735 WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
739 if (hd->auth_passphrase == NULL)
741 WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
746 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
748 if (hd->priv_protocol == NULL)
750 WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
754 if (hd->priv_passphrase == NULL)
756 WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
764 } /* while (status == 0) */
768 csnmp_host_definition_destroy (hd);
772 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
773 hd->name, hd->address, hd->community, hd->version);
775 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
777 memset (&cb_data, 0, sizeof (cb_data));
779 cb_data.free_func = csnmp_host_definition_destroy;
781 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
782 csnmp_read_host, hd->interval, /* user_data = */ &cb_data);
785 ERROR ("snmp plugin: Registering complex read function failed.");
786 csnmp_host_definition_destroy (hd);
791 } /* int csnmp_config_add_host */
793 static int csnmp_config (oconfig_item_t *ci)
797 call_snmp_init_once ();
799 for (i = 0; i < ci->children_num; i++)
801 oconfig_item_t *child = ci->children + i;
802 if (strcasecmp ("Data", child->key) == 0)
803 csnmp_config_add_data (child);
804 else if (strcasecmp ("Host", child->key) == 0)
805 csnmp_config_add_host (child);
808 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
810 } /* for (ci->children) */
813 } /* int csnmp_config */
815 /* }}} End of the config stuff. Now the interesting part begins */
817 static void csnmp_host_open_session (host_definition_t *host)
819 struct snmp_session sess;
822 if (host->sess_handle != NULL)
823 csnmp_host_close_session (host);
825 snmp_sess_init (&sess);
826 sess.peername = host->address;
827 switch (host->version)
830 sess.version = SNMP_VERSION_1;
833 sess.version = SNMP_VERSION_3;
836 sess.version = SNMP_VERSION_2c;
840 if (host->version == 3)
842 sess.securityName = host->username;
843 sess.securityNameLen = strlen (host->username);
844 sess.securityLevel = host->security_level;
846 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
848 sess.securityAuthProto = host->auth_protocol;
849 sess.securityAuthProtoLen = host->auth_protocol_len;
850 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
851 error = generate_Ku (sess.securityAuthProto,
852 sess.securityAuthProtoLen,
853 (u_char *) host->auth_passphrase,
854 strlen(host->auth_passphrase),
855 sess.securityAuthKey,
856 &sess.securityAuthKeyLen);
857 if (error != SNMPERR_SUCCESS) {
858 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
862 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
864 sess.securityPrivProto = host->priv_protocol;
865 sess.securityPrivProtoLen = host->priv_protocol_len;
866 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
867 error = generate_Ku (sess.securityAuthProto,
868 sess.securityAuthProtoLen,
869 (u_char *) host->priv_passphrase,
870 strlen(host->priv_passphrase),
871 sess.securityPrivKey,
872 &sess.securityPrivKeyLen);
873 if (error != SNMPERR_SUCCESS) {
874 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
878 if (host->context != NULL)
880 sess.contextName = host->context;
881 sess.contextNameLen = strlen (host->context);
884 else /* SNMPv1/2 "authenticates" with community string */
886 sess.community = (u_char *) host->community;
887 sess.community_len = strlen (host->community);
890 /* snmp_sess_open will copy the `struct snmp_session *'. */
891 host->sess_handle = snmp_sess_open (&sess);
893 if (host->sess_handle == NULL)
897 snmp_error (&sess, NULL, NULL, &errstr);
899 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
900 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
903 } /* void csnmp_host_open_session */
905 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
906 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
907 double scale, double shift,
908 const char *host_name, const char *data_name)
911 uint64_t tmp_unsigned = 0;
912 int64_t tmp_signed = 0;
914 /* Set to true when the original SNMP type appears to have been signed. */
915 _Bool prefer_signed = 0;
917 if ((vl->type == ASN_INTEGER)
918 || (vl->type == ASN_UINTEGER)
919 || (vl->type == ASN_COUNTER)
921 || (vl->type == ASN_TIMETICKS)
923 || (vl->type == ASN_GAUGE))
925 tmp_unsigned = (uint32_t) *vl->val.integer;
926 tmp_signed = (int32_t) *vl->val.integer;
928 if ((vl->type == ASN_INTEGER)
929 || (vl->type == ASN_GAUGE))
932 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
934 else if (vl->type == ASN_COUNTER64)
936 tmp_unsigned = (uint32_t) vl->val.counter64->high;
937 tmp_unsigned = tmp_unsigned << 32;
938 tmp_unsigned += (uint32_t) vl->val.counter64->low;
939 tmp_signed = (int64_t) tmp_unsigned;
940 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
942 else if (vl->type == ASN_OCTET_STR)
944 /* We'll handle this later.. */
948 char oid_buffer[1024];
950 memset (oid_buffer, 0, sizeof (oid_buffer));
951 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
952 vl->name, vl->name_length);
955 if (vl->type == ASN_NULL)
956 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
960 WARNING ("snmp plugin: I don't know the ASN type #%i "
961 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
962 (int) vl->type, oid_buffer,
963 (data_name != NULL) ? data_name : "UNKNOWN",
964 (host_name != NULL) ? host_name : "UNKNOWN");
969 if (vl->type == ASN_OCTET_STR)
973 if (vl->val.string != NULL)
976 size_t string_length;
978 string_length = sizeof (string) - 1;
979 if (vl->val_len < string_length)
980 string_length = vl->val_len;
982 /* The strings we get from the Net-SNMP library may not be null
983 * terminated. That is why we're using `memcpy' here and not `strcpy'.
984 * `string_length' is set to `vl->val_len' which holds the length of the
986 memcpy (string, vl->val.string, string_length);
987 string[string_length] = 0;
989 status = parse_value (string, &ret, type);
992 ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
993 (host_name != NULL) ? host_name : "UNKNOWN",
994 DS_TYPE_TO_STRING (type), string);
1002 case DS_TYPE_COUNTER:
1003 case DS_TYPE_DERIVE:
1004 case DS_TYPE_ABSOLUTE:
1005 memset (&ret, 0, sizeof (ret));
1013 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
1014 "data source type: %i.", type);
1018 } /* if (vl->type == ASN_OCTET_STR) */
1019 else if (type == DS_TYPE_COUNTER)
1021 ret.counter = tmp_unsigned;
1023 else if (type == DS_TYPE_GAUGE)
1027 else if (prefer_signed)
1028 ret.gauge = (scale * tmp_signed) + shift;
1030 ret.gauge = (scale * tmp_unsigned) + shift;
1032 else if (type == DS_TYPE_DERIVE)
1035 ret.derive = (derive_t) tmp_signed;
1037 ret.derive = (derive_t) tmp_unsigned;
1039 else if (type == DS_TYPE_ABSOLUTE)
1041 ret.absolute = (absolute_t) tmp_unsigned;
1045 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1051 } /* value_t csnmp_value_list_to_value */
1053 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
1054 const struct variable_list *vb, size_t dst_size)
1061 buffer_free = dst_size;
1063 for (i = 0; i < vb->val_len; i++)
1067 status = snprintf (buffer_ptr, buffer_free,
1068 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1070 if (status >= buffer_free)
1072 buffer_ptr += (buffer_free - 1);
1074 return (dst_size + (buffer_free - status));
1076 else /* if (status < buffer_free) */
1078 buffer_ptr += status;
1079 buffer_free -= status;
1083 return ((int) (dst_size - buffer_free));
1084 } /* }}} int csnmp_strvbcopy_hexstring */
1086 static int csnmp_strvbcopy (char *dst, /* {{{ */
1087 const struct variable_list *vb, size_t dst_size)
1093 if (vb->type == ASN_OCTET_STR)
1094 src = (char *) vb->val.string;
1095 else if (vb->type == ASN_BIT_STR)
1096 src = (char *) vb->val.bitstring;
1103 num_chars = dst_size - 1;
1104 if (num_chars > vb->val_len)
1105 num_chars = vb->val_len;
1107 for (i = 0; i < num_chars; i++)
1109 /* Check for control characters. */
1110 if ((unsigned char)src[i] < 32)
1111 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1116 return ((int) vb->val_len);
1117 } /* }}} int csnmp_strvbcopy */
1119 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1120 csnmp_list_instances_t **tail,
1121 const struct snmp_pdu *res,
1122 const host_definition_t *hd, const data_definition_t *dd)
1124 csnmp_list_instances_t *il;
1125 struct variable_list *vb;
1129 uint32_t is_matched;
1131 /* Set vb on the last variable */
1132 for (vb = res->variables;
1133 (vb != NULL) && (vb->next_variable != NULL);
1134 vb = vb->next_variable)
1139 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1141 il = malloc (sizeof (*il));
1144 ERROR ("snmp plugin: malloc failed.");
1147 memset (il, 0, sizeof (*il));
1150 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1157 /* Get instance name */
1158 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1162 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1164 for (i = 0; i < dd->ignores_len; i++)
1166 status = fnmatch(dd->ignores[i], il->instance, 0);
1169 if (dd->invert_match == 0)
1181 if (dd->invert_match != 0 && is_matched == 0)
1186 for (ptr = il->instance; *ptr != '\0'; ptr++)
1188 if ((*ptr > 0) && (*ptr < 32))
1190 else if (*ptr == '/')
1193 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1197 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1198 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1199 ssnprintf (il->instance, sizeof (il->instance),
1200 "%llu", val.counter);
1203 /* TODO: Debugging output */
1212 } /* int csnmp_instance_list_add */
1214 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1215 csnmp_list_instances_t *instance_list,
1216 csnmp_table_values_t **value_table)
1218 const data_set_t *ds;
1219 value_list_t vl = VALUE_LIST_INIT;
1221 csnmp_list_instances_t *instance_list_ptr;
1222 csnmp_table_values_t **value_table_ptr;
1226 oid_t current_suffix;
1228 ds = plugin_get_ds (data->type);
1231 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1234 assert (ds->ds_num == data->values_len);
1236 instance_list_ptr = instance_list;
1238 value_table_ptr = malloc (sizeof (*value_table_ptr) * data->values_len);
1239 if (value_table_ptr == NULL)
1241 for (i = 0; i < data->values_len; i++)
1242 value_table_ptr[i] = value_table[i];
1244 vl.values_len = ds->ds_num;
1245 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1246 if (vl.values == NULL)
1248 ERROR ("snmp plugin: malloc failed.");
1249 sfree (value_table_ptr);
1253 sstrncpy (vl.host, host->name, sizeof (vl.host));
1254 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1256 vl.interval = host->interval;
1259 memset (¤t_suffix, 0, sizeof (current_suffix));
1262 _Bool suffix_skipped = 0;
1264 /* Determine next suffix to handle. */
1265 if (instance_list != NULL)
1267 if (instance_list_ptr == NULL)
1273 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1275 else /* no instance configured */
1277 csnmp_table_values_t *ptr = value_table_ptr[0];
1284 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1287 /* Update all the value_table_ptr to point at the entry with the same
1288 * trailing partial OID */
1289 for (i = 0; i < data->values_len; i++)
1291 while ((value_table_ptr[i] != NULL)
1292 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1293 value_table_ptr[i] = value_table_ptr[i]->next;
1295 if (value_table_ptr[i] == NULL)
1300 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1302 /* This suffix is missing in the subtree. Indicate this with the
1303 * "suffix_skipped" flag and try the next instance / suffix. */
1307 } /* for (i = 0; i < columns; i++) */
1312 /* Matching the values failed. Start from the beginning again. */
1315 if (instance_list != NULL)
1316 instance_list_ptr = instance_list_ptr->next;
1318 value_table_ptr[0] = value_table_ptr[0]->next;
1323 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1324 * to the same subid. instance_list_ptr is either NULL or points to the
1325 * same subid, too. */
1327 for (i = 1; i < data->values_len; i++)
1329 assert (value_table_ptr[i] != NULL);
1330 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1331 &value_table_ptr[i]->suffix) == 0);
1333 assert ((instance_list_ptr == NULL)
1334 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1335 &value_table_ptr[0]->suffix) == 0));
1338 sstrncpy (vl.type, data->type, sizeof (vl.type));
1341 char temp[DATA_MAX_NAME_LEN];
1343 if (instance_list_ptr == NULL)
1344 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1346 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1348 if (data->instance_prefix == NULL)
1349 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1351 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1352 data->instance_prefix, temp);
1355 for (i = 0; i < data->values_len; i++)
1356 vl.values[i] = value_table_ptr[i]->value;
1358 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1359 plugin_dispatch_values (&vl);
1361 if (instance_list != NULL)
1362 instance_list_ptr = instance_list_ptr->next;
1364 value_table_ptr[0] = value_table_ptr[0]->next;
1365 } /* while (have_more) */
1368 sfree (value_table_ptr);
1371 } /* int csnmp_dispatch_table */
1373 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1375 struct snmp_pdu *req;
1376 struct snmp_pdu *res;
1377 struct variable_list *vb;
1379 const data_set_t *ds;
1381 uint32_t oid_list_len = (uint32_t) (data->values_len + 1);
1382 /* Holds the last OID returned by the device. We use this in the GETNEXT
1383 * request to proceed. */
1384 oid_t oid_list[oid_list_len];
1385 /* Set to false when an OID has left its subtree so we don't re-request it
1387 _Bool oid_list_todo[oid_list_len];
1393 /* `value_list_head' and `value_list_tail' implement a linked list for each
1394 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1395 * instance names. This is used to jump gaps in the table. */
1396 csnmp_list_instances_t *instance_list_head;
1397 csnmp_list_instances_t *instance_list_tail;
1398 csnmp_table_values_t **value_list_head;
1399 csnmp_table_values_t **value_list_tail;
1401 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1402 host->name, data->name);
1404 if (host->sess_handle == NULL)
1406 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1410 ds = plugin_get_ds (data->type);
1413 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1417 if (ds->ds_num != data->values_len)
1419 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1420 data->type, ds->ds_num, data->values_len);
1424 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1425 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1426 if (data->instance.oid.oid_len > 0)
1427 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1428 else /* no InstanceFrom option specified. */
1431 for (j = 0; j < oid_list_len; j++)
1432 oid_list_todo[j] = 1;
1434 /* We're going to construct n linked lists, one for each "value".
1435 * value_list_head will contain pointers to the heads of these linked lists,
1436 * value_list_tail will contain pointers to the tail of the lists. */
1437 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1438 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1439 if ((value_list_head == NULL) || (value_list_tail == NULL))
1441 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1442 sfree (value_list_head);
1443 sfree (value_list_tail);
1447 instance_list_head = NULL;
1448 instance_list_tail = NULL;
1453 int oid_list_todo_num;
1455 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1458 ERROR ("snmp plugin: snmp_pdu_create failed.");
1463 oid_list_todo_num = 0;
1464 for (j = 0; j < oid_list_len; j++)
1466 /* Do not rerequest already finished OIDs */
1467 if (!oid_list_todo[j])
1469 oid_list_todo_num++;
1470 snmp_add_null_var (req, oid_list[j].oid, oid_list[j].oid_len);
1473 if (oid_list_todo_num == 0)
1475 /* The request is still empty - so we are finished */
1476 DEBUG ("snmp plugin: all variables have left their subtree");
1482 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1483 if ((status != STAT_SUCCESS) || (res == NULL))
1485 char *errstr = NULL;
1487 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1489 c_complain (LOG_ERR, &host->complaint,
1490 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1491 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1494 snmp_free_pdu (res);
1497 /* snmp_synch_response already freed our PDU */
1500 csnmp_host_close_session (host);
1507 assert (res != NULL);
1508 c_release (LOG_INFO, &host->complaint,
1509 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1512 vb = res->variables;
1519 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1521 /* Calculate value index from todo list */
1522 while (!oid_list_todo[i] && (i < oid_list_len))
1525 /* An instance is configured and the res variable we process is the
1526 * instance value (last index) */
1527 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1529 if ((vb->type == SNMP_ENDOFMIBVIEW)
1530 || (snmp_oid_ncompare (data->instance.oid.oid,
1531 data->instance.oid.oid_len,
1532 vb->name, vb->name_length,
1533 data->instance.oid.oid_len) != 0))
1535 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1536 host->name, data->name);
1537 oid_list_todo[i] = 0;
1541 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1542 * add it to the list */
1543 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1544 res, host, data) != 0)
1546 ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
1552 else /* The variable we are processing is a normal value */
1554 csnmp_table_values_t *vt;
1559 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1561 /* Calculate the current suffix. This is later used to check that the
1562 * suffix is increasing. This also checks if we left the subtree */
1563 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1566 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1567 "Value probably left its subtree.",
1568 host->name, data->name, i);
1569 oid_list_todo[i] = 0;
1573 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1574 * table matching algorithm will get confused. */
1575 if ((value_list_tail[i] != NULL)
1576 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1578 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1579 "Suffix is not increasing.",
1580 host->name, data->name, i);
1581 oid_list_todo[i] = 0;
1585 vt = malloc (sizeof (*vt));
1588 ERROR ("snmp plugin: malloc failed.");
1592 memset (vt, 0, sizeof (*vt));
1594 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1595 data->scale, data->shift, host->name, data->name);
1596 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1599 if (value_list_tail[i] == NULL)
1600 value_list_head[i] = vt;
1602 value_list_tail[i]->next = vt;
1603 value_list_tail[i] = vt;
1606 /* Copy OID to oid_list[i] */
1607 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1608 oid_list[i].oid_len = vb->name_length;
1610 } /* for (vb = res->variables ...) */
1613 snmp_free_pdu (res);
1615 } /* while (status == 0) */
1618 snmp_free_pdu (res);
1622 snmp_free_pdu (req);
1626 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1628 /* Free all allocated variables here */
1629 while (instance_list_head != NULL)
1631 csnmp_list_instances_t *next = instance_list_head->next;
1632 sfree (instance_list_head);
1633 instance_list_head = next;
1636 for (i = 0; i < data->values_len; i++)
1638 while (value_list_head[i] != NULL)
1640 csnmp_table_values_t *next = value_list_head[i]->next;
1641 sfree (value_list_head[i]);
1642 value_list_head[i] = next;
1646 sfree (value_list_head);
1647 sfree (value_list_tail);
1650 } /* int csnmp_read_table */
1652 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1654 struct snmp_pdu *req;
1655 struct snmp_pdu *res;
1656 struct variable_list *vb;
1658 const data_set_t *ds;
1659 value_list_t vl = VALUE_LIST_INIT;
1664 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1665 host->name, data->name);
1667 if (host->sess_handle == NULL)
1669 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1673 ds = plugin_get_ds (data->type);
1676 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1680 if (ds->ds_num != data->values_len)
1682 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1683 data->type, ds->ds_num, data->values_len);
1687 vl.values_len = ds->ds_num;
1688 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1689 if (vl.values == NULL)
1691 for (i = 0; i < vl.values_len; i++)
1693 if (ds->ds[i].type == DS_TYPE_COUNTER)
1694 vl.values[i].counter = 0;
1696 vl.values[i].gauge = NAN;
1699 sstrncpy (vl.host, host->name, sizeof (vl.host));
1700 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1701 sstrncpy (vl.type, data->type, sizeof (vl.type));
1702 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1704 vl.interval = host->interval;
1706 req = snmp_pdu_create (SNMP_MSG_GET);
1709 ERROR ("snmp plugin: snmp_pdu_create failed.");
1714 for (i = 0; i < data->values_len; i++)
1715 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1718 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1720 if ((status != STAT_SUCCESS) || (res == NULL))
1722 char *errstr = NULL;
1724 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1725 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1726 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1729 snmp_free_pdu (res);
1733 csnmp_host_close_session (host);
1739 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1743 snprint_variable (buffer, sizeof (buffer),
1744 vb->name, vb->name_length, vb);
1745 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1746 #endif /* COLLECT_DEBUG */
1748 for (i = 0; i < data->values_len; i++)
1749 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1750 vb->name, vb->name_length) == 0)
1751 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1752 data->scale, data->shift, host->name, data->name);
1753 } /* for (res->variables) */
1756 snmp_free_pdu (res);
1759 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1760 plugin_dispatch_values (&vl);
1764 } /* int csnmp_read_value */
1766 static int csnmp_read_host (user_data_t *ud)
1768 host_definition_t *host;
1769 cdtime_t time_start;
1777 if (host->interval == 0)
1778 host->interval = plugin_get_interval ();
1780 time_start = cdtime ();
1782 if (host->sess_handle == NULL)
1783 csnmp_host_open_session (host);
1785 if (host->sess_handle == NULL)
1789 for (i = 0; i < host->data_list_len; i++)
1791 data_definition_t *data = host->data_list[i];
1794 status = csnmp_read_table (host, data);
1796 status = csnmp_read_value (host, data);
1802 time_end = cdtime ();
1803 if ((time_end - time_start) > host->interval)
1805 WARNING ("snmp plugin: Host `%s' should be queried every %.3f "
1806 "seconds, but reading all values takes %.3f seconds.",
1808 CDTIME_T_TO_DOUBLE (host->interval),
1809 CDTIME_T_TO_DOUBLE (time_end - time_start));
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