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
163 static void call_snmp_init_once (void)
165 static int have_init = 0;
168 init_snmp (PACKAGE_NAME);
170 } /* void call_snmp_init_once */
172 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
174 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
176 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
181 dd->type = strdup (ci->values[0].value.string);
182 if (dd->type == NULL)
186 } /* int csnmp_config_add_data_type */
188 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
190 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
192 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
196 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
199 } /* int csnmp_config_add_data_table */
201 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
203 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
205 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
211 /* Instance is an OID */
212 dd->instance.oid.oid_len = MAX_OID_LEN;
214 if (!read_objid (ci->values[0].value.string,
215 dd->instance.oid.oid, &dd->instance.oid.oid_len))
217 ERROR ("snmp plugin: read_objid (%s) failed.",
218 ci->values[0].value.string);
224 /* Instance is a simple string */
225 sstrncpy (dd->instance.string, ci->values[0].value.string,
226 sizeof (dd->instance.string));
230 } /* int csnmp_config_add_data_instance */
232 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
235 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
237 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
243 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
244 "is set to `false'.", dd->name);
248 sfree (dd->instance_prefix);
249 dd->instance_prefix = strdup (ci->values[0].value.string);
250 if (dd->instance_prefix == NULL)
254 } /* int csnmp_config_add_data_instance_prefix */
256 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
260 if (ci->values_num < 1)
262 WARNING ("snmp plugin: `Values' needs at least one argument.");
266 for (i = 0; i < ci->values_num; i++)
267 if (ci->values[i].type != OCONFIG_TYPE_STRING)
269 WARNING ("snmp plugin: `Values' needs only string argument.");
275 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
276 if (dd->values == NULL)
278 dd->values_len = ci->values_num;
280 for (i = 0; i < ci->values_num; i++)
282 dd->values[i].oid_len = MAX_OID_LEN;
284 if (NULL == snmp_parse_oid (ci->values[i].value.string,
285 dd->values[i].oid, &dd->values[i].oid_len))
287 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
288 ci->values[i].value.string);
297 } /* int csnmp_config_add_data_instance */
299 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
301 if ((ci->values_num != 1)
302 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
304 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
308 dd->shift = ci->values[0].value.number;
311 } /* int csnmp_config_add_data_shift */
313 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
315 if ((ci->values_num != 1)
316 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
318 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
322 dd->scale = ci->values[0].value.number;
325 } /* int csnmp_config_add_data_scale */
327 static int csnmp_config_add_data (oconfig_item_t *ci)
329 data_definition_t *dd;
333 if ((ci->values_num != 1)
334 || (ci->values[0].type != OCONFIG_TYPE_STRING))
336 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
340 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
343 memset (dd, '\0', sizeof (data_definition_t));
345 dd->name = strdup (ci->values[0].value.string);
346 if (dd->name == NULL)
354 for (i = 0; i < ci->children_num; i++)
356 oconfig_item_t *option = ci->children + i;
359 if (strcasecmp ("Type", option->key) == 0)
360 status = csnmp_config_add_data_type (dd, option);
361 else if (strcasecmp ("Table", option->key) == 0)
362 status = csnmp_config_add_data_table (dd, option);
363 else if (strcasecmp ("Instance", option->key) == 0)
364 status = csnmp_config_add_data_instance (dd, option);
365 else if (strcasecmp ("InstancePrefix", option->key) == 0)
366 status = csnmp_config_add_data_instance_prefix (dd, option);
367 else if (strcasecmp ("Values", option->key) == 0)
368 status = csnmp_config_add_data_values (dd, option);
369 else if (strcasecmp ("Shift", option->key) == 0)
370 status = csnmp_config_add_data_shift (dd, option);
371 else if (strcasecmp ("Scale", option->key) == 0)
372 status = csnmp_config_add_data_scale (dd, option);
375 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
381 } /* for (ci->children) */
385 if (dd->type == NULL)
387 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
391 if (dd->values == NULL)
393 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
399 } /* while (status == 0) */
404 sfree (dd->instance_prefix);
410 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
411 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
413 if (data_head == NULL)
417 data_definition_t *last;
419 while (last->next != NULL)
425 } /* int csnmp_config_add_data */
427 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
429 if ((ci->values_num != 1)
430 || (ci->values[0].type != OCONFIG_TYPE_STRING))
432 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
436 if (hd->address == NULL)
439 hd->address = strdup (ci->values[0].value.string);
440 if (hd->address == NULL)
443 DEBUG ("snmp plugin: host = %s; host->address = %s;",
444 hd->name, hd->address);
447 } /* int csnmp_config_add_host_address */
449 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
451 if ((ci->values_num != 1)
452 || (ci->values[0].type != OCONFIG_TYPE_STRING))
454 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
458 if (hd->community == NULL)
459 free (hd->community);
461 hd->community = strdup (ci->values[0].value.string);
462 if (hd->community == NULL)
465 DEBUG ("snmp plugin: host = %s; host->community = %s;",
466 hd->name, hd->community);
469 } /* int csnmp_config_add_host_community */
471 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
475 if ((ci->values_num != 1)
476 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
478 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
482 version = (int) ci->values[0].value.number;
483 if ((version != 1) && (version != 2))
485 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
489 hd->version = version;
492 } /* int csnmp_config_add_host_address */
494 static int csnmp_config_add_host_collect (host_definition_t *host,
497 data_definition_t *data;
498 data_definition_t **data_list;
502 if (ci->values_num < 1)
504 WARNING ("snmp plugin: `Collect' needs at least one argument.");
508 for (i = 0; i < ci->values_num; i++)
509 if (ci->values[i].type != OCONFIG_TYPE_STRING)
511 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
515 data_list_len = host->data_list_len + ci->values_num;
516 data_list = (data_definition_t **) realloc (host->data_list,
517 sizeof (data_definition_t *) * data_list_len);
518 if (data_list == NULL)
520 host->data_list = data_list;
522 for (i = 0; i < ci->values_num; i++)
524 for (data = data_head; data != NULL; data = data->next)
525 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
530 WARNING ("snmp plugin: No such data configured: `%s'",
531 ci->values[i].value.string);
535 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
536 host->name, host->data_list_len, data->name);
538 host->data_list[host->data_list_len] = data;
539 host->data_list_len++;
540 } /* for (values_num) */
543 } /* int csnmp_config_add_host_collect */
545 static int csnmp_config_add_host (oconfig_item_t *ci)
547 host_definition_t *hd;
551 /* Registration stuff. */
552 char cb_name[DATA_MAX_NAME_LEN];
554 struct timespec cb_interval;
556 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
558 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
562 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
565 memset (hd, '\0', sizeof (host_definition_t));
567 C_COMPLAIN_INIT (&hd->complaint);
569 hd->name = strdup (ci->values[0].value.string);
570 if (hd->name == NULL)
576 hd->sess_handle = NULL;
579 for (i = 0; i < ci->children_num; i++)
581 oconfig_item_t *option = ci->children + i;
584 if (strcasecmp ("Address", option->key) == 0)
585 status = csnmp_config_add_host_address (hd, option);
586 else if (strcasecmp ("Community", option->key) == 0)
587 status = csnmp_config_add_host_community (hd, option);
588 else if (strcasecmp ("Version", option->key) == 0)
589 status = csnmp_config_add_host_version (hd, option);
590 else if (strcasecmp ("Collect", option->key) == 0)
591 csnmp_config_add_host_collect (hd, option);
592 else if (strcasecmp ("Interval", option->key) == 0)
593 cf_util_get_cdtime (option, &hd->interval);
596 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
602 } /* for (ci->children) */
606 if (hd->address == NULL)
608 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
612 if (hd->community == NULL)
614 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
620 } /* while (status == 0) */
624 csnmp_host_definition_destroy (hd);
628 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
629 hd->name, hd->address, hd->community, hd->version);
631 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
633 memset (&cb_data, 0, sizeof (cb_data));
635 cb_data.free_func = csnmp_host_definition_destroy;
637 CDTIME_T_TO_TIMESPEC (hd->interval, &cb_interval);
639 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
640 csnmp_read_host, /* interval = */ &cb_interval,
641 /* user_data = */ &cb_data);
644 ERROR ("snmp plugin: Registering complex read function failed.");
645 csnmp_host_definition_destroy (hd);
650 } /* int csnmp_config_add_host */
652 static int csnmp_config (oconfig_item_t *ci)
656 call_snmp_init_once ();
658 for (i = 0; i < ci->children_num; i++)
660 oconfig_item_t *child = ci->children + i;
661 if (strcasecmp ("Data", child->key) == 0)
662 csnmp_config_add_data (child);
663 else if (strcasecmp ("Host", child->key) == 0)
664 csnmp_config_add_host (child);
667 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
669 } /* for (ci->children) */
672 } /* int csnmp_config */
674 /* }}} End of the config stuff. Now the interesting part begins */
676 static void csnmp_host_open_session (host_definition_t *host)
678 struct snmp_session sess;
680 if (host->sess_handle != NULL)
681 csnmp_host_close_session (host);
683 snmp_sess_init (&sess);
684 sess.peername = host->address;
685 sess.community = (u_char *) host->community;
686 sess.community_len = strlen (host->community);
687 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
689 /* snmp_sess_open will copy the `struct snmp_session *'. */
690 host->sess_handle = snmp_sess_open (&sess);
692 if (host->sess_handle == NULL)
696 snmp_error (&sess, NULL, NULL, &errstr);
698 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
699 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
702 } /* void csnmp_host_open_session */
704 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
705 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
706 double scale, double shift)
709 uint64_t tmp_unsigned = 0;
710 int64_t tmp_signed = 0;
713 if ((vl->type == ASN_INTEGER)
714 || (vl->type == ASN_UINTEGER)
715 || (vl->type == ASN_COUNTER)
717 || (vl->type == ASN_TIMETICKS)
719 || (vl->type == ASN_GAUGE))
721 tmp_unsigned = (uint32_t) *vl->val.integer;
722 tmp_signed = (int32_t) *vl->val.integer;
723 DEBUG ("snmp plugin: Parsed int32 value is %"PRIi64".", tmp_signed);
725 else if (vl->type == ASN_COUNTER64)
727 tmp_unsigned = (uint32_t) vl->val.counter64->high;
728 tmp_unsigned = tmp_unsigned << 32;
729 tmp_unsigned += (uint32_t) vl->val.counter64->low;
730 tmp_signed = (int64_t) tmp_unsigned;
731 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
733 else if (vl->type == ASN_OCTET_STR)
735 /* We'll handle this later.. */
739 char oid_buffer[1024];
741 memset (oid_buffer, 0, sizeof (oid_buffer));
742 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
743 vl->name, vl->name_length);
746 if (vl->type == ASN_NULL)
747 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
751 WARNING ("snmp plugin: I don't know the ASN type \"%i\" (OID: %s)",
752 (int) vl->type, oid_buffer);
757 if (vl->type == ASN_OCTET_STR)
761 if (vl->val.string != NULL)
764 size_t string_length;
766 string_length = sizeof (string) - 1;
767 if (vl->val_len < string_length)
768 string_length = vl->val_len;
770 /* The strings we get from the Net-SNMP library may not be null
771 * terminated. That is why we're using `memcpy' here and not `strcpy'.
772 * `string_length' is set to `vl->val_len' which holds the length of the
774 memcpy (string, vl->val.string, string_length);
775 string[string_length] = 0;
777 status = parse_value (string, &ret, type);
780 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
781 DS_TYPE_TO_STRING (type), string);
789 case DS_TYPE_COUNTER:
791 case DS_TYPE_ABSOLUTE:
792 memset (&ret, 0, sizeof (ret));
800 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
801 "data source type: %i.", type);
805 } /* if (vl->type == ASN_OCTET_STR) */
806 else if (type == DS_TYPE_COUNTER)
808 ret.counter = tmp_unsigned;
810 else if (type == DS_TYPE_GAUGE)
814 ret.gauge = (scale * tmp_signed) + shift;
816 else if (type == DS_TYPE_DERIVE)
817 ret.derive = (derive_t) tmp_signed;
818 else if (type == DS_TYPE_ABSOLUTE)
819 ret.absolute = (absolute_t) tmp_unsigned;
822 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
828 } /* value_t csnmp_value_list_to_value */
830 /* Returns true if all OIDs have left their subtree */
831 static int csnmp_check_res_left_subtree (const host_definition_t *host,
832 const data_definition_t *data,
833 struct snmp_pdu *res)
835 struct variable_list *vb;
837 int num_left_subtree;
845 num_left_subtree = 0;
847 /* check all the variables and count how many have left their subtree */
848 for (vb = res->variables, i = 0;
849 (vb != NULL) && (i < data->values_len);
850 vb = vb->next_variable, i++)
853 if (snmp_oid_ncompare (data->values[i].oid,
854 data->values[i].oid_len,
855 vb->name, vb->name_length,
856 data->values[i].oid_len) != 0)
860 /* check if enough variables have been returned */
861 if (i < data->values_len)
863 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
864 host->name, data->values_len, i);
868 if (data->instance.oid.oid_len > 0)
872 ERROR ("snmp plugin: host %s: Expected one more variable for "
873 "the instance..", host->name);
878 if (snmp_oid_ncompare (data->instance.oid.oid,
879 data->instance.oid.oid_len,
880 vb->name, vb->name_length,
881 data->instance.oid.oid_len) != 0)
885 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
886 "left their subtree",
887 num_left_subtree, num_checked);
888 if (num_left_subtree >= num_checked)
891 } /* int csnmp_check_res_left_subtree */
893 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
894 const struct variable_list *vb, size_t dst_size)
901 buffer_free = dst_size;
903 for (i = 0; i < vb->val_len; i++)
907 status = snprintf (buffer_ptr, buffer_free,
908 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
910 if (status >= buffer_free)
912 buffer_ptr += (buffer_free - 1);
914 return (dst_size + (buffer_free - status));
916 else /* if (status < buffer_free) */
918 buffer_ptr += status;
919 buffer_free -= status;
923 return ((int) (dst_size - buffer_free));
924 } /* }}} int csnmp_strvbcopy_hexstring */
926 static int csnmp_strvbcopy (char *dst, /* {{{ */
927 const struct variable_list *vb, size_t dst_size)
933 if (vb->type == ASN_OCTET_STR)
934 src = (char *) vb->val.string;
935 else if (vb->type == ASN_BIT_STR)
936 src = (char *) vb->val.bitstring;
943 num_chars = dst_size - 1;
944 if (num_chars > vb->val_len)
945 num_chars = vb->val_len;
947 for (i = 0; i < num_chars; i++)
949 /* Check for control characters. */
950 if ((unsigned char)src[i] < 32)
951 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
956 return ((int) vb->val_len);
957 } /* }}} int csnmp_strvbcopy */
959 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
960 csnmp_list_instances_t **tail,
961 const struct snmp_pdu *res)
963 csnmp_list_instances_t *il;
964 struct variable_list *vb;
966 /* Set vb on the last variable */
967 for (vb = res->variables;
968 (vb != NULL) && (vb->next_variable != NULL);
969 vb = vb->next_variable)
974 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
977 ERROR ("snmp plugin: malloc failed.");
980 il->subid = vb->name[vb->name_length - 1];
983 /* Get instance name */
984 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
988 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
990 for (ptr = il->instance; *ptr != '\0'; ptr++)
992 if ((*ptr > 0) && (*ptr < 32))
994 else if (*ptr == '/')
997 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1001 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
1002 ssnprintf (il->instance, sizeof (il->instance),
1003 "%llu", val.counter);
1006 /* TODO: Debugging output */
1015 } /* int csnmp_instance_list_add */
1017 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1018 csnmp_list_instances_t *instance_list,
1019 csnmp_table_values_t **value_table)
1021 const data_set_t *ds;
1022 value_list_t vl = VALUE_LIST_INIT;
1024 csnmp_list_instances_t *instance_list_ptr;
1025 csnmp_table_values_t **value_table_ptr;
1031 ds = plugin_get_ds (data->type);
1034 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1037 assert (ds->ds_num == data->values_len);
1039 instance_list_ptr = instance_list;
1041 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1042 * data->values_len);
1043 if (value_table_ptr == NULL)
1045 for (i = 0; i < data->values_len; i++)
1046 value_table_ptr[i] = value_table[i];
1048 vl.values_len = ds->ds_num;
1049 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1050 if (vl.values == NULL)
1052 ERROR ("snmp plugin: malloc failed.");
1053 sfree (value_table_ptr);
1057 sstrncpy (vl.host, host->name, sizeof (vl.host));
1058 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1060 vl.interval = host->interval;
1065 while (have_more != 0)
1067 if (instance_list != NULL)
1069 while ((instance_list_ptr != NULL)
1070 && (instance_list_ptr->subid < subid))
1071 instance_list_ptr = instance_list_ptr->next;
1073 if (instance_list_ptr == NULL)
1078 else if (instance_list_ptr->subid > subid)
1080 subid = instance_list_ptr->subid;
1083 } /* if (instance_list != NULL) */
1085 for (i = 0; i < data->values_len; i++)
1087 while ((value_table_ptr[i] != NULL)
1088 && (value_table_ptr[i]->subid < subid))
1089 value_table_ptr[i] = value_table_ptr[i]->next;
1091 if (value_table_ptr[i] == NULL)
1096 else if (value_table_ptr[i]->subid > subid)
1098 subid = value_table_ptr[i]->subid;
1101 } /* for (i = 0; i < columns; i++) */
1102 /* The subid has been increased - start scanning from the beginning
1104 if (i < data->values_len)
1107 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1108 * to the same subid. instance_list_ptr is either NULL or points to the
1109 * same subid, too. */
1111 for (i = 1; i < data->values_len; i++)
1113 assert (value_table_ptr[i] != NULL);
1114 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
1116 assert ((instance_list_ptr == NULL)
1117 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
1120 sstrncpy (vl.type, data->type, sizeof (vl.type));
1123 char temp[DATA_MAX_NAME_LEN];
1125 if (instance_list_ptr == NULL)
1126 ssnprintf (temp, sizeof (temp), "%"PRIu32, (uint32_t) subid);
1128 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1130 if (data->instance_prefix == NULL)
1131 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1133 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1134 data->instance_prefix, temp);
1137 for (i = 0; i < data->values_len; i++)
1138 vl.values[i] = value_table_ptr[i]->value;
1140 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1141 plugin_dispatch_values (&vl);
1144 } /* while (have_more != 0) */
1147 sfree (value_table_ptr);
1150 } /* int csnmp_dispatch_table */
1152 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1154 struct snmp_pdu *req;
1155 struct snmp_pdu *res;
1156 struct variable_list *vb;
1158 const data_set_t *ds;
1160 uint32_t oid_list_len;
1165 /* `value_table' and `value_table_ptr' implement a linked list for each
1166 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1167 * instance names. This is used to jump gaps in the table. */
1168 csnmp_list_instances_t *instance_list;
1169 csnmp_list_instances_t *instance_list_ptr;
1170 csnmp_table_values_t **value_table;
1171 csnmp_table_values_t **value_table_ptr;
1173 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1174 host->name, data->name);
1176 if (host->sess_handle == NULL)
1178 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1182 ds = plugin_get_ds (data->type);
1185 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1189 if (ds->ds_num != data->values_len)
1191 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1192 data->type, ds->ds_num, data->values_len);
1196 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1197 oid_list_len = data->values_len + 1;
1198 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1199 if (oid_list == NULL)
1201 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1204 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1205 if (data->instance.oid.oid_len > 0)
1206 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1210 /* Allocate the `value_table' */
1211 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1212 * 2 * data->values_len);
1213 if (value_table == NULL)
1215 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1219 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1220 value_table_ptr = value_table + data->values_len;
1222 instance_list = NULL;
1223 instance_list_ptr = NULL;
1228 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1231 ERROR ("snmp plugin: snmp_pdu_create failed.");
1236 for (i = 0; (uint32_t) i < oid_list_len; i++)
1237 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1240 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1242 if ((status != STAT_SUCCESS) || (res == NULL))
1244 char *errstr = NULL;
1246 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1248 c_complain (LOG_ERR, &host->complaint,
1249 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1250 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1253 snmp_free_pdu (res);
1257 csnmp_host_close_session (host);
1263 assert (res != NULL);
1264 c_release (LOG_INFO, &host->complaint,
1265 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1268 vb = res->variables;
1275 /* Check if all values (and possibly the instance) have left their
1277 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1283 /* if an instance-OID is configured.. */
1284 if (data->instance.oid.oid_len > 0)
1286 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1287 * add it to the list */
1288 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1291 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1296 /* Set vb on the last variable */
1297 for (vb = res->variables;
1298 (vb != NULL) && (vb->next_variable != NULL);
1299 vb = vb->next_variable)
1301 assert (vb != NULL);
1303 /* Copy OID to oid_list[data->values_len] */
1304 memcpy (oid_list[data->values_len].oid, vb->name,
1305 sizeof (oid) * vb->name_length);
1306 oid_list[data->values_len].oid_len = vb->name_length;
1309 for (vb = res->variables, i = 0;
1310 (vb != NULL) && (i < data->values_len);
1311 vb = vb->next_variable, i++)
1313 csnmp_table_values_t *vt;
1315 /* Check if we left the subtree */
1316 if (snmp_oid_ncompare (data->values[i].oid,
1317 data->values[i].oid_len,
1318 vb->name, vb->name_length,
1319 data->values[i].oid_len) != 0)
1321 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1322 host->name, data->name, i);
1326 if ((value_table_ptr[i] != NULL)
1327 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1329 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1330 "SUBID is not increasing.",
1331 host->name, data->name, i);
1335 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1338 ERROR ("snmp plugin: malloc failed.");
1343 vt->subid = vb->name[vb->name_length - 1];
1344 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1345 data->scale, data->shift);
1348 if (value_table_ptr[i] == NULL)
1349 value_table[i] = vt;
1351 value_table_ptr[i]->next = vt;
1352 value_table_ptr[i] = vt;
1354 /* Copy OID to oid_list[i + 1] */
1355 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1356 oid_list[i].oid_len = vb->name_length;
1357 } /* for (i = data->values_len) */
1360 snmp_free_pdu (res);
1362 } /* while (status == 0) */
1365 snmp_free_pdu (res);
1369 csnmp_dispatch_table (host, data, instance_list, value_table);
1371 /* Free all allocated variables here */
1372 while (instance_list != NULL)
1374 instance_list_ptr = instance_list->next;
1375 sfree (instance_list);
1376 instance_list = instance_list_ptr;
1379 for (i = 0; i < data->values_len; i++)
1381 csnmp_table_values_t *tmp;
1382 while (value_table[i] != NULL)
1384 tmp = value_table[i]->next;
1385 sfree (value_table[i]);
1386 value_table[i] = tmp;
1390 sfree (value_table);
1394 } /* int csnmp_read_table */
1396 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1398 struct snmp_pdu *req;
1399 struct snmp_pdu *res;
1400 struct variable_list *vb;
1402 const data_set_t *ds;
1403 value_list_t vl = VALUE_LIST_INIT;
1408 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1409 host->name, data->name);
1411 if (host->sess_handle == NULL)
1413 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1417 ds = plugin_get_ds (data->type);
1420 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1424 if (ds->ds_num != data->values_len)
1426 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1427 data->type, ds->ds_num, data->values_len);
1431 vl.values_len = ds->ds_num;
1432 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1433 if (vl.values == NULL)
1435 for (i = 0; i < vl.values_len; i++)
1437 if (ds->ds[i].type == DS_TYPE_COUNTER)
1438 vl.values[i].counter = 0;
1440 vl.values[i].gauge = NAN;
1443 sstrncpy (vl.host, host->name, sizeof (vl.host));
1444 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1445 sstrncpy (vl.type, data->type, sizeof (vl.type));
1446 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1448 vl.interval = host->interval;
1450 req = snmp_pdu_create (SNMP_MSG_GET);
1453 ERROR ("snmp plugin: snmp_pdu_create failed.");
1458 for (i = 0; i < data->values_len; i++)
1459 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1462 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1464 if ((status != STAT_SUCCESS) || (res == NULL))
1466 char *errstr = NULL;
1468 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1469 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1470 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1473 snmp_free_pdu (res);
1477 csnmp_host_close_session (host);
1483 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1487 snprint_variable (buffer, sizeof (buffer),
1488 vb->name, vb->name_length, vb);
1489 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1490 #endif /* COLLECT_DEBUG */
1492 for (i = 0; i < data->values_len; i++)
1493 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1494 vb->name, vb->name_length) == 0)
1495 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1496 data->scale, data->shift);
1497 } /* for (res->variables) */
1500 snmp_free_pdu (res);
1503 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1504 plugin_dispatch_values (&vl);
1508 } /* int csnmp_read_value */
1510 static int csnmp_read_host (user_data_t *ud)
1512 host_definition_t *host;
1513 cdtime_t time_start;
1521 if (host->interval == 0)
1522 host->interval = interval_g;
1524 time_start = cdtime ();
1526 if (host->sess_handle == NULL)
1527 csnmp_host_open_session (host);
1529 if (host->sess_handle == NULL)
1533 for (i = 0; i < host->data_list_len; i++)
1535 data_definition_t *data = host->data_list[i];
1538 status = csnmp_read_table (host, data);
1540 status = csnmp_read_value (host, data);
1546 time_end = cdtime ();
1547 if ((time_end - time_start) > host->interval)
1549 WARNING ("snmp plugin: Host `%s' should be queried every %.3f "
1550 "seconds, but reading all values takes %.3f seconds.",
1552 CDTIME_T_TO_DOUBLE (host->interval),
1553 CDTIME_T_TO_DOUBLE (time_end - time_start));
1560 } /* int csnmp_read_host */
1562 static int csnmp_init (void)
1564 call_snmp_init_once ();
1567 } /* int csnmp_init */
1569 static int csnmp_shutdown (void)
1571 data_definition_t *data_this;
1572 data_definition_t *data_next;
1574 /* When we get here, the read threads have been stopped and all the
1575 * `host_definition_t' will be freed. */
1576 DEBUG ("snmp plugin: Destroying all data definitions.");
1578 data_this = data_head;
1580 while (data_this != NULL)
1582 data_next = data_this->next;
1584 sfree (data_this->name);
1585 sfree (data_this->type);
1586 sfree (data_this->values);
1589 data_this = data_next;
1593 } /* int csnmp_shutdown */
1595 void module_register (void)
1597 plugin_register_complex_config ("snmp", csnmp_config);
1598 plugin_register_init ("snmp", csnmp_init);
1599 plugin_register_shutdown ("snmp", csnmp_shutdown);
1600 } /* void module_register */
1603 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker