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
64 struct snmp_session sess;
65 data_definition_t **data_list;
67 struct host_definition_s *next;
69 typedef struct host_definition_s host_definition_t;
71 /* These two types are used to cache values in `csnmp_read_table' to handle
73 struct csnmp_list_instances_s
76 char instance[DATA_MAX_NAME_LEN];
77 struct csnmp_list_instances_s *next;
79 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
81 struct csnmp_table_values_s
85 struct csnmp_table_values_s *next;
87 typedef struct csnmp_table_values_s csnmp_table_values_t;
92 data_definition_t *data_head = NULL;
93 host_definition_t *host_head = NULL;
98 /* First there are many functions which do configuration stuff. It's a big
99 * bloated and messy, I'm afraid. */
102 * Callgraph for the config stuff:
104 * +-> csnmp_config_add_data
105 * ! +-> csnmp_config_add_data_type
106 * ! +-> csnmp_config_add_data_table
107 * ! +-> csnmp_config_add_data_instance
108 * ! +-> csnmp_config_add_data_values
109 * +-> csnmp_config_add_host
110 * +-> csnmp_config_add_host_collect
112 static void call_snmp_init_once (void)
114 static int have_init = 0;
117 init_snmp (PACKAGE_NAME);
119 } /* void call_snmp_init_once */
121 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
123 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
125 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
129 if (dd->type != NULL)
132 dd->type = strdup (ci->values[0].value.string);
133 if (dd->type == NULL)
137 } /* int csnmp_config_add_data_type */
139 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
141 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
143 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
147 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
150 } /* int csnmp_config_add_data_table */
152 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
154 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
156 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
162 /* Instance is an OID */
163 dd->instance.oid.oid_len = MAX_OID_LEN;
165 if (!read_objid (ci->values[0].value.string,
166 dd->instance.oid.oid, &dd->instance.oid.oid_len))
168 ERROR ("snmp plugin: read_objid (%s) failed.",
169 ci->values[0].value.string);
175 /* Instance is a simple string */
176 strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
180 } /* int csnmp_config_add_data_instance */
182 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
186 if (ci->values_num < 1)
188 WARNING ("snmp plugin: `Values' needs at least one argument.");
192 for (i = 0; i < ci->values_num; i++)
193 if (ci->values[i].type != OCONFIG_TYPE_STRING)
195 WARNING ("snmp plugin: `Values' needs only string argument.");
199 if (dd->values != NULL)
201 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
202 if (dd->values == NULL)
204 dd->values_len = ci->values_num;
206 for (i = 0; i < ci->values_num; i++)
208 dd->values[i].oid_len = MAX_OID_LEN;
210 if (NULL == snmp_parse_oid (ci->values[i].value.string,
211 dd->values[i].oid, &dd->values[i].oid_len))
213 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
214 ci->values[i].value.string);
223 } /* int csnmp_config_add_data_instance */
225 static int csnmp_config_add_data (oconfig_item_t *ci)
227 data_definition_t *dd;
231 if ((ci->values_num != 1)
232 || (ci->values[0].type != OCONFIG_TYPE_STRING))
234 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
238 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
241 memset (dd, '\0', sizeof (data_definition_t));
243 dd->name = strdup (ci->values[0].value.string);
244 if (dd->name == NULL)
250 for (i = 0; i < ci->children_num; i++)
252 oconfig_item_t *option = ci->children + i;
255 if (strcasecmp ("Type", option->key) == 0)
256 status = csnmp_config_add_data_type (dd, option);
257 else if (strcasecmp ("Table", option->key) == 0)
258 status = csnmp_config_add_data_table (dd, option);
259 else if (strcasecmp ("Instance", option->key) == 0)
260 status = csnmp_config_add_data_instance (dd, option);
261 else if (strcasecmp ("Values", option->key) == 0)
262 status = csnmp_config_add_data_values (dd, option);
265 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
271 } /* for (ci->children) */
275 if (dd->type == NULL)
277 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
281 if (dd->values == NULL)
283 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
289 } /* while (status == 0) */
299 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
300 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
302 if (data_head == NULL)
306 data_definition_t *last;
308 while (last->next != NULL)
314 } /* int csnmp_config_add_data */
316 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
318 if ((ci->values_num != 1)
319 || (ci->values[0].type != OCONFIG_TYPE_STRING))
321 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
325 if (hd->address == NULL)
328 hd->address = strdup (ci->values[0].value.string);
329 if (hd->address == NULL)
332 DEBUG ("snmp plugin: host = %s; host->address = %s;",
333 hd->name, hd->address);
335 hd->sess.peername = hd->address;
338 } /* int csnmp_config_add_host_address */
340 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
342 if ((ci->values_num != 1)
343 || (ci->values[0].type != OCONFIG_TYPE_STRING))
345 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
349 if (hd->community == NULL)
350 free (hd->community);
352 hd->community = strdup (ci->values[0].value.string);
353 if (hd->community == NULL)
356 DEBUG ("snmp plugin: host = %s; host->community = %s;",
357 hd->name, hd->community);
359 hd->sess.community = (u_char *) hd->community;
360 hd->sess.community_len = strlen (hd->community);
363 } /* int csnmp_config_add_host_community */
365 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
369 if ((ci->values_num != 1)
370 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
372 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
376 version = (int) ci->values[0].value.number;
377 if ((version != 1) && (version != 2))
379 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
383 hd->version = version;
385 if (hd->version == 1)
386 hd->sess.version = SNMP_VERSION_1;
388 hd->sess.version = SNMP_VERSION_2c;
391 } /* int csnmp_config_add_host_address */
393 static int csnmp_config_add_host_collect (host_definition_t *host,
396 data_definition_t *data;
397 data_definition_t **data_list;
401 if (ci->values_num < 1)
403 WARNING ("snmp plugin: `Collect' needs at least one argument.");
407 for (i = 0; i < ci->values_num; i++)
408 if (ci->values[i].type != OCONFIG_TYPE_STRING)
410 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
414 data_list_len = host->data_list_len + ci->values_num;
415 data_list = (data_definition_t **) realloc (host->data_list,
416 sizeof (data_definition_t *) * data_list_len);
417 if (data_list == NULL)
419 host->data_list = data_list;
421 for (i = 0; i < ci->values_num; i++)
423 for (data = data_head; data != NULL; data = data->next)
424 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
429 WARNING ("snmp plugin: No such data configured: `%s'",
430 ci->values[i].value.string);
434 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
435 host->name, host->data_list_len, data->name);
437 host->data_list[host->data_list_len] = data;
438 host->data_list_len++;
439 } /* for (values_num) */
442 } /* int csnmp_config_add_host_collect */
444 static int csnmp_config_add_host (oconfig_item_t *ci)
446 host_definition_t *hd;
450 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
452 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
456 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
459 memset (hd, '\0', sizeof (host_definition_t));
462 hd->name = strdup (ci->values[0].value.string);
463 if (hd->name == NULL)
469 snmp_sess_init (&hd->sess);
470 hd->sess.version = SNMP_VERSION_2c;
472 for (i = 0; i < ci->children_num; i++)
474 oconfig_item_t *option = ci->children + i;
477 if (strcasecmp ("Address", option->key) == 0)
478 status = csnmp_config_add_host_address (hd, option);
479 else if (strcasecmp ("Community", option->key) == 0)
480 status = csnmp_config_add_host_community (hd, option);
481 else if (strcasecmp ("Version", option->key) == 0)
482 status = csnmp_config_add_host_version (hd, option);
483 else if (strcasecmp ("Collect", option->key) == 0)
484 csnmp_config_add_host_collect (hd, option);
487 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
493 } /* for (ci->children) */
497 if (hd->address == NULL)
499 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
503 if (hd->community == NULL)
505 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
511 } /* while (status == 0) */
520 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
521 hd->name, hd->address, hd->community, hd->version);
523 if (host_head == NULL)
527 host_definition_t *last;
529 while (last->next != NULL)
535 } /* int csnmp_config_add_host */
537 static int csnmp_config (oconfig_item_t *ci)
541 call_snmp_init_once ();
543 for (i = 0; i < ci->children_num; i++)
545 oconfig_item_t *child = ci->children + i;
546 if (strcasecmp ("Data", child->key) == 0)
547 csnmp_config_add_data (child);
548 else if (strcasecmp ("Host", child->key) == 0)
549 csnmp_config_add_host (child);
552 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
554 } /* for (ci->children) */
557 } /* int csnmp_config */
559 static int csnmp_init (void)
561 call_snmp_init_once ();
566 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
569 value_list_t vl = VALUE_LIST_INIT;
571 values[0].gauge = snum;
572 values[1].gauge = mnum;
573 values[2].gauge = lnum;
576 vl.values_len = STATIC_ARRAY_SIZE (values);
577 vl.time = time (NULL);
578 strcpy (vl.host, hostname_g);
579 strcpy (vl.plugin, "load");
581 plugin_dispatch_values ("load", &vl);
585 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
591 if ((vl->type == ASN_INTEGER)
592 || (vl->type == ASN_UINTEGER)
593 || (vl->type == ASN_COUNTER)
594 || (vl->type == ASN_GAUGE))
596 temp = (uint32_t) *vl->val.integer;
597 DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
599 else if (vl->type == ASN_COUNTER64)
601 temp = (uint32_t) vl->val.counter64->high;
603 temp += (uint32_t) vl->val.counter64->low;
604 DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
608 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
612 if (type == DS_TYPE_COUNTER)
616 else if (type == DS_TYPE_GAUGE)
624 } /* value_t csnmp_value_list_to_value */
626 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
627 csnmp_list_instances_t *instance_list,
628 csnmp_table_values_t **value_table)
630 const data_set_t *ds;
631 value_list_t vl = VALUE_LIST_INIT;
633 csnmp_list_instances_t *instance_list_ptr;
634 csnmp_table_values_t **value_table_ptr;
638 ds = plugin_get_ds (data->type);
641 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
644 assert (ds->ds_num == data->values_len);
646 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
648 if (value_table_ptr == NULL)
650 for (i = 0; i < data->values_len; i++)
651 value_table_ptr[i] = value_table[i];
653 vl.values_len = ds->ds_num;
654 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
655 if (vl.values == NULL)
657 sfree (value_table_ptr);
661 strncpy (vl.host, host->name, sizeof (vl.host));
662 vl.host[sizeof (vl.host) - 1] = '\0';
663 strcpy (vl.plugin, "snmp");
665 vl.time = time (NULL);
667 for (instance_list_ptr = instance_list;
668 instance_list_ptr != NULL;
669 instance_list_ptr = instance_list_ptr->next)
671 strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
672 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
674 for (i = 0; i < data->values_len; i++)
676 while ((value_table_ptr[i] != NULL)
677 && (value_table_ptr[i]->subid < instance_list_ptr->subid))
678 value_table_ptr[i] = value_table_ptr[i]->next;
679 if ((value_table_ptr[i] == NULL)
680 || (value_table_ptr[i]->subid != instance_list_ptr->subid))
682 vl.values[i] = value_table_ptr[i]->value;
683 } /* for (data->values_len) */
685 /* If the for-loop was aborted early, not all subid's match. */
686 if (i < data->values_len)
688 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
690 host->name, data->name, i, instance_list_ptr->subid);
694 /* If we get here `vl.type_instance' and all `vl.values' have been set */
695 plugin_dispatch_values (data->type, &vl);
696 } /* for (instance_list) */
699 sfree (value_table_ptr);
702 } /* int csnmp_dispatch_table */
704 static int csnmp_read_table (struct snmp_session *sess_ptr,
705 host_definition_t *host, data_definition_t *data)
707 struct snmp_pdu *req;
708 struct snmp_pdu *res;
709 struct variable_list *vb;
711 const data_set_t *ds;
713 uint32_t oid_list_len;
718 /* `value_table' and `value_table_ptr' implement a linked list for each
719 * value. `instance_list' and `instance_list_ptr' implement a linked list of
720 * instance names. This is used to jump gaps in the table. */
721 csnmp_list_instances_t *instance_list;
722 csnmp_list_instances_t *instance_list_ptr;
723 csnmp_table_values_t **value_table;
724 csnmp_table_values_t **value_table_ptr;
726 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
727 host->name, data->name);
729 ds = plugin_get_ds (data->type);
732 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
736 if (ds->ds_num != data->values_len)
738 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
739 data->type, ds->ds_num, data->values_len);
743 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
744 oid_list_len = data->values_len + 1;
745 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
746 if (oid_list == NULL)
748 memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
749 for (i = 0; i < data->values_len; i++)
750 memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
752 /* Allocate the `value_table' */
753 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
754 * 2 * data->values_len);
755 if (value_table == NULL)
760 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2);
761 value_table_ptr = value_table + data->values_len;
763 instance_list = NULL;
764 instance_list_ptr = NULL;
769 csnmp_list_instances_t *il;
771 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
774 ERROR ("snmp plugin: snmp_pdu_create failed.");
779 for (i = 0; i < oid_list_len; i++)
780 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
782 status = snmp_synch_response (sess_ptr, req, &res);
784 if (status != STAT_SUCCESS)
786 ERROR ("snmp plugin: snmp_synch_response failed.");
791 assert (res != NULL);
800 /* Check if we left the subtree */
801 if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
802 vb->name, vb->name_length,
803 data->instance.oid.oid_len) != 0)
806 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
807 * add it to the list */
808 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
814 il->subid = vb->name[vb->name_length - 1];
817 /* Get instance name */
818 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
820 strncpy (il->instance, (char *) vb->val.bitstring,
821 sizeof (il->instance));
822 il->instance[sizeof (il->instance) - 1] = '\0';
823 DEBUG ("Before escape_slashes: %s", il->instance);
824 escape_slashes (il->instance, strlen (il->instance));
825 DEBUG ("After escape_slashes: %s", il->instance);
829 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
830 snprintf (il->instance, sizeof (il->instance),
831 "%llu", val.counter);
833 il->instance[sizeof (il->instance) - 1] = '\0';
834 DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
835 data->name, il->instance);
837 if (instance_list_ptr == NULL)
840 instance_list_ptr->next = il;
841 instance_list_ptr = il;
843 /* Copy OID to oid_list[0] */
844 memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
845 oid_list[0].oid_len = vb->name_length;
847 for (i = 0; i < data->values_len; i++)
849 csnmp_table_values_t *vt;
851 vb = vb->next_variable;
858 /* Check if we left the subtree */
859 if (snmp_oid_ncompare (data->values[i].oid,
860 data->values[i].oid_len,
861 vb->name, vb->name_length,
862 data->values[i].oid_len) != 0)
864 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
865 host->name, data->name, i);
869 if ((value_table_ptr[i] != NULL)
870 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
872 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
873 host->name, data->name, i);
877 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
880 vt->subid = vb->name[vb->name_length - 1];
881 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
884 if (value_table_ptr[i] == NULL)
887 value_table_ptr[i]->next = vt;
888 value_table_ptr[i] = vt;
891 /* Copy OID to oid_list[i + 1] */
892 memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
893 oid_list[i + 1].oid_len = vb->name_length;
894 } /* for (data->values_len) */
899 } /* while (status == 0) */
902 csnmp_dispatch_table (host, data, instance_list, value_table);
904 /* Free all allocated variables here */
905 while (instance_list != NULL)
907 instance_list_ptr = instance_list->next;
908 sfree (instance_list);
909 instance_list = instance_list_ptr;
912 for (i = 0; i < data->values_len; i++)
914 csnmp_table_values_t *tmp;
915 while (value_table[i] != NULL)
917 tmp = value_table[i]->next;
918 sfree (value_table[i]);
919 value_table[i] = tmp;
927 } /* int csnmp_read_table */
929 static int csnmp_read_value (struct snmp_session *sess_ptr,
930 host_definition_t *host, data_definition_t *data)
932 struct snmp_pdu *req;
933 struct snmp_pdu *res;
934 struct variable_list *vb;
936 const data_set_t *ds;
937 value_list_t vl = VALUE_LIST_INIT;
942 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
943 host->name, data->name);
945 ds = plugin_get_ds (data->type);
948 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
952 if (ds->ds_num != data->values_len)
954 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
955 data->type, ds->ds_num, data->values_len);
959 vl.values_len = ds->ds_num;
960 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
961 if (vl.values == NULL)
963 for (i = 0; i < vl.values_len; i++)
965 if (ds->ds[i].type == DS_TYPE_COUNTER)
966 vl.values[i].counter = 0;
968 vl.values[i].gauge = NAN;
971 strncpy (vl.host, host->name, sizeof (vl.host));
972 vl.host[sizeof (vl.host) - 1] = '\0';
973 strcpy (vl.plugin, "snmp");
974 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
975 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
977 req = snmp_pdu_create (SNMP_MSG_GET);
980 ERROR ("snmp plugin: snmp_pdu_create failed.");
985 for (i = 0; i < data->values_len; i++)
986 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
987 status = snmp_synch_response (sess_ptr, req, &res);
989 if (status != STAT_SUCCESS)
991 ERROR ("snmp plugin: snmp_synch_response failed.");
996 vl.time = time (NULL);
998 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1001 snprint_variable (buffer, sizeof (buffer),
1002 vb->name, vb->name_length, vb);
1003 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1005 for (i = 0; i < data->values_len; i++)
1006 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1007 vb->name, vb->name_length) == 0)
1008 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1009 } /* for (res->variables) */
1011 snmp_free_pdu (res);
1013 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1014 plugin_dispatch_values (data->type, &vl);
1018 } /* int csnmp_read_value */
1020 static int csnmp_read_host (host_definition_t *host)
1022 struct snmp_session *sess_ptr;
1025 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1027 sess_ptr = snmp_open (&host->sess);
1028 if (sess_ptr == NULL)
1030 snmp_perror ("snmp_open");
1031 ERROR ("snmp plugin: snmp_open failed.");
1035 for (i = 0; i < host->data_list_len; i++)
1037 data_definition_t *data = host->data_list[i];
1040 csnmp_read_table (sess_ptr, host, data);
1042 csnmp_read_value (sess_ptr, host, data);
1045 snmp_close (sess_ptr);
1047 } /* int csnmp_read_host */
1049 static int csnmp_read (void)
1051 host_definition_t *host;
1053 if (host_head == NULL)
1055 INFO ("snmp plugin: No hosts configured.");
1059 for (host = host_head; host != NULL; host = host->next)
1060 csnmp_read_host (host);
1063 } /* int csnmp_read */
1065 void module_register (void)
1067 plugin_register_complex_config ("snmp", csnmp_config);
1068 plugin_register_init ("snmp", csnmp_init);
1069 plugin_register_read ("snmp", csnmp_read);
1070 } /* void module_register */
1073 * vim: shiftwidth=2 softtabstop=2 tabstop=8