X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcurl_json.c;h=5aceae4e59e45ee55e3400340660c51b4dc4f78f;hb=2d51f3eaee79b36f9ec51381075b1f486b7921b8;hp=b79c999e2d8bf51116cf1e618a7229843c364f6d;hpb=dbeee0cfad69e701ea2665001fd9f2bce49c9489;p=collectd.git diff --git a/src/curl_json.c b/src/curl_json.c index b79c999e..5aceae4e 100644 --- a/src/curl_json.c +++ b/src/curl_json.c @@ -78,6 +78,7 @@ struct cj_s /* {{{ */ struct curl_slist *headers; char *post_body; cdtime_t interval; + int timeout; CURL *curl; char curl_errbuf[CURL_ERROR_SIZE]; @@ -502,6 +503,7 @@ static int cj_config_add_key (cj_t *db, /* {{{ */ { ERROR ("curl_json plugin: cj_config: " "Invalid key: %s", ci->key); + cj_key_free (key); return (-1); } @@ -551,7 +553,6 @@ static int cj_config_add_key (cj_t *db, /* {{{ */ db->tree = cj_avl_create(); tree = db->tree; - name = key->path; ptr = key->path; if (*ptr == '/') ++ptr; @@ -562,7 +563,7 @@ static int cj_config_add_key (cj_t *db, /* {{{ */ if (*ptr == '/') { c_avl_tree_t *value; - int len; + size_t len; len = ptr-name; if (len == 0) @@ -608,6 +609,8 @@ static int cj_init_curl (cj_t *db) /* {{{ */ curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf); curl_easy_setopt (db->curl, CURLOPT_URL, db->url); + curl_easy_setopt (db->curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt (db->curl, CURLOPT_MAXREDIRS, 50L); if (db->user != NULL) { @@ -648,6 +651,17 @@ static int cj_init_curl (cj_t *db) /* {{{ */ if (db->post_body != NULL) curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body); +#ifdef HAVE_CURLOPT_TIMEOUT_MS + if (db->timeout >= 0) + curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout); + else if (db->interval > 0) + curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, + CDTIME_T_TO_MS(db->timeout)); + else + curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, + CDTIME_T_TO_MS(plugin_get_interval())); +#endif + return (0); } /* }}} int cj_init_curl */ @@ -673,6 +687,8 @@ static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */ } memset (db, 0, sizeof (*db)); + db->timeout = -1; + if (strcasecmp ("URL", ci->key) == 0) status = cf_util_get_string (ci, &db->url); else if (strcasecmp ("Sock", ci->key) == 0) @@ -681,6 +697,7 @@ static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */ { ERROR ("curl_json plugin: cj_config: " "Invalid key: %s", ci->key); + cj_free (db); return (-1); } if (status != 0) @@ -718,6 +735,8 @@ static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */ status = cj_config_add_key (db, child); else if (strcasecmp ("Interval", child->key) == 0) status = cf_util_get_cdtime(child, &db->interval); + else if (strcasecmp ("Timeout", child->key) == 0) + status = cf_util_get_int (child, &db->timeout); else { WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key); @@ -745,9 +764,6 @@ static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */ { user_data_t ud; char *cb_name; - struct timespec interval = { 0, 0 }; - - CDTIME_T_TO_TIMESPEC (db->interval, &interval); if (db->instance == NULL) db->instance = strdup("default"); @@ -763,7 +779,7 @@ static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */ db->instance, db->url ? db->url : db->sock); plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read, - /* interval = */ (db->interval > 0) ? &interval : NULL, + /* interval = */ db->interval, &ud); sfree (cb_name); }