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