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_SIZE (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
211 static void call_snmp_init_once (void)
213 static int have_init = 0;
216 init_snmp (PACKAGE_NAME);
218 } /* void call_snmp_init_once */
220 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
222 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
224 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
229 dd->type = strdup (ci->values[0].value.string);
230 if (dd->type == NULL)
234 } /* int csnmp_config_add_data_type */
236 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
238 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
240 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
244 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
247 } /* int csnmp_config_add_data_table */
249 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
251 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
253 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
259 /* Instance is an OID */
260 dd->instance.oid.oid_len = MAX_OID_LEN;
262 if (!read_objid (ci->values[0].value.string,
263 dd->instance.oid.oid, &dd->instance.oid.oid_len))
265 ERROR ("snmp plugin: read_objid (%s) failed.",
266 ci->values[0].value.string);
272 /* Instance is a simple string */
273 sstrncpy (dd->instance.string, ci->values[0].value.string,
274 sizeof (dd->instance.string));
278 } /* int csnmp_config_add_data_instance */
280 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
283 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
285 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
291 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
292 "is set to `false'.", dd->name);
296 sfree (dd->instance_prefix);
297 dd->instance_prefix = strdup (ci->values[0].value.string);
298 if (dd->instance_prefix == NULL)
302 } /* int csnmp_config_add_data_instance_prefix */
304 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
308 if (ci->values_num < 1)
310 WARNING ("snmp plugin: `Values' needs at least one argument.");
314 for (i = 0; i < ci->values_num; i++)
315 if (ci->values[i].type != OCONFIG_TYPE_STRING)
317 WARNING ("snmp plugin: `Values' needs only string argument.");
323 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
324 if (dd->values == NULL)
326 dd->values_len = ci->values_num;
328 for (i = 0; i < ci->values_num; i++)
330 dd->values[i].oid_len = MAX_OID_LEN;
332 if (NULL == snmp_parse_oid (ci->values[i].value.string,
333 dd->values[i].oid, &dd->values[i].oid_len))
335 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
336 ci->values[i].value.string);
345 } /* int csnmp_config_add_data_instance */
347 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
349 if ((ci->values_num != 1)
350 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
352 WARNING ("snmp plugin: The `Shift' config option needs exactly one number argument.");
356 dd->shift = ci->values[0].value.number;
359 } /* int csnmp_config_add_data_shift */
361 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
363 if ((ci->values_num != 1)
364 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
366 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
370 dd->scale = ci->values[0].value.number;
373 } /* int csnmp_config_add_data_scale */
375 static int csnmp_config_add_data (oconfig_item_t *ci)
377 data_definition_t *dd;
381 if ((ci->values_num != 1)
382 || (ci->values[0].type != OCONFIG_TYPE_STRING))
384 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
388 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
391 memset (dd, '\0', sizeof (data_definition_t));
393 dd->name = strdup (ci->values[0].value.string);
394 if (dd->name == NULL)
402 for (i = 0; i < ci->children_num; i++)
404 oconfig_item_t *option = ci->children + i;
406 if (strcasecmp ("Type", option->key) == 0)
407 status = csnmp_config_add_data_type (dd, option);
408 else if (strcasecmp ("Table", option->key) == 0)
409 status = csnmp_config_add_data_table (dd, option);
410 else if (strcasecmp ("Instance", option->key) == 0)
411 status = csnmp_config_add_data_instance (dd, option);
412 else if (strcasecmp ("InstancePrefix", option->key) == 0)
413 status = csnmp_config_add_data_instance_prefix (dd, option);
414 else if (strcasecmp ("Values", option->key) == 0)
415 status = csnmp_config_add_data_values (dd, option);
416 else if (strcasecmp ("Shift", option->key) == 0)
417 status = csnmp_config_add_data_shift (dd, option);
418 else if (strcasecmp ("Scale", option->key) == 0)
419 status = csnmp_config_add_data_scale (dd, option);
422 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
428 } /* for (ci->children) */
432 if (dd->type == NULL)
434 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
438 if (dd->values == NULL)
440 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
446 } /* while (status == 0) */
451 sfree (dd->instance_prefix);
457 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
458 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
460 if (data_head == NULL)
464 data_definition_t *last;
466 while (last->next != NULL)
472 } /* int csnmp_config_add_data */
474 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
476 if ((ci->values_num != 1)
477 || (ci->values[0].type != OCONFIG_TYPE_STRING))
479 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
483 if (hd->address == NULL)
486 hd->address = strdup (ci->values[0].value.string);
487 if (hd->address == NULL)
490 DEBUG ("snmp plugin: host = %s; host->address = %s;",
491 hd->name, hd->address);
494 } /* int csnmp_config_add_host_address */
496 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
498 if ((ci->values_num != 1)
499 || (ci->values[0].type != OCONFIG_TYPE_STRING))
501 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
505 if (hd->community == NULL)
506 free (hd->community);
508 hd->community = strdup (ci->values[0].value.string);
509 if (hd->community == NULL)
512 DEBUG ("snmp plugin: host = %s; host->community = %s;",
513 hd->name, hd->community);
516 } /* int csnmp_config_add_host_community */
518 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
522 if ((ci->values_num != 1)
523 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
525 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
529 version = (int) ci->values[0].value.number;
530 if ((version != 1) && (version != 2))
532 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
536 hd->version = version;
539 } /* int csnmp_config_add_host_address */
541 static int csnmp_config_add_host_collect (host_definition_t *host,
544 data_definition_t *data;
545 data_definition_t **data_list;
549 if (ci->values_num < 1)
551 WARNING ("snmp plugin: `Collect' needs at least one argument.");
555 for (i = 0; i < ci->values_num; i++)
556 if (ci->values[i].type != OCONFIG_TYPE_STRING)
558 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
562 data_list_len = host->data_list_len + ci->values_num;
563 data_list = (data_definition_t **) realloc (host->data_list,
564 sizeof (data_definition_t *) * data_list_len);
565 if (data_list == NULL)
567 host->data_list = data_list;
569 for (i = 0; i < ci->values_num; i++)
571 for (data = data_head; data != NULL; data = data->next)
572 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
577 WARNING ("snmp plugin: No such data configured: `%s'",
578 ci->values[i].value.string);
582 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
583 host->name, host->data_list_len, data->name);
585 host->data_list[host->data_list_len] = data;
586 host->data_list_len++;
587 } /* for (values_num) */
590 } /* int csnmp_config_add_host_collect */
592 static int csnmp_config_add_host (oconfig_item_t *ci)
594 host_definition_t *hd;
598 /* Registration stuff. */
599 char cb_name[DATA_MAX_NAME_LEN];
601 struct timespec cb_interval;
603 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
605 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
609 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
612 memset (hd, '\0', sizeof (host_definition_t));
614 C_COMPLAIN_INIT (&hd->complaint);
616 hd->name = strdup (ci->values[0].value.string);
617 if (hd->name == NULL)
623 hd->sess_handle = NULL;
626 for (i = 0; i < ci->children_num; i++)
628 oconfig_item_t *option = ci->children + i;
631 if (strcasecmp ("Address", option->key) == 0)
632 status = csnmp_config_add_host_address (hd, option);
633 else if (strcasecmp ("Community", option->key) == 0)
634 status = csnmp_config_add_host_community (hd, option);
635 else if (strcasecmp ("Version", option->key) == 0)
636 status = csnmp_config_add_host_version (hd, option);
637 else if (strcasecmp ("Collect", option->key) == 0)
638 csnmp_config_add_host_collect (hd, option);
639 else if (strcasecmp ("Interval", option->key) == 0)
640 cf_util_get_cdtime (option, &hd->interval);
643 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
649 } /* for (ci->children) */
653 if (hd->address == NULL)
655 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
659 if (hd->community == NULL)
661 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
667 } /* while (status == 0) */
671 csnmp_host_definition_destroy (hd);
675 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
676 hd->name, hd->address, hd->community, hd->version);
678 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
680 memset (&cb_data, 0, sizeof (cb_data));
682 cb_data.free_func = csnmp_host_definition_destroy;
684 CDTIME_T_TO_TIMESPEC (hd->interval, &cb_interval);
686 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
687 csnmp_read_host, /* interval = */ &cb_interval,
688 /* user_data = */ &cb_data);
691 ERROR ("snmp plugin: Registering complex read function failed.");
692 csnmp_host_definition_destroy (hd);
697 } /* int csnmp_config_add_host */
699 static int csnmp_config (oconfig_item_t *ci)
703 call_snmp_init_once ();
705 for (i = 0; i < ci->children_num; i++)
707 oconfig_item_t *child = ci->children + i;
708 if (strcasecmp ("Data", child->key) == 0)
709 csnmp_config_add_data (child);
710 else if (strcasecmp ("Host", child->key) == 0)
711 csnmp_config_add_host (child);
714 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
716 } /* for (ci->children) */
719 } /* int csnmp_config */
721 /* }}} End of the config stuff. Now the interesting part begins */
723 static void csnmp_host_open_session (host_definition_t *host)
725 struct snmp_session sess;
727 if (host->sess_handle != NULL)
728 csnmp_host_close_session (host);
730 snmp_sess_init (&sess);
731 sess.peername = host->address;
732 sess.community = (u_char *) host->community;
733 sess.community_len = strlen (host->community);
734 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
736 /* snmp_sess_open will copy the `struct snmp_session *'. */
737 host->sess_handle = snmp_sess_open (&sess);
739 if (host->sess_handle == NULL)
743 snmp_error (&sess, NULL, NULL, &errstr);
745 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
746 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
749 } /* void csnmp_host_open_session */
751 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
752 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
753 double scale, double shift,
754 const char *host_name, const char *data_name)
757 uint64_t tmp_unsigned = 0;
758 int64_t tmp_signed = 0;
760 /* Set to true when the original SNMP type appears to have been signed. */
761 _Bool prefer_signed = 0;
763 if ((vl->type == ASN_INTEGER)
764 || (vl->type == ASN_UINTEGER)
765 || (vl->type == ASN_COUNTER)
767 || (vl->type == ASN_TIMETICKS)
769 || (vl->type == ASN_GAUGE))
771 tmp_unsigned = (uint32_t) *vl->val.integer;
772 tmp_signed = (int32_t) *vl->val.integer;
774 if (vl->type == ASN_INTEGER)
777 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
779 else if (vl->type == ASN_COUNTER64)
781 tmp_unsigned = (uint32_t) vl->val.counter64->high;
782 tmp_unsigned = tmp_unsigned << 32;
783 tmp_unsigned += (uint32_t) vl->val.counter64->low;
784 tmp_signed = (int64_t) tmp_unsigned;
785 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
787 else if (vl->type == ASN_OCTET_STR)
789 /* We'll handle this later.. */
793 char oid_buffer[1024];
795 memset (oid_buffer, 0, sizeof (oid_buffer));
796 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
797 vl->name, vl->name_length);
800 if (vl->type == ASN_NULL)
801 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
805 WARNING ("snmp plugin: I don't know the ASN type #%i "
806 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
807 (int) vl->type, oid_buffer,
808 (data_name != NULL) ? data_name : "UNKNOWN",
809 (host_name != NULL) ? host_name : "UNKNOWN");
814 if (vl->type == ASN_OCTET_STR)
818 if (vl->val.string != NULL)
821 size_t string_length;
823 string_length = sizeof (string) - 1;
824 if (vl->val_len < string_length)
825 string_length = vl->val_len;
827 /* The strings we get from the Net-SNMP library may not be null
828 * terminated. That is why we're using `memcpy' here and not `strcpy'.
829 * `string_length' is set to `vl->val_len' which holds the length of the
831 memcpy (string, vl->val.string, string_length);
832 string[string_length] = 0;
834 status = parse_value (string, &ret, type);
837 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
838 DS_TYPE_TO_STRING (type), string);
846 case DS_TYPE_COUNTER:
848 case DS_TYPE_ABSOLUTE:
849 memset (&ret, 0, sizeof (ret));
857 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
858 "data source type: %i.", type);
862 } /* if (vl->type == ASN_OCTET_STR) */
863 else if (type == DS_TYPE_COUNTER)
865 ret.counter = tmp_unsigned;
867 else if (type == DS_TYPE_GAUGE)
871 else if (prefer_signed)
872 ret.gauge = (scale * tmp_signed) + shift;
874 ret.gauge = (scale * tmp_unsigned) + shift;
876 else if (type == DS_TYPE_DERIVE)
879 ret.derive = (derive_t) tmp_signed;
881 ret.derive = (derive_t) tmp_unsigned;
883 else if (type == DS_TYPE_ABSOLUTE)
885 ret.absolute = (absolute_t) tmp_unsigned;
889 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
895 } /* value_t csnmp_value_list_to_value */
897 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
898 const struct variable_list *vb, size_t dst_size)
905 buffer_free = dst_size;
907 for (i = 0; i < vb->val_len; i++)
911 status = snprintf (buffer_ptr, buffer_free,
912 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
914 if (status >= buffer_free)
916 buffer_ptr += (buffer_free - 1);
918 return (dst_size + (buffer_free - status));
920 else /* if (status < buffer_free) */
922 buffer_ptr += status;
923 buffer_free -= status;
927 return ((int) (dst_size - buffer_free));
928 } /* }}} int csnmp_strvbcopy_hexstring */
930 static int csnmp_strvbcopy (char *dst, /* {{{ */
931 const struct variable_list *vb, size_t dst_size)
937 if (vb->type == ASN_OCTET_STR)
938 src = (char *) vb->val.string;
939 else if (vb->type == ASN_BIT_STR)
940 src = (char *) vb->val.bitstring;
947 num_chars = dst_size - 1;
948 if (num_chars > vb->val_len)
949 num_chars = vb->val_len;
951 for (i = 0; i < num_chars; i++)
953 /* Check for control characters. */
954 if ((unsigned char)src[i] < 32)
955 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
960 return ((int) vb->val_len);
961 } /* }}} int csnmp_strvbcopy */
963 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
964 csnmp_list_instances_t **tail,
965 const struct snmp_pdu *res,
966 const host_definition_t *hd, const data_definition_t *dd)
968 csnmp_list_instances_t *il;
969 struct variable_list *vb;
973 /* Set vb on the last variable */
974 for (vb = res->variables;
975 (vb != NULL) && (vb->next_variable != NULL);
976 vb = vb->next_variable)
981 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
983 il = malloc (sizeof (*il));
986 ERROR ("snmp plugin: malloc failed.");
989 memset (il, 0, sizeof (*il));
992 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
999 /* Get instance name */
1000 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1004 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1006 for (ptr = il->instance; *ptr != '\0'; ptr++)
1008 if ((*ptr > 0) && (*ptr < 32))
1010 else if (*ptr == '/')
1013 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1017 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1018 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1019 ssnprintf (il->instance, sizeof (il->instance),
1020 "%llu", val.counter);
1023 /* TODO: Debugging output */
1032 } /* int csnmp_instance_list_add */
1034 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1035 csnmp_list_instances_t *instance_list,
1036 csnmp_table_values_t **value_table)
1038 const data_set_t *ds;
1039 value_list_t vl = VALUE_LIST_INIT;
1041 csnmp_list_instances_t *instance_list_ptr;
1042 csnmp_table_values_t **value_table_ptr;
1046 oid_t current_suffix;
1048 ds = plugin_get_ds (data->type);
1051 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1054 assert (ds->ds_num == data->values_len);
1055 assert (data->values_len > 0);
1057 instance_list_ptr = instance_list;
1059 value_table_ptr = calloc ((size_t) data->values_len, sizeof (*value_table_ptr));
1060 if (value_table_ptr == NULL)
1062 for (i = 0; i < data->values_len; i++)
1063 value_table_ptr[i] = value_table[i];
1065 vl.values_len = data->values_len;
1066 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1067 if (vl.values == NULL)
1069 ERROR ("snmp plugin: malloc failed.");
1070 sfree (value_table_ptr);
1074 sstrncpy (vl.host, host->name, sizeof (vl.host));
1075 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1077 vl.interval = host->interval;
1080 memset (¤t_suffix, 0, sizeof (current_suffix));
1083 _Bool suffix_skipped = 0;
1085 /* Determine next suffix to handle. */
1086 if (instance_list != NULL)
1088 if (instance_list_ptr == NULL)
1094 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1096 else /* no instance configured */
1098 csnmp_table_values_t *ptr = value_table_ptr[0];
1105 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1108 /* Update all the value_table_ptr to point at the entry with the same
1109 * trailing partial OID */
1110 for (i = 0; i < data->values_len; i++)
1112 while ((value_table_ptr[i] != NULL)
1113 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1114 value_table_ptr[i] = value_table_ptr[i]->next;
1116 if (value_table_ptr[i] == NULL)
1121 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1123 /* This suffix is missing in the subtree. Indicate this with the
1124 * "suffix_skipped" flag and try the next instance / suffix. */
1128 } /* for (i = 0; i < columns; i++) */
1133 /* Matching the values failed. Start from the beginning again. */
1136 if (instance_list != NULL)
1137 instance_list_ptr = instance_list_ptr->next;
1139 value_table_ptr[0] = value_table_ptr[0]->next;
1144 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1145 * to the same subid. instance_list_ptr is either NULL or points to the
1146 * same subid, too. */
1148 for (i = 1; i < data->values_len; i++)
1150 assert (value_table_ptr[i] != NULL);
1151 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1152 &value_table_ptr[i]->suffix) == 0);
1154 assert ((instance_list_ptr == NULL)
1155 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1156 &value_table_ptr[0]->suffix) == 0));
1159 sstrncpy (vl.type, data->type, sizeof (vl.type));
1162 char temp[DATA_MAX_NAME_LEN];
1164 if (instance_list_ptr == NULL)
1165 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1167 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1169 if (data->instance_prefix == NULL)
1170 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1172 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1173 data->instance_prefix, temp);
1176 for (i = 0; i < data->values_len; i++)
1177 vl.values[i] = value_table_ptr[i]->value;
1179 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1180 plugin_dispatch_values (&vl);
1182 if (instance_list != NULL)
1183 instance_list_ptr = instance_list_ptr->next;
1185 value_table_ptr[0] = value_table_ptr[0]->next;
1186 } /* while (have_more) */
1189 sfree (value_table_ptr);
1192 } /* int csnmp_dispatch_table */
1194 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1196 struct snmp_pdu *req;
1197 struct snmp_pdu *res = NULL;
1198 struct variable_list *vb;
1200 const data_set_t *ds;
1202 uint32_t oid_list_len = (uint32_t) (data->values_len + 1);
1203 /* Holds the last OID returned by the device. We use this in the GETNEXT
1204 * request to proceed. */
1205 oid_t oid_list[oid_list_len];
1206 /* Set to false when an OID has left its subtree so we don't re-request it
1208 _Bool oid_list_todo[oid_list_len];
1214 /* `value_list_head' and `value_list_tail' implement a linked list for each
1215 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1216 * instance names. This is used to jump gaps in the table. */
1217 csnmp_list_instances_t *instance_list_head;
1218 csnmp_list_instances_t *instance_list_tail;
1219 csnmp_table_values_t **value_list_head;
1220 csnmp_table_values_t **value_list_tail;
1222 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1223 host->name, data->name);
1225 if (host->sess_handle == NULL)
1227 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1231 ds = plugin_get_ds (data->type);
1234 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1238 if (ds->ds_num != data->values_len)
1240 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1241 data->type, ds->ds_num, data->values_len);
1244 assert (data->values_len > 0);
1246 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1247 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1248 if (data->instance.oid.oid_len > 0)
1249 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1250 else /* no InstanceFrom option specified. */
1253 for (j = 0; j < oid_list_len; j++)
1254 oid_list_todo[j] = 1;
1256 /* We're going to construct n linked lists, one for each "value".
1257 * value_list_head will contain pointers to the heads of these linked lists,
1258 * value_list_tail will contain pointers to the tail of the lists. */
1259 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1260 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1261 if ((value_list_head == NULL) || (value_list_tail == NULL))
1263 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1264 sfree (value_list_head);
1265 sfree (value_list_tail);
1269 instance_list_head = NULL;
1270 instance_list_tail = NULL;
1275 int oid_list_todo_num;
1277 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1280 ERROR ("snmp plugin: snmp_pdu_create failed.");
1285 oid_list_todo_num = 0;
1286 for (j = 0; j < oid_list_len; j++)
1288 /* Do not rerequest already finished OIDs */
1289 if (!oid_list_todo[j])
1291 oid_list_todo_num++;
1292 snmp_add_null_var (req, oid_list[j].oid, oid_list[j].oid_len);
1295 if (oid_list_todo_num == 0)
1297 /* The request is still empty - so we are finished */
1298 DEBUG ("snmp plugin: all variables have left their subtree");
1304 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1305 if ((status != STAT_SUCCESS) || (res == NULL))
1307 char *errstr = NULL;
1309 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1311 c_complain (LOG_ERR, &host->complaint,
1312 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1313 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1316 snmp_free_pdu (res);
1319 /* snmp_synch_response already freed our PDU */
1322 csnmp_host_close_session (host);
1329 assert (res != NULL);
1330 c_release (LOG_INFO, &host->complaint,
1331 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1334 vb = res->variables;
1341 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1343 /* Calculate value index from todo list */
1344 while ((i < oid_list_len) && !oid_list_todo[i])
1347 /* An instance is configured and the res variable we process is the
1348 * instance value (last index) */
1349 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1351 if ((vb->type == SNMP_ENDOFMIBVIEW)
1352 || (snmp_oid_ncompare (data->instance.oid.oid,
1353 data->instance.oid.oid_len,
1354 vb->name, vb->name_length,
1355 data->instance.oid.oid_len) != 0))
1357 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1358 host->name, data->name);
1359 oid_list_todo[i] = 0;
1363 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1364 * add it to the list */
1365 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1366 res, host, data) != 0)
1368 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1373 else /* The variable we are processing is a normal value */
1375 csnmp_table_values_t *vt;
1380 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1382 /* Calculate the current suffix. This is later used to check that the
1383 * suffix is increasing. This also checks if we left the subtree */
1384 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1387 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1388 "Value probably left its subtree.",
1389 host->name, data->name, i);
1390 oid_list_todo[i] = 0;
1394 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1395 * table matching algorithm will get confused. */
1396 if ((value_list_tail[i] != NULL)
1397 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1399 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1400 "Suffix is not increasing.",
1401 host->name, data->name, i);
1402 oid_list_todo[i] = 0;
1406 vt = malloc (sizeof (*vt));
1409 ERROR ("snmp plugin: malloc failed.");
1413 memset (vt, 0, sizeof (*vt));
1415 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1416 data->scale, data->shift, host->name, data->name);
1417 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1420 if (value_list_tail[i] == NULL)
1421 value_list_head[i] = vt;
1423 value_list_tail[i]->next = vt;
1424 value_list_tail[i] = vt;
1427 /* Copy OID to oid_list[i] */
1428 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1429 oid_list[i].oid_len = vb->name_length;
1431 } /* for (vb = res->variables ...) */
1434 snmp_free_pdu (res);
1436 } /* while (status == 0) */
1439 snmp_free_pdu (res);
1443 snmp_free_pdu (req);
1447 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1449 /* Free all allocated variables here */
1450 while (instance_list_head != NULL)
1452 csnmp_list_instances_t *next = instance_list_head->next;
1453 sfree (instance_list_head);
1454 instance_list_head = next;
1457 for (i = 0; i < data->values_len; i++)
1459 while (value_list_head[i] != NULL)
1461 csnmp_table_values_t *next = value_list_head[i]->next;
1462 sfree (value_list_head[i]);
1463 value_list_head[i] = next;
1467 sfree (value_list_head);
1468 sfree (value_list_tail);
1471 } /* int csnmp_read_table */
1473 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1475 struct snmp_pdu *req;
1476 struct snmp_pdu *res;
1477 struct variable_list *vb;
1479 const data_set_t *ds;
1480 value_list_t vl = VALUE_LIST_INIT;
1485 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1486 host->name, data->name);
1488 if (host->sess_handle == NULL)
1490 DEBUG ("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1494 ds = plugin_get_ds (data->type);
1497 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1501 if (ds->ds_num != data->values_len)
1503 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1504 data->type, ds->ds_num, data->values_len);
1508 vl.values_len = ds->ds_num;
1509 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1510 if (vl.values == NULL)
1512 for (i = 0; i < vl.values_len; i++)
1514 if (ds->ds[i].type == DS_TYPE_COUNTER)
1515 vl.values[i].counter = 0;
1517 vl.values[i].gauge = NAN;
1520 sstrncpy (vl.host, host->name, sizeof (vl.host));
1521 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1522 sstrncpy (vl.type, data->type, sizeof (vl.type));
1523 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1525 vl.interval = host->interval;
1527 req = snmp_pdu_create (SNMP_MSG_GET);
1530 ERROR ("snmp plugin: snmp_pdu_create failed.");
1535 for (i = 0; i < data->values_len; i++)
1536 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1539 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1541 if ((status != STAT_SUCCESS) || (res == NULL))
1543 char *errstr = NULL;
1545 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1546 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1547 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1550 snmp_free_pdu (res);
1555 csnmp_host_close_session (host);
1561 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1565 snprint_variable (buffer, sizeof (buffer),
1566 vb->name, vb->name_length, vb);
1567 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1568 #endif /* COLLECT_DEBUG */
1570 for (i = 0; i < data->values_len; i++)
1571 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1572 vb->name, vb->name_length) == 0)
1573 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1574 data->scale, data->shift, host->name, data->name);
1575 } /* for (res->variables) */
1578 snmp_free_pdu (res);
1581 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1582 plugin_dispatch_values (&vl);
1586 } /* int csnmp_read_value */
1588 static int csnmp_read_host (user_data_t *ud)
1590 host_definition_t *host;
1591 cdtime_t time_start;
1599 if (host->interval == 0)
1600 host->interval = plugin_get_interval ();
1602 time_start = cdtime ();
1604 if (host->sess_handle == NULL)
1605 csnmp_host_open_session (host);
1607 if (host->sess_handle == NULL)
1611 for (i = 0; i < host->data_list_len; i++)
1613 data_definition_t *data = host->data_list[i];
1616 status = csnmp_read_table (host, data);
1618 status = csnmp_read_value (host, data);
1624 time_end = cdtime ();
1625 if ((time_end - time_start) > host->interval)
1627 WARNING ("snmp plugin: Host `%s' should be queried every %.3f "
1628 "seconds, but reading all values takes %.3f seconds.",
1630 CDTIME_T_TO_DOUBLE (host->interval),
1631 CDTIME_T_TO_DOUBLE (time_end - time_start));
1638 } /* int csnmp_read_host */
1640 static int csnmp_init (void)
1642 call_snmp_init_once ();
1645 } /* int csnmp_init */
1647 static int csnmp_shutdown (void)
1649 data_definition_t *data_this;
1650 data_definition_t *data_next;
1652 /* When we get here, the read threads have been stopped and all the
1653 * `host_definition_t' will be freed. */
1654 DEBUG ("snmp plugin: Destroying all data definitions.");
1656 data_this = data_head;
1658 while (data_this != NULL)
1660 data_next = data_this->next;
1662 sfree (data_this->name);
1663 sfree (data_this->type);
1664 sfree (data_this->values);
1667 data_this = data_next;
1671 } /* int csnmp_shutdown */
1673 void module_register (void)
1675 plugin_register_complex_config ("snmp", csnmp_config);
1676 plugin_register_init ("snmp", csnmp_init);
1677 plugin_register_shutdown ("snmp", csnmp_shutdown);
1678 } /* void module_register */
1681 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker