2 * collectd - src/snmp.c
3 * Copyright (C) 2007 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 verplant.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_host_close_session (host_definition_t *host) /* {{{ */
111 if (host->sess_handle == NULL)
114 snmp_sess_close (host->sess_handle);
115 host->sess_handle = NULL;
116 } /* }}} void csnmp_host_close_session */
118 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
120 host_definition_t *hd;
127 if (hd->name != NULL)
129 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
133 csnmp_host_close_session (hd);
137 sfree (hd->community);
138 sfree (hd->data_list);
141 } /* }}} void csnmp_host_definition_destroy */
143 /* Many functions to handle the configuration. {{{ */
144 /* First there are many functions which do configuration stuff. It's a big
145 * bloated and messy, I'm afraid. */
148 * Callgraph for the config stuff:
150 * +-> call_snmp_init_once
151 * +-> csnmp_config_add_data
152 * ! +-> csnmp_config_add_data_type
153 * ! +-> csnmp_config_add_data_table
154 * ! +-> csnmp_config_add_data_instance
155 * ! +-> csnmp_config_add_data_instance_prefix
156 * ! +-> csnmp_config_add_data_values
157 * +-> csnmp_config_add_host
158 * +-> csnmp_config_add_host_address
159 * +-> csnmp_config_add_host_community
160 * +-> csnmp_config_add_host_version
161 * +-> csnmp_config_add_host_collect
162 * +-> csnmp_config_add_host_interval
164 static void call_snmp_init_once (void)
166 static int have_init = 0;
169 init_snmp (PACKAGE_NAME);
171 } /* void call_snmp_init_once */
173 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
175 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
177 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
182 dd->type = strdup (ci->values[0].value.string);
183 if (dd->type == NULL)
187 } /* int csnmp_config_add_data_type */
189 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
191 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
193 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
197 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
200 } /* int csnmp_config_add_data_table */
202 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
204 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
206 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
212 /* Instance is an OID */
213 dd->instance.oid.oid_len = MAX_OID_LEN;
215 if (!read_objid (ci->values[0].value.string,
216 dd->instance.oid.oid, &dd->instance.oid.oid_len))
218 ERROR ("snmp plugin: read_objid (%s) failed.",
219 ci->values[0].value.string);
225 /* Instance is a simple string */
226 sstrncpy (dd->instance.string, ci->values[0].value.string,
227 sizeof (dd->instance.string));
231 } /* int csnmp_config_add_data_instance */
233 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
236 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
238 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
244 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
245 "is set to `false'.", dd->name);
249 sfree (dd->instance_prefix);
250 dd->instance_prefix = strdup (ci->values[0].value.string);
251 if (dd->instance_prefix == NULL)
255 } /* int csnmp_config_add_data_instance_prefix */
257 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
261 if (ci->values_num < 1)
263 WARNING ("snmp plugin: `Values' needs at least one argument.");
267 for (i = 0; i < ci->values_num; i++)
268 if (ci->values[i].type != OCONFIG_TYPE_STRING)
270 WARNING ("snmp plugin: `Values' needs only string argument.");
276 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
277 if (dd->values == NULL)
279 dd->values_len = ci->values_num;
281 for (i = 0; i < ci->values_num; i++)
283 dd->values[i].oid_len = MAX_OID_LEN;
285 if (NULL == snmp_parse_oid (ci->values[i].value.string,
286 dd->values[i].oid, &dd->values[i].oid_len))
288 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
289 ci->values[i].value.string);
298 } /* int csnmp_config_add_data_instance */
300 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
302 if ((ci->values_num != 1)
303 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
305 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
309 dd->shift = ci->values[0].value.number;
312 } /* int csnmp_config_add_data_shift */
314 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
316 if ((ci->values_num != 1)
317 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
319 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
323 dd->scale = ci->values[0].value.number;
326 } /* int csnmp_config_add_data_scale */
328 static int csnmp_config_add_data (oconfig_item_t *ci)
330 data_definition_t *dd;
334 if ((ci->values_num != 1)
335 || (ci->values[0].type != OCONFIG_TYPE_STRING))
337 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
341 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
344 memset (dd, '\0', sizeof (data_definition_t));
346 dd->name = strdup (ci->values[0].value.string);
347 if (dd->name == NULL)
355 for (i = 0; i < ci->children_num; i++)
357 oconfig_item_t *option = ci->children + i;
360 if (strcasecmp ("Type", option->key) == 0)
361 status = csnmp_config_add_data_type (dd, option);
362 else if (strcasecmp ("Table", option->key) == 0)
363 status = csnmp_config_add_data_table (dd, option);
364 else if (strcasecmp ("Instance", option->key) == 0)
365 status = csnmp_config_add_data_instance (dd, option);
366 else if (strcasecmp ("InstancePrefix", option->key) == 0)
367 status = csnmp_config_add_data_instance_prefix (dd, option);
368 else if (strcasecmp ("Values", option->key) == 0)
369 status = csnmp_config_add_data_values (dd, option);
370 else if (strcasecmp ("Shift", option->key) == 0)
371 status = csnmp_config_add_data_shift (dd, option);
372 else if (strcasecmp ("Scale", option->key) == 0)
373 status = csnmp_config_add_data_scale (dd, option);
376 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
382 } /* for (ci->children) */
386 if (dd->type == NULL)
388 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
392 if (dd->values == NULL)
394 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
400 } /* while (status == 0) */
405 sfree (dd->instance_prefix);
411 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
412 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
414 if (data_head == NULL)
418 data_definition_t *last;
420 while (last->next != NULL)
426 } /* int csnmp_config_add_data */
428 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
430 if ((ci->values_num != 1)
431 || (ci->values[0].type != OCONFIG_TYPE_STRING))
433 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
437 if (hd->address == NULL)
440 hd->address = strdup (ci->values[0].value.string);
441 if (hd->address == NULL)
444 DEBUG ("snmp plugin: host = %s; host->address = %s;",
445 hd->name, hd->address);
448 } /* int csnmp_config_add_host_address */
450 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
452 if ((ci->values_num != 1)
453 || (ci->values[0].type != OCONFIG_TYPE_STRING))
455 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
459 if (hd->community == NULL)
460 free (hd->community);
462 hd->community = strdup (ci->values[0].value.string);
463 if (hd->community == NULL)
466 DEBUG ("snmp plugin: host = %s; host->community = %s;",
467 hd->name, hd->community);
470 } /* int csnmp_config_add_host_community */
472 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
476 if ((ci->values_num != 1)
477 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
479 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
483 version = (int) ci->values[0].value.number;
484 if ((version != 1) && (version != 2))
486 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
490 hd->version = version;
493 } /* int csnmp_config_add_host_address */
495 static int csnmp_config_add_host_collect (host_definition_t *host,
498 data_definition_t *data;
499 data_definition_t **data_list;
503 if (ci->values_num < 1)
505 WARNING ("snmp plugin: `Collect' needs at least one argument.");
509 for (i = 0; i < ci->values_num; i++)
510 if (ci->values[i].type != OCONFIG_TYPE_STRING)
512 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
516 data_list_len = host->data_list_len + ci->values_num;
517 data_list = (data_definition_t **) realloc (host->data_list,
518 sizeof (data_definition_t *) * data_list_len);
519 if (data_list == NULL)
521 host->data_list = data_list;
523 for (i = 0; i < ci->values_num; i++)
525 for (data = data_head; data != NULL; data = data->next)
526 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
531 WARNING ("snmp plugin: No such data configured: `%s'",
532 ci->values[i].value.string);
536 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
537 host->name, host->data_list_len, data->name);
539 host->data_list[host->data_list_len] = data;
540 host->data_list_len++;
541 } /* for (values_num) */
544 } /* int csnmp_config_add_host_collect */
546 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
548 if ((ci->values_num != 1)
549 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
551 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
555 hd->interval = ci->values[0].value.number >= 0
556 ? (uint32_t) ci->values[0].value.number
560 } /* int csnmp_config_add_host_interval */
562 static int csnmp_config_add_host (oconfig_item_t *ci)
564 host_definition_t *hd;
568 /* Registration stuff. */
569 char cb_name[DATA_MAX_NAME_LEN];
571 struct timespec cb_interval;
573 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
575 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
579 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
582 memset (hd, '\0', sizeof (host_definition_t));
584 C_COMPLAIN_INIT (&hd->complaint);
586 hd->name = strdup (ci->values[0].value.string);
587 if (hd->name == NULL)
593 hd->sess_handle = NULL;
596 for (i = 0; i < ci->children_num; i++)
598 oconfig_item_t *option = ci->children + i;
601 if (strcasecmp ("Address", option->key) == 0)
602 status = csnmp_config_add_host_address (hd, option);
603 else if (strcasecmp ("Community", option->key) == 0)
604 status = csnmp_config_add_host_community (hd, option);
605 else if (strcasecmp ("Version", option->key) == 0)
606 status = csnmp_config_add_host_version (hd, option);
607 else if (strcasecmp ("Collect", option->key) == 0)
608 csnmp_config_add_host_collect (hd, option);
609 else if (strcasecmp ("Interval", option->key) == 0)
610 csnmp_config_add_host_interval (hd, option);
613 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
619 } /* for (ci->children) */
623 if (hd->address == NULL)
625 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
629 if (hd->community == NULL)
631 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
637 } /* while (status == 0) */
641 csnmp_host_definition_destroy (hd);
645 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
646 hd->name, hd->address, hd->community, hd->version);
648 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
650 memset (&cb_data, 0, sizeof (cb_data));
652 cb_data.free_func = csnmp_host_definition_destroy;
654 memset (&cb_interval, 0, sizeof (cb_interval));
655 if (hd->interval != 0)
656 cb_interval.tv_sec = (time_t) hd->interval;
658 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
659 csnmp_read_host, /* interval = */ &cb_interval,
660 /* user_data = */ &cb_data);
663 ERROR ("snmp plugin: Registering complex read function failed.");
664 csnmp_host_definition_destroy (hd);
669 } /* int csnmp_config_add_host */
671 static int csnmp_config (oconfig_item_t *ci)
675 call_snmp_init_once ();
677 for (i = 0; i < ci->children_num; i++)
679 oconfig_item_t *child = ci->children + i;
680 if (strcasecmp ("Data", child->key) == 0)
681 csnmp_config_add_data (child);
682 else if (strcasecmp ("Host", child->key) == 0)
683 csnmp_config_add_host (child);
686 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
688 } /* for (ci->children) */
691 } /* int csnmp_config */
693 /* }}} End of the config stuff. Now the interesting part begins */
695 static void csnmp_host_open_session (host_definition_t *host)
697 struct snmp_session sess;
699 if (host->sess_handle != NULL)
700 csnmp_host_close_session (host);
702 snmp_sess_init (&sess);
703 sess.peername = host->address;
704 sess.community = (u_char *) host->community;
705 sess.community_len = strlen (host->community);
706 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
708 /* snmp_sess_open will copy the `struct snmp_session *'. */
709 host->sess_handle = snmp_sess_open (&sess);
711 if (host->sess_handle == NULL)
715 snmp_error (&sess, NULL, NULL, &errstr);
717 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
718 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
721 } /* void csnmp_host_open_session */
723 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
724 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
725 double scale, double shift)
728 uint64_t tmp_unsigned = 0;
729 int64_t tmp_signed = 0;
731 /* Set to true when the original SNMP type appears to have been signed. */
732 _Bool prefer_signed = 0;
734 if ((vl->type == ASN_INTEGER)
735 || (vl->type == ASN_UINTEGER)
736 || (vl->type == ASN_COUNTER)
738 || (vl->type == ASN_TIMETICKS)
740 || (vl->type == ASN_GAUGE))
742 tmp_unsigned = (uint32_t) *vl->val.integer;
743 tmp_signed = (int32_t) *vl->val.integer;
745 if ((vl->type == ASN_INTEGER)
746 || (vl->type == ASN_GAUGE))
749 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
751 else if (vl->type == ASN_COUNTER64)
753 tmp_unsigned = (uint32_t) vl->val.counter64->high;
754 tmp_unsigned = tmp_unsigned << 32;
755 tmp_unsigned += (uint32_t) vl->val.counter64->low;
756 tmp_signed = (int64_t) tmp_unsigned;
757 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
759 else if (vl->type == ASN_OCTET_STR)
761 /* We'll handle this later.. */
765 char oid_buffer[1024];
767 memset (oid_buffer, 0, sizeof (oid_buffer));
768 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
769 vl->name, vl->name_length);
772 if (vl->type == ASN_NULL)
773 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
777 WARNING ("snmp plugin: I don't know the ASN type \"%i\" (OID: %s)",
778 (int) vl->type, oid_buffer);
783 if (vl->type == ASN_OCTET_STR)
787 if (vl->val.string != NULL)
790 size_t string_length;
792 string_length = sizeof (string) - 1;
793 if (vl->val_len < string_length)
794 string_length = vl->val_len;
796 /* The strings we get from the Net-SNMP library may not be null
797 * terminated. That is why we're using `memcpy' here and not `strcpy'.
798 * `string_length' is set to `vl->val_len' which holds the length of the
800 memcpy (string, vl->val.string, string_length);
801 string[string_length] = 0;
803 status = parse_value (string, &ret, type);
806 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
807 DS_TYPE_TO_STRING (type), string);
815 case DS_TYPE_COUNTER:
817 case DS_TYPE_ABSOLUTE:
818 memset (&ret, 0, sizeof (ret));
826 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
827 "data source type: %i.", type);
831 } /* if (vl->type == ASN_OCTET_STR) */
832 else if (type == DS_TYPE_COUNTER)
834 ret.counter = tmp_unsigned;
836 else if (type == DS_TYPE_GAUGE)
840 else if (prefer_signed)
841 ret.gauge = (scale * tmp_signed) + shift;
843 ret.gauge = (scale * tmp_unsigned) + shift;
845 else if (type == DS_TYPE_DERIVE)
848 ret.derive = (derive_t) tmp_signed;
850 ret.derive = (derive_t) tmp_unsigned;
852 else if (type == DS_TYPE_ABSOLUTE)
854 ret.absolute = (absolute_t) tmp_unsigned;
858 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
864 } /* value_t csnmp_value_list_to_value */
866 /* Returns true if all OIDs have left their subtree */
867 static int csnmp_check_res_left_subtree (const host_definition_t *host,
868 const data_definition_t *data,
869 struct snmp_pdu *res)
871 struct variable_list *vb;
873 int num_left_subtree;
881 num_left_subtree = 0;
883 /* check all the variables and count how many have left their subtree */
884 for (vb = res->variables, i = 0;
885 (vb != NULL) && (i < data->values_len);
886 vb = vb->next_variable, i++)
889 if (snmp_oid_ncompare (data->values[i].oid,
890 data->values[i].oid_len,
891 vb->name, vb->name_length,
892 data->values[i].oid_len) != 0)
896 /* check if enough variables have been returned */
897 if (i < data->values_len)
899 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
900 host->name, data->values_len, i);
904 if (data->instance.oid.oid_len > 0)
908 ERROR ("snmp plugin: host %s: Expected one more variable for "
909 "the instance..", host->name);
914 if (snmp_oid_ncompare (data->instance.oid.oid,
915 data->instance.oid.oid_len,
916 vb->name, vb->name_length,
917 data->instance.oid.oid_len) != 0)
921 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
922 "left their subtree",
923 num_left_subtree, num_checked);
924 if (num_left_subtree >= num_checked)
927 } /* int csnmp_check_res_left_subtree */
929 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
930 const struct variable_list *vb, size_t dst_size)
937 buffer_free = dst_size;
939 for (i = 0; i < vb->val_len; i++)
943 status = snprintf (buffer_ptr, buffer_free,
944 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
946 if (status >= buffer_free)
948 buffer_ptr += (buffer_free - 1);
950 return (dst_size + (buffer_free - status));
952 else /* if (status < buffer_free) */
954 buffer_ptr += status;
955 buffer_free -= status;
959 return ((int) (dst_size - buffer_free));
960 } /* }}} int csnmp_strvbcopy_hexstring */
962 static int csnmp_strvbcopy (char *dst, /* {{{ */
963 const struct variable_list *vb, size_t dst_size)
969 if (vb->type == ASN_OCTET_STR)
970 src = (char *) vb->val.string;
971 else if (vb->type == ASN_BIT_STR)
972 src = (char *) vb->val.bitstring;
979 num_chars = dst_size - 1;
980 if (num_chars > vb->val_len)
981 num_chars = vb->val_len;
983 for (i = 0; i < num_chars; i++)
985 /* Check for control characters. */
986 if ((unsigned char)src[i] < 32)
987 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
992 return ((int) vb->val_len);
993 } /* }}} int csnmp_strvbcopy */
995 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
996 csnmp_list_instances_t **tail,
997 const struct snmp_pdu *res)
999 csnmp_list_instances_t *il;
1000 struct variable_list *vb;
1002 /* Set vb on the last variable */
1003 for (vb = res->variables;
1004 (vb != NULL) && (vb->next_variable != NULL);
1005 vb = vb->next_variable)
1010 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
1013 ERROR ("snmp plugin: malloc failed.");
1016 il->subid = vb->name[vb->name_length - 1];
1019 /* Get instance name */
1020 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1024 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1026 for (ptr = il->instance; *ptr != '\0'; ptr++)
1028 if ((*ptr > 0) && (*ptr < 32))
1030 else if (*ptr == '/')
1033 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1037 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
1038 ssnprintf (il->instance, sizeof (il->instance),
1039 "%llu", val.counter);
1042 /* TODO: Debugging output */
1051 } /* int csnmp_instance_list_add */
1053 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1054 csnmp_list_instances_t *instance_list,
1055 csnmp_table_values_t **value_table)
1057 const data_set_t *ds;
1058 value_list_t vl = VALUE_LIST_INIT;
1060 csnmp_list_instances_t *instance_list_ptr;
1061 csnmp_table_values_t **value_table_ptr;
1067 ds = plugin_get_ds (data->type);
1070 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1073 assert (ds->ds_num == data->values_len);
1075 instance_list_ptr = instance_list;
1077 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1078 * data->values_len);
1079 if (value_table_ptr == NULL)
1081 for (i = 0; i < data->values_len; i++)
1082 value_table_ptr[i] = value_table[i];
1084 vl.values_len = ds->ds_num;
1085 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1086 if (vl.values == NULL)
1088 ERROR ("snmp plugin: malloc failed.");
1089 sfree (value_table_ptr);
1093 sstrncpy (vl.host, host->name, sizeof (vl.host));
1094 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1096 vl.interval = host->interval;
1101 while (have_more != 0)
1103 if (instance_list != NULL)
1105 while ((instance_list_ptr != NULL)
1106 && (instance_list_ptr->subid < subid))
1107 instance_list_ptr = instance_list_ptr->next;
1109 if (instance_list_ptr == NULL)
1114 else if (instance_list_ptr->subid > subid)
1116 subid = instance_list_ptr->subid;
1119 } /* if (instance_list != NULL) */
1121 for (i = 0; i < data->values_len; i++)
1123 while ((value_table_ptr[i] != NULL)
1124 && (value_table_ptr[i]->subid < subid))
1125 value_table_ptr[i] = value_table_ptr[i]->next;
1127 if (value_table_ptr[i] == NULL)
1132 else if (value_table_ptr[i]->subid > subid)
1134 subid = value_table_ptr[i]->subid;
1137 } /* for (i = 0; i < columns; i++) */
1138 /* The subid has been increased - start scanning from the beginning
1140 if (i < data->values_len)
1143 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1144 * to the same subid. instance_list_ptr is either NULL or points to the
1145 * same subid, too. */
1147 for (i = 1; i < data->values_len; i++)
1149 assert (value_table_ptr[i] != NULL);
1150 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
1152 assert ((instance_list_ptr == NULL)
1153 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
1156 sstrncpy (vl.type, data->type, sizeof (vl.type));
1159 char temp[DATA_MAX_NAME_LEN];
1161 if (instance_list_ptr == NULL)
1162 ssnprintf (temp, sizeof (temp), "%"PRIu32, (uint32_t) subid);
1164 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1166 if (data->instance_prefix == NULL)
1167 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1169 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1170 data->instance_prefix, temp);
1173 for (i = 0; i < data->values_len; i++)
1174 vl.values[i] = value_table_ptr[i]->value;
1176 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1177 plugin_dispatch_values (&vl);
1180 } /* while (have_more != 0) */
1183 sfree (value_table_ptr);
1186 } /* int csnmp_dispatch_table */
1188 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1190 struct snmp_pdu *req;
1191 struct snmp_pdu *res;
1192 struct variable_list *vb;
1194 const data_set_t *ds;
1196 uint32_t oid_list_len;
1201 /* `value_table' and `value_table_ptr' implement a linked list for each
1202 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1203 * instance names. This is used to jump gaps in the table. */
1204 csnmp_list_instances_t *instance_list;
1205 csnmp_list_instances_t *instance_list_ptr;
1206 csnmp_table_values_t **value_table;
1207 csnmp_table_values_t **value_table_ptr;
1209 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1210 host->name, data->name);
1212 if (host->sess_handle == NULL)
1214 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1218 ds = plugin_get_ds (data->type);
1221 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1225 if (ds->ds_num != data->values_len)
1227 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1228 data->type, ds->ds_num, data->values_len);
1232 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1233 oid_list_len = data->values_len + 1;
1234 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1235 if (oid_list == NULL)
1237 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1240 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1241 if (data->instance.oid.oid_len > 0)
1242 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1246 /* Allocate the `value_table' */
1247 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1248 * 2 * data->values_len);
1249 if (value_table == NULL)
1251 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1255 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1256 value_table_ptr = value_table + data->values_len;
1258 instance_list = NULL;
1259 instance_list_ptr = NULL;
1264 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1267 ERROR ("snmp plugin: snmp_pdu_create failed.");
1272 for (i = 0; (uint32_t) i < oid_list_len; i++)
1273 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1276 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1278 if ((status != STAT_SUCCESS) || (res == NULL))
1280 char *errstr = NULL;
1282 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1284 c_complain (LOG_ERR, &host->complaint,
1285 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1286 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1289 snmp_free_pdu (res);
1293 csnmp_host_close_session (host);
1299 assert (res != NULL);
1300 c_release (LOG_INFO, &host->complaint,
1301 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1304 vb = res->variables;
1311 /* Check if all values (and possibly the instance) have left their
1313 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1319 /* if an instance-OID is configured.. */
1320 if (data->instance.oid.oid_len > 0)
1322 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1323 * add it to the list */
1324 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1327 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1332 /* Set vb on the last variable */
1333 for (vb = res->variables;
1334 (vb != NULL) && (vb->next_variable != NULL);
1335 vb = vb->next_variable)
1337 assert (vb != NULL);
1339 /* Copy OID to oid_list[data->values_len] */
1340 memcpy (oid_list[data->values_len].oid, vb->name,
1341 sizeof (oid) * vb->name_length);
1342 oid_list[data->values_len].oid_len = vb->name_length;
1345 for (vb = res->variables, i = 0;
1346 (vb != NULL) && (i < data->values_len);
1347 vb = vb->next_variable, i++)
1349 csnmp_table_values_t *vt;
1351 /* Check if we left the subtree */
1352 if (snmp_oid_ncompare (data->values[i].oid,
1353 data->values[i].oid_len,
1354 vb->name, vb->name_length,
1355 data->values[i].oid_len) != 0)
1357 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1358 host->name, data->name, i);
1362 if ((value_table_ptr[i] != NULL)
1363 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1365 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1366 "SUBID is not increasing.",
1367 host->name, data->name, i);
1371 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1374 ERROR ("snmp plugin: malloc failed.");
1379 vt->subid = vb->name[vb->name_length - 1];
1380 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1381 data->scale, data->shift);
1384 if (value_table_ptr[i] == NULL)
1385 value_table[i] = vt;
1387 value_table_ptr[i]->next = vt;
1388 value_table_ptr[i] = vt;
1390 /* Copy OID to oid_list[i + 1] */
1391 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1392 oid_list[i].oid_len = vb->name_length;
1393 } /* for (i = data->values_len) */
1396 snmp_free_pdu (res);
1398 } /* while (status == 0) */
1401 snmp_free_pdu (res);
1405 csnmp_dispatch_table (host, data, instance_list, value_table);
1407 /* Free all allocated variables here */
1408 while (instance_list != NULL)
1410 instance_list_ptr = instance_list->next;
1411 sfree (instance_list);
1412 instance_list = instance_list_ptr;
1415 for (i = 0; i < data->values_len; i++)
1417 csnmp_table_values_t *tmp;
1418 while (value_table[i] != NULL)
1420 tmp = value_table[i]->next;
1421 sfree (value_table[i]);
1422 value_table[i] = tmp;
1426 sfree (value_table);
1430 } /* int csnmp_read_table */
1432 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1434 struct snmp_pdu *req;
1435 struct snmp_pdu *res;
1436 struct variable_list *vb;
1438 const data_set_t *ds;
1439 value_list_t vl = VALUE_LIST_INIT;
1444 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1445 host->name, data->name);
1447 if (host->sess_handle == NULL)
1449 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1453 ds = plugin_get_ds (data->type);
1456 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1460 if (ds->ds_num != data->values_len)
1462 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1463 data->type, ds->ds_num, data->values_len);
1467 vl.values_len = ds->ds_num;
1468 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1469 if (vl.values == NULL)
1471 for (i = 0; i < vl.values_len; i++)
1473 if (ds->ds[i].type == DS_TYPE_COUNTER)
1474 vl.values[i].counter = 0;
1476 vl.values[i].gauge = NAN;
1479 sstrncpy (vl.host, host->name, sizeof (vl.host));
1480 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1481 sstrncpy (vl.type, data->type, sizeof (vl.type));
1482 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1484 vl.interval = host->interval;
1486 req = snmp_pdu_create (SNMP_MSG_GET);
1489 ERROR ("snmp plugin: snmp_pdu_create failed.");
1494 for (i = 0; i < data->values_len; i++)
1495 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1498 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1500 if ((status != STAT_SUCCESS) || (res == NULL))
1502 char *errstr = NULL;
1504 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1505 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1506 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1509 snmp_free_pdu (res);
1513 csnmp_host_close_session (host);
1519 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1523 snprint_variable (buffer, sizeof (buffer),
1524 vb->name, vb->name_length, vb);
1525 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1526 #endif /* COLLECT_DEBUG */
1528 for (i = 0; i < data->values_len; i++)
1529 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1530 vb->name, vb->name_length) == 0)
1531 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1532 data->scale, data->shift);
1533 } /* for (res->variables) */
1536 snmp_free_pdu (res);
1539 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1540 plugin_dispatch_values (&vl);
1544 } /* int csnmp_read_value */
1546 static int csnmp_read_host (user_data_t *ud)
1548 host_definition_t *host;
1557 if (host->interval == 0)
1558 host->interval = interval_g;
1560 time_start = time (NULL);
1561 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1562 (unsigned int) time_start);
1564 if (host->sess_handle == NULL)
1565 csnmp_host_open_session (host);
1567 if (host->sess_handle == NULL)
1571 for (i = 0; i < host->data_list_len; i++)
1573 data_definition_t *data = host->data_list[i];
1576 status = csnmp_read_table (host, data);
1578 status = csnmp_read_value (host, data);
1584 time_end = time (NULL);
1585 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1586 (unsigned int) time_end);
1587 if ((uint32_t) (time_end - time_start) > host->interval)
1589 WARNING ("snmp plugin: Host `%s' should be queried every %"PRIu32
1590 " seconds, but reading all values takes %u seconds.",
1591 host->name, host->interval, (unsigned int) (time_end - time_start));
1598 } /* int csnmp_read_host */
1600 static int csnmp_init (void)
1602 call_snmp_init_once ();
1605 } /* int csnmp_init */
1607 static int csnmp_shutdown (void)
1609 data_definition_t *data_this;
1610 data_definition_t *data_next;
1612 /* When we get here, the read threads have been stopped and all the
1613 * `host_definition_t' will be freed. */
1614 DEBUG ("snmp plugin: Destroying all data definitions.");
1616 data_this = data_head;
1618 while (data_this != NULL)
1620 data_next = data_this->next;
1622 sfree (data_this->name);
1623 sfree (data_this->type);
1624 sfree (data_this->values);
1627 data_this = data_next;
1631 } /* int csnmp_shutdown */
1633 void module_register (void)
1635 plugin_register_complex_config ("snmp", csnmp_config);
1636 plugin_register_init ("snmp", csnmp_init);
1637 plugin_register_shutdown ("snmp", csnmp_shutdown);
1638 } /* void module_register */
1641 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker