1 /**
2 * collectd - src/snmp.c
3 * Copyright (C) 2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_complain.h"
27 #include <pthread.h>
29 #include <net-snmp/net-snmp-config.h>
30 #include <net-snmp/net-snmp-includes.h>
32 /*
33 * Private data structes
34 */
35 struct oid_s
36 {
37 oid oid[MAX_OID_LEN];
38 size_t oid_len;
39 };
40 typedef struct oid_s oid_t;
42 union instance_u
43 {
44 char string[DATA_MAX_NAME_LEN];
45 oid_t oid;
46 };
47 typedef union instance_u instance_t;
49 struct data_definition_s
50 {
51 char *name; /* used to reference this from the `Collect' option */
52 char *type; /* used to find the data_set */
53 int is_table;
54 instance_t instance;
55 char *instance_prefix;
56 oid_t *values;
57 int values_len;
58 double scale;
59 double shift;
60 struct data_definition_s *next;
61 };
62 typedef struct data_definition_s data_definition_t;
64 struct host_definition_s
65 {
66 char *name;
67 char *address;
68 char *community;
69 int version;
70 void *sess_handle;
71 c_complain_t complaint;
72 uint32_t interval;
73 time_t next_update;
74 data_definition_t **data_list;
75 int data_list_len;
76 enum /******************************************************/
77 { /* This host.. */
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;
83 };
84 typedef struct host_definition_s host_definition_t;
86 /* These two types are used to cache values in `csnmp_read_table' to handle
87 * gaps in tables. */
88 struct csnmp_list_instances_s
89 {
90 oid subid;
91 char instance[DATA_MAX_NAME_LEN];
92 struct csnmp_list_instances_s *next;
93 };
94 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
96 struct csnmp_table_values_s
97 {
98 oid subid;
99 value_t value;
100 struct csnmp_table_values_s *next;
101 };
102 typedef struct csnmp_table_values_s csnmp_table_values_t;
104 /*
105 * Private variables
106 */
107 static int do_shutdown = 0;
109 pthread_t *threads = NULL;
110 int threads_num = 0;
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;
118 /*
119 * Private functions
120 */
121 /* Many functions to handle the configuration. {{{ */
122 /* First there are many functions which do configuration stuff. It's a big
123 * bloated and messy, I'm afraid. */
125 /*
126 * Callgraph for the config stuff:
127 * csnmp_config
128 * +-> call_snmp_init_once
129 * +-> csnmp_config_add_data
130 * ! +-> csnmp_config_add_data_type
131 * ! +-> csnmp_config_add_data_table
132 * ! +-> csnmp_config_add_data_instance
133 * ! +-> csnmp_config_add_data_instance_prefix
134 * ! +-> csnmp_config_add_data_values
135 * +-> csnmp_config_add_host
136 * +-> csnmp_config_add_host_address
137 * +-> csnmp_config_add_host_community
138 * +-> csnmp_config_add_host_version
139 * +-> csnmp_config_add_host_collect
140 * +-> csnmp_config_add_host_interval
141 */
142 static void call_snmp_init_once (void)
143 {
144 static int have_init = 0;
146 if (have_init == 0)
147 init_snmp (PACKAGE_NAME);
148 have_init = 1;
149 } /* void call_snmp_init_once */
151 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
152 {
153 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
154 {
155 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
156 return (-1);
157 }
159 sfree (dd->type);
160 dd->type = strdup (ci->values[0].value.string);
161 if (dd->type == NULL)
162 return (-1);
164 return (0);
165 } /* int csnmp_config_add_data_type */
167 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
168 {
169 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
170 {
171 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
172 return (-1);
173 }
175 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
177 return (0);
178 } /* int csnmp_config_add_data_table */
180 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
181 {
182 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
183 {
184 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
185 return (-1);
186 }
188 if (dd->is_table)
189 {
190 /* Instance is an OID */
191 dd->instance.oid.oid_len = MAX_OID_LEN;
193 if (!read_objid (ci->values[0].value.string,
194 dd->instance.oid.oid, &dd->instance.oid.oid_len))
195 {
196 ERROR ("snmp plugin: read_objid (%s) failed.",
197 ci->values[0].value.string);
198 return (-1);
199 }
200 }
201 else
202 {
203 /* Instance is a simple string */
204 sstrncpy (dd->instance.string, ci->values[0].value.string,
205 sizeof (dd->instance.string));
206 }
208 return (0);
209 } /* int csnmp_config_add_data_instance */
211 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
212 oconfig_item_t *ci)
213 {
214 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
215 {
216 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
217 return (-1);
218 }
220 if (!dd->is_table)
221 {
222 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
223 "is set to `false'.", dd->name);
224 return (-1);
225 }
227 sfree (dd->instance_prefix);
228 dd->instance_prefix = strdup (ci->values[0].value.string);
229 if (dd->instance_prefix == NULL)
230 return (-1);
232 return (0);
233 } /* int csnmp_config_add_data_instance_prefix */
235 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
236 {
237 int i;
239 if (ci->values_num < 1)
240 {
241 WARNING ("snmp plugin: `Values' needs at least one argument.");
242 return (-1);
243 }
245 for (i = 0; i < ci->values_num; i++)
246 if (ci->values[i].type != OCONFIG_TYPE_STRING)
247 {
248 WARNING ("snmp plugin: `Values' needs only string argument.");
249 return (-1);
250 }
252 sfree (dd->values);
253 dd->values_len = 0;
254 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
255 if (dd->values == NULL)
256 return (-1);
257 dd->values_len = ci->values_num;
259 for (i = 0; i < ci->values_num; i++)
260 {
261 dd->values[i].oid_len = MAX_OID_LEN;
263 if (NULL == snmp_parse_oid (ci->values[i].value.string,
264 dd->values[i].oid, &dd->values[i].oid_len))
265 {
266 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
267 ci->values[i].value.string);
268 free (dd->values);
269 dd->values = NULL;
270 dd->values_len = 0;
271 return (-1);
272 }
273 }
275 return (0);
276 } /* int csnmp_config_add_data_instance */
278 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
279 {
280 if ((ci->values_num != 1)
281 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
282 {
283 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
284 return (-1);
285 }
287 dd->shift = ci->values[0].value.number;
289 return (0);
290 } /* int csnmp_config_add_data_shift */
292 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
293 {
294 if ((ci->values_num != 1)
295 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
296 {
297 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
298 return (-1);
299 }
301 dd->scale = ci->values[0].value.number;
303 return (0);
304 } /* int csnmp_config_add_data_scale */
306 static int csnmp_config_add_data (oconfig_item_t *ci)
307 {
308 data_definition_t *dd;
309 int status = 0;
310 int i;
312 if ((ci->values_num != 1)
313 || (ci->values[0].type != OCONFIG_TYPE_STRING))
314 {
315 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
316 return (-1);
317 }
319 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
320 if (dd == NULL)
321 return (-1);
322 memset (dd, '\0', sizeof (data_definition_t));
324 dd->name = strdup (ci->values[0].value.string);
325 if (dd->name == NULL)
326 {
327 free (dd);
328 return (-1);
329 }
330 dd->scale = 1.0;
331 dd->shift = 0.0;
333 for (i = 0; i < ci->children_num; i++)
334 {
335 oconfig_item_t *option = ci->children + i;
336 status = 0;
338 if (strcasecmp ("Type", option->key) == 0)
339 status = csnmp_config_add_data_type (dd, option);
340 else if (strcasecmp ("Table", option->key) == 0)
341 status = csnmp_config_add_data_table (dd, option);
342 else if (strcasecmp ("Instance", option->key) == 0)
343 status = csnmp_config_add_data_instance (dd, option);
344 else if (strcasecmp ("InstancePrefix", option->key) == 0)
345 status = csnmp_config_add_data_instance_prefix (dd, option);
346 else if (strcasecmp ("Values", option->key) == 0)
347 status = csnmp_config_add_data_values (dd, option);
348 else if (strcasecmp ("Shift", option->key) == 0)
349 status = csnmp_config_add_data_shift (dd, option);
350 else if (strcasecmp ("Scale", option->key) == 0)
351 status = csnmp_config_add_data_scale (dd, option);
352 else
353 {
354 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
355 status = -1;
356 }
358 if (status != 0)
359 break;
360 } /* for (ci->children) */
362 while (status == 0)
363 {
364 if (dd->type == NULL)
365 {
366 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
367 status = -1;
368 break;
369 }
370 if (dd->values == NULL)
371 {
372 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
373 status = -1;
374 break;
375 }
377 break;
378 } /* while (status == 0) */
380 if (status != 0)
381 {
382 sfree (dd->name);
383 sfree (dd->instance_prefix);
384 sfree (dd->values);
385 sfree (dd);
386 return (-1);
387 }
389 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
390 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
392 if (data_head == NULL)
393 data_head = dd;
394 else
395 {
396 data_definition_t *last;
397 last = data_head;
398 while (last->next != NULL)
399 last = last->next;
400 last->next = dd;
401 }
403 return (0);
404 } /* int csnmp_config_add_data */
406 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
407 {
408 if ((ci->values_num != 1)
409 || (ci->values[0].type != OCONFIG_TYPE_STRING))
410 {
411 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
412 return (-1);
413 }
415 if (hd->address == NULL)
416 free (hd->address);
418 hd->address = strdup (ci->values[0].value.string);
419 if (hd->address == NULL)
420 return (-1);
422 DEBUG ("snmp plugin: host = %s; host->address = %s;",
423 hd->name, hd->address);
425 return (0);
426 } /* int csnmp_config_add_host_address */
428 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
429 {
430 if ((ci->values_num != 1)
431 || (ci->values[0].type != OCONFIG_TYPE_STRING))
432 {
433 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
434 return (-1);
435 }
437 if (hd->community == NULL)
438 free (hd->community);
440 hd->community = strdup (ci->values[0].value.string);
441 if (hd->community == NULL)
442 return (-1);
444 DEBUG ("snmp plugin: host = %s; host->community = %s;",
445 hd->name, hd->community);
447 return (0);
448 } /* int csnmp_config_add_host_community */
450 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
451 {
452 int version;
454 if ((ci->values_num != 1)
455 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
456 {
457 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
458 return (-1);
459 }
461 version = (int) ci->values[0].value.number;
462 if ((version != 1) && (version != 2))
463 {
464 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
465 return (-1);
466 }
468 hd->version = version;
470 return (0);
471 } /* int csnmp_config_add_host_address */
473 static int csnmp_config_add_host_collect (host_definition_t *host,
474 oconfig_item_t *ci)
475 {
476 data_definition_t *data;
477 data_definition_t **data_list;
478 int data_list_len;
479 int i;
481 if (ci->values_num < 1)
482 {
483 WARNING ("snmp plugin: `Collect' needs at least one argument.");
484 return (-1);
485 }
487 for (i = 0; i < ci->values_num; i++)
488 if (ci->values[i].type != OCONFIG_TYPE_STRING)
489 {
490 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
491 return (-1);
492 }
494 data_list_len = host->data_list_len + ci->values_num;
495 data_list = (data_definition_t **) realloc (host->data_list,
496 sizeof (data_definition_t *) * data_list_len);
497 if (data_list == NULL)
498 return (-1);
499 host->data_list = data_list;
501 for (i = 0; i < ci->values_num; i++)
502 {
503 for (data = data_head; data != NULL; data = data->next)
504 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
505 break;
507 if (data == NULL)
508 {
509 WARNING ("snmp plugin: No such data configured: `%s'",
510 ci->values[i].value.string);
511 continue;
512 }
514 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
515 host->name, host->data_list_len, data->name);
517 host->data_list[host->data_list_len] = data;
518 host->data_list_len++;
519 } /* for (values_num) */
521 return (0);
522 } /* int csnmp_config_add_host_collect */
524 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
525 {
526 if ((ci->values_num != 1)
527 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
528 {
529 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
530 return (-1);
531 }
533 hd->interval = ci->values[0].value.number >= 0
534 ? (uint32_t) ci->values[0].value.number
535 : 0;
537 return (0);
538 } /* int csnmp_config_add_host_interval */
540 static int csnmp_config_add_host (oconfig_item_t *ci)
541 {
542 host_definition_t *hd;
543 int status = 0;
544 int i;
546 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
547 {
548 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
549 return (-1);
550 }
552 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
553 if (hd == NULL)
554 return (-1);
555 memset (hd, '\0', sizeof (host_definition_t));
556 hd->version = 2;
557 C_COMPLAIN_INIT (&hd->complaint);
559 hd->name = strdup (ci->values[0].value.string);
560 if (hd->name == NULL)
561 {
562 free (hd);
563 return (-1);
564 }
566 hd->sess_handle = NULL;
567 hd->interval = 0;
568 hd->next_update = 0;
569 hd->state = STATE_IDLE;
571 for (i = 0; i < ci->children_num; i++)
572 {
573 oconfig_item_t *option = ci->children + i;
574 status = 0;
576 if (strcasecmp ("Address", option->key) == 0)
577 status = csnmp_config_add_host_address (hd, option);
578 else if (strcasecmp ("Community", option->key) == 0)
579 status = csnmp_config_add_host_community (hd, option);
580 else if (strcasecmp ("Version", option->key) == 0)
581 status = csnmp_config_add_host_version (hd, option);
582 else if (strcasecmp ("Collect", option->key) == 0)
583 csnmp_config_add_host_collect (hd, option);
584 else if (strcasecmp ("Interval", option->key) == 0)
585 csnmp_config_add_host_interval (hd, option);
586 else
587 {
588 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
589 status = -1;
590 }
592 if (status != 0)
593 break;
594 } /* for (ci->children) */
596 while (status == 0)
597 {
598 if (hd->address == NULL)
599 {
600 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
601 status = -1;
602 break;
603 }
604 if (hd->community == NULL)
605 {
606 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
607 status = -1;
608 break;
609 }
611 break;
612 } /* while (status == 0) */
614 if (status != 0)
615 {
616 sfree (hd->name);
617 sfree (hd);
618 return (-1);
619 }
621 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
622 hd->name, hd->address, hd->community, hd->version);
624 if (host_head == NULL)
625 host_head = hd;
626 else
627 {
628 host_definition_t *last;
629 last = host_head;
630 while (last->next != NULL)
631 last = last->next;
632 last->next = hd;
633 }
635 return (0);
636 } /* int csnmp_config_add_host */
638 static int csnmp_config (oconfig_item_t *ci)
639 {
640 int i;
642 call_snmp_init_once ();
644 for (i = 0; i < ci->children_num; i++)
645 {
646 oconfig_item_t *child = ci->children + i;
647 if (strcasecmp ("Data", child->key) == 0)
648 csnmp_config_add_data (child);
649 else if (strcasecmp ("Host", child->key) == 0)
650 csnmp_config_add_host (child);
651 else
652 {
653 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
654 }
655 } /* for (ci->children) */
657 return (0);
658 } /* int csnmp_config */
660 /* }}} End of the config stuff. Now the interesting part begins */
662 static void csnmp_host_close_session (host_definition_t *host)
663 {
664 if (host->sess_handle == NULL)
665 return;
667 snmp_sess_close (host->sess_handle);
668 host->sess_handle = NULL;
669 } /* void csnmp_host_close_session */
671 static void csnmp_host_open_session (host_definition_t *host)
672 {
673 struct snmp_session sess;
675 if (host->sess_handle != NULL)
676 csnmp_host_close_session (host);
678 snmp_sess_init (&sess);
679 sess.peername = host->address;
680 sess.community = (u_char *) host->community;
681 sess.community_len = strlen (host->community);
682 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
684 /* snmp_sess_open will copy the `struct snmp_session *'. */
685 host->sess_handle = snmp_sess_open (&sess);
687 if (host->sess_handle == NULL)
688 {
689 char *errstr = NULL;
691 snmp_error (&sess, NULL, NULL, &errstr);
693 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
694 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
695 sfree (errstr);
696 }
697 } /* void csnmp_host_open_session */
699 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
700 double scale, double shift)
701 {
702 value_t ret;
703 uint64_t temp = 0;
704 int defined = 1;
706 if ((vl->type == ASN_INTEGER)
707 || (vl->type == ASN_UINTEGER)
708 || (vl->type == ASN_COUNTER)
709 #ifdef ASN_TIMETICKS
710 || (vl->type == ASN_TIMETICKS)
711 #endif
712 || (vl->type == ASN_GAUGE))
713 {
714 temp = (uint32_t) *vl->val.integer;
715 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", temp);
716 }
717 else if (vl->type == ASN_COUNTER64)
718 {
719 temp = (uint32_t) vl->val.counter64->high;
720 temp = temp << 32;
721 temp += (uint32_t) vl->val.counter64->low;
722 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", temp);
723 }
724 else if (vl->type == ASN_OCTET_STR)
725 {
726 /* We'll handle this later.. */
727 }
728 else
729 {
730 WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
731 defined = 0;
732 }
734 if (vl->type == ASN_OCTET_STR)
735 {
736 char *endptr;
738 endptr = NULL;
739 if (vl->val.string != NULL)
740 {
741 char string[64];
742 size_t string_length;
744 string_length = sizeof (string) - 1;
745 if (vl->val_len < string_length)
746 string_length = vl->val_len;
748 /* The strings we get from the Net-SNMP library may not be null
749 * terminated. That is why we're using `membpy' here and not `strcpy'.
750 * `string_length' is set to `vl->val_len' which holds the length of the
751 * string. -octo */
752 memcpy (string, vl->val.string, string_length);
753 string[string_length] = 0;
755 if (type == DS_TYPE_COUNTER)
756 {
757 ret.counter = (counter_t) strtoll (string, &endptr, /* base = */ 0);
758 DEBUG ("snmp plugin: csnmp_value_list_to_value: String to counter: %s -> %llu",
759 string, (unsigned long long) ret.counter);
760 }
761 else if (type == DS_TYPE_GAUGE)
762 {
763 ret.gauge = (gauge_t) strtod (string, &endptr);
764 DEBUG ("snmp plugin: csnmp_value_list_to_value: String to gauge: %s -> %g",
765 string, (double) ret.gauge);
766 }
767 }
769 /* Check if an error occurred */
770 if ((vl->val.string == NULL) || (endptr == (char *) vl->val.string))
771 {
772 if (type == DS_TYPE_COUNTER)
773 ret.counter = 0;
774 else if (type == DS_TYPE_GAUGE)
775 ret.gauge = NAN;
776 }
777 }
778 else if (type == DS_TYPE_COUNTER)
779 {
780 ret.counter = temp;
781 }
782 else if (type == DS_TYPE_GAUGE)
783 {
784 ret.gauge = NAN;
785 if (defined != 0)
786 ret.gauge = (scale * temp) + shift;
787 }
789 return (ret);
790 } /* value_t csnmp_value_list_to_value */
792 /* Returns true if all OIDs have left their subtree */
793 static int csnmp_check_res_left_subtree (const host_definition_t *host,
794 const data_definition_t *data,
795 struct snmp_pdu *res)
796 {
797 struct variable_list *vb;
798 int num_checked;
799 int num_left_subtree;
800 int i;
802 vb = res->variables;
803 if (vb == NULL)
804 return (-1);
806 num_checked = 0;
807 num_left_subtree = 0;
809 /* check all the variables and count how many have left their subtree */
810 for (vb = res->variables, i = 0;
811 (vb != NULL) && (i < data->values_len);
812 vb = vb->next_variable, i++)
813 {
814 num_checked++;
815 if (snmp_oid_ncompare (data->values[i].oid,
816 data->values[i].oid_len,
817 vb->name, vb->name_length,
818 data->values[i].oid_len) != 0)
819 num_left_subtree++;
820 }
822 /* check if enough variables have been returned */
823 if (i < data->values_len)
824 {
825 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
826 host->name, data->values_len, i);
827 return (-1);
828 }
830 if (data->instance.oid.oid_len > 0)
831 {
832 if (vb == NULL)
833 {
834 ERROR ("snmp plugin: host %s: Expected one more variable for "
835 "the instance..", host->name);
836 return (-1);
837 }
839 num_checked++;
840 if (snmp_oid_ncompare (data->instance.oid.oid,
841 data->instance.oid.oid_len,
842 vb->name, vb->name_length,
843 data->instance.oid.oid_len) != 0)
844 num_left_subtree++;
845 }
847 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
848 "left their subtree",
849 num_left_subtree, num_checked);
850 if (num_left_subtree >= num_checked)
851 return (1);
852 return (0);
853 } /* int csnmp_check_res_left_subtree */
855 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
856 csnmp_list_instances_t **tail,
857 const struct snmp_pdu *res)
858 {
859 csnmp_list_instances_t *il;
860 struct variable_list *vb;
862 /* Set vb on the last variable */
863 for (vb = res->variables;
864 (vb != NULL) && (vb->next_variable != NULL);
865 vb = vb->next_variable)
866 /* do nothing */;
867 if (vb == NULL)
868 return (-1);
870 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
871 if (il == NULL)
872 {
873 ERROR ("snmp plugin: malloc failed.");
874 return (-1);
875 }
876 il->subid = vb->name[vb->name_length - 1];
877 il->next = NULL;
879 /* Get instance name */
880 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
881 {
882 char *ptr;
883 size_t instance_len;
885 memset (il->instance, 0, sizeof (il->instance));
886 instance_len = sizeof (il->instance) - 1;
887 if (instance_len > vb->val_len)
888 instance_len = vb->val_len;
890 sstrncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
891 ? vb->val.string
892 : vb->val.bitstring),
893 instance_len + 1);
895 for (ptr = il->instance; *ptr != '\0'; ptr++)
896 {
897 if ((*ptr > 0) && (*ptr < 32))
898 *ptr = ' ';
899 else if (*ptr == '/')
900 *ptr = '_';
901 }
902 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
903 }
904 else
905 {
906 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
907 ssnprintf (il->instance, sizeof (il->instance),
908 "%llu", val.counter);
909 }
911 /* TODO: Debugging output */
913 if (*head == NULL)
914 *head = il;
915 else
916 (*tail)->next = il;
917 *tail = il;
919 return (0);
920 } /* int csnmp_instance_list_add */
922 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
923 csnmp_list_instances_t *instance_list,
924 csnmp_table_values_t **value_table)
925 {
926 const data_set_t *ds;
927 value_list_t vl = VALUE_LIST_INIT;
929 csnmp_list_instances_t *instance_list_ptr;
930 csnmp_table_values_t **value_table_ptr;
932 int i;
933 oid subid;
934 int have_more;
936 ds = plugin_get_ds (data->type);
937 if (!ds)
938 {
939 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
940 return (-1);
941 }
942 assert (ds->ds_num == data->values_len);
944 instance_list_ptr = instance_list;
946 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
947 * data->values_len);
948 if (value_table_ptr == NULL)
949 return (-1);
950 for (i = 0; i < data->values_len; i++)
951 value_table_ptr[i] = value_table[i];
953 vl.values_len = ds->ds_num;
954 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
955 if (vl.values == NULL)
956 {
957 ERROR ("snmp plugin: malloc failed.");
958 sfree (value_table_ptr);
959 return (-1);
960 }
962 sstrncpy (vl.host, host->name, sizeof (vl.host));
963 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
965 vl.interval = host->interval;
967 subid = 0;
968 have_more = 1;
970 while (have_more != 0)
971 {
972 if (instance_list != NULL)
973 {
974 while ((instance_list_ptr != NULL)
975 && (instance_list_ptr->subid < subid))
976 instance_list_ptr = instance_list_ptr->next;
978 if (instance_list_ptr == NULL)
979 {
980 have_more = 0;
981 continue;
982 }
983 else if (instance_list_ptr->subid > subid)
984 {
985 subid = instance_list_ptr->subid;
986 continue;
987 }
988 } /* if (instance_list != NULL) */
990 for (i = 0; i < data->values_len; i++)
991 {
992 while ((value_table_ptr[i] != NULL)
993 && (value_table_ptr[i]->subid < subid))
994 value_table_ptr[i] = value_table_ptr[i]->next;
996 if (value_table_ptr[i] == NULL)
997 {
998 have_more = 0;
999 break;
1000 }
1001 else if (value_table_ptr[i]->subid > subid)
1002 {
1003 subid = value_table_ptr[i]->subid;
1004 break;
1005 }
1006 } /* for (i = 0; i < columns; i++) */
1007 /* The subid has been increased - start scanning from the beginning
1008 * again.. */
1009 if (i < data->values_len)
1010 continue;
1012 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1013 * to the same subid. instance_list_ptr is either NULL or points to the
1014 * same subid, too. */
1015 #if COLLECT_DEBUG
1016 for (i = 1; i < data->values_len; i++)
1017 {
1018 assert (value_table_ptr[i] != NULL);
1019 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
1020 }
1021 assert ((instance_list_ptr == NULL)
1022 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
1023 #endif
1025 sstrncpy (vl.type, data->type, sizeof (vl.type));
1027 {
1028 char temp[DATA_MAX_NAME_LEN];
1030 if (instance_list_ptr == NULL)
1031 ssnprintf (temp, sizeof (temp), "%u", (uint32_t) subid);
1032 else
1033 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1035 if (data->instance_prefix == NULL)
1036 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1037 else
1038 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1039 data->instance_prefix, temp);
1040 }
1042 for (i = 0; i < data->values_len; i++)
1043 vl.values[i] = value_table_ptr[i]->value;
1045 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1046 plugin_dispatch_values (&vl);
1048 subid++;
1049 } /* while (have_more != 0) */
1051 sfree (vl.values);
1052 sfree (value_table_ptr);
1054 return (0);
1055 } /* int csnmp_dispatch_table */
1057 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1058 {
1059 struct snmp_pdu *req;
1060 struct snmp_pdu *res;
1061 struct variable_list *vb;
1063 const data_set_t *ds;
1064 oid_t *oid_list;
1065 uint32_t oid_list_len;
1067 int status;
1068 int i;
1070 /* `value_table' and `value_table_ptr' implement a linked list for each
1071 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1072 * instance names. This is used to jump gaps in the table. */
1073 csnmp_list_instances_t *instance_list;
1074 csnmp_list_instances_t *instance_list_ptr;
1075 csnmp_table_values_t **value_table;
1076 csnmp_table_values_t **value_table_ptr;
1078 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1079 host->name, data->name);
1081 if (host->sess_handle == NULL)
1082 {
1083 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1084 return (-1);
1085 }
1087 ds = plugin_get_ds (data->type);
1088 if (!ds)
1089 {
1090 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1091 return (-1);
1092 }
1094 if (ds->ds_num != data->values_len)
1095 {
1096 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1097 data->type, ds->ds_num, data->values_len);
1098 return (-1);
1099 }
1101 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1102 oid_list_len = data->values_len + 1;
1103 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1104 if (oid_list == NULL)
1105 {
1106 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1107 return (-1);
1108 }
1109 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1110 if (data->instance.oid.oid_len > 0)
1111 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1112 else
1113 oid_list_len--;
1115 /* Allocate the `value_table' */
1116 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1117 * 2 * data->values_len);
1118 if (value_table == NULL)
1119 {
1120 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1121 sfree (oid_list);
1122 return (-1);
1123 }
1124 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1125 value_table_ptr = value_table + data->values_len;
1127 instance_list = NULL;
1128 instance_list_ptr = NULL;
1130 status = 0;
1131 while (status == 0)
1132 {
1133 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1134 if (req == NULL)
1135 {
1136 ERROR ("snmp plugin: snmp_pdu_create failed.");
1137 status = -1;
1138 break;
1139 }
1141 for (i = 0; (uint32_t) i < oid_list_len; i++)
1142 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1144 res = NULL;
1145 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1147 if ((status != STAT_SUCCESS) || (res == NULL))
1148 {
1149 char *errstr = NULL;
1151 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1153 c_complain (LOG_ERR, &host->complaint,
1154 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1155 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1157 if (res != NULL)
1158 snmp_free_pdu (res);
1159 res = NULL;
1161 sfree (errstr);
1162 csnmp_host_close_session (host);
1164 status = -1;
1165 break;
1166 }
1167 status = 0;
1168 assert (res != NULL);
1169 c_release (LOG_INFO, &host->complaint,
1170 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1171 host->name);
1173 vb = res->variables;
1174 if (vb == NULL)
1175 {
1176 status = -1;
1177 break;
1178 }
1180 /* Check if all values (and possibly the instance) have left their
1181 * subtree */
1182 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1183 {
1184 status = 0;
1185 break;
1186 }
1188 /* if an instance-OID is configured.. */
1189 if (data->instance.oid.oid_len > 0)
1190 {
1191 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1192 * add it to the list */
1193 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1194 res) != 0)
1195 {
1196 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1197 status = -1;
1198 break;
1199 }
1201 /* Set vb on the last variable */
1202 for (vb = res->variables;
1203 (vb != NULL) && (vb->next_variable != NULL);
1204 vb = vb->next_variable)
1205 /* do nothing */;
1206 assert (vb != NULL);
1208 /* Copy OID to oid_list[data->values_len] */
1209 memcpy (oid_list[data->values_len].oid, vb->name,
1210 sizeof (oid) * vb->name_length);
1211 oid_list[data->values_len].oid_len = vb->name_length;
1212 }
1214 for (vb = res->variables, i = 0;
1215 (vb != NULL) && (i < data->values_len);
1216 vb = vb->next_variable, i++)
1217 {
1218 csnmp_table_values_t *vt;
1220 /* Check if we left the subtree */
1221 if (snmp_oid_ncompare (data->values[i].oid,
1222 data->values[i].oid_len,
1223 vb->name, vb->name_length,
1224 data->values[i].oid_len) != 0)
1225 {
1226 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1227 host->name, data->name, i);
1228 continue;
1229 }
1231 if ((value_table_ptr[i] != NULL)
1232 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1233 {
1234 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1235 "SUBID is not increasing.",
1236 host->name, data->name, i);
1237 continue;
1238 }
1240 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1241 if (vt == NULL)
1242 {
1243 ERROR ("snmp plugin: malloc failed.");
1244 status = -1;
1245 break;
1246 }
1248 vt->subid = vb->name[vb->name_length - 1];
1249 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1250 data->scale, data->shift);
1251 vt->next = NULL;
1253 if (value_table_ptr[i] == NULL)
1254 value_table[i] = vt;
1255 else
1256 value_table_ptr[i]->next = vt;
1257 value_table_ptr[i] = vt;
1259 /* Copy OID to oid_list[i + 1] */
1260 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1261 oid_list[i].oid_len = vb->name_length;
1262 } /* for (i = data->values_len) */
1264 if (res != NULL)
1265 snmp_free_pdu (res);
1266 res = NULL;
1267 } /* while (status == 0) */
1269 if (res != NULL)
1270 snmp_free_pdu (res);
1271 res = NULL;
1273 if (status == 0)
1274 csnmp_dispatch_table (host, data, instance_list, value_table);
1276 /* Free all allocated variables here */
1277 while (instance_list != NULL)
1278 {
1279 instance_list_ptr = instance_list->next;
1280 sfree (instance_list);
1281 instance_list = instance_list_ptr;
1282 }
1284 for (i = 0; i < data->values_len; i++)
1285 {
1286 csnmp_table_values_t *tmp;
1287 while (value_table[i] != NULL)
1288 {
1289 tmp = value_table[i]->next;
1290 sfree (value_table[i]);
1291 value_table[i] = tmp;
1292 }
1293 }
1295 sfree (value_table);
1296 sfree (oid_list);
1298 return (0);
1299 } /* int csnmp_read_table */
1301 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1302 {
1303 struct snmp_pdu *req;
1304 struct snmp_pdu *res;
1305 struct variable_list *vb;
1307 const data_set_t *ds;
1308 value_list_t vl = VALUE_LIST_INIT;
1310 int status;
1311 int i;
1313 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1314 host->name, data->name);
1316 if (host->sess_handle == NULL)
1317 {
1318 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1319 return (-1);
1320 }
1322 ds = plugin_get_ds (data->type);
1323 if (!ds)
1324 {
1325 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1326 return (-1);
1327 }
1329 if (ds->ds_num != data->values_len)
1330 {
1331 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1332 data->type, ds->ds_num, data->values_len);
1333 return (-1);
1334 }
1336 vl.values_len = ds->ds_num;
1337 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1338 if (vl.values == NULL)
1339 return (-1);
1340 for (i = 0; i < vl.values_len; i++)
1341 {
1342 if (ds->ds[i].type == DS_TYPE_COUNTER)
1343 vl.values[i].counter = 0;
1344 else
1345 vl.values[i].gauge = NAN;
1346 }
1348 sstrncpy (vl.host, host->name, sizeof (vl.host));
1349 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1350 sstrncpy (vl.type, data->type, sizeof (vl.type));
1351 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1353 vl.interval = host->interval;
1355 req = snmp_pdu_create (SNMP_MSG_GET);
1356 if (req == NULL)
1357 {
1358 ERROR ("snmp plugin: snmp_pdu_create failed.");
1359 sfree (vl.values);
1360 return (-1);
1361 }
1363 for (i = 0; i < data->values_len; i++)
1364 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1366 res = NULL;
1367 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1369 if ((status != STAT_SUCCESS) || (res == NULL))
1370 {
1371 char *errstr = NULL;
1373 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1374 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1375 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1377 if (res != NULL)
1378 snmp_free_pdu (res);
1379 res = NULL;
1381 sfree (errstr);
1382 csnmp_host_close_session (host);
1384 return (-1);
1385 }
1388 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1389 {
1390 #if COLLECT_DEBUG
1391 char buffer[1024];
1392 snprint_variable (buffer, sizeof (buffer),
1393 vb->name, vb->name_length, vb);
1394 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1395 #endif /* COLLECT_DEBUG */
1397 for (i = 0; i < data->values_len; i++)
1398 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1399 vb->name, vb->name_length) == 0)
1400 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1401 data->scale, data->shift);
1402 } /* for (res->variables) */
1404 if (res != NULL)
1405 snmp_free_pdu (res);
1406 res = NULL;
1408 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1409 plugin_dispatch_values (&vl);
1410 sfree (vl.values);
1412 return (0);
1413 } /* int csnmp_read_value */
1415 static int csnmp_read_host (host_definition_t *host)
1416 {
1417 int i;
1418 time_t time_start;
1419 time_t time_end;
1421 time_start = time (NULL);
1422 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1423 (unsigned int) time_start);
1425 if (host->sess_handle == NULL)
1426 csnmp_host_open_session (host);
1428 if (host->sess_handle == NULL)
1429 return (-1);
1431 for (i = 0; i < host->data_list_len; i++)
1432 {
1433 data_definition_t *data = host->data_list[i];
1435 if (data->is_table)
1436 csnmp_read_table (host, data);
1437 else
1438 csnmp_read_value (host, data);
1439 }
1441 time_end = time (NULL);
1442 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1443 (unsigned int) time_end);
1444 if ((uint32_t) (time_end - time_start) > host->interval)
1445 {
1446 WARNING ("snmp plugin: Host `%s' should be queried every %i seconds, "
1447 "but reading all values takes %u seconds.",
1448 host->name, host->interval, (unsigned int) (time_end - time_start));
1449 }
1451 return (0);
1452 } /* int csnmp_read_host */
1454 static void *csnmp_read_thread (void __attribute__((unused)) *data)
1455 {
1456 host_definition_t *host;
1458 pthread_mutex_lock (&host_lock);
1459 while (do_shutdown == 0)
1460 {
1461 pthread_cond_wait (&host_cond, &host_lock);
1463 for (host = host_head; host != NULL; host = host->next)
1464 {
1465 if (do_shutdown != 0)
1466 break;
1467 if (host->state != STATE_WAIT)
1468 continue;
1470 host->state = STATE_BUSY;
1471 pthread_mutex_unlock (&host_lock);
1472 csnmp_read_host (host);
1473 pthread_mutex_lock (&host_lock);
1474 host->state = STATE_IDLE;
1475 } /* for (host) */
1476 } /* while (do_shutdown == 0) */
1477 pthread_mutex_unlock (&host_lock);
1479 pthread_exit ((void *) 0);
1480 return ((void *) 0);
1481 } /* void *csnmp_read_thread */
1483 static int csnmp_init (void)
1484 {
1485 host_definition_t *host;
1486 int i;
1488 if (host_head == NULL)
1489 {
1490 NOTICE ("snmp plugin: No host has been defined.");
1491 return (-1);
1492 }
1494 call_snmp_init_once ();
1496 threads_num = 0;
1497 for (host = host_head; host != NULL; host = host->next)
1498 {
1499 threads_num++;
1500 /* We need to initialize `interval' here, because `interval_g' isn't
1501 * initialized during `configure'. */
1502 host->next_update = time (NULL);
1503 if (host->interval == 0)
1504 {
1505 host->interval = interval_g;
1506 }
1507 else if (host->interval < (uint32_t) interval_g)
1508 {
1509 host->interval = interval_g;
1510 WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1511 host->name, host->interval);
1512 }
1514 csnmp_host_open_session (host);
1515 } /* for (host) */
1517 /* Now start the reading threads */
1518 if (threads_num > 3)
1519 {
1520 threads_num = 3 + ((threads_num - 3) / 10);
1521 if (threads_num > 10)
1522 threads_num = 10;
1523 }
1525 threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1526 if (threads == NULL)
1527 {
1528 ERROR ("snmp plugin: malloc failed.");
1529 return (-1);
1530 }
1531 memset (threads, '\0', threads_num * sizeof (pthread_t));
1533 for (i = 0; i < threads_num; i++)
1534 pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1536 return (0);
1537 } /* int csnmp_init */
1539 static int csnmp_read (void)
1540 {
1541 host_definition_t *host;
1542 time_t now;
1544 if (host_head == NULL)
1545 {
1546 INFO ("snmp plugin: No hosts configured.");
1547 return (-1);
1548 }
1550 now = time (NULL);
1552 pthread_mutex_lock (&host_lock);
1553 for (host = host_head; host != NULL; host = host->next)
1554 {
1555 if (host->state != STATE_IDLE)
1556 continue;
1558 /* Skip this host if the next or a later iteration will be sufficient. */
1559 if (host->next_update >= (now + interval_g))
1560 continue;
1562 host->state = STATE_WAIT;
1563 host->next_update = now + host->interval;
1564 } /* for (host) */
1566 pthread_cond_broadcast (&host_cond);
1567 pthread_mutex_unlock (&host_lock);
1569 return (0);
1570 } /* int csnmp_read */
1572 static int csnmp_shutdown (void)
1573 {
1574 host_definition_t *host_this;
1575 host_definition_t *host_next;
1577 data_definition_t *data_this;
1578 data_definition_t *data_next;
1580 int i;
1582 pthread_mutex_lock (&host_lock);
1583 do_shutdown = 1;
1584 pthread_cond_broadcast (&host_cond);
1585 pthread_mutex_unlock (&host_lock);
1587 for (i = 0; i < threads_num; i++)
1588 pthread_join (threads[i], NULL);
1590 /* Now that all the threads have exited, let's free all the global variables.
1591 * This isn't really neccessary, I guess, but I think it's good stile to do
1592 * so anyway. */
1593 host_this = host_head;
1594 host_head = NULL;
1595 while (host_this != NULL)
1596 {
1597 host_next = host_this->next;
1599 csnmp_host_close_session (host_this);
1601 sfree (host_this->name);
1602 sfree (host_this->address);
1603 sfree (host_this->community);
1604 sfree (host_this->data_list);
1605 sfree (host_this);
1607 host_this = host_next;
1608 }
1610 data_this = data_head;
1611 data_head = NULL;
1612 while (data_this != NULL)
1613 {
1614 data_next = data_this->next;
1616 sfree (data_this->name);
1617 sfree (data_this->type);
1618 sfree (data_this->values);
1619 sfree (data_this);
1621 data_this = data_next;
1622 }
1624 return (0);
1625 } /* int csnmp_shutdown */
1627 void module_register (void)
1628 {
1629 plugin_register_complex_config ("snmp", csnmp_config);
1630 plugin_register_init ("snmp", csnmp_init);
1631 plugin_register_read ("snmp", csnmp_read);
1632 plugin_register_shutdown ("snmp", csnmp_shutdown);
1633 } /* void module_register */
1635 /*
1636 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1637 */