X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fcurl.c;h=7eb480584ebfb7132a58b38005541aa917309110;hp=26ca7e7a0721af3a6c1cf2e5c0cb30910ccfed06;hb=48efd3deb4c9139fd060ff3d289896e9031bcc7c;hpb=45492ba3d0119874e2f2878d68089fc8f62268a4 diff --git a/src/curl.c b/src/curl.c index 26ca7e7a..7eb48058 100644 --- a/src/curl.c +++ b/src/curl.c @@ -23,10 +23,10 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" -#include "utils_curl_stats.h" -#include "utils_match.h" +#include "utils/common/common.h" +#include "utils/curl_stats/curl_stats.h" +#include "utils/match/match.h" #include "utils_time.h" #include @@ -53,20 +53,22 @@ struct web_page_s; typedef struct web_page_s web_page_t; struct web_page_s /* {{{ */ { + char *plugin_name; char *instance; char *url; + int address_family; char *user; char *pass; char *credentials; - _Bool digest; - _Bool verify_peer; - _Bool verify_host; + bool digest; + bool verify_peer; + bool verify_host; char *cacert; struct curl_slist *headers; char *post_body; - _Bool response_time; - _Bool response_code; + bool response_time; + bool response_code; int timeout; curl_stats_t *stats; @@ -77,19 +79,13 @@ struct web_page_s /* {{{ */ size_t buffer_fill; web_match_t *matches; - - web_page_t *next; }; /* }}} */ /* - * Global variables; - */ -/* static CURLM *curl = NULL; */ -static web_page_t *pages_g = NULL; - -/* * Private functions */ +static int cc_read_page(user_data_t *ud); + static size_t cc_curl_callback(void *buf, /* {{{ */ size_t size, size_t nmemb, void *user_data) { web_page_t *wp; @@ -97,11 +93,11 @@ static size_t cc_curl_callback(void *buf, /* {{{ */ len = size * nmemb; if (len == 0) - return (len); + return len; wp = user_data; if (wp == NULL) - return (0); + return 0; if ((wp->buffer_fill + len) >= wp->buffer_size) { char *temp; @@ -111,7 +107,7 @@ static size_t cc_curl_callback(void *buf, /* {{{ */ temp = realloc(wp->buffer, temp_size); if (temp == NULL) { ERROR("curl plugin: realloc failed."); - return (0); + return 0; } wp->buffer = temp; wp->buffer_size = temp_size; @@ -121,7 +117,7 @@ static size_t cc_curl_callback(void *buf, /* {{{ */ wp->buffer_fill += len; wp->buffer[wp->buffer_fill] = 0; - return (len); + return len; } /* }}} size_t cc_curl_callback */ static void cc_web_match_free(web_match_t *wm) /* {{{ */ @@ -137,8 +133,9 @@ static void cc_web_match_free(web_match_t *wm) /* {{{ */ sfree(wm); } /* }}} void cc_web_match_free */ -static void cc_web_page_free(web_page_t *wp) /* {{{ */ +static void cc_web_page_free(void *arg) /* {{{ */ { + web_page_t *wp = (web_page_t *)arg; if (wp == NULL) return; @@ -146,6 +143,7 @@ static void cc_web_page_free(web_page_t *wp) /* {{{ */ curl_easy_cleanup(wp->curl); wp->curl = NULL; + sfree(wp->plugin_name); sfree(wp->instance); sfree(wp->url); @@ -160,7 +158,6 @@ static void cc_web_page_free(web_page_t *wp) /* {{{ */ sfree(wp->buffer); cc_web_match_free(wp->matches); - cc_web_page_free(wp->next); sfree(wp); } /* }}} void cc_web_page_free */ @@ -170,16 +167,16 @@ static int cc_config_append_string(const char *name, struct curl_slist *temp = NULL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING("curl plugin: `%s' needs exactly one string argument.", name); - return (-1); + return -1; } temp = curl_slist_append(*dest, ci->values[0].value.string); if (temp == NULL) - return (-1); + return -1; *dest = temp; - return (0); + return 0; } /* }}} int cc_config_append_string */ static int cc_config_add_match_dstype(int *dstype_ret, /* {{{ */ @@ -188,7 +185,7 @@ static int cc_config_add_match_dstype(int *dstype_ret, /* {{{ */ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING("curl plugin: `DSType' needs exactly one string argument."); - return (-1); + return -1; } if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) { @@ -243,11 +240,11 @@ static int cc_config_add_match_dstype(int *dstype_ret, /* {{{ */ if (dstype == 0) { WARNING("curl plugin: `%s' is not a valid argument to `DSType'.", ci->values[0].value.string); - return (-1); + return -1; } *dstype_ret = dstype; - return (0); + return 0; } /* }}} int cc_config_add_match_dstype */ static int cc_config_add_match(web_page_t *page, /* {{{ */ @@ -262,7 +259,7 @@ static int cc_config_add_match(web_page_t *page, /* {{{ */ match = calloc(1, sizeof(*match)); if (match == NULL) { ERROR("curl plugin: calloc failed."); - return (-1); + return -1; } status = 0; @@ -309,7 +306,7 @@ static int cc_config_add_match(web_page_t *page, /* {{{ */ if (status != 0) { cc_web_match_free(match); - return (status); + return status; } match->match = @@ -317,7 +314,7 @@ static int cc_config_add_match(web_page_t *page, /* {{{ */ if (match->match == NULL) { ERROR("curl plugin: match_create_simple failed."); cc_web_match_free(match); - return (-1); + return -1; } else { web_match_t *prev; @@ -331,7 +328,7 @@ static int cc_config_add_match(web_page_t *page, /* {{{ */ prev->next = match; } - return (0); + return 0; } /* }}} int cc_config_add_match */ static int cc_page_init_curl(web_page_t *wp) /* {{{ */ @@ -339,7 +336,7 @@ static int cc_page_init_curl(web_page_t *wp) /* {{{ */ wp->curl = curl_easy_init(); if (wp->curl == NULL) { ERROR("curl plugin: curl_easy_init failed."); - return (-1); + return -1; } curl_easy_setopt(wp->curl, CURLOPT_NOSIGNAL, 1L); @@ -349,6 +346,7 @@ static int cc_page_init_curl(web_page_t *wp) /* {{{ */ curl_easy_setopt(wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf); curl_easy_setopt(wp->curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(wp->curl, CURLOPT_MAXREDIRS, 50L); + curl_easy_setopt(wp->curl, CURLOPT_IPRESOLVE, wp->address_family); if (wp->user != NULL) { #ifdef HAVE_CURLOPT_USERNAME @@ -365,11 +363,11 @@ static int cc_page_init_curl(web_page_t *wp) /* {{{ */ wp->credentials = malloc(credentials_size); if (wp->credentials == NULL) { ERROR("curl plugin: malloc failed."); - return (-1); + return -1; } - ssnprintf(wp->credentials, credentials_size, "%s:%s", wp->user, - (wp->pass == NULL) ? "" : wp->pass); + snprintf(wp->credentials, credentials_size, "%s:%s", wp->user, + (wp->pass == NULL) ? "" : wp->pass); curl_easy_setopt(wp->curl, CURLOPT_USERPWD, wp->credentials); #endif @@ -394,32 +392,35 @@ static int cc_page_init_curl(web_page_t *wp) /* {{{ */ (long)CDTIME_T_TO_MS(plugin_get_interval())); #endif - return (0); + return 0; } /* }}} int cc_page_init_curl */ static int cc_config_add_page(oconfig_item_t *ci) /* {{{ */ { + cdtime_t interval = 0; web_page_t *page; int status; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING("curl plugin: `Page' blocks need exactly one string argument."); - return (-1); + return -1; } page = calloc(1, sizeof(*page)); if (page == NULL) { ERROR("curl plugin: calloc failed."); - return (-1); + return -1; } + page->plugin_name = NULL; page->url = NULL; + page->address_family = CURL_IPRESOLVE_WHATEVER; page->user = NULL; page->pass = NULL; - page->digest = 0; - page->verify_peer = 1; - page->verify_host = 1; - page->response_time = 0; - page->response_code = 0; + page->digest = false; + page->verify_peer = true; + page->verify_host = true; + page->response_time = false; + page->response_code = false; page->timeout = -1; page->stats = NULL; @@ -427,7 +428,7 @@ static int cc_config_add_page(oconfig_item_t *ci) /* {{{ */ if (page->instance == NULL) { ERROR("curl plugin: strdup failed."); sfree(page); - return (-1); + return -1; } /* Process all children */ @@ -435,9 +436,38 @@ static int cc_config_add_page(oconfig_item_t *ci) /* {{{ */ for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; - if (strcasecmp("URL", child->key) == 0) + if (strcasecmp("Plugin", child->key) == 0) + status = cf_util_get_string(child, &page->plugin_name); + else if (strcasecmp("URL", child->key) == 0) status = cf_util_get_string(child, &page->url); - else if (strcasecmp("User", child->key) == 0) + else if (strcasecmp("AddressFamily", child->key) == 0) { + char *af = NULL; + status = cf_util_get_string(child, &af); + if (status != 0 || af == NULL) { + WARNING("curl plugin: Cannot parse value of `%s' " + "for instance `%s'.", + child->key, page->instance); + } else if (strcasecmp("any", af) == 0) { + page->address_family = CURL_IPRESOLVE_WHATEVER; + } else if (strcasecmp("ipv4", af) == 0) { + page->address_family = CURL_IPRESOLVE_V4; + } else if (strcasecmp("ipv6", af) == 0) { + /* If curl supports ipv6, use it. If not, log a warning and + * fall back to default - don't set status to non-zero. + */ + curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW); + if (curl_info->features & CURL_VERSION_IPV6) + page->address_family = CURL_IPRESOLVE_V6; + else + WARNING("curl plugin: IPv6 not supported by this libCURL. " + "Using fallback `any'."); + } else { + WARNING("curl plugin: Unsupported value of `%s' " + "for instance `%s'.", + child->key, page->instance); + status = -1; + } + } else if (strcasecmp("User", child->key) == 0) status = cf_util_get_string(child, &page->user); else if (strcasecmp("Password", child->key) == 0) status = cf_util_get_string(child, &page->pass); @@ -460,6 +490,8 @@ static int cc_config_add_page(oconfig_item_t *ci) /* {{{ */ status = cc_config_append_string("Header", &page->headers, child); else if (strcasecmp("Post", child->key) == 0) status = cf_util_get_string(child, &page->post_body); + else if (strcasecmp("Interval", child->key) == 0) + status = cf_util_get_cdtime(child, &interval); else if (strcasecmp("Timeout", child->key) == 0) status = cf_util_get_int(child, &page->timeout); else if (strcasecmp("Statistics", child->key) == 0) { @@ -500,22 +532,21 @@ static int cc_config_add_page(oconfig_item_t *ci) /* {{{ */ if (status != 0) { cc_web_page_free(page); - return (status); + return status; } - /* Add the new page to the linked list */ - if (pages_g == NULL) - pages_g = page; - else { - web_page_t *prev; + /* If all went well, register this page for reading */ + char *cb_name = ssnprintf_alloc("curl-%s-%s", page->instance, page->url); - prev = pages_g; - while (prev->next != NULL) - prev = prev->next; - prev->next = page; - } + plugin_register_complex_read(/* group = */ NULL, cb_name, cc_read_page, + interval, + &(user_data_t){ + .data = page, + .free_func = cc_web_page_free, + }); + sfree(cb_name); - return (0); + return 0; } /* }}} int cc_config_add_page */ static int cc_config(oconfig_item_t *ci) /* {{{ */ @@ -544,20 +575,16 @@ static int cc_config(oconfig_item_t *ci) /* {{{ */ if ((success == 0) && (errors > 0)) { ERROR("curl plugin: All statements failed."); - return (-1); + return -1; } - return (0); + return 0; } /* }}} int cc_config */ static int cc_init(void) /* {{{ */ { - if (pages_g == NULL) { - INFO("curl plugin: No pages have been defined."); - return (-1); - } curl_global_init(CURL_GLOBAL_SSL); - return (0); + return 0; } /* }}} int cc_init */ static void cc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */ @@ -566,7 +593,8 @@ static void cc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */ vl.values = &value; vl.values_len = 1; - sstrncpy(vl.plugin, "curl", sizeof(vl.plugin)); + sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl", + sizeof(vl.plugin)); sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance)); sstrncpy(vl.type, wm->type, sizeof(vl.type)); if (wm->instance != NULL) @@ -581,7 +609,8 @@ static void cc_submit_response_code(const web_page_t *wp, long code) /* {{{ */ vl.values = &(value_t){.gauge = (gauge_t)code}; vl.values_len = 1; - sstrncpy(vl.plugin, "curl", sizeof(vl.plugin)); + sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl", + sizeof(vl.plugin)); sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance)); sstrncpy(vl.type, "response_code", sizeof(vl.type)); @@ -594,15 +623,24 @@ static void cc_submit_response_time(const web_page_t *wp, /* {{{ */ vl.values = &(value_t){.gauge = response_time}; vl.values_len = 1; - sstrncpy(vl.plugin, "curl", sizeof(vl.plugin)); + sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl", + sizeof(vl.plugin)); sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance)); sstrncpy(vl.type, "response_time", sizeof(vl.type)); plugin_dispatch_values(&vl); } /* }}} void cc_submit_response_time */ -static int cc_read_page(web_page_t *wp) /* {{{ */ +static int cc_read_page(user_data_t *ud) /* {{{ */ { + + if ((ud == NULL) || (ud->data == NULL)) { + ERROR("curl plugin: cc_read_page: Invalid user data."); + return -1; + } + + web_page_t *wp = (web_page_t *)ud->data; + int status; cdtime_t start = 0; @@ -617,13 +655,13 @@ static int cc_read_page(web_page_t *wp) /* {{{ */ if (status != CURLE_OK) { ERROR("curl plugin: curl_easy_perform failed with status %i: %s", status, wp->curl_errbuf); - return (-1); + return -1; } if (wp->response_time) cc_submit_response_time(wp, CDTIME_T_TO_DOUBLE(cdtime() - start)); if (wp->stats != NULL) - curl_stats_dispatch(wp->stats, wp->curl, hostname_g, "curl", wp->instance); + curl_stats_dispatch(wp->stats, wp->curl, NULL, "curl", wp->instance); if (wp->response_code) { long response_code = 0; @@ -656,30 +694,10 @@ static int cc_read_page(web_page_t *wp) /* {{{ */ match_value_reset(mv); } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */ - return (0); + return 0; } /* }}} int cc_read_page */ -static int cc_read(void) /* {{{ */ -{ - for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next) - cc_read_page(wp); - - return (0); -} /* }}} int cc_read */ - -static int cc_shutdown(void) /* {{{ */ -{ - cc_web_page_free(pages_g); - pages_g = NULL; - - return (0); -} /* }}} int cc_shutdown */ - void module_register(void) { plugin_register_complex_config("curl", cc_config); plugin_register_init("curl", cc_init); - plugin_register_read("curl", cc_read); - plugin_register_shutdown("curl", cc_shutdown); } /* void module_register */ - -/* vim: set sw=2 sts=2 et fdm=marker : */