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 */
56 struct data_definition_s *next;
58 typedef struct data_definition_s data_definition_t;
60 struct host_definition_s
69 data_definition_t **data_list;
71 enum /****************************************************/
73 STATE_IDLE, /* - just sits there until `skip_left < interval_g' */
74 STATE_WAIT, /* - waits to be queried. */
75 STATE_BUSY /* - is currently being queried. */
76 } state; /****************************************************/
77 struct host_definition_s *next;
79 typedef struct host_definition_s host_definition_t;
81 /* These two types are used to cache values in `csnmp_read_table' to handle
83 struct csnmp_list_instances_s
86 char instance[DATA_MAX_NAME_LEN];
87 struct csnmp_list_instances_s *next;
89 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
91 struct csnmp_table_values_s
95 struct csnmp_table_values_s *next;
97 typedef struct csnmp_table_values_s csnmp_table_values_t;
102 static int do_shutdown = 0;
104 pthread_t *threads = NULL;
107 static data_definition_t *data_head = NULL;
108 static host_definition_t *host_head = NULL;
110 static pthread_mutex_t host_lock = PTHREAD_MUTEX_INITIALIZER;
111 static pthread_cond_t host_cond = PTHREAD_COND_INITIALIZER;
116 /* First there are many functions which do configuration stuff. It's a big
117 * bloated and messy, I'm afraid. */
120 * Callgraph for the config stuff:
122 * +-> call_snmp_init_once
123 * +-> csnmp_config_add_data
124 * ! +-> csnmp_config_add_data_type
125 * ! +-> csnmp_config_add_data_table
126 * ! +-> csnmp_config_add_data_instance
127 * ! +-> csnmp_config_add_data_values
128 * +-> csnmp_config_add_host
129 * +-> csnmp_config_add_host_address
130 * +-> csnmp_config_add_host_community
131 * +-> csnmp_config_add_host_version
132 * +-> csnmp_config_add_host_collect
133 * +-> csnmp_config_add_host_interval
135 static void call_snmp_init_once (void)
137 static int have_init = 0;
140 init_snmp (PACKAGE_NAME);
142 } /* void call_snmp_init_once */
144 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
146 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
148 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
152 if (dd->type != NULL)
155 dd->type = strdup (ci->values[0].value.string);
156 if (dd->type == NULL)
160 } /* int csnmp_config_add_data_type */
162 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
164 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
166 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
170 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
173 } /* int csnmp_config_add_data_table */
175 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
177 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
179 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
185 /* Instance is an OID */
186 dd->instance.oid.oid_len = MAX_OID_LEN;
188 if (!read_objid (ci->values[0].value.string,
189 dd->instance.oid.oid, &dd->instance.oid.oid_len))
191 ERROR ("snmp plugin: read_objid (%s) failed.",
192 ci->values[0].value.string);
198 /* Instance is a simple string */
199 strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
203 } /* int csnmp_config_add_data_instance */
205 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
209 if (ci->values_num < 1)
211 WARNING ("snmp plugin: `Values' needs at least one argument.");
215 for (i = 0; i < ci->values_num; i++)
216 if (ci->values[i].type != OCONFIG_TYPE_STRING)
218 WARNING ("snmp plugin: `Values' needs only string argument.");
222 if (dd->values != NULL)
224 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
225 if (dd->values == NULL)
227 dd->values_len = ci->values_num;
229 for (i = 0; i < ci->values_num; i++)
231 dd->values[i].oid_len = MAX_OID_LEN;
233 if (NULL == snmp_parse_oid (ci->values[i].value.string,
234 dd->values[i].oid, &dd->values[i].oid_len))
236 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
237 ci->values[i].value.string);
246 } /* int csnmp_config_add_data_instance */
248 static int csnmp_config_add_data (oconfig_item_t *ci)
250 data_definition_t *dd;
254 if ((ci->values_num != 1)
255 || (ci->values[0].type != OCONFIG_TYPE_STRING))
257 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
261 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
264 memset (dd, '\0', sizeof (data_definition_t));
266 dd->name = strdup (ci->values[0].value.string);
267 if (dd->name == NULL)
273 for (i = 0; i < ci->children_num; i++)
275 oconfig_item_t *option = ci->children + i;
278 if (strcasecmp ("Type", option->key) == 0)
279 status = csnmp_config_add_data_type (dd, option);
280 else if (strcasecmp ("Table", option->key) == 0)
281 status = csnmp_config_add_data_table (dd, option);
282 else if (strcasecmp ("Instance", option->key) == 0)
283 status = csnmp_config_add_data_instance (dd, option);
284 else if (strcasecmp ("Values", option->key) == 0)
285 status = csnmp_config_add_data_values (dd, option);
288 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
294 } /* for (ci->children) */
298 if (dd->type == NULL)
300 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
304 if (dd->values == NULL)
306 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
312 } /* while (status == 0) */
322 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
323 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
325 if (data_head == NULL)
329 data_definition_t *last;
331 while (last->next != NULL)
337 } /* int csnmp_config_add_data */
339 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
341 if ((ci->values_num != 1)
342 || (ci->values[0].type != OCONFIG_TYPE_STRING))
344 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
348 if (hd->address == NULL)
351 hd->address = strdup (ci->values[0].value.string);
352 if (hd->address == NULL)
355 DEBUG ("snmp plugin: host = %s; host->address = %s;",
356 hd->name, hd->address);
359 } /* int csnmp_config_add_host_address */
361 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
363 if ((ci->values_num != 1)
364 || (ci->values[0].type != OCONFIG_TYPE_STRING))
366 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
370 if (hd->community == NULL)
371 free (hd->community);
373 hd->community = strdup (ci->values[0].value.string);
374 if (hd->community == NULL)
377 DEBUG ("snmp plugin: host = %s; host->community = %s;",
378 hd->name, hd->community);
381 } /* int csnmp_config_add_host_community */
383 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
387 if ((ci->values_num != 1)
388 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
390 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
394 version = (int) ci->values[0].value.number;
395 if ((version != 1) && (version != 2))
397 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
401 hd->version = version;
404 } /* int csnmp_config_add_host_address */
406 static int csnmp_config_add_host_collect (host_definition_t *host,
409 data_definition_t *data;
410 data_definition_t **data_list;
414 if (ci->values_num < 1)
416 WARNING ("snmp plugin: `Collect' needs at least one argument.");
420 for (i = 0; i < ci->values_num; i++)
421 if (ci->values[i].type != OCONFIG_TYPE_STRING)
423 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
427 data_list_len = host->data_list_len + ci->values_num;
428 data_list = (data_definition_t **) realloc (host->data_list,
429 sizeof (data_definition_t *) * data_list_len);
430 if (data_list == NULL)
432 host->data_list = data_list;
434 for (i = 0; i < ci->values_num; i++)
436 for (data = data_head; data != NULL; data = data->next)
437 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
442 WARNING ("snmp plugin: No such data configured: `%s'",
443 ci->values[i].value.string);
447 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
448 host->name, host->data_list_len, data->name);
450 host->data_list[host->data_list_len] = data;
451 host->data_list_len++;
452 } /* for (values_num) */
455 } /* int csnmp_config_add_host_collect */
457 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
461 if ((ci->values_num != 1)
462 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
464 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
468 interval = (int) ci->values[0].value.number;
469 hd->skip_num = interval;
470 if (hd->skip_num < 0)
474 } /* int csnmp_config_add_host_interval */
476 static int csnmp_config_add_host (oconfig_item_t *ci)
478 host_definition_t *hd;
482 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
484 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
488 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
491 memset (hd, '\0', sizeof (host_definition_t));
494 hd->name = strdup (ci->values[0].value.string);
495 if (hd->name == NULL)
501 hd->sess_handle = NULL;
504 hd->state = STATE_IDLE;
506 for (i = 0; i < ci->children_num; i++)
508 oconfig_item_t *option = ci->children + i;
511 if (strcasecmp ("Address", option->key) == 0)
512 status = csnmp_config_add_host_address (hd, option);
513 else if (strcasecmp ("Community", option->key) == 0)
514 status = csnmp_config_add_host_community (hd, option);
515 else if (strcasecmp ("Version", option->key) == 0)
516 status = csnmp_config_add_host_version (hd, option);
517 else if (strcasecmp ("Collect", option->key) == 0)
518 csnmp_config_add_host_collect (hd, option);
519 else if (strcasecmp ("Interval", option->key) == 0)
520 csnmp_config_add_host_interval (hd, option);
523 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
529 } /* for (ci->children) */
533 if (hd->address == NULL)
535 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
539 if (hd->community == NULL)
541 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
547 } /* while (status == 0) */
556 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
557 hd->name, hd->address, hd->community, hd->version);
559 if (host_head == NULL)
563 host_definition_t *last;
565 while (last->next != NULL)
571 } /* int csnmp_config_add_host */
573 static int csnmp_config (oconfig_item_t *ci)
577 call_snmp_init_once ();
579 for (i = 0; i < ci->children_num; i++)
581 oconfig_item_t *child = ci->children + i;
582 if (strcasecmp ("Data", child->key) == 0)
583 csnmp_config_add_data (child);
584 else if (strcasecmp ("Host", child->key) == 0)
585 csnmp_config_add_host (child);
588 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
590 } /* for (ci->children) */
593 } /* int csnmp_config */
595 /* End of the config stuff. Now the interesting part begins */
597 static void csnmp_host_close_session (host_definition_t *host)
601 if (host->sess_handle == NULL)
604 status = snmp_sess_close (host->sess_handle);
610 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
612 ERROR ("snmp plugin: snmp_sess_close failed: %s",
613 (errstr == NULL) ? "Unknown problem" : errstr);
617 host->sess_handle = NULL;
618 } /* void csnmp_host_close_session */
620 static void csnmp_host_open_session (host_definition_t *host)
622 struct snmp_session sess;
624 if (host->sess_handle != NULL)
625 csnmp_host_close_session (host);
627 snmp_sess_init (&sess);
628 sess.peername = host->address;
629 sess.community = (u_char *) host->community;
630 sess.community_len = strlen (host->community);
631 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
633 /* snmp_sess_open will copy the `struct snmp_session *'. */
634 host->sess_handle = snmp_sess_open (&sess);
636 if (host->sess_handle == NULL)
640 snmp_error (&sess, NULL, NULL, &errstr);
642 ERROR ("snmp plugin: snmp_sess_open failed: %s",
643 (errstr == NULL) ? "Unknown problem" : errstr);
646 } /* void csnmp_host_open_session */
648 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
654 if ((vl->type == ASN_INTEGER)
655 || (vl->type == ASN_UINTEGER)
656 || (vl->type == ASN_COUNTER)
658 || (vl->type == ASN_TIMETICKS)
660 || (vl->type == ASN_GAUGE))
662 temp = (uint32_t) *vl->val.integer;
663 DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
665 else if (vl->type == ASN_COUNTER64)
667 temp = (uint32_t) vl->val.counter64->high;
669 temp += (uint32_t) vl->val.counter64->low;
670 DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
674 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
678 if (type == DS_TYPE_COUNTER)
682 else if (type == DS_TYPE_GAUGE)
690 } /* value_t csnmp_value_list_to_value */
692 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
693 csnmp_list_instances_t *instance_list,
694 csnmp_table_values_t **value_table)
696 const data_set_t *ds;
697 value_list_t vl = VALUE_LIST_INIT;
699 csnmp_list_instances_t *instance_list_ptr;
700 csnmp_table_values_t **value_table_ptr;
704 ds = plugin_get_ds (data->type);
707 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
710 assert (ds->ds_num == data->values_len);
712 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
714 if (value_table_ptr == NULL)
716 for (i = 0; i < data->values_len; i++)
717 value_table_ptr[i] = value_table[i];
719 vl.values_len = ds->ds_num;
720 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
721 if (vl.values == NULL)
723 sfree (value_table_ptr);
727 strncpy (vl.host, host->name, sizeof (vl.host));
728 vl.host[sizeof (vl.host) - 1] = '\0';
729 strcpy (vl.plugin, "snmp");
731 vl.interval = host->skip_num;
732 vl.time = time (NULL);
734 for (instance_list_ptr = instance_list;
735 instance_list_ptr != NULL;
736 instance_list_ptr = instance_list_ptr->next)
738 strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
739 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
741 for (i = 0; i < data->values_len; i++)
743 while ((value_table_ptr[i] != NULL)
744 && (value_table_ptr[i]->subid < instance_list_ptr->subid))
745 value_table_ptr[i] = value_table_ptr[i]->next;
746 if ((value_table_ptr[i] == NULL)
747 || (value_table_ptr[i]->subid != instance_list_ptr->subid))
749 vl.values[i] = value_table_ptr[i]->value;
750 } /* for (data->values_len) */
752 /* If the for-loop was aborted early, not all subid's match. */
753 if (i < data->values_len)
755 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
757 host->name, data->name, i, instance_list_ptr->subid);
761 /* If we get here `vl.type_instance' and all `vl.values' have been set */
762 plugin_dispatch_values (data->type, &vl);
763 } /* for (instance_list) */
766 sfree (value_table_ptr);
769 } /* int csnmp_dispatch_table */
771 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
773 struct snmp_pdu *req;
774 struct snmp_pdu *res;
775 struct variable_list *vb;
777 const data_set_t *ds;
779 uint32_t oid_list_len;
784 /* `value_table' and `value_table_ptr' implement a linked list for each
785 * value. `instance_list' and `instance_list_ptr' implement a linked list of
786 * instance names. This is used to jump gaps in the table. */
787 csnmp_list_instances_t *instance_list;
788 csnmp_list_instances_t *instance_list_ptr;
789 csnmp_table_values_t **value_table;
790 csnmp_table_values_t **value_table_ptr;
792 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
793 host->name, data->name);
795 ds = plugin_get_ds (data->type);
798 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
802 if (ds->ds_num != data->values_len)
804 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
805 data->type, ds->ds_num, data->values_len);
809 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
810 oid_list_len = data->values_len + 1;
811 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
812 if (oid_list == NULL)
814 memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
815 for (i = 0; i < data->values_len; i++)
816 memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
818 /* Allocate the `value_table' */
819 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
820 * 2 * data->values_len);
821 if (value_table == NULL)
826 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
827 value_table_ptr = value_table + data->values_len;
829 instance_list = NULL;
830 instance_list_ptr = NULL;
835 csnmp_list_instances_t *il;
837 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
840 ERROR ("snmp plugin: snmp_pdu_create failed.");
845 for (i = 0; i < oid_list_len; i++)
846 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
848 status = snmp_sess_synch_response (host->sess_handle, req, &res);
850 if (status != STAT_SUCCESS)
854 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
855 ERROR ("snmp plugin: snmp_sess_synch_response failed: %s",
856 (errstr == NULL) ? "Unknown problem" : errstr);
857 csnmp_host_close_session (host);
863 assert (res != NULL);
872 /* Check if we left the subtree */
873 if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
874 vb->name, vb->name_length,
875 data->instance.oid.oid_len) != 0)
878 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
879 * add it to the list */
880 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
886 il->subid = vb->name[vb->name_length - 1];
889 /* Get instance name */
890 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
895 instance_len = sizeof (il->instance) - 1;
896 if (instance_len > vb->val_len)
897 instance_len = vb->val_len;
899 strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
901 : vb->val.bitstring),
903 il->instance[instance_len] = '\0';
905 for (ptr = il->instance; *ptr != '\0'; ptr++)
907 if ((*ptr > 0) && (*ptr < 32))
909 else if (*ptr == '/')
912 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
916 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
917 snprintf (il->instance, sizeof (il->instance),
918 "%llu", val.counter);
920 il->instance[sizeof (il->instance) - 1] = '\0';
921 DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
922 data->name, il->instance);
924 if (instance_list_ptr == NULL)
927 instance_list_ptr->next = il;
928 instance_list_ptr = il;
930 /* Copy OID to oid_list[0] */
931 memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
932 oid_list[0].oid_len = vb->name_length;
934 for (i = 0; i < data->values_len; i++)
936 csnmp_table_values_t *vt;
938 vb = vb->next_variable;
945 /* Check if we left the subtree */
946 if (snmp_oid_ncompare (data->values[i].oid,
947 data->values[i].oid_len,
948 vb->name, vb->name_length,
949 data->values[i].oid_len) != 0)
951 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
952 host->name, data->name, i);
956 if ((value_table_ptr[i] != NULL)
957 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
959 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
960 host->name, data->name, i);
964 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
967 vt->subid = vb->name[vb->name_length - 1];
968 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
971 if (value_table_ptr[i] == NULL)
974 value_table_ptr[i]->next = vt;
975 value_table_ptr[i] = vt;
978 /* Copy OID to oid_list[i + 1] */
979 memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
980 oid_list[i + 1].oid_len = vb->name_length;
981 } /* for (i = data->values_len) */
986 } /* while (status == 0) */
989 csnmp_dispatch_table (host, data, instance_list, value_table);
991 /* Free all allocated variables here */
992 while (instance_list != NULL)
994 instance_list_ptr = instance_list->next;
995 sfree (instance_list);
996 instance_list = instance_list_ptr;
999 for (i = 0; i < data->values_len; i++)
1001 csnmp_table_values_t *tmp;
1002 while (value_table[i] != NULL)
1004 tmp = value_table[i]->next;
1005 sfree (value_table[i]);
1006 value_table[i] = tmp;
1010 sfree (value_table);
1014 } /* int csnmp_read_table */
1016 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1018 struct snmp_pdu *req;
1019 struct snmp_pdu *res;
1020 struct variable_list *vb;
1022 const data_set_t *ds;
1023 value_list_t vl = VALUE_LIST_INIT;
1028 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1029 host->name, data->name);
1031 ds = plugin_get_ds (data->type);
1034 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1038 if (ds->ds_num != data->values_len)
1040 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1041 data->type, ds->ds_num, data->values_len);
1045 vl.values_len = ds->ds_num;
1046 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1047 if (vl.values == NULL)
1049 for (i = 0; i < vl.values_len; i++)
1051 if (ds->ds[i].type == DS_TYPE_COUNTER)
1052 vl.values[i].counter = 0;
1054 vl.values[i].gauge = NAN;
1057 strncpy (vl.host, host->name, sizeof (vl.host));
1058 vl.host[sizeof (vl.host) - 1] = '\0';
1059 strcpy (vl.plugin, "snmp");
1060 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1061 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1063 vl.interval = host->skip_num;
1065 req = snmp_pdu_create (SNMP_MSG_GET);
1068 ERROR ("snmp plugin: snmp_pdu_create failed.");
1073 for (i = 0; i < data->values_len; i++)
1074 snmp_add_null_var (req, data->values[i].oid, data->values[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: snmp_sess_synch_response failed: %s",
1083 (errstr == NULL) ? "Unknown problem" : errstr);
1084 csnmp_host_close_session (host);
1090 vl.time = time (NULL);
1092 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1095 snprint_variable (buffer, sizeof (buffer),
1096 vb->name, vb->name_length, vb);
1097 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1099 for (i = 0; i < data->values_len; i++)
1100 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1101 vb->name, vb->name_length) == 0)
1102 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1103 } /* for (res->variables) */
1105 snmp_free_pdu (res);
1107 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1108 plugin_dispatch_values (data->type, &vl);
1112 } /* int csnmp_read_value */
1114 static int csnmp_read_host (host_definition_t *host)
1118 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1120 if (host->sess_handle == NULL)
1121 csnmp_host_open_session (host);
1123 if (host->sess_handle == NULL)
1126 for (i = 0; i < host->data_list_len; i++)
1128 data_definition_t *data = host->data_list[i];
1131 csnmp_read_table (host, data);
1133 csnmp_read_value (host, data);
1137 } /* int csnmp_read_host */
1139 static void *csnmp_read_thread (void *data)
1141 host_definition_t *host;
1143 pthread_mutex_lock (&host_lock);
1144 while (do_shutdown == 0)
1146 pthread_cond_wait (&host_cond, &host_lock);
1148 for (host = host_head; host != NULL; host = host->next)
1150 if (do_shutdown != 0)
1152 if (host->state != STATE_WAIT)
1155 host->state = STATE_BUSY;
1156 pthread_mutex_unlock (&host_lock);
1157 csnmp_read_host (host);
1158 pthread_mutex_lock (&host_lock);
1159 host->state = STATE_IDLE;
1161 } /* while (do_shutdown == 0) */
1162 pthread_mutex_unlock (&host_lock);
1164 pthread_exit ((void *) 0);
1165 return ((void *) 0);
1166 } /* void *csnmp_read_thread */
1168 static int csnmp_init (void)
1170 host_definition_t *host;
1173 if (host_head == NULL)
1176 call_snmp_init_once ();
1179 for (host = host_head; host != NULL; host = host->next)
1182 /* We need to initialize `skip_num' here, because `interval_g' isn't
1183 * initialized during `configure'. */
1184 host->skip_left = interval_g;
1185 if (host->skip_num == 0)
1187 host->skip_num = interval_g;
1189 else if (host->skip_num < interval_g)
1191 host->skip_num = interval_g;
1192 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1193 host->name, host->skip_num);
1196 csnmp_host_open_session (host);
1199 /* Now start the reading threads */
1200 if (threads_num > 3)
1202 threads_num = 3 + ((threads_num - 3) / 10);
1203 if (threads_num > 10)
1207 threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1208 if (threads == NULL)
1210 memset (threads, '\0', threads_num * sizeof (pthread_t));
1212 for (i = 0; i < threads_num; i++)
1213 pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1216 } /* int csnmp_init */
1218 static int csnmp_read (void)
1220 host_definition_t *host;
1223 if (host_head == NULL)
1225 INFO ("snmp plugin: No hosts configured.");
1231 pthread_mutex_lock (&host_lock);
1232 for (host = host_head; host != NULL; host = host->next)
1234 if (host->state != STATE_IDLE)
1237 host->skip_left -= interval_g;
1238 if (host->skip_left >= interval_g)
1241 host->state = STATE_WAIT;
1243 host->skip_left = host->skip_num;
1246 pthread_cond_broadcast (&host_cond);
1247 pthread_mutex_unlock (&host_lock);
1250 } /* int csnmp_read */
1252 static int csnmp_shutdown (void)
1254 host_definition_t *host_this;
1255 host_definition_t *host_next;
1257 data_definition_t *data_this;
1258 data_definition_t *data_next;
1262 pthread_mutex_lock (&host_lock);
1264 pthread_cond_broadcast (&host_cond);
1265 pthread_mutex_unlock (&host_lock);
1267 for (i = 0; i < threads_num; i++)
1268 pthread_join (threads[i], NULL);
1270 /* Now that all the threads have exited, let's free all the global variables.
1271 * This isn't really neccessary, I guess, but I think it's good stile to do
1273 host_this = host_head;
1275 while (host_this != NULL)
1277 host_next = host_this->next;
1279 csnmp_host_close_session (host_this);
1281 sfree (host_this->name);
1282 sfree (host_this->address);
1283 sfree (host_this->community);
1284 sfree (host_this->data_list);
1287 host_this = host_next;
1290 data_this = data_head;
1292 while (data_this != NULL)
1294 data_next = data_this->next;
1296 sfree (data_this->name);
1297 sfree (data_this->type);
1298 sfree (data_this->values);
1301 data_this = data_next;
1305 } /* int csnmp_shutdown */
1307 void module_register (void)
1309 plugin_register_complex_config ("snmp", csnmp_config);
1310 plugin_register_init ("snmp", csnmp_init);
1311 plugin_register_read ("snmp", csnmp_read);
1312 plugin_register_shutdown ("snmp", csnmp_shutdown);
1313 } /* void module_register */
1316 * vim: shiftwidth=2 softtabstop=2 tabstop=8