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;
464 if (hd->skip_num < 0)
468 } /* int csnmp_config_add_host_interval */
470 static int csnmp_config_add_host (oconfig_item_t *ci)
472 host_definition_t *hd;
476 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
478 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
482 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
485 memset (hd, '\0', sizeof (host_definition_t));
488 hd->name = strdup (ci->values[0].value.string);
489 if (hd->name == NULL)
495 snmp_sess_init (&hd->sess);
496 hd->sess.version = SNMP_VERSION_2c;
501 for (i = 0; i < ci->children_num; i++)
503 oconfig_item_t *option = ci->children + i;
506 if (strcasecmp ("Address", option->key) == 0)
507 status = csnmp_config_add_host_address (hd, option);
508 else if (strcasecmp ("Community", option->key) == 0)
509 status = csnmp_config_add_host_community (hd, option);
510 else if (strcasecmp ("Version", option->key) == 0)
511 status = csnmp_config_add_host_version (hd, option);
512 else if (strcasecmp ("Collect", option->key) == 0)
513 csnmp_config_add_host_collect (hd, option);
514 else if (strcasecmp ("Interval", option->key) == 0)
515 csnmp_config_add_host_interval (hd, option);
518 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
524 } /* for (ci->children) */
528 if (hd->address == NULL)
530 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
534 if (hd->community == NULL)
536 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
542 } /* while (status == 0) */
551 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
552 hd->name, hd->address, hd->community, hd->version);
554 if (host_head == NULL)
558 host_definition_t *last;
560 while (last->next != NULL)
566 } /* int csnmp_config_add_host */
568 static int csnmp_config (oconfig_item_t *ci)
572 call_snmp_init_once ();
574 for (i = 0; i < ci->children_num; i++)
576 oconfig_item_t *child = ci->children + i;
577 if (strcasecmp ("Data", child->key) == 0)
578 csnmp_config_add_data (child);
579 else if (strcasecmp ("Host", child->key) == 0)
580 csnmp_config_add_host (child);
583 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
585 } /* for (ci->children) */
588 } /* int csnmp_config */
590 static int csnmp_init (void)
592 host_definition_t *host;
594 call_snmp_init_once ();
596 for (host = host_head; host != NULL; host = host->next)
598 host->skip_left = interval_g;
599 if (host->skip_num == 0)
601 host->skip_num = interval_g;
603 else if (host->skip_num < interval_g)
605 host->skip_num = interval_g;
606 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
607 host->name, host->skip_num);
615 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
618 value_list_t vl = VALUE_LIST_INIT;
620 values[0].gauge = snum;
621 values[1].gauge = mnum;
622 values[2].gauge = lnum;
625 vl.values_len = STATIC_ARRAY_SIZE (values);
626 vl.time = time (NULL);
627 strcpy (vl.host, hostname_g);
628 strcpy (vl.plugin, "load");
630 plugin_dispatch_values ("load", &vl);
634 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
640 if ((vl->type == ASN_INTEGER)
641 || (vl->type == ASN_UINTEGER)
642 || (vl->type == ASN_COUNTER)
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->skip_num;
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 (struct snmp_session *sess_ptr,
755 host_definition_t *host, data_definition_t *data)
757 struct snmp_pdu *req;
758 struct snmp_pdu *res;
759 struct variable_list *vb;
761 const data_set_t *ds;
763 uint32_t oid_list_len;
768 /* `value_table' and `value_table_ptr' implement a linked list for each
769 * value. `instance_list' and `instance_list_ptr' implement a linked list of
770 * instance names. This is used to jump gaps in the table. */
771 csnmp_list_instances_t *instance_list;
772 csnmp_list_instances_t *instance_list_ptr;
773 csnmp_table_values_t **value_table;
774 csnmp_table_values_t **value_table_ptr;
776 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
777 host->name, data->name);
779 ds = plugin_get_ds (data->type);
782 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
786 if (ds->ds_num != data->values_len)
788 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
789 data->type, ds->ds_num, data->values_len);
793 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
794 oid_list_len = data->values_len + 1;
795 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
796 if (oid_list == NULL)
798 memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
799 for (i = 0; i < data->values_len; i++)
800 memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
802 /* Allocate the `value_table' */
803 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
804 * 2 * data->values_len);
805 if (value_table == NULL)
810 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
811 value_table_ptr = value_table + data->values_len;
813 instance_list = NULL;
814 instance_list_ptr = NULL;
819 csnmp_list_instances_t *il;
821 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
824 ERROR ("snmp plugin: snmp_pdu_create failed.");
829 for (i = 0; i < oid_list_len; i++)
830 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
832 status = snmp_synch_response (sess_ptr, req, &res);
834 if (status != STAT_SUCCESS)
836 ERROR ("snmp plugin: snmp_synch_response failed.");
841 assert (res != NULL);
850 /* Check if we left the subtree */
851 if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
852 vb->name, vb->name_length,
853 data->instance.oid.oid_len) != 0)
856 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
857 * add it to the list */
858 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
864 il->subid = vb->name[vb->name_length - 1];
867 /* Get instance name */
868 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
873 instance_len = sizeof (il->instance) - 1;
874 if (instance_len > vb->val_len)
875 instance_len = vb->val_len;
877 strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
879 : vb->val.bitstring),
881 il->instance[instance_len] = '\0';
883 for (ptr = il->instance; *ptr != '\0'; ptr++)
885 if ((*ptr > 0) && (*ptr < 32))
887 else if (*ptr == '/')
890 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
894 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
895 snprintf (il->instance, sizeof (il->instance),
896 "%llu", val.counter);
898 il->instance[sizeof (il->instance) - 1] = '\0';
899 DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
900 data->name, il->instance);
902 if (instance_list_ptr == NULL)
905 instance_list_ptr->next = il;
906 instance_list_ptr = il;
908 /* Copy OID to oid_list[0] */
909 memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
910 oid_list[0].oid_len = vb->name_length;
912 for (i = 0; i < data->values_len; i++)
914 csnmp_table_values_t *vt;
916 vb = vb->next_variable;
923 /* Check if we left the subtree */
924 if (snmp_oid_ncompare (data->values[i].oid,
925 data->values[i].oid_len,
926 vb->name, vb->name_length,
927 data->values[i].oid_len) != 0)
929 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
930 host->name, data->name, i);
934 if ((value_table_ptr[i] != NULL)
935 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
937 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
938 host->name, data->name, i);
942 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
945 vt->subid = vb->name[vb->name_length - 1];
946 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
949 if (value_table_ptr[i] == NULL)
952 value_table_ptr[i]->next = vt;
953 value_table_ptr[i] = vt;
956 /* Copy OID to oid_list[i + 1] */
957 memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
958 oid_list[i + 1].oid_len = vb->name_length;
959 } /* for (i = data->values_len) */
964 } /* while (status == 0) */
967 csnmp_dispatch_table (host, data, instance_list, value_table);
969 /* Free all allocated variables here */
970 while (instance_list != NULL)
972 instance_list_ptr = instance_list->next;
973 sfree (instance_list);
974 instance_list = instance_list_ptr;
977 for (i = 0; i < data->values_len; i++)
979 csnmp_table_values_t *tmp;
980 while (value_table[i] != NULL)
982 tmp = value_table[i]->next;
983 sfree (value_table[i]);
984 value_table[i] = tmp;
992 } /* int csnmp_read_table */
994 static int csnmp_read_value (struct snmp_session *sess_ptr,
995 host_definition_t *host, data_definition_t *data)
997 struct snmp_pdu *req;
998 struct snmp_pdu *res;
999 struct variable_list *vb;
1001 const data_set_t *ds;
1002 value_list_t vl = VALUE_LIST_INIT;
1007 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1008 host->name, data->name);
1010 ds = plugin_get_ds (data->type);
1013 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1017 if (ds->ds_num != data->values_len)
1019 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1020 data->type, ds->ds_num, data->values_len);
1024 vl.values_len = ds->ds_num;
1025 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1026 if (vl.values == NULL)
1028 for (i = 0; i < vl.values_len; i++)
1030 if (ds->ds[i].type == DS_TYPE_COUNTER)
1031 vl.values[i].counter = 0;
1033 vl.values[i].gauge = NAN;
1036 strncpy (vl.host, host->name, sizeof (vl.host));
1037 vl.host[sizeof (vl.host) - 1] = '\0';
1038 strcpy (vl.plugin, "snmp");
1039 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1040 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1042 vl.interval = host->skip_num;
1044 req = snmp_pdu_create (SNMP_MSG_GET);
1047 ERROR ("snmp plugin: snmp_pdu_create failed.");
1052 for (i = 0; i < data->values_len; i++)
1053 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1054 status = snmp_synch_response (sess_ptr, req, &res);
1056 if (status != STAT_SUCCESS)
1058 ERROR ("snmp plugin: snmp_synch_response failed.");
1063 vl.time = time (NULL);
1065 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1068 snprint_variable (buffer, sizeof (buffer),
1069 vb->name, vb->name_length, vb);
1070 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1072 for (i = 0; i < data->values_len; i++)
1073 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1074 vb->name, vb->name_length) == 0)
1075 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1076 } /* for (res->variables) */
1078 snmp_free_pdu (res);
1080 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1081 plugin_dispatch_values (data->type, &vl);
1085 } /* int csnmp_read_value */
1087 static int csnmp_read_host (host_definition_t *host)
1089 struct snmp_session *sess_ptr;
1092 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1094 sess_ptr = snmp_open (&host->sess);
1095 if (sess_ptr == NULL)
1097 snmp_perror ("snmp_open");
1098 ERROR ("snmp plugin: snmp_open failed.");
1102 for (i = 0; i < host->data_list_len; i++)
1104 data_definition_t *data = host->data_list[i];
1107 csnmp_read_table (sess_ptr, host, data);
1109 csnmp_read_value (sess_ptr, host, data);
1112 snmp_close (sess_ptr);
1114 } /* int csnmp_read_host */
1116 static int csnmp_read (void)
1118 host_definition_t *host;
1121 if (host_head == NULL)
1123 INFO ("snmp plugin: No hosts configured.");
1129 for (host = host_head; host != NULL; host = host->next)
1131 host->skip_left -= interval_g;
1132 if (host->skip_left >= interval_g)
1135 csnmp_read_host (host);
1137 host->skip_left = host->skip_num;
1141 } /* int csnmp_read */
1143 void module_register (void)
1145 plugin_register_complex_config ("snmp", csnmp_config);
1146 plugin_register_init ("snmp", csnmp_init);
1147 plugin_register_read ("snmp", csnmp_read);
1148 } /* void module_register */
1151 * vim: shiftwidth=2 softtabstop=2 tabstop=8