X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcurl.c;h=8d2893f0781f96118ba204279f6120252bb0600f;hb=c6da31fb30c2fb1d131f92efcde0b3ec9a010b2c;hp=88b5496d4a4b5f317fc3a0ead52cdaf3aec9c5f3;hpb=0a62dc9906182ef285db88d29f413f2a3868200c;p=collectd.git diff --git a/src/curl.c b/src/curl.c index 88b5496d..8d2893f0 100644 --- a/src/curl.c +++ b/src/curl.c @@ -1,6 +1,7 @@ /** * collectd - src/curl.c * Copyright (C) 2006-2009 Florian octo Forster + * Copyright (C) 2009 Aman Gupta * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -17,6 +18,7 @@ * * Authors: * Florian octo Forster + * Aman Gupta **/ #include "collectd.h" @@ -35,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; @@ -57,6 +60,7 @@ struct web_page_s /* {{{ */ int verify_peer; int verify_host; char *cacert; + int response_time; CURL *curl; char curl_errbuf[CURL_ERROR_SIZE]; @@ -222,6 +226,29 @@ static int cc_config_add_match_dstype (int *dstype_ret, /* {{{ */ else dstype = 0; } +else if (strncasecmp ("Derive", ci->values[0].value.string, + strlen ("Derive")) == 0) + { + dstype = UTILS_MATCH_DS_TYPE_DERIVE; + if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0) + dstype |= UTILS_MATCH_CF_DERIVE_SET; + else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0) + dstype |= UTILS_MATCH_CF_DERIVE_ADD; + else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0) + dstype |= UTILS_MATCH_CF_DERIVE_INC; + else + dstype = 0; + } +else if (strncasecmp ("Absolute", ci->values[0].value.string, + strlen ("Absolute")) == 0) + { + dstype = UTILS_MATCH_DS_TYPE_ABSOLUTE; + if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0) /* Absolute DS is reset-on-read so no sense doin anything else but set */ + dstype |= UTILS_MATCH_CF_ABSOLUTE_SET; + else + dstype = 0; + } + else { dstype = 0; @@ -265,6 +292,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) @@ -307,7 +336,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."); @@ -340,12 +370,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, 1L); + curl_easy_setopt (wp->curl, CURLOPT_MAXREDIRS, 50L); if (wp->user != NULL) { @@ -367,9 +400,9 @@ 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); @@ -400,6 +433,7 @@ static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */ page->pass = NULL; page->verify_peer = 1; page->verify_host = 1; + page->response_time = 0; page->instance = strdup (ci->values[0].value.string); if (page->instance == NULL) @@ -425,6 +459,8 @@ static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */ status = cc_config_set_boolean ("VerifyPeer", &page->verify_peer, child); else if (strcasecmp ("VerifyHost", child->key) == 0) status = cc_config_set_boolean ("VerifyHost", &page->verify_host, child); + else if (strcasecmp ("MeasureResponseTime", child->key) == 0) + status = cc_config_set_boolean (child->key, &page->response_time, child); else if (strcasecmp ("CACert", child->key) == 0) status = cc_config_add_string ("CACert", &page->cacert, child); else if (strcasecmp ("Match", child->key) == 0) @@ -449,11 +485,11 @@ static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */ status = -1; } - if (page->matches == NULL) + if (page->matches == NULL && !page->response_time) { assert (page->instance != NULL); WARNING ("curl plugin: No (valid) `Match' block " - "within `Page' block `%s'.", page->instance); + "or MeasureResponseTime within `Page' block `%s'.", page->instance); status = -1; } @@ -530,6 +566,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 */ @@ -553,20 +590,51 @@ 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) /* {{{ */ +{ + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].gauge = 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)); + 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) /* {{{ */ { web_match_t *wm; int status; + struct timeval start, end; + + if (wp->response_time) + gettimeofday (&start, NULL); 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); return (-1); } + if (wp->response_time) + { + 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); + } + for (wm = wp->matches; wm != NULL; wm = wm->next) { cu_match_value_t *mv;