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>
25 #include "utils_complain.h"
29 #include <net-snmp/net-snmp-config.h>
30 #include <net-snmp/net-snmp-includes.h>
33 * Private data structes
40 typedef struct oid_s oid_t;
44 char string[DATA_MAX_NAME_LEN];
47 typedef union instance_u instance_t;
49 struct data_definition_s
51 char *name; /* used to reference this from the `Collect' option */
52 char *type; /* used to find the data_set */
55 char *instance_prefix;
60 struct data_definition_s *next;
62 typedef struct data_definition_s data_definition_t;
64 struct host_definition_s
71 c_complain_t complaint;
74 data_definition_t **data_list;
76 enum /******************************************************/
78 STATE_IDLE, /* - just sits there until `next_update < interval_g' */
79 STATE_WAIT, /* - waits to be queried. */
80 STATE_BUSY /* - is currently being queried. */
81 } state; /******************************************************/
82 struct host_definition_s *next;
84 typedef struct host_definition_s host_definition_t;
86 /* These two types are used to cache values in `csnmp_read_table' to handle
88 struct csnmp_list_instances_s
91 char instance[DATA_MAX_NAME_LEN];
92 struct csnmp_list_instances_s *next;
94 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
96 struct csnmp_table_values_s
100 struct csnmp_table_values_s *next;
102 typedef struct csnmp_table_values_s csnmp_table_values_t;
107 static int do_shutdown = 0;
109 pthread_t *threads = NULL;
112 static data_definition_t *data_head = NULL;
113 static host_definition_t *host_head = NULL;
115 static pthread_mutex_t host_lock = PTHREAD_MUTEX_INITIALIZER;
116 static pthread_cond_t host_cond = PTHREAD_COND_INITIALIZER;
121 /* First there are many functions which do configuration stuff. It's a big
122 * bloated and messy, I'm afraid. */
125 * Callgraph for the config stuff:
127 * +-> call_snmp_init_once
128 * +-> csnmp_config_add_data
129 * ! +-> csnmp_config_add_data_type
130 * ! +-> csnmp_config_add_data_table
131 * ! +-> csnmp_config_add_data_instance
132 * ! +-> csnmp_config_add_data_instance_prefix
133 * ! +-> csnmp_config_add_data_values
134 * +-> csnmp_config_add_host
135 * +-> csnmp_config_add_host_address
136 * +-> csnmp_config_add_host_community
137 * +-> csnmp_config_add_host_version
138 * +-> csnmp_config_add_host_collect
139 * +-> csnmp_config_add_host_interval
141 static void call_snmp_init_once (void)
143 static int have_init = 0;
146 init_snmp (PACKAGE_NAME);
148 } /* void call_snmp_init_once */
150 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
152 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
154 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
159 dd->type = strdup (ci->values[0].value.string);
160 if (dd->type == NULL)
164 } /* int csnmp_config_add_data_type */
166 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
168 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
170 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
174 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
177 } /* int csnmp_config_add_data_table */
179 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
181 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
183 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
189 /* Instance is an OID */
190 dd->instance.oid.oid_len = MAX_OID_LEN;
192 if (!read_objid (ci->values[0].value.string,
193 dd->instance.oid.oid, &dd->instance.oid.oid_len))
195 ERROR ("snmp plugin: read_objid (%s) failed.",
196 ci->values[0].value.string);
202 /* Instance is a simple string */
203 sstrncpy (dd->instance.string, ci->values[0].value.string,
204 sizeof (dd->instance.string));
208 } /* int csnmp_config_add_data_instance */
210 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
213 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
215 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
221 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
222 "is set to `false'.", dd->name);
226 sfree (dd->instance_prefix);
227 dd->instance_prefix = strdup (ci->values[0].value.string);
228 if (dd->instance_prefix == NULL)
232 } /* int csnmp_config_add_data_instance_prefix */
234 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
238 if (ci->values_num < 1)
240 WARNING ("snmp plugin: `Values' needs at least one argument.");
244 for (i = 0; i < ci->values_num; i++)
245 if (ci->values[i].type != OCONFIG_TYPE_STRING)
247 WARNING ("snmp plugin: `Values' needs only string argument.");
253 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
254 if (dd->values == NULL)
256 dd->values_len = ci->values_num;
258 for (i = 0; i < ci->values_num; i++)
260 dd->values[i].oid_len = MAX_OID_LEN;
262 if (NULL == snmp_parse_oid (ci->values[i].value.string,
263 dd->values[i].oid, &dd->values[i].oid_len))
265 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
266 ci->values[i].value.string);
275 } /* int csnmp_config_add_data_instance */
277 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
279 if ((ci->values_num != 1)
280 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
282 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
286 dd->shift = ci->values[0].value.number;
289 } /* int csnmp_config_add_data_shift */
291 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
293 if ((ci->values_num != 1)
294 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
296 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
300 dd->scale = ci->values[0].value.number;
303 } /* int csnmp_config_add_data_scale */
305 static int csnmp_config_add_data (oconfig_item_t *ci)
307 data_definition_t *dd;
311 if ((ci->values_num != 1)
312 || (ci->values[0].type != OCONFIG_TYPE_STRING))
314 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
318 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
321 memset (dd, '\0', sizeof (data_definition_t));
323 dd->name = strdup (ci->values[0].value.string);
324 if (dd->name == NULL)
332 for (i = 0; i < ci->children_num; i++)
334 oconfig_item_t *option = ci->children + i;
337 if (strcasecmp ("Type", option->key) == 0)
338 status = csnmp_config_add_data_type (dd, option);
339 else if (strcasecmp ("Table", option->key) == 0)
340 status = csnmp_config_add_data_table (dd, option);
341 else if (strcasecmp ("Instance", option->key) == 0)
342 status = csnmp_config_add_data_instance (dd, option);
343 else if (strcasecmp ("InstancePrefix", option->key) == 0)
344 status = csnmp_config_add_data_instance_prefix (dd, option);
345 else if (strcasecmp ("Values", option->key) == 0)
346 status = csnmp_config_add_data_values (dd, option);
347 else if (strcasecmp ("Shift", option->key) == 0)
348 status = csnmp_config_add_data_shift (dd, option);
349 else if (strcasecmp ("Scale", option->key) == 0)
350 status = csnmp_config_add_data_scale (dd, option);
353 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
359 } /* for (ci->children) */
363 if (dd->type == NULL)
365 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
369 if (dd->values == NULL)
371 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
377 } /* while (status == 0) */
382 sfree (dd->instance_prefix);
388 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
389 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
391 if (data_head == NULL)
395 data_definition_t *last;
397 while (last->next != NULL)
403 } /* int csnmp_config_add_data */
405 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
407 if ((ci->values_num != 1)
408 || (ci->values[0].type != OCONFIG_TYPE_STRING))
410 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
414 if (hd->address == NULL)
417 hd->address = strdup (ci->values[0].value.string);
418 if (hd->address == NULL)
421 DEBUG ("snmp plugin: host = %s; host->address = %s;",
422 hd->name, hd->address);
425 } /* int csnmp_config_add_host_address */
427 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
429 if ((ci->values_num != 1)
430 || (ci->values[0].type != OCONFIG_TYPE_STRING))
432 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
436 if (hd->community == NULL)
437 free (hd->community);
439 hd->community = strdup (ci->values[0].value.string);
440 if (hd->community == NULL)
443 DEBUG ("snmp plugin: host = %s; host->community = %s;",
444 hd->name, hd->community);
447 } /* int csnmp_config_add_host_community */
449 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
453 if ((ci->values_num != 1)
454 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
456 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
460 version = (int) ci->values[0].value.number;
461 if ((version != 1) && (version != 2))
463 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
467 hd->version = version;
470 } /* int csnmp_config_add_host_address */
472 static int csnmp_config_add_host_collect (host_definition_t *host,
475 data_definition_t *data;
476 data_definition_t **data_list;
480 if (ci->values_num < 1)
482 WARNING ("snmp plugin: `Collect' needs at least one argument.");
486 for (i = 0; i < ci->values_num; i++)
487 if (ci->values[i].type != OCONFIG_TYPE_STRING)
489 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
493 data_list_len = host->data_list_len + ci->values_num;
494 data_list = (data_definition_t **) realloc (host->data_list,
495 sizeof (data_definition_t *) * data_list_len);
496 if (data_list == NULL)
498 host->data_list = data_list;
500 for (i = 0; i < ci->values_num; i++)
502 for (data = data_head; data != NULL; data = data->next)
503 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
508 WARNING ("snmp plugin: No such data configured: `%s'",
509 ci->values[i].value.string);
513 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
514 host->name, host->data_list_len, data->name);
516 host->data_list[host->data_list_len] = data;
517 host->data_list_len++;
518 } /* for (values_num) */
521 } /* int csnmp_config_add_host_collect */
523 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
525 if ((ci->values_num != 1)
526 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
528 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
532 hd->interval = (int) ci->values[0].value.number;
533 if (hd->interval < 0)
537 } /* int csnmp_config_add_host_interval */
539 static int csnmp_config_add_host (oconfig_item_t *ci)
541 host_definition_t *hd;
545 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
547 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
551 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
554 memset (hd, '\0', sizeof (host_definition_t));
556 C_COMPLAIN_INIT (&hd->complaint);
558 hd->name = strdup (ci->values[0].value.string);
559 if (hd->name == NULL)
565 hd->sess_handle = NULL;
568 hd->state = STATE_IDLE;
570 for (i = 0; i < ci->children_num; i++)
572 oconfig_item_t *option = ci->children + i;
575 if (strcasecmp ("Address", option->key) == 0)
576 status = csnmp_config_add_host_address (hd, option);
577 else if (strcasecmp ("Community", option->key) == 0)
578 status = csnmp_config_add_host_community (hd, option);
579 else if (strcasecmp ("Version", option->key) == 0)
580 status = csnmp_config_add_host_version (hd, option);
581 else if (strcasecmp ("Collect", option->key) == 0)
582 csnmp_config_add_host_collect (hd, option);
583 else if (strcasecmp ("Interval", option->key) == 0)
584 csnmp_config_add_host_interval (hd, option);
587 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
593 } /* for (ci->children) */
597 if (hd->address == NULL)
599 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
603 if (hd->community == NULL)
605 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
611 } /* while (status == 0) */
620 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
621 hd->name, hd->address, hd->community, hd->version);
623 if (host_head == NULL)
627 host_definition_t *last;
629 while (last->next != NULL)
635 } /* int csnmp_config_add_host */
637 static int csnmp_config (oconfig_item_t *ci)
641 call_snmp_init_once ();
643 for (i = 0; i < ci->children_num; i++)
645 oconfig_item_t *child = ci->children + i;
646 if (strcasecmp ("Data", child->key) == 0)
647 csnmp_config_add_data (child);
648 else if (strcasecmp ("Host", child->key) == 0)
649 csnmp_config_add_host (child);
652 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
654 } /* for (ci->children) */
657 } /* int csnmp_config */
659 /* End of the config stuff. Now the interesting part begins */
661 static void csnmp_host_close_session (host_definition_t *host)
663 if (host->sess_handle == NULL)
666 snmp_sess_close (host->sess_handle);
667 host->sess_handle = NULL;
668 } /* void csnmp_host_close_session */
670 static void csnmp_host_open_session (host_definition_t *host)
672 struct snmp_session sess;
674 if (host->sess_handle != NULL)
675 csnmp_host_close_session (host);
677 snmp_sess_init (&sess);
678 sess.peername = host->address;
679 sess.community = (u_char *) host->community;
680 sess.community_len = strlen (host->community);
681 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
683 /* snmp_sess_open will copy the `struct snmp_session *'. */
684 host->sess_handle = snmp_sess_open (&sess);
686 if (host->sess_handle == NULL)
690 snmp_error (&sess, NULL, NULL, &errstr);
692 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
693 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
696 } /* void csnmp_host_open_session */
698 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
699 double scale, double shift)
705 if ((vl->type == ASN_INTEGER)
706 || (vl->type == ASN_UINTEGER)
707 || (vl->type == ASN_COUNTER)
709 || (vl->type == ASN_TIMETICKS)
711 || (vl->type == ASN_GAUGE))
713 temp = (uint32_t) *vl->val.integer;
714 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", temp);
716 else if (vl->type == ASN_COUNTER64)
718 temp = (uint32_t) vl->val.counter64->high;
720 temp += (uint32_t) vl->val.counter64->low;
721 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", temp);
723 else if (vl->type == ASN_OCTET_STR)
725 /* We'll handle this later.. */
729 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
733 if (vl->type == ASN_OCTET_STR)
738 string = (char *) vl->val.string;
743 if (type == DS_TYPE_COUNTER)
744 ret.counter = (counter_t) strtoll (string, &endptr, /* base = */ 0);
745 else if (type == DS_TYPE_GAUGE)
746 ret.gauge = (gauge_t) strtod (string, &endptr);
749 /* Check if an error occurred */
750 if ((string == NULL) || (endptr == string))
752 if (type == DS_TYPE_COUNTER)
754 else if (type == DS_TYPE_GAUGE)
758 else if (type == DS_TYPE_COUNTER)
762 else if (type == DS_TYPE_GAUGE)
766 ret.gauge = (scale * temp) + shift;
770 } /* value_t csnmp_value_list_to_value */
772 /* Returns true if all OIDs have left their subtree */
773 static int csnmp_check_res_left_subtree (const host_definition_t *host,
774 const data_definition_t *data,
775 struct snmp_pdu *res)
777 struct variable_list *vb;
779 int num_left_subtree;
787 num_left_subtree = 0;
789 /* check all the variables and count how many have left their subtree */
790 for (vb = res->variables, i = 0;
791 (vb != NULL) && (i < data->values_len);
792 vb = vb->next_variable, i++)
795 if (snmp_oid_ncompare (data->values[i].oid,
796 data->values[i].oid_len,
797 vb->name, vb->name_length,
798 data->values[i].oid_len) != 0)
802 /* check if enough variables have been returned */
803 if (i < data->values_len)
805 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
806 host->name, data->values_len, i);
810 if (data->instance.oid.oid_len > 0)
814 ERROR ("snmp plugin: host %s: Expected one more variable for "
815 "the instance..", host->name);
820 if (snmp_oid_ncompare (data->instance.oid.oid,
821 data->instance.oid.oid_len,
822 vb->name, vb->name_length,
823 data->instance.oid.oid_len) != 0)
827 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
828 "left their subtree",
829 num_left_subtree, num_checked);
830 if (num_left_subtree >= num_checked)
833 } /* int csnmp_check_res_left_subtree */
835 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
836 csnmp_list_instances_t **tail,
837 const struct snmp_pdu *res)
839 csnmp_list_instances_t *il;
840 struct variable_list *vb;
842 /* Set vb on the last variable */
843 for (vb = res->variables;
844 (vb != NULL) && (vb->next_variable != NULL);
845 vb = vb->next_variable)
850 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
853 ERROR ("snmp plugin: malloc failed.");
856 il->subid = vb->name[vb->name_length - 1];
859 /* Get instance name */
860 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
865 memset (il->instance, 0, sizeof (il->instance));
866 instance_len = sizeof (il->instance) - 1;
867 if (instance_len > vb->val_len)
868 instance_len = vb->val_len;
870 sstrncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
872 : vb->val.bitstring),
875 for (ptr = il->instance; *ptr != '\0'; ptr++)
877 if ((*ptr > 0) && (*ptr < 32))
879 else if (*ptr == '/')
882 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
886 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
887 ssnprintf (il->instance, sizeof (il->instance),
888 "%llu", val.counter);
891 /* TODO: Debugging output */
900 } /* int csnmp_instance_list_add */
902 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
903 csnmp_list_instances_t *instance_list,
904 csnmp_table_values_t **value_table)
906 const data_set_t *ds;
907 value_list_t vl = VALUE_LIST_INIT;
909 csnmp_list_instances_t *instance_list_ptr;
910 csnmp_table_values_t **value_table_ptr;
916 ds = plugin_get_ds (data->type);
919 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
922 assert (ds->ds_num == data->values_len);
924 instance_list_ptr = instance_list;
926 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
928 if (value_table_ptr == NULL)
930 for (i = 0; i < data->values_len; i++)
931 value_table_ptr[i] = value_table[i];
933 vl.values_len = ds->ds_num;
934 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
935 if (vl.values == NULL)
937 ERROR ("snmp plugin: malloc failed.");
938 sfree (value_table_ptr);
942 sstrncpy (vl.host, host->name, sizeof (vl.host));
943 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
945 vl.interval = host->interval;
946 vl.time = time (NULL);
951 while (have_more != 0)
953 if (instance_list != NULL)
955 while ((instance_list_ptr != NULL)
956 && (instance_list_ptr->subid < subid))
957 instance_list_ptr = instance_list_ptr->next;
959 if (instance_list_ptr == NULL)
964 else if (instance_list_ptr->subid > subid)
966 subid = instance_list_ptr->subid;
969 } /* if (instance_list != NULL) */
971 for (i = 0; i < data->values_len; i++)
973 while ((value_table_ptr[i] != NULL)
974 && (value_table_ptr[i]->subid < subid))
975 value_table_ptr[i] = value_table_ptr[i]->next;
977 if (value_table_ptr[i] == NULL)
982 else if (value_table_ptr[i]->subid > subid)
984 subid = value_table_ptr[i]->subid;
987 } /* for (i = 0; i < columns; i++) */
988 /* The subid has been increased - start scanning from the beginning
990 if (i < data->values_len)
993 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
994 * to the same subid. instance_list_ptr is either NULL or points to the
995 * same subid, too. */
997 for (i = 1; i < data->values_len; i++)
999 assert (value_table_ptr[i] != NULL);
1000 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
1002 assert ((instance_list_ptr == NULL)
1003 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
1006 sstrncpy (vl.type, data->type, sizeof (vl.type));
1009 char temp[DATA_MAX_NAME_LEN];
1011 if (instance_list_ptr == NULL)
1012 ssnprintf (temp, sizeof (temp), "%u", (uint32_t) subid);
1014 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1016 if (data->instance_prefix == NULL)
1017 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1019 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1020 data->instance_prefix, temp);
1023 for (i = 0; i < data->values_len; i++)
1024 vl.values[i] = value_table_ptr[i]->value;
1026 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1027 plugin_dispatch_values (&vl);
1030 } /* while (have_more != 0) */
1033 sfree (value_table_ptr);
1036 } /* int csnmp_dispatch_table */
1038 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1040 struct snmp_pdu *req;
1041 struct snmp_pdu *res;
1042 struct variable_list *vb;
1044 const data_set_t *ds;
1046 uint32_t oid_list_len;
1051 /* `value_table' and `value_table_ptr' implement a linked list for each
1052 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1053 * instance names. This is used to jump gaps in the table. */
1054 csnmp_list_instances_t *instance_list;
1055 csnmp_list_instances_t *instance_list_ptr;
1056 csnmp_table_values_t **value_table;
1057 csnmp_table_values_t **value_table_ptr;
1059 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1060 host->name, data->name);
1062 if (host->sess_handle == NULL)
1064 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1068 ds = plugin_get_ds (data->type);
1071 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1075 if (ds->ds_num != data->values_len)
1077 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1078 data->type, ds->ds_num, data->values_len);
1082 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1083 oid_list_len = data->values_len + 1;
1084 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1085 if (oid_list == NULL)
1087 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1090 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1091 if (data->instance.oid.oid_len > 0)
1092 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1096 /* Allocate the `value_table' */
1097 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1098 * 2 * data->values_len);
1099 if (value_table == NULL)
1101 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1105 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1106 value_table_ptr = value_table + data->values_len;
1108 instance_list = NULL;
1109 instance_list_ptr = NULL;
1114 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1117 ERROR ("snmp plugin: snmp_pdu_create failed.");
1122 for (i = 0; i < oid_list_len; i++)
1123 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1126 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1128 if ((status != STAT_SUCCESS) || (res == NULL))
1130 char *errstr = NULL;
1132 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1134 c_complain (LOG_ERR, &host->complaint,
1135 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1136 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1139 snmp_free_pdu (res);
1143 csnmp_host_close_session (host);
1149 assert (res != NULL);
1150 c_release (LOG_INFO, &host->complaint,
1151 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1154 vb = res->variables;
1161 /* Check if all values (and possibly the instance) have left their
1163 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1169 /* if an instance-OID is configured.. */
1170 if (data->instance.oid.oid_len > 0)
1172 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1173 * add it to the list */
1174 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1177 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1182 /* Set vb on the last variable */
1183 for (vb = res->variables;
1184 (vb != NULL) && (vb->next_variable != NULL);
1185 vb = vb->next_variable)
1187 assert (vb != NULL);
1189 /* Copy OID to oid_list[data->values_len] */
1190 memcpy (oid_list[data->values_len].oid, vb->name,
1191 sizeof (oid) * vb->name_length);
1192 oid_list[data->values_len].oid_len = vb->name_length;
1195 for (vb = res->variables, i = 0;
1196 (vb != NULL) && (i < data->values_len);
1197 vb = vb->next_variable, i++)
1199 csnmp_table_values_t *vt;
1201 /* Check if we left the subtree */
1202 if (snmp_oid_ncompare (data->values[i].oid,
1203 data->values[i].oid_len,
1204 vb->name, vb->name_length,
1205 data->values[i].oid_len) != 0)
1207 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1208 host->name, data->name, i);
1212 if ((value_table_ptr[i] != NULL)
1213 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1215 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1216 "SUBID is not increasing.",
1217 host->name, data->name, i);
1221 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1224 ERROR ("snmp plugin: malloc failed.");
1229 vt->subid = vb->name[vb->name_length - 1];
1230 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1231 data->scale, data->shift);
1234 if (value_table_ptr[i] == NULL)
1235 value_table[i] = vt;
1237 value_table_ptr[i]->next = vt;
1238 value_table_ptr[i] = vt;
1240 /* Copy OID to oid_list[i + 1] */
1241 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1242 oid_list[i].oid_len = vb->name_length;
1243 } /* for (i = data->values_len) */
1246 snmp_free_pdu (res);
1248 } /* while (status == 0) */
1251 snmp_free_pdu (res);
1255 csnmp_dispatch_table (host, data, instance_list, value_table);
1257 /* Free all allocated variables here */
1258 while (instance_list != NULL)
1260 instance_list_ptr = instance_list->next;
1261 sfree (instance_list);
1262 instance_list = instance_list_ptr;
1265 for (i = 0; i < data->values_len; i++)
1267 csnmp_table_values_t *tmp;
1268 while (value_table[i] != NULL)
1270 tmp = value_table[i]->next;
1271 sfree (value_table[i]);
1272 value_table[i] = tmp;
1276 sfree (value_table);
1280 } /* int csnmp_read_table */
1282 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1284 struct snmp_pdu *req;
1285 struct snmp_pdu *res;
1286 struct variable_list *vb;
1288 const data_set_t *ds;
1289 value_list_t vl = VALUE_LIST_INIT;
1294 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1295 host->name, data->name);
1297 if (host->sess_handle == NULL)
1299 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1303 ds = plugin_get_ds (data->type);
1306 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1310 if (ds->ds_num != data->values_len)
1312 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1313 data->type, ds->ds_num, data->values_len);
1317 vl.values_len = ds->ds_num;
1318 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1319 if (vl.values == NULL)
1321 for (i = 0; i < vl.values_len; i++)
1323 if (ds->ds[i].type == DS_TYPE_COUNTER)
1324 vl.values[i].counter = 0;
1326 vl.values[i].gauge = NAN;
1329 sstrncpy (vl.host, host->name, sizeof (vl.host));
1330 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1331 sstrncpy (vl.type, data->type, sizeof (vl.type));
1332 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1334 vl.interval = host->interval;
1336 req = snmp_pdu_create (SNMP_MSG_GET);
1339 ERROR ("snmp plugin: snmp_pdu_create failed.");
1344 for (i = 0; i < data->values_len; i++)
1345 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1348 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1350 if ((status != STAT_SUCCESS) || (res == NULL))
1352 char *errstr = NULL;
1354 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1355 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1356 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1359 snmp_free_pdu (res);
1363 csnmp_host_close_session (host);
1368 vl.time = time (NULL);
1370 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1374 snprint_variable (buffer, sizeof (buffer),
1375 vb->name, vb->name_length, vb);
1376 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1377 #endif /* COLLECT_DEBUG */
1379 for (i = 0; i < data->values_len; i++)
1380 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1381 vb->name, vb->name_length) == 0)
1382 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1383 data->scale, data->shift);
1384 } /* for (res->variables) */
1387 snmp_free_pdu (res);
1390 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1391 plugin_dispatch_values (&vl);
1395 } /* int csnmp_read_value */
1397 static int csnmp_read_host (host_definition_t *host)
1403 time_start = time (NULL);
1404 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1405 (unsigned int) time_start);
1407 if (host->sess_handle == NULL)
1408 csnmp_host_open_session (host);
1410 if (host->sess_handle == NULL)
1413 for (i = 0; i < host->data_list_len; i++)
1415 data_definition_t *data = host->data_list[i];
1418 csnmp_read_table (host, data);
1420 csnmp_read_value (host, data);
1423 time_end = time (NULL);
1424 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1425 (unsigned int) time_end);
1426 if ((time_end - time_start) > host->interval)
1428 WARNING ("snmp plugin: Host `%s' should be queried every %i seconds, "
1429 "but reading all values takes %u seconds.",
1430 host->name, host->interval, (unsigned int) (time_end - time_start));
1434 } /* int csnmp_read_host */
1436 static void *csnmp_read_thread (void *data)
1438 host_definition_t *host;
1440 pthread_mutex_lock (&host_lock);
1441 while (do_shutdown == 0)
1443 pthread_cond_wait (&host_cond, &host_lock);
1445 for (host = host_head; host != NULL; host = host->next)
1447 if (do_shutdown != 0)
1449 if (host->state != STATE_WAIT)
1452 host->state = STATE_BUSY;
1453 pthread_mutex_unlock (&host_lock);
1454 csnmp_read_host (host);
1455 pthread_mutex_lock (&host_lock);
1456 host->state = STATE_IDLE;
1458 } /* while (do_shutdown == 0) */
1459 pthread_mutex_unlock (&host_lock);
1461 pthread_exit ((void *) 0);
1462 return ((void *) 0);
1463 } /* void *csnmp_read_thread */
1465 static int csnmp_init (void)
1467 host_definition_t *host;
1470 if (host_head == NULL)
1472 NOTICE ("snmp plugin: No host has been defined.");
1476 call_snmp_init_once ();
1479 for (host = host_head; host != NULL; host = host->next)
1482 /* We need to initialize `interval' here, because `interval_g' isn't
1483 * initialized during `configure'. */
1484 host->next_update = time (NULL);
1485 if (host->interval == 0)
1487 host->interval = interval_g;
1489 else if (host->interval < interval_g)
1491 host->interval = interval_g;
1492 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1493 host->name, host->interval);
1496 csnmp_host_open_session (host);
1499 /* Now start the reading threads */
1500 if (threads_num > 3)
1502 threads_num = 3 + ((threads_num - 3) / 10);
1503 if (threads_num > 10)
1507 threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1508 if (threads == NULL)
1510 ERROR ("snmp plugin: malloc failed.");
1513 memset (threads, '\0', threads_num * sizeof (pthread_t));
1515 for (i = 0; i < threads_num; i++)
1516 pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1519 } /* int csnmp_init */
1521 static int csnmp_read (void)
1523 host_definition_t *host;
1526 if (host_head == NULL)
1528 INFO ("snmp plugin: No hosts configured.");
1534 pthread_mutex_lock (&host_lock);
1535 for (host = host_head; host != NULL; host = host->next)
1537 if (host->state != STATE_IDLE)
1540 /* Skip this host if the next or a later iteration will be sufficient. */
1541 if (host->next_update >= (now + interval_g))
1544 host->state = STATE_WAIT;
1545 host->next_update = now + host->interval;
1548 pthread_cond_broadcast (&host_cond);
1549 pthread_mutex_unlock (&host_lock);
1552 } /* int csnmp_read */
1554 static int csnmp_shutdown (void)
1556 host_definition_t *host_this;
1557 host_definition_t *host_next;
1559 data_definition_t *data_this;
1560 data_definition_t *data_next;
1564 pthread_mutex_lock (&host_lock);
1566 pthread_cond_broadcast (&host_cond);
1567 pthread_mutex_unlock (&host_lock);
1569 for (i = 0; i < threads_num; i++)
1570 pthread_join (threads[i], NULL);
1572 /* Now that all the threads have exited, let's free all the global variables.
1573 * This isn't really neccessary, I guess, but I think it's good stile to do
1575 host_this = host_head;
1577 while (host_this != NULL)
1579 host_next = host_this->next;
1581 csnmp_host_close_session (host_this);
1583 sfree (host_this->name);
1584 sfree (host_this->address);
1585 sfree (host_this->community);
1586 sfree (host_this->data_list);
1589 host_this = host_next;
1592 data_this = data_head;
1594 while (data_this != NULL)
1596 data_next = data_this->next;
1598 sfree (data_this->name);
1599 sfree (data_this->type);
1600 sfree (data_this->values);
1603 data_this = data_next;
1607 } /* int csnmp_shutdown */
1609 void module_register (void)
1611 plugin_register_complex_config ("snmp", csnmp_config);
1612 plugin_register_init ("snmp", csnmp_init);
1613 plugin_register_read ("snmp", csnmp_read);
1614 plugin_register_shutdown ("snmp", csnmp_shutdown);
1615 } /* void module_register */
1618 * vim: shiftwidth=2 softtabstop=2 tabstop=8