Plugins using libcurl: Enable the ‘CURLOPT_FOLLOWLOCATION’ option.
[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
226   {
227     dstype = 0;
228   }
229
230   if (dstype == 0)
231   {
232     WARNING ("curl plugin: `%s' is not a valid argument to `DSType'.",
233         ci->values[0].value.string);
234     return (-1);
235   }
236
237   *dstype_ret = dstype;
238   return (0);
239 } /* }}} int cc_config_add_match_dstype */
240
241 static int cc_config_add_match (web_page_t *page, /* {{{ */
242     oconfig_item_t *ci)
243 {
244   web_match_t *match;
245   int status;
246   int i;
247
248   if (ci->values_num != 0)
249   {
250     WARNING ("curl plugin: Ignoring arguments for the `Match' block.");
251   }
252
253   match = (web_match_t *) malloc (sizeof (*match));
254   if (match == NULL)
255   {
256     ERROR ("curl plugin: malloc failed.");
257     return (-1);
258   }
259   memset (match, 0, sizeof (*match));
260
261   status = 0;
262   for (i = 0; i < ci->children_num; i++)
263   {
264     oconfig_item_t *child = ci->children + i;
265
266     if (strcasecmp ("Regex", child->key) == 0)
267       status = cc_config_add_string ("Regex", &match->regex, child);
268     else if (strcasecmp ("DSType", child->key) == 0)
269       status = cc_config_add_match_dstype (&match->dstype, child);
270     else if (strcasecmp ("Type", child->key) == 0)
271       status = cc_config_add_string ("Type", &match->type, child);
272     else if (strcasecmp ("Instance", child->key) == 0)
273       status = cc_config_add_string ("Instance", &match->instance, child);
274     else
275     {
276       WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
277       status = -1;
278     }
279
280     if (status != 0)
281       break;
282   } /* for (i = 0; i < ci->children_num; i++) */
283
284   while (status == 0)
285   {
286     if (match->regex == NULL)
287     {
288       WARNING ("curl plugin: `Regex' missing in `Match' block.");
289       status = -1;
290     }
291
292     if (match->type == NULL)
293     {
294       WARNING ("curl plugin: `Type' missing in `Match' block.");
295       status = -1;
296     }
297
298     if (match->dstype == 0)
299     {
300       WARNING ("curl plugin: `DSType' missing in `Match' block.");
301       status = -1;
302     }
303
304     break;
305   } /* while (status == 0) */
306
307   if (status != 0)
308     return (status);
309
310   match->match = match_create_simple (match->regex, match->dstype);
311   if (match->match == NULL)
312   {
313     ERROR ("curl plugin: tail_match_add_match_simple failed.");
314     cc_web_match_free (match);
315     return (-1);
316   }
317   else
318   {
319     web_match_t *prev;
320
321     prev = page->matches;
322     while ((prev != NULL) && (prev->next != NULL))
323       prev = prev->next;
324
325     if (prev == NULL)
326       page->matches = match;
327     else
328       prev->next = match;
329   }
330
331   return (0);
332 } /* }}} int cc_config_add_match */
333
334 static int cc_page_init_curl (web_page_t *wp) /* {{{ */
335 {
336   wp->curl = curl_easy_init ();
337   if (wp->curl == NULL)
338   {
339     ERROR ("curl plugin: curl_easy_init failed.");
340     return (-1);
341   }
342
343   curl_easy_setopt (wp->curl, CURLOPT_WRITEFUNCTION, cc_curl_callback);
344   curl_easy_setopt (wp->curl, CURLOPT_WRITEDATA, wp);
345   curl_easy_setopt (wp->curl, CURLOPT_USERAGENT,
346       PACKAGE_NAME"/"PACKAGE_VERSION);
347   curl_easy_setopt (wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
348   curl_easy_setopt (wp->curl, CURLOPT_URL, wp->url);
349   curl_easy_setopt (wp->curl, CURLOPT_FOLLOWLOCATION, 1);
350
351   if (wp->user != NULL)
352   {
353     size_t credentials_size;
354
355     credentials_size = strlen (wp->user) + 2;
356     if (wp->pass != NULL)
357       credentials_size += strlen (wp->pass);
358
359     wp->credentials = (char *) malloc (credentials_size);
360     if (wp->credentials == NULL)
361     {
362       ERROR ("curl plugin: malloc failed.");
363       return (-1);
364     }
365
366     ssnprintf (wp->credentials, credentials_size, "%s:%s",
367         wp->user, (wp->pass == NULL) ? "" : wp->pass);
368     curl_easy_setopt (wp->curl, CURLOPT_USERPWD, wp->credentials);
369   }
370
371   curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYPEER, wp->verify_peer);
372   curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYHOST,
373       wp->verify_host ? 2 : 0);
374   if (wp->cacert != NULL)
375     curl_easy_setopt (wp->curl, CURLOPT_CAINFO, wp->cacert);
376
377   return (0);
378 } /* }}} int cc_page_init_curl */
379
380 static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */
381 {
382   web_page_t *page;
383   int status;
384   int i;
385
386   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
387   {
388     WARNING ("curl plugin: `Page' blocks need exactly one string argument.");
389     return (-1);
390   }
391
392   page = (web_page_t *) malloc (sizeof (*page));
393   if (page == NULL)
394   {
395     ERROR ("curl plugin: malloc failed.");
396     return (-1);
397   }
398   memset (page, 0, sizeof (*page));
399   page->url = NULL;
400   page->user = NULL;
401   page->pass = NULL;
402   page->verify_peer = 1;
403   page->verify_host = 1;
404
405   page->instance = strdup (ci->values[0].value.string);
406   if (page->instance == NULL)
407   {
408     ERROR ("curl plugin: strdup failed.");
409     sfree (page);
410     return (-1);
411   }
412
413   /* Process all children */
414   status = 0;
415   for (i = 0; i < ci->children_num; i++)
416   {
417     oconfig_item_t *child = ci->children + i;
418
419     if (strcasecmp ("URL", child->key) == 0)
420       status = cc_config_add_string ("URL", &page->url, child);
421     else if (strcasecmp ("User", child->key) == 0)
422       status = cc_config_add_string ("User", &page->user, child);
423     else if (strcasecmp ("Password", child->key) == 0)
424       status = cc_config_add_string ("Password", &page->pass, child);
425     else if (strcasecmp ("VerifyPeer", child->key) == 0)
426       status = cc_config_set_boolean ("VerifyPeer", &page->verify_peer, child);
427     else if (strcasecmp ("VerifyHost", child->key) == 0)
428       status = cc_config_set_boolean ("VerifyHost", &page->verify_host, child);
429     else if (strcasecmp ("CACert", child->key) == 0)
430       status = cc_config_add_string ("CACert", &page->cacert, child);
431     else if (strcasecmp ("Match", child->key) == 0)
432       /* Be liberal with failing matches => don't set `status'. */
433       cc_config_add_match (page, child);
434     else
435     {
436       WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
437       status = -1;
438     }
439
440     if (status != 0)
441       break;
442   } /* for (i = 0; i < ci->children_num; i++) */
443
444   /* Additionial sanity checks and libCURL initialization. */
445   while (status == 0)
446   {
447     if (page->url == NULL)
448     {
449       WARNING ("curl plugin: `URL' missing in `Page' block.");
450       status = -1;
451     }
452
453     if (page->matches == NULL)
454     {
455       assert (page->instance != NULL);
456       WARNING ("curl plugin: No (valid) `Match' block "
457           "within `Page' block `%s'.", page->instance);
458       status = -1;
459     }
460
461     if (status == 0)
462       status = cc_page_init_curl (page);
463
464     break;
465   } /* while (status == 0) */
466
467   if (status != 0)
468   {
469     cc_web_page_free (page);
470     return (status);
471   }
472
473   /* Add the new page to the linked list */
474   if (pages_g == NULL)
475     pages_g = page;
476   else
477   {
478     web_page_t *prev;
479
480     prev = pages_g;
481     while ((prev != NULL) && (prev->next != NULL))
482       prev = prev->next;
483     prev->next = page;
484   }
485
486   return (0);
487 } /* }}} int cc_config_add_page */
488
489 static int cc_config (oconfig_item_t *ci) /* {{{ */
490 {
491   int success;
492   int errors;
493   int status;
494   int i;
495
496   success = 0;
497   errors = 0;
498
499   for (i = 0; i < ci->children_num; i++)
500   {
501     oconfig_item_t *child = ci->children + i;
502
503     if (strcasecmp ("Page", child->key) == 0)
504     {
505       status = cc_config_add_page (child);
506       if (status == 0)
507         success++;
508       else
509         errors++;
510     }
511     else
512     {
513       WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
514       errors++;
515     }
516   }
517
518   if ((success == 0) && (errors > 0))
519   {
520     ERROR ("curl plugin: All statements failed.");
521     return (-1);
522   }
523
524   return (0);
525 } /* }}} int cc_config */
526
527 static int cc_init (void) /* {{{ */
528 {
529   if (pages_g == NULL)
530   {
531     INFO ("curl plugin: No pages have been defined.");
532     return (-1);
533   }
534   return (0);
535 } /* }}} int cc_init */
536
537 static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
538     const cu_match_value_t *mv)
539 {
540   value_t values[1];
541   value_list_t vl = VALUE_LIST_INIT;
542
543   values[0] = mv->value;
544
545   vl.values = values;
546   vl.values_len = 1;
547   vl.time = time (NULL);
548   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
549   sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
550   sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
551   sstrncpy (vl.type, wm->type, sizeof (vl.type));
552   sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
553
554   plugin_dispatch_values (&vl);
555 } /* }}} void cc_submit */
556
557 static int cc_read_page (web_page_t *wp) /* {{{ */
558 {
559   web_match_t *wm;
560   int status;
561
562   wp->buffer_fill = 0;
563   status = curl_easy_perform (wp->curl);
564   if (status != 0)
565   {
566     ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
567         status, wp->curl_errbuf);
568     return (-1);
569   }
570
571   for (wm = wp->matches; wm != NULL; wm = wm->next)
572   {
573     cu_match_value_t *mv;
574
575     status = match_apply (wm->match, wp->buffer);
576     if (status != 0)
577     {
578       WARNING ("curl plugin: match_apply failed.");
579       continue;
580     }
581
582     mv = match_get_user_data (wm->match);
583     if (mv == NULL)
584     {
585       WARNING ("curl plugin: match_get_user_data returned NULL.");
586       continue;
587     }
588
589     cc_submit (wp, wm, mv);
590   } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
591
592   return (0);
593 } /* }}} int cc_read_page */
594
595 static int cc_read (void) /* {{{ */
596 {
597   web_page_t *wp;
598
599   for (wp = pages_g; wp != NULL; wp = wp->next)
600     cc_read_page (wp);
601
602   return (0);
603 } /* }}} int cc_read */
604
605 static int cc_shutdown (void) /* {{{ */
606 {
607   cc_web_page_free (pages_g);
608   pages_g = NULL;
609
610   return (0);
611 } /* }}} int cc_shutdown */
612
613 void module_register (void)
614 {
615   plugin_register_complex_config ("curl", cc_config);
616   plugin_register_init ("curl", cc_init);
617   plugin_register_read ("curl", cc_read);
618   plugin_register_shutdown ("curl", cc_shutdown);
619 } /* void module_register */
620
621 /* vim: set sw=2 sts=2 et fdm=marker : */