static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */ { int status; DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; " "send_buffer_fill = %zu;", timeout, cb->send_buffer_fill); if (timeout > 0) { time_t now; now = time (NULL); if ((cb->send_buffer_init_time + timeout) > now) return (0); } if (cb->format == WH_FORMAT_COMMAND) { if (cb->send_buffer_fill <= 0) { cb->send_buffer_init_time = time (NULL); return (0); } status = wh_send_buffer (cb); wh_reset_buffer (cb); } else if (cb->format == WH_FORMAT_JSON) { if (cb->send_buffer_fill <= 2) { cb->send_buffer_init_time = time (NULL); return (0); } status = format_json_finalize (cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free); if (status != 0) { ERROR ("write_http: wh_flush_nolock: " "format_json_finalize failed."); wh_reset_buffer (cb); return (status); } status = wh_send_buffer (cb); wh_reset_buffer (cb); } else { ERROR ("write_http: wh_flush_nolock: " "Unknown format: %i", cb->format); return (-1); } return (status); } /* }}} wh_flush_nolock */
static int wh_callback_init (wh_callback_t *cb) /* {{{ */ { struct curl_slist *headers; if (cb->curl != NULL) return (0); cb->curl = curl_easy_init (); if (cb->curl == NULL) { ERROR ("curl plugin: curl_easy_init failed."); return (-1); } curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); headers = NULL; headers = curl_slist_append (headers, "Accept: */*"); if (cb->format == WH_FORMAT_JSON) headers = curl_slist_append (headers, "Content-Type: application/json"); else headers = curl_slist_append (headers, "Content-Type: text/plain"); headers = curl_slist_append (headers, "Expect:"); curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf); curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location); if (cb->user != NULL) { size_t credentials_size; credentials_size = strlen (cb->user) + 2; if (cb->pass != NULL) credentials_size += strlen (cb->pass); cb->credentials = (char *) malloc (credentials_size); if (cb->credentials == NULL) { ERROR ("curl plugin: malloc failed."); return (-1); } ssnprintf (cb->credentials, credentials_size, "%s:%s", cb->user, (cb->pass == NULL) ? "" : cb->pass); curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials); curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); } curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer); curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST, cb->verify_host ? 2L : 0L); if (cb->cacert != NULL) curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert); wh_reset_buffer (cb); return (0); } /* }}} int wh_callback_init */
static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */ { int status; DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; " "send_buffer_fill = %zu;", CDTIME_T_TO_DOUBLE (timeout), cb->send_buffer_fill); /* timeout == 0 => flush unconditionally */ if (timeout > 0) { cdtime_t now; now = cdtime (); if ((cb->send_buffer_init_time + timeout) > now) return (0); } if (cb->format == WH_FORMAT_COMMAND) { if (cb->send_buffer_fill == 0) { cb->send_buffer_init_time = cdtime (); return (0); } status = wh_send_buffer (cb); wh_reset_buffer (cb); } else if (cb->format == WH_FORMAT_JSON) { if (cb->send_buffer_fill <= 2) { cb->send_buffer_init_time = cdtime (); return (0); } status = format_json_finalize (cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free); if (status != 0) { ERROR ("write_http: wh_flush_nolock: " "format_json_finalize failed."); wh_reset_buffer (cb); return (status); } status = wh_send_buffer (cb); wh_reset_buffer (cb); } else { ERROR ("write_http: wh_flush_nolock: " "Unknown format: %i", cb->format); return (-1); } return (status); } /* }}} wh_flush_nolock */
static int wh_callback_init (wh_callback_t *cb) /* {{{ */ { if (cb->curl != NULL) return (0); cb->curl = curl_easy_init (); if (cb->curl == NULL) { ERROR ("curl plugin: curl_easy_init failed."); return (-1); } if (cb->low_speed_limit > 0 && cb->low_speed_time > 0) { curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_LIMIT, (long) (cb->low_speed_limit * cb->low_speed_time)); curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_TIME, (long) cb->low_speed_time); } #ifdef HAVE_CURLOPT_TIMEOUT_MS if (cb->timeout > 0) curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout); #endif curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); cb->headers = curl_slist_append (cb->headers, "Accept: */*"); if (cb->format == WH_FORMAT_JSON) cb->headers = curl_slist_append (cb->headers, "Content-Type: application/json"); else cb->headers = curl_slist_append (cb->headers, "Content-Type: text/plain"); cb->headers = curl_slist_append (cb->headers, "Expect:"); curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, cb->headers); curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf); curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location); curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L); if (cb->user != NULL) { #ifdef HAVE_CURLOPT_USERNAME curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user); curl_easy_setopt (cb->curl, CURLOPT_PASSWORD, (cb->pass == NULL) ? "" : cb->pass); #else size_t credentials_size; credentials_size = strlen (cb->user) + 2; if (cb->pass != NULL) credentials_size += strlen (cb->pass); cb->credentials = malloc (credentials_size); if (cb->credentials == NULL) { ERROR ("curl plugin: malloc failed."); return (-1); } ssnprintf (cb->credentials, credentials_size, "%s:%s", cb->user, (cb->pass == NULL) ? "" : cb->pass); curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials); #endif curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); } curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer); curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST, cb->verify_host ? 2L : 0L); curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion); if (cb->cacert != NULL) curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert); if (cb->capath != NULL) curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath); if (cb->clientkey != NULL && cb->clientcert != NULL) { curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey); curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert); if (cb->clientkeypass != NULL) curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass); } wh_reset_buffer (cb); return (0); } /* }}} int wh_callback_init */