Code

a10509dae1f22097fc0bef24138e21ce423490f7
[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  **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #include <pthread.h>
28 #include <net-snmp/net-snmp-config.h>
29 #include <net-snmp/net-snmp-includes.h>
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;
41 union instance_u
42 {
43   char  string[DATA_MAX_NAME_LEN];
44   oid_t oid;
45 };
46 typedef union instance_u instance_t;
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   char *instance_prefix;
55   oid_t *values;
56   int values_len;
57   double scale;
58   double shift;
59   struct data_definition_s *next;
60 };
61 typedef struct data_definition_s data_definition_t;
63 struct host_definition_s
64 {
65   char *name;
66   char *address;
67   char *community;
68   int version;
69   void *sess_handle;
70   int16_t skip_num;
71   int16_t skip_left;
72   data_definition_t **data_list;
73   int data_list_len;
74   enum          /****************************************************/
75   {             /* This host..                                      */
76     STATE_IDLE, /* - just sits there until `skip_left < interval_g' */
77     STATE_WAIT, /* - waits to be queried.                           */
78     STATE_BUSY  /* - is currently being queried.                    */
79   } state;      /****************************************************/
80   struct host_definition_s *next;
81 };
82 typedef struct host_definition_s host_definition_t;
84 /* These two types are used to cache values in `csnmp_read_table' to handle
85  * gaps in tables. */
86 struct csnmp_list_instances_s
87 {
88   oid subid;
89   char instance[DATA_MAX_NAME_LEN];
90   struct csnmp_list_instances_s *next;
91 };
92 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
94 struct csnmp_table_values_s
95 {
96   oid subid;
97   value_t value;
98   struct csnmp_table_values_s *next;
99 };
100 typedef struct csnmp_table_values_s csnmp_table_values_t;
102 /*
103  * Private variables
104  */
105 static int do_shutdown = 0;
107 pthread_t *threads = NULL;
108 int threads_num = 0;
110 static data_definition_t *data_head = NULL;
111 static host_definition_t *host_head = NULL;
113 static pthread_mutex_t host_lock = PTHREAD_MUTEX_INITIALIZER;
114 static pthread_cond_t  host_cond = PTHREAD_COND_INITIALIZER;
116 /*
117  * Private functions
118  */
119 /* First there are many functions which do configuration stuff. It's a big
120  * bloated and messy, I'm afraid. */
122 /*
123  * Callgraph for the config stuff:
124  *  csnmp_config
125  *  +-> call_snmp_init_once
126  *  +-> csnmp_config_add_data
127  *  !   +-> csnmp_config_add_data_type
128  *  !   +-> csnmp_config_add_data_table
129  *  !   +-> csnmp_config_add_data_instance
130  *  !   +-> csnmp_config_add_data_instance_prefix
131  *  !   +-> csnmp_config_add_data_values
132  *  +-> csnmp_config_add_host
133  *      +-> csnmp_config_add_host_address
134  *      +-> csnmp_config_add_host_community
135  *      +-> csnmp_config_add_host_version
136  *      +-> csnmp_config_add_host_collect
137  *      +-> csnmp_config_add_host_interval
138  */
139 static void call_snmp_init_once (void)
141   static int have_init = 0;
143   if (have_init == 0)
144     init_snmp (PACKAGE_NAME);
145   have_init = 1;
146 } /* void call_snmp_init_once */
148 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
150   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
151   {
152     WARNING ("snmp plugin: `Type' needs exactly one string argument.");
153     return (-1);
154   }
156   sfree (dd->type);
157   dd->type = strdup (ci->values[0].value.string);
158   if (dd->type == NULL)
159     return (-1);
161   return (0);
162 } /* int csnmp_config_add_data_type */
164 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
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   }
172   dd->is_table = ci->values[0].value.boolean ? 1 : 0;
174   return (0);
175 } /* int csnmp_config_add_data_table */
177 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
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   }
185   if (dd->is_table)
186   {
187     /* Instance is an OID */
188     dd->instance.oid.oid_len = MAX_OID_LEN;
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   }
204   return (0);
205 } /* int csnmp_config_add_data_instance */
207 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
208     oconfig_item_t *ci)
210   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
211   {
212     WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
213     return (-1);
214   }
216   if (!dd->is_table)
217   {
218     WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
219         "is set to `false'.", dd->name);
220     return (-1);
221   }
223   sfree (dd->instance_prefix);
224   dd->instance_prefix = strdup (ci->values[0].value.string);
225   if (dd->instance_prefix == NULL)
226     return (-1);
228   return (0);
229 } /* int csnmp_config_add_data_instance_prefix */
231 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
233   int i;
235   if (ci->values_num < 1)
236   {
237     WARNING ("snmp plugin: `Values' needs at least one argument.");
238     return (-1);
239   }
241   for (i = 0; i < ci->values_num; i++)
242     if (ci->values[i].type != OCONFIG_TYPE_STRING)
243     {
244       WARNING ("snmp plugin: `Values' needs only string argument.");
245       return (-1);
246     }
248   sfree (dd->values);
249   dd->values_len = 0;
250   dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
251   if (dd->values == NULL)
252     return (-1);
253   dd->values_len = ci->values_num;
255   for (i = 0; i < ci->values_num; i++)
256   {
257     dd->values[i].oid_len = MAX_OID_LEN;
259     if (NULL == snmp_parse_oid (ci->values[i].value.string,
260           dd->values[i].oid, &dd->values[i].oid_len))
261     {
262       ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
263           ci->values[i].value.string);
264       free (dd->values);
265       dd->values = NULL;
266       dd->values_len = 0;
267       return (-1);
268     }
269   }
271   return (0);
272 } /* int csnmp_config_add_data_instance */
274 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
276   if ((ci->values_num != 1)
277       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
278   {
279     WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
280     return (-1);
281   }
283   dd->shift = ci->values[0].value.number;
285   return (0);
286 } /* int csnmp_config_add_data_shift */
288 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
290   if ((ci->values_num != 1)
291       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
292   {
293     WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
294     return (-1);
295   }
297   dd->scale = ci->values[0].value.number;
299   return (0);
300 } /* int csnmp_config_add_data_scale */
302 static int csnmp_config_add_data (oconfig_item_t *ci)
304   data_definition_t *dd;
305   int status = 0;
306   int i;
308   if ((ci->values_num != 1)
309       || (ci->values[0].type != OCONFIG_TYPE_STRING))
310   {
311     WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
312     return (-1);
313   }
315   dd = (data_definition_t *) malloc (sizeof (data_definition_t));
316   if (dd == NULL)
317     return (-1);
318   memset (dd, '\0', sizeof (data_definition_t));
320   dd->name = strdup (ci->values[0].value.string);
321   if (dd->name == NULL)
322   {
323     free (dd);
324     return (-1);
325   }
326   dd->scale = 1.0;
327   dd->shift = 0.0;
329   for (i = 0; i < ci->children_num; i++)
330   {
331     oconfig_item_t *option = ci->children + i;
332     status = 0;
334     if (strcasecmp ("Type", option->key) == 0)
335       status = csnmp_config_add_data_type (dd, option);
336     else if (strcasecmp ("Table", option->key) == 0)
337       status = csnmp_config_add_data_table (dd, option);
338     else if (strcasecmp ("Instance", option->key) == 0)
339       status = csnmp_config_add_data_instance (dd, option);
340     else if (strcasecmp ("InstancePrefix", option->key) == 0)
341       status = csnmp_config_add_data_instance_prefix (dd, option);
342     else if (strcasecmp ("Values", option->key) == 0)
343       status = csnmp_config_add_data_values (dd, option);
344     else if (strcasecmp ("Shift", option->key) == 0)
345       status = csnmp_config_add_data_shift (dd, option);
346     else if (strcasecmp ("Scale", option->key) == 0)
347       status = csnmp_config_add_data_scale (dd, option);
348     else
349     {
350       WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
351       status = -1;
352     }
354     if (status != 0)
355       break;
356   } /* for (ci->children) */
358   while (status == 0)
359   {
360     if (dd->type == NULL)
361     {
362       WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
363       status = -1;
364       break;
365     }
366     if (dd->values == NULL)
367     {
368       WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
369       status = -1;
370       break;
371     }
373     break;
374   } /* while (status == 0) */
376   if (status != 0)
377   {
378     sfree (dd->name);
379     sfree (dd->instance_prefix);
380     sfree (dd->values);
381     sfree (dd);
382     return (-1);
383   }
385   DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
386       dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
388   if (data_head == NULL)
389     data_head = dd;
390   else
391   {
392     data_definition_t *last;
393     last = data_head;
394     while (last->next != NULL)
395       last = last->next;
396     last->next = dd;
397   }
399   return (0);
400 } /* int csnmp_config_add_data */
402 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
404   if ((ci->values_num != 1)
405       || (ci->values[0].type != OCONFIG_TYPE_STRING))
406   {
407     WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
408     return (-1);
409   }
411   if (hd->address == NULL)
412     free (hd->address);
414   hd->address = strdup (ci->values[0].value.string);
415   if (hd->address == NULL)
416     return (-1);
418   DEBUG ("snmp plugin: host = %s; host->address = %s;",
419       hd->name, hd->address);
421   return (0);
422 } /* int csnmp_config_add_host_address */
424 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
426   if ((ci->values_num != 1)
427       || (ci->values[0].type != OCONFIG_TYPE_STRING))
428   {
429     WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
430     return (-1);
431   }
433   if (hd->community == NULL)
434     free (hd->community);
436   hd->community = strdup (ci->values[0].value.string);
437   if (hd->community == NULL)
438     return (-1);
440   DEBUG ("snmp plugin: host = %s; host->community = %s;",
441       hd->name, hd->community);
443   return (0);
444 } /* int csnmp_config_add_host_community */
446 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
448   int version;
450   if ((ci->values_num != 1)
451       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
452   {
453     WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
454     return (-1);
455   }
457   version = (int) ci->values[0].value.number;
458   if ((version != 1) && (version != 2))
459   {
460     WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
461     return (-1);
462   }
464   hd->version = version;
466   return (0);
467 } /* int csnmp_config_add_host_address */
469 static int csnmp_config_add_host_collect (host_definition_t *host,
470     oconfig_item_t *ci)
472   data_definition_t *data;
473   data_definition_t **data_list;
474   int data_list_len;
475   int i;
477   if (ci->values_num < 1)
478   {
479     WARNING ("snmp plugin: `Collect' needs at least one argument.");
480     return (-1);
481   }
483   for (i = 0; i < ci->values_num; i++)
484     if (ci->values[i].type != OCONFIG_TYPE_STRING)
485     {
486       WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
487       return (-1);
488     }
490   data_list_len = host->data_list_len + ci->values_num;
491   data_list = (data_definition_t **) realloc (host->data_list,
492       sizeof (data_definition_t *) * data_list_len);
493   if (data_list == NULL)
494     return (-1);
495   host->data_list = data_list;
497   for (i = 0; i < ci->values_num; i++)
498   {
499     for (data = data_head; data != NULL; data = data->next)
500       if (strcasecmp (ci->values[i].value.string, data->name) == 0)
501         break;
503     if (data == NULL)
504     {
505       WARNING ("snmp plugin: No such data configured: `%s'",
506           ci->values[i].value.string);
507       continue;
508     }
510     DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
511         host->name, host->data_list_len, data->name);
513     host->data_list[host->data_list_len] = data;
514     host->data_list_len++;
515   } /* for (values_num) */
517   return (0);
518 } /* int csnmp_config_add_host_collect */
520 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
522   int interval;
524   if ((ci->values_num != 1)
525       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
526   {
527     WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
528     return (-1);
529   }
531   interval = (int) ci->values[0].value.number;
532   hd->skip_num = interval;
533   if (hd->skip_num < 0)
534     hd->skip_num = 0;
536   return (0);
537 } /* int csnmp_config_add_host_interval */
539 static int csnmp_config_add_host (oconfig_item_t *ci)
541   host_definition_t *hd;
542   int status = 0;
543   int i;
545   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
546   {
547     WARNING ("snmp plugin: `Host' needs exactly one string argument.");
548     return (-1);
549   }
551   hd = (host_definition_t *) malloc (sizeof (host_definition_t));
552   if (hd == NULL)
553     return (-1);
554   memset (hd, '\0', sizeof (host_definition_t));
555   hd->version = 2;
557   hd->name = strdup (ci->values[0].value.string);
558   if (hd->name == NULL)
559   {
560     free (hd);
561     return (-1);
562   }
564   hd->sess_handle = NULL;
565   hd->skip_num = 0;
566   hd->skip_left = 0;
567   hd->state = STATE_IDLE;
569   for (i = 0; i < ci->children_num; i++)
570   {
571     oconfig_item_t *option = ci->children + i;
572     status = 0;
574     if (strcasecmp ("Address", option->key) == 0)
575       status = csnmp_config_add_host_address (hd, option);
576     else if (strcasecmp ("Community", option->key) == 0)
577       status = csnmp_config_add_host_community (hd, option);
578     else if (strcasecmp ("Version", option->key) == 0)
579       status = csnmp_config_add_host_version (hd, option);
580     else if (strcasecmp ("Collect", option->key) == 0)
581       csnmp_config_add_host_collect (hd, option);
582     else if (strcasecmp ("Interval", option->key) == 0)
583       csnmp_config_add_host_interval (hd, option);
584     else
585     {
586       WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
587       status = -1;
588     }
590     if (status != 0)
591       break;
592   } /* for (ci->children) */
594   while (status == 0)
595   {
596     if (hd->address == NULL)
597     {
598       WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
599       status = -1;
600       break;
601     }
602     if (hd->community == NULL)
603     {
604       WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
605       status = -1;
606       break;
607     }
609     break;
610   } /* while (status == 0) */
612   if (status != 0)
613   {
614     sfree (hd->name);
615     sfree (hd);
616     return (-1);
617   }
619   DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
620       hd->name, hd->address, hd->community, hd->version);
622   if (host_head == NULL)
623     host_head = hd;
624   else
625   {
626     host_definition_t *last;
627     last = host_head;
628     while (last->next != NULL)
629       last = last->next;
630     last->next = hd;
631   }
633   return (0);
634 } /* int csnmp_config_add_host */
636 static int csnmp_config (oconfig_item_t *ci)
638   int i;
640   call_snmp_init_once ();
642   for (i = 0; i < ci->children_num; i++)
643   {
644     oconfig_item_t *child = ci->children + i;
645     if (strcasecmp ("Data", child->key) == 0)
646       csnmp_config_add_data (child);
647     else if (strcasecmp ("Host", child->key) == 0)
648       csnmp_config_add_host (child);
649     else
650     {
651       WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
652     }
653   } /* for (ci->children) */
655   return (0);
656 } /* int csnmp_config */
658 /* End of the config stuff. Now the interesting part begins */
660 static void csnmp_host_close_session (host_definition_t *host)
662   int status;
664   if (host->sess_handle == NULL)
665     return;
667   status = snmp_sess_close (host->sess_handle);
669   if (status != 0)
670   {
671     char *errstr = NULL;
673     snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
675     ERROR ("snmp plugin: host %s: snmp_sess_close failed: %s",
676         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
677     sfree (errstr);
678   }
680   host->sess_handle = NULL;
681 } /* void csnmp_host_close_session */
683 static void csnmp_host_open_session (host_definition_t *host)
685   struct snmp_session sess;
687   if (host->sess_handle != NULL)
688     csnmp_host_close_session (host);
690   snmp_sess_init (&sess);
691   sess.peername = host->address;
692   sess.community = (u_char *) host->community;
693   sess.community_len = strlen (host->community);
694   sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
696   /* snmp_sess_open will copy the `struct snmp_session *'. */
697   host->sess_handle = snmp_sess_open (&sess);
699   if (host->sess_handle == NULL)
700   {
701     char *errstr = NULL;
703     snmp_error (&sess, NULL, NULL, &errstr);
705     ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
706         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
707     sfree (errstr);
708   }
709 } /* void csnmp_host_open_session */
711 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
712     double scale, double shift)
714   value_t ret;
715   uint64_t temp = 0;
716   int defined = 1;
718   if ((vl->type == ASN_INTEGER)
719       || (vl->type == ASN_UINTEGER)
720       || (vl->type == ASN_COUNTER)
721 #ifdef ASN_TIMETICKS
722       || (vl->type == ASN_TIMETICKS)
723 #endif
724       || (vl->type == ASN_GAUGE))
725   {
726     temp = (uint32_t) *vl->val.integer;
727     DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
728   }
729   else if (vl->type == ASN_COUNTER64)
730   {
731     temp = (uint32_t) vl->val.counter64->high;
732     temp = temp << 32;
733     temp += (uint32_t) vl->val.counter64->low;
734     DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
735   }
736   else
737   {
738     WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
739     defined = 0;
740   }
742   if (type == DS_TYPE_COUNTER)
743   {
744     ret.counter = temp;
745   }
746   else if (type == DS_TYPE_GAUGE)
747   {
748     ret.gauge = NAN;
749     if (defined != 0)
750       ret.gauge = (scale * temp) + shift;
751   }
753   return (ret);
754 } /* value_t csnmp_value_list_to_value */
756 /* Returns true if all OIDs have left their subtree */
757 static int csnmp_check_res_left_subtree (const host_definition_t *host,
758     const data_definition_t *data,
759     struct snmp_pdu *res)
761   struct variable_list *vb;
762   int num_checked;
763   int num_left_subtree;
764   int i;
766   vb = res->variables;
767   if (vb == NULL)
768     return (-1);
770   num_checked = 0;
771   num_left_subtree = 0;
773   /* check all the variables and count how many have left their subtree */
774   for (vb = res->variables, i = 0;
775       (vb != NULL) && (i < data->values_len);
776       vb = vb->next_variable, i++)
777   {
778     num_checked++;
779     if (snmp_oid_ncompare (data->values[i].oid,
780           data->values[i].oid_len,
781           vb->name, vb->name_length,
782           data->values[i].oid_len) != 0)
783       num_left_subtree++;
784   }
786   /* check if enough variables have been returned */
787   if (i < data->values_len)
788   {
789     ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
790         host->name, data->values_len, i);
791     return (-1);
792   }
794   if (data->instance.oid.oid_len > 0)
795   {
796     if (vb == NULL)
797     {
798       ERROR ("snmp plugin: host %s: Expected one more variable for "
799           "the instance..");
800       return (-1);
801     }
803     num_checked++;
804     if (snmp_oid_ncompare (data->instance.oid.oid,
805           data->instance.oid.oid_len,
806           vb->name, vb->name_length,
807           data->instance.oid.oid_len) != 0)
808       num_left_subtree++;
809   }
811   DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
812       "left their subtree",
813       num_left_subtree, num_checked);
814   if (num_left_subtree >= num_checked)
815     return (1);
816   return (0);
817 } /* int csnmp_check_res_left_subtree */
819 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
820     csnmp_list_instances_t **tail,
821     const struct snmp_pdu *res)
823   csnmp_list_instances_t *il;
824   struct variable_list *vb;
826   /* Set vb on the last variable */
827   for (vb = res->variables;
828       (vb != NULL) && (vb->next_variable != NULL);
829       vb = vb->next_variable)
830     /* do nothing */;
831   if (vb == NULL)
832     return (-1);
834   il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
835   if (il == NULL)
836   {
837     ERROR ("snmp plugin: malloc failed.");
838     return (-1);
839   }
840   il->subid = vb->name[vb->name_length - 1];
841   il->next = NULL;
843   /* Get instance name */
844   if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
845   {
846     char *ptr;
847     size_t instance_len;
849     instance_len = sizeof (il->instance) - 1;
850     if (instance_len > vb->val_len)
851       instance_len = vb->val_len;
853     strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
854           ? vb->val.string
855           : vb->val.bitstring),
856         instance_len);
857     il->instance[instance_len] = '\0';
859     for (ptr = il->instance; *ptr != '\0'; ptr++)
860     {
861       if ((*ptr > 0) && (*ptr < 32))
862         *ptr = ' ';
863       else if (*ptr == '/')
864         *ptr = '_';
865     }
866     DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
867   }
868   else
869   {
870     value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
871     snprintf (il->instance, sizeof (il->instance),
872         "%llu", val.counter);
873   }
874   il->instance[sizeof (il->instance) - 1] = '\0';
876   /* TODO: Debugging output */
878   if (*head == NULL)
879     *head = il;
880   else
881     (*tail)->next = il;
882   *tail = il;
884   return (0);
885 } /* int csnmp_instance_list_add */
887 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
888     csnmp_list_instances_t *instance_list,
889     csnmp_table_values_t **value_table)
891   const data_set_t *ds;
892   value_list_t vl = VALUE_LIST_INIT;
894   csnmp_list_instances_t *instance_list_ptr;
895   csnmp_table_values_t **value_table_ptr;
897   int i;
898   oid subid;
899   int have_more;
901   ds = plugin_get_ds (data->type);
902   if (!ds)
903   {
904     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
905     return (-1);
906   }
907   assert (ds->ds_num == data->values_len);
909   instance_list_ptr = instance_list;
911   value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
912       * data->values_len);
913   if (value_table_ptr == NULL)
914     return (-1);
915   for (i = 0; i < data->values_len; i++)
916     value_table_ptr[i] = value_table[i];
918   vl.values_len = ds->ds_num;
919   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
920   if (vl.values == NULL)
921   {
922     ERROR ("snmp plugin: malloc failed.");
923     sfree (value_table_ptr);
924     return (-1);
925   }
927   strncpy (vl.host, host->name, sizeof (vl.host));
928   vl.host[sizeof (vl.host) - 1] = '\0';
929   strcpy (vl.plugin, "snmp");
931   vl.interval = host->skip_num;
932   vl.time = time (NULL);
934   subid = 0;
935   have_more = 1;
937   while (have_more != 0)
938   {
939     if (instance_list != NULL)
940     {
941       while ((instance_list_ptr != NULL)
942           && (instance_list_ptr->subid < subid))
943         instance_list_ptr = instance_list_ptr->next;
945       if (instance_list_ptr == NULL)
946       {
947         have_more = 0;
948         continue;
949       }
950       else if (instance_list_ptr->subid > subid)
951       {
952         subid = instance_list_ptr->subid;
953         continue;
954       }
955     } /* if (instance_list != NULL) */
957     for (i = 0; i < data->values_len; i++)
958     {
959       while ((value_table_ptr[i] != NULL)
960           && (value_table_ptr[i]->subid < subid))
961         value_table_ptr[i] = value_table_ptr[i]->next;
963       if (value_table_ptr[i] == NULL)
964       {
965         have_more = 0;
966         break;
967       }
968       else if (value_table_ptr[i]->subid > subid)
969       {
970         subid = value_table_ptr[i]->subid;
971         break;
972       }
973     } /* for (i = 0; i < columns; i++) */
974     /* The subid has been increased - start scanning from the beginning
975      * again.. */
976     if (i < data->values_len)
977       continue;
979     /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
980      * to the same subid. instance_list_ptr is either NULL or points to the
981      * same subid, too. */
982 #if COLLECT_DEBUG
983     for (i = 1; i < data->values_len; i++)
984     {
985       assert (value_table_ptr[i] != NULL);
986       assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
987     }
988     assert ((instance_list_ptr == NULL)
989         || (instance_list_ptr->subid == value_table_ptr[0]->subid));
990 #endif
992     {
993       char temp[DATA_MAX_NAME_LEN];
995       if (instance_list_ptr == NULL)
996         snprintf (temp, sizeof (temp), "%u",
997             (uint32_t) subid);
998       else
999         strncpy (temp, instance_list_ptr->instance,
1000             sizeof (temp));
1001       temp[sizeof (temp) - 1] = '\0';
1003       if (data->instance_prefix == NULL)
1004         strncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1005       else
1006         snprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1007             data->instance_prefix, temp);
1008       vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1009     }
1011     for (i = 0; i < data->values_len; i++)
1012       vl.values[i] = value_table_ptr[i]->value;
1014     /* If we get here `vl.type_instance' and all `vl.values' have been set */
1015     plugin_dispatch_values (data->type, &vl);
1017     subid++;
1018   } /* while (have_more != 0) */
1020   sfree (vl.values);
1021   sfree (value_table_ptr);
1023   return (0);
1024 } /* int csnmp_dispatch_table */
1026 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1028   struct snmp_pdu *req;
1029   struct snmp_pdu *res;
1030   struct variable_list *vb;
1032   const data_set_t *ds;
1033   oid_t *oid_list;
1034   uint32_t oid_list_len;
1036   int status;
1037   int i;
1039   /* `value_table' and `value_table_ptr' implement a linked list for each
1040    * value. `instance_list' and `instance_list_ptr' implement a linked list of
1041    * instance names. This is used to jump gaps in the table. */
1042   csnmp_list_instances_t *instance_list;
1043   csnmp_list_instances_t *instance_list_ptr;
1044   csnmp_table_values_t **value_table;
1045   csnmp_table_values_t **value_table_ptr;
1047   DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1048       host->name, data->name);
1050   if (host->sess_handle == NULL)
1051   {
1052     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1053     return (-1);
1054   }
1056   ds = plugin_get_ds (data->type);
1057   if (!ds)
1058   {
1059     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1060     return (-1);
1061   }
1063   if (ds->ds_num != data->values_len)
1064   {
1065     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1066         data->type, ds->ds_num, data->values_len);
1067     return (-1);
1068   }
1070   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1071   oid_list_len = data->values_len + 1;
1072   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1073   if (oid_list == NULL)
1074   {
1075     ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1076     return (-1);
1077   }
1078   memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1079   if (data->instance.oid.oid_len > 0)
1080     memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1081   else
1082     oid_list_len--;
1084   /* Allocate the `value_table' */
1085   value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1086       * 2 * data->values_len);
1087   if (value_table == NULL)
1088   {
1089     ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1090     sfree (oid_list);
1091     return (-1);
1092   }
1093   memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1094   value_table_ptr = value_table + data->values_len;
1095   
1096   instance_list = NULL;
1097   instance_list_ptr = NULL;
1099   status = 0;
1100   while (status == 0)
1101   {
1102     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1103     if (req == NULL)
1104     {
1105       ERROR ("snmp plugin: snmp_pdu_create failed.");
1106       status = -1;
1107       break;
1108     }
1110     for (i = 0; i < oid_list_len; i++)
1111       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1113     status = snmp_sess_synch_response (host->sess_handle, req, &res);
1115     if (status != STAT_SUCCESS)
1116     {
1117       char *errstr = NULL;
1119       snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1120       ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1121           host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1122       csnmp_host_close_session (host);
1124       status = -1;
1125       break;
1126     }
1127     status = 0;
1128     assert (res != NULL);
1130     vb = res->variables;
1131     if (vb == NULL)
1132     {
1133       status = -1;
1134       break;
1135     }
1137     /* Check if all values (and possibly the instance) have left their
1138      * subtree */
1139     if (csnmp_check_res_left_subtree (host, data, res) != 0)
1140       break;
1142     /* if an instance-OID is configured.. */
1143     if (data->instance.oid.oid_len > 0)
1144     {
1145       /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1146        * add it to the list */
1147       if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1148             res) != 0)
1149       {
1150         ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1151         status = -1;
1152         break;
1153       }
1155       /* Set vb on the last variable */
1156       for (vb = res->variables;
1157           (vb != NULL) && (vb->next_variable != NULL);
1158           vb = vb->next_variable)
1159         /* do nothing */;
1160       if (vb == NULL)
1161       {
1162         status = -1;
1163         break;
1164       }
1166       /* Copy OID to oid_list[data->values_len] */
1167       memcpy (oid_list[data->values_len].oid, vb->name,
1168           sizeof (oid) * vb->name_length);
1169       oid_list[data->values_len].oid_len = vb->name_length;
1170     }
1172     for (vb = res->variables, i = 0;
1173         (vb != NULL) && (i < data->values_len);
1174         vb = vb->next_variable, i++)
1175     {
1176       csnmp_table_values_t *vt;
1178       /* Check if we left the subtree */
1179       if (snmp_oid_ncompare (data->values[i].oid,
1180             data->values[i].oid_len,
1181             vb->name, vb->name_length,
1182             data->values[i].oid_len) != 0)
1183       {
1184         DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1185             host->name, data->name, i);
1186         continue;
1187       }
1189       if ((value_table_ptr[i] != NULL)
1190           && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1191       {
1192         DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1193             "SUBID is not increasing.",
1194             host->name, data->name, i);
1195         continue;
1196       }
1198       vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1199       if (vt == NULL)
1200       {
1201         ERROR ("snmp plugin: malloc failed.");
1202         status = -1;
1203         break;
1204       }
1206       vt->subid = vb->name[vb->name_length - 1];
1207       vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1208           data->scale, data->shift);
1209       vt->next = NULL;
1211       if (value_table_ptr[i] == NULL)
1212         value_table[i] = vt;
1213       else
1214         value_table_ptr[i]->next = vt;
1215       value_table_ptr[i] = vt;
1217       /* Copy OID to oid_list[i + 1] */
1218       memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1219       oid_list[i].oid_len = vb->name_length;
1220     } /* for (i = data->values_len) */
1222     if (res != NULL)
1223       snmp_free_pdu (res);
1224     res = NULL;
1225   } /* while (status == 0) */
1227   if (status == 0)
1228     csnmp_dispatch_table (host, data, instance_list, value_table);
1230   /* Free all allocated variables here */
1231   while (instance_list != NULL)
1232   {
1233     instance_list_ptr = instance_list->next;
1234     sfree (instance_list);
1235     instance_list = instance_list_ptr;
1236   }
1238   for (i = 0; i < data->values_len; i++)
1239   {
1240     csnmp_table_values_t *tmp;
1241     while (value_table[i] != NULL)
1242     {
1243       tmp = value_table[i]->next;
1244       sfree (value_table[i]);
1245       value_table[i] = tmp;
1246     }
1247   }
1249   sfree (value_table);
1250   sfree (oid_list);
1252   return (0);
1253 } /* int csnmp_read_table */
1255 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1257   struct snmp_pdu *req;
1258   struct snmp_pdu *res;
1259   struct variable_list *vb;
1261   const data_set_t *ds;
1262   value_list_t vl = VALUE_LIST_INIT;
1264   int status;
1265   int i;
1267   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1268       host->name, data->name);
1270   if (host->sess_handle == NULL)
1271   {
1272     DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1273     return (-1);
1274   }
1276   ds = plugin_get_ds (data->type);
1277   if (!ds)
1278   {
1279     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1280     return (-1);
1281   }
1283   if (ds->ds_num != data->values_len)
1284   {
1285     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1286         data->type, ds->ds_num, data->values_len);
1287     return (-1);
1288   }
1290   vl.values_len = ds->ds_num;
1291   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1292   if (vl.values == NULL)
1293     return (-1);
1294   for (i = 0; i < vl.values_len; i++)
1295   {
1296     if (ds->ds[i].type == DS_TYPE_COUNTER)
1297       vl.values[i].counter = 0;
1298     else
1299       vl.values[i].gauge = NAN;
1300   }
1302   strncpy (vl.host, host->name, sizeof (vl.host));
1303   vl.host[sizeof (vl.host) - 1] = '\0';
1304   strcpy (vl.plugin, "snmp");
1305   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1306   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
1308   vl.interval = host->skip_num;
1310   req = snmp_pdu_create (SNMP_MSG_GET);
1311   if (req == NULL)
1312   {
1313     ERROR ("snmp plugin: snmp_pdu_create failed.");
1314     sfree (vl.values);
1315     return (-1);
1316   }
1318   for (i = 0; i < data->values_len; i++)
1319     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1320   status = snmp_sess_synch_response (host->sess_handle, req, &res);
1322   if (status != STAT_SUCCESS)
1323   {
1324     char *errstr = NULL;
1326     snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1327     ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1328         host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1329     csnmp_host_close_session (host);
1330     sfree (errstr);
1332     return (-1);
1333   }
1335   vl.time = time (NULL);
1337   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1338   {
1339     char buffer[1024];
1340     snprint_variable (buffer, sizeof (buffer),
1341         vb->name, vb->name_length, vb);
1342     DEBUG ("snmp plugin: Got this variable: %s", buffer);
1344     for (i = 0; i < data->values_len; i++)
1345       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1346             vb->name, vb->name_length) == 0)
1347         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1348             data->scale, data->shift);
1349   } /* for (res->variables) */
1351   snmp_free_pdu (res);
1353   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
1354   plugin_dispatch_values (data->type, &vl);
1355   sfree (vl.values);
1357   return (0);
1358 } /* int csnmp_read_value */
1360 static int csnmp_read_host (host_definition_t *host)
1362   int i;
1364   DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
1366   if (host->sess_handle == NULL)
1367     csnmp_host_open_session (host);
1369   if (host->sess_handle == NULL)
1370     return (-1);
1372   for (i = 0; i < host->data_list_len; i++)
1373   {
1374     data_definition_t *data = host->data_list[i];
1376     if (data->is_table)
1377       csnmp_read_table (host, data);
1378     else
1379       csnmp_read_value (host, data);
1380   }
1382   return (0);
1383 } /* int csnmp_read_host */
1385 static void *csnmp_read_thread (void *data)
1387   host_definition_t *host;
1389   pthread_mutex_lock (&host_lock);
1390   while (do_shutdown == 0)
1391   {
1392     pthread_cond_wait (&host_cond, &host_lock);
1394     for (host = host_head; host != NULL; host = host->next)
1395     {
1396       if (do_shutdown != 0)
1397         break;
1398       if (host->state != STATE_WAIT)
1399         continue;
1401       host->state = STATE_BUSY;
1402       pthread_mutex_unlock (&host_lock);
1403       csnmp_read_host (host);
1404       pthread_mutex_lock (&host_lock);
1405       host->state = STATE_IDLE;
1406     } /* for (host) */
1407   } /* while (do_shutdown == 0) */
1408   pthread_mutex_unlock (&host_lock);
1410   pthread_exit ((void *) 0);
1411   return ((void *) 0);
1412 } /* void *csnmp_read_thread */
1414 static int csnmp_init (void)
1416   host_definition_t *host;
1417   int i;
1419   if (host_head == NULL)
1420   {
1421     NOTICE ("snmp plugin: No host has been defined.");
1422     return (-1);
1423   }
1425   call_snmp_init_once ();
1427   threads_num = 0;
1428   for (host = host_head; host != NULL; host = host->next)
1429   {
1430     threads_num++;
1431     /* We need to initialize `skip_num' here, because `interval_g' isn't
1432      * initialized during `configure'. */
1433     host->skip_left = interval_g;
1434     if (host->skip_num == 0)
1435     {
1436       host->skip_num = interval_g;
1437     }
1438     else if (host->skip_num < interval_g)
1439     {
1440       host->skip_num = interval_g;
1441       WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
1442           host->name, host->skip_num);
1443     }
1445     csnmp_host_open_session (host);
1446   } /* for (host) */
1448   /* Now start the reading threads */
1449   if (threads_num > 3)
1450   {
1451     threads_num = 3 + ((threads_num - 3) / 10);
1452     if (threads_num > 10)
1453       threads_num = 10;
1454   }
1456   threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
1457   if (threads == NULL)
1458   {
1459     ERROR ("snmp plugin: malloc failed.");
1460     return (-1);
1461   }
1462   memset (threads, '\0', threads_num * sizeof (pthread_t));
1464   for (i = 0; i < threads_num; i++)
1465       pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
1467   return (0);
1468 } /* int csnmp_init */
1470 static int csnmp_read (void)
1472   host_definition_t *host;
1473   time_t now;
1475   if (host_head == NULL)
1476   {
1477     INFO ("snmp plugin: No hosts configured.");
1478     return (-1);
1479   }
1481   now = time (NULL);
1483   pthread_mutex_lock (&host_lock);
1484   for (host = host_head; host != NULL; host = host->next)
1485   {
1486     if (host->state != STATE_IDLE)
1487       continue;
1489     host->skip_left -= interval_g;
1490     if (host->skip_left >= interval_g)
1491       continue;
1493     host->state = STATE_WAIT;
1495     host->skip_left = host->skip_num;
1496   } /* for (host) */
1498   pthread_cond_broadcast (&host_cond);
1499   pthread_mutex_unlock (&host_lock);
1501   return (0);
1502 } /* int csnmp_read */
1504 static int csnmp_shutdown (void)
1506   host_definition_t *host_this;
1507   host_definition_t *host_next;
1509   data_definition_t *data_this;
1510   data_definition_t *data_next;
1512   int i;
1514   pthread_mutex_lock (&host_lock);
1515   do_shutdown = 1;
1516   pthread_cond_broadcast (&host_cond);
1517   pthread_mutex_unlock (&host_lock);
1519   for (i = 0; i < threads_num; i++)
1520     pthread_join (threads[i], NULL);
1522   /* Now that all the threads have exited, let's free all the global variables.
1523    * This isn't really neccessary, I guess, but I think it's good stile to do
1524    * so anyway. */
1525   host_this = host_head;
1526   host_head = NULL;
1527   while (host_this != NULL)
1528   {
1529     host_next = host_this->next;
1531     csnmp_host_close_session (host_this);
1533     sfree (host_this->name);
1534     sfree (host_this->address);
1535     sfree (host_this->community);
1536     sfree (host_this->data_list);
1537     sfree (host_this);
1539     host_this = host_next;
1540   }
1542   data_this = data_head;
1543   data_head = NULL;
1544   while (data_this != NULL)
1545   {
1546     data_next = data_this->next;
1548     sfree (data_this->name);
1549     sfree (data_this->type);
1550     sfree (data_this->values);
1551     sfree (data_this);
1553     data_this = data_next;
1554   }
1556   return (0);
1557 } /* int csnmp_shutdown */
1559 void module_register (void)
1561   plugin_register_complex_config ("snmp", csnmp_config);
1562   plugin_register_init ("snmp", csnmp_init);
1563   plugin_register_read ("snmp", csnmp_read);
1564   plugin_register_shutdown ("snmp", csnmp_shutdown);
1565 } /* void module_register */
1567 /*
1568  * vim: shiftwidth=2 softtabstop=2 tabstop=8
1569  */