X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcurl.c;h=9d2196ac2c3e57d30858eebcbf83bdb39f4d6970;hb=f8379dd45f4a43595f4027992696ee8d02908bff;hp=e2c4a132514f797b6abb195729719ebe5ddafc8a;hpb=981a488d5cfbdc80a40d82d160f27e69c66a62d0;p=collectd.git diff --git a/src/curl.c b/src/curl.c index e2c4a132..9d2196ac 100644 --- a/src/curl.c +++ b/src/curl.c @@ -26,6 +26,7 @@ #include "plugin.h" #include "configfile.h" #include "utils_match.h" +#include "utils_time.h" #include @@ -63,6 +64,7 @@ struct web_page_s /* {{{ */ struct curl_slist *headers; char *post_body; _Bool response_time; + _Bool response_code; CURL *curl; char curl_errbuf[CURL_ERROR_SIZE]; @@ -89,7 +91,7 @@ static size_t cc_curl_callback (void *buf, /* {{{ */ { web_page_t *wp; size_t len; - + len = size * nmemb; if (len <= 0) return (len); @@ -323,7 +325,10 @@ static int cc_config_add_match (web_page_t *page, /* {{{ */ } /* while (status == 0) */ if (status != 0) + { + cc_web_match_free (match); return (status); + } match->match = match_create_simple (match->regex, match->exclude_regex, match->dstype); @@ -427,6 +432,7 @@ static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */ page->verify_peer = 1; page->verify_host = 1; page->response_time = 0; + page->response_code = 0; page->instance = strdup (ci->values[0].value.string); if (page->instance == NULL) @@ -454,6 +460,8 @@ static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */ status = cf_util_get_boolean (child, &page->verify_host); else if (strcasecmp ("MeasureResponseTime", child->key) == 0) status = cf_util_get_boolean (child, &page->response_time); + else if (strcasecmp ("MeasureResponseCode", child->key) == 0) + status = cf_util_get_boolean (child, &page->response_code); else if (strcasecmp ("CACert", child->key) == 0) status = cf_util_get_string (child, &page->cacert); else if (strcasecmp ("Match", child->key) == 0) @@ -482,11 +490,12 @@ static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */ status = -1; } - if (page->matches == NULL && !page->response_time) + if (page->matches == NULL && !page->response_time && !page->response_code) { assert (page->instance != NULL); WARNING ("curl plugin: No (valid) `Match' block " - "or MeasureResponseTime within `Page' block `%s'.", page->instance); + "or MeasureResponseTime or MeasureResponseCode within " + "`Page' block `%s'.", page->instance); status = -1; } @@ -587,12 +596,30 @@ static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */ plugin_dispatch_values (&vl); } /* }}} void cc_submit */ -static void cc_submit_response_time (const web_page_t *wp, double seconds) /* {{{ */ +static void cc_submit_response_code (const web_page_t *wp, long code) /* {{{ */ +{ + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].gauge = code; + + vl.values = values; + vl.values_len = 1; + 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, "response_code", sizeof (vl.type)); + + plugin_dispatch_values (&vl); +} /* }}} void cc_submit_response_code */ + +static void cc_submit_response_time (const web_page_t *wp, /* {{{ */ + cdtime_t response_time) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - values[0].gauge = seconds; + values[0].gauge = CDTIME_T_TO_DOUBLE (response_time); vl.values = values; vl.values_len = 1; @@ -608,27 +635,33 @@ static int cc_read_page (web_page_t *wp) /* {{{ */ { web_match_t *wm; int status; - struct timeval start, end; + cdtime_t start = 0; if (wp->response_time) - gettimeofday (&start, NULL); + start = cdtime (); wp->buffer_fill = 0; status = curl_easy_perform (wp->curl); if (status != CURLE_OK) { - ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s", + ERROR ("curl plugin: curl_easy_perform failed with status %i: %s", status, wp->curl_errbuf); return (-1); } if (wp->response_time) + cc_submit_response_time (wp, cdtime() - start); + + if(wp->response_code) { - double secs = 0; - gettimeofday (&end, NULL); - secs += end.tv_sec - start.tv_sec; - secs += (end.tv_usec - start.tv_usec) / 1000000.0; - cc_submit_response_time (wp, secs); + long response_code = 0; + status = curl_easy_getinfo(wp->curl, CURLINFO_RESPONSE_CODE, &response_code); + if(status != CURLE_OK) { + ERROR ("curl plugin: Fetching response code failed with status %i: %s", + status, wp->curl_errbuf); + } else { + cc_submit_response_code(wp, response_code); + } } for (wm = wp->matches; wm != NULL; wm = wm->next) @@ -650,6 +683,7 @@ static int cc_read_page (web_page_t *wp) /* {{{ */ } cc_submit (wp, wm, mv); + match_value_reset (mv); } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */ return (0);