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 (cb_name, csnmp_read_host,
659 /* interval = */ &cb_interval, /* user_data = */ &cb_data);
662 ERROR ("snmp plugin: Registering complex read function failed.");
663 csnmp_host_definition_destroy (hd);
668 } /* int csnmp_config_add_host */
670 static int csnmp_config (oconfig_item_t *ci)
674 call_snmp_init_once ();
676 for (i = 0; i < ci->children_num; i++)
678 oconfig_item_t *child = ci->children + i;
679 if (strcasecmp ("Data", child->key) == 0)
680 csnmp_config_add_data (child);
681 else if (strcasecmp ("Host", child->key) == 0)
682 csnmp_config_add_host (child);
685 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
687 } /* for (ci->children) */
690 } /* int csnmp_config */
692 /* }}} End of the config stuff. Now the interesting part begins */
694 static void csnmp_host_open_session (host_definition_t *host)
696 struct snmp_session sess;
698 if (host->sess_handle != NULL)
699 csnmp_host_close_session (host);
701 snmp_sess_init (&sess);
702 sess.peername = host->address;
703 sess.community = (u_char *) host->community;
704 sess.community_len = strlen (host->community);
705 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
707 /* snmp_sess_open will copy the `struct snmp_session *'. */
708 host->sess_handle = snmp_sess_open (&sess);
710 if (host->sess_handle == NULL)
714 snmp_error (&sess, NULL, NULL, &errstr);
716 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
717 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
720 } /* void csnmp_host_open_session */
722 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
723 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
724 double scale, double shift)
730 if ((vl->type == ASN_INTEGER)
731 || (vl->type == ASN_UINTEGER)
732 || (vl->type == ASN_COUNTER)
734 || (vl->type == ASN_TIMETICKS)
736 || (vl->type == ASN_GAUGE))
738 temp = (uint32_t) *vl->val.integer;
739 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", temp);
741 else if (vl->type == ASN_COUNTER64)
743 temp = (uint32_t) vl->val.counter64->high;
745 temp += (uint32_t) vl->val.counter64->low;
746 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", temp);
748 else if (vl->type == ASN_OCTET_STR)
750 /* We'll handle this later.. */
754 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
758 if (vl->type == ASN_OCTET_STR)
763 if (vl->val.string != NULL)
766 size_t string_length;
768 string_length = sizeof (string) - 1;
769 if (vl->val_len < string_length)
770 string_length = vl->val_len;
772 /* The strings we get from the Net-SNMP library may not be null
773 * terminated. That is why we're using `membpy' here and not `strcpy'.
774 * `string_length' is set to `vl->val_len' which holds the length of the
776 memcpy (string, vl->val.string, string_length);
777 string[string_length] = 0;
779 if (type == DS_TYPE_COUNTER)
781 ret.counter = (counter_t) strtoll (string, &endptr, /* base = */ 0);
782 DEBUG ("snmp plugin: csnmp_value_list_to_value: String to counter: %s -> %llu",
783 string, (unsigned long long) ret.counter);
785 else if (type == DS_TYPE_GAUGE)
787 ret.gauge = (gauge_t) strtod (string, &endptr);
788 DEBUG ("snmp plugin: csnmp_value_list_to_value: String to gauge: %s -> %g",
789 string, (double) ret.gauge);
793 /* Check if an error occurred */
794 if ((vl->val.string == NULL) || (endptr == (char *) vl->val.string))
796 if (type == DS_TYPE_COUNTER)
798 else if (type == DS_TYPE_GAUGE)
802 else if (type == DS_TYPE_COUNTER)
806 else if (type == DS_TYPE_GAUGE)
810 ret.gauge = (scale * temp) + shift;
814 } /* value_t csnmp_value_list_to_value */
816 /* Returns true if all OIDs have left their subtree */
817 static int csnmp_check_res_left_subtree (const host_definition_t *host,
818 const data_definition_t *data,
819 struct snmp_pdu *res)
821 struct variable_list *vb;
823 int num_left_subtree;
831 num_left_subtree = 0;
833 /* check all the variables and count how many have left their subtree */
834 for (vb = res->variables, i = 0;
835 (vb != NULL) && (i < data->values_len);
836 vb = vb->next_variable, i++)
839 if (snmp_oid_ncompare (data->values[i].oid,
840 data->values[i].oid_len,
841 vb->name, vb->name_length,
842 data->values[i].oid_len) != 0)
846 /* check if enough variables have been returned */
847 if (i < data->values_len)
849 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
850 host->name, data->values_len, i);
854 if (data->instance.oid.oid_len > 0)
858 ERROR ("snmp plugin: host %s: Expected one more variable for "
859 "the instance..", host->name);
864 if (snmp_oid_ncompare (data->instance.oid.oid,
865 data->instance.oid.oid_len,
866 vb->name, vb->name_length,
867 data->instance.oid.oid_len) != 0)
871 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
872 "left their subtree",
873 num_left_subtree, num_checked);
874 if (num_left_subtree >= num_checked)
877 } /* int csnmp_check_res_left_subtree */
879 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
880 csnmp_list_instances_t **tail,
881 const struct snmp_pdu *res)
883 csnmp_list_instances_t *il;
884 struct variable_list *vb;
886 /* Set vb on the last variable */
887 for (vb = res->variables;
888 (vb != NULL) && (vb->next_variable != NULL);
889 vb = vb->next_variable)
894 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
897 ERROR ("snmp plugin: malloc failed.");
900 il->subid = vb->name[vb->name_length - 1];
903 /* Get instance name */
904 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
909 memset (il->instance, 0, sizeof (il->instance));
910 instance_len = sizeof (il->instance) - 1;
911 if (instance_len > vb->val_len)
912 instance_len = vb->val_len;
914 sstrncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
916 : vb->val.bitstring),
919 for (ptr = il->instance; *ptr != '\0'; ptr++)
921 if ((*ptr > 0) && (*ptr < 32))
923 else if (*ptr == '/')
926 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
930 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
931 ssnprintf (il->instance, sizeof (il->instance),
932 "%llu", val.counter);
935 /* TODO: Debugging output */
944 } /* int csnmp_instance_list_add */
946 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
947 csnmp_list_instances_t *instance_list,
948 csnmp_table_values_t **value_table)
950 const data_set_t *ds;
951 value_list_t vl = VALUE_LIST_INIT;
953 csnmp_list_instances_t *instance_list_ptr;
954 csnmp_table_values_t **value_table_ptr;
960 ds = plugin_get_ds (data->type);
963 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
966 assert (ds->ds_num == data->values_len);
968 instance_list_ptr = instance_list;
970 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
972 if (value_table_ptr == NULL)
974 for (i = 0; i < data->values_len; i++)
975 value_table_ptr[i] = value_table[i];
977 vl.values_len = ds->ds_num;
978 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
979 if (vl.values == NULL)
981 ERROR ("snmp plugin: malloc failed.");
982 sfree (value_table_ptr);
986 sstrncpy (vl.host, host->name, sizeof (vl.host));
987 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
989 vl.interval = host->interval;
994 while (have_more != 0)
996 if (instance_list != NULL)
998 while ((instance_list_ptr != NULL)
999 && (instance_list_ptr->subid < subid))
1000 instance_list_ptr = instance_list_ptr->next;
1002 if (instance_list_ptr == NULL)
1007 else if (instance_list_ptr->subid > subid)
1009 subid = instance_list_ptr->subid;
1012 } /* if (instance_list != NULL) */
1014 for (i = 0; i < data->values_len; i++)
1016 while ((value_table_ptr[i] != NULL)
1017 && (value_table_ptr[i]->subid < subid))
1018 value_table_ptr[i] = value_table_ptr[i]->next;
1020 if (value_table_ptr[i] == NULL)
1025 else if (value_table_ptr[i]->subid > subid)
1027 subid = value_table_ptr[i]->subid;
1030 } /* for (i = 0; i < columns; i++) */
1031 /* The subid has been increased - start scanning from the beginning
1033 if (i < data->values_len)
1036 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1037 * to the same subid. instance_list_ptr is either NULL or points to the
1038 * same subid, too. */
1040 for (i = 1; i < data->values_len; i++)
1042 assert (value_table_ptr[i] != NULL);
1043 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
1045 assert ((instance_list_ptr == NULL)
1046 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
1049 sstrncpy (vl.type, data->type, sizeof (vl.type));
1052 char temp[DATA_MAX_NAME_LEN];
1054 if (instance_list_ptr == NULL)
1055 ssnprintf (temp, sizeof (temp), "%u", (uint32_t) subid);
1057 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1059 if (data->instance_prefix == NULL)
1060 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1062 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1063 data->instance_prefix, temp);
1066 for (i = 0; i < data->values_len; i++)
1067 vl.values[i] = value_table_ptr[i]->value;
1069 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1070 plugin_dispatch_values (&vl);
1073 } /* while (have_more != 0) */
1076 sfree (value_table_ptr);
1079 } /* int csnmp_dispatch_table */
1081 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1083 struct snmp_pdu *req;
1084 struct snmp_pdu *res;
1085 struct variable_list *vb;
1087 const data_set_t *ds;
1089 uint32_t oid_list_len;
1094 /* `value_table' and `value_table_ptr' implement a linked list for each
1095 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1096 * instance names. This is used to jump gaps in the table. */
1097 csnmp_list_instances_t *instance_list;
1098 csnmp_list_instances_t *instance_list_ptr;
1099 csnmp_table_values_t **value_table;
1100 csnmp_table_values_t **value_table_ptr;
1102 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1103 host->name, data->name);
1105 if (host->sess_handle == NULL)
1107 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1111 ds = plugin_get_ds (data->type);
1114 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1118 if (ds->ds_num != data->values_len)
1120 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1121 data->type, ds->ds_num, data->values_len);
1125 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1126 oid_list_len = data->values_len + 1;
1127 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1128 if (oid_list == NULL)
1130 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1133 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1134 if (data->instance.oid.oid_len > 0)
1135 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1139 /* Allocate the `value_table' */
1140 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1141 * 2 * data->values_len);
1142 if (value_table == NULL)
1144 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1148 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1149 value_table_ptr = value_table + data->values_len;
1151 instance_list = NULL;
1152 instance_list_ptr = NULL;
1157 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1160 ERROR ("snmp plugin: snmp_pdu_create failed.");
1165 for (i = 0; (uint32_t) i < oid_list_len; i++)
1166 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1169 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1171 if ((status != STAT_SUCCESS) || (res == NULL))
1173 char *errstr = NULL;
1175 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1177 c_complain (LOG_ERR, &host->complaint,
1178 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1179 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1182 snmp_free_pdu (res);
1186 csnmp_host_close_session (host);
1192 assert (res != NULL);
1193 c_release (LOG_INFO, &host->complaint,
1194 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1197 vb = res->variables;
1204 /* Check if all values (and possibly the instance) have left their
1206 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1212 /* if an instance-OID is configured.. */
1213 if (data->instance.oid.oid_len > 0)
1215 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1216 * add it to the list */
1217 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1220 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1225 /* Set vb on the last variable */
1226 for (vb = res->variables;
1227 (vb != NULL) && (vb->next_variable != NULL);
1228 vb = vb->next_variable)
1230 assert (vb != NULL);
1232 /* Copy OID to oid_list[data->values_len] */
1233 memcpy (oid_list[data->values_len].oid, vb->name,
1234 sizeof (oid) * vb->name_length);
1235 oid_list[data->values_len].oid_len = vb->name_length;
1238 for (vb = res->variables, i = 0;
1239 (vb != NULL) && (i < data->values_len);
1240 vb = vb->next_variable, i++)
1242 csnmp_table_values_t *vt;
1244 /* Check if we left the subtree */
1245 if (snmp_oid_ncompare (data->values[i].oid,
1246 data->values[i].oid_len,
1247 vb->name, vb->name_length,
1248 data->values[i].oid_len) != 0)
1250 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1251 host->name, data->name, i);
1255 if ((value_table_ptr[i] != NULL)
1256 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1258 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1259 "SUBID is not increasing.",
1260 host->name, data->name, i);
1264 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1267 ERROR ("snmp plugin: malloc failed.");
1272 vt->subid = vb->name[vb->name_length - 1];
1273 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1274 data->scale, data->shift);
1277 if (value_table_ptr[i] == NULL)
1278 value_table[i] = vt;
1280 value_table_ptr[i]->next = vt;
1281 value_table_ptr[i] = vt;
1283 /* Copy OID to oid_list[i + 1] */
1284 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1285 oid_list[i].oid_len = vb->name_length;
1286 } /* for (i = data->values_len) */
1289 snmp_free_pdu (res);
1291 } /* while (status == 0) */
1294 snmp_free_pdu (res);
1298 csnmp_dispatch_table (host, data, instance_list, value_table);
1300 /* Free all allocated variables here */
1301 while (instance_list != NULL)
1303 instance_list_ptr = instance_list->next;
1304 sfree (instance_list);
1305 instance_list = instance_list_ptr;
1308 for (i = 0; i < data->values_len; i++)
1310 csnmp_table_values_t *tmp;
1311 while (value_table[i] != NULL)
1313 tmp = value_table[i]->next;
1314 sfree (value_table[i]);
1315 value_table[i] = tmp;
1319 sfree (value_table);
1323 } /* int csnmp_read_table */
1325 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1327 struct snmp_pdu *req;
1328 struct snmp_pdu *res;
1329 struct variable_list *vb;
1331 const data_set_t *ds;
1332 value_list_t vl = VALUE_LIST_INIT;
1337 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1338 host->name, data->name);
1340 if (host->sess_handle == NULL)
1342 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1346 ds = plugin_get_ds (data->type);
1349 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1353 if (ds->ds_num != data->values_len)
1355 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1356 data->type, ds->ds_num, data->values_len);
1360 vl.values_len = ds->ds_num;
1361 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1362 if (vl.values == NULL)
1364 for (i = 0; i < vl.values_len; i++)
1366 if (ds->ds[i].type == DS_TYPE_COUNTER)
1367 vl.values[i].counter = 0;
1369 vl.values[i].gauge = NAN;
1372 sstrncpy (vl.host, host->name, sizeof (vl.host));
1373 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1374 sstrncpy (vl.type, data->type, sizeof (vl.type));
1375 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1377 vl.interval = host->interval;
1379 req = snmp_pdu_create (SNMP_MSG_GET);
1382 ERROR ("snmp plugin: snmp_pdu_create failed.");
1387 for (i = 0; i < data->values_len; i++)
1388 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1391 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1393 if ((status != STAT_SUCCESS) || (res == NULL))
1395 char *errstr = NULL;
1397 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1398 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1399 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1402 snmp_free_pdu (res);
1406 csnmp_host_close_session (host);
1412 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1416 snprint_variable (buffer, sizeof (buffer),
1417 vb->name, vb->name_length, vb);
1418 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1419 #endif /* COLLECT_DEBUG */
1421 for (i = 0; i < data->values_len; i++)
1422 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1423 vb->name, vb->name_length) == 0)
1424 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1425 data->scale, data->shift);
1426 } /* for (res->variables) */
1429 snmp_free_pdu (res);
1432 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1433 plugin_dispatch_values (&vl);
1437 } /* int csnmp_read_value */
1439 static int csnmp_read_host (user_data_t *ud)
1441 host_definition_t *host;
1450 if (host->interval == 0)
1451 host->interval = interval_g;
1453 time_start = time (NULL);
1454 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1455 (unsigned int) time_start);
1457 if (host->sess_handle == NULL)
1458 csnmp_host_open_session (host);
1460 if (host->sess_handle == NULL)
1464 for (i = 0; i < host->data_list_len; i++)
1466 data_definition_t *data = host->data_list[i];
1469 status = csnmp_read_table (host, data);
1471 status = csnmp_read_value (host, data);
1477 time_end = time (NULL);
1478 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1479 (unsigned int) time_end);
1480 if ((uint32_t) (time_end - time_start) > host->interval)
1482 WARNING ("snmp plugin: Host `%s' should be queried every %"PRIu32
1483 " seconds, but reading all values takes %u seconds.",
1484 host->name, host->interval, (unsigned int) (time_end - time_start));
1491 } /* int csnmp_read_host */
1493 static int csnmp_init (void)
1495 call_snmp_init_once ();
1498 } /* int csnmp_init */
1500 static int csnmp_shutdown (void)
1502 data_definition_t *data_this;
1503 data_definition_t *data_next;
1505 /* When we get here, the read threads have been stopped and all the
1506 * `host_definition_t' will be freed. */
1507 DEBUG ("snmp plugin: Destroying all data definitions.");
1509 data_this = data_head;
1511 while (data_this != NULL)
1513 data_next = data_this->next;
1515 sfree (data_this->name);
1516 sfree (data_this->type);
1517 sfree (data_this->values);
1520 data_this = data_next;
1524 } /* int csnmp_shutdown */
1526 void module_register (void)
1528 plugin_register_complex_config ("snmp", csnmp_config);
1529 plugin_register_init ("snmp", csnmp_init);
1530 plugin_register_shutdown ("snmp", csnmp_shutdown);
1531 } /* void module_register */
1534 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker