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>
26 #include <net-snmp/net-snmp-config.h>
27 #include <net-snmp/net-snmp-includes.h>
30 * Private data structes
37 typedef struct oid_s oid_t;
41 char string[DATA_MAX_NAME_LEN];
44 typedef union instance_u instance_t;
46 struct data_definition_s
48 char *name; /* used to reference this from the `Collect' option */
49 char *type; /* used to find the data_set */
54 struct data_definition_s *next;
56 typedef struct data_definition_s data_definition_t;
58 struct host_definition_s
67 data_definition_t **data_list;
69 struct host_definition_s *next;
71 typedef struct host_definition_s host_definition_t;
73 /* These two types are used to cache values in `csnmp_read_table' to handle
75 struct csnmp_list_instances_s
78 char instance[DATA_MAX_NAME_LEN];
79 struct csnmp_list_instances_s *next;
81 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
83 struct csnmp_table_values_s
87 struct csnmp_table_values_s *next;
89 typedef struct csnmp_table_values_s csnmp_table_values_t;
94 static data_definition_t *data_head = NULL;
95 static host_definition_t *host_head = NULL;
100 /* First there are many functions which do configuration stuff. It's a big
101 * bloated and messy, I'm afraid. */
104 * Callgraph for the config stuff:
106 * +-> call_snmp_init_once
107 * +-> csnmp_config_add_data
108 * ! +-> csnmp_config_add_data_type
109 * ! +-> csnmp_config_add_data_table
110 * ! +-> csnmp_config_add_data_instance
111 * ! +-> csnmp_config_add_data_values
112 * +-> csnmp_config_add_host
113 * +-> csnmp_config_add_host_address
114 * +-> csnmp_config_add_host_community
115 * +-> csnmp_config_add_host_version
116 * +-> csnmp_config_add_host_collect
117 * +-> csnmp_config_add_host_interval
119 static void call_snmp_init_once (void)
121 static int have_init = 0;
124 init_snmp (PACKAGE_NAME);
126 } /* void call_snmp_init_once */
128 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
130 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
132 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
136 if (dd->type != NULL)
139 dd->type = strdup (ci->values[0].value.string);
140 if (dd->type == NULL)
144 } /* int csnmp_config_add_data_type */
146 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
148 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
150 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
154 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
157 } /* int csnmp_config_add_data_table */
159 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
161 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
163 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
169 /* Instance is an OID */
170 dd->instance.oid.oid_len = MAX_OID_LEN;
172 if (!read_objid (ci->values[0].value.string,
173 dd->instance.oid.oid, &dd->instance.oid.oid_len))
175 ERROR ("snmp plugin: read_objid (%s) failed.",
176 ci->values[0].value.string);
182 /* Instance is a simple string */
183 strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
187 } /* int csnmp_config_add_data_instance */
189 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
193 if (ci->values_num < 1)
195 WARNING ("snmp plugin: `Values' needs at least one argument.");
199 for (i = 0; i < ci->values_num; i++)
200 if (ci->values[i].type != OCONFIG_TYPE_STRING)
202 WARNING ("snmp plugin: `Values' needs only string argument.");
206 if (dd->values != NULL)
208 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
209 if (dd->values == NULL)
211 dd->values_len = ci->values_num;
213 for (i = 0; i < ci->values_num; i++)
215 dd->values[i].oid_len = MAX_OID_LEN;
217 if (NULL == snmp_parse_oid (ci->values[i].value.string,
218 dd->values[i].oid, &dd->values[i].oid_len))
220 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
221 ci->values[i].value.string);
230 } /* int csnmp_config_add_data_instance */
232 static int csnmp_config_add_data (oconfig_item_t *ci)
234 data_definition_t *dd;
238 if ((ci->values_num != 1)
239 || (ci->values[0].type != OCONFIG_TYPE_STRING))
241 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
245 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
248 memset (dd, '\0', sizeof (data_definition_t));
250 dd->name = strdup (ci->values[0].value.string);
251 if (dd->name == NULL)
257 for (i = 0; i < ci->children_num; i++)
259 oconfig_item_t *option = ci->children + i;
262 if (strcasecmp ("Type", option->key) == 0)
263 status = csnmp_config_add_data_type (dd, option);
264 else if (strcasecmp ("Table", option->key) == 0)
265 status = csnmp_config_add_data_table (dd, option);
266 else if (strcasecmp ("Instance", option->key) == 0)
267 status = csnmp_config_add_data_instance (dd, option);
268 else if (strcasecmp ("Values", option->key) == 0)
269 status = csnmp_config_add_data_values (dd, option);
272 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
278 } /* for (ci->children) */
282 if (dd->type == NULL)
284 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
288 if (dd->values == NULL)
290 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
296 } /* while (status == 0) */
306 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
307 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
309 if (data_head == NULL)
313 data_definition_t *last;
315 while (last->next != NULL)
321 } /* int csnmp_config_add_data */
323 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
325 if ((ci->values_num != 1)
326 || (ci->values[0].type != OCONFIG_TYPE_STRING))
328 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
332 if (hd->address == NULL)
335 hd->address = strdup (ci->values[0].value.string);
336 if (hd->address == NULL)
339 DEBUG ("snmp plugin: host = %s; host->address = %s;",
340 hd->name, hd->address);
343 } /* int csnmp_config_add_host_address */
345 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
347 if ((ci->values_num != 1)
348 || (ci->values[0].type != OCONFIG_TYPE_STRING))
350 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
354 if (hd->community == NULL)
355 free (hd->community);
357 hd->community = strdup (ci->values[0].value.string);
358 if (hd->community == NULL)
361 DEBUG ("snmp plugin: host = %s; host->community = %s;",
362 hd->name, hd->community);
365 } /* int csnmp_config_add_host_community */
367 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
371 if ((ci->values_num != 1)
372 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
374 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
378 version = (int) ci->values[0].value.number;
379 if ((version != 1) && (version != 2))
381 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
385 hd->version = version;
388 } /* int csnmp_config_add_host_address */
390 static int csnmp_config_add_host_collect (host_definition_t *host,
393 data_definition_t *data;
394 data_definition_t **data_list;
398 if (ci->values_num < 1)
400 WARNING ("snmp plugin: `Collect' needs at least one argument.");
404 for (i = 0; i < ci->values_num; i++)
405 if (ci->values[i].type != OCONFIG_TYPE_STRING)
407 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
411 data_list_len = host->data_list_len + ci->values_num;
412 data_list = (data_definition_t **) realloc (host->data_list,
413 sizeof (data_definition_t *) * data_list_len);
414 if (data_list == NULL)
416 host->data_list = data_list;
418 for (i = 0; i < ci->values_num; i++)
420 for (data = data_head; data != NULL; data = data->next)
421 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
426 WARNING ("snmp plugin: No such data configured: `%s'",
427 ci->values[i].value.string);
431 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
432 host->name, host->data_list_len, data->name);
434 host->data_list[host->data_list_len] = data;
435 host->data_list_len++;
436 } /* for (values_num) */
439 } /* int csnmp_config_add_host_collect */
441 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
445 if ((ci->values_num != 1)
446 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
448 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
452 interval = (int) ci->values[0].value.number;
453 hd->skip_num = interval;
454 if (hd->skip_num < 0)
458 } /* int csnmp_config_add_host_interval */
460 static int csnmp_config_add_host (oconfig_item_t *ci)
462 host_definition_t *hd;
466 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
468 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
472 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
475 memset (hd, '\0', sizeof (host_definition_t));
478 hd->name = strdup (ci->values[0].value.string);
479 if (hd->name == NULL)
485 hd->sess_handle = NULL;
489 for (i = 0; i < ci->children_num; i++)
491 oconfig_item_t *option = ci->children + i;
494 if (strcasecmp ("Address", option->key) == 0)
495 status = csnmp_config_add_host_address (hd, option);
496 else if (strcasecmp ("Community", option->key) == 0)
497 status = csnmp_config_add_host_community (hd, option);
498 else if (strcasecmp ("Version", option->key) == 0)
499 status = csnmp_config_add_host_version (hd, option);
500 else if (strcasecmp ("Collect", option->key) == 0)
501 csnmp_config_add_host_collect (hd, option);
502 else if (strcasecmp ("Interval", option->key) == 0)
503 csnmp_config_add_host_interval (hd, option);
506 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
512 } /* for (ci->children) */
516 if (hd->address == NULL)
518 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
522 if (hd->community == NULL)
524 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
530 } /* while (status == 0) */
539 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
540 hd->name, hd->address, hd->community, hd->version);
542 if (host_head == NULL)
546 host_definition_t *last;
548 while (last->next != NULL)
554 } /* int csnmp_config_add_host */
556 static int csnmp_config (oconfig_item_t *ci)
560 call_snmp_init_once ();
562 for (i = 0; i < ci->children_num; i++)
564 oconfig_item_t *child = ci->children + i;
565 if (strcasecmp ("Data", child->key) == 0)
566 csnmp_config_add_data (child);
567 else if (strcasecmp ("Host", child->key) == 0)
568 csnmp_config_add_host (child);
571 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
573 } /* for (ci->children) */
576 } /* int csnmp_config */
578 /* End of the config stuff. Now the interesting part begins */
580 static void csnmp_host_close_session (host_definition_t *host)
584 if (host->sess_handle == NULL)
587 status = snmp_sess_close (host->sess_handle);
593 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
595 ERROR ("snmp plugin: snmp_sess_close failed: %s",
596 (errstr == NULL) ? "Unknown problem" : errstr);
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: snmp_sess_open failed: %s",
626 (errstr == NULL) ? "Unknown problem" : errstr);
629 } /* void csnmp_host_open_session */
631 static int csnmp_init (void)
633 host_definition_t *host;
635 call_snmp_init_once ();
637 for (host = host_head; host != NULL; host = host->next)
639 /* We need to initialize `skip_num' here, because `interval_g' isn't
640 * initialized during `configure'. */
641 host->skip_left = interval_g;
642 if (host->skip_num == 0)
644 host->skip_num = interval_g;
646 else if (host->skip_num < interval_g)
648 host->skip_num = interval_g;
649 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
650 host->name, host->skip_num);
653 csnmp_host_open_session (host);
657 } /* int csnmp_init */
659 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
665 if ((vl->type == ASN_INTEGER)
666 || (vl->type == ASN_UINTEGER)
667 || (vl->type == ASN_COUNTER)
668 || (vl->type == ASN_GAUGE))
670 temp = (uint32_t) *vl->val.integer;
671 DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
673 else if (vl->type == ASN_COUNTER64)
675 temp = (uint32_t) vl->val.counter64->high;
677 temp += (uint32_t) vl->val.counter64->low;
678 DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
682 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
686 if (type == DS_TYPE_COUNTER)
690 else if (type == DS_TYPE_GAUGE)
698 } /* value_t csnmp_value_list_to_value */
700 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
701 csnmp_list_instances_t *instance_list,
702 csnmp_table_values_t **value_table)
704 const data_set_t *ds;
705 value_list_t vl = VALUE_LIST_INIT;
707 csnmp_list_instances_t *instance_list_ptr;
708 csnmp_table_values_t **value_table_ptr;
712 ds = plugin_get_ds (data->type);
715 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
718 assert (ds->ds_num == data->values_len);
720 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
722 if (value_table_ptr == NULL)
724 for (i = 0; i < data->values_len; i++)
725 value_table_ptr[i] = value_table[i];
727 vl.values_len = ds->ds_num;
728 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
729 if (vl.values == NULL)
731 sfree (value_table_ptr);
735 strncpy (vl.host, host->name, sizeof (vl.host));
736 vl.host[sizeof (vl.host) - 1] = '\0';
737 strcpy (vl.plugin, "snmp");
739 vl.interval = host->skip_num;
740 vl.time = time (NULL);
742 for (instance_list_ptr = instance_list;
743 instance_list_ptr != NULL;
744 instance_list_ptr = instance_list_ptr->next)
746 strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
747 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
749 for (i = 0; i < data->values_len; i++)
751 while ((value_table_ptr[i] != NULL)
752 && (value_table_ptr[i]->subid < instance_list_ptr->subid))
753 value_table_ptr[i] = value_table_ptr[i]->next;
754 if ((value_table_ptr[i] == NULL)
755 || (value_table_ptr[i]->subid != instance_list_ptr->subid))
757 vl.values[i] = value_table_ptr[i]->value;
758 } /* for (data->values_len) */
760 /* If the for-loop was aborted early, not all subid's match. */
761 if (i < data->values_len)
763 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
765 host->name, data->name, i, instance_list_ptr->subid);
769 /* If we get here `vl.type_instance' and all `vl.values' have been set */
770 plugin_dispatch_values (data->type, &vl);
771 } /* for (instance_list) */
774 sfree (value_table_ptr);
777 } /* int csnmp_dispatch_table */
779 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
781 struct snmp_pdu *req;
782 struct snmp_pdu *res;
783 struct variable_list *vb;
785 const data_set_t *ds;
787 uint32_t oid_list_len;
792 /* `value_table' and `value_table_ptr' implement a linked list for each
793 * value. `instance_list' and `instance_list_ptr' implement a linked list of
794 * instance names. This is used to jump gaps in the table. */
795 csnmp_list_instances_t *instance_list;
796 csnmp_list_instances_t *instance_list_ptr;
797 csnmp_table_values_t **value_table;
798 csnmp_table_values_t **value_table_ptr;
800 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
801 host->name, data->name);
803 ds = plugin_get_ds (data->type);
806 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
810 if (ds->ds_num != data->values_len)
812 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
813 data->type, ds->ds_num, data->values_len);
817 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
818 oid_list_len = data->values_len + 1;
819 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
820 if (oid_list == NULL)
822 memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
823 for (i = 0; i < data->values_len; i++)
824 memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
826 /* Allocate the `value_table' */
827 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
828 * 2 * data->values_len);
829 if (value_table == NULL)
834 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
835 value_table_ptr = value_table + data->values_len;
837 instance_list = NULL;
838 instance_list_ptr = NULL;
843 csnmp_list_instances_t *il;
845 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
848 ERROR ("snmp plugin: snmp_pdu_create failed.");
853 for (i = 0; i < oid_list_len; i++)
854 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
856 status = snmp_sess_synch_response (host->sess_handle, req, &res);
858 if (status != STAT_SUCCESS)
862 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
863 ERROR ("snmp plugin: snmp_sess_synch_response failed: %s",
864 (errstr == NULL) ? "Unknown problem" : errstr);
865 csnmp_host_close_session (host);
871 assert (res != NULL);
880 /* Check if we left the subtree */
881 if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
882 vb->name, vb->name_length,
883 data->instance.oid.oid_len) != 0)
886 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
887 * add it to the list */
888 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
894 il->subid = vb->name[vb->name_length - 1];
897 /* Get instance name */
898 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
903 instance_len = sizeof (il->instance) - 1;
904 if (instance_len > vb->val_len)
905 instance_len = vb->val_len;
907 strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
909 : vb->val.bitstring),
911 il->instance[instance_len] = '\0';
913 for (ptr = il->instance; *ptr != '\0'; ptr++)
915 if ((*ptr > 0) && (*ptr < 32))
917 else if (*ptr == '/')
920 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
924 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
925 snprintf (il->instance, sizeof (il->instance),
926 "%llu", val.counter);
928 il->instance[sizeof (il->instance) - 1] = '\0';
929 DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
930 data->name, il->instance);
932 if (instance_list_ptr == NULL)
935 instance_list_ptr->next = il;
936 instance_list_ptr = il;
938 /* Copy OID to oid_list[0] */
939 memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
940 oid_list[0].oid_len = vb->name_length;
942 for (i = 0; i < data->values_len; i++)
944 csnmp_table_values_t *vt;
946 vb = vb->next_variable;
953 /* Check if we left the subtree */
954 if (snmp_oid_ncompare (data->values[i].oid,
955 data->values[i].oid_len,
956 vb->name, vb->name_length,
957 data->values[i].oid_len) != 0)
959 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
960 host->name, data->name, i);
964 if ((value_table_ptr[i] != NULL)
965 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
967 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
968 host->name, data->name, i);
972 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
975 vt->subid = vb->name[vb->name_length - 1];
976 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
979 if (value_table_ptr[i] == NULL)
982 value_table_ptr[i]->next = vt;
983 value_table_ptr[i] = vt;
986 /* Copy OID to oid_list[i + 1] */
987 memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
988 oid_list[i + 1].oid_len = vb->name_length;
989 } /* for (i = data->values_len) */
994 } /* while (status == 0) */
997 csnmp_dispatch_table (host, data, instance_list, value_table);
999 /* Free all allocated variables here */
1000 while (instance_list != NULL)
1002 instance_list_ptr = instance_list->next;
1003 sfree (instance_list);
1004 instance_list = instance_list_ptr;
1007 for (i = 0; i < data->values_len; i++)
1009 csnmp_table_values_t *tmp;
1010 while (value_table[i] != NULL)
1012 tmp = value_table[i]->next;
1013 sfree (value_table[i]);
1014 value_table[i] = tmp;
1018 sfree (value_table);
1022 } /* int csnmp_read_table */
1024 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1026 struct snmp_pdu *req;
1027 struct snmp_pdu *res;
1028 struct variable_list *vb;
1030 const data_set_t *ds;
1031 value_list_t vl = VALUE_LIST_INIT;
1036 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1037 host->name, data->name);
1039 ds = plugin_get_ds (data->type);
1042 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1046 if (ds->ds_num != data->values_len)
1048 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1049 data->type, ds->ds_num, data->values_len);
1053 vl.values_len = ds->ds_num;
1054 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1055 if (vl.values == NULL)
1057 for (i = 0; i < vl.values_len; i++)
1059 if (ds->ds[i].type == DS_TYPE_COUNTER)
1060 vl.values[i].counter = 0;
1062 vl.values[i].gauge = NAN;
1065 strncpy (vl.host, host->name, sizeof (vl.host));
1066 vl.host[sizeof (vl.host) - 1] = '\0';
1067 strcpy (vl.plugin, "snmp");
1068 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1069 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1071 vl.interval = host->skip_num;
1073 req = snmp_pdu_create (SNMP_MSG_GET);
1076 ERROR ("snmp plugin: snmp_pdu_create failed.");
1081 for (i = 0; i < data->values_len; i++)
1082 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1083 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1085 if (status != STAT_SUCCESS)
1087 char *errstr = NULL;
1089 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1090 ERROR ("snmp plugin: snmp_sess_synch_response failed: %s",
1091 (errstr == NULL) ? "Unknown problem" : errstr);
1092 csnmp_host_close_session (host);
1098 vl.time = time (NULL);
1100 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1103 snprint_variable (buffer, sizeof (buffer),
1104 vb->name, vb->name_length, vb);
1105 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1107 for (i = 0; i < data->values_len; i++)
1108 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1109 vb->name, vb->name_length) == 0)
1110 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1111 } /* for (res->variables) */
1113 snmp_free_pdu (res);
1115 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1116 plugin_dispatch_values (data->type, &vl);
1120 } /* int csnmp_read_value */
1122 static int csnmp_read_host (host_definition_t *host)
1126 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1128 if (host->sess_handle == NULL)
1129 csnmp_host_open_session (host);
1131 if (host->sess_handle == NULL)
1134 for (i = 0; i < host->data_list_len; i++)
1136 data_definition_t *data = host->data_list[i];
1139 csnmp_read_table (host, data);
1141 csnmp_read_value (host, data);
1145 } /* int csnmp_read_host */
1147 static int csnmp_read (void)
1149 host_definition_t *host;
1152 if (host_head == NULL)
1154 INFO ("snmp plugin: No hosts configured.");
1160 for (host = host_head; host != NULL; host = host->next)
1162 host->skip_left -= interval_g;
1163 if (host->skip_left >= interval_g)
1166 csnmp_read_host (host);
1168 host->skip_left = host->skip_num;
1172 } /* int csnmp_read */
1174 void module_register (void)
1176 plugin_register_complex_config ("snmp", csnmp_config);
1177 plugin_register_init ("snmp", csnmp_init);
1178 plugin_register_read ("snmp", csnmp_read);
1179 } /* void module_register */
1182 * vim: shiftwidth=2 softtabstop=2 tabstop=8