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_format_json.h"
30 #include "utils_format_kairosdb.h"
32 #include <curl/curl.h>
34 #ifndef WRITE_HTTP_DEFAULT_BUFFER_SIZE
35 # define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096
60 time_t low_speed_time;
63 #define WH_FORMAT_COMMAND 0
64 #define WH_FORMAT_JSON 1
65 #define WH_FORMAT_KAIROSDB 2
69 struct curl_slist *headers;
70 char curl_errbuf[CURL_ERROR_SIZE];
73 size_t send_buffer_size;
74 size_t send_buffer_free;
75 size_t send_buffer_fill;
76 cdtime_t send_buffer_init_time;
78 pthread_mutex_t send_lock;
80 typedef struct wh_callback_s wh_callback_t;
82 static void wh_log_http_error (wh_callback_t *cb)
84 if (!cb->log_http_error)
89 curl_easy_getinfo (cb->curl, CURLINFO_RESPONSE_CODE, &http_code);
92 INFO ("write_http plugin: HTTP Error code: %lu", http_code);
95 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
97 memset (cb->send_buffer, 0, cb->send_buffer_size);
98 cb->send_buffer_free = cb->send_buffer_size;
99 cb->send_buffer_fill = 0;
100 cb->send_buffer_init_time = cdtime ();
102 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
104 format_json_initialize (cb->send_buffer,
105 &cb->send_buffer_fill,
106 &cb->send_buffer_free);
108 } /* }}} wh_reset_buffer */
110 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
114 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
115 status = curl_easy_perform (cb->curl);
117 wh_log_http_error (cb);
119 if (status != CURLE_OK)
121 ERROR ("write_http plugin: curl_easy_perform failed with "
123 status, cb->curl_errbuf);
126 } /* }}} wh_send_buffer */
128 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
130 if (cb->curl != NULL)
133 cb->curl = curl_easy_init ();
134 if (cb->curl == NULL)
136 ERROR ("curl plugin: curl_easy_init failed.");
140 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0)
142 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_LIMIT,
143 (long) (cb->low_speed_limit * cb->low_speed_time));
144 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_TIME,
145 (long) cb->low_speed_time);
148 #ifdef HAVE_CURLOPT_TIMEOUT_MS
150 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout);
153 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
154 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
156 cb->headers = curl_slist_append (cb->headers, "Accept: */*");
157 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
158 cb->headers = curl_slist_append (cb->headers, "Content-Type: application/json");
160 cb->headers = curl_slist_append (cb->headers, "Content-Type: text/plain");
161 cb->headers = curl_slist_append (cb->headers, "Expect:");
162 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, cb->headers);
164 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
165 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
166 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
167 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
169 if (cb->user != NULL)
171 #ifdef HAVE_CURLOPT_USERNAME
172 curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user);
173 curl_easy_setopt (cb->curl, CURLOPT_PASSWORD,
174 (cb->pass == NULL) ? "" : cb->pass);
176 size_t credentials_size;
178 credentials_size = strlen (cb->user) + 2;
179 if (cb->pass != NULL)
180 credentials_size += strlen (cb->pass);
182 cb->credentials = malloc (credentials_size);
183 if (cb->credentials == NULL)
185 ERROR ("curl plugin: malloc failed.");
189 ssnprintf (cb->credentials, credentials_size, "%s:%s",
190 cb->user, (cb->pass == NULL) ? "" : cb->pass);
191 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
193 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
196 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
197 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
198 cb->verify_host ? 2L : 0L);
199 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
200 if (cb->cacert != NULL)
201 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
202 if (cb->capath != NULL)
203 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
205 if (cb->clientkey != NULL && cb->clientcert != NULL)
207 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
208 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
210 if (cb->clientkeypass != NULL)
211 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
214 wh_reset_buffer (cb);
217 } /* }}} int wh_callback_init */
219 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
223 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
224 "send_buffer_fill = %zu;",
225 CDTIME_T_TO_DOUBLE (timeout),
226 cb->send_buffer_fill);
228 /* timeout == 0 => flush unconditionally */
234 if ((cb->send_buffer_init_time + timeout) > now)
238 if (cb->format == WH_FORMAT_COMMAND)
240 if (cb->send_buffer_fill == 0)
242 cb->send_buffer_init_time = cdtime ();
246 status = wh_send_buffer (cb);
247 wh_reset_buffer (cb);
249 else if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
251 if (cb->send_buffer_fill <= 2)
253 cb->send_buffer_init_time = cdtime ();
257 status = format_json_finalize (cb->send_buffer,
258 &cb->send_buffer_fill,
259 &cb->send_buffer_free);
262 ERROR ("write_http: wh_flush_nolock: "
263 "format_json_finalize failed.");
264 wh_reset_buffer (cb);
268 status = wh_send_buffer (cb);
269 wh_reset_buffer (cb);
273 ERROR ("write_http: wh_flush_nolock: "
274 "Unknown format: %i",
280 } /* }}} wh_flush_nolock */
282 static int wh_flush (cdtime_t timeout, /* {{{ */
283 const char *identifier __attribute__((unused)),
284 user_data_t *user_data)
289 if (user_data == NULL)
292 cb = user_data->data;
294 pthread_mutex_lock (&cb->send_lock);
296 if (cb->curl == NULL)
298 status = wh_callback_init (cb);
301 ERROR ("write_http plugin: wh_callback_init failed.");
302 pthread_mutex_unlock (&cb->send_lock);
307 status = wh_flush_nolock (timeout, cb);
308 pthread_mutex_unlock (&cb->send_lock);
311 } /* }}} int wh_flush */
313 static void wh_callback_free (void *data) /* {{{ */
322 wh_flush_nolock (/* timeout = */ 0, cb);
324 if (cb->curl != NULL)
326 curl_easy_cleanup (cb->curl);
330 if (cb->headers != NULL)
332 curl_slist_free_all (cb->headers);
337 sfree (cb->location);
340 sfree (cb->credentials);
343 sfree (cb->clientkey);
344 sfree (cb->clientcert);
345 sfree (cb->clientkeypass);
346 sfree (cb->send_buffer);
349 } /* }}} void wh_callback_free */
351 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
354 char key[10*DATA_MAX_NAME_LEN];
361 if (0 != strcmp (ds->type, vl->type)) {
362 ERROR ("write_http plugin: DS type does not match "
367 /* Copy the identifier to `key' and escape it. */
368 status = FORMAT_VL (key, sizeof (key), vl);
370 ERROR ("write_http plugin: error with format_name");
373 escape_string (key, sizeof (key));
375 /* Convert the values to an ASCII representation and put that into
377 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
379 ERROR ("write_http plugin: error with "
380 "wh_value_list_to_string");
384 command_len = (size_t) ssnprintf (command, sizeof (command),
385 "PUTVAL %s interval=%.3f %s\r\n",
387 CDTIME_T_TO_DOUBLE (vl->interval),
389 if (command_len >= sizeof (command)) {
390 ERROR ("write_http plugin: Command buffer too small: "
391 "Need %zu bytes.", command_len + 1);
395 pthread_mutex_lock (&cb->send_lock);
397 if (cb->curl == NULL)
399 status = wh_callback_init (cb);
402 ERROR ("write_http plugin: wh_callback_init failed.");
403 pthread_mutex_unlock (&cb->send_lock);
408 if (command_len >= cb->send_buffer_free)
410 status = wh_flush_nolock (/* timeout = */ 0, cb);
413 pthread_mutex_unlock (&cb->send_lock);
417 assert (command_len < cb->send_buffer_free);
419 /* `command_len + 1' because `command_len' does not include the
420 * trailing null byte. Neither does `send_buffer_fill'. */
421 memcpy (cb->send_buffer + cb->send_buffer_fill,
422 command, command_len + 1);
423 cb->send_buffer_fill += command_len;
424 cb->send_buffer_free -= command_len;
426 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
428 cb->send_buffer_fill, cb->send_buffer_size,
429 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
432 /* Check if we have enough space for this command. */
433 pthread_mutex_unlock (&cb->send_lock);
436 } /* }}} int wh_write_command */
438 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
443 pthread_mutex_lock (&cb->send_lock);
445 if (cb->curl == NULL)
447 status = wh_callback_init (cb);
450 ERROR ("write_http plugin: wh_callback_init failed.");
451 pthread_mutex_unlock (&cb->send_lock);
456 status = format_json_value_list (cb->send_buffer,
457 &cb->send_buffer_fill,
458 &cb->send_buffer_free,
459 ds, vl, cb->store_rates);
460 if (status == -ENOMEM)
462 status = wh_flush_nolock (/* timeout = */ 0, cb);
465 wh_reset_buffer (cb);
466 pthread_mutex_unlock (&cb->send_lock);
470 status = format_json_value_list (cb->send_buffer,
471 &cb->send_buffer_fill,
472 &cb->send_buffer_free,
473 ds, vl, cb->store_rates);
477 pthread_mutex_unlock (&cb->send_lock);
481 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
483 cb->send_buffer_fill, cb->send_buffer_size,
484 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
486 /* Check if we have enough space for this command. */
487 pthread_mutex_unlock (&cb->send_lock);
490 } /* }}} int wh_write_json */
492 static int wh_write_kairosdb (const data_set_t *ds, const value_list_t *vl, /* {{{ */
497 pthread_mutex_lock (&cb->send_lock);
499 if (cb->curl == NULL)
501 status = wh_callback_init (cb);
504 ERROR ("write_http plugin: wh_callback_init failed.");
505 pthread_mutex_unlock (&cb->send_lock);
510 status = format_kairosdb_value_list (cb->send_buffer,
511 &cb->send_buffer_fill,
512 &cb->send_buffer_free,
513 ds, vl, cb->store_rates);
514 if (status == -ENOMEM)
516 status = wh_flush_nolock (/* timeout = */ 0, cb);
519 wh_reset_buffer (cb);
520 pthread_mutex_unlock (&cb->send_lock);
524 status = format_kairosdb_value_list (cb->send_buffer,
525 &cb->send_buffer_fill,
526 &cb->send_buffer_free,
527 ds, vl, cb->store_rates);
531 pthread_mutex_unlock (&cb->send_lock);
535 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
537 cb->send_buffer_fill, cb->send_buffer_size,
538 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
540 /* Check if we have enough space for this command. */
541 pthread_mutex_unlock (&cb->send_lock);
544 } /* }}} int wh_write_kairosdb */
546 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
547 user_data_t *user_data)
552 if (user_data == NULL)
555 cb = user_data->data;
559 status = wh_write_json (ds, vl, cb);
561 case WH_FORMAT_KAIROSDB:
562 status = wh_write_kairosdb (ds, vl, cb);
565 status = wh_write_command (ds, vl, cb);
569 } /* }}} int wh_write */
571 static int config_set_format (wh_callback_t *cb, /* {{{ */
576 if ((ci->values_num != 1)
577 || (ci->values[0].type != OCONFIG_TYPE_STRING))
579 WARNING ("write_http plugin: The `%s' config option "
580 "needs exactly one string argument.", ci->key);
584 string = ci->values[0].value.string;
585 if (strcasecmp ("Command", string) == 0)
586 cb->format = WH_FORMAT_COMMAND;
587 else if (strcasecmp ("JSON", string) == 0)
588 cb->format = WH_FORMAT_JSON;
589 else if (strcasecmp ("KAIROSDB", string) == 0)
590 cb->format = WH_FORMAT_KAIROSDB;
593 ERROR ("write_http plugin: Invalid format string: %s",
599 } /* }}} int config_set_format */
601 static int wh_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
604 struct curl_slist *temp = NULL;
605 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
607 WARNING ("write_http plugin: `%s' needs exactly one string argument.", name);
611 temp = curl_slist_append(*dest, ci->values[0].value.string);
618 } /* }}} int wh_config_append_string */
620 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
624 user_data_t user_data;
625 char callback_name[DATA_MAX_NAME_LEN];
629 cb = calloc (1, sizeof (*cb));
632 ERROR ("write_http plugin: calloc failed.");
637 cb->format = WH_FORMAT_COMMAND;
638 cb->sslversion = CURL_SSLVERSION_DEFAULT;
639 cb->low_speed_limit = 0;
641 cb->log_http_error = 0;
645 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
647 cf_util_get_string (ci, &cb->name);
649 /* FIXME: Remove this legacy mode in version 6. */
650 if (strcasecmp ("URL", ci->key) == 0)
651 cf_util_get_string (ci, &cb->location);
653 for (i = 0; i < ci->children_num; i++)
655 oconfig_item_t *child = ci->children + i;
657 if (strcasecmp ("URL", child->key) == 0)
658 status = cf_util_get_string (child, &cb->location);
659 else if (strcasecmp ("User", child->key) == 0)
660 status = cf_util_get_string (child, &cb->user);
661 else if (strcasecmp ("Password", child->key) == 0)
662 status = cf_util_get_string (child, &cb->pass);
663 else if (strcasecmp ("VerifyPeer", child->key) == 0)
664 status = cf_util_get_boolean (child, &cb->verify_peer);
665 else if (strcasecmp ("VerifyHost", child->key) == 0)
666 status = cf_util_get_boolean (child, &cb->verify_host);
667 else if (strcasecmp ("CACert", child->key) == 0)
668 status = cf_util_get_string (child, &cb->cacert);
669 else if (strcasecmp ("CAPath", child->key) == 0)
670 status = cf_util_get_string (child, &cb->capath);
671 else if (strcasecmp ("ClientKey", child->key) == 0)
672 status = cf_util_get_string (child, &cb->clientkey);
673 else if (strcasecmp ("ClientCert", child->key) == 0)
674 status = cf_util_get_string (child, &cb->clientcert);
675 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
676 status = cf_util_get_string (child, &cb->clientkeypass);
677 else if (strcasecmp ("SSLVersion", child->key) == 0)
681 status = cf_util_get_string (child, &value);
685 if (value == NULL || strcasecmp ("default", value) == 0)
686 cb->sslversion = CURL_SSLVERSION_DEFAULT;
687 else if (strcasecmp ("SSLv2", value) == 0)
688 cb->sslversion = CURL_SSLVERSION_SSLv2;
689 else if (strcasecmp ("SSLv3", value) == 0)
690 cb->sslversion = CURL_SSLVERSION_SSLv3;
691 else if (strcasecmp ("TLSv1", value) == 0)
692 cb->sslversion = CURL_SSLVERSION_TLSv1;
693 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
694 else if (strcasecmp ("TLSv1_0", value) == 0)
695 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
696 else if (strcasecmp ("TLSv1_1", value) == 0)
697 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
698 else if (strcasecmp ("TLSv1_2", value) == 0)
699 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
703 ERROR ("write_http plugin: Invalid SSLVersion "
704 "option: %s.", value);
710 else if (strcasecmp ("Format", child->key) == 0)
711 status = config_set_format (cb, child);
712 else if (strcasecmp ("StoreRates", child->key) == 0)
713 status = cf_util_get_boolean (child, &cb->store_rates);
714 else if (strcasecmp ("BufferSize", child->key) == 0)
715 status = cf_util_get_int (child, &buffer_size);
716 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
717 status = cf_util_get_int (child, &cb->low_speed_limit);
718 else if (strcasecmp ("Timeout", child->key) == 0)
719 status = cf_util_get_int (child, &cb->timeout);
720 else if (strcasecmp ("LogHttpError", child->key) == 0)
721 status = cf_util_get_boolean (child, &cb->log_http_error);
722 else if (strcasecmp ("Header", child->key) == 0)
723 status = wh_config_append_string ("Header", &cb->headers, child);
726 ERROR ("write_http plugin: Invalid configuration "
727 "option: %s.", child->key);
737 wh_callback_free (cb);
741 if (cb->location == NULL)
743 ERROR ("write_http plugin: no URL defined for instance '%s'",
745 wh_callback_free (cb);
749 if (cb->low_speed_limit > 0)
750 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
752 /* Determine send_buffer_size. */
753 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
754 if (buffer_size >= 1024)
755 cb->send_buffer_size = (size_t) buffer_size;
756 else if (buffer_size != 0)
757 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
760 /* Allocate the buffer. */
761 cb->send_buffer = malloc (cb->send_buffer_size);
762 if (cb->send_buffer == NULL)
764 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
765 wh_callback_free (cb);
768 /* Nulls the buffer and sets ..._free and ..._fill. */
769 wh_reset_buffer (cb);
771 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
773 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
774 callback_name, cb->location);
776 memset (&user_data, 0, sizeof (user_data));
778 user_data.free_func = NULL;
779 plugin_register_flush (callback_name, wh_flush, &user_data);
781 user_data.free_func = wh_callback_free;
782 plugin_register_write (callback_name, wh_write, &user_data);
785 } /* }}} int wh_config_node */
787 static int wh_config (oconfig_item_t *ci) /* {{{ */
791 for (i = 0; i < ci->children_num; i++)
793 oconfig_item_t *child = ci->children + i;
795 if (strcasecmp ("Node", child->key) == 0)
796 wh_config_node (child);
797 /* FIXME: Remove this legacy mode in version 6. */
798 else if (strcasecmp ("URL", child->key) == 0) {
799 WARNING ("write_http plugin: Legacy <URL> block found. "
800 "Please use <Node> instead.");
801 wh_config_node (child);
805 ERROR ("write_http plugin: Invalid configuration "
806 "option: %s.", child->key);
811 } /* }}} int wh_config */
813 static int wh_init (void) /* {{{ */
815 /* Call this while collectd is still single-threaded to avoid
816 * initialization issues in libgcrypt. */
817 curl_global_init (CURL_GLOBAL_SSL);
819 } /* }}} int wh_init */
821 void module_register (void) /* {{{ */
823 plugin_register_complex_config ("write_http", wh_config);
824 plugin_register_init ("write_http", wh_init);
825 } /* }}} void module_register */
827 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */