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