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