snmp plugin: Added the options `Scale' and `Shift' to Data-blocks..
[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: snmp_sess_close failed: %s",
649         (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: snmp_sess_open failed: %s",
679         (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       || (vl->type == ASN_GAUGE))
695   {
696     temp = (uint32_t) *vl->val.integer;
697     DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
698   }
699   else if (vl->type == ASN_COUNTER64)
700   {
701     temp = (uint32_t) vl->val.counter64->high;
702     temp = temp << 32;
703     temp += (uint32_t) vl->val.counter64->low;
704     DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
705   }
706   else
707   {
708     WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
709     defined = 0;
710   }
711
712   if (type == DS_TYPE_COUNTER)
713   {
714     ret.counter = temp;
715   }
716   else if (type == DS_TYPE_GAUGE)
717   {
718     ret.gauge = NAN;
719     if (defined != 0)
720       ret.gauge = (scale * temp) + shift;
721   }
722
723   return (ret);
724 } /* value_t csnmp_value_list_to_value */
725
726 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
727     csnmp_list_instances_t *instance_list,
728     csnmp_table_values_t **value_table)
729 {
730   const data_set_t *ds;
731   value_list_t vl = VALUE_LIST_INIT;
732
733   csnmp_list_instances_t *instance_list_ptr;
734   csnmp_table_values_t **value_table_ptr;
735
736   int i;
737
738   ds = plugin_get_ds (data->type);
739   if (!ds)
740   {
741     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
742     return (-1);
743   }
744   assert (ds->ds_num == data->values_len);
745
746   value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
747       * data->values_len);
748   if (value_table_ptr == NULL)
749     return (-1);
750   for (i = 0; i < data->values_len; i++)
751     value_table_ptr[i] = value_table[i];
752
753   vl.values_len = ds->ds_num;
754   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
755   if (vl.values == NULL)
756   {
757     sfree (value_table_ptr);
758     return (-1);
759   }
760
761   strncpy (vl.host, host->name, sizeof (vl.host));
762   vl.host[sizeof (vl.host) - 1] = '\0';
763   strcpy (vl.plugin, "snmp");
764
765   vl.interval = host->skip_num;
766   vl.time = time (NULL);
767
768   for (instance_list_ptr = instance_list;
769       instance_list_ptr != NULL;
770       instance_list_ptr = instance_list_ptr->next)
771   {
772     strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
773     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
774
775     for (i = 0; i < data->values_len; i++)
776     {
777       while ((value_table_ptr[i] != NULL)
778           && (value_table_ptr[i]->subid < instance_list_ptr->subid))
779         value_table_ptr[i] = value_table_ptr[i]->next;
780       if ((value_table_ptr[i] == NULL)
781           || (value_table_ptr[i]->subid != instance_list_ptr->subid))
782         break;
783       vl.values[i] = value_table_ptr[i]->value;
784     } /* for (data->values_len) */
785
786     /* If the for-loop was aborted early, not all subid's match. */
787     if (i < data->values_len)
788     {
789       DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
790           "Skipping SUBID %i",
791           host->name, data->name, i, instance_list_ptr->subid);
792       continue;
793     }
794
795     /* If we get here `vl.type_instance' and all `vl.values' have been set */
796     plugin_dispatch_values (data->type, &vl);
797   } /* for (instance_list) */
798
799   sfree (vl.values);
800   sfree (value_table_ptr);
801
802   return (0);
803 } /* int csnmp_dispatch_table */
804
805 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
806 {
807   struct snmp_pdu *req;
808   struct snmp_pdu *res;
809   struct variable_list *vb;
810
811   const data_set_t *ds;
812   oid_t *oid_list;
813   uint32_t oid_list_len;
814
815   int status;
816   int i;
817
818   /* `value_table' and `value_table_ptr' implement a linked list for each
819    * value. `instance_list' and `instance_list_ptr' implement a linked list of
820    * instance names. This is used to jump gaps in the table. */
821   csnmp_list_instances_t *instance_list;
822   csnmp_list_instances_t *instance_list_ptr;
823   csnmp_table_values_t **value_table;
824   csnmp_table_values_t **value_table_ptr;
825
826   DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
827       host->name, data->name);
828
829   ds = plugin_get_ds (data->type);
830   if (!ds)
831   {
832     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
833     return (-1);
834   }
835
836   if (ds->ds_num != data->values_len)
837   {
838     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
839         data->type, ds->ds_num, data->values_len);
840     return (-1);
841   }
842
843   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
844   oid_list_len = data->values_len + 1;
845   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
846   if (oid_list == NULL)
847     return (-1);
848   memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
849   for (i = 0; i < data->values_len; i++)
850     memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
851
852   /* Allocate the `value_table' */
853   value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
854       * 2 * data->values_len);
855   if (value_table == NULL)
856   {
857     sfree (oid_list);
858     return (-1);
859   }
860   memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
861   value_table_ptr = value_table + data->values_len;
862   
863   instance_list = NULL;
864   instance_list_ptr = NULL;
865
866   status = 0;
867   while (status == 0)
868   {
869     csnmp_list_instances_t *il;
870
871     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
872     if (req == NULL)
873     {
874       ERROR ("snmp plugin: snmp_pdu_create failed.");
875       status = -1;
876       break;
877     }
878
879     for (i = 0; i < oid_list_len; i++)
880       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
881
882     status = snmp_sess_synch_response (host->sess_handle, req, &res);
883
884     if (status != STAT_SUCCESS)
885     {
886       char *errstr = NULL;
887
888       snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
889       ERROR ("snmp plugin: snmp_sess_synch_response failed: %s",
890           (errstr == NULL) ? "Unknown problem" : errstr);
891       csnmp_host_close_session (host);
892
893       status = -1;
894       break;
895     }
896     status = 0;
897     assert (res != NULL);
898
899     vb = res->variables;
900     if (vb == NULL)
901     {
902       status = -1;
903       break;
904     }
905
906     /* Check if we left the subtree */
907     if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
908           vb->name, vb->name_length,
909           data->instance.oid.oid_len) != 0)
910       break;
911
912     /* Allocate a new `csnmp_list_instances_t', insert the instance name and
913      * add it to the list */
914     il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
915     if (il == NULL)
916     {
917       status = -1;
918       break;
919     }
920     il->subid = vb->name[vb->name_length - 1];
921     il->next = NULL;
922
923     /* Get instance name */
924     if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
925     {
926       char *ptr;
927       size_t instance_len;
928
929       instance_len = sizeof (il->instance) - 1;
930       if (instance_len > vb->val_len)
931         instance_len = vb->val_len;
932
933       strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
934             ? vb->val.string
935             : vb->val.bitstring),
936           instance_len);
937       il->instance[instance_len] = '\0';
938
939       for (ptr = il->instance; *ptr != '\0'; ptr++)
940       {
941         if ((*ptr > 0) && (*ptr < 32))
942           *ptr = ' ';
943         else if (*ptr == '/')
944           *ptr = '_';
945       }
946       DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
947     }
948     else
949     {
950       value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
951       snprintf (il->instance, sizeof (il->instance),
952           "%llu", val.counter);
953     }
954     il->instance[sizeof (il->instance) - 1] = '\0';
955     DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
956         data->name, il->instance);
957
958     if (instance_list_ptr == NULL)
959       instance_list = il;
960     else
961       instance_list_ptr->next = il;
962     instance_list_ptr = il;
963
964     /* Copy OID to oid_list[0] */
965     memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
966     oid_list[0].oid_len = vb->name_length;
967
968     for (i = 0; i < data->values_len; i++)
969     {
970       csnmp_table_values_t *vt;
971
972       vb = vb->next_variable;
973       if (vb == NULL)
974       {
975         status = -1;
976         break;
977       }
978
979       /* Check if we left the subtree */
980       if (snmp_oid_ncompare (data->values[i].oid,
981             data->values[i].oid_len,
982             vb->name, vb->name_length,
983             data->values[i].oid_len) != 0)
984       {
985         DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
986             host->name, data->name, i);
987         continue;
988       }
989
990       if ((value_table_ptr[i] != NULL)
991           && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
992       {
993         DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
994             host->name, data->name, i);
995         continue;
996       }
997
998       vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
999       if (vt != NULL)
1000       {
1001         vt->subid = vb->name[vb->name_length - 1];
1002         vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1003             data->scale, data->shift);
1004         vt->next = NULL;
1005
1006         if (value_table_ptr[i] == NULL)
1007           value_table[i] = vt;
1008         else
1009           value_table_ptr[i]->next = vt;
1010         value_table_ptr[i] = vt;
1011       }
1012
1013       /* Copy OID to oid_list[i + 1] */
1014       memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
1015       oid_list[i + 1].oid_len = vb->name_length;
1016     } /* for (i = data->values_len) */
1017
1018     if (res != NULL)
1019       snmp_free_pdu (res);
1020     res = NULL;
1021   } /* while (status == 0) */
1022
1023   if (status == 0)
1024     csnmp_dispatch_table (host, data, instance_list, value_table);
1025
1026   /* Free all allocated variables here */
1027   while (instance_list != NULL)
1028   {
1029     instance_list_ptr = instance_list->next;
1030     sfree (instance_list);
1031     instance_list = instance_list_ptr;
1032   }
1033
1034   for (i = 0; i < data->values_len; i++)
1035   {
1036     csnmp_table_values_t *tmp;
1037     while (value_table[i] != NULL)
1038     {
1039       tmp = value_table[i]->next;
1040       sfree (value_table[i]);
1041       value_table[i] = tmp;
1042     }
1043   }
1044
1045   sfree (value_table);
1046   sfree (oid_list);
1047
1048   return (0);
1049 } /* int csnmp_read_table */
1050
1051 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1052 {
1053   struct snmp_pdu *req;
1054   struct snmp_pdu *res;
1055   struct variable_list *vb;
1056
1057   const data_set_t *ds;
1058   value_list_t vl = VALUE_LIST_INIT;
1059
1060   int status;
1061   int i;
1062
1063   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1064       host->name, data->name);
1065
1066   ds = plugin_get_ds (data->type);
1067   if (!ds)
1068   {
1069     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1070     return (-1);
1071   }
1072
1073   if (ds->ds_num != data->values_len)
1074   {
1075     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1076         data->type, ds->ds_num, data->values_len);
1077     return (-1);
1078   }
1079
1080   vl.values_len = ds->ds_num;
1081   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1082   if (vl.values == NULL)
1083     return (-1);
1084   for (i = 0; i < vl.values_len; i++)
1085   {
1086     if (ds->ds[i].type == DS_TYPE_COUNTER)
1087       vl.values[i].counter = 0;
1088     else
1089       vl.values[i].gauge = NAN;
1090   }
1091
1092   strncpy (vl.host, host->name, sizeof (vl.host));
1093   vl.host[sizeof (vl.host) - 1] = '\0';
1094   strcpy (vl.plugin, "snmp");
1095   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1096   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1097
1098   vl.interval = host->skip_num;
1099
1100   req = snmp_pdu_create (SNMP_MSG_GET);
1101   if (req == NULL)
1102   {
1103     ERROR ("snmp plugin: snmp_pdu_create failed.");
1104     sfree (vl.values);
1105     return (-1);
1106   }
1107
1108   for (i = 0; i < data->values_len; i++)
1109     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1110   status = snmp_sess_synch_response (host->sess_handle, req, &res);
1111
1112   if (status != STAT_SUCCESS)
1113   {
1114     char *errstr = NULL;
1115
1116     snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1117     ERROR ("snmp plugin: snmp_sess_synch_response failed: %s",
1118         (errstr == NULL) ? "Unknown problem" : errstr);
1119     csnmp_host_close_session (host);
1120     sfree (errstr);
1121
1122     return (-1);
1123   }
1124
1125   vl.time = time (NULL);
1126
1127   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1128   {
1129     char buffer[1024];
1130     snprint_variable (buffer, sizeof (buffer),
1131         vb->name, vb->name_length, vb);
1132     DEBUG ("snmp plugin: Got this variable: %s", buffer);
1133
1134     for (i = 0; i < data->values_len; i++)
1135       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1136             vb->name, vb->name_length) == 0)
1137         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1138             data->scale, data->shift);
1139   } /* for (res->variables) */
1140
1141   snmp_free_pdu (res);
1142
1143   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1144   plugin_dispatch_values (data->type, &vl);
1145   sfree (vl.values);
1146
1147   return (0);
1148 } /* int csnmp_read_value */
1149
1150 static int csnmp_read_host (host_definition_t *host)
1151 {
1152   int i;
1153
1154   DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1155
1156   if (host->sess_handle == NULL)
1157     csnmp_host_open_session (host);
1158
1159   if (host->sess_handle == NULL)
1160     return (-1);
1161
1162   for (i = 0; i < host->data_list_len; i++)
1163   {
1164     data_definition_t *data = host->data_list[i];
1165
1166     if (data->is_table)
1167       csnmp_read_table (host, data);
1168     else
1169       csnmp_read_value (host, data);
1170   }
1171
1172   return (0);
1173 } /* int csnmp_read_host */
1174
1175 static void *csnmp_read_thread (void *data)
1176 {
1177   host_definition_t *host;
1178
1179   pthread_mutex_lock (&host_lock);
1180   while (do_shutdown == 0)
1181   {
1182     pthread_cond_wait (&host_cond, &host_lock);
1183
1184     for (host = host_head; host != NULL; host = host->next)
1185     {
1186       if (do_shutdown != 0)
1187         break;
1188       if (host->state != STATE_WAIT)
1189         continue;
1190
1191       host->state = STATE_BUSY;
1192       pthread_mutex_unlock (&host_lock);
1193       csnmp_read_host (host);
1194       pthread_mutex_lock (&host_lock);
1195       host->state = STATE_IDLE;
1196     } /* for (host) */
1197   } /* while (do_shutdown == 0) */
1198   pthread_mutex_unlock (&host_lock);
1199
1200   pthread_exit ((void *) 0);
1201   return ((void *) 0);
1202 } /* void *csnmp_read_thread */
1203
1204 static int csnmp_init (void)
1205 {
1206   host_definition_t *host;
1207   int i;
1208
1209   if (host_head == NULL)
1210     return (-1);
1211
1212   call_snmp_init_once ();
1213
1214   threads_num = 0;
1215   for (host = host_head; host != NULL; host = host->next)
1216   {
1217     threads_num++;
1218     /* We need to initialize `skip_num' here, because `interval_g' isn't
1219      * initialized during `configure'. */
1220     host->skip_left = interval_g;
1221     if (host->skip_num == 0)
1222     {
1223       host->skip_num = interval_g;
1224     }
1225     else if (host->skip_num < interval_g)
1226     {
1227       host->skip_num = interval_g;
1228       WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1229           host->name, host->skip_num);
1230     }
1231
1232     csnmp_host_open_session (host);
1233   } /* for (host) */
1234
1235   /* Now start the reading threads */
1236   if (threads_num > 3)
1237   {
1238     threads_num = 3 + ((threads_num - 3) / 10);
1239     if (threads_num > 10)
1240       threads_num = 10;
1241   }
1242
1243   threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1244   if (threads == NULL)
1245     return (-1);
1246   memset (threads, '\0', threads_num * sizeof (pthread_t));
1247
1248   for (i = 0; i < threads_num; i++)
1249       pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1250
1251   return (0);
1252 } /* int csnmp_init */
1253
1254 static int csnmp_read (void)
1255 {
1256   host_definition_t *host;
1257   time_t now;
1258
1259   if (host_head == NULL)
1260   {
1261     INFO ("snmp plugin: No hosts configured.");
1262     return (-1);
1263   }
1264
1265   now = time (NULL);
1266
1267   pthread_mutex_lock (&host_lock);
1268   for (host = host_head; host != NULL; host = host->next)
1269   {
1270     if (host->state != STATE_IDLE)
1271       continue;
1272
1273     host->skip_left -= interval_g;
1274     if (host->skip_left >= interval_g)
1275       continue;
1276
1277     host->state = STATE_WAIT;
1278
1279     host->skip_left = host->skip_num;
1280   } /* for (host) */
1281
1282   pthread_cond_broadcast (&host_cond);
1283   pthread_mutex_unlock (&host_lock);
1284
1285   return (0);
1286 } /* int csnmp_read */
1287
1288 static int csnmp_shutdown (void)
1289 {
1290   host_definition_t *host_this;
1291   host_definition_t *host_next;
1292
1293   data_definition_t *data_this;
1294   data_definition_t *data_next;
1295
1296   int i;
1297
1298   pthread_mutex_lock (&host_lock);
1299   do_shutdown = 1;
1300   pthread_cond_broadcast (&host_cond);
1301   pthread_mutex_unlock (&host_lock);
1302
1303   for (i = 0; i < threads_num; i++)
1304     pthread_join (threads[i], NULL);
1305
1306   /* Now that all the threads have exited, let's free all the global variables.
1307    * This isn't really neccessary, I guess, but I think it's good stile to do
1308    * so anyway. */
1309   host_this = host_head;
1310   host_head = NULL;
1311   while (host_this != NULL)
1312   {
1313     host_next = host_this->next;
1314
1315     csnmp_host_close_session (host_this);
1316
1317     sfree (host_this->name);
1318     sfree (host_this->address);
1319     sfree (host_this->community);
1320     sfree (host_this->data_list);
1321     sfree (host_this);
1322
1323     host_this = host_next;
1324   }
1325
1326   data_this = data_head;
1327   data_head = NULL;
1328   while (data_this != NULL)
1329   {
1330     data_next = data_this->next;
1331
1332     sfree (data_this->name);
1333     sfree (data_this->type);
1334     sfree (data_this->values);
1335     sfree (data_this);
1336
1337     data_this = data_next;
1338   }
1339
1340   return (0);
1341 } /* int csnmp_shutdown */
1342
1343 void module_register (void)
1344 {
1345   plugin_register_complex_config ("snmp", csnmp_config);
1346   plugin_register_init ("snmp", csnmp_init);
1347   plugin_register_read ("snmp", csnmp_read);
1348   plugin_register_shutdown ("snmp", csnmp_shutdown);
1349 } /* void module_register */
1350
1351 /*
1352  * vim: shiftwidth=2 softtabstop=2 tabstop=8
1353  */