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 strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
205 } /* int csnmp_config_add_data_instance */
207 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
210 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
212 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
218 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
219 "is set to `false'.", dd->name);
223 sfree (dd->instance_prefix);
224 dd->instance_prefix = strdup (ci->values[0].value.string);
225 if (dd->instance_prefix == NULL)
229 } /* int csnmp_config_add_data_instance_prefix */
231 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
235 if (ci->values_num < 1)
237 WARNING ("snmp plugin: `Values' needs at least one argument.");
241 for (i = 0; i < ci->values_num; i++)
242 if (ci->values[i].type != OCONFIG_TYPE_STRING)
244 WARNING ("snmp plugin: `Values' needs only string argument.");
250 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
251 if (dd->values == NULL)
253 dd->values_len = ci->values_num;
255 for (i = 0; i < ci->values_num; i++)
257 dd->values[i].oid_len = MAX_OID_LEN;
259 if (NULL == snmp_parse_oid (ci->values[i].value.string,
260 dd->values[i].oid, &dd->values[i].oid_len))
262 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
263 ci->values[i].value.string);
272 } /* int csnmp_config_add_data_instance */
274 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
276 if ((ci->values_num != 1)
277 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
279 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
283 dd->shift = ci->values[0].value.number;
286 } /* int csnmp_config_add_data_shift */
288 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
290 if ((ci->values_num != 1)
291 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
293 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
297 dd->scale = ci->values[0].value.number;
300 } /* int csnmp_config_add_data_scale */
302 static int csnmp_config_add_data (oconfig_item_t *ci)
304 data_definition_t *dd;
308 if ((ci->values_num != 1)
309 || (ci->values[0].type != OCONFIG_TYPE_STRING))
311 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
315 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
318 memset (dd, '\0', sizeof (data_definition_t));
320 dd->name = strdup (ci->values[0].value.string);
321 if (dd->name == NULL)
329 for (i = 0; i < ci->children_num; i++)
331 oconfig_item_t *option = ci->children + i;
334 if (strcasecmp ("Type", option->key) == 0)
335 status = csnmp_config_add_data_type (dd, option);
336 else if (strcasecmp ("Table", option->key) == 0)
337 status = csnmp_config_add_data_table (dd, option);
338 else if (strcasecmp ("Instance", option->key) == 0)
339 status = csnmp_config_add_data_instance (dd, option);
340 else if (strcasecmp ("InstancePrefix", option->key) == 0)
341 status = csnmp_config_add_data_instance_prefix (dd, option);
342 else if (strcasecmp ("Values", option->key) == 0)
343 status = csnmp_config_add_data_values (dd, option);
344 else if (strcasecmp ("Shift", option->key) == 0)
345 status = csnmp_config_add_data_shift (dd, option);
346 else if (strcasecmp ("Scale", option->key) == 0)
347 status = csnmp_config_add_data_scale (dd, option);
350 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
356 } /* for (ci->children) */
360 if (dd->type == NULL)
362 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
366 if (dd->values == NULL)
368 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
374 } /* while (status == 0) */
379 sfree (dd->instance_prefix);
385 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
386 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
388 if (data_head == NULL)
392 data_definition_t *last;
394 while (last->next != NULL)
400 } /* int csnmp_config_add_data */
402 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
404 if ((ci->values_num != 1)
405 || (ci->values[0].type != OCONFIG_TYPE_STRING))
407 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
411 if (hd->address == NULL)
414 hd->address = strdup (ci->values[0].value.string);
415 if (hd->address == NULL)
418 DEBUG ("snmp plugin: host = %s; host->address = %s;",
419 hd->name, hd->address);
422 } /* int csnmp_config_add_host_address */
424 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
426 if ((ci->values_num != 1)
427 || (ci->values[0].type != OCONFIG_TYPE_STRING))
429 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
433 if (hd->community == NULL)
434 free (hd->community);
436 hd->community = strdup (ci->values[0].value.string);
437 if (hd->community == NULL)
440 DEBUG ("snmp plugin: host = %s; host->community = %s;",
441 hd->name, hd->community);
444 } /* int csnmp_config_add_host_community */
446 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
450 if ((ci->values_num != 1)
451 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
453 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
457 version = (int) ci->values[0].value.number;
458 if ((version != 1) && (version != 2))
460 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
464 hd->version = version;
467 } /* int csnmp_config_add_host_address */
469 static int csnmp_config_add_host_collect (host_definition_t *host,
472 data_definition_t *data;
473 data_definition_t **data_list;
477 if (ci->values_num < 1)
479 WARNING ("snmp plugin: `Collect' needs at least one argument.");
483 for (i = 0; i < ci->values_num; i++)
484 if (ci->values[i].type != OCONFIG_TYPE_STRING)
486 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
490 data_list_len = host->data_list_len + ci->values_num;
491 data_list = (data_definition_t **) realloc (host->data_list,
492 sizeof (data_definition_t *) * data_list_len);
493 if (data_list == NULL)
495 host->data_list = data_list;
497 for (i = 0; i < ci->values_num; i++)
499 for (data = data_head; data != NULL; data = data->next)
500 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
505 WARNING ("snmp plugin: No such data configured: `%s'",
506 ci->values[i].value.string);
510 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
511 host->name, host->data_list_len, data->name);
513 host->data_list[host->data_list_len] = data;
514 host->data_list_len++;
515 } /* for (values_num) */
518 } /* int csnmp_config_add_host_collect */
520 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
522 if ((ci->values_num != 1)
523 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
525 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
529 hd->interval = (int) ci->values[0].value.number;
530 if (hd->interval < 0)
534 } /* int csnmp_config_add_host_interval */
536 static int csnmp_config_add_host (oconfig_item_t *ci)
538 host_definition_t *hd;
542 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
544 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
548 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
551 memset (hd, '\0', sizeof (host_definition_t));
554 hd->name = strdup (ci->values[0].value.string);
555 if (hd->name == NULL)
561 hd->sess_handle = NULL;
564 hd->state = STATE_IDLE;
566 for (i = 0; i < ci->children_num; i++)
568 oconfig_item_t *option = ci->children + i;
571 if (strcasecmp ("Address", option->key) == 0)
572 status = csnmp_config_add_host_address (hd, option);
573 else if (strcasecmp ("Community", option->key) == 0)
574 status = csnmp_config_add_host_community (hd, option);
575 else if (strcasecmp ("Version", option->key) == 0)
576 status = csnmp_config_add_host_version (hd, option);
577 else if (strcasecmp ("Collect", option->key) == 0)
578 csnmp_config_add_host_collect (hd, option);
579 else if (strcasecmp ("Interval", option->key) == 0)
580 csnmp_config_add_host_interval (hd, option);
583 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
589 } /* for (ci->children) */
593 if (hd->address == NULL)
595 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
599 if (hd->community == NULL)
601 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
607 } /* while (status == 0) */
616 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
617 hd->name, hd->address, hd->community, hd->version);
619 if (host_head == NULL)
623 host_definition_t *last;
625 while (last->next != NULL)
631 } /* int csnmp_config_add_host */
633 static int csnmp_config (oconfig_item_t *ci)
637 call_snmp_init_once ();
639 for (i = 0; i < ci->children_num; i++)
641 oconfig_item_t *child = ci->children + i;
642 if (strcasecmp ("Data", child->key) == 0)
643 csnmp_config_add_data (child);
644 else if (strcasecmp ("Host", child->key) == 0)
645 csnmp_config_add_host (child);
648 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
650 } /* for (ci->children) */
653 } /* int csnmp_config */
655 /* End of the config stuff. Now the interesting part begins */
657 static void csnmp_host_close_session (host_definition_t *host)
659 if (host->sess_handle == NULL)
662 snmp_sess_close (host->sess_handle);
663 host->sess_handle = NULL;
664 } /* void csnmp_host_close_session */
666 static void csnmp_host_open_session (host_definition_t *host)
668 struct snmp_session sess;
670 if (host->sess_handle != NULL)
671 csnmp_host_close_session (host);
673 snmp_sess_init (&sess);
674 sess.peername = host->address;
675 sess.community = (u_char *) host->community;
676 sess.community_len = strlen (host->community);
677 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
679 /* snmp_sess_open will copy the `struct snmp_session *'. */
680 host->sess_handle = snmp_sess_open (&sess);
682 if (host->sess_handle == NULL)
686 snmp_error (&sess, NULL, NULL, &errstr);
688 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
689 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
692 } /* void csnmp_host_open_session */
694 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
695 double scale, double shift)
701 if ((vl->type == ASN_INTEGER)
702 || (vl->type == ASN_UINTEGER)
703 || (vl->type == ASN_COUNTER)
705 || (vl->type == ASN_TIMETICKS)
707 || (vl->type == ASN_GAUGE))
709 temp = (uint32_t) *vl->val.integer;
710 DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
712 else if (vl->type == ASN_COUNTER64)
714 temp = (uint32_t) vl->val.counter64->high;
716 temp += (uint32_t) vl->val.counter64->low;
717 DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
721 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
725 if (type == DS_TYPE_COUNTER)
729 else if (type == DS_TYPE_GAUGE)
733 ret.gauge = (scale * temp) + shift;
737 } /* value_t csnmp_value_list_to_value */
739 /* Returns true if all OIDs have left their subtree */
740 static int csnmp_check_res_left_subtree (const host_definition_t *host,
741 const data_definition_t *data,
742 struct snmp_pdu *res)
744 struct variable_list *vb;
746 int num_left_subtree;
754 num_left_subtree = 0;
756 /* check all the variables and count how many have left their subtree */
757 for (vb = res->variables, i = 0;
758 (vb != NULL) && (i < data->values_len);
759 vb = vb->next_variable, i++)
762 if (snmp_oid_ncompare (data->values[i].oid,
763 data->values[i].oid_len,
764 vb->name, vb->name_length,
765 data->values[i].oid_len) != 0)
769 /* check if enough variables have been returned */
770 if (i < data->values_len)
772 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
773 host->name, data->values_len, i);
777 if (data->instance.oid.oid_len > 0)
781 ERROR ("snmp plugin: host %s: Expected one more variable for "
787 if (snmp_oid_ncompare (data->instance.oid.oid,
788 data->instance.oid.oid_len,
789 vb->name, vb->name_length,
790 data->instance.oid.oid_len) != 0)
794 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
795 "left their subtree",
796 num_left_subtree, num_checked);
797 if (num_left_subtree >= num_checked)
800 } /* int csnmp_check_res_left_subtree */
802 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
803 csnmp_list_instances_t **tail,
804 const struct snmp_pdu *res)
806 csnmp_list_instances_t *il;
807 struct variable_list *vb;
809 /* Set vb on the last variable */
810 for (vb = res->variables;
811 (vb != NULL) && (vb->next_variable != NULL);
812 vb = vb->next_variable)
817 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
820 ERROR ("snmp plugin: malloc failed.");
823 il->subid = vb->name[vb->name_length - 1];
826 /* Get instance name */
827 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
832 instance_len = sizeof (il->instance) - 1;
833 if (instance_len > vb->val_len)
834 instance_len = vb->val_len;
836 strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
838 : vb->val.bitstring),
840 il->instance[instance_len] = '\0';
842 for (ptr = il->instance; *ptr != '\0'; ptr++)
844 if ((*ptr > 0) && (*ptr < 32))
846 else if (*ptr == '/')
849 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
853 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
854 snprintf (il->instance, sizeof (il->instance),
855 "%llu", val.counter);
857 il->instance[sizeof (il->instance) - 1] = '\0';
859 /* TODO: Debugging output */
868 } /* int csnmp_instance_list_add */
870 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
871 csnmp_list_instances_t *instance_list,
872 csnmp_table_values_t **value_table)
874 const data_set_t *ds;
875 value_list_t vl = VALUE_LIST_INIT;
877 csnmp_list_instances_t *instance_list_ptr;
878 csnmp_table_values_t **value_table_ptr;
884 ds = plugin_get_ds (data->type);
887 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
890 assert (ds->ds_num == data->values_len);
892 instance_list_ptr = instance_list;
894 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
896 if (value_table_ptr == NULL)
898 for (i = 0; i < data->values_len; i++)
899 value_table_ptr[i] = value_table[i];
901 vl.values_len = ds->ds_num;
902 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
903 if (vl.values == NULL)
905 ERROR ("snmp plugin: malloc failed.");
906 sfree (value_table_ptr);
910 strncpy (vl.host, host->name, sizeof (vl.host));
911 vl.host[sizeof (vl.host) - 1] = '\0';
912 strcpy (vl.plugin, "snmp");
914 vl.interval = host->interval;
915 vl.time = time (NULL);
920 while (have_more != 0)
922 if (instance_list != NULL)
924 while ((instance_list_ptr != NULL)
925 && (instance_list_ptr->subid < subid))
926 instance_list_ptr = instance_list_ptr->next;
928 if (instance_list_ptr == NULL)
933 else if (instance_list_ptr->subid > subid)
935 subid = instance_list_ptr->subid;
938 } /* if (instance_list != NULL) */
940 for (i = 0; i < data->values_len; i++)
942 while ((value_table_ptr[i] != NULL)
943 && (value_table_ptr[i]->subid < subid))
944 value_table_ptr[i] = value_table_ptr[i]->next;
946 if (value_table_ptr[i] == NULL)
951 else if (value_table_ptr[i]->subid > subid)
953 subid = value_table_ptr[i]->subid;
956 } /* for (i = 0; i < columns; i++) */
957 /* The subid has been increased - start scanning from the beginning
959 if (i < data->values_len)
962 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
963 * to the same subid. instance_list_ptr is either NULL or points to the
964 * same subid, too. */
966 for (i = 1; i < data->values_len; i++)
968 assert (value_table_ptr[i] != NULL);
969 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
971 assert ((instance_list_ptr == NULL)
972 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
976 char temp[DATA_MAX_NAME_LEN];
978 if (instance_list_ptr == NULL)
979 snprintf (temp, sizeof (temp), "%u",
982 strncpy (temp, instance_list_ptr->instance,
984 temp[sizeof (temp) - 1] = '\0';
986 if (data->instance_prefix == NULL)
987 strncpy (vl.type_instance, temp, sizeof (vl.type_instance));
989 snprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
990 data->instance_prefix, temp);
991 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
994 for (i = 0; i < data->values_len; i++)
995 vl.values[i] = value_table_ptr[i]->value;
997 /* If we get here `vl.type_instance' and all `vl.values' have been set */
998 plugin_dispatch_values (data->type, &vl);
1001 } /* while (have_more != 0) */
1004 sfree (value_table_ptr);
1007 } /* int csnmp_dispatch_table */
1009 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1011 struct snmp_pdu *req;
1012 struct snmp_pdu *res;
1013 struct variable_list *vb;
1015 const data_set_t *ds;
1017 uint32_t oid_list_len;
1022 /* `value_table' and `value_table_ptr' implement a linked list for each
1023 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1024 * instance names. This is used to jump gaps in the table. */
1025 csnmp_list_instances_t *instance_list;
1026 csnmp_list_instances_t *instance_list_ptr;
1027 csnmp_table_values_t **value_table;
1028 csnmp_table_values_t **value_table_ptr;
1030 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1031 host->name, data->name);
1033 if (host->sess_handle == NULL)
1035 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1039 ds = plugin_get_ds (data->type);
1042 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1046 if (ds->ds_num != data->values_len)
1048 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1049 data->type, ds->ds_num, data->values_len);
1053 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1054 oid_list_len = data->values_len + 1;
1055 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1056 if (oid_list == NULL)
1058 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1061 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1062 if (data->instance.oid.oid_len > 0)
1063 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1067 /* Allocate the `value_table' */
1068 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1069 * 2 * data->values_len);
1070 if (value_table == NULL)
1072 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1076 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1077 value_table_ptr = value_table + data->values_len;
1079 instance_list = NULL;
1080 instance_list_ptr = NULL;
1085 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1088 ERROR ("snmp plugin: snmp_pdu_create failed.");
1093 for (i = 0; i < oid_list_len; i++)
1094 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1097 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1099 if ((status != STAT_SUCCESS) || (res == NULL))
1101 char *errstr = NULL;
1103 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1104 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1105 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1108 snmp_free_pdu (res);
1112 csnmp_host_close_session (host);
1118 assert (res != NULL);
1120 vb = res->variables;
1124 snmp_free_pdu (res);
1131 /* Check if all values (and possibly the instance) have left their
1133 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1136 snmp_free_pdu (res);
1142 /* if an instance-OID is configured.. */
1143 if (data->instance.oid.oid_len > 0)
1145 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1146 * add it to the list */
1147 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1150 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1155 /* Set vb on the last variable */
1156 for (vb = res->variables;
1157 (vb != NULL) && (vb->next_variable != NULL);
1158 vb = vb->next_variable)
1166 /* Copy OID to oid_list[data->values_len] */
1167 memcpy (oid_list[data->values_len].oid, vb->name,
1168 sizeof (oid) * vb->name_length);
1169 oid_list[data->values_len].oid_len = vb->name_length;
1172 for (vb = res->variables, i = 0;
1173 (vb != NULL) && (i < data->values_len);
1174 vb = vb->next_variable, i++)
1176 csnmp_table_values_t *vt;
1178 /* Check if we left the subtree */
1179 if (snmp_oid_ncompare (data->values[i].oid,
1180 data->values[i].oid_len,
1181 vb->name, vb->name_length,
1182 data->values[i].oid_len) != 0)
1184 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1185 host->name, data->name, i);
1189 if ((value_table_ptr[i] != NULL)
1190 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1192 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1193 "SUBID is not increasing.",
1194 host->name, data->name, i);
1198 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1201 ERROR ("snmp plugin: malloc failed.");
1206 vt->subid = vb->name[vb->name_length - 1];
1207 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1208 data->scale, data->shift);
1211 if (value_table_ptr[i] == NULL)
1212 value_table[i] = vt;
1214 value_table_ptr[i]->next = vt;
1215 value_table_ptr[i] = vt;
1217 /* Copy OID to oid_list[i + 1] */
1218 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1219 oid_list[i].oid_len = vb->name_length;
1220 } /* for (i = data->values_len) */
1223 snmp_free_pdu (res);
1225 } /* while (status == 0) */
1228 csnmp_dispatch_table (host, data, instance_list, value_table);
1230 /* Free all allocated variables here */
1231 while (instance_list != NULL)
1233 instance_list_ptr = instance_list->next;
1234 sfree (instance_list);
1235 instance_list = instance_list_ptr;
1238 for (i = 0; i < data->values_len; i++)
1240 csnmp_table_values_t *tmp;
1241 while (value_table[i] != NULL)
1243 tmp = value_table[i]->next;
1244 sfree (value_table[i]);
1245 value_table[i] = tmp;
1249 sfree (value_table);
1253 } /* int csnmp_read_table */
1255 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1257 struct snmp_pdu *req;
1258 struct snmp_pdu *res;
1259 struct variable_list *vb;
1261 const data_set_t *ds;
1262 value_list_t vl = VALUE_LIST_INIT;
1267 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1268 host->name, data->name);
1270 if (host->sess_handle == NULL)
1272 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1276 ds = plugin_get_ds (data->type);
1279 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1283 if (ds->ds_num != data->values_len)
1285 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1286 data->type, ds->ds_num, data->values_len);
1290 vl.values_len = ds->ds_num;
1291 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1292 if (vl.values == NULL)
1294 for (i = 0; i < vl.values_len; i++)
1296 if (ds->ds[i].type == DS_TYPE_COUNTER)
1297 vl.values[i].counter = 0;
1299 vl.values[i].gauge = NAN;
1302 strncpy (vl.host, host->name, sizeof (vl.host));
1303 vl.host[sizeof (vl.host) - 1] = '\0';
1304 strcpy (vl.plugin, "snmp");
1305 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1306 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1308 vl.interval = host->interval;
1310 req = snmp_pdu_create (SNMP_MSG_GET);
1313 ERROR ("snmp plugin: snmp_pdu_create failed.");
1318 for (i = 0; i < data->values_len; i++)
1319 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1322 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1324 if ((status != STAT_SUCCESS) || (res == NULL))
1326 char *errstr = NULL;
1328 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1329 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1330 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1333 snmp_free_pdu (res);
1337 csnmp_host_close_session (host);
1342 vl.time = time (NULL);
1344 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1348 snprint_variable (buffer, sizeof (buffer),
1349 vb->name, vb->name_length, vb);
1350 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1351 #endif /* COLLECT_DEBUG */
1353 for (i = 0; i < data->values_len; i++)
1354 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1355 vb->name, vb->name_length) == 0)
1356 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1357 data->scale, data->shift);
1358 } /* for (res->variables) */
1361 snmp_free_pdu (res);
1364 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1365 plugin_dispatch_values (data->type, &vl);
1369 } /* int csnmp_read_value */
1371 static int csnmp_read_host (host_definition_t *host)
1377 time_start = time (NULL);
1378 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1379 (unsigned int) time_start);
1381 if (host->sess_handle == NULL)
1382 csnmp_host_open_session (host);
1384 if (host->sess_handle == NULL)
1387 for (i = 0; i < host->data_list_len; i++)
1389 data_definition_t *data = host->data_list[i];
1392 csnmp_read_table (host, data);
1394 csnmp_read_value (host, data);
1397 time_end = time (NULL);
1398 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1399 (unsigned int) time_end);
1400 if ((time_end - time_start) > host->interval)
1402 WARNING ("snmp plugin: Host `%s' should be queried every %i seconds, "
1403 "but reading all values takes %i seconds.",
1404 host->name, host->interval, time_end - time_start);
1408 } /* int csnmp_read_host */
1410 static void *csnmp_read_thread (void *data)
1412 host_definition_t *host;
1414 pthread_mutex_lock (&host_lock);
1415 while (do_shutdown == 0)
1417 pthread_cond_wait (&host_cond, &host_lock);
1419 for (host = host_head; host != NULL; host = host->next)
1421 if (do_shutdown != 0)
1423 if (host->state != STATE_WAIT)
1426 host->state = STATE_BUSY;
1427 pthread_mutex_unlock (&host_lock);
1428 csnmp_read_host (host);
1429 pthread_mutex_lock (&host_lock);
1430 host->state = STATE_IDLE;
1432 } /* while (do_shutdown == 0) */
1433 pthread_mutex_unlock (&host_lock);
1435 pthread_exit ((void *) 0);
1436 return ((void *) 0);
1437 } /* void *csnmp_read_thread */
1439 static int csnmp_init (void)
1441 host_definition_t *host;
1444 if (host_head == NULL)
1446 NOTICE ("snmp plugin: No host has been defined.");
1450 call_snmp_init_once ();
1453 for (host = host_head; host != NULL; host = host->next)
1456 /* We need to initialize `interval' here, because `interval_g' isn't
1457 * initialized during `configure'. */
1458 host->next_update = time (NULL);
1459 if (host->interval == 0)
1461 host->interval = interval_g;
1463 else if (host->interval < interval_g)
1465 host->interval = interval_g;
1466 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1467 host->name, host->interval);
1470 csnmp_host_open_session (host);
1473 /* Now start the reading threads */
1474 if (threads_num > 3)
1476 threads_num = 3 + ((threads_num - 3) / 10);
1477 if (threads_num > 10)
1481 threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1482 if (threads == NULL)
1484 ERROR ("snmp plugin: malloc failed.");
1487 memset (threads, '\0', threads_num * sizeof (pthread_t));
1489 for (i = 0; i < threads_num; i++)
1490 pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1493 } /* int csnmp_init */
1495 static int csnmp_read (void)
1497 host_definition_t *host;
1500 if (host_head == NULL)
1502 INFO ("snmp plugin: No hosts configured.");
1508 pthread_mutex_lock (&host_lock);
1509 for (host = host_head; host != NULL; host = host->next)
1511 if (host->state != STATE_IDLE)
1514 /* Skip this host if the next or a later iteration will be sufficient. */
1515 if (host->next_update >= (now + interval_g))
1518 host->state = STATE_WAIT;
1519 host->next_update = now + host->interval;
1522 pthread_cond_broadcast (&host_cond);
1523 pthread_mutex_unlock (&host_lock);
1526 } /* int csnmp_read */
1528 static int csnmp_shutdown (void)
1530 host_definition_t *host_this;
1531 host_definition_t *host_next;
1533 data_definition_t *data_this;
1534 data_definition_t *data_next;
1538 pthread_mutex_lock (&host_lock);
1540 pthread_cond_broadcast (&host_cond);
1541 pthread_mutex_unlock (&host_lock);
1543 for (i = 0; i < threads_num; i++)
1544 pthread_join (threads[i], NULL);
1546 /* Now that all the threads have exited, let's free all the global variables.
1547 * This isn't really neccessary, I guess, but I think it's good stile to do
1549 host_this = host_head;
1551 while (host_this != NULL)
1553 host_next = host_this->next;
1555 csnmp_host_close_session (host_this);
1557 sfree (host_this->name);
1558 sfree (host_this->address);
1559 sfree (host_this->community);
1560 sfree (host_this->data_list);
1563 host_this = host_next;
1566 data_this = data_head;
1568 while (data_this != NULL)
1570 data_next = data_this->next;
1572 sfree (data_this->name);
1573 sfree (data_this->type);
1574 sfree (data_this->values);
1577 data_this = data_next;
1581 } /* int csnmp_shutdown */
1583 void module_register (void)
1585 plugin_register_complex_config ("snmp", csnmp_config);
1586 plugin_register_init ("snmp", csnmp_init);
1587 plugin_register_read ("snmp", csnmp_read);
1588 plugin_register_shutdown ("snmp", csnmp_shutdown);
1589 } /* void module_register */
1592 * vim: shiftwidth=2 softtabstop=2 tabstop=8