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