2 * collectd - src/write_http.c
3 * Copyright (C) 2009 Paul Sadauskas
4 * Copyright (C) 2009 Doug MacEachern
5 * Copyright (C) 2007-2014 Florian octo Forster
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at collectd.org>
22 * Doug MacEachern <dougm@hyperic.com>
23 * Paul Sadauskas <psadauskas@gmail.com>
29 #include "utils_cache.h"
30 #include "utils_format_json.h"
36 #include <curl/curl.h>
38 #ifndef WRITE_HTTP_DEFAULT_BUFFER_SIZE
39 # define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096
64 time_t low_speed_time;
67 #define WH_FORMAT_COMMAND 0
68 #define WH_FORMAT_JSON 1
72 struct curl_slist *headers;
73 char curl_errbuf[CURL_ERROR_SIZE];
76 size_t send_buffer_size;
77 size_t send_buffer_free;
78 size_t send_buffer_fill;
79 cdtime_t send_buffer_init_time;
81 pthread_mutex_t send_lock;
83 typedef struct wh_callback_s wh_callback_t;
85 static void wh_log_http_error (wh_callback_t *cb)
87 if (!cb->log_http_error)
92 curl_easy_getinfo (cb->curl, CURLINFO_RESPONSE_CODE, &http_code);
95 INFO ("write_http plugin: HTTP Error code: %lu", http_code);
98 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
100 memset (cb->send_buffer, 0, cb->send_buffer_size);
101 cb->send_buffer_free = cb->send_buffer_size;
102 cb->send_buffer_fill = 0;
103 cb->send_buffer_init_time = cdtime ();
105 if (cb->format == WH_FORMAT_JSON)
107 format_json_initialize (cb->send_buffer,
108 &cb->send_buffer_fill,
109 &cb->send_buffer_free);
111 } /* }}} wh_reset_buffer */
113 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
117 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
118 status = curl_easy_perform (cb->curl);
120 wh_log_http_error (cb);
122 if (status != CURLE_OK)
124 ERROR ("write_http plugin: curl_easy_perform failed with "
126 status, cb->curl_errbuf);
129 } /* }}} wh_send_buffer */
131 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
133 if (cb->curl != NULL)
136 cb->curl = curl_easy_init ();
137 if (cb->curl == NULL)
139 ERROR ("curl plugin: curl_easy_init failed.");
143 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0)
145 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_LIMIT,
146 (long) (cb->low_speed_limit * cb->low_speed_time));
147 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_TIME,
148 (long) cb->low_speed_time);
151 #ifdef HAVE_CURLOPT_TIMEOUT_MS
153 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout);
156 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
157 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
159 cb->headers = curl_slist_append (cb->headers, "Accept: */*");
160 if (cb->format == WH_FORMAT_JSON)
161 cb->headers = curl_slist_append (cb->headers, "Content-Type: application/json");
163 cb->headers = curl_slist_append (cb->headers, "Content-Type: text/plain");
164 cb->headers = curl_slist_append (cb->headers, "Expect:");
165 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, cb->headers);
167 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
168 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
169 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
170 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
172 if (cb->user != NULL)
174 #ifdef HAVE_CURLOPT_USERNAME
175 curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user);
176 curl_easy_setopt (cb->curl, CURLOPT_PASSWORD,
177 (cb->pass == NULL) ? "" : cb->pass);
179 size_t credentials_size;
181 credentials_size = strlen (cb->user) + 2;
182 if (cb->pass != NULL)
183 credentials_size += strlen (cb->pass);
185 cb->credentials = malloc (credentials_size);
186 if (cb->credentials == NULL)
188 ERROR ("curl plugin: malloc failed.");
192 ssnprintf (cb->credentials, credentials_size, "%s:%s",
193 cb->user, (cb->pass == NULL) ? "" : cb->pass);
194 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
196 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
199 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
200 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
201 cb->verify_host ? 2L : 0L);
202 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
203 if (cb->cacert != NULL)
204 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
205 if (cb->capath != NULL)
206 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
208 if (cb->clientkey != NULL && cb->clientcert != NULL)
210 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
211 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
213 if (cb->clientkeypass != NULL)
214 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
217 wh_reset_buffer (cb);
220 } /* }}} int wh_callback_init */
222 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
226 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
227 "send_buffer_fill = %zu;",
228 CDTIME_T_TO_DOUBLE (timeout),
229 cb->send_buffer_fill);
231 /* timeout == 0 => flush unconditionally */
237 if ((cb->send_buffer_init_time + timeout) > now)
241 if (cb->format == WH_FORMAT_COMMAND)
243 if (cb->send_buffer_fill == 0)
245 cb->send_buffer_init_time = cdtime ();
249 status = wh_send_buffer (cb);
250 wh_reset_buffer (cb);
252 else if (cb->format == WH_FORMAT_JSON)
254 if (cb->send_buffer_fill <= 2)
256 cb->send_buffer_init_time = cdtime ();
260 status = format_json_finalize (cb->send_buffer,
261 &cb->send_buffer_fill,
262 &cb->send_buffer_free);
265 ERROR ("write_http: wh_flush_nolock: "
266 "format_json_finalize failed.");
267 wh_reset_buffer (cb);
271 status = wh_send_buffer (cb);
272 wh_reset_buffer (cb);
276 ERROR ("write_http: wh_flush_nolock: "
277 "Unknown format: %i",
283 } /* }}} wh_flush_nolock */
285 static int wh_flush (cdtime_t timeout, /* {{{ */
286 const char *identifier __attribute__((unused)),
287 user_data_t *user_data)
292 if (user_data == NULL)
295 cb = user_data->data;
297 pthread_mutex_lock (&cb->send_lock);
299 if (cb->curl == NULL)
301 status = wh_callback_init (cb);
304 ERROR ("write_http plugin: wh_callback_init failed.");
305 pthread_mutex_unlock (&cb->send_lock);
310 status = wh_flush_nolock (timeout, cb);
311 pthread_mutex_unlock (&cb->send_lock);
314 } /* }}} int wh_flush */
316 static void wh_callback_free (void *data) /* {{{ */
325 wh_flush_nolock (/* timeout = */ 0, cb);
327 if (cb->curl != NULL)
329 curl_easy_cleanup (cb->curl);
333 if (cb->headers != NULL)
335 curl_slist_free_all (cb->headers);
340 sfree (cb->location);
343 sfree (cb->credentials);
346 sfree (cb->clientkey);
347 sfree (cb->clientcert);
348 sfree (cb->clientkeypass);
349 sfree (cb->send_buffer);
352 } /* }}} void wh_callback_free */
354 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
357 char key[10*DATA_MAX_NAME_LEN];
364 if (0 != strcmp (ds->type, vl->type)) {
365 ERROR ("write_http plugin: DS type does not match "
370 /* Copy the identifier to `key' and escape it. */
371 status = FORMAT_VL (key, sizeof (key), vl);
373 ERROR ("write_http plugin: error with format_name");
376 escape_string (key, sizeof (key));
378 /* Convert the values to an ASCII representation and put that into
380 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
382 ERROR ("write_http plugin: error with "
383 "wh_value_list_to_string");
387 command_len = (size_t) ssnprintf (command, sizeof (command),
388 "PUTVAL %s interval=%.3f %s\r\n",
390 CDTIME_T_TO_DOUBLE (vl->interval),
392 if (command_len >= sizeof (command)) {
393 ERROR ("write_http plugin: Command buffer too small: "
394 "Need %zu bytes.", command_len + 1);
398 pthread_mutex_lock (&cb->send_lock);
400 if (cb->curl == NULL)
402 status = wh_callback_init (cb);
405 ERROR ("write_http plugin: wh_callback_init failed.");
406 pthread_mutex_unlock (&cb->send_lock);
411 if (command_len >= cb->send_buffer_free)
413 status = wh_flush_nolock (/* timeout = */ 0, cb);
416 pthread_mutex_unlock (&cb->send_lock);
420 assert (command_len < cb->send_buffer_free);
422 /* `command_len + 1' because `command_len' does not include the
423 * trailing null byte. Neither does `send_buffer_fill'. */
424 memcpy (cb->send_buffer + cb->send_buffer_fill,
425 command, command_len + 1);
426 cb->send_buffer_fill += command_len;
427 cb->send_buffer_free -= command_len;
429 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
431 cb->send_buffer_fill, cb->send_buffer_size,
432 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
435 /* Check if we have enough space for this command. */
436 pthread_mutex_unlock (&cb->send_lock);
439 } /* }}} int wh_write_command */
441 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
446 pthread_mutex_lock (&cb->send_lock);
448 if (cb->curl == NULL)
450 status = wh_callback_init (cb);
453 ERROR ("write_http plugin: wh_callback_init failed.");
454 pthread_mutex_unlock (&cb->send_lock);
459 status = format_json_value_list (cb->send_buffer,
460 &cb->send_buffer_fill,
461 &cb->send_buffer_free,
462 ds, vl, cb->store_rates);
463 if (status == (-ENOMEM))
465 status = wh_flush_nolock (/* timeout = */ 0, cb);
468 wh_reset_buffer (cb);
469 pthread_mutex_unlock (&cb->send_lock);
473 status = format_json_value_list (cb->send_buffer,
474 &cb->send_buffer_fill,
475 &cb->send_buffer_free,
476 ds, vl, cb->store_rates);
480 pthread_mutex_unlock (&cb->send_lock);
484 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
486 cb->send_buffer_fill, cb->send_buffer_size,
487 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
489 /* Check if we have enough space for this command. */
490 pthread_mutex_unlock (&cb->send_lock);
493 } /* }}} int wh_write_json */
495 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
496 user_data_t *user_data)
501 if (user_data == NULL)
504 cb = user_data->data;
506 if (cb->format == WH_FORMAT_JSON)
507 status = wh_write_json (ds, vl, cb);
509 status = wh_write_command (ds, vl, cb);
512 } /* }}} int wh_write */
514 static int config_set_format (wh_callback_t *cb, /* {{{ */
519 if ((ci->values_num != 1)
520 || (ci->values[0].type != OCONFIG_TYPE_STRING))
522 WARNING ("write_http plugin: The `%s' config option "
523 "needs exactly one string argument.", ci->key);
527 string = ci->values[0].value.string;
528 if (strcasecmp ("Command", string) == 0)
529 cb->format = WH_FORMAT_COMMAND;
530 else if (strcasecmp ("JSON", string) == 0)
531 cb->format = WH_FORMAT_JSON;
534 ERROR ("write_http plugin: Invalid format string: %s",
540 } /* }}} int config_set_format */
542 static int wh_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
545 struct curl_slist *temp = NULL;
546 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
548 WARNING ("write_http plugin: `%s' needs exactly one string argument.", name);
552 temp = curl_slist_append(*dest, ci->values[0].value.string);
559 } /* }}} int wh_config_append_string */
561 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
565 user_data_t user_data;
566 char callback_name[DATA_MAX_NAME_LEN];
570 cb = calloc (1, sizeof (*cb));
573 ERROR ("write_http plugin: calloc failed.");
578 cb->format = WH_FORMAT_COMMAND;
579 cb->sslversion = CURL_SSLVERSION_DEFAULT;
580 cb->low_speed_limit = 0;
582 cb->log_http_error = 0;
586 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
588 cf_util_get_string (ci, &cb->name);
590 /* FIXME: Remove this legacy mode in version 6. */
591 if (strcasecmp ("URL", ci->key) == 0)
592 cf_util_get_string (ci, &cb->location);
594 for (i = 0; i < ci->children_num; i++)
596 oconfig_item_t *child = ci->children + i;
598 if (strcasecmp ("URL", child->key) == 0)
599 status = cf_util_get_string (child, &cb->location);
600 else if (strcasecmp ("User", child->key) == 0)
601 status = cf_util_get_string (child, &cb->user);
602 else if (strcasecmp ("Password", child->key) == 0)
603 status = cf_util_get_string (child, &cb->pass);
604 else if (strcasecmp ("VerifyPeer", child->key) == 0)
605 status = cf_util_get_boolean (child, &cb->verify_peer);
606 else if (strcasecmp ("VerifyHost", child->key) == 0)
607 status = cf_util_get_boolean (child, &cb->verify_host);
608 else if (strcasecmp ("CACert", child->key) == 0)
609 status = cf_util_get_string (child, &cb->cacert);
610 else if (strcasecmp ("CAPath", child->key) == 0)
611 status = cf_util_get_string (child, &cb->capath);
612 else if (strcasecmp ("ClientKey", child->key) == 0)
613 status = cf_util_get_string (child, &cb->clientkey);
614 else if (strcasecmp ("ClientCert", child->key) == 0)
615 status = cf_util_get_string (child, &cb->clientcert);
616 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
617 status = cf_util_get_string (child, &cb->clientkeypass);
618 else if (strcasecmp ("SSLVersion", child->key) == 0)
622 status = cf_util_get_string (child, &value);
626 if (value == NULL || strcasecmp ("default", value) == 0)
627 cb->sslversion = CURL_SSLVERSION_DEFAULT;
628 else if (strcasecmp ("SSLv2", value) == 0)
629 cb->sslversion = CURL_SSLVERSION_SSLv2;
630 else if (strcasecmp ("SSLv3", value) == 0)
631 cb->sslversion = CURL_SSLVERSION_SSLv3;
632 else if (strcasecmp ("TLSv1", value) == 0)
633 cb->sslversion = CURL_SSLVERSION_TLSv1;
634 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
635 else if (strcasecmp ("TLSv1_0", value) == 0)
636 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
637 else if (strcasecmp ("TLSv1_1", value) == 0)
638 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
639 else if (strcasecmp ("TLSv1_2", value) == 0)
640 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
644 ERROR ("write_http plugin: Invalid SSLVersion "
645 "option: %s.", value);
651 else if (strcasecmp ("Format", child->key) == 0)
652 status = config_set_format (cb, child);
653 else if (strcasecmp ("StoreRates", child->key) == 0)
654 status = cf_util_get_boolean (child, &cb->store_rates);
655 else if (strcasecmp ("BufferSize", child->key) == 0)
656 status = cf_util_get_int (child, &buffer_size);
657 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
658 status = cf_util_get_int (child, &cb->low_speed_limit);
659 else if (strcasecmp ("Timeout", child->key) == 0)
660 status = cf_util_get_int (child, &cb->timeout);
661 else if (strcasecmp ("LogHttpError", child->key) == 0)
662 status = cf_util_get_boolean (child, &cb->log_http_error);
663 else if (strcasecmp ("Header", child->key) == 0)
664 status = wh_config_append_string ("Header", &cb->headers, child);
667 ERROR ("write_http plugin: Invalid configuration "
668 "option: %s.", child->key);
678 wh_callback_free (cb);
682 if (cb->location == NULL)
684 ERROR ("write_http plugin: no URL defined for instance '%s'",
686 wh_callback_free (cb);
690 if (cb->low_speed_limit > 0)
691 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
693 /* Determine send_buffer_size. */
694 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
695 if (buffer_size >= 1024)
696 cb->send_buffer_size = (size_t) buffer_size;
697 else if (buffer_size != 0)
698 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
701 /* Allocate the buffer. */
702 cb->send_buffer = malloc (cb->send_buffer_size);
703 if (cb->send_buffer == NULL)
705 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
706 wh_callback_free (cb);
709 /* Nulls the buffer and sets ..._free and ..._fill. */
710 wh_reset_buffer (cb);
712 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
714 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
715 callback_name, cb->location);
717 memset (&user_data, 0, sizeof (user_data));
719 user_data.free_func = NULL;
720 plugin_register_flush (callback_name, wh_flush, &user_data);
722 user_data.free_func = wh_callback_free;
723 plugin_register_write (callback_name, wh_write, &user_data);
726 } /* }}} int wh_config_node */
728 static int wh_config (oconfig_item_t *ci) /* {{{ */
732 for (i = 0; i < ci->children_num; i++)
734 oconfig_item_t *child = ci->children + i;
736 if (strcasecmp ("Node", child->key) == 0)
737 wh_config_node (child);
738 /* FIXME: Remove this legacy mode in version 6. */
739 else if (strcasecmp ("URL", child->key) == 0) {
740 WARNING ("write_http plugin: Legacy <URL> block found. "
741 "Please use <Node> instead.");
742 wh_config_node (child);
746 ERROR ("write_http plugin: Invalid configuration "
747 "option: %s.", child->key);
752 } /* }}} int wh_config */
754 static int wh_init (void) /* {{{ */
756 /* Call this while collectd is still single-threaded to avoid
757 * initialization issues in libgcrypt. */
758 curl_global_init (CURL_GLOBAL_SSL);
760 } /* }}} int wh_init */
762 void module_register (void) /* {{{ */
764 plugin_register_complex_config ("write_http", wh_config);
765 plugin_register_init ("write_http", wh_init);
766 } /* }}} void module_register */
768 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */