7c37a27efd1c3573b27470eae455cf3f113e25cf
[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 <net-snmp/net-snmp-config.h>
27 #include <net-snmp/net-snmp-includes.h>
28
29 /*
30  * Private data structes
31  */
32 struct oid_s
33 {
34   oid oid[MAX_OID_LEN];
35   uint32_t oid_len;
36 };
37 typedef struct oid_s oid_t;
38
39 union instance_u
40 {
41   char  string[DATA_MAX_NAME_LEN];
42   oid_t oid;
43 };
44 typedef union instance_u instance_t;
45
46 struct data_definition_s
47 {
48   char *name; /* used to reference this from the `Collect' option */
49   char *type; /* used to find the data_set */
50   int is_table;
51   instance_t instance;
52   oid_t *values;
53   int values_len;
54   struct data_definition_s *next;
55 };
56 typedef struct data_definition_s data_definition_t;
57
58 struct host_definition_s
59 {
60   char *name;
61   char *address;
62   char *community;
63   int version;
64   struct snmp_session sess;
65   data_definition_t **data_list;
66   int data_list_len;
67   struct host_definition_s *next;
68 };
69 typedef struct host_definition_s host_definition_t;
70
71 /*
72  * Private variables
73  */
74 data_definition_t *data_head = NULL;
75 host_definition_t *host_head = NULL;
76
77 /*
78  * Private functions
79  */
80 /* First there are many functions which do configuration stuff. It's a big
81  * bloated and messy, I'm afraid. */
82
83 /*
84  * Callgraph for the config stuff:
85  *  csnmp_config
86  *  +-> csnmp_config_add_data
87  *  !   +-> csnmp_config_add_data_type
88  *  !   +-> csnmp_config_add_data_table
89  *  !   +-> csnmp_config_add_data_instance
90  *  !   +-> csnmp_config_add_data_values
91  *  +-> csnmp_config_add_host
92  *  +-> csnmp_config_add_host_collect
93  */
94 static void call_snmp_init_once (void)
95 {
96   static int have_init = 0;
97
98   if (have_init == 0)
99     init_snmp (PACKAGE_NAME);
100   have_init = 1;
101 } /* void call_snmp_init_once */
102
103 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
104 {
105   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
106   {
107     WARNING ("snmp plugin: `Type' needs exactly one string argument.");
108     return (-1);
109   }
110
111   if (dd->type != NULL)
112     free (dd->type);
113
114   dd->type = strdup (ci->values[0].value.string);
115   if (dd->type == NULL)
116     return (-1);
117
118   return (0);
119 } /* int csnmp_config_add_data_type */
120
121 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
122 {
123   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
124   {
125     WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
126     return (-1);
127   }
128
129   dd->is_table = ci->values[0].value.boolean ? 1 : 0;
130
131   return (0);
132 } /* int csnmp_config_add_data_table */
133
134 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
135 {
136   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
137   {
138     WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
139     return (-1);
140   }
141
142   if (dd->is_table)
143   {
144     /* Instance is an OID */
145     dd->instance.oid.oid_len = MAX_OID_LEN;
146
147     if (!read_objid (ci->values[0].value.string,
148           dd->instance.oid.oid, &dd->instance.oid.oid_len))
149     {
150       ERROR ("snmp plugin: read_objid (%s) failed.",
151           ci->values[0].value.string);
152       return (-1);
153     }
154   }
155   else
156   {
157     /* Instance is a simple string */
158     strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
159   }
160
161   return (0);
162 } /* int csnmp_config_add_data_instance */
163
164 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
165 {
166   int i;
167
168   if (ci->values_num < 1)
169   {
170     WARNING ("snmp plugin: `Values' needs at least one argument.");
171     return (-1);
172   }
173
174   for (i = 0; i < ci->values_num; i++)
175     if (ci->values[i].type != OCONFIG_TYPE_STRING)
176     {
177       WARNING ("snmp plugin: `Values' needs only string argument.");
178       return (-1);
179     }
180
181   if (dd->values != NULL)
182     free (dd->values);
183   dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
184   if (dd->values == NULL)
185     return (-1);
186   dd->values_len = ci->values_num;
187
188   for (i = 0; i < ci->values_num; i++)
189   {
190     dd->values[i].oid_len = MAX_OID_LEN;
191
192     if (NULL == snmp_parse_oid (ci->values[i].value.string,
193           dd->values[i].oid, &dd->values[i].oid_len))
194     {
195       ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
196           ci->values[i].value.string);
197       free (dd->values);
198       dd->values = NULL;
199       dd->values_len = 0;
200       return (-1);
201     }
202   }
203
204   return (0);
205 } /* int csnmp_config_add_data_instance */
206
207 static int csnmp_config_add_data (oconfig_item_t *ci)
208 {
209   data_definition_t *dd;
210   int status = 0;
211   int i;
212
213   if ((ci->values_num != 1)
214       || (ci->values[0].type != OCONFIG_TYPE_STRING))
215   {
216     WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
217     return (-1);
218   }
219
220   dd = (data_definition_t *) malloc (sizeof (data_definition_t));
221   if (dd == NULL)
222     return (-1);
223   memset (dd, '\0', sizeof (data_definition_t));
224
225   dd->name = strdup (ci->values[0].value.string);
226   if (dd->name == NULL)
227   {
228     free (dd);
229     return (-1);
230   }
231
232   for (i = 0; i < ci->children_num; i++)
233   {
234     oconfig_item_t *option = ci->children + i;
235     status = 0;
236
237     if (strcasecmp ("Type", option->key) == 0)
238       status = csnmp_config_add_data_type (dd, option);
239     else if (strcasecmp ("Table", option->key) == 0)
240       status = csnmp_config_add_data_table (dd, option);
241     else if (strcasecmp ("Instance", option->key) == 0)
242       status = csnmp_config_add_data_instance (dd, option);
243     else if (strcasecmp ("Values", option->key) == 0)
244       status = csnmp_config_add_data_values (dd, option);
245     else
246     {
247       WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
248       status = -1;
249     }
250
251     if (status != 0)
252       break;
253   } /* for (ci->children) */
254
255   while (status == 0)
256   {
257     if (dd->type == NULL)
258     {
259       WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
260       status = -1;
261       break;
262     }
263     if (dd->values == NULL)
264     {
265       WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
266       status = -1;
267       break;
268     }
269
270     break;
271   } /* while (status == 0) */
272
273   if (status != 0)
274   {
275     sfree (dd->name);
276     sfree (dd->values);
277     sfree (dd);
278     return (-1);
279   }
280
281   DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
282       dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
283
284   if (data_head == NULL)
285     data_head = dd;
286   else
287   {
288     data_definition_t *last;
289     last = data_head;
290     while (last->next != NULL)
291       last = last->next;
292     last->next = dd;
293   }
294
295   return (0);
296 } /* int csnmp_config_add_data */
297
298 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
299 {
300   if ((ci->values_num != 1)
301       || (ci->values[0].type != OCONFIG_TYPE_STRING))
302   {
303     WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
304     return (-1);
305   }
306
307   if (hd->address == NULL)
308     free (hd->address);
309
310   hd->address = strdup (ci->values[0].value.string);
311   if (hd->address == NULL)
312     return (-1);
313
314   DEBUG ("snmp plugin: host = %s; host->address = %s;",
315       hd->name, hd->address);
316
317   hd->sess.peername = hd->address;
318
319   return (0);
320 } /* int csnmp_config_add_host_address */
321
322 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
323 {
324   if ((ci->values_num != 1)
325       || (ci->values[0].type != OCONFIG_TYPE_STRING))
326   {
327     WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
328     return (-1);
329   }
330
331   if (hd->community == NULL)
332     free (hd->community);
333
334   hd->community = strdup (ci->values[0].value.string);
335   if (hd->community == NULL)
336     return (-1);
337
338   DEBUG ("snmp plugin: host = %s; host->community = %s;",
339       hd->name, hd->community);
340
341   hd->sess.community = (u_char *) hd->community;
342   hd->sess.community_len = strlen (hd->community);
343
344   return (0);
345 } /* int csnmp_config_add_host_community */
346
347 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
348 {
349   int version;
350
351   if ((ci->values_num != 1)
352       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
353   {
354     WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
355     return (-1);
356   }
357
358   version = (int) ci->values[0].value.number;
359   if ((version != 1) && (version != 2))
360   {
361     WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
362     return (-1);
363   }
364
365   hd->version = version;
366
367   if (hd->version == 1)
368     hd->sess.version = SNMP_VERSION_1;
369   else
370     hd->sess.version = SNMP_VERSION_2c;
371
372   return (0);
373 } /* int csnmp_config_add_host_address */
374
375 static int csnmp_config_add_host_collect (host_definition_t *host,
376     oconfig_item_t *ci)
377 {
378   data_definition_t *data;
379   data_definition_t **data_list;
380   int data_list_len;
381   int i;
382
383   if (ci->values_num < 1)
384   {
385     WARNING ("snmp plugin: `Collect' needs at least one argument.");
386     return (-1);
387   }
388
389   for (i = 0; i < ci->values_num; i++)
390     if (ci->values[i].type != OCONFIG_TYPE_STRING)
391     {
392       WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
393       return (-1);
394     }
395
396   data_list_len = host->data_list_len + ci->values_num;
397   data_list = (data_definition_t **) realloc (host->data_list,
398       sizeof (data_definition_t *) * data_list_len);
399   if (data_list == NULL)
400     return (-1);
401   host->data_list = data_list;
402
403   for (i = 0; i < ci->values_num; i++)
404   {
405     for (data = data_head; data != NULL; data = data->next)
406       if (strcasecmp (ci->values[i].value.string, data->name) == 0)
407         break;
408
409     if (data == NULL)
410     {
411       WARNING ("snmp plugin: No such data configured: `%s'",
412           ci->values[i].value.string);
413       continue;
414     }
415
416     DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
417         host->name, host->data_list_len, data->name);
418
419     host->data_list[host->data_list_len] = data;
420     host->data_list_len++;
421   } /* for (values_num) */
422
423   return (0);
424 } /* int csnmp_config_add_host_collect */
425
426 static int csnmp_config_add_host (oconfig_item_t *ci)
427 {
428   host_definition_t *hd;
429   int status = 0;
430   int i;
431
432   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
433   {
434     WARNING ("snmp plugin: `Host' needs exactly one string argument.");
435     return (-1);
436   }
437
438   hd = (host_definition_t *) malloc (sizeof (host_definition_t));
439   if (hd == NULL)
440     return (-1);
441   memset (hd, '\0', sizeof (host_definition_t));
442   hd->version = 2;
443
444   hd->name = strdup (ci->values[0].value.string);
445   if (hd->name == NULL)
446   {
447     free (hd);
448     return (-1);
449   }
450
451   snmp_sess_init (&hd->sess);
452   hd->sess.version = SNMP_VERSION_2c;
453
454   for (i = 0; i < ci->children_num; i++)
455   {
456     oconfig_item_t *option = ci->children + i;
457     status = 0;
458
459     if (strcasecmp ("Address", option->key) == 0)
460       status = csnmp_config_add_host_address (hd, option);
461     else if (strcasecmp ("Community", option->key) == 0)
462       status = csnmp_config_add_host_community (hd, option);
463     else if (strcasecmp ("Version", option->key) == 0)
464       status = csnmp_config_add_host_version (hd, option);
465     else if (strcasecmp ("Collect", option->key) == 0)
466       csnmp_config_add_host_collect (hd, option);
467     else
468     {
469       WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
470       status = -1;
471     }
472
473     if (status != 0)
474       break;
475   } /* for (ci->children) */
476
477   while (status == 0)
478   {
479     if (hd->address == NULL)
480     {
481       WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
482       status = -1;
483       break;
484     }
485     if (hd->community == NULL)
486     {
487       WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
488       status = -1;
489       break;
490     }
491
492     break;
493   } /* while (status == 0) */
494
495   if (status != 0)
496   {
497     sfree (hd->name);
498     sfree (hd);
499     return (-1);
500   }
501
502   DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
503       hd->name, hd->address, hd->community, hd->version);
504
505   if (host_head == NULL)
506     host_head = hd;
507   else
508   {
509     host_definition_t *last;
510     last = host_head;
511     while (last->next != NULL)
512       last = last->next;
513     last->next = hd;
514   }
515
516   return (0);
517 } /* int csnmp_config_add_host */
518
519 static int csnmp_config (oconfig_item_t *ci)
520 {
521   int i;
522
523   call_snmp_init_once ();
524
525   for (i = 0; i < ci->children_num; i++)
526   {
527     oconfig_item_t *child = ci->children + i;
528     if (strcasecmp ("Data", child->key) == 0)
529       csnmp_config_add_data (child);
530     else if (strcasecmp ("Host", child->key) == 0)
531       csnmp_config_add_host (child);
532     else
533     {
534       WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
535     }
536   } /* for (ci->children) */
537
538   return (0);
539 } /* int csnmp_config */
540
541 static int csnmp_init (void)
542 {
543   call_snmp_init_once ();
544   return (0);
545 }
546
547 #if 0
548 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
549 {
550   value_t values[3];
551   value_list_t vl = VALUE_LIST_INIT;
552
553   values[0].gauge = snum;
554   values[1].gauge = mnum;
555   values[2].gauge = lnum;
556
557   vl.values = values;
558   vl.values_len = STATIC_ARRAY_SIZE (values);
559   vl.time = time (NULL);
560   strcpy (vl.host, hostname_g);
561   strcpy (vl.plugin, "load");
562
563   plugin_dispatch_values ("load", &vl);
564 }
565 #endif
566
567 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
568 {
569   value_t ret;
570   uint64_t temp = 0;
571   int defined = 1;
572
573   if ((vl->type == ASN_INTEGER)
574       || (vl->type == ASN_UINTEGER)
575       || (vl->type == ASN_COUNTER)
576       || (vl->type == ASN_GAUGE))
577   {
578     temp = (uint32_t) *vl->val.integer;
579     DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
580   }
581   else if (vl->type == ASN_COUNTER64)
582   {
583     temp = (uint32_t) vl->val.counter64->high;
584     temp = temp << 32;
585     temp += (uint32_t) vl->val.counter64->low;
586     DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
587   }
588   else
589   {
590     WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
591     defined = 0;
592   }
593
594   if (type == DS_TYPE_COUNTER)
595   {
596     ret.counter = temp;
597   }
598   else if (type == DS_TYPE_GAUGE)
599   {
600     ret.gauge = NAN;
601     if (defined != 0)
602       ret.gauge = temp;
603   }
604
605   return (ret);
606 } /* value_t csnmp_value_list_to_value */
607
608 static int csnmp_read_table (struct snmp_session *sess_ptr,
609     host_definition_t *host, data_definition_t *data)
610 {
611   struct snmp_pdu *req;
612   struct snmp_pdu *res;
613   struct variable_list *vb;
614
615   oid_t *oid_list;
616   uint32_t oid_list_len;
617
618   const data_set_t *ds;
619   value_list_t vl = VALUE_LIST_INIT;
620
621   int status;
622   int i;
623
624   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
625       host->name, data->name);
626
627   ds = plugin_get_ds (data->type);
628   if (!ds)
629   {
630     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
631     return (-1);
632   }
633
634   if (ds->ds_num != data->values_len)
635   {
636     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
637         data->type, ds->ds_num, data->values_len);
638     return (-1);
639   }
640
641   vl.values_len = ds->ds_num;
642   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
643   if (vl.values == NULL)
644     return (-1);
645   for (i = 0; i < vl.values_len; i++)
646   {
647     if (ds->ds[i].type == DS_TYPE_COUNTER)
648       vl.values[i].counter = 0;
649     else
650       vl.values[i].gauge = NAN;
651   }
652
653   /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
654   oid_list_len = data->values_len + 1;
655   oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
656   memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
657   for (i = 0; i < data->values_len; i++)
658     memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
659
660   strncpy (vl.host, host->name, sizeof (vl.host));
661   vl.host[sizeof (vl.host) - 1] = '\0';
662   strcpy (vl.plugin, "snmp");
663   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
664   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
665
666   vl.time = time (NULL);
667
668   status = 0;
669   while (status == 0)
670   {
671     oid subid;
672
673     req = snmp_pdu_create (SNMP_MSG_GETNEXT);
674     if (req == NULL)
675     {
676       ERROR ("snmp plugin: snmp_pdu_create failed.");
677       status = -1;
678       break;
679     }
680
681     for (i = 0; i < oid_list_len; i++)
682       snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
683
684     status = snmp_synch_response (sess_ptr, req, &res);
685
686     if (status != STAT_SUCCESS)
687     {
688       ERROR ("snmp plugin: snmp_synch_response failed.");
689       status = -1;
690       break;
691     }
692     status = 0;
693     assert (res != NULL);
694
695     vb = res->variables;
696     if (vb == NULL)
697     {
698       status = -1;
699       break;
700     }
701
702     /* Check if we left the subtree */
703     if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
704           vb->name, vb->name_length,
705           data->instance.oid.oid_len) != 0)
706       break;
707
708     /* Get instance name */
709     if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
710     {
711       strncpy (vl.type_instance, vb->val.bitstring,
712           sizeof (vl.type_instance));
713       escape_slashes (vl.type_instance, strlen (vl.type_instance));
714     }
715     else
716     {
717       value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
718       snprintf (vl.type_instance, sizeof (vl.type_instance),
719           "%llu", val.counter);
720     }
721     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
722     DEBUG ("snmp plugin: data = `%s'; vl.type_instance = `%s';",
723         data->name, vl.type_instance);
724
725     /* Copy OID to oid_list[0] */
726     memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
727     oid_list[0].oid_len = vb->name_length;
728
729     /* Save the SUBID for the other values to check against */
730     subid = vb->name[vb->name_length - 1];
731
732     for (i = 0; i < data->values_len; i++)
733     {
734       vb = vb->next_variable;
735       if (vb == NULL)
736       {
737         status = -1;
738         break;
739       }
740
741       /* Check if we left the subtree */
742       if (snmp_oid_ncompare (data->values[i].oid,
743             data->values[i].oid_len,
744             vb->name, vb->name_length,
745             data->values[i].oid_len) != 0)
746       {
747         WARNING ("snmp plugin: Column %i of data `%s' left subtree before the index did.",
748             i + 1, data->type);
749         status = -1;
750         break;
751       }
752
753       /* Check SUBID */
754       if (subid != vb->name[vb->name_length - 1])
755       {
756         WARNING ("snmp plugin: Column %i of data `%s': SUBID mismatch: expected %i, found %i",
757             i + 1, data->type, (int) subid, (int) vb->name[vb->name_length - 1]);
758         status = -1;
759         break;
760       }
761
762       /* Get value */
763       vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
764       if (ds->ds[i].type == DS_TYPE_COUNTER)
765         DEBUG ("snmp plugin: data = `%s'; vl.values[%i] = (counter) %llu",
766             data->name, i, vl.values[i].counter);
767       else
768         DEBUG ("snmp plugin: data = `%s'; vl.values[%i] = (gauge)   %lf",
769             data->name, i, vl.values[i].gauge);
770
771       /* Copy OID to oid_list[i + 1] */
772       memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
773       oid_list[i + 1].oid_len = vb->name_length;
774     } /* for (data->values_len) */
775
776     if (res != NULL)
777       snmp_free_pdu (res);
778     res = NULL;
779
780     DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
781     if (status == 0)
782       plugin_dispatch_values (data->type, &vl);
783   } /* while (status == 0) */
784
785   sfree (vl.values);
786   sfree (oid_list);
787
788   return (0);
789 } /* int csnmp_read_table */
790
791 static int csnmp_read_value (struct snmp_session *sess_ptr,
792     host_definition_t *host, data_definition_t *data)
793 {
794   struct snmp_pdu *req;
795   struct snmp_pdu *res;
796   struct variable_list *vb;
797
798   const data_set_t *ds;
799   value_list_t vl = VALUE_LIST_INIT;
800
801   int status;
802   int i;
803
804   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
805       host->name, data->name);
806
807   ds = plugin_get_ds (data->type);
808   if (!ds)
809   {
810     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
811     return (-1);
812   }
813
814   if (ds->ds_num != data->values_len)
815   {
816     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
817         data->type, ds->ds_num, data->values_len);
818     return (-1);
819   }
820
821   vl.values_len = ds->ds_num;
822   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
823   if (vl.values == NULL)
824     return (-1);
825   for (i = 0; i < vl.values_len; i++)
826   {
827     if (ds->ds[i].type == DS_TYPE_COUNTER)
828       vl.values[i].counter = 0;
829     else
830       vl.values[i].gauge = NAN;
831   }
832
833   strncpy (vl.host, host->name, sizeof (vl.host));
834   vl.host[sizeof (vl.host) - 1] = '\0';
835   strcpy (vl.plugin, "snmp");
836   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
837   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
838
839   req = snmp_pdu_create (SNMP_MSG_GET);
840   if (req == NULL)
841   {
842     ERROR ("snmp plugin: snmp_pdu_create failed.");
843     sfree (vl.values);
844     return (-1);
845   }
846
847   for (i = 0; i < data->values_len; i++)
848     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
849   status = snmp_synch_response (sess_ptr, req, &res);
850
851   if (status != STAT_SUCCESS)
852   {
853     ERROR ("snmp plugin: snmp_synch_response failed.");
854     sfree (vl.values);
855     return (-1);
856   }
857
858   vl.time = time (NULL);
859
860   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
861   {
862     char buffer[1024];
863     snprint_variable (buffer, sizeof (buffer),
864         vb->name, vb->name_length, vb);
865     DEBUG ("snmp plugin: Got this variable: %s", buffer);
866
867     for (i = 0; i < data->values_len; i++)
868       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
869             vb->name, vb->name_length) == 0)
870         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
871   } /* for (res->variables) */
872
873   snmp_free_pdu (res);
874
875   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
876   plugin_dispatch_values (data->type, &vl);
877   sfree (vl.values);
878
879   return (0);
880 } /* int csnmp_read_value */
881
882 static int csnmp_read_host (host_definition_t *host)
883 {
884   struct snmp_session *sess_ptr;
885   int i;
886
887   DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
888
889   sess_ptr = snmp_open (&host->sess);
890   if (sess_ptr == NULL)
891   {
892     snmp_perror ("snmp_open");
893     ERROR ("snmp plugin: snmp_open failed.");
894     return (-1);
895   }
896
897   for (i = 0; i < host->data_list_len; i++)
898   {
899     data_definition_t *data = host->data_list[i];
900
901     if (data->is_table)
902       csnmp_read_table (sess_ptr, host, data);
903     else
904       csnmp_read_value (sess_ptr, host, data);
905   }
906
907   snmp_close (sess_ptr);
908   return (0);
909 } /* int csnmp_read_host */
910
911 static int csnmp_read (void)
912 {
913   host_definition_t *host;
914
915   if (host_head == NULL)
916   {
917     INFO ("snmp plugin: No hosts configured.");
918     return (-1);
919   }
920
921   for (host = host_head; host != NULL; host = host->next)
922     csnmp_read_host (host);
923
924   return (0);
925 } /* int csnmp_read */
926
927 void module_register (void)
928 {
929   plugin_register_complex_config ("snmp", csnmp_config);
930   plugin_register_init ("snmp", csnmp_init);
931   plugin_register_read ("snmp", csnmp_read);
932 } /* void module_register */
933
934 /*
935  * vim: shiftwidth=2 softtabstop=2 tabstop=8
936  */