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 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
730 csnmp_list_instances_t *instance_list,
731 csnmp_table_values_t **value_table)
733 const data_set_t *ds;
734 value_list_t vl = VALUE_LIST_INIT;
736 csnmp_list_instances_t *instance_list_ptr;
737 csnmp_table_values_t **value_table_ptr;
741 ds = plugin_get_ds (data->type);
744 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
747 assert (ds->ds_num == data->values_len);
749 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
751 if (value_table_ptr == NULL)
753 for (i = 0; i < data->values_len; i++)
754 value_table_ptr[i] = value_table[i];
756 vl.values_len = ds->ds_num;
757 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
758 if (vl.values == NULL)
760 sfree (value_table_ptr);
764 strncpy (vl.host, host->name, sizeof (vl.host));
765 vl.host[sizeof (vl.host) - 1] = '\0';
766 strcpy (vl.plugin, "snmp");
768 vl.interval = host->skip_num;
769 vl.time = time (NULL);
771 for (instance_list_ptr = instance_list;
772 instance_list_ptr != NULL;
773 instance_list_ptr = instance_list_ptr->next)
775 strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
776 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
778 for (i = 0; i < data->values_len; i++)
780 while ((value_table_ptr[i] != NULL)
781 && (value_table_ptr[i]->subid < instance_list_ptr->subid))
782 value_table_ptr[i] = value_table_ptr[i]->next;
783 if ((value_table_ptr[i] == NULL)
784 || (value_table_ptr[i]->subid != instance_list_ptr->subid))
786 vl.values[i] = value_table_ptr[i]->value;
787 } /* for (data->values_len) */
789 /* If the for-loop was aborted early, not all subid's match. */
790 if (i < data->values_len)
792 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
794 host->name, data->name, i, instance_list_ptr->subid);
798 /* If we get here `vl.type_instance' and all `vl.values' have been set */
799 plugin_dispatch_values (data->type, &vl);
800 } /* for (instance_list) */
803 sfree (value_table_ptr);
806 } /* int csnmp_dispatch_table */
808 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
810 struct snmp_pdu *req;
811 struct snmp_pdu *res;
812 struct variable_list *vb;
814 const data_set_t *ds;
816 uint32_t oid_list_len;
821 /* `value_table' and `value_table_ptr' implement a linked list for each
822 * value. `instance_list' and `instance_list_ptr' implement a linked list of
823 * instance names. This is used to jump gaps in the table. */
824 csnmp_list_instances_t *instance_list;
825 csnmp_list_instances_t *instance_list_ptr;
826 csnmp_table_values_t **value_table;
827 csnmp_table_values_t **value_table_ptr;
829 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
830 host->name, data->name);
832 if (host->sess_handle == NULL)
834 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
838 ds = plugin_get_ds (data->type);
841 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
845 if (ds->ds_num != data->values_len)
847 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
848 data->type, ds->ds_num, data->values_len);
852 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
853 oid_list_len = data->values_len + 1;
854 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
855 if (oid_list == NULL)
857 memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
858 for (i = 0; i < data->values_len; i++)
859 memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
861 /* Allocate the `value_table' */
862 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
863 * 2 * data->values_len);
864 if (value_table == NULL)
869 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
870 value_table_ptr = value_table + data->values_len;
872 instance_list = NULL;
873 instance_list_ptr = NULL;
878 csnmp_list_instances_t *il;
880 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
883 ERROR ("snmp plugin: snmp_pdu_create failed.");
888 for (i = 0; i < oid_list_len; i++)
889 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
891 status = snmp_sess_synch_response (host->sess_handle, req, &res);
893 if (status != STAT_SUCCESS)
897 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
898 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
899 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
900 csnmp_host_close_session (host);
906 assert (res != NULL);
915 /* Check if we left the subtree */
916 if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
917 vb->name, vb->name_length,
918 data->instance.oid.oid_len) != 0)
921 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
922 * add it to the list */
923 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
929 il->subid = vb->name[vb->name_length - 1];
932 /* Get instance name */
933 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
938 instance_len = sizeof (il->instance) - 1;
939 if (instance_len > vb->val_len)
940 instance_len = vb->val_len;
942 strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
944 : vb->val.bitstring),
946 il->instance[instance_len] = '\0';
948 for (ptr = il->instance; *ptr != '\0'; ptr++)
950 if ((*ptr > 0) && (*ptr < 32))
952 else if (*ptr == '/')
955 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
959 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
960 snprintf (il->instance, sizeof (il->instance),
961 "%llu", val.counter);
963 il->instance[sizeof (il->instance) - 1] = '\0';
964 DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
965 data->name, il->instance);
967 if (instance_list_ptr == NULL)
970 instance_list_ptr->next = il;
971 instance_list_ptr = il;
973 /* Copy OID to oid_list[0] */
974 memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
975 oid_list[0].oid_len = vb->name_length;
977 for (i = 0; i < data->values_len; i++)
979 csnmp_table_values_t *vt;
981 vb = vb->next_variable;
988 /* Check if we left the subtree */
989 if (snmp_oid_ncompare (data->values[i].oid,
990 data->values[i].oid_len,
991 vb->name, vb->name_length,
992 data->values[i].oid_len) != 0)
994 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
995 host->name, data->name, i);
999 if ((value_table_ptr[i] != NULL)
1000 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1002 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
1003 host->name, data->name, i);
1007 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1010 vt->subid = vb->name[vb->name_length - 1];
1011 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1012 data->scale, data->shift);
1015 if (value_table_ptr[i] == NULL)
1016 value_table[i] = vt;
1018 value_table_ptr[i]->next = vt;
1019 value_table_ptr[i] = vt;
1022 /* Copy OID to oid_list[i + 1] */
1023 memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
1024 oid_list[i + 1].oid_len = vb->name_length;
1025 } /* for (i = data->values_len) */
1028 snmp_free_pdu (res);
1030 } /* while (status == 0) */
1033 csnmp_dispatch_table (host, data, instance_list, value_table);
1035 /* Free all allocated variables here */
1036 while (instance_list != NULL)
1038 instance_list_ptr = instance_list->next;
1039 sfree (instance_list);
1040 instance_list = instance_list_ptr;
1043 for (i = 0; i < data->values_len; i++)
1045 csnmp_table_values_t *tmp;
1046 while (value_table[i] != NULL)
1048 tmp = value_table[i]->next;
1049 sfree (value_table[i]);
1050 value_table[i] = tmp;
1054 sfree (value_table);
1058 } /* int csnmp_read_table */
1060 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1062 struct snmp_pdu *req;
1063 struct snmp_pdu *res;
1064 struct variable_list *vb;
1066 const data_set_t *ds;
1067 value_list_t vl = VALUE_LIST_INIT;
1072 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1073 host->name, data->name);
1075 if (host->sess_handle == NULL)
1077 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1081 ds = plugin_get_ds (data->type);
1084 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1088 if (ds->ds_num != data->values_len)
1090 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1091 data->type, ds->ds_num, data->values_len);
1095 vl.values_len = ds->ds_num;
1096 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1097 if (vl.values == NULL)
1099 for (i = 0; i < vl.values_len; i++)
1101 if (ds->ds[i].type == DS_TYPE_COUNTER)
1102 vl.values[i].counter = 0;
1104 vl.values[i].gauge = NAN;
1107 strncpy (vl.host, host->name, sizeof (vl.host));
1108 vl.host[sizeof (vl.host) - 1] = '\0';
1109 strcpy (vl.plugin, "snmp");
1110 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1111 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1113 vl.interval = host->skip_num;
1115 req = snmp_pdu_create (SNMP_MSG_GET);
1118 ERROR ("snmp plugin: snmp_pdu_create failed.");
1123 for (i = 0; i < data->values_len; i++)
1124 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1125 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1127 if (status != STAT_SUCCESS)
1129 char *errstr = NULL;
1131 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1132 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1133 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1134 csnmp_host_close_session (host);
1140 vl.time = time (NULL);
1142 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1145 snprint_variable (buffer, sizeof (buffer),
1146 vb->name, vb->name_length, vb);
1147 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1149 for (i = 0; i < data->values_len; i++)
1150 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1151 vb->name, vb->name_length) == 0)
1152 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1153 data->scale, data->shift);
1154 } /* for (res->variables) */
1156 snmp_free_pdu (res);
1158 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1159 plugin_dispatch_values (data->type, &vl);
1163 } /* int csnmp_read_value */
1165 static int csnmp_read_host (host_definition_t *host)
1169 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1171 if (host->sess_handle == NULL)
1172 csnmp_host_open_session (host);
1174 if (host->sess_handle == NULL)
1177 for (i = 0; i < host->data_list_len; i++)
1179 data_definition_t *data = host->data_list[i];
1182 csnmp_read_table (host, data);
1184 csnmp_read_value (host, data);
1188 } /* int csnmp_read_host */
1190 static void *csnmp_read_thread (void *data)
1192 host_definition_t *host;
1194 pthread_mutex_lock (&host_lock);
1195 while (do_shutdown == 0)
1197 pthread_cond_wait (&host_cond, &host_lock);
1199 for (host = host_head; host != NULL; host = host->next)
1201 if (do_shutdown != 0)
1203 if (host->state != STATE_WAIT)
1206 host->state = STATE_BUSY;
1207 pthread_mutex_unlock (&host_lock);
1208 csnmp_read_host (host);
1209 pthread_mutex_lock (&host_lock);
1210 host->state = STATE_IDLE;
1212 } /* while (do_shutdown == 0) */
1213 pthread_mutex_unlock (&host_lock);
1215 pthread_exit ((void *) 0);
1216 return ((void *) 0);
1217 } /* void *csnmp_read_thread */
1219 static int csnmp_init (void)
1221 host_definition_t *host;
1224 if (host_head == NULL)
1226 NOTICE ("snmp plugin: No host has been defined.");
1230 call_snmp_init_once ();
1233 for (host = host_head; host != NULL; host = host->next)
1236 /* We need to initialize `skip_num' here, because `interval_g' isn't
1237 * initialized during `configure'. */
1238 host->skip_left = interval_g;
1239 if (host->skip_num == 0)
1241 host->skip_num = interval_g;
1243 else if (host->skip_num < interval_g)
1245 host->skip_num = interval_g;
1246 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1247 host->name, host->skip_num);
1250 csnmp_host_open_session (host);
1253 /* Now start the reading threads */
1254 if (threads_num > 3)
1256 threads_num = 3 + ((threads_num - 3) / 10);
1257 if (threads_num > 10)
1261 threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1262 if (threads == NULL)
1264 ERROR ("snmp plugin: malloc failed.");
1267 memset (threads, '\0', threads_num * sizeof (pthread_t));
1269 for (i = 0; i < threads_num; i++)
1270 pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1273 } /* int csnmp_init */
1275 static int csnmp_read (void)
1277 host_definition_t *host;
1280 if (host_head == NULL)
1282 INFO ("snmp plugin: No hosts configured.");
1288 pthread_mutex_lock (&host_lock);
1289 for (host = host_head; host != NULL; host = host->next)
1291 if (host->state != STATE_IDLE)
1294 host->skip_left -= interval_g;
1295 if (host->skip_left >= interval_g)
1298 host->state = STATE_WAIT;
1300 host->skip_left = host->skip_num;
1303 pthread_cond_broadcast (&host_cond);
1304 pthread_mutex_unlock (&host_lock);
1307 } /* int csnmp_read */
1309 static int csnmp_shutdown (void)
1311 host_definition_t *host_this;
1312 host_definition_t *host_next;
1314 data_definition_t *data_this;
1315 data_definition_t *data_next;
1319 pthread_mutex_lock (&host_lock);
1321 pthread_cond_broadcast (&host_cond);
1322 pthread_mutex_unlock (&host_lock);
1324 for (i = 0; i < threads_num; i++)
1325 pthread_join (threads[i], NULL);
1327 /* Now that all the threads have exited, let's free all the global variables.
1328 * This isn't really neccessary, I guess, but I think it's good stile to do
1330 host_this = host_head;
1332 while (host_this != NULL)
1334 host_next = host_this->next;
1336 csnmp_host_close_session (host_this);
1338 sfree (host_this->name);
1339 sfree (host_this->address);
1340 sfree (host_this->community);
1341 sfree (host_this->data_list);
1344 host_this = host_next;
1347 data_this = data_head;
1349 while (data_this != NULL)
1351 data_next = data_this->next;
1353 sfree (data_this->name);
1354 sfree (data_this->type);
1355 sfree (data_this->values);
1358 data_this = data_next;
1362 } /* int csnmp_shutdown */
1364 void module_register (void)
1366 plugin_register_complex_config ("snmp", csnmp_config);
1367 plugin_register_init ("snmp", csnmp_init);
1368 plugin_register_read ("snmp", csnmp_read);
1369 plugin_register_shutdown ("snmp", csnmp_shutdown);
1370 } /* void module_register */
1373 * vim: shiftwidth=2 softtabstop=2 tabstop=8