snmp plugin: Many important bugfixes.
[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   int16_t skip_num;
66   int16_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;
464   if (hd->skip_num < 0)
465     hd->skip_num = 0;
466
467   return (0);
468 } /* int csnmp_config_add_host_interval */
469
470 static int csnmp_config_add_host (oconfig_item_t *ci)
471 {
472   host_definition_t *hd;
473   int status = 0;
474   int i;
475
476   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
477   {
478     WARNING ("snmp plugin: `Host' needs exactly one string argument.");
479     return (-1);
480   }
481
482   hd = (host_definition_t *) malloc (sizeof (host_definition_t));
483   if (hd == NULL)
484     return (-1);
485   memset (hd, '\0', sizeof (host_definition_t));
486   hd->version = 2;
487
488   hd->name = strdup (ci->values[0].value.string);
489   if (hd->name == NULL)
490   {
491     free (hd);
492     return (-1);
493   }
494
495   snmp_sess_init (&hd->sess);
496   hd->sess.version = SNMP_VERSION_2c;
497
498   hd->skip_num = 0;
499   hd->skip_left = 0;
500
501   for (i = 0; i < ci->children_num; i++)
502   {
503     oconfig_item_t *option = ci->children + i;
504     status = 0;
505
506     if (strcasecmp ("Address", option->key) == 0)
507       status = csnmp_config_add_host_address (hd, option);
508     else if (strcasecmp ("Community", option->key) == 0)
509       status = csnmp_config_add_host_community (hd, option);
510     else if (strcasecmp ("Version", option->key) == 0)
511       status = csnmp_config_add_host_version (hd, option);
512     else if (strcasecmp ("Collect", option->key) == 0)
513       csnmp_config_add_host_collect (hd, option);
514     else if (strcasecmp ("Interval", option->key) == 0)
515       csnmp_config_add_host_interval (hd, option);
516     else
517     {
518       WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
519       status = -1;
520     }
521
522     if (status != 0)
523       break;
524   } /* for (ci->children) */
525
526   while (status == 0)
527   {
528     if (hd->address == NULL)
529     {
530       WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
531       status = -1;
532       break;
533     }
534     if (hd->community == NULL)
535     {
536       WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
537       status = -1;
538       break;
539     }
540
541     break;
542   } /* while (status == 0) */
543
544   if (status != 0)
545   {
546     sfree (hd->name);
547     sfree (hd);
548     return (-1);
549   }
550
551   DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
552       hd->name, hd->address, hd->community, hd->version);
553
554   if (host_head == NULL)
555     host_head = hd;
556   else
557   {
558     host_definition_t *last;
559     last = host_head;
560     while (last->next != NULL)
561       last = last->next;
562     last->next = hd;
563   }
564
565   return (0);
566 } /* int csnmp_config_add_host */
567
568 static int csnmp_config (oconfig_item_t *ci)
569 {
570   int i;
571
572   call_snmp_init_once ();
573
574   for (i = 0; i < ci->children_num; i++)
575   {
576     oconfig_item_t *child = ci->children + i;
577     if (strcasecmp ("Data", child->key) == 0)
578       csnmp_config_add_data (child);
579     else if (strcasecmp ("Host", child->key) == 0)
580       csnmp_config_add_host (child);
581     else
582     {
583       WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
584     }
585   } /* for (ci->children) */
586
587   return (0);
588 } /* int csnmp_config */
589
590 static int csnmp_init (void)
591 {
592   host_definition_t *host;
593
594   call_snmp_init_once ();
595
596   for (host = host_head; host != NULL; host = host->next)
597   {
598     host->skip_left = interval_g;
599     if (host->skip_num == 0)
600     {
601       host->skip_num = interval_g;
602     }
603     else if (host->skip_num < interval_g)
604     {
605       host->skip_num = interval_g;
606       WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
607           host->name, host->skip_num);
608     }
609   } /* for (host) */
610
611   return (0);
612 }
613
614 #if 0
615 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
616 {
617   value_t values[3];
618   value_list_t vl = VALUE_LIST_INIT;
619
620   values[0].gauge = snum;
621   values[1].gauge = mnum;
622   values[2].gauge = lnum;
623
624   vl.values = values;
625   vl.values_len = STATIC_ARRAY_SIZE (values);
626   vl.time = time (NULL);
627   strcpy (vl.host, hostname_g);
628   strcpy (vl.plugin, "load");
629
630   plugin_dispatch_values ("load", &vl);
631 }
632 #endif
633
634 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
635 {
636   value_t ret;
637   uint64_t temp = 0;
638   int defined = 1;
639
640   if ((vl->type == ASN_INTEGER)
641       || (vl->type == ASN_UINTEGER)
642       || (vl->type == ASN_COUNTER)
643       || (vl->type == ASN_GAUGE))
644   {
645     temp = (uint32_t) *vl->val.integer;
646     DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
647   }
648   else if (vl->type == ASN_COUNTER64)
649   {
650     temp = (uint32_t) vl->val.counter64->high;
651     temp = temp << 32;
652     temp += (uint32_t) vl->val.counter64->low;
653     DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
654   }
655   else
656   {
657     WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
658     defined = 0;
659   }
660
661   if (type == DS_TYPE_COUNTER)
662   {
663     ret.counter = temp;
664   }
665   else if (type == DS_TYPE_GAUGE)
666   {
667     ret.gauge = NAN;
668     if (defined != 0)
669       ret.gauge = temp;
670   }
671
672   return (ret);
673 } /* value_t csnmp_value_list_to_value */
674
675 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
676     csnmp_list_instances_t *instance_list,
677     csnmp_table_values_t **value_table)
678 {
679   const data_set_t *ds;
680   value_list_t vl = VALUE_LIST_INIT;
681
682   csnmp_list_instances_t *instance_list_ptr;
683   csnmp_table_values_t **value_table_ptr;
684
685   int i;
686
687   ds = plugin_get_ds (data->type);
688   if (!ds)
689   {
690     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
691     return (-1);
692   }
693   assert (ds->ds_num == data->values_len);
694
695   value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
696       * data->values_len);
697   if (value_table_ptr == NULL)
698     return (-1);
699   for (i = 0; i < data->values_len; i++)
700     value_table_ptr[i] = value_table[i];
701
702   vl.values_len = ds->ds_num;
703   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
704   if (vl.values == NULL)
705   {
706     sfree (value_table_ptr);
707     return (-1);
708   }
709
710   strncpy (vl.host, host->name, sizeof (vl.host));
711   vl.host[sizeof (vl.host) - 1] = '\0';
712   strcpy (vl.plugin, "snmp");
713
714   vl.interval = host->skip_num;
715   vl.time = time (NULL);
716
717   for (instance_list_ptr = instance_list;
718       instance_list_ptr != NULL;
719       instance_list_ptr = instance_list_ptr->next)
720   {
721     strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
722     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
723
724     for (i = 0; i < data->values_len; i++)
725     {
726       while ((value_table_ptr[i] != NULL)
727           && (value_table_ptr[i]->subid < instance_list_ptr->subid))
728         value_table_ptr[i] = value_table_ptr[i]->next;
729       if ((value_table_ptr[i] == NULL)
730           || (value_table_ptr[i]->subid != instance_list_ptr->subid))
731         break;
732       vl.values[i] = value_table_ptr[i]->value;
733     } /* for (data->values_len) */
734
735     /* If the for-loop was aborted early, not all subid's match. */
736     if (i < data->values_len)
737     {
738       DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
739           "Skipping SUBID %i",
740           host->name, data->name, i, instance_list_ptr->subid);
741       continue;
742     }
743
744     /* If we get here `vl.type_instance' and all `vl.values' have been set */
745     plugin_dispatch_values (data->type, &vl);
746   } /* for (instance_list) */
747
748   sfree (vl.values);
749   sfree (value_table_ptr);
750
751   return (0);
752 } /* int csnmp_dispatch_table */
753
754 static int csnmp_read_table (struct snmp_session *sess_ptr,
755     host_definition_t *host, data_definition_t *data)
756 {
757   struct snmp_pdu *req;
758   struct snmp_pdu *res;
759   struct variable_list *vb;
760
761   const data_set_t *ds;
762   oid_t *oid_list;
763   uint32_t oid_list_len;
764
765   int status;
766   int i;
767
768   /* `value_table' and `value_table_ptr' implement a linked list for each
769    * value. `instance_list' and `instance_list_ptr' implement a linked list of
770    * instance names. This is used to jump gaps in the table. */
771   csnmp_list_instances_t *instance_list;
772   csnmp_list_instances_t *instance_list_ptr;
773   csnmp_table_values_t **value_table;
774   csnmp_table_values_t **value_table_ptr;
775
776   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
777       host->name, data->name);
778
779   ds = plugin_get_ds (data->type);
780   if (!ds)
781   {
782     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
783     return (-1);
784   }
785
786   if (ds->ds_num != data->values_len)
787   {
788     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
789         data->type, ds->ds_num, data->values_len);
790     return (-1);
791   }
792
793   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
794   oid_list_len = data->values_len + 1;
795   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
796   if (oid_list == NULL)
797     return (-1);
798   memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
799   for (i = 0; i < data->values_len; i++)
800     memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
801
802   /* Allocate the `value_table' */
803   value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
804       * 2 * data->values_len);
805   if (value_table == NULL)
806   {
807     sfree (oid_list);
808     return (-1);
809   }
810   memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
811   value_table_ptr = value_table + data->values_len;
812   
813   instance_list = NULL;
814   instance_list_ptr = NULL;
815
816   status = 0;
817   while (status == 0)
818   {
819     csnmp_list_instances_t *il;
820
821     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
822     if (req == NULL)
823     {
824       ERROR ("snmp plugin: snmp_pdu_create failed.");
825       status = -1;
826       break;
827     }
828
829     for (i = 0; i < oid_list_len; i++)
830       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
831
832     status = snmp_synch_response (sess_ptr, req, &res);
833
834     if (status != STAT_SUCCESS)
835     {
836       ERROR ("snmp plugin: snmp_synch_response failed.");
837       status = -1;
838       break;
839     }
840     status = 0;
841     assert (res != NULL);
842
843     vb = res->variables;
844     if (vb == NULL)
845     {
846       status = -1;
847       break;
848     }
849
850     /* Check if we left the subtree */
851     if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
852           vb->name, vb->name_length,
853           data->instance.oid.oid_len) != 0)
854       break;
855
856     /* Allocate a new `csnmp_list_instances_t', insert the instance name and
857      * add it to the list */
858     il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
859     if (il == NULL)
860     {
861       status = -1;
862       break;
863     }
864     il->subid = vb->name[vb->name_length - 1];
865     il->next = NULL;
866
867     /* Get instance name */
868     if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
869     {
870       char *ptr;
871       size_t instance_len;
872
873       instance_len = sizeof (il->instance) - 1;
874       if (instance_len > vb->val_len)
875         instance_len = vb->val_len;
876
877       strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
878             ? vb->val.string
879             : vb->val.bitstring),
880           instance_len);
881       il->instance[instance_len] = '\0';
882
883       for (ptr = il->instance; *ptr != '\0'; ptr++)
884       {
885         if ((*ptr > 0) && (*ptr < 32))
886           *ptr = ' ';
887         else if (*ptr == '/')
888           *ptr = '_';
889       }
890       DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
891     }
892     else
893     {
894       value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
895       snprintf (il->instance, sizeof (il->instance),
896           "%llu", val.counter);
897     }
898     il->instance[sizeof (il->instance) - 1] = '\0';
899     DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
900         data->name, il->instance);
901
902     if (instance_list_ptr == NULL)
903       instance_list = il;
904     else
905       instance_list_ptr->next = il;
906     instance_list_ptr = il;
907
908     /* Copy OID to oid_list[0] */
909     memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
910     oid_list[0].oid_len = vb->name_length;
911
912     for (i = 0; i < data->values_len; i++)
913     {
914       csnmp_table_values_t *vt;
915
916       vb = vb->next_variable;
917       if (vb == NULL)
918       {
919         status = -1;
920         break;
921       }
922
923       /* Check if we left the subtree */
924       if (snmp_oid_ncompare (data->values[i].oid,
925             data->values[i].oid_len,
926             vb->name, vb->name_length,
927             data->values[i].oid_len) != 0)
928       {
929         DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
930             host->name, data->name, i);
931         continue;
932       }
933
934       if ((value_table_ptr[i] != NULL)
935           && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
936       {
937         DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
938             host->name, data->name, i);
939         continue;
940       }
941
942       vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
943       if (vt != NULL)
944       {
945         vt->subid = vb->name[vb->name_length - 1];
946         vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
947         vt->next = NULL;
948
949         if (value_table_ptr[i] == NULL)
950           value_table[i] = vt;
951         else
952           value_table_ptr[i]->next = vt;
953         value_table_ptr[i] = vt;
954       }
955
956       /* Copy OID to oid_list[i + 1] */
957       memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
958       oid_list[i + 1].oid_len = vb->name_length;
959     } /* for (i = data->values_len) */
960
961     if (res != NULL)
962       snmp_free_pdu (res);
963     res = NULL;
964   } /* while (status == 0) */
965
966   if (status == 0)
967     csnmp_dispatch_table (host, data, instance_list, value_table);
968
969   /* Free all allocated variables here */
970   while (instance_list != NULL)
971   {
972     instance_list_ptr = instance_list->next;
973     sfree (instance_list);
974     instance_list = instance_list_ptr;
975   }
976
977   for (i = 0; i < data->values_len; i++)
978   {
979     csnmp_table_values_t *tmp;
980     while (value_table[i] != NULL)
981     {
982       tmp = value_table[i]->next;
983       sfree (value_table[i]);
984       value_table[i] = tmp;
985     }
986   }
987
988   sfree (value_table);
989   sfree (oid_list);
990
991   return (0);
992 } /* int csnmp_read_table */
993
994 static int csnmp_read_value (struct snmp_session *sess_ptr,
995     host_definition_t *host, data_definition_t *data)
996 {
997   struct snmp_pdu *req;
998   struct snmp_pdu *res;
999   struct variable_list *vb;
1000
1001   const data_set_t *ds;
1002   value_list_t vl = VALUE_LIST_INIT;
1003
1004   int status;
1005   int i;
1006
1007   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1008       host->name, data->name);
1009
1010   ds = plugin_get_ds (data->type);
1011   if (!ds)
1012   {
1013     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1014     return (-1);
1015   }
1016
1017   if (ds->ds_num != data->values_len)
1018   {
1019     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1020         data->type, ds->ds_num, data->values_len);
1021     return (-1);
1022   }
1023
1024   vl.values_len = ds->ds_num;
1025   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1026   if (vl.values == NULL)
1027     return (-1);
1028   for (i = 0; i < vl.values_len; i++)
1029   {
1030     if (ds->ds[i].type == DS_TYPE_COUNTER)
1031       vl.values[i].counter = 0;
1032     else
1033       vl.values[i].gauge = NAN;
1034   }
1035
1036   strncpy (vl.host, host->name, sizeof (vl.host));
1037   vl.host[sizeof (vl.host) - 1] = '\0';
1038   strcpy (vl.plugin, "snmp");
1039   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1040   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1041
1042   vl.interval = host->skip_num;
1043
1044   req = snmp_pdu_create (SNMP_MSG_GET);
1045   if (req == NULL)
1046   {
1047     ERROR ("snmp plugin: snmp_pdu_create failed.");
1048     sfree (vl.values);
1049     return (-1);
1050   }
1051
1052   for (i = 0; i < data->values_len; i++)
1053     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1054   status = snmp_synch_response (sess_ptr, req, &res);
1055
1056   if (status != STAT_SUCCESS)
1057   {
1058     ERROR ("snmp plugin: snmp_synch_response failed.");
1059     sfree (vl.values);
1060     return (-1);
1061   }
1062
1063   vl.time = time (NULL);
1064
1065   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1066   {
1067     char buffer[1024];
1068     snprint_variable (buffer, sizeof (buffer),
1069         vb->name, vb->name_length, vb);
1070     DEBUG ("snmp plugin: Got this variable: %s", buffer);
1071
1072     for (i = 0; i < data->values_len; i++)
1073       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1074             vb->name, vb->name_length) == 0)
1075         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1076   } /* for (res->variables) */
1077
1078   snmp_free_pdu (res);
1079
1080   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1081   plugin_dispatch_values (data->type, &vl);
1082   sfree (vl.values);
1083
1084   return (0);
1085 } /* int csnmp_read_value */
1086
1087 static int csnmp_read_host (host_definition_t *host)
1088 {
1089   struct snmp_session *sess_ptr;
1090   int i;
1091
1092   DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1093
1094   sess_ptr = snmp_open (&host->sess);
1095   if (sess_ptr == NULL)
1096   {
1097     snmp_perror ("snmp_open");
1098     ERROR ("snmp plugin: snmp_open failed.");
1099     return (-1);
1100   }
1101
1102   for (i = 0; i < host->data_list_len; i++)
1103   {
1104     data_definition_t *data = host->data_list[i];
1105
1106     if (data->is_table)
1107       csnmp_read_table (sess_ptr, host, data);
1108     else
1109       csnmp_read_value (sess_ptr, host, data);
1110   }
1111
1112   snmp_close (sess_ptr);
1113   return (0);
1114 } /* int csnmp_read_host */
1115
1116 static int csnmp_read (void)
1117 {
1118   host_definition_t *host;
1119   time_t now;
1120
1121   if (host_head == NULL)
1122   {
1123     INFO ("snmp plugin: No hosts configured.");
1124     return (-1);
1125   }
1126
1127   now = time (NULL);
1128
1129   for (host = host_head; host != NULL; host = host->next)
1130   {
1131     host->skip_left -= interval_g;
1132     if (host->skip_left >= interval_g)
1133       continue;
1134
1135     csnmp_read_host (host);
1136
1137     host->skip_left = host->skip_num;
1138   } /* for (host) */
1139
1140   return (0);
1141 } /* int csnmp_read */
1142
1143 void module_register (void)
1144 {
1145   plugin_register_complex_config ("snmp", csnmp_config);
1146   plugin_register_init ("snmp", csnmp_init);
1147   plugin_register_read ("snmp", csnmp_read);
1148 } /* void module_register */
1149
1150 /*
1151  * vim: shiftwidth=2 softtabstop=2 tabstop=8
1152  */