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