snmp plugin: Added the first prototype of an SNMP plugin.
[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_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 (oconfig_item_t *ci)
376 {
377   host_definition_t *hd;
378   int status = 0;
379   int i;
380
381   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
382   {
383     WARNING ("snmp plugin: `Host' needs exactly one string argument.");
384     return (-1);
385   }
386
387   hd = (host_definition_t *) malloc (sizeof (host_definition_t));
388   if (hd == NULL)
389     return (-1);
390   memset (hd, '\0', sizeof (host_definition_t));
391   hd->version = 2;
392
393   hd->name = strdup (ci->values[0].value.string);
394   if (hd->name == NULL)
395   {
396     free (hd);
397     return (-1);
398   }
399
400   snmp_sess_init (&hd->sess);
401   hd->sess.version = SNMP_VERSION_2c;
402
403   for (i = 0; i < ci->children_num; i++)
404   {
405     oconfig_item_t *option = ci->children + i;
406     status = 0;
407
408     if (strcasecmp ("Address", option->key) == 0)
409       status = csnmp_config_add_host_address (hd, option);
410     else if (strcasecmp ("Community", option->key) == 0)
411       status = csnmp_config_add_host_community (hd, option);
412     else if (strcasecmp ("Version", option->key) == 0)
413       status = csnmp_config_add_host_version (hd, option);
414     else
415     {
416       WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
417       status = -1;
418     }
419
420     if (status != 0)
421       break;
422   } /* for (ci->children) */
423
424   while (status == 0)
425   {
426     if (hd->address == NULL)
427     {
428       WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
429       status = -1;
430       break;
431     }
432     if (hd->community == NULL)
433     {
434       WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
435       status = -1;
436       break;
437     }
438
439     break;
440   } /* while (status == 0) */
441
442   if (status != 0)
443   {
444     sfree (hd->name);
445     sfree (hd);
446     return (-1);
447   }
448
449   /* TODO: Check all fields in `hd'. */
450   DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
451       hd->name, hd->address, hd->community, hd->version);
452
453   if (host_head == NULL)
454     host_head = hd;
455   else
456   {
457     host_definition_t *last;
458     last = host_head;
459     while (last->next != NULL)
460       last = last->next;
461     last->next = hd;
462   }
463
464   return (0);
465 } /* int csnmp_config_add_host */
466
467 static int csnmp_config_add_collect (oconfig_item_t *ci)
468 {
469   data_definition_t *data;
470   host_definition_t *host;
471   data_definition_t **data_list;
472   int data_list_len;
473   int i;
474
475   if (ci->values_num < 2)
476   {
477     WARNING ("snmp plugin: `Collect' needs at least two arguments.");
478     return (-1);
479   }
480
481   for (i = 0; i < ci->values_num; i++)
482     if (ci->values[i].type != OCONFIG_TYPE_STRING)
483     {
484       WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
485       return (-1);
486     }
487
488   for (host = host_head; host != NULL; host = host->next)
489     if (strcasecmp (ci->values[0].value.string, host->name) == 0)
490       break;
491
492   if (host == NULL)
493   {
494     WARNING ("snmp plugin: No such host configured: `%s'",
495         ci->values[0].value.string);
496     return (-1);
497   }
498
499   data_list_len = host->data_list_len + ci->values_num - 1;
500   data_list = (data_definition_t **) realloc (host->data_list,
501       sizeof (data_definition_t *) * data_list_len);
502   if (data_list == NULL)
503     return (-1);
504   host->data_list = data_list;
505
506   for (i = 1; i < ci->values_num; i++)
507   {
508     for (data = data_head; data != NULL; data = data->next)
509       if (strcasecmp (ci->values[i].value.string, data->name) == 0)
510         break;
511
512     if (data == NULL)
513     {
514       WARNING ("snmp plugin: No such data configured: `%s'",
515           ci->values[i].value.string);
516       continue;
517     }
518
519     DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
520         host->name, host->data_list_len, data->name);
521
522     host->data_list[host->data_list_len] = data;
523     host->data_list_len++;
524   } /* for (values_num) */
525
526   return (0);
527 } /* int csnmp_config_add_collect */
528
529 static int csnmp_config (oconfig_item_t *ci)
530 {
531   int i;
532
533   call_snmp_init_once ();
534
535   for (i = 0; i < ci->children_num; i++)
536   {
537     oconfig_item_t *child = ci->children + i;
538     if (strcasecmp ("Data", child->key) == 0)
539       csnmp_config_add_data (child);
540     else if (strcasecmp ("Host", child->key) == 0)
541       csnmp_config_add_host (child);
542     else if (strcasecmp ("Collect", child->key) == 0)
543       csnmp_config_add_collect (child);
544     else
545     {
546       WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
547     }
548   } /* for (ci->children) */
549
550   return (0);
551 } /* int csnmp_config */
552
553 static int csnmp_init (void)
554 {
555   call_snmp_init_once ();
556   return (0);
557 }
558
559 #if 0
560 static void csnmp_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
561 {
562   value_t values[3];
563   value_list_t vl = VALUE_LIST_INIT;
564
565   values[0].gauge = snum;
566   values[1].gauge = mnum;
567   values[2].gauge = lnum;
568
569   vl.values = values;
570   vl.values_len = STATIC_ARRAY_SIZE (values);
571   vl.time = time (NULL);
572   strcpy (vl.host, hostname_g);
573   strcpy (vl.plugin, "load");
574
575   plugin_dispatch_values ("load", &vl);
576 }
577 #endif
578
579 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
580 {
581   value_t ret;
582   uint64_t temp = 0;
583   int defined = 1;
584
585   if ((vl->type == ASN_INTEGER)
586       || (vl->type == ASN_UINTEGER)
587       || (vl->type == ASN_COUNTER)
588       || (vl->type == ASN_GAUGE))
589   {
590     temp = *vl->val.integer;
591   }
592   else if (vl->type == ASN_COUNTER64)
593   {
594     temp = vl->val.counter64->high;
595     temp = temp << 32;
596     temp += vl->val.counter64->low;
597   }
598   else
599   {
600     WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
601     defined = 0;
602   }
603
604   if (type == DS_TYPE_COUNTER)
605   {
606     ret.counter = temp;
607   }
608   else if (type == DS_TYPE_GAUGE)
609   {
610     ret.gauge = NAN;
611     if (defined != 0)
612       ret.gauge = temp;
613   }
614
615   return (ret);
616 } /* value_t csnmp_value_list_to_value */
617
618 static int csnmp_read_table (struct snmp_session *sess_ptr,
619     host_definition_t *host, data_definition_t *data)
620 {
621   return (0);
622 } /* int csnmp_read_table */
623
624 static int csnmp_read_value (struct snmp_session *sess_ptr,
625     host_definition_t *host, data_definition_t *data)
626 {
627   struct snmp_pdu *req;
628   struct snmp_pdu *res;
629   struct variable_list *vb;
630
631   const data_set_t *ds;
632   value_list_t vl = VALUE_LIST_INIT;
633
634   int status;
635   int i;
636
637   DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
638       host->name, data->name);
639
640   ds = plugin_get_ds (data->type);
641   if (!ds)
642   {
643     ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
644     return (-1);
645   }
646
647   if (ds->ds_num != data->values_len)
648   {
649     ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
650         data->type, ds->ds_num, data->values_len);
651     return (-1);
652   }
653
654   vl.values_len = ds->ds_num;
655   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
656   if (vl.values == NULL)
657     return (-1);
658   for (i = 0; i < vl.values_len; i++)
659   {
660     if (ds->ds[i].type == DS_TYPE_COUNTER)
661       vl.values[i].counter = 0;
662     else
663       vl.values[i].gauge = NAN;
664   }
665
666   strncpy (vl.host, host->name, sizeof (vl.host));
667   vl.host[sizeof (vl.host) - 1] = '\0';
668   strcpy (vl.plugin, "snmp");
669   strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
670   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
671
672   req = snmp_pdu_create (SNMP_MSG_GET);
673   if (req == NULL)
674   {
675     ERROR ("snmp plugin: snmp_pdu_create failed.");
676     sfree (vl.values);
677     return (-1);
678   }
679
680   for (i = 0; i < data->values_len; i++)
681     snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
682   status = snmp_synch_response (sess_ptr, req, &res);
683
684   if (status != STAT_SUCCESS)
685   {
686     ERROR ("snmp plugin: snmp_synch_response failed.");
687     sfree (vl.values);
688     return (-1);
689   }
690
691   vl.time = time (NULL);
692
693   for (vb = res->variables; vb != NULL; vb = vb->next_variable)
694   {
695     char buffer[1024];
696     snprint_variable (buffer, sizeof (buffer),
697         vb->name, vb->name_length, vb);
698     DEBUG ("snmp plugin: Got this variable: %s", buffer);
699
700     for (i = 0; i < data->values_len; i++)
701       if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
702             vb->name, vb->name_length) == 0)
703         vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
704   } /* for (res->variables) */
705
706   snmp_free_pdu (res);
707
708   DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
709   plugin_dispatch_values (data->type, &vl);
710
711   return (0);
712 } /* int csnmp_read_value */
713
714 static int csnmp_read_host (host_definition_t *host)
715 {
716   struct snmp_session *sess_ptr;
717   int i;
718
719   DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
720
721   sess_ptr = snmp_open (&host->sess);
722   if (sess_ptr == NULL)
723   {
724     snmp_perror ("snmp_open");
725     ERROR ("snmp plugin: snmp_open failed.");
726     return (-1);
727   }
728
729   for (i = 0; i < host->data_list_len; i++)
730   {
731     data_definition_t *data = host->data_list[i];
732
733     if (data->is_table)
734       csnmp_read_table (sess_ptr, host, data);
735     else
736       csnmp_read_value (sess_ptr, host, data);
737   }
738
739   snmp_close (sess_ptr);
740   return (0);
741 } /* int csnmp_read_host */
742
743 static int csnmp_read (void)
744 {
745   host_definition_t *host;
746
747   if (host_head == NULL)
748   {
749     INFO ("snmp plugin: No hosts configured.");
750     return (-1);
751   }
752
753   for (host = host_head; host != NULL; host = host->next)
754     csnmp_read_host (host);
755
756   return (0);
757 } /* int csnmp_read */
758
759 void module_register (void)
760 {
761   plugin_register_complex_config ("snmp", csnmp_config);
762   plugin_register_init ("snmp", csnmp_init);
763   plugin_register_read ("snmp", csnmp_read);
764 } /* void module_register */
765
766 /*
767  * vim: shiftwidth=2 softtabstop=2 tabstop=8
768  */