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