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;
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);
342 hd->sess.peername = hd->address;
345 } /* int csnmp_config_add_host_address */
347 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
349 if ((ci->values_num != 1)
350 || (ci->values[0].type != OCONFIG_TYPE_STRING))
352 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
356 if (hd->community == NULL)
357 free (hd->community);
359 hd->community = strdup (ci->values[0].value.string);
360 if (hd->community == NULL)
363 DEBUG ("snmp plugin: host = %s; host->community = %s;",
364 hd->name, hd->community);
366 hd->sess.community = (u_char *) hd->community;
367 hd->sess.community_len = strlen (hd->community);
370 } /* int csnmp_config_add_host_community */
372 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
376 if ((ci->values_num != 1)
377 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
379 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
383 version = (int) ci->values[0].value.number;
384 if ((version != 1) && (version != 2))
386 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
390 hd->version = version;
392 if (hd->version == 1)
393 hd->sess.version = SNMP_VERSION_1;
395 hd->sess.version = SNMP_VERSION_2c;
398 } /* int csnmp_config_add_host_address */
400 static int csnmp_config_add_host_collect (host_definition_t *host,
403 data_definition_t *data;
404 data_definition_t **data_list;
408 if (ci->values_num < 1)
410 WARNING ("snmp plugin: `Collect' needs at least one argument.");
414 for (i = 0; i < ci->values_num; i++)
415 if (ci->values[i].type != OCONFIG_TYPE_STRING)
417 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
421 data_list_len = host->data_list_len + ci->values_num;
422 data_list = (data_definition_t **) realloc (host->data_list,
423 sizeof (data_definition_t *) * data_list_len);
424 if (data_list == NULL)
426 host->data_list = data_list;
428 for (i = 0; i < ci->values_num; i++)
430 for (data = data_head; data != NULL; data = data->next)
431 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
436 WARNING ("snmp plugin: No such data configured: `%s'",
437 ci->values[i].value.string);
441 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
442 host->name, host->data_list_len, data->name);
444 host->data_list[host->data_list_len] = data;
445 host->data_list_len++;
446 } /* for (values_num) */
449 } /* int csnmp_config_add_host_collect */
451 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
455 if ((ci->values_num != 1)
456 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
458 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
462 interval = (int) ci->values[0].value.number;
463 hd->skip_num = interval / interval_g;
464 if (hd->skip_num < 1)
467 if ((hd->skip_num * interval_g) != interval)
469 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
470 hd->name, hd->skip_num * interval_g);
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 snmp_sess_init (&hd->sess);
502 hd->sess.version = SNMP_VERSION_2c;
507 for (i = 0; i < ci->children_num; i++)
509 oconfig_item_t *option = ci->children + i;
512 if (strcasecmp ("Address", option->key) == 0)
513 status = csnmp_config_add_host_address (hd, option);
514 else if (strcasecmp ("Community", option->key) == 0)
515 status = csnmp_config_add_host_community (hd, option);
516 else if (strcasecmp ("Version", option->key) == 0)
517 status = csnmp_config_add_host_version (hd, option);
518 else if (strcasecmp ("Collect", option->key) == 0)
519 csnmp_config_add_host_collect (hd, option);
520 else if (strcasecmp ("Interval", option->key) == 0)
521 csnmp_config_add_host_interval (hd, option);
524 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
530 } /* for (ci->children) */
534 if (hd->address == NULL)
536 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
540 if (hd->community == NULL)
542 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
548 } /* while (status == 0) */
557 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
558 hd->name, hd->address, hd->community, hd->version);
560 if (host_head == NULL)
564 host_definition_t *last;
566 while (last->next != NULL)
572 } /* int csnmp_config_add_host */
574 static int csnmp_config (oconfig_item_t *ci)
578 call_snmp_init_once ();
580 for (i = 0; i < ci->children_num; i++)
582 oconfig_item_t *child = ci->children + i;
583 if (strcasecmp ("Data", child->key) == 0)
584 csnmp_config_add_data (child);
585 else if (strcasecmp ("Host", child->key) == 0)
586 csnmp_config_add_host (child);
589 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
591 } /* for (ci->children) */
594 } /* int csnmp_config */
596 static int csnmp_init (void)
598 call_snmp_init_once ();
603 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
606 value_list_t vl = VALUE_LIST_INIT;
608 values[0].gauge = snum;
609 values[1].gauge = mnum;
610 values[2].gauge = lnum;
613 vl.values_len = STATIC_ARRAY_SIZE (values);
614 vl.time = time (NULL);
615 strcpy (vl.host, hostname_g);
616 strcpy (vl.plugin, "load");
618 plugin_dispatch_values ("load", &vl);
622 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
628 if ((vl->type == ASN_INTEGER)
629 || (vl->type == ASN_UINTEGER)
630 || (vl->type == ASN_COUNTER)
631 || (vl->type == ASN_GAUGE))
633 temp = (uint32_t) *vl->val.integer;
634 DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
636 else if (vl->type == ASN_COUNTER64)
638 temp = (uint32_t) vl->val.counter64->high;
640 temp += (uint32_t) vl->val.counter64->low;
641 DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
645 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
649 if (type == DS_TYPE_COUNTER)
653 else if (type == DS_TYPE_GAUGE)
661 } /* value_t csnmp_value_list_to_value */
663 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
664 csnmp_list_instances_t *instance_list,
665 csnmp_table_values_t **value_table)
667 const data_set_t *ds;
668 value_list_t vl = VALUE_LIST_INIT;
670 csnmp_list_instances_t *instance_list_ptr;
671 csnmp_table_values_t **value_table_ptr;
675 ds = plugin_get_ds (data->type);
678 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
681 assert (ds->ds_num == data->values_len);
683 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
685 if (value_table_ptr == NULL)
687 for (i = 0; i < data->values_len; i++)
688 value_table_ptr[i] = value_table[i];
690 vl.values_len = ds->ds_num;
691 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
692 if (vl.values == NULL)
694 sfree (value_table_ptr);
698 strncpy (vl.host, host->name, sizeof (vl.host));
699 vl.host[sizeof (vl.host) - 1] = '\0';
700 strcpy (vl.plugin, "snmp");
702 vl.time = time (NULL);
704 for (instance_list_ptr = instance_list;
705 instance_list_ptr != NULL;
706 instance_list_ptr = instance_list_ptr->next)
708 strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
709 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
711 for (i = 0; i < data->values_len; i++)
713 while ((value_table_ptr[i] != NULL)
714 && (value_table_ptr[i]->subid < instance_list_ptr->subid))
715 value_table_ptr[i] = value_table_ptr[i]->next;
716 if ((value_table_ptr[i] == NULL)
717 || (value_table_ptr[i]->subid != instance_list_ptr->subid))
719 vl.values[i] = value_table_ptr[i]->value;
720 } /* for (data->values_len) */
722 /* If the for-loop was aborted early, not all subid's match. */
723 if (i < data->values_len)
725 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
727 host->name, data->name, i, instance_list_ptr->subid);
731 /* If we get here `vl.type_instance' and all `vl.values' have been set */
732 plugin_dispatch_values (data->type, &vl);
733 } /* for (instance_list) */
736 sfree (value_table_ptr);
739 } /* int csnmp_dispatch_table */
741 static int csnmp_read_table (struct snmp_session *sess_ptr,
742 host_definition_t *host, data_definition_t *data)
744 struct snmp_pdu *req;
745 struct snmp_pdu *res;
746 struct variable_list *vb;
748 const data_set_t *ds;
750 uint32_t oid_list_len;
755 /* `value_table' and `value_table_ptr' implement a linked list for each
756 * value. `instance_list' and `instance_list_ptr' implement a linked list of
757 * instance names. This is used to jump gaps in the table. */
758 csnmp_list_instances_t *instance_list;
759 csnmp_list_instances_t *instance_list_ptr;
760 csnmp_table_values_t **value_table;
761 csnmp_table_values_t **value_table_ptr;
763 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
764 host->name, data->name);
766 ds = plugin_get_ds (data->type);
769 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
773 if (ds->ds_num != data->values_len)
775 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
776 data->type, ds->ds_num, data->values_len);
780 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
781 oid_list_len = data->values_len + 1;
782 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
783 if (oid_list == NULL)
785 memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
786 for (i = 0; i < data->values_len; i++)
787 memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
789 /* Allocate the `value_table' */
790 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
791 * 2 * data->values_len);
792 if (value_table == NULL)
797 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2);
798 value_table_ptr = value_table + data->values_len;
800 instance_list = NULL;
801 instance_list_ptr = NULL;
806 csnmp_list_instances_t *il;
808 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
811 ERROR ("snmp plugin: snmp_pdu_create failed.");
816 for (i = 0; i < oid_list_len; i++)
817 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
819 status = snmp_synch_response (sess_ptr, req, &res);
821 if (status != STAT_SUCCESS)
823 ERROR ("snmp plugin: snmp_synch_response failed.");
828 assert (res != NULL);
837 /* Check if we left the subtree */
838 if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
839 vb->name, vb->name_length,
840 data->instance.oid.oid_len) != 0)
843 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
844 * add it to the list */
845 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
851 il->subid = vb->name[vb->name_length - 1];
854 /* Get instance name */
855 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
857 strncpy (il->instance, (char *) vb->val.bitstring,
858 sizeof (il->instance));
859 il->instance[sizeof (il->instance) - 1] = '\0';
860 DEBUG ("Before escape_slashes: %s", il->instance);
861 escape_slashes (il->instance, strlen (il->instance));
862 DEBUG ("After escape_slashes: %s", il->instance);
866 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
867 snprintf (il->instance, sizeof (il->instance),
868 "%llu", val.counter);
870 il->instance[sizeof (il->instance) - 1] = '\0';
871 DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
872 data->name, il->instance);
874 if (instance_list_ptr == NULL)
877 instance_list_ptr->next = il;
878 instance_list_ptr = il;
880 /* Copy OID to oid_list[0] */
881 memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
882 oid_list[0].oid_len = vb->name_length;
884 for (i = 0; i < data->values_len; i++)
886 csnmp_table_values_t *vt;
888 vb = vb->next_variable;
895 /* Check if we left the subtree */
896 if (snmp_oid_ncompare (data->values[i].oid,
897 data->values[i].oid_len,
898 vb->name, vb->name_length,
899 data->values[i].oid_len) != 0)
901 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
902 host->name, data->name, i);
906 if ((value_table_ptr[i] != NULL)
907 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
909 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
910 host->name, data->name, i);
914 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
917 vt->subid = vb->name[vb->name_length - 1];
918 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
921 if (value_table_ptr[i] == NULL)
924 value_table_ptr[i]->next = vt;
925 value_table_ptr[i] = vt;
928 /* Copy OID to oid_list[i + 1] */
929 memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
930 oid_list[i + 1].oid_len = vb->name_length;
931 } /* for (data->values_len) */
936 } /* while (status == 0) */
939 csnmp_dispatch_table (host, data, instance_list, value_table);
941 /* Free all allocated variables here */
942 while (instance_list != NULL)
944 instance_list_ptr = instance_list->next;
945 sfree (instance_list);
946 instance_list = instance_list_ptr;
949 for (i = 0; i < data->values_len; i++)
951 csnmp_table_values_t *tmp;
952 while (value_table[i] != NULL)
954 tmp = value_table[i]->next;
955 sfree (value_table[i]);
956 value_table[i] = tmp;
964 } /* int csnmp_read_table */
966 static int csnmp_read_value (struct snmp_session *sess_ptr,
967 host_definition_t *host, data_definition_t *data)
969 struct snmp_pdu *req;
970 struct snmp_pdu *res;
971 struct variable_list *vb;
973 const data_set_t *ds;
974 value_list_t vl = VALUE_LIST_INIT;
979 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
980 host->name, data->name);
982 ds = plugin_get_ds (data->type);
985 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
989 if (ds->ds_num != data->values_len)
991 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
992 data->type, ds->ds_num, data->values_len);
996 vl.values_len = ds->ds_num;
997 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
998 if (vl.values == NULL)
1000 for (i = 0; i < vl.values_len; i++)
1002 if (ds->ds[i].type == DS_TYPE_COUNTER)
1003 vl.values[i].counter = 0;
1005 vl.values[i].gauge = NAN;
1008 strncpy (vl.host, host->name, sizeof (vl.host));
1009 vl.host[sizeof (vl.host) - 1] = '\0';
1010 strcpy (vl.plugin, "snmp");
1011 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1012 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1014 vl.interval = interval_g * host->skip_num;
1016 req = snmp_pdu_create (SNMP_MSG_GET);
1019 ERROR ("snmp plugin: snmp_pdu_create failed.");
1024 for (i = 0; i < data->values_len; i++)
1025 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1026 status = snmp_synch_response (sess_ptr, req, &res);
1028 if (status != STAT_SUCCESS)
1030 ERROR ("snmp plugin: snmp_synch_response failed.");
1035 vl.time = time (NULL);
1037 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1040 snprint_variable (buffer, sizeof (buffer),
1041 vb->name, vb->name_length, vb);
1042 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1044 for (i = 0; i < data->values_len; i++)
1045 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1046 vb->name, vb->name_length) == 0)
1047 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1048 } /* for (res->variables) */
1050 snmp_free_pdu (res);
1052 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1053 plugin_dispatch_values (data->type, &vl);
1057 } /* int csnmp_read_value */
1059 static int csnmp_read_host (host_definition_t *host)
1061 struct snmp_session *sess_ptr;
1064 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1066 sess_ptr = snmp_open (&host->sess);
1067 if (sess_ptr == NULL)
1069 snmp_perror ("snmp_open");
1070 ERROR ("snmp plugin: snmp_open failed.");
1074 for (i = 0; i < host->data_list_len; i++)
1076 data_definition_t *data = host->data_list[i];
1079 csnmp_read_table (sess_ptr, host, data);
1081 csnmp_read_value (sess_ptr, host, data);
1084 snmp_close (sess_ptr);
1086 } /* int csnmp_read_host */
1088 static int csnmp_read (void)
1090 host_definition_t *host;
1093 if (host_head == NULL)
1095 INFO ("snmp plugin: No hosts configured.");
1101 for (host = host_head; host != NULL; host = host->next)
1104 if (host->skip_left > 0)
1107 csnmp_read_host (host);
1109 host->skip_left = host->skip_num;
1113 } /* int csnmp_read */
1115 void module_register (void)
1117 plugin_register_complex_config ("snmp", csnmp_config);
1118 plugin_register_init ("snmp", csnmp_init);
1119 plugin_register_read ("snmp", csnmp_read);
1120 } /* void module_register */
1123 * vim: shiftwidth=2 softtabstop=2 tabstop=8