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;
74 data_definition_t *data_head = NULL;
75 host_definition_t *host_head = NULL;
80 /* First there are many functions which do configuration stuff. It's a big
81 * bloated and messy, I'm afraid. */
84 * Callgraph for the config stuff:
86 * +-> csnmp_config_add_data
87 * ! +-> csnmp_config_add_data_type
88 * ! +-> csnmp_config_add_data_table
89 * ! +-> csnmp_config_add_data_instance
90 * ! +-> csnmp_config_add_data_values
91 * +-> csnmp_config_add_host
92 * +-> csnmp_config_add_collect
94 static void call_snmp_init_once (void)
96 static int have_init = 0;
99 init_snmp (PACKAGE_NAME);
101 } /* void call_snmp_init_once */
103 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
105 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
107 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
111 if (dd->type != NULL)
114 dd->type = strdup (ci->values[0].value.string);
115 if (dd->type == NULL)
119 } /* int csnmp_config_add_data_type */
121 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
123 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
125 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
129 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
132 } /* int csnmp_config_add_data_table */
134 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
136 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
138 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
144 /* Instance is an OID */
145 dd->instance.oid.oid_len = MAX_OID_LEN;
147 if (!read_objid (ci->values[0].value.string,
148 dd->instance.oid.oid, &dd->instance.oid.oid_len))
150 ERROR ("snmp plugin: read_objid (%s) failed.",
151 ci->values[0].value.string);
157 /* Instance is a simple string */
158 strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
162 } /* int csnmp_config_add_data_instance */
164 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
168 if (ci->values_num < 1)
170 WARNING ("snmp plugin: `Values' needs at least one argument.");
174 for (i = 0; i < ci->values_num; i++)
175 if (ci->values[i].type != OCONFIG_TYPE_STRING)
177 WARNING ("snmp plugin: `Values' needs only string argument.");
181 if (dd->values != NULL)
183 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
184 if (dd->values == NULL)
186 dd->values_len = ci->values_num;
188 for (i = 0; i < ci->values_num; i++)
190 dd->values[i].oid_len = MAX_OID_LEN;
192 if (NULL == snmp_parse_oid (ci->values[i].value.string,
193 dd->values[i].oid, &dd->values[i].oid_len))
195 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
196 ci->values[i].value.string);
205 } /* int csnmp_config_add_data_instance */
207 static int csnmp_config_add_data (oconfig_item_t *ci)
209 data_definition_t *dd;
213 if ((ci->values_num != 1)
214 || (ci->values[0].type != OCONFIG_TYPE_STRING))
216 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
220 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
223 memset (dd, '\0', sizeof (data_definition_t));
225 dd->name = strdup (ci->values[0].value.string);
226 if (dd->name == NULL)
232 for (i = 0; i < ci->children_num; i++)
234 oconfig_item_t *option = ci->children + i;
237 if (strcasecmp ("Type", option->key) == 0)
238 status = csnmp_config_add_data_type (dd, option);
239 else if (strcasecmp ("Table", option->key) == 0)
240 status = csnmp_config_add_data_table (dd, option);
241 else if (strcasecmp ("Instance", option->key) == 0)
242 status = csnmp_config_add_data_instance (dd, option);
243 else if (strcasecmp ("Values", option->key) == 0)
244 status = csnmp_config_add_data_values (dd, option);
247 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
253 } /* for (ci->children) */
257 if (dd->type == NULL)
259 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
263 if (dd->values == NULL)
265 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
271 } /* while (status == 0) */
281 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
282 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
284 if (data_head == NULL)
288 data_definition_t *last;
290 while (last->next != NULL)
296 } /* int csnmp_config_add_data */
298 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
300 if ((ci->values_num != 1)
301 || (ci->values[0].type != OCONFIG_TYPE_STRING))
303 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
307 if (hd->address == NULL)
310 hd->address = strdup (ci->values[0].value.string);
311 if (hd->address == NULL)
314 DEBUG ("snmp plugin: host = %s; host->address = %s;",
315 hd->name, hd->address);
317 hd->sess.peername = hd->address;
320 } /* int csnmp_config_add_host_address */
322 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
324 if ((ci->values_num != 1)
325 || (ci->values[0].type != OCONFIG_TYPE_STRING))
327 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
331 if (hd->community == NULL)
332 free (hd->community);
334 hd->community = strdup (ci->values[0].value.string);
335 if (hd->community == NULL)
338 DEBUG ("snmp plugin: host = %s; host->community = %s;",
339 hd->name, hd->community);
341 hd->sess.community = (u_char *) hd->community;
342 hd->sess.community_len = strlen (hd->community);
345 } /* int csnmp_config_add_host_community */
347 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
351 if ((ci->values_num != 1)
352 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
354 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
358 version = (int) ci->values[0].value.number;
359 if ((version != 1) && (version != 2))
361 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
365 hd->version = version;
367 if (hd->version == 1)
368 hd->sess.version = SNMP_VERSION_1;
370 hd->sess.version = SNMP_VERSION_2c;
373 } /* int csnmp_config_add_host_address */
375 static int csnmp_config_add_host (oconfig_item_t *ci)
377 host_definition_t *hd;
381 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
383 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
387 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
390 memset (hd, '\0', sizeof (host_definition_t));
393 hd->name = strdup (ci->values[0].value.string);
394 if (hd->name == NULL)
400 snmp_sess_init (&hd->sess);
401 hd->sess.version = SNMP_VERSION_2c;
403 for (i = 0; i < ci->children_num; i++)
405 oconfig_item_t *option = ci->children + i;
408 if (strcasecmp ("Address", option->key) == 0)
409 status = csnmp_config_add_host_address (hd, option);
410 else if (strcasecmp ("Community", option->key) == 0)
411 status = csnmp_config_add_host_community (hd, option);
412 else if (strcasecmp ("Version", option->key) == 0)
413 status = csnmp_config_add_host_version (hd, option);
416 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
422 } /* for (ci->children) */
426 if (hd->address == NULL)
428 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
432 if (hd->community == NULL)
434 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
440 } /* while (status == 0) */
449 /* TODO: Check all fields in `hd'. */
450 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
451 hd->name, hd->address, hd->community, hd->version);
453 if (host_head == NULL)
457 host_definition_t *last;
459 while (last->next != NULL)
465 } /* int csnmp_config_add_host */
467 static int csnmp_config_add_collect (oconfig_item_t *ci)
469 data_definition_t *data;
470 host_definition_t *host;
471 data_definition_t **data_list;
475 if (ci->values_num < 2)
477 WARNING ("snmp plugin: `Collect' needs at least two arguments.");
481 for (i = 0; i < ci->values_num; i++)
482 if (ci->values[i].type != OCONFIG_TYPE_STRING)
484 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
488 for (host = host_head; host != NULL; host = host->next)
489 if (strcasecmp (ci->values[0].value.string, host->name) == 0)
494 WARNING ("snmp plugin: No such host configured: `%s'",
495 ci->values[0].value.string);
499 data_list_len = host->data_list_len + ci->values_num - 1;
500 data_list = (data_definition_t **) realloc (host->data_list,
501 sizeof (data_definition_t *) * data_list_len);
502 if (data_list == NULL)
504 host->data_list = data_list;
506 for (i = 1; i < ci->values_num; i++)
508 for (data = data_head; data != NULL; data = data->next)
509 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
514 WARNING ("snmp plugin: No such data configured: `%s'",
515 ci->values[i].value.string);
519 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
520 host->name, host->data_list_len, data->name);
522 host->data_list[host->data_list_len] = data;
523 host->data_list_len++;
524 } /* for (values_num) */
527 } /* int csnmp_config_add_collect */
529 static int csnmp_config (oconfig_item_t *ci)
533 call_snmp_init_once ();
535 for (i = 0; i < ci->children_num; i++)
537 oconfig_item_t *child = ci->children + i;
538 if (strcasecmp ("Data", child->key) == 0)
539 csnmp_config_add_data (child);
540 else if (strcasecmp ("Host", child->key) == 0)
541 csnmp_config_add_host (child);
542 else if (strcasecmp ("Collect", child->key) == 0)
543 csnmp_config_add_collect (child);
546 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
548 } /* for (ci->children) */
551 } /* int csnmp_config */
553 static int csnmp_init (void)
555 call_snmp_init_once ();
560 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
563 value_list_t vl = VALUE_LIST_INIT;
565 values[0].gauge = snum;
566 values[1].gauge = mnum;
567 values[2].gauge = lnum;
570 vl.values_len = STATIC_ARRAY_SIZE (values);
571 vl.time = time (NULL);
572 strcpy (vl.host, hostname_g);
573 strcpy (vl.plugin, "load");
575 plugin_dispatch_values ("load", &vl);
579 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
585 if ((vl->type == ASN_INTEGER)
586 || (vl->type == ASN_UINTEGER)
587 || (vl->type == ASN_COUNTER)
588 || (vl->type == ASN_GAUGE))
590 temp = *vl->val.integer;
592 else if (vl->type == ASN_COUNTER64)
594 temp = vl->val.counter64->high;
596 temp += vl->val.counter64->low;
600 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
604 if (type == DS_TYPE_COUNTER)
608 else if (type == DS_TYPE_GAUGE)
616 } /* value_t csnmp_value_list_to_value */
618 static int csnmp_read_table (struct snmp_session *sess_ptr,
619 host_definition_t *host, data_definition_t *data)
622 } /* int csnmp_read_table */
624 static int csnmp_read_value (struct snmp_session *sess_ptr,
625 host_definition_t *host, data_definition_t *data)
627 struct snmp_pdu *req;
628 struct snmp_pdu *res;
629 struct variable_list *vb;
631 const data_set_t *ds;
632 value_list_t vl = VALUE_LIST_INIT;
637 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
638 host->name, data->name);
640 ds = plugin_get_ds (data->type);
643 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
647 if (ds->ds_num != data->values_len)
649 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
650 data->type, ds->ds_num, data->values_len);
654 vl.values_len = ds->ds_num;
655 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
656 if (vl.values == NULL)
658 for (i = 0; i < vl.values_len; i++)
660 if (ds->ds[i].type == DS_TYPE_COUNTER)
661 vl.values[i].counter = 0;
663 vl.values[i].gauge = NAN;
666 strncpy (vl.host, host->name, sizeof (vl.host));
667 vl.host[sizeof (vl.host) - 1] = '\0';
668 strcpy (vl.plugin, "snmp");
669 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
670 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
672 req = snmp_pdu_create (SNMP_MSG_GET);
675 ERROR ("snmp plugin: snmp_pdu_create failed.");
680 for (i = 0; i < data->values_len; i++)
681 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
682 status = snmp_synch_response (sess_ptr, req, &res);
684 if (status != STAT_SUCCESS)
686 ERROR ("snmp plugin: snmp_synch_response failed.");
691 vl.time = time (NULL);
693 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
696 snprint_variable (buffer, sizeof (buffer),
697 vb->name, vb->name_length, vb);
698 DEBUG ("snmp plugin: Got this variable: %s", buffer);
700 for (i = 0; i < data->values_len; i++)
701 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
702 vb->name, vb->name_length) == 0)
703 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
704 } /* for (res->variables) */
708 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
709 plugin_dispatch_values (data->type, &vl);
712 } /* int csnmp_read_value */
714 static int csnmp_read_host (host_definition_t *host)
716 struct snmp_session *sess_ptr;
719 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
721 sess_ptr = snmp_open (&host->sess);
722 if (sess_ptr == NULL)
724 snmp_perror ("snmp_open");
725 ERROR ("snmp plugin: snmp_open failed.");
729 for (i = 0; i < host->data_list_len; i++)
731 data_definition_t *data = host->data_list[i];
734 csnmp_read_table (sess_ptr, host, data);
736 csnmp_read_value (sess_ptr, host, data);
739 snmp_close (sess_ptr);
741 } /* int csnmp_read_host */
743 static int csnmp_read (void)
745 host_definition_t *host;
747 if (host_head == NULL)
749 INFO ("snmp plugin: No hosts configured.");
753 for (host = host_head; host != NULL; host = host->next)
754 csnmp_read_host (host);
757 } /* int csnmp_read */
759 void module_register (void)
761 plugin_register_complex_config ("snmp", csnmp_config);
762 plugin_register_init ("snmp", csnmp_init);
763 plugin_register_read ("snmp", csnmp_read);
764 } /* void module_register */
767 * vim: shiftwidth=2 softtabstop=2 tabstop=8