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