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