snmp plugin: Fix iterating over tables without an instance configuration.
[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 = malloc (sizeof (*value_table_ptr) * data->values_len);
1132   if (value_table_ptr == NULL)
1133     return (-1);
1134   for (i = 0; i < data->values_len; i++)
1135     value_table_ptr[i] = value_table[i];
1136
1137   vl.values_len = ds->ds_num;
1138   vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1139   if (vl.values == NULL)
1140   {
1141     ERROR ("snmp plugin: malloc failed.");
1142     sfree (value_table_ptr);
1143     return (-1);
1144   }
1145
1146   sstrncpy (vl.host, host->name, sizeof (vl.host));
1147   sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1148
1149   vl.interval = host->interval;
1150
1151   have_more = 1;
1152   memset (&current_suffix, 0, sizeof (current_suffix));
1153   while (have_more)
1154   {
1155     _Bool suffix_skipped = 0;
1156
1157     /* Determine next suffix to handle. */
1158     if (instance_list != NULL)
1159     {
1160       if (instance_list_ptr == NULL)
1161       {
1162         have_more = 0;
1163         continue;
1164       }
1165
1166       memcpy (&current_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1167     }
1168     else /* no instance configured */
1169     {
1170       csnmp_table_values_t *ptr = value_table_ptr[0];
1171       if (ptr == NULL)
1172       {
1173         have_more = 0;
1174         continue;
1175       }
1176
1177       memcpy (&current_suffix, &ptr->suffix, sizeof (current_suffix));
1178     }
1179
1180     /* Update all the value_table_ptr to point at the entry with the same
1181      * trailing partial OID */
1182     for (i = 0; i < data->values_len; i++)
1183     {
1184       while ((value_table_ptr[i] != NULL)
1185           && (csnmp_oid_compare (&value_table_ptr[i]->suffix, &current_suffix) < 0))
1186         value_table_ptr[i] = value_table_ptr[i]->next;
1187
1188       if (value_table_ptr[i] == NULL)
1189       {
1190         have_more = 0;
1191         break;
1192       }
1193       else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, &current_suffix) > 0)
1194       {
1195         /* This suffix is missing in the subtree. Indicate this with the
1196          * "suffix_skipped" flag and try the next instance / suffix. */
1197         suffix_skipped = 1;
1198         break;
1199       }
1200     } /* for (i = 0; i < columns; i++) */
1201
1202     if (!have_more)
1203       break;
1204
1205     /* Matching the values failed. Start from the beginning again. */
1206     if (suffix_skipped)
1207     {
1208       if (instance_list != NULL)
1209         instance_list_ptr = instance_list_ptr->next;
1210       else
1211         value_table_ptr[0] = value_table_ptr[0]->next;
1212
1213       continue;
1214     }
1215
1216     /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1217      * to the same subid. instance_list_ptr is either NULL or points to the
1218      * same subid, too. */
1219 #if COLLECT_DEBUG
1220     for (i = 1; i < data->values_len; i++)
1221     {
1222       assert (value_table_ptr[i] != NULL);
1223       assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1224             &value_table_ptr[i]->suffix) == 0);
1225     }
1226     assert ((instance_list_ptr == NULL)
1227         || (csnmp_oid_compare (&instance_list_ptr->suffix,
1228             &value_table_ptr[0]->suffix) == 0));
1229 #endif
1230
1231     sstrncpy (vl.type, data->type, sizeof (vl.type));
1232
1233     {
1234       char temp[DATA_MAX_NAME_LEN];
1235
1236       if (instance_list_ptr == NULL)
1237         csnmp_oid_to_string (temp, sizeof (temp), &current_suffix);
1238       else
1239         sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1240
1241       if (data->instance_prefix == NULL)
1242         sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1243       else
1244         ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1245             data->instance_prefix, temp);
1246     }
1247
1248     for (i = 0; i < data->values_len; i++)
1249       vl.values[i] = value_table_ptr[i]->value;
1250
1251     /* If we get here `vl.type_instance' and all `vl.values' have been set */
1252     plugin_dispatch_values (&vl);
1253
1254     if (instance_list != NULL)
1255       instance_list_ptr = instance_list_ptr->next;
1256     else
1257       value_table_ptr[0] = value_table_ptr[0]->next;
1258   } /* while (have_more) */
1259
1260   sfree (vl.values);
1261   sfree (value_table_ptr);
1262
1263   return (0);
1264 } /* int csnmp_dispatch_table */
1265
1266 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1267 {
1268   struct snmp_pdu *req;
1269   struct snmp_pdu *res;
1270   struct variable_list *vb;
1271
1272   const data_set_t *ds;
1273   oid_t *oid_list;
1274   uint32_t oid_list_len;
1275
1276   int status;
1277   int i;
1278
1279   /* `value_list_head' and `value_list_tail' implement a linked list for each
1280    * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1281    * instance names. This is used to jump gaps in the table. */
1282   csnmp_list_instances_t *instance_list_head;
1283   csnmp_list_instances_t *instance_list_tail;
1284   csnmp_table_values_t **value_list_head;
1285   csnmp_table_values_t **value_list_tail;
1286
1287   DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1288       host->name, data->name);
1289
1290   if (host->sess_handle == NULL)
1291   {
1292     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1293     return (-1);
1294   }
1295
1296   ds = plugin_get_ds (data->type);
1297   if (!ds)
1298   {
1299     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1300     return (-1);
1301   }
1302
1303   if (ds->ds_num != data->values_len)
1304   {
1305     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1306         data->type, ds->ds_num, data->values_len);
1307     return (-1);
1308   }
1309
1310   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1311   oid_list_len = data->values_len + 1;
1312   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1313   if (oid_list == NULL)
1314   {
1315     ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1316     return (-1);
1317   }
1318   memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1319   if (data->instance.oid.oid_len > 0)
1320     memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1321   else
1322     oid_list_len--;
1323
1324   /* We're going to construct n linked lists, one for each "value".
1325    * value_list_head will contain pointers to the heads of these linked lists,
1326    * value_list_tail will contain pointers to the tail of the lists. */
1327   value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1328   value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1329   if ((value_list_head == NULL) || (value_list_tail == NULL))
1330   {
1331     ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1332     sfree (oid_list);
1333     sfree (value_list_head);
1334     sfree (value_list_tail);
1335     return (-1);
1336   }
1337
1338   instance_list_head = NULL;
1339   instance_list_tail = NULL;
1340
1341   status = 0;
1342   while (status == 0)
1343   {
1344     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1345     if (req == NULL)
1346     {
1347       ERROR ("snmp plugin: snmp_pdu_create failed.");
1348       status = -1;
1349       break;
1350     }
1351
1352     for (i = 0; (uint32_t) i < oid_list_len; i++)
1353       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1354
1355     res = NULL;
1356     status = snmp_sess_synch_response (host->sess_handle, req, &res);
1357
1358     if ((status != STAT_SUCCESS) || (res == NULL))
1359     {
1360       char *errstr = NULL;
1361
1362       snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1363
1364       c_complain (LOG_ERR, &host->complaint,
1365           "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1366           host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1367
1368       if (res != NULL)
1369         snmp_free_pdu (res);
1370       res = NULL;
1371
1372       sfree (errstr);
1373       csnmp_host_close_session (host);
1374
1375       status = -1;
1376       break;
1377     }
1378     status = 0;
1379     assert (res != NULL);
1380     c_release (LOG_INFO, &host->complaint,
1381         "snmp plugin: host %s: snmp_sess_synch_response successful.",
1382         host->name);
1383
1384     vb = res->variables;
1385     if (vb == NULL)
1386     {
1387       status = -1;
1388       break;
1389     }
1390
1391     /* Check if all values (and possibly the instance) have left their
1392      * subtree */
1393     if (csnmp_check_res_left_subtree (host, data, res) != 0)
1394     {
1395       status = 0;
1396       break;
1397     }
1398
1399     /* Copy the OID of the value used as instance to oid_list, if an instance
1400      * is configured. */
1401     if (data->instance.oid.oid_len > 0)
1402     {
1403       /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1404        * add it to the list */
1405       if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1406             res, &data->instance.oid) != 0)
1407       {
1408         ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1409         status = -1;
1410         break;
1411       }
1412
1413       /* The instance OID is added to the list of OIDs to GET from the
1414        * snmp agent last, so set vb on the last variable returned and copy
1415        * that OID. */
1416       for (vb = res->variables;
1417           (vb != NULL) && (vb->next_variable != NULL);
1418           vb = vb->next_variable)
1419         /* do nothing */;
1420       assert (vb != NULL);
1421
1422       /* Copy the OID of the instance value to oid_list[data->values_len].
1423        * "oid_list" is used for the next GETNEXT request. */
1424       memcpy (oid_list[data->values_len].oid, vb->name,
1425           sizeof (oid) * vb->name_length);
1426       oid_list[data->values_len].oid_len = vb->name_length;
1427     }
1428
1429     /* Iterate over all the (non-instance) values returned by the agent. The
1430      * (i < value_len) check will make sure we're not handling the instance OID
1431      * twice. */
1432     for (vb = res->variables, i = 0;
1433         (vb != NULL) && (i < data->values_len);
1434         vb = vb->next_variable, i++)
1435     {
1436       csnmp_table_values_t *vt;
1437       oid_t vb_name;
1438
1439       csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1440
1441       /* Check if we left the subtree */
1442       if (snmp_oid_ncompare (data->values[i].oid,
1443             data->values[i].oid_len,
1444             vb->name, vb->name_length,
1445             data->values[i].oid_len) != 0)
1446       {
1447         DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1448             host->name, data->name, i);
1449         continue;
1450       }
1451
1452       /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1453        * table matching algorithm will get confused. */
1454       if ((value_list_tail[i] != NULL)
1455           && (csnmp_oid_compare (&vb_name, &value_list_tail[i]->suffix) <= 0))
1456       {
1457         DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1458             "SUBID is not increasing.",
1459             host->name, data->name, i);
1460         continue;
1461       }
1462
1463       vt = malloc (sizeof (*vt));
1464       if (vt == NULL)
1465       {
1466         ERROR ("snmp plugin: malloc failed.");
1467         status = -1;
1468         break;
1469       }
1470       memset (vt, 0, sizeof (*vt));
1471
1472       csnmp_oid_suffix (&vt->suffix, &vb_name, data->values + i);
1473       vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1474           data->scale, data->shift);
1475       vt->next = NULL;
1476
1477       if (value_list_tail[i] == NULL)
1478         value_list_head[i] = vt;
1479       else
1480         value_list_tail[i]->next = vt;
1481       value_list_tail[i] = vt;
1482
1483       /* Copy OID to oid_list[i + 1] */
1484       memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1485       oid_list[i].oid_len = vb->name_length;
1486     } /* for (i = data->values_len) */
1487
1488     if (res != NULL)
1489       snmp_free_pdu (res);
1490     res = NULL;
1491   } /* while (status == 0) */
1492
1493   if (res != NULL)
1494     snmp_free_pdu (res);
1495   res = NULL;
1496
1497   if (status == 0)
1498     csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1499
1500   /* Free all allocated variables here */
1501   while (instance_list_head != NULL)
1502   {
1503     csnmp_list_instances_t *next = instance_list_head->next;
1504     sfree (instance_list_head);
1505     instance_list_head = next;
1506   }
1507
1508   for (i = 0; i < data->values_len; i++)
1509   {
1510     while (value_list_head[i] != NULL)
1511     {
1512       csnmp_table_values_t *next = value_list_head[i]->next;
1513       sfree (value_list_head[i]);
1514       value_list_head[i] = next;
1515     }
1516   }
1517
1518   sfree (value_list_head);
1519   sfree (value_list_tail);
1520   sfree (oid_list);
1521
1522   return (0);
1523 } /* int csnmp_read_table */
1524
1525 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1526 {
1527   struct snmp_pdu *req;
1528   struct snmp_pdu *res;
1529   struct variable_list *vb;
1530
1531   const data_set_t *ds;
1532   value_list_t vl = VALUE_LIST_INIT;
1533
1534   int status;
1535   int i;
1536
1537   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1538       host->name, data->name);
1539
1540   if (host->sess_handle == NULL)
1541   {
1542     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1543     return (-1);
1544   }
1545
1546   ds = plugin_get_ds (data->type);
1547   if (!ds)
1548   {
1549     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1550     return (-1);
1551   }
1552
1553   if (ds->ds_num != data->values_len)
1554   {
1555     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1556         data->type, ds->ds_num, data->values_len);
1557     return (-1);
1558   }
1559
1560   vl.values_len = ds->ds_num;
1561   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1562   if (vl.values == NULL)
1563     return (-1);
1564   for (i = 0; i < vl.values_len; i++)
1565   {
1566     if (ds->ds[i].type == DS_TYPE_COUNTER)
1567       vl.values[i].counter = 0;
1568     else
1569       vl.values[i].gauge = NAN;
1570   }
1571
1572   sstrncpy (vl.host, host->name, sizeof (vl.host));
1573   sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1574   sstrncpy (vl.type, data->type, sizeof (vl.type));
1575   sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1576
1577   vl.interval = host->interval;
1578
1579   req = snmp_pdu_create (SNMP_MSG_GET);
1580   if (req == NULL)
1581   {
1582     ERROR ("snmp plugin: snmp_pdu_create failed.");
1583     sfree (vl.values);
1584     return (-1);
1585   }
1586
1587   for (i = 0; i < data->values_len; i++)
1588     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1589
1590   res = NULL;
1591   status = snmp_sess_synch_response (host->sess_handle, req, &res);
1592
1593   if ((status != STAT_SUCCESS) || (res == NULL))
1594   {
1595     char *errstr = NULL;
1596
1597     snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1598     ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1599         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1600
1601     if (res != NULL)
1602       snmp_free_pdu (res);
1603     res = NULL;
1604
1605     sfree (errstr);
1606     csnmp_host_close_session (host);
1607
1608     return (-1);
1609   }
1610
1611
1612   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1613   {
1614 #if COLLECT_DEBUG
1615     char buffer[1024];
1616     snprint_variable (buffer, sizeof (buffer),
1617         vb->name, vb->name_length, vb);
1618     DEBUG ("snmp plugin: Got this variable: %s", buffer);
1619 #endif /* COLLECT_DEBUG */
1620
1621     for (i = 0; i < data->values_len; i++)
1622       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1623             vb->name, vb->name_length) == 0)
1624         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1625             data->scale, data->shift);
1626   } /* for (res->variables) */
1627
1628   if (res != NULL)
1629     snmp_free_pdu (res);
1630   res = NULL;
1631
1632   DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1633   plugin_dispatch_values (&vl);
1634   sfree (vl.values);
1635
1636   return (0);
1637 } /* int csnmp_read_value */
1638
1639 static int csnmp_read_host (user_data_t *ud)
1640 {
1641   host_definition_t *host;
1642   time_t time_start;
1643   time_t time_end;
1644   int status;
1645   int success;
1646   int i;
1647
1648   host = ud->data;
1649
1650   if (host->interval == 0)
1651     host->interval = interval_g;
1652
1653   time_start = time (NULL);
1654   DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1655       (unsigned int) time_start);
1656
1657   if (host->sess_handle == NULL)
1658     csnmp_host_open_session (host);
1659
1660   if (host->sess_handle == NULL)
1661     return (-1);
1662
1663   success = 0;
1664   for (i = 0; i < host->data_list_len; i++)
1665   {
1666     data_definition_t *data = host->data_list[i];
1667
1668     if (data->is_table)
1669       status = csnmp_read_table (host, data);
1670     else
1671       status = csnmp_read_value (host, data);
1672
1673     if (status == 0)
1674       success++;
1675   }
1676
1677   time_end = time (NULL);
1678   DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1679       (unsigned int) time_end);
1680   if ((uint32_t) (time_end - time_start) > host->interval)
1681   {
1682     WARNING ("snmp plugin: Host `%s' should be queried every %"PRIu32
1683         " seconds, but reading all values takes %u seconds.",
1684         host->name, host->interval, (unsigned int) (time_end - time_start));
1685   }
1686
1687   if (success == 0)
1688     return (-1);
1689
1690   return (0);
1691 } /* int csnmp_read_host */
1692
1693 static int csnmp_init (void)
1694 {
1695   call_snmp_init_once ();
1696
1697   return (0);
1698 } /* int csnmp_init */
1699
1700 static int csnmp_shutdown (void)
1701 {
1702   data_definition_t *data_this;
1703   data_definition_t *data_next;
1704
1705   /* When we get here, the read threads have been stopped and all the
1706    * `host_definition_t' will be freed. */
1707   DEBUG ("snmp plugin: Destroying all data definitions.");
1708
1709   data_this = data_head;
1710   data_head = NULL;
1711   while (data_this != NULL)
1712   {
1713     data_next = data_this->next;
1714
1715     sfree (data_this->name);
1716     sfree (data_this->type);
1717     sfree (data_this->values);
1718     sfree (data_this);
1719
1720     data_this = data_next;
1721   }
1722
1723   return (0);
1724 } /* int csnmp_shutdown */
1725
1726 void module_register (void)
1727 {
1728   plugin_register_complex_config ("snmp", csnmp_config);
1729   plugin_register_init ("snmp", csnmp_init);
1730   plugin_register_shutdown ("snmp", csnmp_shutdown);
1731 } /* void module_register */
1732
1733 /*
1734  * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1735  */