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