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_host_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_collect (host_definition_t *host,
378 data_definition_t *data;
379 data_definition_t **data_list;
383 if (ci->values_num < 1)
385 WARNING ("snmp plugin: `Collect' needs at least one argument.");
389 for (i = 0; i < ci->values_num; i++)
390 if (ci->values[i].type != OCONFIG_TYPE_STRING)
392 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
396 data_list_len = host->data_list_len + ci->values_num;
397 data_list = (data_definition_t **) realloc (host->data_list,
398 sizeof (data_definition_t *) * data_list_len);
399 if (data_list == NULL)
401 host->data_list = data_list;
403 for (i = 0; i < ci->values_num; i++)
405 for (data = data_head; data != NULL; data = data->next)
406 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
411 WARNING ("snmp plugin: No such data configured: `%s'",
412 ci->values[i].value.string);
416 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
417 host->name, host->data_list_len, data->name);
419 host->data_list[host->data_list_len] = data;
420 host->data_list_len++;
421 } /* for (values_num) */
424 } /* int csnmp_config_add_host_collect */
426 static int csnmp_config_add_host (oconfig_item_t *ci)
428 host_definition_t *hd;
432 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
434 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
438 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
441 memset (hd, '\0', sizeof (host_definition_t));
444 hd->name = strdup (ci->values[0].value.string);
445 if (hd->name == NULL)
451 snmp_sess_init (&hd->sess);
452 hd->sess.version = SNMP_VERSION_2c;
454 for (i = 0; i < ci->children_num; i++)
456 oconfig_item_t *option = ci->children + i;
459 if (strcasecmp ("Address", option->key) == 0)
460 status = csnmp_config_add_host_address (hd, option);
461 else if (strcasecmp ("Community", option->key) == 0)
462 status = csnmp_config_add_host_community (hd, option);
463 else if (strcasecmp ("Version", option->key) == 0)
464 status = csnmp_config_add_host_version (hd, option);
465 else if (strcasecmp ("Collect", option->key) == 0)
466 csnmp_config_add_host_collect (hd, option);
469 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
475 } /* for (ci->children) */
479 if (hd->address == NULL)
481 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
485 if (hd->community == NULL)
487 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
493 } /* while (status == 0) */
502 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
503 hd->name, hd->address, hd->community, hd->version);
505 if (host_head == NULL)
509 host_definition_t *last;
511 while (last->next != NULL)
517 } /* int csnmp_config_add_host */
519 static int csnmp_config (oconfig_item_t *ci)
523 call_snmp_init_once ();
525 for (i = 0; i < ci->children_num; i++)
527 oconfig_item_t *child = ci->children + i;
528 if (strcasecmp ("Data", child->key) == 0)
529 csnmp_config_add_data (child);
530 else if (strcasecmp ("Host", child->key) == 0)
531 csnmp_config_add_host (child);
534 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
536 } /* for (ci->children) */
539 } /* int csnmp_config */
541 static int csnmp_init (void)
543 call_snmp_init_once ();
548 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
551 value_list_t vl = VALUE_LIST_INIT;
553 values[0].gauge = snum;
554 values[1].gauge = mnum;
555 values[2].gauge = lnum;
558 vl.values_len = STATIC_ARRAY_SIZE (values);
559 vl.time = time (NULL);
560 strcpy (vl.host, hostname_g);
561 strcpy (vl.plugin, "load");
563 plugin_dispatch_values ("load", &vl);
567 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
573 if ((vl->type == ASN_INTEGER)
574 || (vl->type == ASN_UINTEGER)
575 || (vl->type == ASN_COUNTER)
576 || (vl->type == ASN_GAUGE))
578 temp = (uint32_t) *vl->val.integer;
579 DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
581 else if (vl->type == ASN_COUNTER64)
583 temp = (uint32_t) vl->val.counter64->high;
585 temp += (uint32_t) vl->val.counter64->low;
586 DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
590 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
594 if (type == DS_TYPE_COUNTER)
598 else if (type == DS_TYPE_GAUGE)
606 } /* value_t csnmp_value_list_to_value */
608 static int csnmp_read_table (struct snmp_session *sess_ptr,
609 host_definition_t *host, data_definition_t *data)
611 struct snmp_pdu *req;
612 struct snmp_pdu *res;
613 struct variable_list *vb;
616 uint32_t oid_list_len;
618 const data_set_t *ds;
619 value_list_t vl = VALUE_LIST_INIT;
624 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
625 host->name, data->name);
627 ds = plugin_get_ds (data->type);
630 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
634 if (ds->ds_num != data->values_len)
636 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
637 data->type, ds->ds_num, data->values_len);
641 vl.values_len = ds->ds_num;
642 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
643 if (vl.values == NULL)
645 for (i = 0; i < vl.values_len; i++)
647 if (ds->ds[i].type == DS_TYPE_COUNTER)
648 vl.values[i].counter = 0;
650 vl.values[i].gauge = NAN;
653 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
654 oid_list_len = data->values_len + 1;
655 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
656 memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
657 for (i = 0; i < data->values_len; i++)
658 memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
660 strncpy (vl.host, host->name, sizeof (vl.host));
661 vl.host[sizeof (vl.host) - 1] = '\0';
662 strcpy (vl.plugin, "snmp");
663 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
664 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
666 vl.time = time (NULL);
673 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
676 ERROR ("snmp plugin: snmp_pdu_create failed.");
681 for (i = 0; i < oid_list_len; i++)
682 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
684 status = snmp_synch_response (sess_ptr, req, &res);
686 if (status != STAT_SUCCESS)
688 ERROR ("snmp plugin: snmp_synch_response failed.");
693 assert (res != NULL);
702 /* Check if we left the subtree */
703 if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
704 vb->name, vb->name_length,
705 data->instance.oid.oid_len) != 0)
708 /* Get instance name */
709 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
711 strncpy (vl.type_instance, vb->val.bitstring,
712 sizeof (vl.type_instance));
713 escape_slashes (vl.type_instance, strlen (vl.type_instance));
717 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
718 snprintf (vl.type_instance, sizeof (vl.type_instance),
719 "%llu", val.counter);
721 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
722 DEBUG ("snmp plugin: data = `%s'; vl.type_instance = `%s';",
723 data->name, vl.type_instance);
725 /* Copy OID to oid_list[0] */
726 memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
727 oid_list[0].oid_len = vb->name_length;
729 /* Save the SUBID for the other values to check against */
730 subid = vb->name[vb->name_length - 1];
732 for (i = 0; i < data->values_len; i++)
734 vb = vb->next_variable;
741 /* Check if we left the subtree */
742 if (snmp_oid_ncompare (data->values[i].oid,
743 data->values[i].oid_len,
744 vb->name, vb->name_length,
745 data->values[i].oid_len) != 0)
747 WARNING ("snmp plugin: Column %i of data `%s' left subtree before the index did.",
754 if (subid != vb->name[vb->name_length - 1])
756 WARNING ("snmp plugin: Column %i of data `%s': SUBID mismatch: expected %i, found %i",
757 i + 1, data->type, (int) subid, (int) vb->name[vb->name_length - 1]);
763 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
764 if (ds->ds[i].type == DS_TYPE_COUNTER)
765 DEBUG ("snmp plugin: data = `%s'; vl.values[%i] = (counter) %llu",
766 data->name, i, vl.values[i].counter);
768 DEBUG ("snmp plugin: data = `%s'; vl.values[%i] = (gauge) %lf",
769 data->name, i, vl.values[i].gauge);
771 /* Copy OID to oid_list[i + 1] */
772 memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
773 oid_list[i + 1].oid_len = vb->name_length;
774 } /* for (data->values_len) */
780 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
782 plugin_dispatch_values (data->type, &vl);
783 } /* while (status == 0) */
789 } /* int csnmp_read_table */
791 static int csnmp_read_value (struct snmp_session *sess_ptr,
792 host_definition_t *host, data_definition_t *data)
794 struct snmp_pdu *req;
795 struct snmp_pdu *res;
796 struct variable_list *vb;
798 const data_set_t *ds;
799 value_list_t vl = VALUE_LIST_INIT;
804 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
805 host->name, data->name);
807 ds = plugin_get_ds (data->type);
810 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
814 if (ds->ds_num != data->values_len)
816 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
817 data->type, ds->ds_num, data->values_len);
821 vl.values_len = ds->ds_num;
822 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
823 if (vl.values == NULL)
825 for (i = 0; i < vl.values_len; i++)
827 if (ds->ds[i].type == DS_TYPE_COUNTER)
828 vl.values[i].counter = 0;
830 vl.values[i].gauge = NAN;
833 strncpy (vl.host, host->name, sizeof (vl.host));
834 vl.host[sizeof (vl.host) - 1] = '\0';
835 strcpy (vl.plugin, "snmp");
836 strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
837 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
839 req = snmp_pdu_create (SNMP_MSG_GET);
842 ERROR ("snmp plugin: snmp_pdu_create failed.");
847 for (i = 0; i < data->values_len; i++)
848 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
849 status = snmp_synch_response (sess_ptr, req, &res);
851 if (status != STAT_SUCCESS)
853 ERROR ("snmp plugin: snmp_synch_response failed.");
858 vl.time = time (NULL);
860 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
863 snprint_variable (buffer, sizeof (buffer),
864 vb->name, vb->name_length, vb);
865 DEBUG ("snmp plugin: Got this variable: %s", buffer);
867 for (i = 0; i < data->values_len; i++)
868 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
869 vb->name, vb->name_length) == 0)
870 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
871 } /* for (res->variables) */
875 DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
876 plugin_dispatch_values (data->type, &vl);
880 } /* int csnmp_read_value */
882 static int csnmp_read_host (host_definition_t *host)
884 struct snmp_session *sess_ptr;
887 DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
889 sess_ptr = snmp_open (&host->sess);
890 if (sess_ptr == NULL)
892 snmp_perror ("snmp_open");
893 ERROR ("snmp plugin: snmp_open failed.");
897 for (i = 0; i < host->data_list_len; i++)
899 data_definition_t *data = host->data_list[i];
902 csnmp_read_table (sess_ptr, host, data);
904 csnmp_read_value (sess_ptr, host, data);
907 snmp_close (sess_ptr);
909 } /* int csnmp_read_host */
911 static int csnmp_read (void)
913 host_definition_t *host;
915 if (host_head == NULL)
917 INFO ("snmp plugin: No hosts configured.");
921 for (host = host_head; host != NULL; host = host->next)
922 csnmp_read_host (host);
925 } /* int csnmp_read */
927 void module_register (void)
929 plugin_register_complex_config ("snmp", csnmp_config);
930 plugin_register_init ("snmp", csnmp_init);
931 plugin_register_read ("snmp", csnmp_read);
932 } /* void module_register */
935 * vim: shiftwidth=2 softtabstop=2 tabstop=8