Introduce the DERIVE and ABSOLUTE data source types.
[collectd.git] / src / curl.c
1 /**
2  * collectd - src/curl.c
3  * Copyright (C) 2006-2009  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 #include "configfile.h"
26 #include "utils_match.h"
27
28 #include <curl/curl.h>
29
30 /*
31  * Data types
32  */
33 struct web_match_s;
34 typedef struct web_match_s web_match_t;
35 struct web_match_s /* {{{ */
36 {
37   char *regex;
38   int dstype;
39   char *type;
40   char *instance;
41
42   cu_match_t *match;
43
44   web_match_t *next;
45 }; /* }}} */
46
47 struct web_page_s;
48 typedef struct web_page_s web_page_t;
49 struct web_page_s /* {{{ */
50 {
51   char *instance;
52
53   char *url;
54   char *user;
55   char *pass;
56   char *credentials;
57   int   verify_peer;
58   int   verify_host;
59   char *cacert;
60
61   CURL *curl;
62   char curl_errbuf[CURL_ERROR_SIZE];
63   char *buffer;
64   size_t buffer_size;
65   size_t buffer_fill;
66
67   web_match_t *matches;
68
69   web_page_t *next;
70 }; /* }}} */
71
72 /*
73  * Global variables;
74  */
75 /* static CURLM *curl = NULL; */
76 static web_page_t *pages_g = NULL;
77
78 /*
79  * Private functions
80  */
81 static size_t cc_curl_callback (void *buf, /* {{{ */
82     size_t size, size_t nmemb, void *user_data)
83 {
84   web_page_t *wp;
85   size_t len;
86   
87   len = size * nmemb;
88   if (len <= 0)
89     return (len);
90
91   wp = user_data;
92   if (wp == NULL)
93     return (0);
94
95   if ((wp->buffer_fill + len) >= wp->buffer_size)
96   {
97     char *temp;
98     size_t temp_size;
99
100     temp_size = wp->buffer_fill + len + 1;
101     temp = (char *) realloc (wp->buffer, temp_size);
102     if (temp == NULL)
103     {
104       ERROR ("curl plugin: realloc failed.");
105       return (0);
106     }
107     wp->buffer = temp;
108     wp->buffer_size = temp_size;
109   }
110
111   memcpy (wp->buffer + wp->buffer_fill, (char *) buf, len);
112   wp->buffer_fill += len;
113   wp->buffer[wp->buffer_fill] = 0;
114
115   return (len);
116 } /* }}} size_t cc_curl_callback */
117
118 static void cc_web_match_free (web_match_t *wm) /* {{{ */
119 {
120   if (wm == NULL)
121     return;
122
123   sfree (wm->regex);
124   sfree (wm->type);
125   sfree (wm->instance);
126   match_destroy (wm->match);
127   cc_web_match_free (wm->next);
128   sfree (wm);
129 } /* }}} void cc_web_match_free */
130
131 static void cc_web_page_free (web_page_t *wp) /* {{{ */
132 {
133   if (wp == NULL)
134     return;
135
136   if (wp->curl != NULL)
137     curl_easy_cleanup (wp->curl);
138   wp->curl = NULL;
139
140   sfree (wp->instance);
141
142   sfree (wp->url);
143   sfree (wp->user);
144   sfree (wp->pass);
145   sfree (wp->credentials);
146   sfree (wp->cacert);
147
148   sfree (wp->buffer);
149
150   cc_web_match_free (wp->matches);
151   cc_web_page_free (wp->next);
152   sfree (wp);
153 } /* }}} void cc_web_page_free */
154
155 static int cc_config_add_string (const char *name, char **dest, /* {{{ */
156     oconfig_item_t *ci)
157 {
158   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
159   {
160     WARNING ("curl plugin: `%s' needs exactly one string argument.", name);
161     return (-1);
162   }
163
164   sfree (*dest);
165   *dest = strdup (ci->values[0].value.string);
166   if (*dest == NULL)
167     return (-1);
168
169   return (0);
170 } /* }}} int cc_config_add_string */
171
172 static int cc_config_set_boolean (const char *name, int *dest, /* {{{ */
173     oconfig_item_t *ci)
174 {
175   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
176   {
177     WARNING ("curl plugin: `%s' needs exactly one boolean argument.", name);
178     return (-1);
179   }
180
181   *dest = ci->values[0].value.boolean ? 1 : 0;
182
183   return (0);
184 } /* }}} int cc_config_set_boolean */
185
186 static int cc_config_add_match_dstype (int *dstype_ret, /* {{{ */
187     oconfig_item_t *ci)
188 {
189   int dstype;
190
191   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
192   {
193     WARNING ("curl plugin: `DSType' needs exactly one string argument.");
194     return (-1);
195   }
196
197   if (strncasecmp ("Gauge", ci->values[0].value.string,
198         strlen ("Gauge")) == 0)
199   {
200     dstype = UTILS_MATCH_DS_TYPE_GAUGE;
201     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
202       dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
203     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
204       dstype |= UTILS_MATCH_CF_GAUGE_MIN;
205     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
206       dstype |= UTILS_MATCH_CF_GAUGE_MAX;
207     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
208       dstype |= UTILS_MATCH_CF_GAUGE_LAST;
209     else
210       dstype = 0;
211   }
212   else if (strncasecmp ("Counter", ci->values[0].value.string,
213         strlen ("Counter")) == 0)
214   {
215     dstype = UTILS_MATCH_DS_TYPE_COUNTER;
216     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
217       dstype |= UTILS_MATCH_CF_COUNTER_SET;
218     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
219       dstype |= UTILS_MATCH_CF_COUNTER_ADD;
220     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
221       dstype |= UTILS_MATCH_CF_COUNTER_INC;
222     else
223       dstype = 0;
224   }
225 else if (strncasecmp ("Derive", ci->values[0].value.string,
226         strlen ("Derive")) == 0)
227   {
228     dstype = UTILS_MATCH_DS_TYPE_DERIVE;
229     if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
230       dstype |= UTILS_MATCH_CF_DERIVE_SET;
231     else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
232       dstype |= UTILS_MATCH_CF_DERIVE_ADD;
233     else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
234       dstype |= UTILS_MATCH_CF_DERIVE_INC;
235     else
236       dstype = 0;
237   }
238 else if (strncasecmp ("Absolute", ci->values[0].value.string,
239         strlen ("Absolute")) == 0)
240   {
241     dstype = UTILS_MATCH_DS_TYPE_ABSOLUTE;
242     if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0) /* Absolute DS is reset-on-read so no sense doin anything else but set */
243       dstype |= UTILS_MATCH_CF_ABSOLUTE_SET;
244     else
245       dstype = 0;
246   }
247
248   else
249   {
250     dstype = 0;
251   }
252
253   if (dstype == 0)
254   {
255     WARNING ("curl plugin: `%s' is not a valid argument to `DSType'.",
256         ci->values[0].value.string);
257     return (-1);
258   }
259
260   *dstype_ret = dstype;
261   return (0);
262 } /* }}} int cc_config_add_match_dstype */
263
264 static int cc_config_add_match (web_page_t *page, /* {{{ */
265     oconfig_item_t *ci)
266 {
267   web_match_t *match;
268   int status;
269   int i;
270
271   if (ci->values_num != 0)
272   {
273     WARNING ("curl plugin: Ignoring arguments for the `Match' block.");
274   }
275
276   match = (web_match_t *) malloc (sizeof (*match));
277   if (match == NULL)
278   {
279     ERROR ("curl plugin: malloc failed.");
280     return (-1);
281   }
282   memset (match, 0, sizeof (*match));
283
284   status = 0;
285   for (i = 0; i < ci->children_num; i++)
286   {
287     oconfig_item_t *child = ci->children + i;
288
289     if (strcasecmp ("Regex", child->key) == 0)
290       status = cc_config_add_string ("Regex", &match->regex, child);
291     else if (strcasecmp ("DSType", child->key) == 0)
292       status = cc_config_add_match_dstype (&match->dstype, child);
293     else if (strcasecmp ("Type", child->key) == 0)
294       status = cc_config_add_string ("Type", &match->type, child);
295     else if (strcasecmp ("Instance", child->key) == 0)
296       status = cc_config_add_string ("Instance", &match->instance, child);
297     else
298     {
299       WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
300       status = -1;
301     }
302
303     if (status != 0)
304       break;
305   } /* for (i = 0; i < ci->children_num; i++) */
306
307   while (status == 0)
308   {
309     if (match->regex == NULL)
310     {
311       WARNING ("curl plugin: `Regex' missing in `Match' block.");
312       status = -1;
313     }
314
315     if (match->type == NULL)
316     {
317       WARNING ("curl plugin: `Type' missing in `Match' block.");
318       status = -1;
319     }
320
321     if (match->dstype == 0)
322     {
323       WARNING ("curl plugin: `DSType' missing in `Match' block.");
324       status = -1;
325     }
326
327     break;
328   } /* while (status == 0) */
329
330   if (status != 0)
331     return (status);
332
333   match->match = match_create_simple (match->regex, match->dstype);
334   if (match->match == NULL)
335   {
336     ERROR ("curl plugin: tail_match_add_match_simple failed.");
337     cc_web_match_free (match);
338     return (-1);
339   }
340   else
341   {
342     web_match_t *prev;
343
344     prev = page->matches;
345     while ((prev != NULL) && (prev->next != NULL))
346       prev = prev->next;
347
348     if (prev == NULL)
349       page->matches = match;
350     else
351       prev->next = match;
352   }
353
354   return (0);
355 } /* }}} int cc_config_add_match */
356
357 static int cc_page_init_curl (web_page_t *wp) /* {{{ */
358 {
359   wp->curl = curl_easy_init ();
360   if (wp->curl == NULL)
361   {
362     ERROR ("curl plugin: curl_easy_init failed.");
363     return (-1);
364   }
365
366   curl_easy_setopt (wp->curl, CURLOPT_WRITEFUNCTION, cc_curl_callback);
367   curl_easy_setopt (wp->curl, CURLOPT_WRITEDATA, wp);
368   curl_easy_setopt (wp->curl, CURLOPT_USERAGENT,
369       PACKAGE_NAME"/"PACKAGE_VERSION);
370   curl_easy_setopt (wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
371   curl_easy_setopt (wp->curl, CURLOPT_URL, wp->url);
372
373   if (wp->user != NULL)
374   {
375     size_t credentials_size;
376
377     credentials_size = strlen (wp->user) + 2;
378     if (wp->pass != NULL)
379       credentials_size += strlen (wp->pass);
380
381     wp->credentials = (char *) malloc (credentials_size);
382     if (wp->credentials == NULL)
383     {
384       ERROR ("curl plugin: malloc failed.");
385       return (-1);
386     }
387
388     ssnprintf (wp->credentials, credentials_size, "%s:%s",
389         wp->user, (wp->pass == NULL) ? "" : wp->pass);
390     curl_easy_setopt (wp->curl, CURLOPT_USERPWD, wp->credentials);
391   }
392
393   curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYPEER, wp->verify_peer);
394   curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYHOST,
395       wp->verify_host ? 2 : 0);
396   if (wp->cacert != NULL)
397     curl_easy_setopt (wp->curl, CURLOPT_CAINFO, wp->cacert);
398
399   return (0);
400 } /* }}} int cc_page_init_curl */
401
402 static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */
403 {
404   web_page_t *page;
405   int status;
406   int i;
407
408   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
409   {
410     WARNING ("curl plugin: `Page' blocks need exactly one string argument.");
411     return (-1);
412   }
413
414   page = (web_page_t *) malloc (sizeof (*page));
415   if (page == NULL)
416   {
417     ERROR ("curl plugin: malloc failed.");
418     return (-1);
419   }
420   memset (page, 0, sizeof (*page));
421   page->url = NULL;
422   page->user = NULL;
423   page->pass = NULL;
424   page->verify_peer = 1;
425   page->verify_host = 1;
426
427   page->instance = strdup (ci->values[0].value.string);
428   if (page->instance == NULL)
429   {
430     ERROR ("curl plugin: strdup failed.");
431     sfree (page);
432     return (-1);
433   }
434
435   /* Process all children */
436   status = 0;
437   for (i = 0; i < ci->children_num; i++)
438   {
439     oconfig_item_t *child = ci->children + i;
440
441     if (strcasecmp ("URL", child->key) == 0)
442       status = cc_config_add_string ("URL", &page->url, child);
443     else if (strcasecmp ("User", child->key) == 0)
444       status = cc_config_add_string ("User", &page->user, child);
445     else if (strcasecmp ("Password", child->key) == 0)
446       status = cc_config_add_string ("Password", &page->pass, child);
447     else if (strcasecmp ("VerifyPeer", child->key) == 0)
448       status = cc_config_set_boolean ("VerifyPeer", &page->verify_peer, child);
449     else if (strcasecmp ("VerifyHost", child->key) == 0)
450       status = cc_config_set_boolean ("VerifyHost", &page->verify_host, child);
451     else if (strcasecmp ("CACert", child->key) == 0)
452       status = cc_config_add_string ("CACert", &page->cacert, child);
453     else if (strcasecmp ("Match", child->key) == 0)
454       /* Be liberal with failing matches => don't set `status'. */
455       cc_config_add_match (page, child);
456     else
457     {
458       WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
459       status = -1;
460     }
461
462     if (status != 0)
463       break;
464   } /* for (i = 0; i < ci->children_num; i++) */
465
466   /* Additionial sanity checks and libCURL initialization. */
467   while (status == 0)
468   {
469     if (page->url == NULL)
470     {
471       WARNING ("curl plugin: `URL' missing in `Page' block.");
472       status = -1;
473     }
474
475     if (page->matches == NULL)
476     {
477       assert (page->instance != NULL);
478       WARNING ("curl plugin: No (valid) `Match' block "
479           "within `Page' block `%s'.", page->instance);
480       status = -1;
481     }
482
483     if (status == 0)
484       status = cc_page_init_curl (page);
485
486     break;
487   } /* while (status == 0) */
488
489   if (status != 0)
490   {
491     cc_web_page_free (page);
492     return (status);
493   }
494
495   /* Add the new page to the linked list */
496   if (pages_g == NULL)
497     pages_g = page;
498   else
499   {
500     web_page_t *prev;
501
502     prev = pages_g;
503     while ((prev != NULL) && (prev->next != NULL))
504       prev = prev->next;
505     prev->next = page;
506   }
507
508   return (0);
509 } /* }}} int cc_config_add_page */
510
511 static int cc_config (oconfig_item_t *ci) /* {{{ */
512 {
513   int success;
514   int errors;
515   int status;
516   int i;
517
518   success = 0;
519   errors = 0;
520
521   for (i = 0; i < ci->children_num; i++)
522   {
523     oconfig_item_t *child = ci->children + i;
524
525     if (strcasecmp ("Page", child->key) == 0)
526     {
527       status = cc_config_add_page (child);
528       if (status == 0)
529         success++;
530       else
531         errors++;
532     }
533     else
534     {
535       WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
536       errors++;
537     }
538   }
539
540   if ((success == 0) && (errors > 0))
541   {
542     ERROR ("curl plugin: All statements failed.");
543     return (-1);
544   }
545
546   return (0);
547 } /* }}} int cc_config */
548
549 static int cc_init (void) /* {{{ */
550 {
551   if (pages_g == NULL)
552   {
553     INFO ("curl plugin: No pages have been defined.");
554     return (-1);
555   }
556   return (0);
557 } /* }}} int cc_init */
558
559 static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
560     const cu_match_value_t *mv)
561 {
562   value_t values[1];
563   value_list_t vl = VALUE_LIST_INIT;
564
565   values[0] = mv->value;
566
567   vl.values = values;
568   vl.values_len = 1;
569   vl.time = time (NULL);
570   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
571   sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
572   sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
573   sstrncpy (vl.type, wm->type, sizeof (vl.type));
574   sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
575
576   plugin_dispatch_values (&vl);
577 } /* }}} void cc_submit */
578
579 static int cc_read_page (web_page_t *wp) /* {{{ */
580 {
581   web_match_t *wm;
582   int status;
583
584   wp->buffer_fill = 0;
585   status = curl_easy_perform (wp->curl);
586   if (status != 0)
587   {
588     ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
589         status, wp->curl_errbuf);
590     return (-1);
591   }
592
593   for (wm = wp->matches; wm != NULL; wm = wm->next)
594   {
595     cu_match_value_t *mv;
596
597     status = match_apply (wm->match, wp->buffer);
598     if (status != 0)
599     {
600       WARNING ("curl plugin: match_apply failed.");
601       continue;
602     }
603
604     mv = match_get_user_data (wm->match);
605     if (mv == NULL)
606     {
607       WARNING ("curl plugin: match_get_user_data returned NULL.");
608       continue;
609     }
610
611     cc_submit (wp, wm, mv);
612   } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
613
614   return (0);
615 } /* }}} int cc_read_page */
616
617 static int cc_read (void) /* {{{ */
618 {
619   web_page_t *wp;
620
621   for (wp = pages_g; wp != NULL; wp = wp->next)
622     cc_read_page (wp);
623
624   return (0);
625 } /* }}} int cc_read */
626
627 static int cc_shutdown (void) /* {{{ */
628 {
629   cc_web_page_free (pages_g);
630   pages_g = NULL;
631
632   return (0);
633 } /* }}} int cc_shutdown */
634
635 void module_register (void)
636 {
637   plugin_register_complex_config ("curl", cc_config);
638   plugin_register_init ("curl", cc_init);
639   plugin_register_read ("curl", cc_read);
640   plugin_register_shutdown ("curl", cc_shutdown);
641 } /* void module_register */
642
643 /* vim: set sw=2 sts=2 et fdm=marker : */