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