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