2 * collectd - src/snmp.c
3 * Copyright (C) 2007-2012 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
25 #include "utils_complain.h"
29 #include <net-snmp/net-snmp-config.h>
30 #include <net-snmp/net-snmp-includes.h>
33 * Private data structes
40 typedef struct oid_s oid_t;
44 char string[DATA_MAX_NAME_LEN];
47 typedef union instance_u instance_t;
49 struct data_definition_s
51 char *name; /* used to reference this from the `Collect' option */
52 char *type; /* used to find the data_set */
55 char *instance_prefix;
60 struct data_definition_s *next;
62 typedef struct data_definition_s data_definition_t;
64 struct host_definition_s
70 /* snmpv1/2 options */
73 /* snmpv3 security options */
76 size_t auth_protocol_len;
77 char *auth_passphrase;
79 size_t priv_protocol_len;
80 char *priv_passphrase;
85 c_complain_t complaint;
87 data_definition_t **data_list;
90 typedef struct host_definition_s host_definition_t;
92 /* These two types are used to cache values in `csnmp_read_table' to handle
94 struct csnmp_list_instances_s
97 char instance[DATA_MAX_NAME_LEN];
98 struct csnmp_list_instances_s *next;
100 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
102 struct csnmp_table_values_s
106 struct csnmp_table_values_s *next;
108 typedef struct csnmp_table_values_s csnmp_table_values_t;
113 static data_definition_t *data_head = NULL;
118 static int csnmp_read_host (user_data_t *ud);
123 static void csnmp_oid_init (oid_t *dst, oid const *src, size_t n)
125 assert (n <= STATIC_ARRAY_SIZE (dst->oid));
126 memcpy (dst->oid, src, sizeof (*src) * n);
130 static int csnmp_oid_compare (oid_t const *left, oid_t const *right)
132 return (snmp_oid_compare (left->oid, left->oid_len,
133 right->oid, right->oid_len));
136 static int csnmp_oid_suffix (oid_t *dst, oid_t const *src,
139 /* Make sure "src" is in "root"s subtree. */
140 if (src->oid_len <= root->oid_len)
142 if (snmp_oid_ncompare (root->oid, root->oid_len,
143 src->oid, src->oid_len,
144 /* n = */ root->oid_len) != 0)
147 memset (dst, 0, sizeof (*dst));
148 dst->oid_len = src->oid_len - root->oid_len;
149 memcpy (dst->oid, &src->oid[root->oid_len],
150 dst->oid_len * sizeof (dst->oid[0]));
154 static int csnmp_oid_to_string (char *buffer, size_t buffer_size,
157 char oid_str[MAX_OID_LEN][16];
158 char *oid_str_ptr[MAX_OID_LEN];
161 for (i = 0; i < o->oid_len; i++)
163 ssnprintf (oid_str[i], sizeof (oid_str[i]), "%lu", (unsigned long) o->oid[i]);
164 oid_str_ptr[i] = oid_str[i];
167 return (strjoin (buffer, buffer_size,
168 oid_str_ptr, o->oid_len, /* separator = */ "."));
171 static void csnmp_host_close_session (host_definition_t *host) /* {{{ */
173 if (host->sess_handle == NULL)
176 snmp_sess_close (host->sess_handle);
177 host->sess_handle = NULL;
178 } /* }}} void csnmp_host_close_session */
180 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
182 host_definition_t *hd;
189 if (hd->name != NULL)
191 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
195 csnmp_host_close_session (hd);
199 sfree (hd->community);
200 sfree (hd->username);
201 sfree (hd->auth_passphrase);
202 sfree (hd->priv_passphrase);
204 sfree (hd->data_list);
207 } /* }}} void csnmp_host_definition_destroy */
209 /* Many functions to handle the configuration. {{{ */
210 /* First there are many functions which do configuration stuff. It's a big
211 * bloated and messy, I'm afraid. */
214 * Callgraph for the config stuff:
216 * +-> call_snmp_init_once
217 * +-> csnmp_config_add_data
218 * ! +-> csnmp_config_add_data_instance
219 * ! +-> csnmp_config_add_data_instance_prefix
220 * ! +-> csnmp_config_add_data_values
221 * +-> csnmp_config_add_host
222 * +-> csnmp_config_add_host_version
223 * +-> csnmp_config_add_host_collect
224 * +-> csnmp_config_add_host_auth_protocol
225 * +-> csnmp_config_add_host_priv_protocol
226 * +-> csnmp_config_add_host_security_level
228 static void call_snmp_init_once (void)
230 static int have_init = 0;
233 init_snmp (PACKAGE_NAME);
235 } /* void call_snmp_init_once */
237 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
239 char buffer[DATA_MAX_NAME_LEN];
242 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
248 /* Instance is an OID */
249 dd->instance.oid.oid_len = MAX_OID_LEN;
251 if (!read_objid (buffer,
252 dd->instance.oid.oid, &dd->instance.oid.oid_len))
254 ERROR ("snmp plugin: read_objid (%s) failed.", buffer);
260 /* Instance is a simple string */
261 sstrncpy (dd->instance.string, buffer,
262 sizeof (dd->instance.string));
266 } /* int csnmp_config_add_data_instance */
268 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
275 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
276 "is set to `false'.", dd->name);
280 status = cf_util_get_string(ci, &dd->instance_prefix);
282 } /* int csnmp_config_add_data_instance_prefix */
284 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
288 if (ci->values_num < 1)
290 WARNING ("snmp plugin: `Values' needs at least one argument.");
294 for (i = 0; i < ci->values_num; i++)
295 if (ci->values[i].type != OCONFIG_TYPE_STRING)
297 WARNING ("snmp plugin: `Values' needs only string argument.");
303 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
304 if (dd->values == NULL)
306 dd->values_len = ci->values_num;
308 for (i = 0; i < ci->values_num; i++)
310 dd->values[i].oid_len = MAX_OID_LEN;
312 if (NULL == snmp_parse_oid (ci->values[i].value.string,
313 dd->values[i].oid, &dd->values[i].oid_len))
315 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
316 ci->values[i].value.string);
325 } /* int csnmp_config_add_data_instance */
327 static int csnmp_config_add_data (oconfig_item_t *ci)
329 data_definition_t *dd;
333 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
336 memset (dd, '\0', sizeof (data_definition_t));
338 status = cf_util_get_string(ci, &dd->name);
348 for (i = 0; i < ci->children_num; i++)
350 oconfig_item_t *option = ci->children + i;
353 if (strcasecmp ("Type", option->key) == 0)
354 status = cf_util_get_string(option, &dd->type);
355 else if (strcasecmp ("Table", option->key) == 0)
356 status = cf_util_get_boolean(option, &dd->is_table);
357 else if (strcasecmp ("Instance", option->key) == 0)
358 status = csnmp_config_add_data_instance (dd, option);
359 else if (strcasecmp ("InstancePrefix", option->key) == 0)
360 status = csnmp_config_add_data_instance_prefix (dd, option);
361 else if (strcasecmp ("Values", option->key) == 0)
362 status = csnmp_config_add_data_values (dd, option);
363 else if (strcasecmp ("Shift", option->key) == 0)
364 status = cf_util_get_double(option, &dd->shift);
365 else if (strcasecmp ("Scale", option->key) == 0)
366 status = cf_util_get_double(option, &dd->scale);
369 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
375 } /* for (ci->children) */
379 if (dd->type == NULL)
381 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
385 if (dd->values == NULL)
387 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
393 } /* while (status == 0) */
398 sfree (dd->instance_prefix);
404 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
405 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
407 if (data_head == NULL)
411 data_definition_t *last;
413 while (last->next != NULL)
419 } /* int csnmp_config_add_data */
421 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
425 if ((ci->values_num != 1)
426 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
428 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
432 version = (int) ci->values[0].value.number;
433 if ((version < 1) || (version > 3))
435 WARNING ("snmp plugin: `Version' must either be `1', `2', or `3'.");
439 hd->version = version;
442 } /* int csnmp_config_add_host_address */
444 static int csnmp_config_add_host_collect (host_definition_t *host,
447 data_definition_t *data;
448 data_definition_t **data_list;
452 if (ci->values_num < 1)
454 WARNING ("snmp plugin: `Collect' needs at least one argument.");
458 for (i = 0; i < ci->values_num; i++)
459 if (ci->values[i].type != OCONFIG_TYPE_STRING)
461 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
465 data_list_len = host->data_list_len + ci->values_num;
466 data_list = (data_definition_t **) realloc (host->data_list,
467 sizeof (data_definition_t *) * data_list_len);
468 if (data_list == NULL)
470 host->data_list = data_list;
472 for (i = 0; i < ci->values_num; i++)
474 for (data = data_head; data != NULL; data = data->next)
475 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
480 WARNING ("snmp plugin: No such data configured: `%s'",
481 ci->values[i].value.string);
485 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
486 host->name, host->data_list_len, data->name);
488 host->data_list[host->data_list_len] = data;
489 host->data_list_len++;
490 } /* for (values_num) */
493 } /* int csnmp_config_add_host_collect */
495 static int csnmp_config_add_host_auth_protocol (host_definition_t *hd, oconfig_item_t *ci)
500 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
504 if (strcasecmp("MD5", buffer) == 0) {
505 hd->auth_protocol = usmHMACMD5AuthProtocol;
506 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
508 else if (strcasecmp("SHA", buffer) == 0) {
509 hd->auth_protocol = usmHMACSHA1AuthProtocol;
510 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol)/sizeof(oid);
514 WARNING ("snmp plugin: The `AuthProtocol' config option must be `MD5' or `SHA'.");
518 DEBUG ("snmp plugin: host = %s; host->auth_protocol = %s;",
519 hd->name, hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
522 } /* int csnmp_config_add_host_auth_protocol */
524 static int csnmp_config_add_host_priv_protocol (host_definition_t *hd, oconfig_item_t *ci)
529 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
533 if (strcasecmp("AES", buffer) == 0)
535 hd->priv_protocol = usmAESPrivProtocol;
536 hd->priv_protocol_len = sizeof(usmAESPrivProtocol)/sizeof(oid);
538 else if (strcasecmp("DES", buffer) == 0) {
539 hd->priv_protocol = usmDESPrivProtocol;
540 hd->priv_protocol_len = sizeof(usmDESPrivProtocol)/sizeof(oid);
544 WARNING ("snmp plugin: The `PrivProtocol' config option must be `AES' or `DES'.");
548 DEBUG ("snmp plugin: host = %s; host->priv_protocol = %s;",
549 hd->name, hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
552 } /* int csnmp_config_add_host_priv_protocol */
554 static int csnmp_config_add_host_security_level (host_definition_t *hd, oconfig_item_t *ci)
559 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
563 if (strcasecmp("noAuthNoPriv", buffer) == 0)
564 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
565 else if (strcasecmp("authNoPriv", buffer) == 0)
566 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
567 else if (strcasecmp("authPriv", buffer) == 0)
568 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
571 WARNING ("snmp plugin: The `SecurityLevel' config option must be `noAuthNoPriv', `authNoPriv', or `authPriv'.");
575 DEBUG ("snmp plugin: host = %s; host->security_level = %d;",
576 hd->name, hd->security_level);
579 } /* int csnmp_config_add_host_security_level */
581 static int csnmp_config_add_host (oconfig_item_t *ci)
583 host_definition_t *hd;
587 /* Registration stuff. */
588 char cb_name[DATA_MAX_NAME_LEN];
590 struct timespec cb_interval;
592 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
595 memset (hd, '\0', sizeof (host_definition_t));
597 C_COMPLAIN_INIT (&hd->complaint);
599 status = cf_util_get_string(ci, &hd->name);
603 hd->sess_handle = NULL;
606 for (i = 0; i < ci->children_num; i++)
608 oconfig_item_t *option = ci->children + i;
611 if (strcasecmp ("Address", option->key) == 0)
612 status = cf_util_get_string(option, &hd->address);
613 else if (strcasecmp ("Community", option->key) == 0)
614 status = cf_util_get_string(option, &hd->community);
615 else if (strcasecmp ("Version", option->key) == 0)
616 status = csnmp_config_add_host_version (hd, option);
617 else if (strcasecmp ("Collect", option->key) == 0)
618 csnmp_config_add_host_collect (hd, option);
619 else if (strcasecmp ("Interval", option->key) == 0)
620 cf_util_get_cdtime (option, &hd->interval);
621 else if (strcasecmp ("Username", option->key) == 0)
622 status = cf_util_get_string(option, &hd->username);
623 else if (strcasecmp ("AuthProtocol", option->key) == 0)
624 status = csnmp_config_add_host_auth_protocol (hd, option);
625 else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
626 status = csnmp_config_add_host_priv_protocol (hd, option);
627 else if (strcasecmp ("AuthPassphrase", option->key) == 0)
628 status = cf_util_get_string(option, &hd->auth_passphrase);
629 else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
630 status = cf_util_get_string(option, &hd->priv_passphrase);
631 else if (strcasecmp ("SecurityLevel", option->key) == 0)
632 status = csnmp_config_add_host_security_level (hd, option);
633 else if (strcasecmp ("Context", option->key) == 0)
634 status = cf_util_get_string(option, &hd->context);
637 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
643 } /* for (ci->children) */
647 if (hd->address == NULL)
649 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
653 if (hd->community == NULL && hd->version < 3)
655 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
659 if (hd->version == 3)
661 if (hd->username == NULL)
663 WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
667 if (hd->security_level == 0)
669 WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
673 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
675 if (hd->auth_protocol == NULL)
677 WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
681 if (hd->auth_passphrase == NULL)
683 WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
688 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
690 if (hd->priv_protocol == NULL)
692 WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
696 if (hd->priv_passphrase == NULL)
698 WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
706 } /* while (status == 0) */
710 csnmp_host_definition_destroy (hd);
714 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
715 hd->name, hd->address, hd->community, hd->version);
717 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
719 memset (&cb_data, 0, sizeof (cb_data));
721 cb_data.free_func = csnmp_host_definition_destroy;
723 CDTIME_T_TO_TIMESPEC (hd->interval, &cb_interval);
725 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
726 csnmp_read_host, /* interval = */ &cb_interval,
727 /* user_data = */ &cb_data);
730 ERROR ("snmp plugin: Registering complex read function failed.");
731 csnmp_host_definition_destroy (hd);
736 } /* int csnmp_config_add_host */
738 static int csnmp_config (oconfig_item_t *ci)
742 call_snmp_init_once ();
744 for (i = 0; i < ci->children_num; i++)
746 oconfig_item_t *child = ci->children + i;
747 if (strcasecmp ("Data", child->key) == 0)
748 csnmp_config_add_data (child);
749 else if (strcasecmp ("Host", child->key) == 0)
750 csnmp_config_add_host (child);
753 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
755 } /* for (ci->children) */
758 } /* int csnmp_config */
760 /* }}} End of the config stuff. Now the interesting part begins */
762 static void csnmp_host_open_session (host_definition_t *host)
764 struct snmp_session sess;
767 if (host->sess_handle != NULL)
768 csnmp_host_close_session (host);
770 snmp_sess_init (&sess);
771 sess.peername = host->address;
772 switch (host->version)
775 sess.version = SNMP_VERSION_1;
778 sess.version = SNMP_VERSION_3;
781 sess.version = SNMP_VERSION_2c;
785 if (host->version == 3)
787 sess.securityName = host->username;
788 sess.securityNameLen = strlen (host->username);
789 sess.securityLevel = host->security_level;
791 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
793 sess.securityAuthProto = host->auth_protocol;
794 sess.securityAuthProtoLen = host->auth_protocol_len;
795 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
796 error = generate_Ku (sess.securityAuthProto,
797 sess.securityAuthProtoLen,
798 (u_char *) host->auth_passphrase,
799 strlen(host->auth_passphrase),
800 sess.securityAuthKey,
801 &sess.securityAuthKeyLen);
802 if (error != SNMPERR_SUCCESS) {
803 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
807 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
809 sess.securityPrivProto = host->priv_protocol;
810 sess.securityPrivProtoLen = host->priv_protocol_len;
811 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
812 error = generate_Ku (sess.securityAuthProto,
813 sess.securityAuthProtoLen,
814 (u_char *) host->priv_passphrase,
815 strlen(host->priv_passphrase),
816 sess.securityPrivKey,
817 &sess.securityPrivKeyLen);
818 if (error != SNMPERR_SUCCESS) {
819 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
823 if (host->context != NULL)
825 sess.contextName = host->context;
826 sess.contextNameLen = strlen (host->context);
829 else /* SNMPv1/2 "authenticates" with community string */
831 sess.community = (u_char *) host->community;
832 sess.community_len = strlen (host->community);
835 /* snmp_sess_open will copy the `struct snmp_session *'. */
836 host->sess_handle = snmp_sess_open (&sess);
838 if (host->sess_handle == NULL)
842 snmp_error (&sess, NULL, NULL, &errstr);
844 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
845 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
848 } /* void csnmp_host_open_session */
850 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
851 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
852 double scale, double shift,
853 const char *host_name, const char *data_name)
856 uint64_t tmp_unsigned = 0;
857 int64_t tmp_signed = 0;
859 /* Set to true when the original SNMP type appears to have been signed. */
860 _Bool prefer_signed = 0;
862 if ((vl->type == ASN_INTEGER)
863 || (vl->type == ASN_UINTEGER)
864 || (vl->type == ASN_COUNTER)
866 || (vl->type == ASN_TIMETICKS)
868 || (vl->type == ASN_GAUGE))
870 tmp_unsigned = (uint32_t) *vl->val.integer;
871 tmp_signed = (int32_t) *vl->val.integer;
873 if ((vl->type == ASN_INTEGER)
874 || (vl->type == ASN_GAUGE))
877 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
879 else if (vl->type == ASN_COUNTER64)
881 tmp_unsigned = (uint32_t) vl->val.counter64->high;
882 tmp_unsigned = tmp_unsigned << 32;
883 tmp_unsigned += (uint32_t) vl->val.counter64->low;
884 tmp_signed = (int64_t) tmp_unsigned;
885 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
887 else if (vl->type == ASN_OCTET_STR)
889 /* We'll handle this later.. */
893 char oid_buffer[1024];
895 memset (oid_buffer, 0, sizeof (oid_buffer));
896 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
897 vl->name, vl->name_length);
900 if (vl->type == ASN_NULL)
901 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
905 WARNING ("snmp plugin: I don't know the ASN type #%i "
906 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
907 (int) vl->type, oid_buffer,
908 (data_name != NULL) ? data_name : "UNKNOWN",
909 (host_name != NULL) ? host_name : "UNKNOWN");
914 if (vl->type == ASN_OCTET_STR)
918 if (vl->val.string != NULL)
921 size_t string_length;
923 string_length = sizeof (string) - 1;
924 if (vl->val_len < string_length)
925 string_length = vl->val_len;
927 /* The strings we get from the Net-SNMP library may not be null
928 * terminated. That is why we're using `memcpy' here and not `strcpy'.
929 * `string_length' is set to `vl->val_len' which holds the length of the
931 memcpy (string, vl->val.string, string_length);
932 string[string_length] = 0;
934 status = parse_value (string, &ret, type);
937 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
938 DS_TYPE_TO_STRING (type), string);
946 case DS_TYPE_COUNTER:
948 case DS_TYPE_ABSOLUTE:
949 memset (&ret, 0, sizeof (ret));
957 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
958 "data source type: %i.", type);
962 } /* if (vl->type == ASN_OCTET_STR) */
963 else if (type == DS_TYPE_COUNTER)
965 ret.counter = tmp_unsigned;
967 else if (type == DS_TYPE_GAUGE)
971 else if (prefer_signed)
972 ret.gauge = (scale * tmp_signed) + shift;
974 ret.gauge = (scale * tmp_unsigned) + shift;
976 else if (type == DS_TYPE_DERIVE)
979 ret.derive = (derive_t) tmp_signed;
981 ret.derive = (derive_t) tmp_unsigned;
983 else if (type == DS_TYPE_ABSOLUTE)
985 ret.absolute = (absolute_t) tmp_unsigned;
989 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
995 } /* value_t csnmp_value_list_to_value */
997 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
998 const struct variable_list *vb, size_t dst_size)
1005 buffer_free = dst_size;
1007 for (i = 0; i < vb->val_len; i++)
1011 status = snprintf (buffer_ptr, buffer_free,
1012 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1014 if (status >= buffer_free)
1016 buffer_ptr += (buffer_free - 1);
1018 return (dst_size + (buffer_free - status));
1020 else /* if (status < buffer_free) */
1022 buffer_ptr += status;
1023 buffer_free -= status;
1027 return ((int) (dst_size - buffer_free));
1028 } /* }}} int csnmp_strvbcopy_hexstring */
1030 static int csnmp_strvbcopy (char *dst, /* {{{ */
1031 const struct variable_list *vb, size_t dst_size)
1037 if (vb->type == ASN_OCTET_STR)
1038 src = (char *) vb->val.string;
1039 else if (vb->type == ASN_BIT_STR)
1040 src = (char *) vb->val.bitstring;
1047 num_chars = dst_size - 1;
1048 if (num_chars > vb->val_len)
1049 num_chars = vb->val_len;
1051 for (i = 0; i < num_chars; i++)
1053 /* Check for control characters. */
1054 if ((unsigned char)src[i] < 32)
1055 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1060 return ((int) vb->val_len);
1061 } /* }}} int csnmp_strvbcopy */
1063 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1064 csnmp_list_instances_t **tail,
1065 const struct snmp_pdu *res,
1066 const host_definition_t *hd, const data_definition_t *dd)
1068 csnmp_list_instances_t *il;
1069 struct variable_list *vb;
1073 /* Set vb on the last variable */
1074 for (vb = res->variables;
1075 (vb != NULL) && (vb->next_variable != NULL);
1076 vb = vb->next_variable)
1081 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1083 il = malloc (sizeof (*il));
1086 ERROR ("snmp plugin: malloc failed.");
1089 memset (il, 0, sizeof (*il));
1092 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1099 /* Get instance name */
1100 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1104 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1106 for (ptr = il->instance; *ptr != '\0'; ptr++)
1108 if ((*ptr > 0) && (*ptr < 32))
1110 else if (*ptr == '/')
1113 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1117 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1118 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1119 ssnprintf (il->instance, sizeof (il->instance),
1120 "%llu", val.counter);
1123 /* TODO: Debugging output */
1132 } /* int csnmp_instance_list_add */
1134 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1135 csnmp_list_instances_t *instance_list,
1136 csnmp_table_values_t **value_table)
1138 const data_set_t *ds;
1139 value_list_t vl = VALUE_LIST_INIT;
1141 csnmp_list_instances_t *instance_list_ptr;
1142 csnmp_table_values_t **value_table_ptr;
1146 oid_t current_suffix;
1148 ds = plugin_get_ds (data->type);
1151 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1154 assert (ds->ds_num == data->values_len);
1156 instance_list_ptr = instance_list;
1158 value_table_ptr = malloc (sizeof (*value_table_ptr) * data->values_len);
1159 if (value_table_ptr == NULL)
1161 for (i = 0; i < data->values_len; i++)
1162 value_table_ptr[i] = value_table[i];
1164 vl.values_len = ds->ds_num;
1165 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1166 if (vl.values == NULL)
1168 ERROR ("snmp plugin: malloc failed.");
1169 sfree (value_table_ptr);
1173 sstrncpy (vl.host, host->name, sizeof (vl.host));
1174 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1176 vl.interval = host->interval;
1179 memset (¤t_suffix, 0, sizeof (current_suffix));
1182 _Bool suffix_skipped = 0;
1184 /* Determine next suffix to handle. */
1185 if (instance_list != NULL)
1187 if (instance_list_ptr == NULL)
1193 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1195 else /* no instance configured */
1197 csnmp_table_values_t *ptr = value_table_ptr[0];
1204 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1207 /* Update all the value_table_ptr to point at the entry with the same
1208 * trailing partial OID */
1209 for (i = 0; i < data->values_len; i++)
1211 while ((value_table_ptr[i] != NULL)
1212 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1213 value_table_ptr[i] = value_table_ptr[i]->next;
1215 if (value_table_ptr[i] == NULL)
1220 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1222 /* This suffix is missing in the subtree. Indicate this with the
1223 * "suffix_skipped" flag and try the next instance / suffix. */
1227 } /* for (i = 0; i < columns; i++) */
1232 /* Matching the values failed. Start from the beginning again. */
1235 if (instance_list != NULL)
1236 instance_list_ptr = instance_list_ptr->next;
1238 value_table_ptr[0] = value_table_ptr[0]->next;
1243 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1244 * to the same subid. instance_list_ptr is either NULL or points to the
1245 * same subid, too. */
1247 for (i = 1; i < data->values_len; i++)
1249 assert (value_table_ptr[i] != NULL);
1250 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1251 &value_table_ptr[i]->suffix) == 0);
1253 assert ((instance_list_ptr == NULL)
1254 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1255 &value_table_ptr[0]->suffix) == 0));
1258 sstrncpy (vl.type, data->type, sizeof (vl.type));
1261 char temp[DATA_MAX_NAME_LEN];
1263 if (instance_list_ptr == NULL)
1264 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1266 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1268 if (data->instance_prefix == NULL)
1269 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1271 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1272 data->instance_prefix, temp);
1275 for (i = 0; i < data->values_len; i++)
1276 vl.values[i] = value_table_ptr[i]->value;
1278 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1279 plugin_dispatch_values (&vl);
1281 if (instance_list != NULL)
1282 instance_list_ptr = instance_list_ptr->next;
1284 value_table_ptr[0] = value_table_ptr[0]->next;
1285 } /* while (have_more) */
1288 sfree (value_table_ptr);
1291 } /* int csnmp_dispatch_table */
1293 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1295 struct snmp_pdu *req;
1296 struct snmp_pdu *res;
1297 struct variable_list *vb;
1299 const data_set_t *ds;
1301 uint32_t oid_list_len = (uint32_t) (data->values_len + 1);
1302 /* Holds the last OID returned by the device. We use this in the GETNEXT
1303 * request to proceed. */
1304 oid_t oid_list[oid_list_len];
1305 /* Set to false when an OID has left its subtree so we don't re-request it
1307 _Bool oid_list_todo[oid_list_len];
1313 /* `value_list_head' and `value_list_tail' implement a linked list for each
1314 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1315 * instance names. This is used to jump gaps in the table. */
1316 csnmp_list_instances_t *instance_list_head;
1317 csnmp_list_instances_t *instance_list_tail;
1318 csnmp_table_values_t **value_list_head;
1319 csnmp_table_values_t **value_list_tail;
1321 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1322 host->name, data->name);
1324 if (host->sess_handle == NULL)
1326 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1330 ds = plugin_get_ds (data->type);
1333 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1337 if (ds->ds_num != data->values_len)
1339 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1340 data->type, ds->ds_num, data->values_len);
1344 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1345 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1346 if (data->instance.oid.oid_len > 0)
1347 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1348 else /* no InstanceFrom option specified. */
1351 for (j = 0; j < oid_list_len; j++)
1352 oid_list_todo[j] = 1;
1354 /* We're going to construct n linked lists, one for each "value".
1355 * value_list_head will contain pointers to the heads of these linked lists,
1356 * value_list_tail will contain pointers to the tail of the lists. */
1357 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1358 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1359 if ((value_list_head == NULL) || (value_list_tail == NULL))
1361 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1362 sfree (value_list_head);
1363 sfree (value_list_tail);
1367 instance_list_head = NULL;
1368 instance_list_tail = NULL;
1373 int oid_list_todo_num;
1375 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1378 ERROR ("snmp plugin: snmp_pdu_create failed.");
1383 oid_list_todo_num = 0;
1384 for (j = 0; j < oid_list_len; j++)
1386 /* Do not rerequest already finished OIDs */
1387 if (!oid_list_todo[j])
1389 oid_list_todo_num++;
1390 snmp_add_null_var (req, oid_list[j].oid, oid_list[j].oid_len);
1393 if (oid_list_todo_num == 0)
1395 /* The request is still empty - so we are finished */
1396 DEBUG ("snmp plugin: all variables have left their subtree");
1402 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1403 if ((status != STAT_SUCCESS) || (res == NULL))
1405 char *errstr = NULL;
1407 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1409 c_complain (LOG_ERR, &host->complaint,
1410 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1411 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1414 snmp_free_pdu (res);
1418 csnmp_host_close_session (host);
1425 assert (res != NULL);
1426 c_release (LOG_INFO, &host->complaint,
1427 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1430 vb = res->variables;
1437 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1439 /* Calculate value index from todo list */
1440 while (!oid_list_todo[i] && (i < oid_list_len))
1443 /* An instance is configured and the res variable we process is the
1444 * instance value (last index) */
1445 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1447 if ((vb->type == SNMP_ENDOFMIBVIEW)
1448 || (snmp_oid_ncompare (data->instance.oid.oid,
1449 data->instance.oid.oid_len,
1450 vb->name, vb->name_length,
1451 data->instance.oid.oid_len) != 0))
1453 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1454 host->name, data->name);
1455 oid_list_todo[i] = 0;
1459 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1460 * add it to the list */
1461 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1462 res, host, data) != 0)
1464 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1469 else /* The variable we are processing is a normal value */
1471 csnmp_table_values_t *vt;
1476 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1478 /* Calculate the current suffix. This is later used to check that the
1479 * suffix is increasing. This also checks if we left the subtree */
1480 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1483 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1484 "Value probably left its subtree.",
1485 host->name, data->name, i);
1486 oid_list_todo[i] = 0;
1490 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1491 * table matching algorithm will get confused. */
1492 if ((value_list_tail[i] != NULL)
1493 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1495 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1496 "Suffix is not increasing.",
1497 host->name, data->name, i);
1498 oid_list_todo[i] = 0;
1502 vt = malloc (sizeof (*vt));
1505 ERROR ("snmp plugin: malloc failed.");
1509 memset (vt, 0, sizeof (*vt));
1511 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1512 data->scale, data->shift, host->name, data->name);
1513 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1516 if (value_list_tail[i] == NULL)
1517 value_list_head[i] = vt;
1519 value_list_tail[i]->next = vt;
1520 value_list_tail[i] = vt;
1523 /* Copy OID to oid_list[i] */
1524 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1525 oid_list[i].oid_len = vb->name_length;
1527 } /* for (vb = res->variables ...) */
1530 snmp_free_pdu (res);
1532 } /* while (status == 0) */
1535 snmp_free_pdu (res);
1539 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1541 /* Free all allocated variables here */
1542 while (instance_list_head != NULL)
1544 csnmp_list_instances_t *next = instance_list_head->next;
1545 sfree (instance_list_head);
1546 instance_list_head = next;
1549 for (i = 0; i < data->values_len; i++)
1551 while (value_list_head[i] != NULL)
1553 csnmp_table_values_t *next = value_list_head[i]->next;
1554 sfree (value_list_head[i]);
1555 value_list_head[i] = next;
1559 sfree (value_list_head);
1560 sfree (value_list_tail);
1563 } /* int csnmp_read_table */
1565 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1567 struct snmp_pdu *req;
1568 struct snmp_pdu *res;
1569 struct variable_list *vb;
1571 const data_set_t *ds;
1572 value_list_t vl = VALUE_LIST_INIT;
1577 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1578 host->name, data->name);
1580 if (host->sess_handle == NULL)
1582 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1586 ds = plugin_get_ds (data->type);
1589 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1593 if (ds->ds_num != data->values_len)
1595 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1596 data->type, ds->ds_num, data->values_len);
1600 vl.values_len = ds->ds_num;
1601 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1602 if (vl.values == NULL)
1604 for (i = 0; i < vl.values_len; i++)
1606 if (ds->ds[i].type == DS_TYPE_COUNTER)
1607 vl.values[i].counter = 0;
1609 vl.values[i].gauge = NAN;
1612 sstrncpy (vl.host, host->name, sizeof (vl.host));
1613 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1614 sstrncpy (vl.type, data->type, sizeof (vl.type));
1615 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1617 vl.interval = host->interval;
1619 req = snmp_pdu_create (SNMP_MSG_GET);
1622 ERROR ("snmp plugin: snmp_pdu_create failed.");
1627 for (i = 0; i < data->values_len; i++)
1628 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1631 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1633 if ((status != STAT_SUCCESS) || (res == NULL))
1635 char *errstr = NULL;
1637 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1638 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1639 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1642 snmp_free_pdu (res);
1646 csnmp_host_close_session (host);
1652 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1656 snprint_variable (buffer, sizeof (buffer),
1657 vb->name, vb->name_length, vb);
1658 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1659 #endif /* COLLECT_DEBUG */
1661 for (i = 0; i < data->values_len; i++)
1662 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1663 vb->name, vb->name_length) == 0)
1664 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1665 data->scale, data->shift, host->name, data->name);
1666 } /* for (res->variables) */
1669 snmp_free_pdu (res);
1672 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1673 plugin_dispatch_values (&vl);
1677 } /* int csnmp_read_value */
1679 static int csnmp_read_host (user_data_t *ud)
1681 host_definition_t *host;
1682 cdtime_t time_start;
1690 if (host->interval == 0)
1691 host->interval = plugin_get_interval ();
1693 time_start = cdtime ();
1695 if (host->sess_handle == NULL)
1696 csnmp_host_open_session (host);
1698 if (host->sess_handle == NULL)
1702 for (i = 0; i < host->data_list_len; i++)
1704 data_definition_t *data = host->data_list[i];
1707 status = csnmp_read_table (host, data);
1709 status = csnmp_read_value (host, data);
1715 time_end = cdtime ();
1716 if ((time_end - time_start) > host->interval)
1718 WARNING ("snmp plugin: Host `%s' should be queried every %.3f "
1719 "seconds, but reading all values takes %.3f seconds.",
1721 CDTIME_T_TO_DOUBLE (host->interval),
1722 CDTIME_T_TO_DOUBLE (time_end - time_start));
1729 } /* int csnmp_read_host */
1731 static int csnmp_init (void)
1733 call_snmp_init_once ();
1736 } /* int csnmp_init */
1738 static int csnmp_shutdown (void)
1740 data_definition_t *data_this;
1741 data_definition_t *data_next;
1743 /* When we get here, the read threads have been stopped and all the
1744 * `host_definition_t' will be freed. */
1745 DEBUG ("snmp plugin: Destroying all data definitions.");
1747 data_this = data_head;
1749 while (data_this != NULL)
1751 data_next = data_this->next;
1753 sfree (data_this->name);
1754 sfree (data_this->type);
1755 sfree (data_this->values);
1758 data_this = data_next;
1762 } /* int csnmp_shutdown */
1764 void module_register (void)
1766 plugin_register_complex_config ("snmp", csnmp_config);
1767 plugin_register_init ("snmp", csnmp_init);
1768 plugin_register_shutdown ("snmp", csnmp_shutdown);
1769 } /* void module_register */
1772 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker