snmp plugin: Changed the table code so that an `instance' is optional.
[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 /* Returns true if all OIDs have left their subtree */
730 static int csnmp_check_res_left_subtree (const host_definition_t *host,
731     const data_definition_t *data,
732     struct snmp_pdu *res)
733 {
734   struct variable_list *vb;
735   int num_checked;
736   int num_left_subtree;
737   int i;
738
739   vb = res->variables;
740   if (vb == NULL)
741     return (-1);
742
743   num_checked = 0;
744   num_left_subtree = 0;
745
746   /* check all the variables and count how many have left their subtree */
747   for (vb = res->variables, i = 0;
748       (vb != NULL) && (i < data->values_len);
749       vb = vb->next_variable, i++)
750   {
751     num_checked++;
752     if (snmp_oid_ncompare (data->values[i].oid,
753           data->values[i].oid_len,
754           vb->name, vb->name_length,
755           data->values[i].oid_len) != 0)
756       num_left_subtree++;
757   }
758
759   /* check if enough variables have been returned */
760   if (i < data->values_len)
761   {
762     ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
763         host->name, data->values_len, i);
764     return (-1);
765   }
766
767   if (data->instance.oid.oid_len > 0)
768   {
769     if (vb == NULL)
770     {
771       ERROR ("snmp plugin: host %s: Expected one more variable for "
772           "the instance..");
773       return (-1);
774     }
775
776     num_checked++;
777     if (snmp_oid_ncompare (data->instance.oid.oid,
778           data->instance.oid.oid_len,
779           vb->name, vb->name_length,
780           data->instance.oid.oid_len) != 0)
781       num_left_subtree++;
782   }
783
784   DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
785       "left their subtree",
786       num_left_subtree, num_checked);
787   if (num_left_subtree >= num_checked)
788     return (1);
789   return (0);
790 } /* int csnmp_check_res_left_subtree */
791
792 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
793     csnmp_list_instances_t **tail,
794     const struct snmp_pdu *res)
795 {
796   csnmp_list_instances_t *il;
797   struct variable_list *vb;
798
799   /* Set vb on the last variable */
800   for (vb = res->variables;
801       (vb != NULL) && (vb->next_variable != NULL);
802       vb = vb->next_variable)
803     /* do nothing */;
804   if (vb == NULL)
805     return (-1);
806
807   il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
808   if (il == NULL)
809   {
810     ERROR ("snmp plugin: malloc failed.");
811     return (-1);
812   }
813   il->subid = vb->name[vb->name_length - 1];
814   il->next = NULL;
815
816   /* Get instance name */
817   if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
818   {
819     char *ptr;
820     size_t instance_len;
821
822     instance_len = sizeof (il->instance) - 1;
823     if (instance_len > vb->val_len)
824       instance_len = vb->val_len;
825
826     strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
827           ? vb->val.string
828           : vb->val.bitstring),
829         instance_len);
830     il->instance[instance_len] = '\0';
831
832     for (ptr = il->instance; *ptr != '\0'; ptr++)
833     {
834       if ((*ptr > 0) && (*ptr < 32))
835         *ptr = ' ';
836       else if (*ptr == '/')
837         *ptr = '_';
838     }
839     DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
840   }
841   else
842   {
843     value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
844     snprintf (il->instance, sizeof (il->instance),
845         "%llu", val.counter);
846   }
847   il->instance[sizeof (il->instance) - 1] = '\0';
848
849   /* TODO: Debugging output */
850
851   if (*head == NULL)
852     *head = il;
853   else
854     (*tail)->next = il;
855   *tail = il;
856
857   return (0);
858 } /* int csnmp_instance_list_add */
859
860 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
861     csnmp_list_instances_t *instance_list,
862     csnmp_table_values_t **value_table)
863 {
864   const data_set_t *ds;
865   value_list_t vl = VALUE_LIST_INIT;
866
867   csnmp_list_instances_t *instance_list_ptr;
868   csnmp_table_values_t **value_table_ptr;
869
870   int i;
871   oid subid;
872   int have_more;
873
874   ds = plugin_get_ds (data->type);
875   if (!ds)
876   {
877     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
878     return (-1);
879   }
880   assert (ds->ds_num == data->values_len);
881
882   instance_list_ptr = instance_list;
883
884   value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
885       * data->values_len);
886   if (value_table_ptr == NULL)
887     return (-1);
888   for (i = 0; i < data->values_len; i++)
889     value_table_ptr[i] = value_table[i];
890
891   vl.values_len = ds->ds_num;
892   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
893   if (vl.values == NULL)
894   {
895     ERROR ("snmp plugin: malloc failed.");
896     sfree (value_table_ptr);
897     return (-1);
898   }
899
900   strncpy (vl.host, host->name, sizeof (vl.host));
901   vl.host[sizeof (vl.host) - 1] = '\0';
902   strcpy (vl.plugin, "snmp");
903
904   vl.interval = host->skip_num;
905   vl.time = time (NULL);
906
907   subid = 0;
908   have_more = 1;
909
910   while (have_more != 0)
911   {
912     if (instance_list != NULL)
913     {
914       while ((instance_list_ptr != NULL)
915           && (instance_list_ptr->subid < subid))
916         instance_list_ptr = instance_list_ptr->next;
917
918       if (instance_list_ptr == NULL)
919       {
920         have_more = 0;
921         continue;
922       }
923       else if (instance_list_ptr->subid > subid)
924       {
925         subid = instance_list_ptr->subid;
926         continue;
927       }
928     } /* if (instance_list != NULL) */
929
930     for (i = 0; i < data->values_len; i++)
931     {
932       while ((value_table_ptr[i] != NULL)
933           && (value_table_ptr[i]->subid < subid))
934         value_table_ptr[i] = value_table_ptr[i]->next;
935
936       if (value_table_ptr[i] == NULL)
937       {
938         have_more = 0;
939         break;
940       }
941       else if (value_table_ptr[i]->subid > subid)
942       {
943         subid = value_table_ptr[i]->subid;
944         break;
945       }
946     } /* for (i = 0; i < columns; i++) */
947     /* The subid has been increased - start scanning from the beginning
948      * again.. */
949     if (i < data->values_len)
950       continue;
951
952     /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
953      * to the same subid. instance_list_ptr is either NULL or points to the
954      * same subid, too. */
955 #if COLLECT_DEBUG
956     for (i = 1; i < data->values_len; i++)
957     {
958       assert (value_table_ptr[i] != NULL);
959       assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
960     }
961     assert ((instance_list_ptr == NULL)
962         || (instance_list_ptr->subid == value_table_ptr[0]->subid));
963 #endif
964
965     if (instance_list_ptr == NULL)
966       snprintf (vl.type_instance, sizeof (vl.type_instance), "%u",
967           (uint32_t) subid);
968     else
969       strncpy (vl.type_instance, instance_list_ptr->instance,
970           sizeof (vl.type_instance));
971     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
972
973     for (i = 0; i < data->values_len; i++)
974       vl.values[i] = value_table_ptr[i]->value;
975
976     /* If we get here `vl.type_instance' and all `vl.values' have been set */
977     plugin_dispatch_values (data->type, &vl);
978
979     subid++;
980   } /* while (have_more != 0) */
981
982   sfree (vl.values);
983   sfree (value_table_ptr);
984
985   return (0);
986 } /* int csnmp_dispatch_table */
987
988 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
989 {
990   struct snmp_pdu *req;
991   struct snmp_pdu *res;
992   struct variable_list *vb;
993
994   const data_set_t *ds;
995   oid_t *oid_list;
996   uint32_t oid_list_len;
997
998   int status;
999   int i;
1000
1001   /* `value_table' and `value_table_ptr' implement a linked list for each
1002    * value. `instance_list' and `instance_list_ptr' implement a linked list of
1003    * instance names. This is used to jump gaps in the table. */
1004   csnmp_list_instances_t *instance_list;
1005   csnmp_list_instances_t *instance_list_ptr;
1006   csnmp_table_values_t **value_table;
1007   csnmp_table_values_t **value_table_ptr;
1008
1009   DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1010       host->name, data->name);
1011
1012   if (host->sess_handle == NULL)
1013   {
1014     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1015     return (-1);
1016   }
1017
1018   ds = plugin_get_ds (data->type);
1019   if (!ds)
1020   {
1021     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1022     return (-1);
1023   }
1024
1025   if (ds->ds_num != data->values_len)
1026   {
1027     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1028         data->type, ds->ds_num, data->values_len);
1029     return (-1);
1030   }
1031
1032   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1033   oid_list_len = data->values_len + 1;
1034   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1035   if (oid_list == NULL)
1036   {
1037     ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1038     return (-1);
1039   }
1040   memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1041   if (data->instance.oid.oid_len > 0)
1042     memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1043   else
1044     oid_list_len--;
1045
1046   /* Allocate the `value_table' */
1047   value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1048       * 2 * data->values_len);
1049   if (value_table == NULL)
1050   {
1051     ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1052     sfree (oid_list);
1053     return (-1);
1054   }
1055   memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1056   value_table_ptr = value_table + data->values_len;
1057   
1058   instance_list = NULL;
1059   instance_list_ptr = NULL;
1060
1061   status = 0;
1062   while (status == 0)
1063   {
1064     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1065     if (req == NULL)
1066     {
1067       ERROR ("snmp plugin: snmp_pdu_create failed.");
1068       status = -1;
1069       break;
1070     }
1071
1072     for (i = 0; i < oid_list_len; i++)
1073       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1074
1075     status = snmp_sess_synch_response (host->sess_handle, req, &res);
1076
1077     if (status != STAT_SUCCESS)
1078     {
1079       char *errstr = NULL;
1080
1081       snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1082       ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1083           host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1084       csnmp_host_close_session (host);
1085
1086       status = -1;
1087       break;
1088     }
1089     status = 0;
1090     assert (res != NULL);
1091
1092     vb = res->variables;
1093     if (vb == NULL)
1094     {
1095       status = -1;
1096       break;
1097     }
1098
1099     /* Check if all values (and possibly the instance) have left their
1100      * subtree */
1101     if (csnmp_check_res_left_subtree (host, data, res) != 0)
1102       break;
1103
1104     /* if an instance-OID is configured.. */
1105     if (data->instance.oid.oid_len > 0)
1106     {
1107       /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1108        * add it to the list */
1109       if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1110             res) != 0)
1111       {
1112         ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1113         status = -1;
1114         break;
1115       }
1116
1117       /* Set vb on the last variable */
1118       for (vb = res->variables;
1119           (vb != NULL) && (vb->next_variable != NULL);
1120           vb = vb->next_variable)
1121         /* do nothing */;
1122       if (vb == NULL)
1123       {
1124         status = -1;
1125         break;
1126       }
1127
1128       /* Copy OID to oid_list[data->values_len] */
1129       memcpy (oid_list[data->values_len].oid, vb->name,
1130           sizeof (oid) * vb->name_length);
1131       oid_list[data->values_len].oid_len = vb->name_length;
1132     }
1133
1134     for (vb = res->variables, i = 0;
1135         (vb != NULL) && (i < data->values_len);
1136         vb = vb->next_variable, i++)
1137     {
1138       csnmp_table_values_t *vt;
1139
1140       /* Check if we left the subtree */
1141       if (snmp_oid_ncompare (data->values[i].oid,
1142             data->values[i].oid_len,
1143             vb->name, vb->name_length,
1144             data->values[i].oid_len) != 0)
1145       {
1146         DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1147             host->name, data->name, i);
1148         continue;
1149       }
1150
1151       if ((value_table_ptr[i] != NULL)
1152           && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1153       {
1154         DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1155             "SUBID is not increasing.",
1156             host->name, data->name, i);
1157         continue;
1158       }
1159
1160       vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1161       if (vt == NULL)
1162       {
1163         ERROR ("snmp plugin: malloc failed.");
1164         status = -1;
1165         break;
1166       }
1167
1168       vt->subid = vb->name[vb->name_length - 1];
1169       vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1170           data->scale, data->shift);
1171       vt->next = NULL;
1172
1173       if (value_table_ptr[i] == NULL)
1174         value_table[i] = vt;
1175       else
1176         value_table_ptr[i]->next = vt;
1177       value_table_ptr[i] = vt;
1178
1179       /* Copy OID to oid_list[i + 1] */
1180       memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1181       oid_list[i].oid_len = vb->name_length;
1182     } /* for (i = data->values_len) */
1183
1184     if (res != NULL)
1185       snmp_free_pdu (res);
1186     res = NULL;
1187   } /* while (status == 0) */
1188
1189   if (status == 0)
1190     csnmp_dispatch_table (host, data, instance_list, value_table);
1191
1192   /* Free all allocated variables here */
1193   while (instance_list != NULL)
1194   {
1195     instance_list_ptr = instance_list->next;
1196     sfree (instance_list);
1197     instance_list = instance_list_ptr;
1198   }
1199
1200   for (i = 0; i < data->values_len; i++)
1201   {
1202     csnmp_table_values_t *tmp;
1203     while (value_table[i] != NULL)
1204     {
1205       tmp = value_table[i]->next;
1206       sfree (value_table[i]);
1207       value_table[i] = tmp;
1208     }
1209   }
1210
1211   sfree (value_table);
1212   sfree (oid_list);
1213
1214   return (0);
1215 } /* int csnmp_read_table */
1216
1217 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1218 {
1219   struct snmp_pdu *req;
1220   struct snmp_pdu *res;
1221   struct variable_list *vb;
1222
1223   const data_set_t *ds;
1224   value_list_t vl = VALUE_LIST_INIT;
1225
1226   int status;
1227   int i;
1228
1229   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1230       host->name, data->name);
1231
1232   if (host->sess_handle == NULL)
1233   {
1234     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1235     return (-1);
1236   }
1237
1238   ds = plugin_get_ds (data->type);
1239   if (!ds)
1240   {
1241     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1242     return (-1);
1243   }
1244
1245   if (ds->ds_num != data->values_len)
1246   {
1247     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1248         data->type, ds->ds_num, data->values_len);
1249     return (-1);
1250   }
1251
1252   vl.values_len = ds->ds_num;
1253   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1254   if (vl.values == NULL)
1255     return (-1);
1256   for (i = 0; i < vl.values_len; i++)
1257   {
1258     if (ds->ds[i].type == DS_TYPE_COUNTER)
1259       vl.values[i].counter = 0;
1260     else
1261       vl.values[i].gauge = NAN;
1262   }
1263
1264   strncpy (vl.host, host->name, sizeof (vl.host));
1265   vl.host[sizeof (vl.host) - 1] = '\0';
1266   strcpy (vl.plugin, "snmp");
1267   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1268   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1269
1270   vl.interval = host->skip_num;
1271
1272   req = snmp_pdu_create (SNMP_MSG_GET);
1273   if (req == NULL)
1274   {
1275     ERROR ("snmp plugin: snmp_pdu_create failed.");
1276     sfree (vl.values);
1277     return (-1);
1278   }
1279
1280   for (i = 0; i < data->values_len; i++)
1281     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1282   status = snmp_sess_synch_response (host->sess_handle, req, &res);
1283
1284   if (status != STAT_SUCCESS)
1285   {
1286     char *errstr = NULL;
1287
1288     snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1289     ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1290         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1291     csnmp_host_close_session (host);
1292     sfree (errstr);
1293
1294     return (-1);
1295   }
1296
1297   vl.time = time (NULL);
1298
1299   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1300   {
1301     char buffer[1024];
1302     snprint_variable (buffer, sizeof (buffer),
1303         vb->name, vb->name_length, vb);
1304     DEBUG ("snmp plugin: Got this variable: %s", buffer);
1305
1306     for (i = 0; i < data->values_len; i++)
1307       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1308             vb->name, vb->name_length) == 0)
1309         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1310             data->scale, data->shift);
1311   } /* for (res->variables) */
1312
1313   snmp_free_pdu (res);
1314
1315   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1316   plugin_dispatch_values (data->type, &vl);
1317   sfree (vl.values);
1318
1319   return (0);
1320 } /* int csnmp_read_value */
1321
1322 static int csnmp_read_host (host_definition_t *host)
1323 {
1324   int i;
1325
1326   DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1327
1328   if (host->sess_handle == NULL)
1329     csnmp_host_open_session (host);
1330
1331   if (host->sess_handle == NULL)
1332     return (-1);
1333
1334   for (i = 0; i < host->data_list_len; i++)
1335   {
1336     data_definition_t *data = host->data_list[i];
1337
1338     if (data->is_table)
1339       csnmp_read_table (host, data);
1340     else
1341       csnmp_read_value (host, data);
1342   }
1343
1344   return (0);
1345 } /* int csnmp_read_host */
1346
1347 static void *csnmp_read_thread (void *data)
1348 {
1349   host_definition_t *host;
1350
1351   pthread_mutex_lock (&host_lock);
1352   while (do_shutdown == 0)
1353   {
1354     pthread_cond_wait (&host_cond, &host_lock);
1355
1356     for (host = host_head; host != NULL; host = host->next)
1357     {
1358       if (do_shutdown != 0)
1359         break;
1360       if (host->state != STATE_WAIT)
1361         continue;
1362
1363       host->state = STATE_BUSY;
1364       pthread_mutex_unlock (&host_lock);
1365       csnmp_read_host (host);
1366       pthread_mutex_lock (&host_lock);
1367       host->state = STATE_IDLE;
1368     } /* for (host) */
1369   } /* while (do_shutdown == 0) */
1370   pthread_mutex_unlock (&host_lock);
1371
1372   pthread_exit ((void *) 0);
1373   return ((void *) 0);
1374 } /* void *csnmp_read_thread */
1375
1376 static int csnmp_init (void)
1377 {
1378   host_definition_t *host;
1379   int i;
1380
1381   if (host_head == NULL)
1382   {
1383     NOTICE ("snmp plugin: No host has been defined.");
1384     return (-1);
1385   }
1386
1387   call_snmp_init_once ();
1388
1389   threads_num = 0;
1390   for (host = host_head; host != NULL; host = host->next)
1391   {
1392     threads_num++;
1393     /* We need to initialize `skip_num' here, because `interval_g' isn't
1394      * initialized during `configure'. */
1395     host->skip_left = interval_g;
1396     if (host->skip_num == 0)
1397     {
1398       host->skip_num = interval_g;
1399     }
1400     else if (host->skip_num < interval_g)
1401     {
1402       host->skip_num = interval_g;
1403       WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1404           host->name, host->skip_num);
1405     }
1406
1407     csnmp_host_open_session (host);
1408   } /* for (host) */
1409
1410   /* Now start the reading threads */
1411   if (threads_num > 3)
1412   {
1413     threads_num = 3 + ((threads_num - 3) / 10);
1414     if (threads_num > 10)
1415       threads_num = 10;
1416   }
1417
1418   threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1419   if (threads == NULL)
1420   {
1421     ERROR ("snmp plugin: malloc failed.");
1422     return (-1);
1423   }
1424   memset (threads, '\0', threads_num * sizeof (pthread_t));
1425
1426   for (i = 0; i < threads_num; i++)
1427       pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1428
1429   return (0);
1430 } /* int csnmp_init */
1431
1432 static int csnmp_read (void)
1433 {
1434   host_definition_t *host;
1435   time_t now;
1436
1437   if (host_head == NULL)
1438   {
1439     INFO ("snmp plugin: No hosts configured.");
1440     return (-1);
1441   }
1442
1443   now = time (NULL);
1444
1445   pthread_mutex_lock (&host_lock);
1446   for (host = host_head; host != NULL; host = host->next)
1447   {
1448     if (host->state != STATE_IDLE)
1449       continue;
1450
1451     host->skip_left -= interval_g;
1452     if (host->skip_left >= interval_g)
1453       continue;
1454
1455     host->state = STATE_WAIT;
1456
1457     host->skip_left = host->skip_num;
1458   } /* for (host) */
1459
1460   pthread_cond_broadcast (&host_cond);
1461   pthread_mutex_unlock (&host_lock);
1462
1463   return (0);
1464 } /* int csnmp_read */
1465
1466 static int csnmp_shutdown (void)
1467 {
1468   host_definition_t *host_this;
1469   host_definition_t *host_next;
1470
1471   data_definition_t *data_this;
1472   data_definition_t *data_next;
1473
1474   int i;
1475
1476   pthread_mutex_lock (&host_lock);
1477   do_shutdown = 1;
1478   pthread_cond_broadcast (&host_cond);
1479   pthread_mutex_unlock (&host_lock);
1480
1481   for (i = 0; i < threads_num; i++)
1482     pthread_join (threads[i], NULL);
1483
1484   /* Now that all the threads have exited, let's free all the global variables.
1485    * This isn't really neccessary, I guess, but I think it's good stile to do
1486    * so anyway. */
1487   host_this = host_head;
1488   host_head = NULL;
1489   while (host_this != NULL)
1490   {
1491     host_next = host_this->next;
1492
1493     csnmp_host_close_session (host_this);
1494
1495     sfree (host_this->name);
1496     sfree (host_this->address);
1497     sfree (host_this->community);
1498     sfree (host_this->data_list);
1499     sfree (host_this);
1500
1501     host_this = host_next;
1502   }
1503
1504   data_this = data_head;
1505   data_head = NULL;
1506   while (data_this != NULL)
1507   {
1508     data_next = data_this->next;
1509
1510     sfree (data_this->name);
1511     sfree (data_this->type);
1512     sfree (data_this->values);
1513     sfree (data_this);
1514
1515     data_this = data_next;
1516   }
1517
1518   return (0);
1519 } /* int csnmp_shutdown */
1520
1521 void module_register (void)
1522 {
1523   plugin_register_complex_config ("snmp", csnmp_config);
1524   plugin_register_init ("snmp", csnmp_init);
1525   plugin_register_read ("snmp", csnmp_read);
1526   plugin_register_shutdown ("snmp", csnmp_shutdown);
1527 } /* void module_register */
1528
1529 /*
1530  * vim: shiftwidth=2 softtabstop=2 tabstop=8
1531  */