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