X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcurl.c;h=899dd15cc5fcf3404de7e72da9a73481a939bc43;hb=96e0f2341bace029acefe0a88bab96ae326c0ff5;hp=bc11c28afdaf149154e1f2bf83fd333769f4c6c1;hpb=9988d61c84dfff5d04ddf48f4b93f6c9449cdc41;p=collectd.git diff --git a/src/curl.c b/src/curl.c index bc11c28a..899dd15c 100644 --- a/src/curl.c +++ b/src/curl.c @@ -37,6 +37,7 @@ typedef struct web_match_s web_match_t; struct web_match_s /* {{{ */ { char *regex; + char *exclude_regex; int dstype; char *type; char *instance; @@ -59,6 +60,8 @@ struct web_page_s /* {{{ */ int verify_peer; int verify_host; char *cacert; + struct curl_slist *headers; + char *post_body; int response_time; CURL *curl; @@ -147,6 +150,8 @@ static void cc_web_page_free (web_page_t *wp) /* {{{ */ sfree (wp->pass); sfree (wp->credentials); sfree (wp->cacert); + sfree (wp->post_body); + curl_slist_free_all (wp->headers); sfree (wp->buffer); @@ -172,6 +177,23 @@ static int cc_config_add_string (const char *name, char **dest, /* {{{ */ return (0); } /* }}} int cc_config_add_string */ +static int cc_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */ + oconfig_item_t *ci) +{ + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) + { + WARNING ("curl plugin: `%s' needs exactly one string argument.", name); + return (-1); + } + + *dest = curl_slist_append(*dest, ci->values[0].value.string); + if (*dest == NULL) + return (-1); + + return (0); +} /* }}} int cc_config_append_string */ + + static int cc_config_set_boolean (const char *name, int *dest, /* {{{ */ oconfig_item_t *ci) { @@ -291,6 +313,8 @@ static int cc_config_add_match (web_page_t *page, /* {{{ */ if (strcasecmp ("Regex", child->key) == 0) status = cc_config_add_string ("Regex", &match->regex, child); + else if (strcasecmp ("ExcludeRegex", child->key) == 0) + status = cc_config_add_string ("ExcludeRegex", &match->exclude_regex, child); else if (strcasecmp ("DSType", child->key) == 0) status = cc_config_add_match_dstype (&match->dstype, child); else if (strcasecmp ("Type", child->key) == 0) @@ -333,7 +357,8 @@ static int cc_config_add_match (web_page_t *page, /* {{{ */ if (status != 0) return (status); - match->match = match_create_simple (match->regex, match->dstype); + match->match = match_create_simple (match->regex, match->exclude_regex, + match->dstype); if (match->match == NULL) { ERROR ("curl plugin: tail_match_add_match_simple failed."); @@ -366,13 +391,15 @@ static int cc_page_init_curl (web_page_t *wp) /* {{{ */ return (-1); } + curl_easy_setopt (wp->curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt (wp->curl, CURLOPT_WRITEFUNCTION, cc_curl_callback); curl_easy_setopt (wp->curl, CURLOPT_WRITEDATA, wp); curl_easy_setopt (wp->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION); curl_easy_setopt (wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf); curl_easy_setopt (wp->curl, CURLOPT_URL, wp->url); - curl_easy_setopt (wp->curl, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt (wp->curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt (wp->curl, CURLOPT_MAXREDIRS, 50L); if (wp->user != NULL) { @@ -394,11 +421,15 @@ static int cc_page_init_curl (web_page_t *wp) /* {{{ */ curl_easy_setopt (wp->curl, CURLOPT_USERPWD, wp->credentials); } - curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYPEER, wp->verify_peer); + curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYPEER, (long) wp->verify_peer); curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYHOST, - wp->verify_host ? 2 : 0); + wp->verify_host ? 2L : 0L); if (wp->cacert != NULL) curl_easy_setopt (wp->curl, CURLOPT_CAINFO, wp->cacert); + if (wp->headers != NULL) + curl_easy_setopt (wp->curl, CURLOPT_HTTPHEADER, wp->headers); + if (wp->post_body != NULL) + curl_easy_setopt (wp->curl, CURLOPT_POSTFIELDS, wp->post_body); return (0); } /* }}} int cc_page_init_curl */ @@ -460,6 +491,10 @@ static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */ else if (strcasecmp ("Match", child->key) == 0) /* Be liberal with failing matches => don't set `status'. */ cc_config_add_match (page, child); + else if (strcasecmp ("Header", child->key) == 0) + status = cc_config_append_string ("Header", &page->headers, child); + else if (strcasecmp ("Post", child->key) == 0) + status = cc_config_add_string ("Post", &page->post_body, child); else { WARNING ("curl plugin: Option `%s' not allowed here.", child->key); @@ -560,6 +595,7 @@ static int cc_init (void) /* {{{ */ INFO ("curl plugin: No pages have been defined."); return (-1); } + curl_global_init (CURL_GLOBAL_SSL); return (0); } /* }}} int cc_init */ @@ -573,12 +609,12 @@ static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */ vl.values = values; vl.values_len = 1; - vl.time = time (NULL); sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "curl", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance)); sstrncpy (vl.type, wm->type, sizeof (vl.type)); - sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance)); + if (wm->instance != NULL) + sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance)); plugin_dispatch_values (&vl); } /* }}} void cc_submit */ @@ -592,7 +628,6 @@ static void cc_submit_response_time (const web_page_t *wp, double seconds) /* {{ vl.values = values; vl.values_len = 1; - vl.time = time (NULL); sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "curl", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance)); @@ -612,7 +647,7 @@ static int cc_read_page (web_page_t *wp) /* {{{ */ wp->buffer_fill = 0; status = curl_easy_perform (wp->curl); - if (status != 0) + if (status != CURLE_OK) { ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s", status, wp->curl_errbuf);