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>
28 #include <net-snmp/net-snmp-config.h>
29 #include <net-snmp/net-snmp-includes.h>
32 * Private data structes
39 typedef struct oid_s oid_t;
43 char string[DATA_MAX_NAME_LEN];
46 typedef union instance_u instance_t;
48 struct data_definition_s
50 char *name; /* used to reference this from the `Collect' option */
51 char *type; /* used to find the data_set */
54 char *instance_prefix;
59 struct data_definition_s *next;
61 typedef struct data_definition_s data_definition_t;
63 struct host_definition_s
72 data_definition_t **data_list;
74 enum /******************************************************/
76 STATE_IDLE, /* - just sits there until `next_update < interval_g' */
77 STATE_WAIT, /* - waits to be queried. */
78 STATE_BUSY /* - is currently being queried. */
79 } state; /******************************************************/
80 struct host_definition_s *next;
82 typedef struct host_definition_s host_definition_t;
84 /* These two types are used to cache values in `csnmp_read_table' to handle
86 struct csnmp_list_instances_s
89 char instance[DATA_MAX_NAME_LEN];
90 struct csnmp_list_instances_s *next;
92 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
94 struct csnmp_table_values_s
98 struct csnmp_table_values_s *next;
100 typedef struct csnmp_table_values_s csnmp_table_values_t;
105 static int do_shutdown = 0;
107 pthread_t *threads = NULL;
110 static data_definition_t *data_head = NULL;
111 static host_definition_t *host_head = NULL;
113 static pthread_mutex_t host_lock = PTHREAD_MUTEX_INITIALIZER;
114 static pthread_cond_t host_cond = PTHREAD_COND_INITIALIZER;
119 /* First there are many functions which do configuration stuff. It's a big
120 * bloated and messy, I'm afraid. */
123 * Callgraph for the config stuff:
125 * +-> call_snmp_init_once
126 * +-> csnmp_config_add_data
127 * ! +-> csnmp_config_add_data_type
128 * ! +-> csnmp_config_add_data_table
129 * ! +-> csnmp_config_add_data_instance
130 * ! +-> csnmp_config_add_data_instance_prefix
131 * ! +-> csnmp_config_add_data_values
132 * +-> csnmp_config_add_host
133 * +-> csnmp_config_add_host_address
134 * +-> csnmp_config_add_host_community
135 * +-> csnmp_config_add_host_version
136 * +-> csnmp_config_add_host_collect
137 * +-> csnmp_config_add_host_interval
139 static void call_snmp_init_once (void)
141 static int have_init = 0;
144 init_snmp (PACKAGE_NAME);
146 } /* void call_snmp_init_once */
148 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
150 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
152 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
157 dd->type = strdup (ci->values[0].value.string);
158 if (dd->type == NULL)
162 } /* int csnmp_config_add_data_type */
164 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
166 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
168 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
172 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
175 } /* int csnmp_config_add_data_table */
177 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
179 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
181 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
187 /* Instance is an OID */
188 dd->instance.oid.oid_len = MAX_OID_LEN;
190 if (!read_objid (ci->values[0].value.string,
191 dd->instance.oid.oid, &dd->instance.oid.oid_len))
193 ERROR ("snmp plugin: read_objid (%s) failed.",
194 ci->values[0].value.string);
200 /* Instance is a simple string */
201 sstrncpy (dd->instance.string, ci->values[0].value.string,
202 sizeof (dd->instance.string));
206 } /* int csnmp_config_add_data_instance */
208 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
211 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
213 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
219 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
220 "is set to `false'.", dd->name);
224 sfree (dd->instance_prefix);
225 dd->instance_prefix = strdup (ci->values[0].value.string);
226 if (dd->instance_prefix == NULL)
230 } /* int csnmp_config_add_data_instance_prefix */
232 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
236 if (ci->values_num < 1)
238 WARNING ("snmp plugin: `Values' needs at least one argument.");
242 for (i = 0; i < ci->values_num; i++)
243 if (ci->values[i].type != OCONFIG_TYPE_STRING)
245 WARNING ("snmp plugin: `Values' needs only string argument.");
251 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
252 if (dd->values == NULL)
254 dd->values_len = ci->values_num;
256 for (i = 0; i < ci->values_num; i++)
258 dd->values[i].oid_len = MAX_OID_LEN;
260 if (NULL == snmp_parse_oid (ci->values[i].value.string,
261 dd->values[i].oid, &dd->values[i].oid_len))
263 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
264 ci->values[i].value.string);
273 } /* int csnmp_config_add_data_instance */
275 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
277 if ((ci->values_num != 1)
278 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
280 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
284 dd->shift = ci->values[0].value.number;
287 } /* int csnmp_config_add_data_shift */
289 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
291 if ((ci->values_num != 1)
292 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
294 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
298 dd->scale = ci->values[0].value.number;
301 } /* int csnmp_config_add_data_scale */
303 static int csnmp_config_add_data (oconfig_item_t *ci)
305 data_definition_t *dd;
309 if ((ci->values_num != 1)
310 || (ci->values[0].type != OCONFIG_TYPE_STRING))
312 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
316 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
319 memset (dd, '\0', sizeof (data_definition_t));
321 dd->name = strdup (ci->values[0].value.string);
322 if (dd->name == NULL)
330 for (i = 0; i < ci->children_num; i++)
332 oconfig_item_t *option = ci->children + i;
335 if (strcasecmp ("Type", option->key) == 0)
336 status = csnmp_config_add_data_type (dd, option);
337 else if (strcasecmp ("Table", option->key) == 0)
338 status = csnmp_config_add_data_table (dd, option);
339 else if (strcasecmp ("Instance", option->key) == 0)
340 status = csnmp_config_add_data_instance (dd, option);
341 else if (strcasecmp ("InstancePrefix", option->key) == 0)
342 status = csnmp_config_add_data_instance_prefix (dd, option);
343 else if (strcasecmp ("Values", option->key) == 0)
344 status = csnmp_config_add_data_values (dd, option);
345 else if (strcasecmp ("Shift", option->key) == 0)
346 status = csnmp_config_add_data_shift (dd, option);
347 else if (strcasecmp ("Scale", option->key) == 0)
348 status = csnmp_config_add_data_scale (dd, option);
351 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
357 } /* for (ci->children) */
361 if (dd->type == NULL)
363 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
367 if (dd->values == NULL)
369 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
375 } /* while (status == 0) */
380 sfree (dd->instance_prefix);
386 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
387 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
389 if (data_head == NULL)
393 data_definition_t *last;
395 while (last->next != NULL)
401 } /* int csnmp_config_add_data */
403 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
405 if ((ci->values_num != 1)
406 || (ci->values[0].type != OCONFIG_TYPE_STRING))
408 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
412 if (hd->address == NULL)
415 hd->address = strdup (ci->values[0].value.string);
416 if (hd->address == NULL)
419 DEBUG ("snmp plugin: host = %s; host->address = %s;",
420 hd->name, hd->address);
423 } /* int csnmp_config_add_host_address */
425 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
427 if ((ci->values_num != 1)
428 || (ci->values[0].type != OCONFIG_TYPE_STRING))
430 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
434 if (hd->community == NULL)
435 free (hd->community);
437 hd->community = strdup (ci->values[0].value.string);
438 if (hd->community == NULL)
441 DEBUG ("snmp plugin: host = %s; host->community = %s;",
442 hd->name, hd->community);
445 } /* int csnmp_config_add_host_community */
447 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
451 if ((ci->values_num != 1)
452 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
454 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
458 version = (int) ci->values[0].value.number;
459 if ((version != 1) && (version != 2))
461 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
465 hd->version = version;
468 } /* int csnmp_config_add_host_address */
470 static int csnmp_config_add_host_collect (host_definition_t *host,
473 data_definition_t *data;
474 data_definition_t **data_list;
478 if (ci->values_num < 1)
480 WARNING ("snmp plugin: `Collect' needs at least one argument.");
484 for (i = 0; i < ci->values_num; i++)
485 if (ci->values[i].type != OCONFIG_TYPE_STRING)
487 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
491 data_list_len = host->data_list_len + ci->values_num;
492 data_list = (data_definition_t **) realloc (host->data_list,
493 sizeof (data_definition_t *) * data_list_len);
494 if (data_list == NULL)
496 host->data_list = data_list;
498 for (i = 0; i < ci->values_num; i++)
500 for (data = data_head; data != NULL; data = data->next)
501 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
506 WARNING ("snmp plugin: No such data configured: `%s'",
507 ci->values[i].value.string);
511 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
512 host->name, host->data_list_len, data->name);
514 host->data_list[host->data_list_len] = data;
515 host->data_list_len++;
516 } /* for (values_num) */
519 } /* int csnmp_config_add_host_collect */
521 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
523 if ((ci->values_num != 1)
524 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
526 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
530 hd->interval = (int) ci->values[0].value.number;
531 if (hd->interval < 0)
535 } /* int csnmp_config_add_host_interval */
537 static int csnmp_config_add_host (oconfig_item_t *ci)
539 host_definition_t *hd;
543 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
545 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
549 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
552 memset (hd, '\0', sizeof (host_definition_t));
555 hd->name = strdup (ci->values[0].value.string);
556 if (hd->name == NULL)
562 hd->sess_handle = NULL;
565 hd->state = STATE_IDLE;
567 for (i = 0; i < ci->children_num; i++)
569 oconfig_item_t *option = ci->children + i;
572 if (strcasecmp ("Address", option->key) == 0)
573 status = csnmp_config_add_host_address (hd, option);
574 else if (strcasecmp ("Community", option->key) == 0)
575 status = csnmp_config_add_host_community (hd, option);
576 else if (strcasecmp ("Version", option->key) == 0)
577 status = csnmp_config_add_host_version (hd, option);
578 else if (strcasecmp ("Collect", option->key) == 0)
579 csnmp_config_add_host_collect (hd, option);
580 else if (strcasecmp ("Interval", option->key) == 0)
581 csnmp_config_add_host_interval (hd, option);
584 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
590 } /* for (ci->children) */
594 if (hd->address == NULL)
596 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
600 if (hd->community == NULL)
602 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
608 } /* while (status == 0) */
617 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
618 hd->name, hd->address, hd->community, hd->version);
620 if (host_head == NULL)
624 host_definition_t *last;
626 while (last->next != NULL)
632 } /* int csnmp_config_add_host */
634 static int csnmp_config (oconfig_item_t *ci)
638 call_snmp_init_once ();
640 for (i = 0; i < ci->children_num; i++)
642 oconfig_item_t *child = ci->children + i;
643 if (strcasecmp ("Data", child->key) == 0)
644 csnmp_config_add_data (child);
645 else if (strcasecmp ("Host", child->key) == 0)
646 csnmp_config_add_host (child);
649 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
651 } /* for (ci->children) */
654 } /* int csnmp_config */
656 /* End of the config stuff. Now the interesting part begins */
658 static void csnmp_host_close_session (host_definition_t *host)
660 if (host->sess_handle == NULL)
663 snmp_sess_close (host->sess_handle);
664 host->sess_handle = NULL;
665 } /* void csnmp_host_close_session */
667 static void csnmp_host_open_session (host_definition_t *host)
669 struct snmp_session sess;
671 if (host->sess_handle != NULL)
672 csnmp_host_close_session (host);
674 snmp_sess_init (&sess);
675 sess.peername = host->address;
676 sess.community = (u_char *) host->community;
677 sess.community_len = strlen (host->community);
678 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
680 /* snmp_sess_open will copy the `struct snmp_session *'. */
681 host->sess_handle = snmp_sess_open (&sess);
683 if (host->sess_handle == NULL)
687 snmp_error (&sess, NULL, NULL, &errstr);
689 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
690 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
693 } /* void csnmp_host_open_session */
695 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
696 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
697 double scale, double shift)
703 if ((vl->type == ASN_INTEGER)
704 || (vl->type == ASN_UINTEGER)
705 || (vl->type == ASN_COUNTER)
707 || (vl->type == ASN_TIMETICKS)
709 || (vl->type == ASN_GAUGE))
711 temp = (uint32_t) *vl->val.integer;
712 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", temp);
714 else if (vl->type == ASN_COUNTER64)
716 temp = (uint32_t) vl->val.counter64->high;
718 temp += (uint32_t) vl->val.counter64->low;
719 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", temp);
721 else if (vl->type == ASN_OCTET_STR)
723 /* We'll handle this later.. */
727 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
731 if (vl->type == ASN_OCTET_STR)
736 if (vl->val.string != NULL)
739 size_t string_length;
741 string_length = sizeof (string) - 1;
742 if (vl->val_len < string_length)
743 string_length = vl->val_len;
745 /* The strings we get from the Net-SNMP library may not be null
746 * terminated. That is why we're using `membpy' here and not `strcpy'.
747 * `string_length' is set to `vl->val_len' which holds the length of the
749 memcpy (string, vl->val.string, string_length);
750 string[string_length] = 0;
752 if (type == DS_TYPE_COUNTER)
754 ret.counter = (counter_t) strtoll (string, &endptr, /* base = */ 0);
755 DEBUG ("snmp plugin: csnmp_value_list_to_value: String to counter: %s -> %llu",
756 string, (unsigned long long) ret.counter);
758 else if (type == DS_TYPE_GAUGE)
760 ret.gauge = (gauge_t) strtod (string, &endptr);
761 DEBUG ("snmp plugin: csnmp_value_list_to_value: String to gauge: %s -> %g",
762 string, (double) ret.gauge);
766 /* Check if an error occurred */
767 if ((vl->val.string == NULL) || (endptr == (char *) vl->val.string))
769 if (type == DS_TYPE_COUNTER)
771 else if (type == DS_TYPE_GAUGE)
775 else if (type == DS_TYPE_COUNTER)
779 else if (type == DS_TYPE_GAUGE)
783 ret.gauge = (scale * temp) + shift;
787 } /* value_t csnmp_value_list_to_value */
789 /* Returns true if all OIDs have left their subtree */
790 static int csnmp_check_res_left_subtree (const host_definition_t *host,
791 const data_definition_t *data,
792 struct snmp_pdu *res)
794 struct variable_list *vb;
796 int num_left_subtree;
804 num_left_subtree = 0;
806 /* check all the variables and count how many have left their subtree */
807 for (vb = res->variables, i = 0;
808 (vb != NULL) && (i < data->values_len);
809 vb = vb->next_variable, i++)
812 if (snmp_oid_ncompare (data->values[i].oid,
813 data->values[i].oid_len,
814 vb->name, vb->name_length,
815 data->values[i].oid_len) != 0)
819 /* check if enough variables have been returned */
820 if (i < data->values_len)
822 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
823 host->name, data->values_len, i);
827 if (data->instance.oid.oid_len > 0)
831 ERROR ("snmp plugin: host %s: Expected one more variable for "
832 "the instance..", host->name);
837 if (snmp_oid_ncompare (data->instance.oid.oid,
838 data->instance.oid.oid_len,
839 vb->name, vb->name_length,
840 data->instance.oid.oid_len) != 0)
844 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
845 "left their subtree",
846 num_left_subtree, num_checked);
847 if (num_left_subtree >= num_checked)
850 } /* int csnmp_check_res_left_subtree */
852 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
853 csnmp_list_instances_t **tail,
854 const struct snmp_pdu *res)
856 csnmp_list_instances_t *il;
857 struct variable_list *vb;
859 /* Set vb on the last variable */
860 for (vb = res->variables;
861 (vb != NULL) && (vb->next_variable != NULL);
862 vb = vb->next_variable)
867 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
870 ERROR ("snmp plugin: malloc failed.");
873 il->subid = vb->name[vb->name_length - 1];
876 /* Get instance name */
877 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
882 memset (il->instance, 0, sizeof (il->instance));
883 instance_len = sizeof (il->instance) - 1;
884 if (instance_len > vb->val_len)
885 instance_len = vb->val_len;
887 sstrncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
889 : vb->val.bitstring),
892 for (ptr = il->instance; *ptr != '\0'; ptr++)
894 if ((*ptr > 0) && (*ptr < 32))
896 else if (*ptr == '/')
899 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
903 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
904 ssnprintf (il->instance, sizeof (il->instance),
905 "%llu", val.counter);
908 /* TODO: Debugging output */
917 } /* int csnmp_instance_list_add */
919 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
920 csnmp_list_instances_t *instance_list,
921 csnmp_table_values_t **value_table)
923 const data_set_t *ds;
924 value_list_t vl = VALUE_LIST_INIT;
926 csnmp_list_instances_t *instance_list_ptr;
927 csnmp_table_values_t **value_table_ptr;
933 ds = plugin_get_ds (data->type);
936 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
939 assert (ds->ds_num == data->values_len);
941 instance_list_ptr = instance_list;
943 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
945 if (value_table_ptr == NULL)
947 for (i = 0; i < data->values_len; i++)
948 value_table_ptr[i] = value_table[i];
950 vl.values_len = ds->ds_num;
951 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
952 if (vl.values == NULL)
954 ERROR ("snmp plugin: malloc failed.");
955 sfree (value_table_ptr);
959 sstrncpy (vl.host, host->name, sizeof (vl.host));
960 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
962 vl.interval = host->interval;
963 vl.time = time (NULL);
968 while (have_more != 0)
970 if (instance_list != NULL)
972 while ((instance_list_ptr != NULL)
973 && (instance_list_ptr->subid < subid))
974 instance_list_ptr = instance_list_ptr->next;
976 if (instance_list_ptr == NULL)
981 else if (instance_list_ptr->subid > subid)
983 subid = instance_list_ptr->subid;
986 } /* if (instance_list != NULL) */
988 for (i = 0; i < data->values_len; i++)
990 while ((value_table_ptr[i] != NULL)
991 && (value_table_ptr[i]->subid < subid))
992 value_table_ptr[i] = value_table_ptr[i]->next;
994 if (value_table_ptr[i] == NULL)
999 else if (value_table_ptr[i]->subid > subid)
1001 subid = value_table_ptr[i]->subid;
1004 } /* for (i = 0; i < columns; i++) */
1005 /* The subid has been increased - start scanning from the beginning
1007 if (i < data->values_len)
1010 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1011 * to the same subid. instance_list_ptr is either NULL or points to the
1012 * same subid, too. */
1014 for (i = 1; i < data->values_len; i++)
1016 assert (value_table_ptr[i] != NULL);
1017 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
1019 assert ((instance_list_ptr == NULL)
1020 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
1023 sstrncpy (vl.type, data->type, sizeof (vl.type));
1026 char temp[DATA_MAX_NAME_LEN];
1028 if (instance_list_ptr == NULL)
1029 ssnprintf (temp, sizeof (temp), "%u", (uint32_t) subid);
1031 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1033 if (data->instance_prefix == NULL)
1034 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1036 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1037 data->instance_prefix, temp);
1040 for (i = 0; i < data->values_len; i++)
1041 vl.values[i] = value_table_ptr[i]->value;
1043 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1044 plugin_dispatch_values (&vl);
1047 } /* while (have_more != 0) */
1050 sfree (value_table_ptr);
1053 } /* int csnmp_dispatch_table */
1055 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1057 struct snmp_pdu *req;
1058 struct snmp_pdu *res;
1059 struct variable_list *vb;
1061 const data_set_t *ds;
1063 uint32_t oid_list_len;
1068 /* `value_table' and `value_table_ptr' implement a linked list for each
1069 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1070 * instance names. This is used to jump gaps in the table. */
1071 csnmp_list_instances_t *instance_list;
1072 csnmp_list_instances_t *instance_list_ptr;
1073 csnmp_table_values_t **value_table;
1074 csnmp_table_values_t **value_table_ptr;
1076 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1077 host->name, data->name);
1079 if (host->sess_handle == NULL)
1081 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1085 ds = plugin_get_ds (data->type);
1088 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1092 if (ds->ds_num != data->values_len)
1094 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1095 data->type, ds->ds_num, data->values_len);
1099 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1100 oid_list_len = data->values_len + 1;
1101 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1102 if (oid_list == NULL)
1104 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1107 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1108 if (data->instance.oid.oid_len > 0)
1109 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1113 /* Allocate the `value_table' */
1114 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1115 * 2 * data->values_len);
1116 if (value_table == NULL)
1118 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1122 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1123 value_table_ptr = value_table + data->values_len;
1125 instance_list = NULL;
1126 instance_list_ptr = NULL;
1131 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1134 ERROR ("snmp plugin: snmp_pdu_create failed.");
1139 for (i = 0; i < oid_list_len; i++)
1140 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1143 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1145 if ((status != STAT_SUCCESS) || (res == NULL))
1147 char *errstr = NULL;
1149 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1150 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1151 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1154 snmp_free_pdu (res);
1158 csnmp_host_close_session (host);
1164 assert (res != NULL);
1166 vb = res->variables;
1173 /* Check if all values (and possibly the instance) have left their
1175 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1181 /* if an instance-OID is configured.. */
1182 if (data->instance.oid.oid_len > 0)
1184 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1185 * add it to the list */
1186 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1189 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1194 /* Set vb on the last variable */
1195 for (vb = res->variables;
1196 (vb != NULL) && (vb->next_variable != NULL);
1197 vb = vb->next_variable)
1199 assert (vb != NULL);
1201 /* Copy OID to oid_list[data->values_len] */
1202 memcpy (oid_list[data->values_len].oid, vb->name,
1203 sizeof (oid) * vb->name_length);
1204 oid_list[data->values_len].oid_len = vb->name_length;
1207 for (vb = res->variables, i = 0;
1208 (vb != NULL) && (i < data->values_len);
1209 vb = vb->next_variable, i++)
1211 csnmp_table_values_t *vt;
1213 /* Check if we left the subtree */
1214 if (snmp_oid_ncompare (data->values[i].oid,
1215 data->values[i].oid_len,
1216 vb->name, vb->name_length,
1217 data->values[i].oid_len) != 0)
1219 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1220 host->name, data->name, i);
1224 if ((value_table_ptr[i] != NULL)
1225 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1227 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1228 "SUBID is not increasing.",
1229 host->name, data->name, i);
1233 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1236 ERROR ("snmp plugin: malloc failed.");
1241 vt->subid = vb->name[vb->name_length - 1];
1242 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1243 data->scale, data->shift);
1246 if (value_table_ptr[i] == NULL)
1247 value_table[i] = vt;
1249 value_table_ptr[i]->next = vt;
1250 value_table_ptr[i] = vt;
1252 /* Copy OID to oid_list[i + 1] */
1253 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1254 oid_list[i].oid_len = vb->name_length;
1255 } /* for (i = data->values_len) */
1258 snmp_free_pdu (res);
1260 } /* while (status == 0) */
1263 snmp_free_pdu (res);
1267 csnmp_dispatch_table (host, data, instance_list, value_table);
1269 /* Free all allocated variables here */
1270 while (instance_list != NULL)
1272 instance_list_ptr = instance_list->next;
1273 sfree (instance_list);
1274 instance_list = instance_list_ptr;
1277 for (i = 0; i < data->values_len; i++)
1279 csnmp_table_values_t *tmp;
1280 while (value_table[i] != NULL)
1282 tmp = value_table[i]->next;
1283 sfree (value_table[i]);
1284 value_table[i] = tmp;
1288 sfree (value_table);
1292 } /* int csnmp_read_table */
1294 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1296 struct snmp_pdu *req;
1297 struct snmp_pdu *res;
1298 struct variable_list *vb;
1300 const data_set_t *ds;
1301 value_list_t vl = VALUE_LIST_INIT;
1306 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1307 host->name, data->name);
1309 if (host->sess_handle == NULL)
1311 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1315 ds = plugin_get_ds (data->type);
1318 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1322 if (ds->ds_num != data->values_len)
1324 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1325 data->type, ds->ds_num, data->values_len);
1329 vl.values_len = ds->ds_num;
1330 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1331 if (vl.values == NULL)
1333 for (i = 0; i < vl.values_len; i++)
1335 if (ds->ds[i].type == DS_TYPE_COUNTER)
1336 vl.values[i].counter = 0;
1338 vl.values[i].gauge = NAN;
1341 sstrncpy (vl.host, host->name, sizeof (vl.host));
1342 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1343 sstrncpy (vl.type, data->type, sizeof (vl.type));
1344 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1346 vl.interval = host->interval;
1348 req = snmp_pdu_create (SNMP_MSG_GET);
1351 ERROR ("snmp plugin: snmp_pdu_create failed.");
1356 for (i = 0; i < data->values_len; i++)
1357 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1360 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1362 if ((status != STAT_SUCCESS) || (res == NULL))
1364 char *errstr = NULL;
1366 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1367 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1368 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1371 snmp_free_pdu (res);
1375 csnmp_host_close_session (host);
1380 vl.time = time (NULL);
1382 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1386 snprint_variable (buffer, sizeof (buffer),
1387 vb->name, vb->name_length, vb);
1388 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1389 #endif /* COLLECT_DEBUG */
1391 for (i = 0; i < data->values_len; i++)
1392 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1393 vb->name, vb->name_length) == 0)
1394 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1395 data->scale, data->shift);
1396 } /* for (res->variables) */
1399 snmp_free_pdu (res);
1402 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1403 plugin_dispatch_values (&vl);
1407 } /* int csnmp_read_value */
1409 static int csnmp_read_host (host_definition_t *host)
1415 time_start = time (NULL);
1416 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1417 (unsigned int) time_start);
1419 if (host->sess_handle == NULL)
1420 csnmp_host_open_session (host);
1422 if (host->sess_handle == NULL)
1425 for (i = 0; i < host->data_list_len; i++)
1427 data_definition_t *data = host->data_list[i];
1430 csnmp_read_table (host, data);
1432 csnmp_read_value (host, data);
1435 time_end = time (NULL);
1436 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1437 (unsigned int) time_end);
1438 if ((time_end - time_start) > host->interval)
1440 WARNING ("snmp plugin: Host `%s' should be queried every %i seconds, "
1441 "but reading all values takes %u seconds.",
1442 host->name, host->interval, (unsigned int) (time_end - time_start));
1446 } /* int csnmp_read_host */
1448 static void *csnmp_read_thread (void *data)
1450 host_definition_t *host;
1452 pthread_mutex_lock (&host_lock);
1453 while (do_shutdown == 0)
1455 pthread_cond_wait (&host_cond, &host_lock);
1457 for (host = host_head; host != NULL; host = host->next)
1459 if (do_shutdown != 0)
1461 if (host->state != STATE_WAIT)
1464 host->state = STATE_BUSY;
1465 pthread_mutex_unlock (&host_lock);
1466 csnmp_read_host (host);
1467 pthread_mutex_lock (&host_lock);
1468 host->state = STATE_IDLE;
1470 } /* while (do_shutdown == 0) */
1471 pthread_mutex_unlock (&host_lock);
1473 pthread_exit ((void *) 0);
1474 return ((void *) 0);
1475 } /* void *csnmp_read_thread */
1477 static int csnmp_init (void)
1479 host_definition_t *host;
1482 if (host_head == NULL)
1484 NOTICE ("snmp plugin: No host has been defined.");
1488 call_snmp_init_once ();
1491 for (host = host_head; host != NULL; host = host->next)
1494 /* We need to initialize `interval' here, because `interval_g' isn't
1495 * initialized during `configure'. */
1496 host->next_update = time (NULL);
1497 if (host->interval == 0)
1499 host->interval = interval_g;
1501 else if (host->interval < interval_g)
1503 host->interval = interval_g;
1504 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1505 host->name, host->interval);
1508 csnmp_host_open_session (host);
1511 /* Now start the reading threads */
1512 if (threads_num > 3)
1514 threads_num = 3 + ((threads_num - 3) / 10);
1515 if (threads_num > 10)
1519 threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1520 if (threads == NULL)
1522 ERROR ("snmp plugin: malloc failed.");
1525 memset (threads, '\0', threads_num * sizeof (pthread_t));
1527 for (i = 0; i < threads_num; i++)
1528 pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1531 } /* int csnmp_init */
1533 static int csnmp_read (void)
1535 host_definition_t *host;
1538 if (host_head == NULL)
1540 INFO ("snmp plugin: No hosts configured.");
1546 pthread_mutex_lock (&host_lock);
1547 for (host = host_head; host != NULL; host = host->next)
1549 if (host->state != STATE_IDLE)
1552 /* Skip this host if the next or a later iteration will be sufficient. */
1553 if (host->next_update >= (now + interval_g))
1556 host->state = STATE_WAIT;
1557 host->next_update = now + host->interval;
1560 pthread_cond_broadcast (&host_cond);
1561 pthread_mutex_unlock (&host_lock);
1564 } /* int csnmp_read */
1566 static int csnmp_shutdown (void)
1568 host_definition_t *host_this;
1569 host_definition_t *host_next;
1571 data_definition_t *data_this;
1572 data_definition_t *data_next;
1576 pthread_mutex_lock (&host_lock);
1578 pthread_cond_broadcast (&host_cond);
1579 pthread_mutex_unlock (&host_lock);
1581 for (i = 0; i < threads_num; i++)
1582 pthread_join (threads[i], NULL);
1584 /* Now that all the threads have exited, let's free all the global variables.
1585 * This isn't really neccessary, I guess, but I think it's good stile to do
1587 host_this = host_head;
1589 while (host_this != NULL)
1591 host_next = host_this->next;
1593 csnmp_host_close_session (host_this);
1595 sfree (host_this->name);
1596 sfree (host_this->address);
1597 sfree (host_this->community);
1598 sfree (host_this->data_list);
1601 host_this = host_next;
1604 data_this = data_head;
1606 while (data_this != NULL)
1608 data_next = data_this->next;
1610 sfree (data_this->name);
1611 sfree (data_this->type);
1612 sfree (data_this->values);
1615 data_this = data_next;
1619 } /* int csnmp_shutdown */
1621 void module_register (void)
1623 plugin_register_complex_config ("snmp", csnmp_config);
1624 plugin_register_init ("snmp", csnmp_init);
1625 plugin_register_read ("snmp", csnmp_read);
1626 plugin_register_shutdown ("snmp", csnmp_shutdown);
1627 } /* void module_register */
1630 * vim: shiftwidth=2 softtabstop=2 tabstop=8