2 * collectd - src/curl.c
3 * Copyright (C) 2006-2009 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Aman Gupta <aman at tmm1.net>
27 #include "configfile.h"
28 #include "utils_match.h"
30 #include <curl/curl.h>
36 typedef struct web_match_s web_match_t;
37 struct web_match_s /* {{{ */
51 typedef struct web_page_s web_page_t;
52 struct web_page_s /* {{{ */
63 struct curl_slist *headers;
69 char curl_errbuf[CURL_ERROR_SIZE];
82 /* static CURLM *curl = NULL; */
83 static web_page_t *pages_g = NULL;
88 static size_t cc_curl_callback (void *buf, /* {{{ */
89 size_t size, size_t nmemb, void *user_data)
102 if ((wp->buffer_fill + len) >= wp->buffer_size)
107 temp_size = wp->buffer_fill + len + 1;
108 temp = (char *) realloc (wp->buffer, temp_size);
111 ERROR ("curl plugin: realloc failed.");
115 wp->buffer_size = temp_size;
118 memcpy (wp->buffer + wp->buffer_fill, (char *) buf, len);
119 wp->buffer_fill += len;
120 wp->buffer[wp->buffer_fill] = 0;
123 } /* }}} size_t cc_curl_callback */
125 static void cc_web_match_free (web_match_t *wm) /* {{{ */
132 sfree (wm->instance);
133 match_destroy (wm->match);
134 cc_web_match_free (wm->next);
136 } /* }}} void cc_web_match_free */
138 static void cc_web_page_free (web_page_t *wp) /* {{{ */
143 if (wp->curl != NULL)
144 curl_easy_cleanup (wp->curl);
147 sfree (wp->instance);
152 sfree (wp->credentials);
154 sfree (wp->post_body);
155 curl_slist_free_all (wp->headers);
159 cc_web_match_free (wp->matches);
160 cc_web_page_free (wp->next);
162 } /* }}} void cc_web_page_free */
164 static int cc_config_add_string (const char *name, char **dest, /* {{{ */
167 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
169 WARNING ("curl plugin: `%s' needs exactly one string argument.", name);
174 *dest = strdup (ci->values[0].value.string);
179 } /* }}} int cc_config_add_string */
181 static int cc_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
184 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
186 WARNING ("curl plugin: `%s' needs exactly one string argument.", name);
190 *dest = curl_slist_append(*dest, ci->values[0].value.string);
195 } /* }}} int cc_config_append_string */
198 static int cc_config_set_boolean (const char *name, int *dest, /* {{{ */
201 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
203 WARNING ("curl plugin: `%s' needs exactly one boolean argument.", name);
207 *dest = ci->values[0].value.boolean ? 1 : 0;
210 } /* }}} int cc_config_set_boolean */
212 static int cc_config_add_match_dstype (int *dstype_ret, /* {{{ */
217 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
219 WARNING ("curl plugin: `DSType' needs exactly one string argument.");
223 if (strncasecmp ("Gauge", ci->values[0].value.string,
224 strlen ("Gauge")) == 0)
226 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
227 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
228 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
229 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
230 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
231 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
232 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
233 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
234 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
238 else if (strncasecmp ("Counter", ci->values[0].value.string,
239 strlen ("Counter")) == 0)
241 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
242 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
243 dstype |= UTILS_MATCH_CF_COUNTER_SET;
244 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
245 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
246 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
247 dstype |= UTILS_MATCH_CF_COUNTER_INC;
251 else if (strncasecmp ("Derive", ci->values[0].value.string,
252 strlen ("Derive")) == 0)
254 dstype = UTILS_MATCH_DS_TYPE_DERIVE;
255 if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
256 dstype |= UTILS_MATCH_CF_DERIVE_SET;
257 else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
258 dstype |= UTILS_MATCH_CF_DERIVE_ADD;
259 else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
260 dstype |= UTILS_MATCH_CF_DERIVE_INC;
264 else if (strncasecmp ("Absolute", ci->values[0].value.string,
265 strlen ("Absolute")) == 0)
267 dstype = UTILS_MATCH_DS_TYPE_ABSOLUTE;
268 if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0) /* Absolute DS is reset-on-read so no sense doin anything else but set */
269 dstype |= UTILS_MATCH_CF_ABSOLUTE_SET;
281 WARNING ("curl plugin: `%s' is not a valid argument to `DSType'.",
282 ci->values[0].value.string);
286 *dstype_ret = dstype;
288 } /* }}} int cc_config_add_match_dstype */
290 static int cc_config_add_match (web_page_t *page, /* {{{ */
297 if (ci->values_num != 0)
299 WARNING ("curl plugin: Ignoring arguments for the `Match' block.");
302 match = (web_match_t *) malloc (sizeof (*match));
305 ERROR ("curl plugin: malloc failed.");
308 memset (match, 0, sizeof (*match));
311 for (i = 0; i < ci->children_num; i++)
313 oconfig_item_t *child = ci->children + i;
315 if (strcasecmp ("Regex", child->key) == 0)
316 status = cc_config_add_string ("Regex", &match->regex, child);
317 else if (strcasecmp ("ExcludeRegex", child->key) == 0)
318 status = cc_config_add_string ("ExcludeRegex", &match->exclude_regex, child);
319 else if (strcasecmp ("DSType", child->key) == 0)
320 status = cc_config_add_match_dstype (&match->dstype, child);
321 else if (strcasecmp ("Type", child->key) == 0)
322 status = cc_config_add_string ("Type", &match->type, child);
323 else if (strcasecmp ("Instance", child->key) == 0)
324 status = cc_config_add_string ("Instance", &match->instance, child);
327 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
333 } /* for (i = 0; i < ci->children_num; i++) */
337 if (match->regex == NULL)
339 WARNING ("curl plugin: `Regex' missing in `Match' block.");
343 if (match->type == NULL)
345 WARNING ("curl plugin: `Type' missing in `Match' block.");
349 if (match->dstype == 0)
351 WARNING ("curl plugin: `DSType' missing in `Match' block.");
356 } /* while (status == 0) */
361 match->match = match_create_simple (match->regex, match->exclude_regex,
363 if (match->match == NULL)
365 ERROR ("curl plugin: tail_match_add_match_simple failed.");
366 cc_web_match_free (match);
373 prev = page->matches;
374 while ((prev != NULL) && (prev->next != NULL))
378 page->matches = match;
384 } /* }}} int cc_config_add_match */
386 static int cc_page_init_curl (web_page_t *wp) /* {{{ */
388 wp->curl = curl_easy_init ();
389 if (wp->curl == NULL)
391 ERROR ("curl plugin: curl_easy_init failed.");
395 curl_easy_setopt (wp->curl, CURLOPT_NOSIGNAL, 1L);
396 curl_easy_setopt (wp->curl, CURLOPT_WRITEFUNCTION, cc_curl_callback);
397 curl_easy_setopt (wp->curl, CURLOPT_WRITEDATA, wp);
398 curl_easy_setopt (wp->curl, CURLOPT_USERAGENT,
399 PACKAGE_NAME"/"PACKAGE_VERSION);
400 curl_easy_setopt (wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
401 curl_easy_setopt (wp->curl, CURLOPT_URL, wp->url);
402 curl_easy_setopt (wp->curl, CURLOPT_FOLLOWLOCATION, 1L);
403 curl_easy_setopt (wp->curl, CURLOPT_MAXREDIRS, 50L);
405 if (wp->user != NULL)
407 size_t credentials_size;
409 credentials_size = strlen (wp->user) + 2;
410 if (wp->pass != NULL)
411 credentials_size += strlen (wp->pass);
413 wp->credentials = (char *) malloc (credentials_size);
414 if (wp->credentials == NULL)
416 ERROR ("curl plugin: malloc failed.");
420 ssnprintf (wp->credentials, credentials_size, "%s:%s",
421 wp->user, (wp->pass == NULL) ? "" : wp->pass);
422 curl_easy_setopt (wp->curl, CURLOPT_USERPWD, wp->credentials);
425 curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYPEER, (long) wp->verify_peer);
426 curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYHOST,
427 wp->verify_host ? 2L : 0L);
428 if (wp->cacert != NULL)
429 curl_easy_setopt (wp->curl, CURLOPT_CAINFO, wp->cacert);
430 if (wp->headers != NULL)
431 curl_easy_setopt (wp->curl, CURLOPT_HTTPHEADER, wp->headers);
432 if (wp->post_body != NULL)
433 curl_easy_setopt (wp->curl, CURLOPT_POSTFIELDS, wp->post_body);
436 } /* }}} int cc_page_init_curl */
438 static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */
444 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
446 WARNING ("curl plugin: `Page' blocks need exactly one string argument.");
450 page = (web_page_t *) malloc (sizeof (*page));
453 ERROR ("curl plugin: malloc failed.");
456 memset (page, 0, sizeof (*page));
460 page->verify_peer = 1;
461 page->verify_host = 1;
462 page->response_time = 0;
463 page->response_code = 0;
465 page->instance = strdup (ci->values[0].value.string);
466 if (page->instance == NULL)
468 ERROR ("curl plugin: strdup failed.");
473 /* Process all children */
475 for (i = 0; i < ci->children_num; i++)
477 oconfig_item_t *child = ci->children + i;
479 if (strcasecmp ("URL", child->key) == 0)
480 status = cc_config_add_string ("URL", &page->url, child);
481 else if (strcasecmp ("User", child->key) == 0)
482 status = cc_config_add_string ("User", &page->user, child);
483 else if (strcasecmp ("Password", child->key) == 0)
484 status = cc_config_add_string ("Password", &page->pass, child);
485 else if (strcasecmp ("VerifyPeer", child->key) == 0)
486 status = cc_config_set_boolean ("VerifyPeer", &page->verify_peer, child);
487 else if (strcasecmp ("VerifyHost", child->key) == 0)
488 status = cc_config_set_boolean ("VerifyHost", &page->verify_host, child);
489 else if (strcasecmp ("MeasureResponseTime", child->key) == 0)
490 status = cc_config_set_boolean (child->key, &page->response_time, child);
491 else if (strcasecmp ("MeasureResponseCode", child->key) == 0)
492 status = cc_config_set_boolean (child->key, &page->response_code, child);
493 else if (strcasecmp ("CACert", child->key) == 0)
494 status = cc_config_add_string ("CACert", &page->cacert, child);
495 else if (strcasecmp ("Match", child->key) == 0)
496 /* Be liberal with failing matches => don't set `status'. */
497 cc_config_add_match (page, child);
498 else if (strcasecmp ("Header", child->key) == 0)
499 status = cc_config_append_string ("Header", &page->headers, child);
500 else if (strcasecmp ("Post", child->key) == 0)
501 status = cc_config_add_string ("Post", &page->post_body, child);
504 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
510 } /* for (i = 0; i < ci->children_num; i++) */
512 /* Additionial sanity checks and libCURL initialization. */
515 if (page->url == NULL)
517 WARNING ("curl plugin: `URL' missing in `Page' block.");
521 if (page->matches == NULL && !page->response_time && !page->response_code)
523 assert (page->instance != NULL);
524 WARNING ("curl plugin: No (valid) `Match' block "
525 "or MeasureResponseTime or MeasureResponseCode within "
526 "`Page' block `%s'.", page->instance);
531 status = cc_page_init_curl (page);
534 } /* while (status == 0) */
538 cc_web_page_free (page);
542 /* Add the new page to the linked list */
550 while ((prev != NULL) && (prev->next != NULL))
556 } /* }}} int cc_config_add_page */
558 static int cc_config (oconfig_item_t *ci) /* {{{ */
568 for (i = 0; i < ci->children_num; i++)
570 oconfig_item_t *child = ci->children + i;
572 if (strcasecmp ("Page", child->key) == 0)
574 status = cc_config_add_page (child);
582 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
587 if ((success == 0) && (errors > 0))
589 ERROR ("curl plugin: All statements failed.");
594 } /* }}} int cc_config */
596 static int cc_init (void) /* {{{ */
600 INFO ("curl plugin: No pages have been defined.");
604 } /* }}} int cc_init */
606 static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
607 const cu_match_value_t *mv)
610 value_list_t vl = VALUE_LIST_INIT;
612 values[0] = mv->value;
616 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
617 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
618 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
619 sstrncpy (vl.type, wm->type, sizeof (vl.type));
620 sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
622 plugin_dispatch_values (&vl);
623 } /* }}} void cc_submit */
625 static void cc_submit_response_code (const web_page_t *wp, long code) /* {{{ */
628 value_list_t vl = VALUE_LIST_INIT;
630 values[0].gauge = code;
634 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
635 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
636 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
637 sstrncpy (vl.type, "response_code", sizeof (vl.type));
639 plugin_dispatch_values (&vl);
640 } /* }}} void cc_submit_response_code */
642 static void cc_submit_response_time (const web_page_t *wp, double seconds) /* {{{ */
645 value_list_t vl = VALUE_LIST_INIT;
647 values[0].gauge = seconds;
651 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
652 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
653 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
654 sstrncpy (vl.type, "response_time", sizeof (vl.type));
656 plugin_dispatch_values (&vl);
657 } /* }}} void cc_submit_response_time */
659 static int cc_read_page (web_page_t *wp) /* {{{ */
663 struct timeval start, end;
665 if (wp->response_time)
666 gettimeofday (&start, NULL);
669 status = curl_easy_perform (wp->curl);
670 if (status != CURLE_OK)
672 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
673 status, wp->curl_errbuf);
677 if(wp->response_code)
679 long response_code = 0;
680 status = curl_easy_getinfo(wp->curl, CURLINFO_RESPONSE_CODE, &response_code);
681 if(status != CURLE_OK) {
682 ERROR ("curl plugin: curl_easy_getinfo failed with staus %i: %s",
683 status, wp->curl_errbuf);
684 return (-1); // TODO: do we need to return in here? this is nonfatal error
686 cc_submit_response_code(wp, response_code);
689 if (wp->response_time)
692 gettimeofday (&end, NULL);
693 secs += end.tv_sec - start.tv_sec;
694 secs += (end.tv_usec - start.tv_usec) / 1000000.0;
695 cc_submit_response_time (wp, secs);
698 for (wm = wp->matches; wm != NULL; wm = wm->next)
700 cu_match_value_t *mv;
702 status = match_apply (wm->match, wp->buffer);
705 WARNING ("curl plugin: match_apply failed.");
709 mv = match_get_user_data (wm->match);
712 WARNING ("curl plugin: match_get_user_data returned NULL.");
716 cc_submit (wp, wm, mv);
717 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
720 } /* }}} int cc_read_page */
722 static int cc_read (void) /* {{{ */
726 for (wp = pages_g; wp != NULL; wp = wp->next)
730 } /* }}} int cc_read */
732 static int cc_shutdown (void) /* {{{ */
734 cc_web_page_free (pages_g);
738 } /* }}} int cc_shutdown */
740 void module_register (void)
742 plugin_register_complex_config ("curl", cc_config);
743 plugin_register_init ("curl", cc_init);
744 plugin_register_read ("curl", cc_read);
745 plugin_register_shutdown ("curl", cc_shutdown);
746 } /* void module_register */
748 /* vim: set sw=2 sts=2 et fdm=marker : */