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