snmp plugin: Fix a possible memory leak.
[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   if (host->sess_handle == NULL)
660     return;
661
662   snmp_sess_close (host->sess_handle);
663   host->sess_handle = NULL;
664 } /* void csnmp_host_close_session */
665
666 static void csnmp_host_open_session (host_definition_t *host)
667 {
668   struct snmp_session sess;
669
670   if (host->sess_handle != NULL)
671     csnmp_host_close_session (host);
672
673   snmp_sess_init (&sess);
674   sess.peername = host->address;
675   sess.community = (u_char *) host->community;
676   sess.community_len = strlen (host->community);
677   sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
678
679   /* snmp_sess_open will copy the `struct snmp_session *'. */
680   host->sess_handle = snmp_sess_open (&sess);
681
682   if (host->sess_handle == NULL)
683   {
684     char *errstr = NULL;
685
686     snmp_error (&sess, NULL, NULL, &errstr);
687
688     ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
689         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
690     sfree (errstr);
691   }
692 } /* void csnmp_host_open_session */
693
694 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
695     double scale, double shift)
696 {
697   value_t ret;
698   uint64_t temp = 0;
699   int defined = 1;
700
701   if ((vl->type == ASN_INTEGER)
702       || (vl->type == ASN_UINTEGER)
703       || (vl->type == ASN_COUNTER)
704 #ifdef ASN_TIMETICKS
705       || (vl->type == ASN_TIMETICKS)
706 #endif
707       || (vl->type == ASN_GAUGE))
708   {
709     temp = (uint32_t) *vl->val.integer;
710     DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", temp);
711   }
712   else if (vl->type == ASN_COUNTER64)
713   {
714     temp = (uint32_t) vl->val.counter64->high;
715     temp = temp << 32;
716     temp += (uint32_t) vl->val.counter64->low;
717     DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", temp);
718   }
719   else
720   {
721     WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
722     defined = 0;
723   }
724
725   if (type == DS_TYPE_COUNTER)
726   {
727     ret.counter = temp;
728   }
729   else if (type == DS_TYPE_GAUGE)
730   {
731     ret.gauge = NAN;
732     if (defined != 0)
733       ret.gauge = (scale * temp) + shift;
734   }
735
736   return (ret);
737 } /* value_t csnmp_value_list_to_value */
738
739 /* Returns true if all OIDs have left their subtree */
740 static int csnmp_check_res_left_subtree (const host_definition_t *host,
741     const data_definition_t *data,
742     struct snmp_pdu *res)
743 {
744   struct variable_list *vb;
745   int num_checked;
746   int num_left_subtree;
747   int i;
748
749   vb = res->variables;
750   if (vb == NULL)
751     return (-1);
752
753   num_checked = 0;
754   num_left_subtree = 0;
755
756   /* check all the variables and count how many have left their subtree */
757   for (vb = res->variables, i = 0;
758       (vb != NULL) && (i < data->values_len);
759       vb = vb->next_variable, i++)
760   {
761     num_checked++;
762     if (snmp_oid_ncompare (data->values[i].oid,
763           data->values[i].oid_len,
764           vb->name, vb->name_length,
765           data->values[i].oid_len) != 0)
766       num_left_subtree++;
767   }
768
769   /* check if enough variables have been returned */
770   if (i < data->values_len)
771   {
772     ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
773         host->name, data->values_len, i);
774     return (-1);
775   }
776
777   if (data->instance.oid.oid_len > 0)
778   {
779     if (vb == NULL)
780     {
781       ERROR ("snmp plugin: host %s: Expected one more variable for "
782           "the instance..", host->name);
783       return (-1);
784     }
785
786     num_checked++;
787     if (snmp_oid_ncompare (data->instance.oid.oid,
788           data->instance.oid.oid_len,
789           vb->name, vb->name_length,
790           data->instance.oid.oid_len) != 0)
791       num_left_subtree++;
792   }
793
794   DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
795       "left their subtree",
796       num_left_subtree, num_checked);
797   if (num_left_subtree >= num_checked)
798     return (1);
799   return (0);
800 } /* int csnmp_check_res_left_subtree */
801
802 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
803     csnmp_list_instances_t **tail,
804     const struct snmp_pdu *res)
805 {
806   csnmp_list_instances_t *il;
807   struct variable_list *vb;
808
809   /* Set vb on the last variable */
810   for (vb = res->variables;
811       (vb != NULL) && (vb->next_variable != NULL);
812       vb = vb->next_variable)
813     /* do nothing */;
814   if (vb == NULL)
815     return (-1);
816
817   il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
818   if (il == NULL)
819   {
820     ERROR ("snmp plugin: malloc failed.");
821     return (-1);
822   }
823   il->subid = vb->name[vb->name_length - 1];
824   il->next = NULL;
825
826   /* Get instance name */
827   if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
828   {
829     char *ptr;
830     size_t instance_len;
831
832     instance_len = sizeof (il->instance) - 1;
833     if (instance_len > vb->val_len)
834       instance_len = vb->val_len;
835
836     strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
837           ? vb->val.string
838           : vb->val.bitstring),
839         instance_len);
840     il->instance[instance_len] = '\0';
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     snprintf (il->instance, sizeof (il->instance),
855         "%llu", val.counter);
856   }
857   il->instance[sizeof (il->instance) - 1] = '\0';
858
859   /* TODO: Debugging output */
860
861   if (*head == NULL)
862     *head = il;
863   else
864     (*tail)->next = il;
865   *tail = il;
866
867   return (0);
868 } /* int csnmp_instance_list_add */
869
870 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
871     csnmp_list_instances_t *instance_list,
872     csnmp_table_values_t **value_table)
873 {
874   const data_set_t *ds;
875   value_list_t vl = VALUE_LIST_INIT;
876
877   csnmp_list_instances_t *instance_list_ptr;
878   csnmp_table_values_t **value_table_ptr;
879
880   int i;
881   oid subid;
882   int have_more;
883
884   ds = plugin_get_ds (data->type);
885   if (!ds)
886   {
887     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
888     return (-1);
889   }
890   assert (ds->ds_num == data->values_len);
891
892   instance_list_ptr = instance_list;
893
894   value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
895       * data->values_len);
896   if (value_table_ptr == NULL)
897     return (-1);
898   for (i = 0; i < data->values_len; i++)
899     value_table_ptr[i] = value_table[i];
900
901   vl.values_len = ds->ds_num;
902   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
903   if (vl.values == NULL)
904   {
905     ERROR ("snmp plugin: malloc failed.");
906     sfree (value_table_ptr);
907     return (-1);
908   }
909
910   strncpy (vl.host, host->name, sizeof (vl.host));
911   vl.host[sizeof (vl.host) - 1] = '\0';
912   sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
913
914   vl.interval = host->interval;
915   vl.time = time (NULL);
916
917   subid = 0;
918   have_more = 1;
919
920   while (have_more != 0)
921   {
922     if (instance_list != NULL)
923     {
924       while ((instance_list_ptr != NULL)
925           && (instance_list_ptr->subid < subid))
926         instance_list_ptr = instance_list_ptr->next;
927
928       if (instance_list_ptr == NULL)
929       {
930         have_more = 0;
931         continue;
932       }
933       else if (instance_list_ptr->subid > subid)
934       {
935         subid = instance_list_ptr->subid;
936         continue;
937       }
938     } /* if (instance_list != NULL) */
939
940     for (i = 0; i < data->values_len; i++)
941     {
942       while ((value_table_ptr[i] != NULL)
943           && (value_table_ptr[i]->subid < subid))
944         value_table_ptr[i] = value_table_ptr[i]->next;
945
946       if (value_table_ptr[i] == NULL)
947       {
948         have_more = 0;
949         break;
950       }
951       else if (value_table_ptr[i]->subid > subid)
952       {
953         subid = value_table_ptr[i]->subid;
954         break;
955       }
956     } /* for (i = 0; i < columns; i++) */
957     /* The subid has been increased - start scanning from the beginning
958      * again.. */
959     if (i < data->values_len)
960       continue;
961
962     /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
963      * to the same subid. instance_list_ptr is either NULL or points to the
964      * same subid, too. */
965 #if COLLECT_DEBUG
966     for (i = 1; i < data->values_len; i++)
967     {
968       assert (value_table_ptr[i] != NULL);
969       assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
970     }
971     assert ((instance_list_ptr == NULL)
972         || (instance_list_ptr->subid == value_table_ptr[0]->subid));
973 #endif
974
975     {
976       char temp[DATA_MAX_NAME_LEN];
977
978       if (instance_list_ptr == NULL)
979         snprintf (temp, sizeof (temp), "%u",
980             (uint32_t) subid);
981       else
982         strncpy (temp, instance_list_ptr->instance,
983             sizeof (temp));
984       temp[sizeof (temp) - 1] = '\0';
985
986       if (data->instance_prefix == NULL)
987         strncpy (vl.type_instance, temp, sizeof (vl.type_instance));
988       else
989         snprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
990             data->instance_prefix, temp);
991       vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
992     }
993
994     for (i = 0; i < data->values_len; i++)
995       vl.values[i] = value_table_ptr[i]->value;
996
997     /* If we get here `vl.type_instance' and all `vl.values' have been set */
998     plugin_dispatch_values (data->type, &vl);
999
1000     subid++;
1001   } /* while (have_more != 0) */
1002
1003   sfree (vl.values);
1004   sfree (value_table_ptr);
1005
1006   return (0);
1007 } /* int csnmp_dispatch_table */
1008
1009 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1010 {
1011   struct snmp_pdu *req;
1012   struct snmp_pdu *res;
1013   struct variable_list *vb;
1014
1015   const data_set_t *ds;
1016   oid_t *oid_list;
1017   uint32_t oid_list_len;
1018
1019   int status;
1020   int i;
1021
1022   /* `value_table' and `value_table_ptr' implement a linked list for each
1023    * value. `instance_list' and `instance_list_ptr' implement a linked list of
1024    * instance names. This is used to jump gaps in the table. */
1025   csnmp_list_instances_t *instance_list;
1026   csnmp_list_instances_t *instance_list_ptr;
1027   csnmp_table_values_t **value_table;
1028   csnmp_table_values_t **value_table_ptr;
1029
1030   DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1031       host->name, data->name);
1032
1033   if (host->sess_handle == NULL)
1034   {
1035     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1036     return (-1);
1037   }
1038
1039   ds = plugin_get_ds (data->type);
1040   if (!ds)
1041   {
1042     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1043     return (-1);
1044   }
1045
1046   if (ds->ds_num != data->values_len)
1047   {
1048     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1049         data->type, ds->ds_num, data->values_len);
1050     return (-1);
1051   }
1052
1053   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1054   oid_list_len = data->values_len + 1;
1055   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1056   if (oid_list == NULL)
1057   {
1058     ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1059     return (-1);
1060   }
1061   memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1062   if (data->instance.oid.oid_len > 0)
1063     memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1064   else
1065     oid_list_len--;
1066
1067   /* Allocate the `value_table' */
1068   value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1069       * 2 * data->values_len);
1070   if (value_table == NULL)
1071   {
1072     ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1073     sfree (oid_list);
1074     return (-1);
1075   }
1076   memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1077   value_table_ptr = value_table + data->values_len;
1078   
1079   instance_list = NULL;
1080   instance_list_ptr = NULL;
1081
1082   status = 0;
1083   while (status == 0)
1084   {
1085     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1086     if (req == NULL)
1087     {
1088       ERROR ("snmp plugin: snmp_pdu_create failed.");
1089       status = -1;
1090       break;
1091     }
1092
1093     for (i = 0; i < oid_list_len; i++)
1094       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1095
1096     res = NULL;
1097     status = snmp_sess_synch_response (host->sess_handle, req, &res);
1098
1099     if ((status != STAT_SUCCESS) || (res == NULL))
1100     {
1101       char *errstr = NULL;
1102
1103       snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1104       ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1105           host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1106
1107       if (res != NULL)
1108         snmp_free_pdu (res);
1109       res = NULL;
1110
1111       sfree (errstr);
1112       csnmp_host_close_session (host);
1113
1114       status = -1;
1115       break;
1116     }
1117     status = 0;
1118     assert (res != NULL);
1119
1120     vb = res->variables;
1121     if (vb == 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       status = 0;
1132       break;
1133     }
1134
1135     /* if an instance-OID is configured.. */
1136     if (data->instance.oid.oid_len > 0)
1137     {
1138       /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1139        * add it to the list */
1140       if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1141             res) != 0)
1142       {
1143         ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1144         status = -1;
1145         break;
1146       }
1147
1148       /* Set vb on the last variable */
1149       for (vb = res->variables;
1150           (vb != NULL) && (vb->next_variable != NULL);
1151           vb = vb->next_variable)
1152         /* do nothing */;
1153       assert (vb != NULL);
1154
1155       /* Copy OID to oid_list[data->values_len] */
1156       memcpy (oid_list[data->values_len].oid, vb->name,
1157           sizeof (oid) * vb->name_length);
1158       oid_list[data->values_len].oid_len = vb->name_length;
1159     }
1160
1161     for (vb = res->variables, i = 0;
1162         (vb != NULL) && (i < data->values_len);
1163         vb = vb->next_variable, i++)
1164     {
1165       csnmp_table_values_t *vt;
1166
1167       /* Check if we left the subtree */
1168       if (snmp_oid_ncompare (data->values[i].oid,
1169             data->values[i].oid_len,
1170             vb->name, vb->name_length,
1171             data->values[i].oid_len) != 0)
1172       {
1173         DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1174             host->name, data->name, i);
1175         continue;
1176       }
1177
1178       if ((value_table_ptr[i] != NULL)
1179           && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1180       {
1181         DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1182             "SUBID is not increasing.",
1183             host->name, data->name, i);
1184         continue;
1185       }
1186
1187       vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1188       if (vt == NULL)
1189       {
1190         ERROR ("snmp plugin: malloc failed.");
1191         status = -1;
1192         break;
1193       }
1194
1195       vt->subid = vb->name[vb->name_length - 1];
1196       vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1197           data->scale, data->shift);
1198       vt->next = NULL;
1199
1200       if (value_table_ptr[i] == NULL)
1201         value_table[i] = vt;
1202       else
1203         value_table_ptr[i]->next = vt;
1204       value_table_ptr[i] = vt;
1205
1206       /* Copy OID to oid_list[i + 1] */
1207       memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1208       oid_list[i].oid_len = vb->name_length;
1209     } /* for (i = data->values_len) */
1210
1211     if (res != NULL)
1212       snmp_free_pdu (res);
1213     res = NULL;
1214   } /* while (status == 0) */
1215
1216   if (res != NULL)
1217     snmp_free_pdu (res);
1218   res = NULL;
1219
1220   if (status == 0)
1221     csnmp_dispatch_table (host, data, instance_list, value_table);
1222
1223   /* Free all allocated variables here */
1224   while (instance_list != NULL)
1225   {
1226     instance_list_ptr = instance_list->next;
1227     sfree (instance_list);
1228     instance_list = instance_list_ptr;
1229   }
1230
1231   for (i = 0; i < data->values_len; i++)
1232   {
1233     csnmp_table_values_t *tmp;
1234     while (value_table[i] != NULL)
1235     {
1236       tmp = value_table[i]->next;
1237       sfree (value_table[i]);
1238       value_table[i] = tmp;
1239     }
1240   }
1241
1242   sfree (value_table);
1243   sfree (oid_list);
1244
1245   return (0);
1246 } /* int csnmp_read_table */
1247
1248 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1249 {
1250   struct snmp_pdu *req;
1251   struct snmp_pdu *res;
1252   struct variable_list *vb;
1253
1254   const data_set_t *ds;
1255   value_list_t vl = VALUE_LIST_INIT;
1256
1257   int status;
1258   int i;
1259
1260   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1261       host->name, data->name);
1262
1263   if (host->sess_handle == NULL)
1264   {
1265     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1266     return (-1);
1267   }
1268
1269   ds = plugin_get_ds (data->type);
1270   if (!ds)
1271   {
1272     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1273     return (-1);
1274   }
1275
1276   if (ds->ds_num != data->values_len)
1277   {
1278     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1279         data->type, ds->ds_num, data->values_len);
1280     return (-1);
1281   }
1282
1283   vl.values_len = ds->ds_num;
1284   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1285   if (vl.values == NULL)
1286     return (-1);
1287   for (i = 0; i < vl.values_len; i++)
1288   {
1289     if (ds->ds[i].type == DS_TYPE_COUNTER)
1290       vl.values[i].counter = 0;
1291     else
1292       vl.values[i].gauge = NAN;
1293   }
1294
1295   strncpy (vl.host, host->name, sizeof (vl.host));
1296   vl.host[sizeof (vl.host) - 1] = '\0';
1297   sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1298   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1299   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1300
1301   vl.interval = host->interval;
1302
1303   req = snmp_pdu_create (SNMP_MSG_GET);
1304   if (req == NULL)
1305   {
1306     ERROR ("snmp plugin: snmp_pdu_create failed.");
1307     sfree (vl.values);
1308     return (-1);
1309   }
1310
1311   for (i = 0; i < data->values_len; i++)
1312     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1313
1314   res = NULL;
1315   status = snmp_sess_synch_response (host->sess_handle, req, &res);
1316
1317   if ((status != STAT_SUCCESS) || (res == NULL))
1318   {
1319     char *errstr = NULL;
1320
1321     snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1322     ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1323         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1324
1325     if (res != NULL)
1326       snmp_free_pdu (res);
1327     res = NULL;
1328
1329     sfree (errstr);
1330     csnmp_host_close_session (host);
1331
1332     return (-1);
1333   }
1334
1335   vl.time = time (NULL);
1336
1337   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1338   {
1339 #if COLLECT_DEBUG
1340     char buffer[1024];
1341     snprint_variable (buffer, sizeof (buffer),
1342         vb->name, vb->name_length, vb);
1343     DEBUG ("snmp plugin: Got this variable: %s", buffer);
1344 #endif /* COLLECT_DEBUG */
1345
1346     for (i = 0; i < data->values_len; i++)
1347       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1348             vb->name, vb->name_length) == 0)
1349         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1350             data->scale, data->shift);
1351   } /* for (res->variables) */
1352
1353   if (res != NULL)
1354     snmp_free_pdu (res);
1355   res = NULL;
1356
1357   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1358   plugin_dispatch_values (data->type, &vl);
1359   sfree (vl.values);
1360
1361   return (0);
1362 } /* int csnmp_read_value */
1363
1364 static int csnmp_read_host (host_definition_t *host)
1365 {
1366   int i;
1367   time_t time_start;
1368   time_t time_end;
1369
1370   time_start = time (NULL);
1371   DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1372       (unsigned int) time_start);
1373
1374   if (host->sess_handle == NULL)
1375     csnmp_host_open_session (host);
1376
1377   if (host->sess_handle == NULL)
1378     return (-1);
1379
1380   for (i = 0; i < host->data_list_len; i++)
1381   {
1382     data_definition_t *data = host->data_list[i];
1383
1384     if (data->is_table)
1385       csnmp_read_table (host, data);
1386     else
1387       csnmp_read_value (host, data);
1388   }
1389
1390   time_end = time (NULL);
1391   DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1392       (unsigned int) time_end);
1393   if ((time_end - time_start) > host->interval)
1394   {
1395     WARNING ("snmp plugin: Host `%s' should be queried every %i seconds, "
1396         "but reading all values takes %u seconds.",
1397         host->name, host->interval, (unsigned int) (time_end - time_start));
1398   }
1399
1400   return (0);
1401 } /* int csnmp_read_host */
1402
1403 static void *csnmp_read_thread (void *data)
1404 {
1405   host_definition_t *host;
1406
1407   pthread_mutex_lock (&host_lock);
1408   while (do_shutdown == 0)
1409   {
1410     pthread_cond_wait (&host_cond, &host_lock);
1411
1412     for (host = host_head; host != NULL; host = host->next)
1413     {
1414       if (do_shutdown != 0)
1415         break;
1416       if (host->state != STATE_WAIT)
1417         continue;
1418
1419       host->state = STATE_BUSY;
1420       pthread_mutex_unlock (&host_lock);
1421       csnmp_read_host (host);
1422       pthread_mutex_lock (&host_lock);
1423       host->state = STATE_IDLE;
1424     } /* for (host) */
1425   } /* while (do_shutdown == 0) */
1426   pthread_mutex_unlock (&host_lock);
1427
1428   pthread_exit ((void *) 0);
1429   return ((void *) 0);
1430 } /* void *csnmp_read_thread */
1431
1432 static int csnmp_init (void)
1433 {
1434   host_definition_t *host;
1435   int i;
1436
1437   if (host_head == NULL)
1438   {
1439     NOTICE ("snmp plugin: No host has been defined.");
1440     return (-1);
1441   }
1442
1443   call_snmp_init_once ();
1444
1445   threads_num = 0;
1446   for (host = host_head; host != NULL; host = host->next)
1447   {
1448     threads_num++;
1449     /* We need to initialize `interval' here, because `interval_g' isn't
1450      * initialized during `configure'. */
1451     host->next_update = time (NULL);
1452     if (host->interval == 0)
1453     {
1454       host->interval = interval_g;
1455     }
1456     else if (host->interval < interval_g)
1457     {
1458       host->interval = interval_g;
1459       WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1460           host->name, host->interval);
1461     }
1462
1463     csnmp_host_open_session (host);
1464   } /* for (host) */
1465
1466   /* Now start the reading threads */
1467   if (threads_num > 3)
1468   {
1469     threads_num = 3 + ((threads_num - 3) / 10);
1470     if (threads_num > 10)
1471       threads_num = 10;
1472   }
1473
1474   threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1475   if (threads == NULL)
1476   {
1477     ERROR ("snmp plugin: malloc failed.");
1478     return (-1);
1479   }
1480   memset (threads, '\0', threads_num * sizeof (pthread_t));
1481
1482   for (i = 0; i < threads_num; i++)
1483       pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1484
1485   return (0);
1486 } /* int csnmp_init */
1487
1488 static int csnmp_read (void)
1489 {
1490   host_definition_t *host;
1491   time_t now;
1492
1493   if (host_head == NULL)
1494   {
1495     INFO ("snmp plugin: No hosts configured.");
1496     return (-1);
1497   }
1498
1499   now = time (NULL);
1500
1501   pthread_mutex_lock (&host_lock);
1502   for (host = host_head; host != NULL; host = host->next)
1503   {
1504     if (host->state != STATE_IDLE)
1505       continue;
1506
1507     /* Skip this host if the next or a later iteration will be sufficient. */
1508     if (host->next_update >= (now + interval_g))
1509       continue;
1510
1511     host->state = STATE_WAIT;
1512     host->next_update = now + host->interval;
1513   } /* for (host) */
1514
1515   pthread_cond_broadcast (&host_cond);
1516   pthread_mutex_unlock (&host_lock);
1517
1518   return (0);
1519 } /* int csnmp_read */
1520
1521 static int csnmp_shutdown (void)
1522 {
1523   host_definition_t *host_this;
1524   host_definition_t *host_next;
1525
1526   data_definition_t *data_this;
1527   data_definition_t *data_next;
1528
1529   int i;
1530
1531   pthread_mutex_lock (&host_lock);
1532   do_shutdown = 1;
1533   pthread_cond_broadcast (&host_cond);
1534   pthread_mutex_unlock (&host_lock);
1535
1536   for (i = 0; i < threads_num; i++)
1537     pthread_join (threads[i], NULL);
1538
1539   /* Now that all the threads have exited, let's free all the global variables.
1540    * This isn't really neccessary, I guess, but I think it's good stile to do
1541    * so anyway. */
1542   host_this = host_head;
1543   host_head = NULL;
1544   while (host_this != NULL)
1545   {
1546     host_next = host_this->next;
1547
1548     csnmp_host_close_session (host_this);
1549
1550     sfree (host_this->name);
1551     sfree (host_this->address);
1552     sfree (host_this->community);
1553     sfree (host_this->data_list);
1554     sfree (host_this);
1555
1556     host_this = host_next;
1557   }
1558
1559   data_this = data_head;
1560   data_head = NULL;
1561   while (data_this != NULL)
1562   {
1563     data_next = data_this->next;
1564
1565     sfree (data_this->name);
1566     sfree (data_this->type);
1567     sfree (data_this->values);
1568     sfree (data_this);
1569
1570     data_this = data_next;
1571   }
1572
1573   return (0);
1574 } /* int csnmp_shutdown */
1575
1576 void module_register (void)
1577 {
1578   plugin_register_complex_config ("snmp", csnmp_config);
1579   plugin_register_init ("snmp", csnmp_init);
1580   plugin_register_read ("snmp", csnmp_read);
1581   plugin_register_shutdown ("snmp", csnmp_shutdown);
1582 } /* void module_register */
1583
1584 /*
1585  * vim: shiftwidth=2 softtabstop=2 tabstop=8
1586  */