#include "plugin.h"
#include "configfile.h"
#include "utils_match.h"
+#include "utils_time.h"
#include <curl/curl.h>
struct curl_slist *headers;
char *post_body;
_Bool response_time;
+ _Bool response_code;
CURL *curl;
char curl_errbuf[CURL_ERROR_SIZE];
{
web_page_t *wp;
size_t len;
-
+
len = size * nmemb;
if (len <= 0)
return (len);
} /* 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);
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)
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)
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;
}
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;
{
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)