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 `next_update < 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)
459 if ((ci->values_num != 1)
460 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
462 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
466 hd->interval = (int) ci->values[0].value.number;
467 if (hd->interval < 0)
471 } /* int csnmp_config_add_host_interval */
473 static int csnmp_config_add_host (oconfig_item_t *ci)
475 host_definition_t *hd;
479 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
481 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
485 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
488 memset (hd, '\0', sizeof (host_definition_t));
491 hd->name = strdup (ci->values[0].value.string);
492 if (hd->name == NULL)
498 hd->sess_handle = NULL;
501 hd->state = STATE_IDLE;
503 for (i = 0; i < ci->children_num; i++)
505 oconfig_item_t *option = ci->children + i;
508 if (strcasecmp ("Address", option->key) == 0)
509 status = csnmp_config_add_host_address (hd, option);
510 else if (strcasecmp ("Community", option->key) == 0)
511 status = csnmp_config_add_host_community (hd, option);
512 else if (strcasecmp ("Version", option->key) == 0)
513 status = csnmp_config_add_host_version (hd, option);
514 else if (strcasecmp ("Collect", option->key) == 0)
515 csnmp_config_add_host_collect (hd, option);
516 else if (strcasecmp ("Interval", option->key) == 0)
517 csnmp_config_add_host_interval (hd, option);
520 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
526 } /* for (ci->children) */
530 if (hd->address == NULL)
532 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
536 if (hd->community == NULL)
538 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
544 } /* while (status == 0) */
553 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
554 hd->name, hd->address, hd->community, hd->version);
556 if (host_head == NULL)
560 host_definition_t *last;
562 while (last->next != NULL)
568 } /* int csnmp_config_add_host */
570 static int csnmp_config (oconfig_item_t *ci)
574 call_snmp_init_once ();
576 for (i = 0; i < ci->children_num; i++)
578 oconfig_item_t *child = ci->children + i;
579 if (strcasecmp ("Data", child->key) == 0)
580 csnmp_config_add_data (child);
581 else if (strcasecmp ("Host", child->key) == 0)
582 csnmp_config_add_host (child);
585 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
587 } /* for (ci->children) */
590 } /* int csnmp_config */
592 /* End of the config stuff. Now the interesting part begins */
594 static void csnmp_host_close_session (host_definition_t *host)
596 if (host->sess_handle == NULL)
599 snmp_sess_close (host->sess_handle);
600 host->sess_handle = NULL;
601 } /* void csnmp_host_close_session */
603 static void csnmp_host_open_session (host_definition_t *host)
605 struct snmp_session sess;
607 if (host->sess_handle != NULL)
608 csnmp_host_close_session (host);
610 snmp_sess_init (&sess);
611 sess.peername = host->address;
612 sess.community = (u_char *) host->community;
613 sess.community_len = strlen (host->community);
614 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
616 /* snmp_sess_open will copy the `struct snmp_session *'. */
617 host->sess_handle = snmp_sess_open (&sess);
619 if (host->sess_handle == NULL)
623 snmp_error (&sess, NULL, NULL, &errstr);
625 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
626 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
629 } /* void csnmp_host_open_session */
631 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
637 if ((vl->type == ASN_INTEGER)
638 || (vl->type == ASN_UINTEGER)
639 || (vl->type == ASN_COUNTER)
641 || (vl->type == ASN_TIMETICKS)
643 || (vl->type == ASN_GAUGE))
645 temp = (uint32_t) *vl->val.integer;
646 DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
648 else if (vl->type == ASN_COUNTER64)
650 temp = (uint32_t) vl->val.counter64->high;
652 temp += (uint32_t) vl->val.counter64->low;
653 DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
657 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
661 if (type == DS_TYPE_COUNTER)
665 else if (type == DS_TYPE_GAUGE)
673 } /* value_t csnmp_value_list_to_value */
675 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
676 csnmp_list_instances_t *instance_list,
677 csnmp_table_values_t **value_table)
679 const data_set_t *ds;
680 value_list_t vl = VALUE_LIST_INIT;
682 csnmp_list_instances_t *instance_list_ptr;
683 csnmp_table_values_t **value_table_ptr;
687 ds = plugin_get_ds (data->type);
690 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
693 assert (ds->ds_num == data->values_len);
695 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
697 if (value_table_ptr == NULL)
699 for (i = 0; i < data->values_len; i++)
700 value_table_ptr[i] = value_table[i];
702 vl.values_len = ds->ds_num;
703 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
704 if (vl.values == NULL)
706 sfree (value_table_ptr);
710 strncpy (vl.host, host->name, sizeof (vl.host));
711 vl.host[sizeof (vl.host) - 1] = '\0';
712 strcpy (vl.plugin, "snmp");
714 vl.interval = host->interval;
715 vl.time = time (NULL);
717 for (instance_list_ptr = instance_list;
718 instance_list_ptr != NULL;
719 instance_list_ptr = instance_list_ptr->next)
721 strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
722 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
724 for (i = 0; i < data->values_len; i++)
726 while ((value_table_ptr[i] != NULL)
727 && (value_table_ptr[i]->subid < instance_list_ptr->subid))
728 value_table_ptr[i] = value_table_ptr[i]->next;
729 if ((value_table_ptr[i] == NULL)
730 || (value_table_ptr[i]->subid != instance_list_ptr->subid))
732 vl.values[i] = value_table_ptr[i]->value;
733 } /* for (data->values_len) */
735 /* If the for-loop was aborted early, not all subid's match. */
736 if (i < data->values_len)
738 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
740 host->name, data->name, i, instance_list_ptr->subid);
744 /* If we get here `vl.type_instance' and all `vl.values' have been set */
745 plugin_dispatch_values (data->type, &vl);
746 } /* for (instance_list) */
749 sfree (value_table_ptr);
752 } /* int csnmp_dispatch_table */
754 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
756 struct snmp_pdu *req;
757 struct snmp_pdu *res;
758 struct variable_list *vb;
760 const data_set_t *ds;
762 uint32_t oid_list_len;
767 /* `value_table' and `value_table_ptr' implement a linked list for each
768 * value. `instance_list' and `instance_list_ptr' implement a linked list of
769 * instance names. This is used to jump gaps in the table. */
770 csnmp_list_instances_t *instance_list;
771 csnmp_list_instances_t *instance_list_ptr;
772 csnmp_table_values_t **value_table;
773 csnmp_table_values_t **value_table_ptr;
775 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
776 host->name, data->name);
778 if (host->sess_handle == NULL)
780 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
784 ds = plugin_get_ds (data->type);
787 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
791 if (ds->ds_num != data->values_len)
793 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
794 data->type, ds->ds_num, data->values_len);
798 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
799 oid_list_len = data->values_len + 1;
800 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
801 if (oid_list == NULL)
803 memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
804 for (i = 0; i < data->values_len; i++)
805 memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
807 /* Allocate the `value_table' */
808 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
809 * 2 * data->values_len);
810 if (value_table == NULL)
815 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
816 value_table_ptr = value_table + data->values_len;
818 instance_list = NULL;
819 instance_list_ptr = NULL;
824 csnmp_list_instances_t *il;
826 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
829 ERROR ("snmp plugin: snmp_pdu_create failed.");
834 for (i = 0; i < oid_list_len; i++)
835 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
838 status = snmp_sess_synch_response (host->sess_handle, req, &res);
844 if ((status != STAT_SUCCESS) || (res == NULL))
848 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
849 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
850 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
857 csnmp_host_close_session (host);
863 assert (res != NULL);
876 /* Check if we left the subtree */
877 if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
878 vb->name, vb->name_length,
879 data->instance.oid.oid_len) != 0)
888 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
889 * add it to the list */
890 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
900 il->subid = vb->name[vb->name_length - 1];
903 /* Get instance name */
904 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
909 instance_len = sizeof (il->instance) - 1;
910 if (instance_len > vb->val_len)
911 instance_len = vb->val_len;
913 strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
915 : vb->val.bitstring),
917 il->instance[instance_len] = '\0';
919 for (ptr = il->instance; *ptr != '\0'; ptr++)
921 if ((*ptr > 0) && (*ptr < 32))
923 else if (*ptr == '/')
926 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
930 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
931 snprintf (il->instance, sizeof (il->instance),
932 "%llu", val.counter);
934 il->instance[sizeof (il->instance) - 1] = '\0';
935 DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
936 data->name, il->instance);
938 if (instance_list_ptr == NULL)
941 instance_list_ptr->next = il;
942 instance_list_ptr = il;
944 /* Copy OID to oid_list[0] */
945 memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
946 oid_list[0].oid_len = vb->name_length;
948 for (i = 0; i < data->values_len; i++)
950 csnmp_table_values_t *vt;
952 vb = vb->next_variable;
959 /* Check if we left the subtree */
960 if (snmp_oid_ncompare (data->values[i].oid,
961 data->values[i].oid_len,
962 vb->name, vb->name_length,
963 data->values[i].oid_len) != 0)
965 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
966 host->name, data->name, i);
970 if ((value_table_ptr[i] != NULL)
971 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
973 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
974 host->name, data->name, i);
978 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
981 vt->subid = vb->name[vb->name_length - 1];
982 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
985 if (value_table_ptr[i] == NULL)
988 value_table_ptr[i]->next = vt;
989 value_table_ptr[i] = vt;
992 /* Copy OID to oid_list[i + 1] */
993 memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
994 oid_list[i + 1].oid_len = vb->name_length;
995 } /* for (i = data->values_len) */
1000 } /* while (status == 0) */
1003 csnmp_dispatch_table (host, data, instance_list, value_table);
1005 /* Free all allocated variables here */
1006 while (instance_list != NULL)
1008 instance_list_ptr = instance_list->next;
1009 sfree (instance_list);
1010 instance_list = instance_list_ptr;
1013 for (i = 0; i < data->values_len; i++)
1015 csnmp_table_values_t *tmp;
1016 while (value_table[i] != NULL)
1018 tmp = value_table[i]->next;
1019 sfree (value_table[i]);
1020 value_table[i] = tmp;
1024 sfree (value_table);
1028 } /* int csnmp_read_table */
1030 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1032 struct snmp_pdu *req;
1033 struct snmp_pdu *res;
1034 struct variable_list *vb;
1036 const data_set_t *ds;
1037 value_list_t vl = VALUE_LIST_INIT;
1042 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1043 host->name, data->name);
1045 if (host->sess_handle == NULL)
1047 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1051 ds = plugin_get_ds (data->type);
1054 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1058 if (ds->ds_num != data->values_len)
1060 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1061 data->type, ds->ds_num, data->values_len);
1065 vl.values_len = ds->ds_num;
1066 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1067 if (vl.values == NULL)
1069 for (i = 0; i < vl.values_len; i++)
1071 if (ds->ds[i].type == DS_TYPE_COUNTER)
1072 vl.values[i].counter = 0;
1074 vl.values[i].gauge = NAN;
1077 strncpy (vl.host, host->name, sizeof (vl.host));
1078 vl.host[sizeof (vl.host) - 1] = '\0';
1079 strcpy (vl.plugin, "snmp");
1080 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1081 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1083 vl.interval = host->interval;
1085 req = snmp_pdu_create (SNMP_MSG_GET);
1088 ERROR ("snmp plugin: snmp_pdu_create failed.");
1093 for (i = 0; i < data->values_len; i++)
1094 snmp_add_null_var (req, data->values[i].oid, data->values[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 (req);
1112 snmp_free_pdu (res);
1116 csnmp_host_close_session (host);
1121 vl.time = time (NULL);
1123 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1127 snprint_variable (buffer, sizeof (buffer),
1128 vb->name, vb->name_length, vb);
1129 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1130 #endif /* COLLECT_DEBUG */
1132 for (i = 0; i < data->values_len; i++)
1133 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1134 vb->name, vb->name_length) == 0)
1135 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1136 } /* for (res->variables) */
1139 snmp_free_pdu (req);
1143 snmp_free_pdu (res);
1146 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1147 plugin_dispatch_values (data->type, &vl);
1151 } /* int csnmp_read_value */
1153 static int csnmp_read_host (host_definition_t *host)
1159 time_start = time (NULL);
1160 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1161 (unsigned int) time_start);
1163 if (host->sess_handle == NULL)
1164 csnmp_host_open_session (host);
1166 if (host->sess_handle == NULL)
1169 for (i = 0; i < host->data_list_len; i++)
1171 data_definition_t *data = host->data_list[i];
1174 csnmp_read_table (host, data);
1176 csnmp_read_value (host, data);
1179 time_end = time (NULL);
1180 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1181 (unsigned int) time_end);
1182 if ((time_end - time_start) > host->interval)
1184 WARNING ("snmp plugin: Host `%s' should be queried every %i seconds, "
1185 "but reading all values takes %i seconds.",
1186 host->name, host->interval, time_end - time_start);
1190 } /* int csnmp_read_host */
1192 static void *csnmp_read_thread (void *data)
1194 host_definition_t *host;
1196 pthread_mutex_lock (&host_lock);
1197 while (do_shutdown == 0)
1199 pthread_cond_wait (&host_cond, &host_lock);
1201 for (host = host_head; host != NULL; host = host->next)
1203 if (do_shutdown != 0)
1205 if (host->state != STATE_WAIT)
1208 host->state = STATE_BUSY;
1209 pthread_mutex_unlock (&host_lock);
1210 csnmp_read_host (host);
1211 pthread_mutex_lock (&host_lock);
1212 host->state = STATE_IDLE;
1214 } /* while (do_shutdown == 0) */
1215 pthread_mutex_unlock (&host_lock);
1217 pthread_exit ((void *) 0);
1218 return ((void *) 0);
1219 } /* void *csnmp_read_thread */
1221 static int csnmp_init (void)
1223 host_definition_t *host;
1226 if (host_head == NULL)
1228 NOTICE ("snmp plugin: No host has been defined.");
1232 call_snmp_init_once ();
1235 for (host = host_head; host != NULL; host = host->next)
1238 /* We need to initialize `interval' here, because `interval_g' isn't
1239 * initialized during `configure'. */
1240 host->next_update = time (NULL);
1241 if (host->interval == 0)
1243 host->interval = interval_g;
1245 else if (host->interval < interval_g)
1247 host->interval = interval_g;
1248 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1249 host->name, host->interval);
1252 csnmp_host_open_session (host);
1255 /* Now start the reading threads */
1256 if (threads_num > 3)
1258 threads_num = 3 + ((threads_num - 3) / 10);
1259 if (threads_num > 10)
1263 threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1264 if (threads == NULL)
1266 ERROR ("snmp plugin: malloc failed.");
1269 memset (threads, '\0', threads_num * sizeof (pthread_t));
1271 for (i = 0; i < threads_num; i++)
1272 pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1275 } /* int csnmp_init */
1277 static int csnmp_read (void)
1279 host_definition_t *host;
1282 if (host_head == NULL)
1284 INFO ("snmp plugin: No hosts configured.");
1290 pthread_mutex_lock (&host_lock);
1291 for (host = host_head; host != NULL; host = host->next)
1293 if (host->state != STATE_IDLE)
1296 /* Skip this host if the next or a later iteration will be sufficient. */
1297 if (host->next_update >= (now + interval_g))
1300 host->state = STATE_WAIT;
1301 host->next_update = now + host->interval;
1304 pthread_cond_broadcast (&host_cond);
1305 pthread_mutex_unlock (&host_lock);
1308 } /* int csnmp_read */
1310 static int csnmp_shutdown (void)
1312 host_definition_t *host_this;
1313 host_definition_t *host_next;
1315 data_definition_t *data_this;
1316 data_definition_t *data_next;
1320 pthread_mutex_lock (&host_lock);
1322 pthread_cond_broadcast (&host_cond);
1323 pthread_mutex_unlock (&host_lock);
1325 for (i = 0; i < threads_num; i++)
1326 pthread_join (threads[i], NULL);
1328 /* Now that all the threads have exited, let's free all the global variables.
1329 * This isn't really neccessary, I guess, but I think it's good stile to do
1331 host_this = host_head;
1333 while (host_this != NULL)
1335 host_next = host_this->next;
1337 csnmp_host_close_session (host_this);
1339 sfree (host_this->name);
1340 sfree (host_this->address);
1341 sfree (host_this->community);
1342 sfree (host_this->data_list);
1345 host_this = host_next;
1348 data_this = data_head;
1350 while (data_this != NULL)
1352 data_next = data_this->next;
1354 sfree (data_this->name);
1355 sfree (data_this->type);
1356 sfree (data_this->values);
1359 data_this = data_next;
1363 } /* int csnmp_shutdown */
1365 void module_register (void)
1367 plugin_register_complex_config ("snmp", csnmp_config);
1368 plugin_register_init ("snmp", csnmp_init);
1369 plugin_register_read ("snmp", csnmp_read);
1370 plugin_register_shutdown ("snmp", csnmp_shutdown);
1371 } /* void module_register */
1374 * vim: shiftwidth=2 softtabstop=2 tabstop=8