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 */
58 struct data_definition_s *next;
60 typedef struct data_definition_s data_definition_t;
62 struct host_definition_s
71 data_definition_t **data_list;
73 enum /****************************************************/
75 STATE_IDLE, /* - just sits there until `skip_left < interval_g' */
76 STATE_WAIT, /* - waits to be queried. */
77 STATE_BUSY /* - is currently being queried. */
78 } state; /****************************************************/
79 struct host_definition_s *next;
81 typedef struct host_definition_s host_definition_t;
83 /* These two types are used to cache values in `csnmp_read_table' to handle
85 struct csnmp_list_instances_s
88 char instance[DATA_MAX_NAME_LEN];
89 struct csnmp_list_instances_s *next;
91 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
93 struct csnmp_table_values_s
97 struct csnmp_table_values_s *next;
99 typedef struct csnmp_table_values_s csnmp_table_values_t;
104 static int do_shutdown = 0;
106 pthread_t *threads = NULL;
109 static data_definition_t *data_head = NULL;
110 static host_definition_t *host_head = NULL;
112 static pthread_mutex_t host_lock = PTHREAD_MUTEX_INITIALIZER;
113 static pthread_cond_t host_cond = PTHREAD_COND_INITIALIZER;
118 /* First there are many functions which do configuration stuff. It's a big
119 * bloated and messy, I'm afraid. */
122 * Callgraph for the config stuff:
124 * +-> call_snmp_init_once
125 * +-> csnmp_config_add_data
126 * ! +-> csnmp_config_add_data_type
127 * ! +-> csnmp_config_add_data_table
128 * ! +-> csnmp_config_add_data_instance
129 * ! +-> csnmp_config_add_data_values
130 * +-> csnmp_config_add_host
131 * +-> csnmp_config_add_host_address
132 * +-> csnmp_config_add_host_community
133 * +-> csnmp_config_add_host_version
134 * +-> csnmp_config_add_host_collect
135 * +-> csnmp_config_add_host_interval
137 static void call_snmp_init_once (void)
139 static int have_init = 0;
142 init_snmp (PACKAGE_NAME);
144 } /* void call_snmp_init_once */
146 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
148 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
150 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
154 if (dd->type != NULL)
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_values (data_definition_t *dd, oconfig_item_t *ci)
211 if (ci->values_num < 1)
213 WARNING ("snmp plugin: `Values' needs at least one argument.");
217 for (i = 0; i < ci->values_num; i++)
218 if (ci->values[i].type != OCONFIG_TYPE_STRING)
220 WARNING ("snmp plugin: `Values' needs only string argument.");
224 if (dd->values != NULL)
226 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
227 if (dd->values == NULL)
229 dd->values_len = ci->values_num;
231 for (i = 0; i < ci->values_num; i++)
233 dd->values[i].oid_len = MAX_OID_LEN;
235 if (NULL == snmp_parse_oid (ci->values[i].value.string,
236 dd->values[i].oid, &dd->values[i].oid_len))
238 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
239 ci->values[i].value.string);
248 } /* int csnmp_config_add_data_instance */
250 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
252 if ((ci->values_num != 1)
253 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
255 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
259 dd->shift = ci->values[0].value.number;
262 } /* int csnmp_config_add_data_shift */
264 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
266 if ((ci->values_num != 1)
267 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
269 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
273 dd->scale = ci->values[0].value.number;
276 } /* int csnmp_config_add_data_scale */
278 static int csnmp_config_add_data (oconfig_item_t *ci)
280 data_definition_t *dd;
284 if ((ci->values_num != 1)
285 || (ci->values[0].type != OCONFIG_TYPE_STRING))
287 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
291 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
294 memset (dd, '\0', sizeof (data_definition_t));
296 dd->name = strdup (ci->values[0].value.string);
297 if (dd->name == NULL)
305 for (i = 0; i < ci->children_num; i++)
307 oconfig_item_t *option = ci->children + i;
310 if (strcasecmp ("Type", option->key) == 0)
311 status = csnmp_config_add_data_type (dd, option);
312 else if (strcasecmp ("Table", option->key) == 0)
313 status = csnmp_config_add_data_table (dd, option);
314 else if (strcasecmp ("Instance", option->key) == 0)
315 status = csnmp_config_add_data_instance (dd, option);
316 else if (strcasecmp ("Values", option->key) == 0)
317 status = csnmp_config_add_data_values (dd, option);
318 else if (strcasecmp ("Shift", option->key) == 0)
319 status = csnmp_config_add_data_shift (dd, option);
320 else if (strcasecmp ("Scale", option->key) == 0)
321 status = csnmp_config_add_data_scale (dd, option);
324 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
330 } /* for (ci->children) */
334 if (dd->type == NULL)
336 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
340 if (dd->values == NULL)
342 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
348 } /* while (status == 0) */
358 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
359 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
361 if (data_head == NULL)
365 data_definition_t *last;
367 while (last->next != NULL)
373 } /* int csnmp_config_add_data */
375 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
377 if ((ci->values_num != 1)
378 || (ci->values[0].type != OCONFIG_TYPE_STRING))
380 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
384 if (hd->address == NULL)
387 hd->address = strdup (ci->values[0].value.string);
388 if (hd->address == NULL)
391 DEBUG ("snmp plugin: host = %s; host->address = %s;",
392 hd->name, hd->address);
395 } /* int csnmp_config_add_host_address */
397 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
399 if ((ci->values_num != 1)
400 || (ci->values[0].type != OCONFIG_TYPE_STRING))
402 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
406 if (hd->community == NULL)
407 free (hd->community);
409 hd->community = strdup (ci->values[0].value.string);
410 if (hd->community == NULL)
413 DEBUG ("snmp plugin: host = %s; host->community = %s;",
414 hd->name, hd->community);
417 } /* int csnmp_config_add_host_community */
419 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
423 if ((ci->values_num != 1)
424 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
426 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
430 version = (int) ci->values[0].value.number;
431 if ((version != 1) && (version != 2))
433 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
437 hd->version = version;
440 } /* int csnmp_config_add_host_address */
442 static int csnmp_config_add_host_collect (host_definition_t *host,
445 data_definition_t *data;
446 data_definition_t **data_list;
450 if (ci->values_num < 1)
452 WARNING ("snmp plugin: `Collect' needs at least one argument.");
456 for (i = 0; i < ci->values_num; i++)
457 if (ci->values[i].type != OCONFIG_TYPE_STRING)
459 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
463 data_list_len = host->data_list_len + ci->values_num;
464 data_list = (data_definition_t **) realloc (host->data_list,
465 sizeof (data_definition_t *) * data_list_len);
466 if (data_list == NULL)
468 host->data_list = data_list;
470 for (i = 0; i < ci->values_num; i++)
472 for (data = data_head; data != NULL; data = data->next)
473 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
478 WARNING ("snmp plugin: No such data configured: `%s'",
479 ci->values[i].value.string);
483 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
484 host->name, host->data_list_len, data->name);
486 host->data_list[host->data_list_len] = data;
487 host->data_list_len++;
488 } /* for (values_num) */
491 } /* int csnmp_config_add_host_collect */
493 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
497 if ((ci->values_num != 1)
498 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
500 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
504 interval = (int) ci->values[0].value.number;
505 hd->skip_num = interval;
506 if (hd->skip_num < 0)
510 } /* int csnmp_config_add_host_interval */
512 static int csnmp_config_add_host (oconfig_item_t *ci)
514 host_definition_t *hd;
518 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
520 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
524 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
527 memset (hd, '\0', sizeof (host_definition_t));
530 hd->name = strdup (ci->values[0].value.string);
531 if (hd->name == NULL)
537 hd->sess_handle = NULL;
540 hd->state = STATE_IDLE;
542 for (i = 0; i < ci->children_num; i++)
544 oconfig_item_t *option = ci->children + i;
547 if (strcasecmp ("Address", option->key) == 0)
548 status = csnmp_config_add_host_address (hd, option);
549 else if (strcasecmp ("Community", option->key) == 0)
550 status = csnmp_config_add_host_community (hd, option);
551 else if (strcasecmp ("Version", option->key) == 0)
552 status = csnmp_config_add_host_version (hd, option);
553 else if (strcasecmp ("Collect", option->key) == 0)
554 csnmp_config_add_host_collect (hd, option);
555 else if (strcasecmp ("Interval", option->key) == 0)
556 csnmp_config_add_host_interval (hd, option);
559 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
565 } /* for (ci->children) */
569 if (hd->address == NULL)
571 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
575 if (hd->community == NULL)
577 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
583 } /* while (status == 0) */
592 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
593 hd->name, hd->address, hd->community, hd->version);
595 if (host_head == NULL)
599 host_definition_t *last;
601 while (last->next != NULL)
607 } /* int csnmp_config_add_host */
609 static int csnmp_config (oconfig_item_t *ci)
613 call_snmp_init_once ();
615 for (i = 0; i < ci->children_num; i++)
617 oconfig_item_t *child = ci->children + i;
618 if (strcasecmp ("Data", child->key) == 0)
619 csnmp_config_add_data (child);
620 else if (strcasecmp ("Host", child->key) == 0)
621 csnmp_config_add_host (child);
624 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
626 } /* for (ci->children) */
629 } /* int csnmp_config */
631 /* End of the config stuff. Now the interesting part begins */
633 static void csnmp_host_close_session (host_definition_t *host)
637 if (host->sess_handle == NULL)
640 status = snmp_sess_close (host->sess_handle);
646 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
648 ERROR ("snmp plugin: host %s: snmp_sess_close failed: %s",
649 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
653 host->sess_handle = NULL;
654 } /* void csnmp_host_close_session */
656 static void csnmp_host_open_session (host_definition_t *host)
658 struct snmp_session sess;
660 if (host->sess_handle != NULL)
661 csnmp_host_close_session (host);
663 snmp_sess_init (&sess);
664 sess.peername = host->address;
665 sess.community = (u_char *) host->community;
666 sess.community_len = strlen (host->community);
667 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
669 /* snmp_sess_open will copy the `struct snmp_session *'. */
670 host->sess_handle = snmp_sess_open (&sess);
672 if (host->sess_handle == NULL)
676 snmp_error (&sess, NULL, NULL, &errstr);
678 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
679 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
682 } /* void csnmp_host_open_session */
684 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
685 double scale, double shift)
691 if ((vl->type == ASN_INTEGER)
692 || (vl->type == ASN_UINTEGER)
693 || (vl->type == ASN_COUNTER)
695 || (vl->type == ASN_TIMETICKS)
697 || (vl->type == ASN_GAUGE))
699 temp = (uint32_t) *vl->val.integer;
700 DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
702 else if (vl->type == ASN_COUNTER64)
704 temp = (uint32_t) vl->val.counter64->high;
706 temp += (uint32_t) vl->val.counter64->low;
707 DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
711 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
715 if (type == DS_TYPE_COUNTER)
719 else if (type == DS_TYPE_GAUGE)
723 ret.gauge = (scale * temp) + shift;
727 } /* value_t csnmp_value_list_to_value */
729 /* Returns true if all OIDs have left their subtree */
730 static int csnmp_check_res_left_subtree (const host_definition_t *host,
731 const data_definition_t *data,
732 struct snmp_pdu *res)
734 struct variable_list *vb;
736 int num_left_subtree;
744 num_left_subtree = 0;
746 /* check all the variables and count how many have left their subtree */
747 for (vb = res->variables, i = 0;
748 (vb != NULL) && (i < data->values_len);
749 vb = vb->next_variable, i++)
752 if (snmp_oid_ncompare (data->values[i].oid,
753 data->values[i].oid_len,
754 vb->name, vb->name_length,
755 data->values[i].oid_len) != 0)
759 /* check if enough variables have been returned */
760 if (i < data->values_len)
762 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
763 host->name, data->values_len, i);
767 if (data->instance.oid.oid_len > 0)
771 ERROR ("snmp plugin: host %s: Expected one more variable for "
777 if (snmp_oid_ncompare (data->instance.oid.oid,
778 data->instance.oid.oid_len,
779 vb->name, vb->name_length,
780 data->instance.oid.oid_len) != 0)
784 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
785 "left their subtree",
786 num_left_subtree, num_checked);
787 if (num_left_subtree >= num_checked)
790 } /* int csnmp_check_res_left_subtree */
792 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
793 csnmp_list_instances_t **tail,
794 const struct snmp_pdu *res)
796 csnmp_list_instances_t *il;
797 struct variable_list *vb;
799 /* Set vb on the last variable */
800 for (vb = res->variables;
801 (vb != NULL) && (vb->next_variable != NULL);
802 vb = vb->next_variable)
807 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
810 ERROR ("snmp plugin: malloc failed.");
813 il->subid = vb->name[vb->name_length - 1];
816 /* Get instance name */
817 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
822 instance_len = sizeof (il->instance) - 1;
823 if (instance_len > vb->val_len)
824 instance_len = vb->val_len;
826 strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
828 : vb->val.bitstring),
830 il->instance[instance_len] = '\0';
832 for (ptr = il->instance; *ptr != '\0'; ptr++)
834 if ((*ptr > 0) && (*ptr < 32))
836 else if (*ptr == '/')
839 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
843 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
844 snprintf (il->instance, sizeof (il->instance),
845 "%llu", val.counter);
847 il->instance[sizeof (il->instance) - 1] = '\0';
849 /* TODO: Debugging output */
858 } /* int csnmp_instance_list_add */
860 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
861 csnmp_list_instances_t *instance_list,
862 csnmp_table_values_t **value_table)
864 const data_set_t *ds;
865 value_list_t vl = VALUE_LIST_INIT;
867 csnmp_list_instances_t *instance_list_ptr;
868 csnmp_table_values_t **value_table_ptr;
874 ds = plugin_get_ds (data->type);
877 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
880 assert (ds->ds_num == data->values_len);
882 instance_list_ptr = instance_list;
884 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
886 if (value_table_ptr == NULL)
888 for (i = 0; i < data->values_len; i++)
889 value_table_ptr[i] = value_table[i];
891 vl.values_len = ds->ds_num;
892 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
893 if (vl.values == NULL)
895 ERROR ("snmp plugin: malloc failed.");
896 sfree (value_table_ptr);
900 strncpy (vl.host, host->name, sizeof (vl.host));
901 vl.host[sizeof (vl.host) - 1] = '\0';
902 strcpy (vl.plugin, "snmp");
904 vl.interval = host->skip_num;
905 vl.time = time (NULL);
910 while (have_more != 0)
912 if (instance_list != NULL)
914 while ((instance_list_ptr != NULL)
915 && (instance_list_ptr->subid < subid))
916 instance_list_ptr = instance_list_ptr->next;
918 if (instance_list_ptr == NULL)
923 else if (instance_list_ptr->subid > subid)
925 subid = instance_list_ptr->subid;
928 } /* if (instance_list != NULL) */
930 for (i = 0; i < data->values_len; i++)
932 while ((value_table_ptr[i] != NULL)
933 && (value_table_ptr[i]->subid < subid))
934 value_table_ptr[i] = value_table_ptr[i]->next;
936 if (value_table_ptr[i] == NULL)
941 else if (value_table_ptr[i]->subid > subid)
943 subid = value_table_ptr[i]->subid;
946 } /* for (i = 0; i < columns; i++) */
947 /* The subid has been increased - start scanning from the beginning
949 if (i < data->values_len)
952 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
953 * to the same subid. instance_list_ptr is either NULL or points to the
954 * same subid, too. */
956 for (i = 1; i < data->values_len; i++)
958 assert (value_table_ptr[i] != NULL);
959 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
961 assert ((instance_list_ptr == NULL)
962 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
965 if (instance_list_ptr == NULL)
966 snprintf (vl.type_instance, sizeof (vl.type_instance), "%u",
969 strncpy (vl.type_instance, instance_list_ptr->instance,
970 sizeof (vl.type_instance));
971 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
973 for (i = 0; i < data->values_len; i++)
974 vl.values[i] = value_table_ptr[i]->value;
976 /* If we get here `vl.type_instance' and all `vl.values' have been set */
977 plugin_dispatch_values (data->type, &vl);
980 } /* while (have_more != 0) */
983 sfree (value_table_ptr);
986 } /* int csnmp_dispatch_table */
988 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
990 struct snmp_pdu *req;
991 struct snmp_pdu *res;
992 struct variable_list *vb;
994 const data_set_t *ds;
996 uint32_t oid_list_len;
1001 /* `value_table' and `value_table_ptr' implement a linked list for each
1002 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1003 * instance names. This is used to jump gaps in the table. */
1004 csnmp_list_instances_t *instance_list;
1005 csnmp_list_instances_t *instance_list_ptr;
1006 csnmp_table_values_t **value_table;
1007 csnmp_table_values_t **value_table_ptr;
1009 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1010 host->name, data->name);
1012 if (host->sess_handle == NULL)
1014 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1018 ds = plugin_get_ds (data->type);
1021 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1025 if (ds->ds_num != data->values_len)
1027 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1028 data->type, ds->ds_num, data->values_len);
1032 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1033 oid_list_len = data->values_len + 1;
1034 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1035 if (oid_list == NULL)
1037 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1040 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1041 if (data->instance.oid.oid_len > 0)
1042 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1046 /* Allocate the `value_table' */
1047 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1048 * 2 * data->values_len);
1049 if (value_table == NULL)
1051 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1055 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1056 value_table_ptr = value_table + data->values_len;
1058 instance_list = NULL;
1059 instance_list_ptr = NULL;
1064 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1067 ERROR ("snmp plugin: snmp_pdu_create failed.");
1072 for (i = 0; i < oid_list_len; i++)
1073 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1075 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1077 if (status != STAT_SUCCESS)
1079 char *errstr = NULL;
1081 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1082 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1083 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1084 csnmp_host_close_session (host);
1090 assert (res != NULL);
1092 vb = res->variables;
1099 /* Check if all values (and possibly the instance) have left their
1101 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1104 /* if an instance-OID is configured.. */
1105 if (data->instance.oid.oid_len > 0)
1107 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1108 * add it to the list */
1109 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1112 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1117 /* Set vb on the last variable */
1118 for (vb = res->variables;
1119 (vb != NULL) && (vb->next_variable != NULL);
1120 vb = vb->next_variable)
1128 /* Copy OID to oid_list[data->values_len] */
1129 memcpy (oid_list[data->values_len].oid, vb->name,
1130 sizeof (oid) * vb->name_length);
1131 oid_list[data->values_len].oid_len = vb->name_length;
1134 for (vb = res->variables, i = 0;
1135 (vb != NULL) && (i < data->values_len);
1136 vb = vb->next_variable, i++)
1138 csnmp_table_values_t *vt;
1140 /* Check if we left the subtree */
1141 if (snmp_oid_ncompare (data->values[i].oid,
1142 data->values[i].oid_len,
1143 vb->name, vb->name_length,
1144 data->values[i].oid_len) != 0)
1146 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1147 host->name, data->name, i);
1151 if ((value_table_ptr[i] != NULL)
1152 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1154 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1155 "SUBID is not increasing.",
1156 host->name, data->name, i);
1160 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1163 ERROR ("snmp plugin: malloc failed.");
1168 vt->subid = vb->name[vb->name_length - 1];
1169 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1170 data->scale, data->shift);
1173 if (value_table_ptr[i] == NULL)
1174 value_table[i] = vt;
1176 value_table_ptr[i]->next = vt;
1177 value_table_ptr[i] = vt;
1179 /* Copy OID to oid_list[i + 1] */
1180 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1181 oid_list[i].oid_len = vb->name_length;
1182 } /* for (i = data->values_len) */
1185 snmp_free_pdu (res);
1187 } /* while (status == 0) */
1190 csnmp_dispatch_table (host, data, instance_list, value_table);
1192 /* Free all allocated variables here */
1193 while (instance_list != NULL)
1195 instance_list_ptr = instance_list->next;
1196 sfree (instance_list);
1197 instance_list = instance_list_ptr;
1200 for (i = 0; i < data->values_len; i++)
1202 csnmp_table_values_t *tmp;
1203 while (value_table[i] != NULL)
1205 tmp = value_table[i]->next;
1206 sfree (value_table[i]);
1207 value_table[i] = tmp;
1211 sfree (value_table);
1215 } /* int csnmp_read_table */
1217 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1219 struct snmp_pdu *req;
1220 struct snmp_pdu *res;
1221 struct variable_list *vb;
1223 const data_set_t *ds;
1224 value_list_t vl = VALUE_LIST_INIT;
1229 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1230 host->name, data->name);
1232 if (host->sess_handle == NULL)
1234 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1238 ds = plugin_get_ds (data->type);
1241 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1245 if (ds->ds_num != data->values_len)
1247 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1248 data->type, ds->ds_num, data->values_len);
1252 vl.values_len = ds->ds_num;
1253 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1254 if (vl.values == NULL)
1256 for (i = 0; i < vl.values_len; i++)
1258 if (ds->ds[i].type == DS_TYPE_COUNTER)
1259 vl.values[i].counter = 0;
1261 vl.values[i].gauge = NAN;
1264 strncpy (vl.host, host->name, sizeof (vl.host));
1265 vl.host[sizeof (vl.host) - 1] = '\0';
1266 strcpy (vl.plugin, "snmp");
1267 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1268 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1270 vl.interval = host->skip_num;
1272 req = snmp_pdu_create (SNMP_MSG_GET);
1275 ERROR ("snmp plugin: snmp_pdu_create failed.");
1280 for (i = 0; i < data->values_len; i++)
1281 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1282 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1284 if (status != STAT_SUCCESS)
1286 char *errstr = NULL;
1288 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1289 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1290 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1291 csnmp_host_close_session (host);
1297 vl.time = time (NULL);
1299 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1302 snprint_variable (buffer, sizeof (buffer),
1303 vb->name, vb->name_length, vb);
1304 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1306 for (i = 0; i < data->values_len; i++)
1307 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1308 vb->name, vb->name_length) == 0)
1309 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1310 data->scale, data->shift);
1311 } /* for (res->variables) */
1313 snmp_free_pdu (res);
1315 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1316 plugin_dispatch_values (data->type, &vl);
1320 } /* int csnmp_read_value */
1322 static int csnmp_read_host (host_definition_t *host)
1326 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1328 if (host->sess_handle == NULL)
1329 csnmp_host_open_session (host);
1331 if (host->sess_handle == NULL)
1334 for (i = 0; i < host->data_list_len; i++)
1336 data_definition_t *data = host->data_list[i];
1339 csnmp_read_table (host, data);
1341 csnmp_read_value (host, data);
1345 } /* int csnmp_read_host */
1347 static void *csnmp_read_thread (void *data)
1349 host_definition_t *host;
1351 pthread_mutex_lock (&host_lock);
1352 while (do_shutdown == 0)
1354 pthread_cond_wait (&host_cond, &host_lock);
1356 for (host = host_head; host != NULL; host = host->next)
1358 if (do_shutdown != 0)
1360 if (host->state != STATE_WAIT)
1363 host->state = STATE_BUSY;
1364 pthread_mutex_unlock (&host_lock);
1365 csnmp_read_host (host);
1366 pthread_mutex_lock (&host_lock);
1367 host->state = STATE_IDLE;
1369 } /* while (do_shutdown == 0) */
1370 pthread_mutex_unlock (&host_lock);
1372 pthread_exit ((void *) 0);
1373 return ((void *) 0);
1374 } /* void *csnmp_read_thread */
1376 static int csnmp_init (void)
1378 host_definition_t *host;
1381 if (host_head == NULL)
1383 NOTICE ("snmp plugin: No host has been defined.");
1387 call_snmp_init_once ();
1390 for (host = host_head; host != NULL; host = host->next)
1393 /* We need to initialize `skip_num' here, because `interval_g' isn't
1394 * initialized during `configure'. */
1395 host->skip_left = interval_g;
1396 if (host->skip_num == 0)
1398 host->skip_num = interval_g;
1400 else if (host->skip_num < interval_g)
1402 host->skip_num = interval_g;
1403 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1404 host->name, host->skip_num);
1407 csnmp_host_open_session (host);
1410 /* Now start the reading threads */
1411 if (threads_num > 3)
1413 threads_num = 3 + ((threads_num - 3) / 10);
1414 if (threads_num > 10)
1418 threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1419 if (threads == NULL)
1421 ERROR ("snmp plugin: malloc failed.");
1424 memset (threads, '\0', threads_num * sizeof (pthread_t));
1426 for (i = 0; i < threads_num; i++)
1427 pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1430 } /* int csnmp_init */
1432 static int csnmp_read (void)
1434 host_definition_t *host;
1437 if (host_head == NULL)
1439 INFO ("snmp plugin: No hosts configured.");
1445 pthread_mutex_lock (&host_lock);
1446 for (host = host_head; host != NULL; host = host->next)
1448 if (host->state != STATE_IDLE)
1451 host->skip_left -= interval_g;
1452 if (host->skip_left >= interval_g)
1455 host->state = STATE_WAIT;
1457 host->skip_left = host->skip_num;
1460 pthread_cond_broadcast (&host_cond);
1461 pthread_mutex_unlock (&host_lock);
1464 } /* int csnmp_read */
1466 static int csnmp_shutdown (void)
1468 host_definition_t *host_this;
1469 host_definition_t *host_next;
1471 data_definition_t *data_this;
1472 data_definition_t *data_next;
1476 pthread_mutex_lock (&host_lock);
1478 pthread_cond_broadcast (&host_cond);
1479 pthread_mutex_unlock (&host_lock);
1481 for (i = 0; i < threads_num; i++)
1482 pthread_join (threads[i], NULL);
1484 /* Now that all the threads have exited, let's free all the global variables.
1485 * This isn't really neccessary, I guess, but I think it's good stile to do
1487 host_this = host_head;
1489 while (host_this != NULL)
1491 host_next = host_this->next;
1493 csnmp_host_close_session (host_this);
1495 sfree (host_this->name);
1496 sfree (host_this->address);
1497 sfree (host_this->community);
1498 sfree (host_this->data_list);
1501 host_this = host_next;
1504 data_this = data_head;
1506 while (data_this != NULL)
1508 data_next = data_this->next;
1510 sfree (data_this->name);
1511 sfree (data_this->type);
1512 sfree (data_this->values);
1515 data_this = data_next;
1519 } /* int csnmp_shutdown */
1521 void module_register (void)
1523 plugin_register_complex_config ("snmp", csnmp_config);
1524 plugin_register_init ("snmp", csnmp_init);
1525 plugin_register_read ("snmp", csnmp_read);
1526 plugin_register_shutdown ("snmp", csnmp_shutdown);
1527 } /* void module_register */
1530 * vim: shiftwidth=2 softtabstop=2 tabstop=8