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