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
71 c_complain_t complaint;
73 data_definition_t **data_list;
76 typedef struct host_definition_s host_definition_t;
78 /* These two types are used to cache values in `csnmp_read_table' to handle
80 struct csnmp_list_instances_s
83 char instance[DATA_MAX_NAME_LEN];
84 struct csnmp_list_instances_s *next;
86 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
88 struct csnmp_table_values_s
92 struct csnmp_table_values_s *next;
94 typedef struct csnmp_table_values_s csnmp_table_values_t;
99 static data_definition_t *data_head = NULL;
104 static int csnmp_read_host (user_data_t *ud);
109 static void csnmp_oid_init (oid_t *dst, oid const *src, size_t n)
111 assert (n <= STATIC_ARRAY_LEN (dst->oid));
112 memcpy (dst->oid, src, sizeof (*src) * n);
116 static int csnmp_oid_compare (oid_t const *left, oid_t const *right)
118 return (snmp_oid_compare (left->oid, left->oid_len,
119 right->oid, right->oid_len));
122 static int csnmp_oid_suffix (oid_t *dst, oid_t const *src,
125 /* Make sure "src" is in "root"s subtree. */
126 if (src->oid_len <= root->oid_len)
128 if (snmp_oid_ncompare (root->oid, root->oid_len,
129 src->oid, src->oid_len,
130 /* n = */ root->oid_len) != 0)
133 memset (dst, 0, sizeof (*dst));
134 dst->oid_len = src->oid_len - root->oid_len;
135 memcpy (dst->oid, &src->oid[root->oid_len],
136 dst->oid_len * sizeof (dst->oid[0]));
140 static int csnmp_oid_to_string (char *buffer, size_t buffer_size,
143 char oid_str[MAX_OID_LEN][16];
144 char *oid_str_ptr[MAX_OID_LEN];
147 for (i = 0; i < o->oid_len; i++)
149 ssnprintf (oid_str[i], sizeof (oid_str[i]), "%lu", (unsigned long) o->oid[i]);
150 oid_str_ptr[i] = oid_str[i];
153 return (strjoin (buffer, buffer_size,
154 oid_str_ptr, o->oid_len, /* separator = */ "."));
157 static void csnmp_host_close_session (host_definition_t *host) /* {{{ */
159 if (host->sess_handle == NULL)
162 snmp_sess_close (host->sess_handle);
163 host->sess_handle = NULL;
164 } /* }}} void csnmp_host_close_session */
166 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
168 host_definition_t *hd;
175 if (hd->name != NULL)
177 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
181 csnmp_host_close_session (hd);
185 sfree (hd->community);
186 sfree (hd->data_list);
189 } /* }}} void csnmp_host_definition_destroy */
191 /* Many functions to handle the configuration. {{{ */
192 /* First there are many functions which do configuration stuff. It's a big
193 * bloated and messy, I'm afraid. */
196 * Callgraph for the config stuff:
198 * +-> call_snmp_init_once
199 * +-> csnmp_config_add_data
200 * ! +-> csnmp_config_add_data_type
201 * ! +-> csnmp_config_add_data_table
202 * ! +-> csnmp_config_add_data_instance
203 * ! +-> csnmp_config_add_data_instance_prefix
204 * ! +-> csnmp_config_add_data_values
205 * +-> csnmp_config_add_host
206 * +-> csnmp_config_add_host_address
207 * +-> csnmp_config_add_host_community
208 * +-> csnmp_config_add_host_version
209 * +-> csnmp_config_add_host_collect
210 * +-> csnmp_config_add_host_interval
212 static void call_snmp_init_once (void)
214 static int have_init = 0;
217 init_snmp (PACKAGE_NAME);
219 } /* void call_snmp_init_once */
221 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
223 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
225 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
230 dd->type = strdup (ci->values[0].value.string);
231 if (dd->type == NULL)
235 } /* int csnmp_config_add_data_type */
237 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
239 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
241 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
245 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
248 } /* int csnmp_config_add_data_table */
250 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
252 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
254 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
260 /* Instance is an OID */
261 dd->instance.oid.oid_len = MAX_OID_LEN;
263 if (!read_objid (ci->values[0].value.string,
264 dd->instance.oid.oid, &dd->instance.oid.oid_len))
266 ERROR ("snmp plugin: read_objid (%s) failed.",
267 ci->values[0].value.string);
273 /* Instance is a simple string */
274 sstrncpy (dd->instance.string, ci->values[0].value.string,
275 sizeof (dd->instance.string));
279 } /* int csnmp_config_add_data_instance */
281 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
284 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
286 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
292 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
293 "is set to `false'.", dd->name);
297 sfree (dd->instance_prefix);
298 dd->instance_prefix = strdup (ci->values[0].value.string);
299 if (dd->instance_prefix == NULL)
303 } /* int csnmp_config_add_data_instance_prefix */
305 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
309 if (ci->values_num < 1)
311 WARNING ("snmp plugin: `Values' needs at least one argument.");
315 for (i = 0; i < ci->values_num; i++)
316 if (ci->values[i].type != OCONFIG_TYPE_STRING)
318 WARNING ("snmp plugin: `Values' needs only string argument.");
324 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
325 if (dd->values == NULL)
327 dd->values_len = ci->values_num;
329 for (i = 0; i < ci->values_num; i++)
331 dd->values[i].oid_len = MAX_OID_LEN;
333 if (NULL == snmp_parse_oid (ci->values[i].value.string,
334 dd->values[i].oid, &dd->values[i].oid_len))
336 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
337 ci->values[i].value.string);
346 } /* int csnmp_config_add_data_instance */
348 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
350 if ((ci->values_num != 1)
351 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
353 WARNING ("snmp plugin: The `Shift' config option needs exactly one number argument.");
357 dd->shift = ci->values[0].value.number;
360 } /* int csnmp_config_add_data_shift */
362 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
364 if ((ci->values_num != 1)
365 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
367 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
371 dd->scale = ci->values[0].value.number;
374 } /* int csnmp_config_add_data_scale */
376 static int csnmp_config_add_data (oconfig_item_t *ci)
378 data_definition_t *dd;
382 if ((ci->values_num != 1)
383 || (ci->values[0].type != OCONFIG_TYPE_STRING))
385 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
389 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
392 memset (dd, '\0', sizeof (data_definition_t));
394 dd->name = strdup (ci->values[0].value.string);
395 if (dd->name == NULL)
403 for (i = 0; i < ci->children_num; i++)
405 oconfig_item_t *option = ci->children + i;
408 if (strcasecmp ("Type", option->key) == 0)
409 status = csnmp_config_add_data_type (dd, option);
410 else if (strcasecmp ("Table", option->key) == 0)
411 status = csnmp_config_add_data_table (dd, option);
412 else if (strcasecmp ("Instance", option->key) == 0)
413 status = csnmp_config_add_data_instance (dd, option);
414 else if (strcasecmp ("InstancePrefix", option->key) == 0)
415 status = csnmp_config_add_data_instance_prefix (dd, option);
416 else if (strcasecmp ("Values", option->key) == 0)
417 status = csnmp_config_add_data_values (dd, option);
418 else if (strcasecmp ("Shift", option->key) == 0)
419 status = csnmp_config_add_data_shift (dd, option);
420 else if (strcasecmp ("Scale", option->key) == 0)
421 status = csnmp_config_add_data_scale (dd, option);
424 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
430 } /* for (ci->children) */
434 if (dd->type == NULL)
436 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
440 if (dd->values == NULL)
442 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
448 } /* while (status == 0) */
453 sfree (dd->instance_prefix);
459 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
460 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
462 if (data_head == NULL)
466 data_definition_t *last;
468 while (last->next != NULL)
474 } /* int csnmp_config_add_data */
476 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
478 if ((ci->values_num != 1)
479 || (ci->values[0].type != OCONFIG_TYPE_STRING))
481 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
485 if (hd->address == NULL)
488 hd->address = strdup (ci->values[0].value.string);
489 if (hd->address == NULL)
492 DEBUG ("snmp plugin: host = %s; host->address = %s;",
493 hd->name, hd->address);
496 } /* int csnmp_config_add_host_address */
498 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
500 if ((ci->values_num != 1)
501 || (ci->values[0].type != OCONFIG_TYPE_STRING))
503 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
507 if (hd->community == NULL)
508 free (hd->community);
510 hd->community = strdup (ci->values[0].value.string);
511 if (hd->community == NULL)
514 DEBUG ("snmp plugin: host = %s; host->community = %s;",
515 hd->name, hd->community);
518 } /* int csnmp_config_add_host_community */
520 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
524 if ((ci->values_num != 1)
525 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
527 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
531 version = (int) ci->values[0].value.number;
532 if ((version != 1) && (version != 2))
534 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
538 hd->version = version;
541 } /* int csnmp_config_add_host_address */
543 static int csnmp_config_add_host_collect (host_definition_t *host,
546 data_definition_t *data;
547 data_definition_t **data_list;
551 if (ci->values_num < 1)
553 WARNING ("snmp plugin: `Collect' needs at least one argument.");
557 for (i = 0; i < ci->values_num; i++)
558 if (ci->values[i].type != OCONFIG_TYPE_STRING)
560 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
564 data_list_len = host->data_list_len + ci->values_num;
565 data_list = (data_definition_t **) realloc (host->data_list,
566 sizeof (data_definition_t *) * data_list_len);
567 if (data_list == NULL)
569 host->data_list = data_list;
571 for (i = 0; i < ci->values_num; i++)
573 for (data = data_head; data != NULL; data = data->next)
574 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
579 WARNING ("snmp plugin: No such data configured: `%s'",
580 ci->values[i].value.string);
584 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
585 host->name, host->data_list_len, data->name);
587 host->data_list[host->data_list_len] = data;
588 host->data_list_len++;
589 } /* for (values_num) */
592 } /* int csnmp_config_add_host_collect */
594 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
596 if ((ci->values_num != 1)
597 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
599 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
603 hd->interval = ci->values[0].value.number >= 0
604 ? (uint32_t) ci->values[0].value.number
608 } /* int csnmp_config_add_host_interval */
610 static int csnmp_config_add_host (oconfig_item_t *ci)
612 host_definition_t *hd;
616 /* Registration stuff. */
617 char cb_name[DATA_MAX_NAME_LEN];
619 struct timespec cb_interval;
621 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
623 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
627 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
630 memset (hd, '\0', sizeof (host_definition_t));
632 C_COMPLAIN_INIT (&hd->complaint);
634 hd->name = strdup (ci->values[0].value.string);
635 if (hd->name == NULL)
641 hd->sess_handle = NULL;
644 for (i = 0; i < ci->children_num; i++)
646 oconfig_item_t *option = ci->children + i;
649 if (strcasecmp ("Address", option->key) == 0)
650 status = csnmp_config_add_host_address (hd, option);
651 else if (strcasecmp ("Community", option->key) == 0)
652 status = csnmp_config_add_host_community (hd, option);
653 else if (strcasecmp ("Version", option->key) == 0)
654 status = csnmp_config_add_host_version (hd, option);
655 else if (strcasecmp ("Collect", option->key) == 0)
656 csnmp_config_add_host_collect (hd, option);
657 else if (strcasecmp ("Interval", option->key) == 0)
658 csnmp_config_add_host_interval (hd, option);
661 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
667 } /* for (ci->children) */
671 if (hd->address == NULL)
673 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
677 if (hd->community == NULL)
679 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
685 } /* while (status == 0) */
689 csnmp_host_definition_destroy (hd);
693 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
694 hd->name, hd->address, hd->community, hd->version);
696 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
698 memset (&cb_data, 0, sizeof (cb_data));
700 cb_data.free_func = csnmp_host_definition_destroy;
702 memset (&cb_interval, 0, sizeof (cb_interval));
703 if (hd->interval != 0)
704 cb_interval.tv_sec = (time_t) hd->interval;
706 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
707 csnmp_read_host, /* interval = */ &cb_interval,
708 /* user_data = */ &cb_data);
711 ERROR ("snmp plugin: Registering complex read function failed.");
712 csnmp_host_definition_destroy (hd);
717 } /* int csnmp_config_add_host */
719 static int csnmp_config (oconfig_item_t *ci)
723 call_snmp_init_once ();
725 for (i = 0; i < ci->children_num; i++)
727 oconfig_item_t *child = ci->children + i;
728 if (strcasecmp ("Data", child->key) == 0)
729 csnmp_config_add_data (child);
730 else if (strcasecmp ("Host", child->key) == 0)
731 csnmp_config_add_host (child);
734 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
736 } /* for (ci->children) */
739 } /* int csnmp_config */
741 /* }}} End of the config stuff. Now the interesting part begins */
743 static void csnmp_host_open_session (host_definition_t *host)
745 struct snmp_session sess;
747 if (host->sess_handle != NULL)
748 csnmp_host_close_session (host);
750 snmp_sess_init (&sess);
751 sess.peername = host->address;
752 sess.community = (u_char *) host->community;
753 sess.community_len = strlen (host->community);
754 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
756 /* snmp_sess_open will copy the `struct snmp_session *'. */
757 host->sess_handle = snmp_sess_open (&sess);
759 if (host->sess_handle == NULL)
763 snmp_error (&sess, NULL, NULL, &errstr);
765 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
766 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
769 } /* void csnmp_host_open_session */
771 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
772 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
773 double scale, double shift)
776 uint64_t tmp_unsigned = 0;
777 int64_t tmp_signed = 0;
779 /* Set to true when the original SNMP type appears to have been signed. */
780 _Bool prefer_signed = 0;
782 if ((vl->type == ASN_INTEGER)
783 || (vl->type == ASN_UINTEGER)
784 || (vl->type == ASN_COUNTER)
786 || (vl->type == ASN_TIMETICKS)
788 || (vl->type == ASN_GAUGE))
790 tmp_unsigned = (uint32_t) *vl->val.integer;
791 tmp_signed = (int32_t) *vl->val.integer;
793 if ((vl->type == ASN_INTEGER)
794 || (vl->type == ASN_GAUGE))
797 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
799 else if (vl->type == ASN_COUNTER64)
801 tmp_unsigned = (uint32_t) vl->val.counter64->high;
802 tmp_unsigned = tmp_unsigned << 32;
803 tmp_unsigned += (uint32_t) vl->val.counter64->low;
804 tmp_signed = (int64_t) tmp_unsigned;
805 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
807 else if (vl->type == ASN_OCTET_STR)
809 /* We'll handle this later.. */
813 char oid_buffer[1024];
815 memset (oid_buffer, 0, sizeof (oid_buffer));
816 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
817 vl->name, vl->name_length);
820 if (vl->type == ASN_NULL)
821 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
825 WARNING ("snmp plugin: I don't know the ASN type \"%i\" (OID: %s)",
826 (int) vl->type, oid_buffer);
831 if (vl->type == ASN_OCTET_STR)
835 if (vl->val.string != NULL)
838 size_t string_length;
840 string_length = sizeof (string) - 1;
841 if (vl->val_len < string_length)
842 string_length = vl->val_len;
844 /* The strings we get from the Net-SNMP library may not be null
845 * terminated. That is why we're using `memcpy' here and not `strcpy'.
846 * `string_length' is set to `vl->val_len' which holds the length of the
848 memcpy (string, vl->val.string, string_length);
849 string[string_length] = 0;
851 status = parse_value (string, &ret, type);
854 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
855 DS_TYPE_TO_STRING (type), string);
863 case DS_TYPE_COUNTER:
865 case DS_TYPE_ABSOLUTE:
866 memset (&ret, 0, sizeof (ret));
874 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
875 "data source type: %i.", type);
879 } /* if (vl->type == ASN_OCTET_STR) */
880 else if (type == DS_TYPE_COUNTER)
882 ret.counter = tmp_unsigned;
884 else if (type == DS_TYPE_GAUGE)
888 else if (prefer_signed)
889 ret.gauge = (scale * tmp_signed) + shift;
891 ret.gauge = (scale * tmp_unsigned) + shift;
893 else if (type == DS_TYPE_DERIVE)
896 ret.derive = (derive_t) tmp_signed;
898 ret.derive = (derive_t) tmp_unsigned;
900 else if (type == DS_TYPE_ABSOLUTE)
902 ret.absolute = (absolute_t) tmp_unsigned;
906 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
912 } /* value_t csnmp_value_list_to_value */
914 /* Returns true if all OIDs have left their subtree */
915 static int csnmp_check_res_left_subtree (const host_definition_t *host,
916 const data_definition_t *data,
917 struct snmp_pdu *res)
919 struct variable_list *vb;
921 int num_left_subtree;
929 num_left_subtree = 0;
931 /* check all the variables and count how many have left their subtree */
932 for (vb = res->variables, i = 0;
933 (vb != NULL) && (i < data->values_len);
934 vb = vb->next_variable, i++)
938 if ((vb->type == SNMP_ENDOFMIBVIEW)
939 || (snmp_oid_ncompare (data->values[i].oid,
940 data->values[i].oid_len,
941 vb->name, vb->name_length,
942 data->values[i].oid_len) != 0))
946 /* check if enough variables have been returned */
947 if (i < data->values_len)
949 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
950 host->name, data->values_len, i);
954 if (data->instance.oid.oid_len > 0)
958 ERROR ("snmp plugin: host %s: Expected one more variable for "
959 "the instance..", host->name);
964 if (snmp_oid_ncompare (data->instance.oid.oid,
965 data->instance.oid.oid_len,
966 vb->name, vb->name_length,
967 data->instance.oid.oid_len) != 0)
971 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
972 "left their subtree",
973 num_left_subtree, num_checked);
974 if (num_left_subtree >= num_checked)
977 } /* int csnmp_check_res_left_subtree */
979 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
980 const struct variable_list *vb, size_t dst_size)
987 buffer_free = dst_size;
989 for (i = 0; i < vb->val_len; i++)
993 status = snprintf (buffer_ptr, buffer_free,
994 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
996 if (status >= buffer_free)
998 buffer_ptr += (buffer_free - 1);
1000 return (dst_size + (buffer_free - status));
1002 else /* if (status < buffer_free) */
1004 buffer_ptr += status;
1005 buffer_free -= status;
1009 return ((int) (dst_size - buffer_free));
1010 } /* }}} int csnmp_strvbcopy_hexstring */
1012 static int csnmp_strvbcopy (char *dst, /* {{{ */
1013 const struct variable_list *vb, size_t dst_size)
1019 if (vb->type == ASN_OCTET_STR)
1020 src = (char *) vb->val.string;
1021 else if (vb->type == ASN_BIT_STR)
1022 src = (char *) vb->val.bitstring;
1029 num_chars = dst_size - 1;
1030 if (num_chars > vb->val_len)
1031 num_chars = vb->val_len;
1033 for (i = 0; i < num_chars; i++)
1035 /* Check for control characters. */
1036 if ((unsigned char)src[i] < 32)
1037 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1042 return ((int) vb->val_len);
1043 } /* }}} int csnmp_strvbcopy */
1045 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1046 csnmp_list_instances_t **tail,
1047 struct snmp_pdu const *res,
1050 csnmp_list_instances_t *il;
1051 struct variable_list *vb;
1055 /* Set vb on the last variable */
1056 for (vb = res->variables;
1057 (vb != NULL) && (vb->next_variable != NULL);
1058 vb = vb->next_variable)
1063 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1065 il = malloc (sizeof (*il));
1068 ERROR ("snmp plugin: malloc failed.");
1071 memset (il, 0, sizeof (*il));
1074 status = csnmp_oid_suffix (&il->suffix, &vb_name, root);
1081 /* Get instance name */
1082 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1086 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1088 for (ptr = il->instance; *ptr != '\0'; ptr++)
1090 if ((*ptr > 0) && (*ptr < 32))
1092 else if (*ptr == '/')
1095 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1099 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
1100 ssnprintf (il->instance, sizeof (il->instance),
1101 "%llu", val.counter);
1104 /* TODO: Debugging output */
1113 } /* int csnmp_instance_list_add */
1115 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1116 csnmp_list_instances_t *instance_list,
1117 csnmp_table_values_t **value_table)
1119 const data_set_t *ds;
1120 value_list_t vl = VALUE_LIST_INIT;
1122 csnmp_list_instances_t *instance_list_ptr;
1123 csnmp_table_values_t **value_table_ptr;
1127 oid_t current_suffix;
1129 ds = plugin_get_ds (data->type);
1132 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1135 assert (ds->ds_num == data->values_len);
1137 instance_list_ptr = instance_list;
1139 value_table_ptr = malloc (sizeof (*value_table_ptr) * data->values_len);
1140 if (value_table_ptr == NULL)
1142 for (i = 0; i < data->values_len; i++)
1143 value_table_ptr[i] = value_table[i];
1145 vl.values_len = ds->ds_num;
1146 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1147 if (vl.values == NULL)
1149 ERROR ("snmp plugin: malloc failed.");
1150 sfree (value_table_ptr);
1154 sstrncpy (vl.host, host->name, sizeof (vl.host));
1155 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1157 vl.interval = host->interval;
1160 memset (¤t_suffix, 0, sizeof (current_suffix));
1163 _Bool suffix_skipped = 0;
1165 /* Determine next suffix to handle. */
1166 if (instance_list != NULL)
1168 if (instance_list_ptr == NULL)
1174 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1176 else /* no instance configured */
1178 csnmp_table_values_t *ptr = value_table_ptr[0];
1185 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1188 /* Update all the value_table_ptr to point at the entry with the same
1189 * trailing partial OID */
1190 for (i = 0; i < data->values_len; i++)
1192 while ((value_table_ptr[i] != NULL)
1193 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1194 value_table_ptr[i] = value_table_ptr[i]->next;
1196 if (value_table_ptr[i] == NULL)
1201 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1203 /* This suffix is missing in the subtree. Indicate this with the
1204 * "suffix_skipped" flag and try the next instance / suffix. */
1208 } /* for (i = 0; i < columns; i++) */
1213 /* Matching the values failed. Start from the beginning again. */
1216 if (instance_list != NULL)
1217 instance_list_ptr = instance_list_ptr->next;
1219 value_table_ptr[0] = value_table_ptr[0]->next;
1224 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1225 * to the same subid. instance_list_ptr is either NULL or points to the
1226 * same subid, too. */
1228 for (i = 1; i < data->values_len; i++)
1230 assert (value_table_ptr[i] != NULL);
1231 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1232 &value_table_ptr[i]->suffix) == 0);
1234 assert ((instance_list_ptr == NULL)
1235 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1236 &value_table_ptr[0]->suffix) == 0));
1239 sstrncpy (vl.type, data->type, sizeof (vl.type));
1242 char temp[DATA_MAX_NAME_LEN];
1244 if (instance_list_ptr == NULL)
1245 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1247 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1249 if (data->instance_prefix == NULL)
1250 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1252 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1253 data->instance_prefix, temp);
1256 for (i = 0; i < data->values_len; i++)
1257 vl.values[i] = value_table_ptr[i]->value;
1259 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1260 plugin_dispatch_values (&vl);
1262 if (instance_list != NULL)
1263 instance_list_ptr = instance_list_ptr->next;
1265 value_table_ptr[0] = value_table_ptr[0]->next;
1266 } /* while (have_more) */
1269 sfree (value_table_ptr);
1272 } /* int csnmp_dispatch_table */
1274 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1276 struct snmp_pdu *req;
1277 struct snmp_pdu *res;
1278 struct variable_list *vb;
1280 const data_set_t *ds;
1282 uint32_t oid_list_len;
1287 /* `value_list_head' and `value_list_tail' implement a linked list for each
1288 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1289 * instance names. This is used to jump gaps in the table. */
1290 csnmp_list_instances_t *instance_list_head;
1291 csnmp_list_instances_t *instance_list_tail;
1292 csnmp_table_values_t **value_list_head;
1293 csnmp_table_values_t **value_list_tail;
1295 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1296 host->name, data->name);
1298 if (host->sess_handle == NULL)
1300 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1304 ds = plugin_get_ds (data->type);
1307 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1311 if (ds->ds_num != data->values_len)
1313 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1314 data->type, ds->ds_num, data->values_len);
1318 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1319 oid_list_len = data->values_len + 1;
1320 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1321 if (oid_list == NULL)
1323 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1326 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1327 if (data->instance.oid.oid_len > 0)
1328 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1332 /* We're going to construct n linked lists, one for each "value".
1333 * value_list_head will contain pointers to the heads of these linked lists,
1334 * value_list_tail will contain pointers to the tail of the lists. */
1335 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1336 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1337 if ((value_list_head == NULL) || (value_list_tail == NULL))
1339 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1341 sfree (value_list_head);
1342 sfree (value_list_tail);
1346 instance_list_head = NULL;
1347 instance_list_tail = NULL;
1352 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1355 ERROR ("snmp plugin: snmp_pdu_create failed.");
1360 for (i = 0; (uint32_t) i < oid_list_len; i++)
1361 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1364 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1366 if ((status != STAT_SUCCESS) || (res == NULL))
1368 char *errstr = NULL;
1370 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1372 c_complain (LOG_ERR, &host->complaint,
1373 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1374 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1377 snmp_free_pdu (res);
1381 csnmp_host_close_session (host);
1387 assert (res != NULL);
1388 c_release (LOG_INFO, &host->complaint,
1389 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1392 vb = res->variables;
1399 /* Check if all values (and possibly the instance) have left their
1401 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1407 /* Copy the OID of the value used as instance to oid_list, if an instance
1409 if (data->instance.oid.oid_len > 0)
1411 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1412 * add it to the list */
1413 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1414 res, &data->instance.oid) != 0)
1416 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1421 /* The instance OID is added to the list of OIDs to GET from the
1422 * snmp agent last, so set vb on the last variable returned and copy
1424 for (vb = res->variables;
1425 (vb != NULL) && (vb->next_variable != NULL);
1426 vb = vb->next_variable)
1428 assert (vb != NULL);
1430 /* Copy the OID of the instance value to oid_list[data->values_len].
1431 * "oid_list" is used for the next GETNEXT request. */
1432 memcpy (oid_list[data->values_len].oid, vb->name,
1433 sizeof (oid) * vb->name_length);
1434 oid_list[data->values_len].oid_len = vb->name_length;
1437 /* Iterate over all the (non-instance) values returned by the agent. The
1438 * (i < value_len) check will make sure we're not handling the instance OID
1440 for (vb = res->variables, i = 0;
1441 (vb != NULL) && (i < data->values_len);
1442 vb = vb->next_variable, i++)
1444 csnmp_table_values_t *vt;
1448 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1450 /* Calculate the current suffix. This is later used to check that the
1451 * suffix is increasing. This also checks if we left the subtree */
1452 status = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1455 DEBUG ("snmp plugin: host = %s; data = %s; Value %i failed. "
1456 "It probably left its subtree.",
1457 host->name, data->name, i);
1461 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1462 * table matching algorithm will get confused. */
1463 if ((value_list_tail[i] != NULL)
1464 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1466 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1467 "Suffix is not increasing.",
1468 host->name, data->name, i);
1472 vt = malloc (sizeof (*vt));
1475 ERROR ("snmp plugin: malloc failed.");
1479 memset (vt, 0, sizeof (*vt));
1481 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1482 data->scale, data->shift);
1483 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1486 if (value_list_tail[i] == NULL)
1487 value_list_head[i] = vt;
1489 value_list_tail[i]->next = vt;
1490 value_list_tail[i] = vt;
1492 /* Copy OID to oid_list[i + 1] */
1493 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1494 oid_list[i].oid_len = vb->name_length;
1495 } /* for (i = data->values_len) */
1498 snmp_free_pdu (res);
1500 } /* while (status == 0) */
1503 snmp_free_pdu (res);
1507 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1509 /* Free all allocated variables here */
1510 while (instance_list_head != NULL)
1512 csnmp_list_instances_t *next = instance_list_head->next;
1513 sfree (instance_list_head);
1514 instance_list_head = next;
1517 for (i = 0; i < data->values_len; i++)
1519 while (value_list_head[i] != NULL)
1521 csnmp_table_values_t *next = value_list_head[i]->next;
1522 sfree (value_list_head[i]);
1523 value_list_head[i] = next;
1527 sfree (value_list_head);
1528 sfree (value_list_tail);
1532 } /* int csnmp_read_table */
1534 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1536 struct snmp_pdu *req;
1537 struct snmp_pdu *res;
1538 struct variable_list *vb;
1540 const data_set_t *ds;
1541 value_list_t vl = VALUE_LIST_INIT;
1546 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1547 host->name, data->name);
1549 if (host->sess_handle == NULL)
1551 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1555 ds = plugin_get_ds (data->type);
1558 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1562 if (ds->ds_num != data->values_len)
1564 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1565 data->type, ds->ds_num, data->values_len);
1569 vl.values_len = ds->ds_num;
1570 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1571 if (vl.values == NULL)
1573 for (i = 0; i < vl.values_len; i++)
1575 if (ds->ds[i].type == DS_TYPE_COUNTER)
1576 vl.values[i].counter = 0;
1578 vl.values[i].gauge = NAN;
1581 sstrncpy (vl.host, host->name, sizeof (vl.host));
1582 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1583 sstrncpy (vl.type, data->type, sizeof (vl.type));
1584 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1586 vl.interval = host->interval;
1588 req = snmp_pdu_create (SNMP_MSG_GET);
1591 ERROR ("snmp plugin: snmp_pdu_create failed.");
1596 for (i = 0; i < data->values_len; i++)
1597 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1600 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1602 if ((status != STAT_SUCCESS) || (res == NULL))
1604 char *errstr = NULL;
1606 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1607 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1608 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1611 snmp_free_pdu (res);
1615 csnmp_host_close_session (host);
1621 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1625 snprint_variable (buffer, sizeof (buffer),
1626 vb->name, vb->name_length, vb);
1627 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1628 #endif /* COLLECT_DEBUG */
1630 for (i = 0; i < data->values_len; i++)
1631 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1632 vb->name, vb->name_length) == 0)
1633 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1634 data->scale, data->shift);
1635 } /* for (res->variables) */
1638 snmp_free_pdu (res);
1641 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1642 plugin_dispatch_values (&vl);
1646 } /* int csnmp_read_value */
1648 static int csnmp_read_host (user_data_t *ud)
1650 host_definition_t *host;
1659 if (host->interval == 0)
1660 host->interval = interval_g;
1662 time_start = time (NULL);
1663 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1664 (unsigned int) time_start);
1666 if (host->sess_handle == NULL)
1667 csnmp_host_open_session (host);
1669 if (host->sess_handle == NULL)
1673 for (i = 0; i < host->data_list_len; i++)
1675 data_definition_t *data = host->data_list[i];
1678 status = csnmp_read_table (host, data);
1680 status = csnmp_read_value (host, data);
1686 time_end = time (NULL);
1687 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1688 (unsigned int) time_end);
1689 if ((uint32_t) (time_end - time_start) > host->interval)
1691 WARNING ("snmp plugin: Host `%s' should be queried every %"PRIu32
1692 " seconds, but reading all values takes %u seconds.",
1693 host->name, host->interval, (unsigned int) (time_end - time_start));
1700 } /* int csnmp_read_host */
1702 static int csnmp_init (void)
1704 call_snmp_init_once ();
1707 } /* int csnmp_init */
1709 static int csnmp_shutdown (void)
1711 data_definition_t *data_this;
1712 data_definition_t *data_next;
1714 /* When we get here, the read threads have been stopped and all the
1715 * `host_definition_t' will be freed. */
1716 DEBUG ("snmp plugin: Destroying all data definitions.");
1718 data_this = data_head;
1720 while (data_this != NULL)
1722 data_next = data_this->next;
1724 sfree (data_this->name);
1725 sfree (data_this->type);
1726 sfree (data_this->values);
1729 data_this = data_next;
1733 } /* int csnmp_shutdown */
1735 void module_register (void)
1737 plugin_register_complex_config ("snmp", csnmp_config);
1738 plugin_register_init ("snmp", csnmp_init);
1739 plugin_register_shutdown ("snmp", csnmp_shutdown);
1740 } /* void module_register */
1743 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker