Merge branch 'pull/collectd-4.1' into collectd-4.1
[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   uint32_t interval;
68   time_t next_update;
69   data_definition_t **data_list;
70   int data_list_len;
71   enum          /******************************************************/
72   {             /* This host..                                        */
73     STATE_IDLE, /* - just sits there until `next_update < 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   if ((ci->values_num != 1)
460       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
461   {
462     WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
463     return (-1);
464   }
465
466   hd->interval = (int) ci->values[0].value.number;
467   if (hd->interval < 0)
468     hd->interval = 0;
469
470   return (0);
471 } /* int csnmp_config_add_host_interval */
472
473 static int csnmp_config_add_host (oconfig_item_t *ci)
474 {
475   host_definition_t *hd;
476   int status = 0;
477   int i;
478
479   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
480   {
481     WARNING ("snmp plugin: `Host' needs exactly one string argument.");
482     return (-1);
483   }
484
485   hd = (host_definition_t *) malloc (sizeof (host_definition_t));
486   if (hd == NULL)
487     return (-1);
488   memset (hd, '\0', sizeof (host_definition_t));
489   hd->version = 2;
490
491   hd->name = strdup (ci->values[0].value.string);
492   if (hd->name == NULL)
493   {
494     free (hd);
495     return (-1);
496   }
497
498   hd->sess_handle = NULL;
499   hd->interval = 0;
500   hd->next_update = 0;
501   hd->state = STATE_IDLE;
502
503   for (i = 0; i < ci->children_num; i++)
504   {
505     oconfig_item_t *option = ci->children + i;
506     status = 0;
507
508     if (strcasecmp ("Address", option->key) == 0)
509       status = csnmp_config_add_host_address (hd, option);
510     else if (strcasecmp ("Community", option->key) == 0)
511       status = csnmp_config_add_host_community (hd, option);
512     else if (strcasecmp ("Version", option->key) == 0)
513       status = csnmp_config_add_host_version (hd, option);
514     else if (strcasecmp ("Collect", option->key) == 0)
515       csnmp_config_add_host_collect (hd, option);
516     else if (strcasecmp ("Interval", option->key) == 0)
517       csnmp_config_add_host_interval (hd, option);
518     else
519     {
520       WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
521       status = -1;
522     }
523
524     if (status != 0)
525       break;
526   } /* for (ci->children) */
527
528   while (status == 0)
529   {
530     if (hd->address == NULL)
531     {
532       WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
533       status = -1;
534       break;
535     }
536     if (hd->community == NULL)
537     {
538       WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
539       status = -1;
540       break;
541     }
542
543     break;
544   } /* while (status == 0) */
545
546   if (status != 0)
547   {
548     sfree (hd->name);
549     sfree (hd);
550     return (-1);
551   }
552
553   DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
554       hd->name, hd->address, hd->community, hd->version);
555
556   if (host_head == NULL)
557     host_head = hd;
558   else
559   {
560     host_definition_t *last;
561     last = host_head;
562     while (last->next != NULL)
563       last = last->next;
564     last->next = hd;
565   }
566
567   return (0);
568 } /* int csnmp_config_add_host */
569
570 static int csnmp_config (oconfig_item_t *ci)
571 {
572   int i;
573
574   call_snmp_init_once ();
575
576   for (i = 0; i < ci->children_num; i++)
577   {
578     oconfig_item_t *child = ci->children + i;
579     if (strcasecmp ("Data", child->key) == 0)
580       csnmp_config_add_data (child);
581     else if (strcasecmp ("Host", child->key) == 0)
582       csnmp_config_add_host (child);
583     else
584     {
585       WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
586     }
587   } /* for (ci->children) */
588
589   return (0);
590 } /* int csnmp_config */
591
592 /* End of the config stuff. Now the interesting part begins */
593
594 static void csnmp_host_close_session (host_definition_t *host)
595 {
596   if (host->sess_handle == NULL)
597     return;
598
599   snmp_sess_close (host->sess_handle);
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: host %s: snmp_sess_open failed: %s",
626         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
627     sfree (errstr);
628   }
629 } /* void csnmp_host_open_session */
630
631 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
632 {
633   value_t ret;
634   uint64_t temp = 0;
635   int defined = 1;
636
637   if ((vl->type == ASN_INTEGER)
638       || (vl->type == ASN_UINTEGER)
639       || (vl->type == ASN_COUNTER)
640 #ifdef ASN_TIMETICKS
641       || (vl->type == ASN_TIMETICKS)
642 #endif
643       || (vl->type == ASN_GAUGE))
644   {
645     temp = (uint32_t) *vl->val.integer;
646     DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
647   }
648   else if (vl->type == ASN_COUNTER64)
649   {
650     temp = (uint32_t) vl->val.counter64->high;
651     temp = temp << 32;
652     temp += (uint32_t) vl->val.counter64->low;
653     DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
654   }
655   else
656   {
657     WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
658     defined = 0;
659   }
660
661   if (type == DS_TYPE_COUNTER)
662   {
663     ret.counter = temp;
664   }
665   else if (type == DS_TYPE_GAUGE)
666   {
667     ret.gauge = NAN;
668     if (defined != 0)
669       ret.gauge = temp;
670   }
671
672   return (ret);
673 } /* value_t csnmp_value_list_to_value */
674
675 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
676     csnmp_list_instances_t *instance_list,
677     csnmp_table_values_t **value_table)
678 {
679   const data_set_t *ds;
680   value_list_t vl = VALUE_LIST_INIT;
681
682   csnmp_list_instances_t *instance_list_ptr;
683   csnmp_table_values_t **value_table_ptr;
684
685   int i;
686
687   ds = plugin_get_ds (data->type);
688   if (!ds)
689   {
690     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
691     return (-1);
692   }
693   assert (ds->ds_num == data->values_len);
694
695   value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
696       * data->values_len);
697   if (value_table_ptr == NULL)
698     return (-1);
699   for (i = 0; i < data->values_len; i++)
700     value_table_ptr[i] = value_table[i];
701
702   vl.values_len = ds->ds_num;
703   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
704   if (vl.values == NULL)
705   {
706     sfree (value_table_ptr);
707     return (-1);
708   }
709
710   strncpy (vl.host, host->name, sizeof (vl.host));
711   vl.host[sizeof (vl.host) - 1] = '\0';
712   strcpy (vl.plugin, "snmp");
713
714   vl.interval = host->interval;
715   vl.time = time (NULL);
716
717   for (instance_list_ptr = instance_list;
718       instance_list_ptr != NULL;
719       instance_list_ptr = instance_list_ptr->next)
720   {
721     strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
722     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
723
724     for (i = 0; i < data->values_len; i++)
725     {
726       while ((value_table_ptr[i] != NULL)
727           && (value_table_ptr[i]->subid < instance_list_ptr->subid))
728         value_table_ptr[i] = value_table_ptr[i]->next;
729       if ((value_table_ptr[i] == NULL)
730           || (value_table_ptr[i]->subid != instance_list_ptr->subid))
731         break;
732       vl.values[i] = value_table_ptr[i]->value;
733     } /* for (data->values_len) */
734
735     /* If the for-loop was aborted early, not all subid's match. */
736     if (i < data->values_len)
737     {
738       DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
739           "Skipping SUBID %i",
740           host->name, data->name, i, instance_list_ptr->subid);
741       continue;
742     }
743
744     /* If we get here `vl.type_instance' and all `vl.values' have been set */
745     plugin_dispatch_values (data->type, &vl);
746   } /* for (instance_list) */
747
748   sfree (vl.values);
749   sfree (value_table_ptr);
750
751   return (0);
752 } /* int csnmp_dispatch_table */
753
754 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
755 {
756   struct snmp_pdu *req;
757   struct snmp_pdu *res;
758   struct variable_list *vb;
759
760   const data_set_t *ds;
761   oid_t *oid_list;
762   uint32_t oid_list_len;
763
764   int status;
765   int i;
766
767   /* `value_table' and `value_table_ptr' implement a linked list for each
768    * value. `instance_list' and `instance_list_ptr' implement a linked list of
769    * instance names. This is used to jump gaps in the table. */
770   csnmp_list_instances_t *instance_list;
771   csnmp_list_instances_t *instance_list_ptr;
772   csnmp_table_values_t **value_table;
773   csnmp_table_values_t **value_table_ptr;
774
775   DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
776       host->name, data->name);
777
778   if (host->sess_handle == NULL)
779   {
780     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
781     return (-1);
782   }
783
784   ds = plugin_get_ds (data->type);
785   if (!ds)
786   {
787     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
788     return (-1);
789   }
790
791   if (ds->ds_num != data->values_len)
792   {
793     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
794         data->type, ds->ds_num, data->values_len);
795     return (-1);
796   }
797
798   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
799   oid_list_len = data->values_len + 1;
800   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
801   if (oid_list == NULL)
802     return (-1);
803   memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
804   for (i = 0; i < data->values_len; i++)
805     memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
806
807   /* Allocate the `value_table' */
808   value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
809       * 2 * data->values_len);
810   if (value_table == NULL)
811   {
812     sfree (oid_list);
813     return (-1);
814   }
815   memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
816   value_table_ptr = value_table + data->values_len;
817   
818   instance_list = NULL;
819   instance_list_ptr = NULL;
820
821   status = 0;
822   while (status == 0)
823   {
824     csnmp_list_instances_t *il;
825
826     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
827     if (req == NULL)
828     {
829       ERROR ("snmp plugin: snmp_pdu_create failed.");
830       status = -1;
831       break;
832     }
833
834     for (i = 0; i < oid_list_len; i++)
835       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
836
837     res = NULL;
838     status = snmp_sess_synch_response (host->sess_handle, req, &res);
839
840     if ((status != STAT_SUCCESS) || (res == NULL))
841     {
842       char *errstr = NULL;
843
844       snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
845       ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
846           host->name, (errstr == NULL) ? "Unknown problem" : errstr);
847
848       if (res != NULL)
849         snmp_free_pdu (res);
850       res = NULL;
851
852       sfree (errstr);
853       csnmp_host_close_session (host);
854
855       status = -1;
856       break;
857     }
858     status = 0;
859     assert (res != NULL);
860
861     vb = res->variables;
862     if (vb == NULL)
863     {
864       if (res != NULL)
865         snmp_free_pdu (res);
866       res = NULL;
867
868       status = -1;
869       break;
870     }
871
872     /* Check if we left the subtree */
873     if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
874           vb->name, vb->name_length,
875           data->instance.oid.oid_len) != 0)
876     {
877       if (res != NULL)
878         snmp_free_pdu (res);
879       res = NULL;
880
881       break;
882     }
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       if (res != NULL)
890         snmp_free_pdu (res);
891       res = NULL;
892
893       status = -1;
894       break;
895     }
896     il->subid = vb->name[vb->name_length - 1];
897     il->next = NULL;
898
899     /* Get instance name */
900     if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
901     {
902       char *ptr;
903       size_t instance_len;
904
905       instance_len = sizeof (il->instance) - 1;
906       if (instance_len > vb->val_len)
907         instance_len = vb->val_len;
908
909       strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
910             ? vb->val.string
911             : vb->val.bitstring),
912           instance_len);
913       il->instance[instance_len] = '\0';
914
915       for (ptr = il->instance; *ptr != '\0'; ptr++)
916       {
917         if ((*ptr > 0) && (*ptr < 32))
918           *ptr = ' ';
919         else if (*ptr == '/')
920           *ptr = '_';
921       }
922       DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
923     }
924     else
925     {
926       value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
927       snprintf (il->instance, sizeof (il->instance),
928           "%llu", val.counter);
929     }
930     il->instance[sizeof (il->instance) - 1] = '\0';
931     DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
932         data->name, il->instance);
933
934     if (instance_list_ptr == NULL)
935       instance_list = il;
936     else
937       instance_list_ptr->next = il;
938     instance_list_ptr = il;
939
940     /* Copy OID to oid_list[0] */
941     memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
942     oid_list[0].oid_len = vb->name_length;
943
944     for (i = 0; i < data->values_len; i++)
945     {
946       csnmp_table_values_t *vt;
947
948       vb = vb->next_variable;
949       if (vb == NULL)
950       {
951         status = -1;
952         break;
953       }
954
955       /* Check if we left the subtree */
956       if (snmp_oid_ncompare (data->values[i].oid,
957             data->values[i].oid_len,
958             vb->name, vb->name_length,
959             data->values[i].oid_len) != 0)
960       {
961         DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
962             host->name, data->name, i);
963         continue;
964       }
965
966       if ((value_table_ptr[i] != NULL)
967           && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
968       {
969         DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
970             host->name, data->name, i);
971         continue;
972       }
973
974       vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
975       if (vt != NULL)
976       {
977         vt->subid = vb->name[vb->name_length - 1];
978         vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
979         vt->next = NULL;
980
981         if (value_table_ptr[i] == NULL)
982           value_table[i] = vt;
983         else
984           value_table_ptr[i]->next = vt;
985         value_table_ptr[i] = vt;
986       }
987
988       /* Copy OID to oid_list[i + 1] */
989       memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
990       oid_list[i + 1].oid_len = vb->name_length;
991     } /* for (i = data->values_len) */
992
993     if (res != NULL)
994       snmp_free_pdu (res);
995     res = NULL;
996   } /* while (status == 0) */
997
998   if (status == 0)
999     csnmp_dispatch_table (host, data, instance_list, value_table);
1000
1001   /* Free all allocated variables here */
1002   while (instance_list != NULL)
1003   {
1004     instance_list_ptr = instance_list->next;
1005     sfree (instance_list);
1006     instance_list = instance_list_ptr;
1007   }
1008
1009   for (i = 0; i < data->values_len; i++)
1010   {
1011     csnmp_table_values_t *tmp;
1012     while (value_table[i] != NULL)
1013     {
1014       tmp = value_table[i]->next;
1015       sfree (value_table[i]);
1016       value_table[i] = tmp;
1017     }
1018   }
1019
1020   sfree (value_table);
1021   sfree (oid_list);
1022
1023   return (0);
1024 } /* int csnmp_read_table */
1025
1026 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1027 {
1028   struct snmp_pdu *req;
1029   struct snmp_pdu *res;
1030   struct variable_list *vb;
1031
1032   const data_set_t *ds;
1033   value_list_t vl = VALUE_LIST_INIT;
1034
1035   int status;
1036   int i;
1037
1038   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1039       host->name, data->name);
1040
1041   if (host->sess_handle == NULL)
1042   {
1043     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1044     return (-1);
1045   }
1046
1047   ds = plugin_get_ds (data->type);
1048   if (!ds)
1049   {
1050     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1051     return (-1);
1052   }
1053
1054   if (ds->ds_num != data->values_len)
1055   {
1056     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1057         data->type, ds->ds_num, data->values_len);
1058     return (-1);
1059   }
1060
1061   vl.values_len = ds->ds_num;
1062   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1063   if (vl.values == NULL)
1064     return (-1);
1065   for (i = 0; i < vl.values_len; i++)
1066   {
1067     if (ds->ds[i].type == DS_TYPE_COUNTER)
1068       vl.values[i].counter = 0;
1069     else
1070       vl.values[i].gauge = NAN;
1071   }
1072
1073   strncpy (vl.host, host->name, sizeof (vl.host));
1074   vl.host[sizeof (vl.host) - 1] = '\0';
1075   strcpy (vl.plugin, "snmp");
1076   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1077   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1078
1079   vl.interval = host->interval;
1080
1081   req = snmp_pdu_create (SNMP_MSG_GET);
1082   if (req == NULL)
1083   {
1084     ERROR ("snmp plugin: snmp_pdu_create failed.");
1085     sfree (vl.values);
1086     return (-1);
1087   }
1088
1089   for (i = 0; i < data->values_len; i++)
1090     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1091
1092   res = NULL;
1093   status = snmp_sess_synch_response (host->sess_handle, req, &res);
1094
1095   if ((status != STAT_SUCCESS) || (res == NULL))
1096   {
1097     char *errstr = NULL;
1098
1099     snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1100     ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1101         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1102
1103     if (res != NULL)
1104       snmp_free_pdu (res);
1105     res = NULL;
1106
1107     sfree (errstr);
1108     csnmp_host_close_session (host);
1109
1110     return (-1);
1111   }
1112
1113   vl.time = time (NULL);
1114
1115   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1116   {
1117 #if COLLECT_DEBUG
1118     char buffer[1024];
1119     snprint_variable (buffer, sizeof (buffer),
1120         vb->name, vb->name_length, vb);
1121     DEBUG ("snmp plugin: Got this variable: %s", buffer);
1122 #endif /* COLLECT_DEBUG */
1123
1124     for (i = 0; i < data->values_len; i++)
1125       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1126             vb->name, vb->name_length) == 0)
1127         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
1128   } /* for (res->variables) */
1129
1130   if (res != NULL)
1131     snmp_free_pdu (res);
1132   res = NULL;
1133
1134   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1135   plugin_dispatch_values (data->type, &vl);
1136   sfree (vl.values);
1137
1138   return (0);
1139 } /* int csnmp_read_value */
1140
1141 static int csnmp_read_host (host_definition_t *host)
1142 {
1143   int i;
1144   time_t time_start;
1145   time_t time_end;
1146
1147   time_start = time (NULL);
1148   DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1149       (unsigned int) time_start);
1150
1151   if (host->sess_handle == NULL)
1152     csnmp_host_open_session (host);
1153
1154   if (host->sess_handle == NULL)
1155     return (-1);
1156
1157   for (i = 0; i < host->data_list_len; i++)
1158   {
1159     data_definition_t *data = host->data_list[i];
1160
1161     if (data->is_table)
1162       csnmp_read_table (host, data);
1163     else
1164       csnmp_read_value (host, data);
1165   }
1166
1167   time_end = time (NULL);
1168   DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1169       (unsigned int) time_end);
1170   if ((time_end - time_start) > host->interval)
1171   {
1172     WARNING ("snmp plugin: Host `%s' should be queried every %i seconds, "
1173         "but reading all values takes %i seconds.",
1174         host->name, host->interval, time_end - time_start);
1175   }
1176
1177   return (0);
1178 } /* int csnmp_read_host */
1179
1180 static void *csnmp_read_thread (void *data)
1181 {
1182   host_definition_t *host;
1183
1184   pthread_mutex_lock (&host_lock);
1185   while (do_shutdown == 0)
1186   {
1187     pthread_cond_wait (&host_cond, &host_lock);
1188
1189     for (host = host_head; host != NULL; host = host->next)
1190     {
1191       if (do_shutdown != 0)
1192         break;
1193       if (host->state != STATE_WAIT)
1194         continue;
1195
1196       host->state = STATE_BUSY;
1197       pthread_mutex_unlock (&host_lock);
1198       csnmp_read_host (host);
1199       pthread_mutex_lock (&host_lock);
1200       host->state = STATE_IDLE;
1201     } /* for (host) */
1202   } /* while (do_shutdown == 0) */
1203   pthread_mutex_unlock (&host_lock);
1204
1205   pthread_exit ((void *) 0);
1206   return ((void *) 0);
1207 } /* void *csnmp_read_thread */
1208
1209 static int csnmp_init (void)
1210 {
1211   host_definition_t *host;
1212   int i;
1213
1214   if (host_head == NULL)
1215   {
1216     NOTICE ("snmp plugin: No host has been defined.");
1217     return (-1);
1218   }
1219
1220   call_snmp_init_once ();
1221
1222   threads_num = 0;
1223   for (host = host_head; host != NULL; host = host->next)
1224   {
1225     threads_num++;
1226     /* We need to initialize `interval' here, because `interval_g' isn't
1227      * initialized during `configure'. */
1228     host->next_update = time (NULL);
1229     if (host->interval == 0)
1230     {
1231       host->interval = interval_g;
1232     }
1233     else if (host->interval < interval_g)
1234     {
1235       host->interval = interval_g;
1236       WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1237           host->name, host->interval);
1238     }
1239
1240     csnmp_host_open_session (host);
1241   } /* for (host) */
1242
1243   /* Now start the reading threads */
1244   if (threads_num > 3)
1245   {
1246     threads_num = 3 + ((threads_num - 3) / 10);
1247     if (threads_num > 10)
1248       threads_num = 10;
1249   }
1250
1251   threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1252   if (threads == NULL)
1253   {
1254     ERROR ("snmp plugin: malloc failed.");
1255     return (-1);
1256   }
1257   memset (threads, '\0', threads_num * sizeof (pthread_t));
1258
1259   for (i = 0; i < threads_num; i++)
1260       pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1261
1262   return (0);
1263 } /* int csnmp_init */
1264
1265 static int csnmp_read (void)
1266 {
1267   host_definition_t *host;
1268   time_t now;
1269
1270   if (host_head == NULL)
1271   {
1272     INFO ("snmp plugin: No hosts configured.");
1273     return (-1);
1274   }
1275
1276   now = time (NULL);
1277
1278   pthread_mutex_lock (&host_lock);
1279   for (host = host_head; host != NULL; host = host->next)
1280   {
1281     if (host->state != STATE_IDLE)
1282       continue;
1283
1284     /* Skip this host if the next or a later iteration will be sufficient. */
1285     if (host->next_update >= (now + interval_g))
1286       continue;
1287
1288     host->state = STATE_WAIT;
1289     host->next_update = now + host->interval;
1290   } /* for (host) */
1291
1292   pthread_cond_broadcast (&host_cond);
1293   pthread_mutex_unlock (&host_lock);
1294
1295   return (0);
1296 } /* int csnmp_read */
1297
1298 static int csnmp_shutdown (void)
1299 {
1300   host_definition_t *host_this;
1301   host_definition_t *host_next;
1302
1303   data_definition_t *data_this;
1304   data_definition_t *data_next;
1305
1306   int i;
1307
1308   pthread_mutex_lock (&host_lock);
1309   do_shutdown = 1;
1310   pthread_cond_broadcast (&host_cond);
1311   pthread_mutex_unlock (&host_lock);
1312
1313   for (i = 0; i < threads_num; i++)
1314     pthread_join (threads[i], NULL);
1315
1316   /* Now that all the threads have exited, let's free all the global variables.
1317    * This isn't really neccessary, I guess, but I think it's good stile to do
1318    * so anyway. */
1319   host_this = host_head;
1320   host_head = NULL;
1321   while (host_this != NULL)
1322   {
1323     host_next = host_this->next;
1324
1325     csnmp_host_close_session (host_this);
1326
1327     sfree (host_this->name);
1328     sfree (host_this->address);
1329     sfree (host_this->community);
1330     sfree (host_this->data_list);
1331     sfree (host_this);
1332
1333     host_this = host_next;
1334   }
1335
1336   data_this = data_head;
1337   data_head = NULL;
1338   while (data_this != NULL)
1339   {
1340     data_next = data_this->next;
1341
1342     sfree (data_this->name);
1343     sfree (data_this->type);
1344     sfree (data_this->values);
1345     sfree (data_this);
1346
1347     data_this = data_next;
1348   }
1349
1350   return (0);
1351 } /* int csnmp_shutdown */
1352
1353 void module_register (void)
1354 {
1355   plugin_register_complex_config ("snmp", csnmp_config);
1356   plugin_register_init ("snmp", csnmp_init);
1357   plugin_register_read ("snmp", csnmp_read);
1358   plugin_register_shutdown ("snmp", csnmp_shutdown);
1359 } /* void module_register */
1360
1361 /*
1362  * vim: shiftwidth=2 softtabstop=2 tabstop=8
1363  */