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