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 collectd.org>
21 * Aman Gupta <aman at tmm1.net>
27 #include "utils/common/common.h"
28 #include "utils/curl_stats/curl_stats.h"
29 #include "utils/match/match.h"
30 #include "utils_time.h"
32 #include <curl/curl.h>
38 typedef struct web_match_s web_match_t;
39 struct web_match_s /* {{{ */
53 typedef struct web_page_s web_page_t;
54 struct web_page_s /* {{{ */
68 struct curl_slist *headers;
76 char curl_errbuf[CURL_ERROR_SIZE];
87 static int cc_read_page(user_data_t *ud);
89 static size_t cc_curl_callback(void *buf, /* {{{ */
90 size_t size, size_t nmemb, void *user_data) {
102 if ((wp->buffer_fill + len) >= wp->buffer_size) {
106 temp_size = wp->buffer_fill + len + 1;
107 temp = realloc(wp->buffer, temp_size);
109 ERROR("curl plugin: realloc failed.");
113 wp->buffer_size = temp_size;
116 memcpy(wp->buffer + wp->buffer_fill, (char *)buf, len);
117 wp->buffer_fill += len;
118 wp->buffer[wp->buffer_fill] = 0;
121 } /* }}} size_t cc_curl_callback */
123 static void cc_web_match_free(web_match_t *wm) /* {{{ */
131 match_destroy(wm->match);
132 cc_web_match_free(wm->next);
134 } /* }}} void cc_web_match_free */
136 static void cc_web_page_free(void *arg) /* {{{ */
138 web_page_t *wp = (web_page_t *)arg;
142 if (wp->curl != NULL)
143 curl_easy_cleanup(wp->curl);
146 sfree(wp->plugin_name);
152 sfree(wp->credentials);
154 sfree(wp->post_body);
155 curl_slist_free_all(wp->headers);
156 curl_stats_destroy(wp->stats);
160 cc_web_match_free(wp->matches);
162 } /* }}} void cc_web_page_free */
164 static int cc_config_append_string(const char *name,
165 struct curl_slist **dest, /* {{{ */
166 oconfig_item_t *ci) {
167 struct curl_slist *temp = NULL;
168 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
169 WARNING("curl plugin: `%s' needs exactly one string argument.", name);
173 temp = curl_slist_append(*dest, ci->values[0].value.string);
180 } /* }}} int cc_config_append_string */
182 static int cc_config_add_match_dstype(int *dstype_ret, /* {{{ */
183 oconfig_item_t *ci) {
186 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
187 WARNING("curl plugin: `DSType' needs exactly one string argument.");
191 if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) {
192 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
193 if (strcasecmp("GaugeAverage", ci->values[0].value.string) == 0)
194 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
195 else if (strcasecmp("GaugeMin", ci->values[0].value.string) == 0)
196 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
197 else if (strcasecmp("GaugeMax", ci->values[0].value.string) == 0)
198 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
199 else if (strcasecmp("GaugeLast", ci->values[0].value.string) == 0)
200 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
203 } else if (strncasecmp("Counter", ci->values[0].value.string,
204 strlen("Counter")) == 0) {
205 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
206 if (strcasecmp("CounterSet", ci->values[0].value.string) == 0)
207 dstype |= UTILS_MATCH_CF_COUNTER_SET;
208 else if (strcasecmp("CounterAdd", ci->values[0].value.string) == 0)
209 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
210 else if (strcasecmp("CounterInc", ci->values[0].value.string) == 0)
211 dstype |= UTILS_MATCH_CF_COUNTER_INC;
214 } else if (strncasecmp("Derive", ci->values[0].value.string,
215 strlen("Derive")) == 0) {
216 dstype = UTILS_MATCH_DS_TYPE_DERIVE;
217 if (strcasecmp("DeriveSet", ci->values[0].value.string) == 0)
218 dstype |= UTILS_MATCH_CF_DERIVE_SET;
219 else if (strcasecmp("DeriveAdd", ci->values[0].value.string) == 0)
220 dstype |= UTILS_MATCH_CF_DERIVE_ADD;
221 else if (strcasecmp("DeriveInc", ci->values[0].value.string) == 0)
222 dstype |= UTILS_MATCH_CF_DERIVE_INC;
225 } else if (strncasecmp("Absolute", ci->values[0].value.string,
226 strlen("Absolute")) == 0) {
227 dstype = UTILS_MATCH_DS_TYPE_ABSOLUTE;
228 if (strcasecmp("AbsoluteSet", ci->values[0].value.string) ==
229 0) /* Absolute DS is reset-on-read so no sense doin anything else but
231 dstype |= UTILS_MATCH_CF_ABSOLUTE_SET;
241 WARNING("curl plugin: `%s' is not a valid argument to `DSType'.",
242 ci->values[0].value.string);
246 *dstype_ret = dstype;
248 } /* }}} int cc_config_add_match_dstype */
250 static int cc_config_add_match(web_page_t *page, /* {{{ */
251 oconfig_item_t *ci) {
255 if (ci->values_num != 0) {
256 WARNING("curl plugin: Ignoring arguments for the `Match' block.");
259 match = calloc(1, sizeof(*match));
261 ERROR("curl plugin: calloc failed.");
266 for (int i = 0; i < ci->children_num; i++) {
267 oconfig_item_t *child = ci->children + i;
269 if (strcasecmp("Regex", child->key) == 0)
270 status = cf_util_get_string(child, &match->regex);
271 else if (strcasecmp("ExcludeRegex", child->key) == 0)
272 status = cf_util_get_string(child, &match->exclude_regex);
273 else if (strcasecmp("DSType", child->key) == 0)
274 status = cc_config_add_match_dstype(&match->dstype, child);
275 else if (strcasecmp("Type", child->key) == 0)
276 status = cf_util_get_string(child, &match->type);
277 else if (strcasecmp("Instance", child->key) == 0)
278 status = cf_util_get_string(child, &match->instance);
280 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
286 } /* for (i = 0; i < ci->children_num; i++) */
288 while (status == 0) {
289 if (match->regex == NULL) {
290 WARNING("curl plugin: `Regex' missing in `Match' block.");
294 if (match->type == NULL) {
295 WARNING("curl plugin: `Type' missing in `Match' block.");
299 if (match->dstype == 0) {
300 WARNING("curl plugin: `DSType' missing in `Match' block.");
305 } /* while (status == 0) */
308 cc_web_match_free(match);
313 match_create_simple(match->regex, match->exclude_regex, match->dstype);
314 if (match->match == NULL) {
315 ERROR("curl plugin: match_create_simple failed.");
316 cc_web_match_free(match);
321 prev = page->matches;
322 while ((prev != NULL) && (prev->next != NULL))
326 page->matches = match;
332 } /* }}} int cc_config_add_match */
334 static int cc_page_init_curl(web_page_t *wp) /* {{{ */
336 wp->curl = curl_easy_init();
337 if (wp->curl == NULL) {
338 ERROR("curl plugin: curl_easy_init failed.");
342 curl_easy_setopt(wp->curl, CURLOPT_NOSIGNAL, 1L);
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, COLLECTD_USERAGENT);
346 curl_easy_setopt(wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
347 curl_easy_setopt(wp->curl, CURLOPT_FOLLOWLOCATION, 1L);
348 curl_easy_setopt(wp->curl, CURLOPT_MAXREDIRS, 50L);
349 curl_easy_setopt(wp->curl, CURLOPT_IPRESOLVE, wp->address_family);
351 if (wp->user != NULL) {
352 #ifdef HAVE_CURLOPT_USERNAME
353 curl_easy_setopt(wp->curl, CURLOPT_USERNAME, wp->user);
354 curl_easy_setopt(wp->curl, CURLOPT_PASSWORD,
355 (wp->pass == NULL) ? "" : wp->pass);
357 size_t credentials_size;
359 credentials_size = strlen(wp->user) + 2;
360 if (wp->pass != NULL)
361 credentials_size += strlen(wp->pass);
363 wp->credentials = malloc(credentials_size);
364 if (wp->credentials == NULL) {
365 ERROR("curl plugin: malloc failed.");
369 snprintf(wp->credentials, credentials_size, "%s:%s", wp->user,
370 (wp->pass == NULL) ? "" : wp->pass);
371 curl_easy_setopt(wp->curl, CURLOPT_USERPWD, wp->credentials);
375 curl_easy_setopt(wp->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
378 curl_easy_setopt(wp->curl, CURLOPT_SSL_VERIFYPEER, (long)wp->verify_peer);
379 curl_easy_setopt(wp->curl, CURLOPT_SSL_VERIFYHOST, wp->verify_host ? 2L : 0L);
380 if (wp->cacert != NULL)
381 curl_easy_setopt(wp->curl, CURLOPT_CAINFO, wp->cacert);
382 if (wp->headers != NULL)
383 curl_easy_setopt(wp->curl, CURLOPT_HTTPHEADER, wp->headers);
384 if (wp->post_body != NULL)
385 curl_easy_setopt(wp->curl, CURLOPT_POSTFIELDS, wp->post_body);
387 #ifdef HAVE_CURLOPT_TIMEOUT_MS
388 if (wp->timeout >= 0)
389 curl_easy_setopt(wp->curl, CURLOPT_TIMEOUT_MS, (long)wp->timeout);
391 curl_easy_setopt(wp->curl, CURLOPT_TIMEOUT_MS,
392 (long)CDTIME_T_TO_MS(plugin_get_interval()));
396 } /* }}} int cc_page_init_curl */
398 static int cc_config_add_page(oconfig_item_t *ci) /* {{{ */
400 cdtime_t interval = 0;
404 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
405 WARNING("curl plugin: `Page' blocks need exactly one string argument.");
409 page = calloc(1, sizeof(*page));
411 ERROR("curl plugin: calloc failed.");
414 page->plugin_name = NULL;
416 page->address_family = CURL_IPRESOLVE_WHATEVER;
419 page->digest = false;
420 page->verify_peer = true;
421 page->verify_host = true;
422 page->response_time = false;
423 page->response_code = false;
427 page->instance = strdup(ci->values[0].value.string);
428 if (page->instance == NULL) {
429 ERROR("curl plugin: strdup failed.");
434 /* Process all children */
436 for (int i = 0; i < ci->children_num; i++) {
437 oconfig_item_t *child = ci->children + i;
439 if (strcasecmp("Plugin", child->key) == 0)
440 status = cf_util_get_string(child, &page->plugin_name);
441 else if (strcasecmp("URL", child->key) == 0)
442 status = cf_util_get_string(child, &page->url);
443 else if (strcasecmp("AddressFamily", child->key) == 0) {
445 status = cf_util_get_string(child, &af);
446 if (status != 0 || af == NULL) {
447 WARNING("curl plugin: Cannot parse value of `%s' "
448 "for instance `%s'.",
449 child->key, page->instance);
450 } else if (strcasecmp("any", af) == 0) {
451 page->address_family = CURL_IPRESOLVE_WHATEVER;
452 } else if (strcasecmp("ipv4", af) == 0) {
453 page->address_family = CURL_IPRESOLVE_V4;
454 } else if (strcasecmp("ipv6", af) == 0) {
455 /* If curl supports ipv6, use it. If not, log a warning and
456 * fall back to default - don't set status to non-zero.
458 curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
459 if (curl_info->features & CURL_VERSION_IPV6)
460 page->address_family = CURL_IPRESOLVE_V6;
462 WARNING("curl plugin: IPv6 not supported by this libCURL. "
463 "Using fallback `any'.");
465 WARNING("curl plugin: Unsupported value of `%s' "
466 "for instance `%s'.",
467 child->key, page->instance);
470 } else if (strcasecmp("User", child->key) == 0)
471 status = cf_util_get_string(child, &page->user);
472 else if (strcasecmp("Password", child->key) == 0)
473 status = cf_util_get_string(child, &page->pass);
474 else if (strcasecmp("Digest", child->key) == 0)
475 status = cf_util_get_boolean(child, &page->digest);
476 else if (strcasecmp("VerifyPeer", child->key) == 0)
477 status = cf_util_get_boolean(child, &page->verify_peer);
478 else if (strcasecmp("VerifyHost", child->key) == 0)
479 status = cf_util_get_boolean(child, &page->verify_host);
480 else if (strcasecmp("MeasureResponseTime", child->key) == 0)
481 status = cf_util_get_boolean(child, &page->response_time);
482 else if (strcasecmp("MeasureResponseCode", child->key) == 0)
483 status = cf_util_get_boolean(child, &page->response_code);
484 else if (strcasecmp("CACert", child->key) == 0)
485 status = cf_util_get_string(child, &page->cacert);
486 else if (strcasecmp("Match", child->key) == 0)
487 /* Be liberal with failing matches => don't set `status'. */
488 cc_config_add_match(page, child);
489 else if (strcasecmp("Header", child->key) == 0)
490 status = cc_config_append_string("Header", &page->headers, child);
491 else if (strcasecmp("Post", child->key) == 0)
492 status = cf_util_get_string(child, &page->post_body);
493 else if (strcasecmp("Interval", child->key) == 0)
494 status = cf_util_get_cdtime(child, &interval);
495 else if (strcasecmp("Timeout", child->key) == 0)
496 status = cf_util_get_int(child, &page->timeout);
497 else if (strcasecmp("Statistics", child->key) == 0) {
498 page->stats = curl_stats_from_config(child);
499 if (page->stats == NULL)
502 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
508 } /* for (i = 0; i < ci->children_num; i++) */
510 /* Additionial sanity checks and libCURL initialization. */
511 while (status == 0) {
512 if (page->url == NULL) {
513 WARNING("curl plugin: `URL' missing in `Page' block.");
517 if (page->matches == NULL && page->stats == NULL && !page->response_time &&
518 !page->response_code) {
519 assert(page->instance != NULL);
520 WARNING("curl plugin: No (valid) `Match' block "
521 "or Statistics or MeasureResponseTime or MeasureResponseCode "
522 "within `Page' block `%s'.",
528 status = cc_page_init_curl(page);
531 } /* while (status == 0) */
534 cc_web_page_free(page);
538 /* If all went well, register this page for reading */
539 char *cb_name = ssnprintf_alloc("curl-%s-%s", page->instance, page->url);
541 plugin_register_complex_read(/* group = */ NULL, cb_name, cc_read_page,
545 .free_func = cc_web_page_free,
550 } /* }}} int cc_config_add_page */
552 static int cc_config(oconfig_item_t *ci) /* {{{ */
561 for (int i = 0; i < ci->children_num; i++) {
562 oconfig_item_t *child = ci->children + i;
564 if (strcasecmp("Page", child->key) == 0) {
565 status = cc_config_add_page(child);
571 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
576 if ((success == 0) && (errors > 0)) {
577 ERROR("curl plugin: All statements failed.");
582 } /* }}} int cc_config */
584 static int cc_init(void) /* {{{ */
586 curl_global_init(CURL_GLOBAL_SSL);
588 } /* }}} int cc_init */
590 static void cc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
592 value_list_t vl = VALUE_LIST_INIT;
596 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
598 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
599 sstrncpy(vl.type, wm->type, sizeof(vl.type));
600 if (wm->instance != NULL)
601 sstrncpy(vl.type_instance, wm->instance, sizeof(vl.type_instance));
603 plugin_dispatch_values(&vl);
604 } /* }}} void cc_submit */
606 static void cc_submit_response_code(const web_page_t *wp, long code) /* {{{ */
608 value_list_t vl = VALUE_LIST_INIT;
610 vl.values = &(value_t){.gauge = (gauge_t)code};
612 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
614 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
615 sstrncpy(vl.type, "response_code", sizeof(vl.type));
617 plugin_dispatch_values(&vl);
618 } /* }}} void cc_submit_response_code */
620 static void cc_submit_response_time(const web_page_t *wp, /* {{{ */
621 gauge_t response_time) {
622 value_list_t vl = VALUE_LIST_INIT;
624 vl.values = &(value_t){.gauge = response_time};
626 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
628 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
629 sstrncpy(vl.type, "response_time", sizeof(vl.type));
631 plugin_dispatch_values(&vl);
632 } /* }}} void cc_submit_response_time */
634 static int cc_read_page(user_data_t *ud) /* {{{ */
637 if ((ud == NULL) || (ud->data == NULL)) {
638 ERROR("curl plugin: cc_read_page: Invalid user data.");
642 web_page_t *wp = (web_page_t *)ud->data;
647 if (wp->response_time)
652 curl_easy_setopt(wp->curl, CURLOPT_URL, wp->url);
654 status = curl_easy_perform(wp->curl);
655 if (status != CURLE_OK) {
656 ERROR("curl plugin: curl_easy_perform failed with status %i: %s", status,
661 if (wp->response_time)
662 cc_submit_response_time(wp, CDTIME_T_TO_DOUBLE(cdtime() - start));
663 if (wp->stats != NULL)
664 curl_stats_dispatch(wp->stats, wp->curl, NULL, "curl", wp->instance);
666 if (wp->response_code) {
667 long response_code = 0;
669 curl_easy_getinfo(wp->curl, CURLINFO_RESPONSE_CODE, &response_code);
670 if (status != CURLE_OK) {
671 ERROR("curl plugin: Fetching response code failed with status %i: %s",
672 status, wp->curl_errbuf);
674 cc_submit_response_code(wp, response_code);
678 for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next) {
679 cu_match_value_t *mv;
681 status = match_apply(wm->match, wp->buffer);
683 WARNING("curl plugin: match_apply failed.");
687 mv = match_get_user_data(wm->match);
689 WARNING("curl plugin: match_get_user_data returned NULL.");
693 cc_submit(wp, wm, mv->value);
694 match_value_reset(mv);
695 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
698 } /* }}} int cc_read_page */
700 void module_register(void) {
701 plugin_register_complex_config("curl", cc_config);
702 plugin_register_init("curl", cc_init);
703 } /* void module_register */