snmp plugin: Implemented the host-option `Interval'.
[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
26 #include <net-snmp/net-snmp-config.h>
27 #include <net-snmp/net-snmp-includes.h>
28
29 /*
30  * Private data structes
31  */
32 struct oid_s
33 {
34   oid oid[MAX_OID_LEN];
35   size_t oid_len;
36 };
37 typedef struct oid_s oid_t;
38
39 union instance_u
40 {
41   char  string[DATA_MAX_NAME_LEN];
42   oid_t oid;
43 };
44 typedef union instance_u instance_t;
45
46 struct data_definition_s
47 {
48   char *name; /* used to reference this from the `Collect' option */
49   char *type; /* used to find the data_set */
50   int is_table;
51   instance_t instance;
52   oid_t *values;
53   int values_len;
54   struct data_definition_s *next;
55 };
56 typedef struct data_definition_s data_definition_t;
57
58 struct host_definition_s
59 {
60   char *name;
61   char *address;
62   char *community;
63   int version;
64   struct snmp_session sess;
65   uint16_t skip_num;
66   uint16_t skip_left;
67   data_definition_t **data_list;
68   int data_list_len;
69   struct host_definition_s *next;
70 };
71 typedef struct host_definition_s host_definition_t;
72
73 /* These two types are used to cache values in `csnmp_read_table' to handle
74  * gaps in tables. */
75 struct csnmp_list_instances_s
76 {
77   oid subid;
78   char instance[DATA_MAX_NAME_LEN];
79   struct csnmp_list_instances_s *next;
80 };
81 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
82
83 struct csnmp_table_values_s
84 {
85   oid subid;
86   value_t value;
87   struct csnmp_table_values_s *next;
88 };
89 typedef struct csnmp_table_values_s csnmp_table_values_t;
90
91 /*
92  * Private variables
93  */
94 static data_definition_t *data_head = NULL;
95 static host_definition_t *host_head = NULL;
96
97 /*
98  * Private functions
99  */
100 /* First there are many functions which do configuration stuff. It's a big
101  * bloated and messy, I'm afraid. */
102
103 /*
104  * Callgraph for the config stuff:
105  *  csnmp_config
106  *  +-> call_snmp_init_once
107  *  +-> csnmp_config_add_data
108  *  !   +-> csnmp_config_add_data_type
109  *  !   +-> csnmp_config_add_data_table
110  *  !   +-> csnmp_config_add_data_instance
111  *  !   +-> csnmp_config_add_data_values
112  *  +-> csnmp_config_add_host
113  *      +-> csnmp_config_add_host_address
114  *      +-> csnmp_config_add_host_community
115  *      +-> csnmp_config_add_host_version
116  *      +-> csnmp_config_add_host_collect
117  *      +-> csnmp_config_add_host_interval
118  */
119 static void call_snmp_init_once (void)
120 {
121   static int have_init = 0;
122
123   if (have_init == 0)
124     init_snmp (PACKAGE_NAME);
125   have_init = 1;
126 } /* void call_snmp_init_once */
127
128 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
129 {
130   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
131   {
132     WARNING ("snmp plugin: `Type' needs exactly one string argument.");
133     return (-1);
134   }
135
136   if (dd->type != NULL)
137     free (dd->type);
138
139   dd->type = strdup (ci->values[0].value.string);
140   if (dd->type == NULL)
141     return (-1);
142
143   return (0);
144 } /* int csnmp_config_add_data_type */
145
146 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
147 {
148   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
149   {
150     WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
151     return (-1);
152   }
153
154   dd->is_table = ci->values[0].value.boolean ? 1 : 0;
155
156   return (0);
157 } /* int csnmp_config_add_data_table */
158
159 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
160 {
161   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
162   {
163     WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
164     return (-1);
165   }
166
167   if (dd->is_table)
168   {
169     /* Instance is an OID */
170     dd->instance.oid.oid_len = MAX_OID_LEN;
171
172     if (!read_objid (ci->values[0].value.string,
173           dd->instance.oid.oid, &dd->instance.oid.oid_len))
174     {
175       ERROR ("snmp plugin: read_objid (%s) failed.",
176           ci->values[0].value.string);
177       return (-1);
178     }
179   }
180   else
181   {
182     /* Instance is a simple string */
183     strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
184   }
185
186   return (0);
187 } /* int csnmp_config_add_data_instance */
188
189 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
190 {
191   int i;
192
193   if (ci->values_num < 1)
194   {
195     WARNING ("snmp plugin: `Values' needs at least one argument.");
196     return (-1);
197   }
198
199   for (i = 0; i < ci->values_num; i++)
200     if (ci->values[i].type != OCONFIG_TYPE_STRING)
201     {
202       WARNING ("snmp plugin: `Values' needs only string argument.");
203       return (-1);
204     }
205
206   if (dd->values != NULL)
207     free (dd->values);
208   dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
209   if (dd->values == NULL)
210     return (-1);
211   dd->values_len = ci->values_num;
212
213   for (i = 0; i < ci->values_num; i++)
214   {
215     dd->values[i].oid_len = MAX_OID_LEN;
216
217     if (NULL == snmp_parse_oid (ci->values[i].value.string,
218           dd->values[i].oid, &dd->values[i].oid_len))
219     {
220       ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
221           ci->values[i].value.string);
222       free (dd->values);
223       dd->values = NULL;
224       dd->values_len = 0;
225       return (-1);
226     }
227   }
228
229   return (0);
230 } /* int csnmp_config_add_data_instance */
231
232 static int csnmp_config_add_data (oconfig_item_t *ci)
233 {
234   data_definition_t *dd;
235   int status = 0;
236   int i;
237
238   if ((ci->values_num != 1)
239       || (ci->values[0].type != OCONFIG_TYPE_STRING))
240   {
241     WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
242     return (-1);
243   }
244
245   dd = (data_definition_t *) malloc (sizeof (data_definition_t));
246   if (dd == NULL)
247     return (-1);
248   memset (dd, '\0', sizeof (data_definition_t));
249
250   dd->name = strdup (ci->values[0].value.string);
251   if (dd->name == NULL)
252   {
253     free (dd);
254     return (-1);
255   }
256
257   for (i = 0; i < ci->children_num; i++)
258   {
259     oconfig_item_t *option = ci->children + i;
260     status = 0;
261
262     if (strcasecmp ("Type", option->key) == 0)
263       status = csnmp_config_add_data_type (dd, option);
264     else if (strcasecmp ("Table", option->key) == 0)
265       status = csnmp_config_add_data_table (dd, option);
266     else if (strcasecmp ("Instance", option->key) == 0)
267       status = csnmp_config_add_data_instance (dd, option);
268     else if (strcasecmp ("Values", option->key) == 0)
269       status = csnmp_config_add_data_values (dd, option);
270     else
271     {
272       WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
273       status = -1;
274     }
275
276     if (status != 0)
277       break;
278   } /* for (ci->children) */
279
280   while (status == 0)
281   {
282     if (dd->type == NULL)
283     {
284       WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
285       status = -1;
286       break;
287     }
288     if (dd->values == NULL)
289     {
290       WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
291       status = -1;
292       break;
293     }
294
295     break;
296   } /* while (status == 0) */
297
298   if (status != 0)
299   {
300     sfree (dd->name);
301     sfree (dd->values);
302     sfree (dd);
303     return (-1);
304   }
305
306   DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
307       dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
308
309   if (data_head == NULL)
310     data_head = dd;
311   else
312   {
313     data_definition_t *last;
314     last = data_head;
315     while (last->next != NULL)
316       last = last->next;
317     last->next = dd;
318   }
319
320   return (0);
321 } /* int csnmp_config_add_data */
322
323 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
324 {
325   if ((ci->values_num != 1)
326       || (ci->values[0].type != OCONFIG_TYPE_STRING))
327   {
328     WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
329     return (-1);
330   }
331
332   if (hd->address == NULL)
333     free (hd->address);
334
335   hd->address = strdup (ci->values[0].value.string);
336   if (hd->address == NULL)
337     return (-1);
338
339   DEBUG ("snmp plugin: host = %s; host->address = %s;",
340       hd->name, hd->address);
341
342   hd->sess.peername = hd->address;
343
344   return (0);
345 } /* int csnmp_config_add_host_address */
346
347 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
348 {
349   if ((ci->values_num != 1)
350       || (ci->values[0].type != OCONFIG_TYPE_STRING))
351   {
352     WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
353     return (-1);
354   }
355
356   if (hd->community == NULL)
357     free (hd->community);
358
359   hd->community = strdup (ci->values[0].value.string);
360   if (hd->community == NULL)
361     return (-1);
362
363   DEBUG ("snmp plugin: host = %s; host->community = %s;",
364       hd->name, hd->community);
365
366   hd->sess.community = (u_char *) hd->community;
367   hd->sess.community_len = strlen (hd->community);
368
369   return (0);
370 } /* int csnmp_config_add_host_community */
371
372 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
373 {
374   int version;
375
376   if ((ci->values_num != 1)
377       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
378   {
379     WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
380     return (-1);
381   }
382
383   version = (int) ci->values[0].value.number;
384   if ((version != 1) && (version != 2))
385   {
386     WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
387     return (-1);
388   }
389
390   hd->version = version;
391
392   if (hd->version == 1)
393     hd->sess.version = SNMP_VERSION_1;
394   else
395     hd->sess.version = SNMP_VERSION_2c;
396
397   return (0);
398 } /* int csnmp_config_add_host_address */
399
400 static int csnmp_config_add_host_collect (host_definition_t *host,
401     oconfig_item_t *ci)
402 {
403   data_definition_t *data;
404   data_definition_t **data_list;
405   int data_list_len;
406   int i;
407
408   if (ci->values_num < 1)
409   {
410     WARNING ("snmp plugin: `Collect' needs at least one argument.");
411     return (-1);
412   }
413
414   for (i = 0; i < ci->values_num; i++)
415     if (ci->values[i].type != OCONFIG_TYPE_STRING)
416     {
417       WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
418       return (-1);
419     }
420
421   data_list_len = host->data_list_len + ci->values_num;
422   data_list = (data_definition_t **) realloc (host->data_list,
423       sizeof (data_definition_t *) * data_list_len);
424   if (data_list == NULL)
425     return (-1);
426   host->data_list = data_list;
427
428   for (i = 0; i < ci->values_num; i++)
429   {
430     for (data = data_head; data != NULL; data = data->next)
431       if (strcasecmp (ci->values[i].value.string, data->name) == 0)
432         break;
433
434     if (data == NULL)
435     {
436       WARNING ("snmp plugin: No such data configured: `%s'",
437           ci->values[i].value.string);
438       continue;
439     }
440
441     DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
442         host->name, host->data_list_len, data->name);
443
444     host->data_list[host->data_list_len] = data;
445     host->data_list_len++;
446   } /* for (values_num) */
447
448   return (0);
449 } /* int csnmp_config_add_host_collect */
450
451 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
452 {
453   int interval;
454
455   if ((ci->values_num != 1)
456       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
457   {
458     WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
459     return (-1);
460   }
461
462   interval = (int) ci->values[0].value.number;
463   hd->skip_num = interval / interval_g;
464   if (hd->skip_num < 1)
465     hd->skip_num = 1;
466
467   if ((hd->skip_num * interval_g) != interval)
468   {
469     WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
470         hd->name, hd->skip_num * interval_g);
471   }
472
473   return (0);
474 } /* int csnmp_config_add_host_interval */
475
476 static int csnmp_config_add_host (oconfig_item_t *ci)
477 {
478   host_definition_t *hd;
479   int status = 0;
480   int i;
481
482   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
483   {
484     WARNING ("snmp plugin: `Host' needs exactly one string argument.");
485     return (-1);
486   }
487
488   hd = (host_definition_t *) malloc (sizeof (host_definition_t));
489   if (hd == NULL)
490     return (-1);
491   memset (hd, '\0', sizeof (host_definition_t));
492   hd->version = 2;
493
494   hd->name = strdup (ci->values[0].value.string);
495   if (hd->name == NULL)
496   {
497     free (hd);
498     return (-1);
499   }
500
501   snmp_sess_init (&hd->sess);
502   hd->sess.version = SNMP_VERSION_2c;
503
504   hd->skip_num = 1;
505   hd->skip_left = 0;
506
507   for (i = 0; i < ci->children_num; i++)
508   {
509     oconfig_item_t *option = ci->children + i;
510     status = 0;
511
512     if (strcasecmp ("Address", option->key) == 0)
513       status = csnmp_config_add_host_address (hd, option);
514     else if (strcasecmp ("Community", option->key) == 0)
515       status = csnmp_config_add_host_community (hd, option);
516     else if (strcasecmp ("Version", option->key) == 0)
517       status = csnmp_config_add_host_version (hd, option);
518     else if (strcasecmp ("Collect", option->key) == 0)
519       csnmp_config_add_host_collect (hd, option);
520     else if (strcasecmp ("Interval", option->key) == 0)
521       csnmp_config_add_host_interval (hd, option);
522     else
523     {
524       WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
525       status = -1;
526     }
527
528     if (status != 0)
529       break;
530   } /* for (ci->children) */
531
532   while (status == 0)
533   {
534     if (hd->address == NULL)
535     {
536       WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
537       status = -1;
538       break;
539     }
540     if (hd->community == NULL)
541     {
542       WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
543       status = -1;
544       break;
545     }
546
547     break;
548   } /* while (status == 0) */
549
550   if (status != 0)
551   {
552     sfree (hd->name);
553     sfree (hd);
554     return (-1);
555   }
556
557   DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
558       hd->name, hd->address, hd->community, hd->version);
559
560   if (host_head == NULL)
561     host_head = hd;
562   else
563   {
564     host_definition_t *last;
565     last = host_head;
566     while (last->next != NULL)
567       last = last->next;
568     last->next = hd;
569   }
570
571   return (0);
572 } /* int csnmp_config_add_host */
573
574 static int csnmp_config (oconfig_item_t *ci)
575 {
576   int i;
577
578   call_snmp_init_once ();
579
580   for (i = 0; i < ci->children_num; i++)
581   {
582     oconfig_item_t *child = ci->children + i;
583     if (strcasecmp ("Data", child->key) == 0)
584       csnmp_config_add_data (child);
585     else if (strcasecmp ("Host", child->key) == 0)
586       csnmp_config_add_host (child);
587     else
588     {
589       WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
590     }
591   } /* for (ci->children) */
592
593   return (0);
594 } /* int csnmp_config */
595
596 static int csnmp_init (void)
597 {
598   call_snmp_init_once ();
599   return (0);
600 }
601
602 #if 0
603 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
604 {
605   value_t values[3];
606   value_list_t vl = VALUE_LIST_INIT;
607
608   values[0].gauge = snum;
609   values[1].gauge = mnum;
610   values[2].gauge = lnum;
611
612   vl.values = values;
613   vl.values_len = STATIC_ARRAY_SIZE (values);
614   vl.time = time (NULL);
615   strcpy (vl.host, hostname_g);
616   strcpy (vl.plugin, "load");
617
618   plugin_dispatch_values ("load", &vl);
619 }
620 #endif
621
622 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
623 {
624   value_t ret;
625   uint64_t temp = 0;
626   int defined = 1;
627
628   if ((vl->type == ASN_INTEGER)
629       || (vl->type == ASN_UINTEGER)
630       || (vl->type == ASN_COUNTER)
631       || (vl->type == ASN_GAUGE))
632   {
633     temp = (uint32_t) *vl->val.integer;
634     DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
635   }
636   else if (vl->type == ASN_COUNTER64)
637   {
638     temp = (uint32_t) vl->val.counter64->high;
639     temp = temp << 32;
640     temp += (uint32_t) vl->val.counter64->low;
641     DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
642   }
643   else
644   {
645     WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
646     defined = 0;
647   }
648
649   if (type == DS_TYPE_COUNTER)
650   {
651     ret.counter = temp;
652   }
653   else if (type == DS_TYPE_GAUGE)
654   {
655     ret.gauge = NAN;
656     if (defined != 0)
657       ret.gauge = temp;
658   }
659
660   return (ret);
661 } /* value_t csnmp_value_list_to_value */
662
663 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
664     csnmp_list_instances_t *instance_list,
665     csnmp_table_values_t **value_table)
666 {
667   const data_set_t *ds;
668   value_list_t vl = VALUE_LIST_INIT;
669
670   csnmp_list_instances_t *instance_list_ptr;
671   csnmp_table_values_t **value_table_ptr;
672
673   int i;
674
675   ds = plugin_get_ds (data->type);
676   if (!ds)
677   {
678     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
679     return (-1);
680   }
681   assert (ds->ds_num == data->values_len);
682
683   value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
684       * data->values_len);
685   if (value_table_ptr == NULL)
686     return (-1);
687   for (i = 0; i < data->values_len; i++)
688     value_table_ptr[i] = value_table[i];
689
690   vl.values_len = ds->ds_num;
691   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
692   if (vl.values == NULL)
693   {
694     sfree (value_table_ptr);
695     return (-1);
696   }
697
698   strncpy (vl.host, host->name, sizeof (vl.host));
699   vl.host[sizeof (vl.host) - 1] = '\0';
700   strcpy (vl.plugin, "snmp");
701
702   vl.time = time (NULL);
703
704   for (instance_list_ptr = instance_list;
705       instance_list_ptr != NULL;
706       instance_list_ptr = instance_list_ptr->next)
707   {
708     strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
709     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
710
711     for (i = 0; i < data->values_len; i++)
712     {
713       while ((value_table_ptr[i] != NULL)
714           && (value_table_ptr[i]->subid < instance_list_ptr->subid))
715         value_table_ptr[i] = value_table_ptr[i]->next;
716       if ((value_table_ptr[i] == NULL)
717           || (value_table_ptr[i]->subid != instance_list_ptr->subid))
718         break;
719       vl.values[i] = value_table_ptr[i]->value;
720     } /* for (data->values_len) */
721
722     /* If the for-loop was aborted early, not all subid's match. */
723     if (i < data->values_len)
724     {
725       DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
726           "Skipping SUBID %i",
727           host->name, data->name, i, instance_list_ptr->subid);
728       continue;
729     }
730
731     /* If we get here `vl.type_instance' and all `vl.values' have been set */
732     plugin_dispatch_values (data->type, &vl);
733   } /* for (instance_list) */
734
735   sfree (vl.values);
736   sfree (value_table_ptr);
737
738   return (0);
739 } /* int csnmp_dispatch_table */
740
741 static int csnmp_read_table (struct snmp_session *sess_ptr,
742     host_definition_t *host, data_definition_t *data)
743 {
744   struct snmp_pdu *req;
745   struct snmp_pdu *res;
746   struct variable_list *vb;
747
748   const data_set_t *ds;
749   oid_t *oid_list;
750   uint32_t oid_list_len;
751
752   int status;
753   int i;
754
755   /* `value_table' and `value_table_ptr' implement a linked list for each
756    * value. `instance_list' and `instance_list_ptr' implement a linked list of
757    * instance names. This is used to jump gaps in the table. */
758   csnmp_list_instances_t *instance_list;
759   csnmp_list_instances_t *instance_list_ptr;
760   csnmp_table_values_t **value_table;
761   csnmp_table_values_t **value_table_ptr;
762
763   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
764       host->name, data->name);
765
766   ds = plugin_get_ds (data->type);
767   if (!ds)
768   {
769     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
770     return (-1);
771   }
772
773   if (ds->ds_num != data->values_len)
774   {
775     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
776         data->type, ds->ds_num, data->values_len);
777     return (-1);
778   }
779
780   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
781   oid_list_len = data->values_len + 1;
782   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
783   if (oid_list == NULL)
784     return (-1);
785   memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
786   for (i = 0; i < data->values_len; i++)
787     memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
788
789   /* Allocate the `value_table' */
790   value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
791       * 2 * data->values_len);
792   if (value_table == NULL)
793   {
794     sfree (oid_list);
795     return (-1);
796   }
797   memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2);
798   value_table_ptr = value_table + data->values_len;
799   
800   instance_list = NULL;
801   instance_list_ptr = NULL;
802
803   status = 0;
804   while (status == 0)
805   {
806     csnmp_list_instances_t *il;
807
808     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
809     if (req == NULL)
810     {
811       ERROR ("snmp plugin: snmp_pdu_create failed.");
812       status = -1;
813       break;
814     }
815
816     for (i = 0; i < oid_list_len; i++)
817       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
818
819     status = snmp_synch_response (sess_ptr, req, &res);
820
821     if (status != STAT_SUCCESS)
822     {
823       ERROR ("snmp plugin: snmp_synch_response failed.");
824       status = -1;
825       break;
826     }
827     status = 0;
828     assert (res != NULL);
829
830     vb = res->variables;
831     if (vb == NULL)
832     {
833       status = -1;
834       break;
835     }
836
837     /* Check if we left the subtree */
838     if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
839           vb->name, vb->name_length,
840           data->instance.oid.oid_len) != 0)
841       break;
842
843     /* Allocate a new `csnmp_list_instances_t', insert the instance name and
844      * add it to the list */
845     il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
846     if (il == NULL)
847     {
848       status = -1;
849       break;
850     }
851     il->subid = vb->name[vb->name_length - 1];
852     il->next = NULL;
853
854     /* Get instance name */
855     if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
856     {
857       strncpy (il->instance, (char *) vb->val.bitstring,
858           sizeof (il->instance));
859       il->instance[sizeof (il->instance) - 1] = '\0';
860       DEBUG ("Before escape_slashes: %s", il->instance);
861       escape_slashes (il->instance, strlen (il->instance));
862       DEBUG ("After  escape_slashes: %s", il->instance);
863     }
864     else
865     {
866       value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
867       snprintf (il->instance, sizeof (il->instance),
868           "%llu", val.counter);
869     }
870     il->instance[sizeof (il->instance) - 1] = '\0';
871     DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
872         data->name, il->instance);
873
874     if (instance_list_ptr == NULL)
875       instance_list = il;
876     else
877       instance_list_ptr->next = il;
878     instance_list_ptr = il;
879
880     /* Copy OID to oid_list[0] */
881     memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
882     oid_list[0].oid_len = vb->name_length;
883
884     for (i = 0; i < data->values_len; i++)
885     {
886       csnmp_table_values_t *vt;
887
888       vb = vb->next_variable;
889       if (vb == NULL)
890       {
891         status = -1;
892         break;
893       }
894
895       /* Check if we left the subtree */
896       if (snmp_oid_ncompare (data->values[i].oid,
897             data->values[i].oid_len,
898             vb->name, vb->name_length,
899             data->values[i].oid_len) != 0)
900       {
901         DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
902             host->name, data->name, i);
903         continue;
904       }
905
906       if ((value_table_ptr[i] != NULL)
907           && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
908       {
909         DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
910             host->name, data->name, i);
911         continue;
912       }
913
914       vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
915       if (vt != NULL)
916       {
917         vt->subid = vb->name[vb->name_length - 1];
918         vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
919         vt->next = NULL;
920
921         if (value_table_ptr[i] == NULL)
922           value_table[i] = vt;
923         else
924           value_table_ptr[i]->next = vt;
925         value_table_ptr[i] = vt;
926       }
927
928       /* Copy OID to oid_list[i + 1] */
929       memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
930       oid_list[i + 1].oid_len = vb->name_length;
931     } /* for (data->values_len) */
932
933     if (res != NULL)
934       snmp_free_pdu (res);
935     res = NULL;
936   } /* while (status == 0) */
937
938   if (status == 0)
939     csnmp_dispatch_table (host, data, instance_list, value_table);
940
941   /* Free all allocated variables here */
942   while (instance_list != NULL)
943   {
944     instance_list_ptr = instance_list->next;
945     sfree (instance_list);
946     instance_list = instance_list_ptr;
947   }
948
949   for (i = 0; i < data->values_len; i++)
950   {
951     csnmp_table_values_t *tmp;
952     while (value_table[i] != NULL)
953     {
954       tmp = value_table[i]->next;
955       sfree (value_table[i]);
956       value_table[i] = tmp;
957     }
958   }
959
960   sfree (value_table);
961   sfree (oid_list);
962
963   return (0);
964 } /* int csnmp_read_table */
965
966 static int csnmp_read_value (struct snmp_session *sess_ptr,
967     host_definition_t *host, data_definition_t *data)
968 {
969   struct snmp_pdu *req;
970   struct snmp_pdu *res;
971   struct variable_list *vb;
972
973   const data_set_t *ds;
974   value_list_t vl = VALUE_LIST_INIT;
975
976   int status;
977   int i;
978
979   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
980       host->name, data->name);
981
982   ds = plugin_get_ds (data->type);
983   if (!ds)
984   {
985     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
986     return (-1);
987   }
988
989   if (ds->ds_num != data->values_len)
990   {
991     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
992         data->type, ds->ds_num, data->values_len);
993     return (-1);
994   }
995
996   vl.values_len = ds->ds_num;
997   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
998   if (vl.values == NULL)
999     return (-1);
1000   for (i = 0; i < vl.values_len; i++)
1001   {
1002     if (ds->ds[i].type == DS_TYPE_COUNTER)
1003       vl.values[i].counter = 0;
1004     else
1005       vl.values[i].gauge = NAN;
1006   }
1007
1008   strncpy (vl.host, host->name, sizeof (vl.host));
1009   vl.host[sizeof (vl.host) - 1] = '\0';
1010   strcpy (vl.plugin, "snmp");
1011   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1012   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1013
1014   vl.interval = interval_g * host->skip_num;
1015
1016   req = snmp_pdu_create (SNMP_MSG_GET);
1017   if (req == NULL)
1018   {
1019     ERROR ("snmp plugin: snmp_pdu_create failed.");
1020     sfree (vl.values);
1021     return (-1);
1022   }
1023
1024   for (i = 0; i < data->values_len; i++)
1025     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1026   status = snmp_synch_response (sess_ptr, req, &res);
1027
1028   if (status != STAT_SUCCESS)
1029   {
1030     ERROR ("snmp plugin: snmp_synch_response failed.");
1031     sfree (vl.values);
1032     return (-1);
1033   }
1034
1035   vl.time = time (NULL);
1036
1037   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1038   {
1039     char buffer[1024];
1040     snprint_variable (buffer, sizeof (buffer),
1041         vb->name, vb->name_length, vb);
1042     DEBUG ("snmp plugin: Got this variable: %s", buffer);
1043
1044     for (i = 0; i < data->values_len; i++)
1045       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1046             vb->name, vb->name_length) == 0)
1047         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1048   } /* for (res->variables) */
1049
1050   snmp_free_pdu (res);
1051
1052   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1053   plugin_dispatch_values (data->type, &vl);
1054   sfree (vl.values);
1055
1056   return (0);
1057 } /* int csnmp_read_value */
1058
1059 static int csnmp_read_host (host_definition_t *host)
1060 {
1061   struct snmp_session *sess_ptr;
1062   int i;
1063
1064   DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1065
1066   sess_ptr = snmp_open (&host->sess);
1067   if (sess_ptr == NULL)
1068   {
1069     snmp_perror ("snmp_open");
1070     ERROR ("snmp plugin: snmp_open failed.");
1071     return (-1);
1072   }
1073
1074   for (i = 0; i < host->data_list_len; i++)
1075   {
1076     data_definition_t *data = host->data_list[i];
1077
1078     if (data->is_table)
1079       csnmp_read_table (sess_ptr, host, data);
1080     else
1081       csnmp_read_value (sess_ptr, host, data);
1082   }
1083
1084   snmp_close (sess_ptr);
1085   return (0);
1086 } /* int csnmp_read_host */
1087
1088 static int csnmp_read (void)
1089 {
1090   host_definition_t *host;
1091   time_t now;
1092
1093   if (host_head == NULL)
1094   {
1095     INFO ("snmp plugin: No hosts configured.");
1096     return (-1);
1097   }
1098
1099   now = time (NULL);
1100
1101   for (host = host_head; host != NULL; host = host->next)
1102   {
1103     host->skip_left--;
1104     if (host->skip_left > 0)
1105       continue;
1106
1107     csnmp_read_host (host);
1108
1109     host->skip_left = host->skip_num;
1110   } /* for (host) */
1111
1112   return (0);
1113 } /* int csnmp_read */
1114
1115 void module_register (void)
1116 {
1117   plugin_register_complex_config ("snmp", csnmp_config);
1118   plugin_register_init ("snmp", csnmp_init);
1119   plugin_register_read ("snmp", csnmp_read);
1120 } /* void module_register */
1121
1122 /*
1123  * vim: shiftwidth=2 softtabstop=2 tabstop=8
1124  */