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
63 #define WH_FORMAT_COMMAND 0
64 #define WH_FORMAT_JSON 1
68 char curl_errbuf[CURL_ERROR_SIZE];
71 size_t send_buffer_size;
72 size_t send_buffer_free;
73 size_t send_buffer_fill;
74 cdtime_t send_buffer_init_time;
76 pthread_mutex_t send_lock;
78 typedef struct wh_callback_s wh_callback_t;
80 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
82 memset (cb->send_buffer, 0, cb->send_buffer_size);
83 cb->send_buffer_free = cb->send_buffer_size;
84 cb->send_buffer_fill = 0;
85 cb->send_buffer_init_time = cdtime ();
87 if (cb->format == WH_FORMAT_JSON)
89 format_json_initialize (cb->send_buffer,
90 &cb->send_buffer_fill,
91 &cb->send_buffer_free);
93 } /* }}} wh_reset_buffer */
95 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
99 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
100 status = curl_easy_perform (cb->curl);
101 if (status != CURLE_OK)
103 ERROR ("write_http plugin: curl_easy_perform failed with "
105 status, cb->curl_errbuf);
108 } /* }}} wh_send_buffer */
110 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
112 struct curl_slist *headers;
114 if (cb->curl != NULL)
117 cb->curl = curl_easy_init ();
118 if (cb->curl == NULL)
120 ERROR ("curl plugin: curl_easy_init failed.");
124 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
125 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
128 headers = curl_slist_append (headers, "Accept: */*");
129 if (cb->format == WH_FORMAT_JSON)
130 headers = curl_slist_append (headers, "Content-Type: application/json");
132 headers = curl_slist_append (headers, "Content-Type: text/plain");
133 headers = curl_slist_append (headers, "Expect:");
134 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
136 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
137 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
138 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
139 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
141 if (cb->user != NULL)
143 #ifdef HAVE_CURLOPT_USERNAME
144 curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user);
145 curl_easy_setopt (cb->curl, CURLOPT_PASSWORD,
146 (cb->pass == NULL) ? "" : cb->pass);
148 size_t credentials_size;
150 credentials_size = strlen (cb->user) + 2;
151 if (cb->pass != NULL)
152 credentials_size += strlen (cb->pass);
154 cb->credentials = (char *) malloc (credentials_size);
155 if (cb->credentials == NULL)
157 ERROR ("curl plugin: malloc failed.");
161 ssnprintf (cb->credentials, credentials_size, "%s:%s",
162 cb->user, (cb->pass == NULL) ? "" : cb->pass);
163 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
165 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
168 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
169 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
170 cb->verify_host ? 2L : 0L);
171 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
172 if (cb->cacert != NULL)
173 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
174 if (cb->capath != NULL)
175 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
177 if (cb->clientkey != NULL && cb->clientcert != NULL)
179 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
180 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
182 if (cb->clientkeypass != NULL)
183 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
186 wh_reset_buffer (cb);
189 } /* }}} int wh_callback_init */
191 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
195 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
196 "send_buffer_fill = %zu;",
197 CDTIME_T_TO_DOUBLE (timeout),
198 cb->send_buffer_fill);
200 /* timeout == 0 => flush unconditionally */
206 if ((cb->send_buffer_init_time + timeout) > now)
210 if (cb->format == WH_FORMAT_COMMAND)
212 if (cb->send_buffer_fill <= 0)
214 cb->send_buffer_init_time = cdtime ();
218 status = wh_send_buffer (cb);
219 wh_reset_buffer (cb);
221 else if (cb->format == WH_FORMAT_JSON)
223 if (cb->send_buffer_fill <= 2)
225 cb->send_buffer_init_time = cdtime ();
229 status = format_json_finalize (cb->send_buffer,
230 &cb->send_buffer_fill,
231 &cb->send_buffer_free);
234 ERROR ("write_http: wh_flush_nolock: "
235 "format_json_finalize failed.");
236 wh_reset_buffer (cb);
240 status = wh_send_buffer (cb);
241 wh_reset_buffer (cb);
245 ERROR ("write_http: wh_flush_nolock: "
246 "Unknown format: %i",
252 } /* }}} wh_flush_nolock */
254 static int wh_flush (cdtime_t timeout, /* {{{ */
255 const char *identifier __attribute__((unused)),
256 user_data_t *user_data)
261 if (user_data == NULL)
264 cb = user_data->data;
266 pthread_mutex_lock (&cb->send_lock);
268 if (cb->curl == NULL)
270 status = wh_callback_init (cb);
273 ERROR ("write_http plugin: wh_callback_init failed.");
274 pthread_mutex_unlock (&cb->send_lock);
279 status = wh_flush_nolock (timeout, cb);
280 pthread_mutex_unlock (&cb->send_lock);
283 } /* }}} int wh_flush */
285 static void wh_callback_free (void *data) /* {{{ */
294 wh_flush_nolock (/* timeout = */ 0, cb);
296 if (cb->curl != NULL)
298 curl_easy_cleanup (cb->curl);
302 sfree (cb->location);
305 sfree (cb->credentials);
308 sfree (cb->clientkey);
309 sfree (cb->clientcert);
310 sfree (cb->clientkeypass);
311 sfree (cb->send_buffer);
314 } /* }}} void wh_callback_free */
316 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
319 char key[10*DATA_MAX_NAME_LEN];
326 if (0 != strcmp (ds->type, vl->type)) {
327 ERROR ("write_http plugin: DS type does not match "
332 /* Copy the identifier to `key' and escape it. */
333 status = FORMAT_VL (key, sizeof (key), vl);
335 ERROR ("write_http plugin: error with format_name");
338 escape_string (key, sizeof (key));
340 /* Convert the values to an ASCII representation and put that into
342 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
344 ERROR ("write_http plugin: error with "
345 "wh_value_list_to_string");
349 command_len = (size_t) ssnprintf (command, sizeof (command),
350 "PUTVAL %s interval=%.3f %s\r\n",
352 CDTIME_T_TO_DOUBLE (vl->interval),
354 if (command_len >= sizeof (command)) {
355 ERROR ("write_http plugin: Command buffer too small: "
356 "Need %zu bytes.", command_len + 1);
360 pthread_mutex_lock (&cb->send_lock);
362 if (cb->curl == NULL)
364 status = wh_callback_init (cb);
367 ERROR ("write_http plugin: wh_callback_init failed.");
368 pthread_mutex_unlock (&cb->send_lock);
373 if (command_len >= cb->send_buffer_free)
375 status = wh_flush_nolock (/* timeout = */ 0, cb);
378 pthread_mutex_unlock (&cb->send_lock);
382 assert (command_len < cb->send_buffer_free);
384 /* `command_len + 1' because `command_len' does not include the
385 * trailing null byte. Neither does `send_buffer_fill'. */
386 memcpy (cb->send_buffer + cb->send_buffer_fill,
387 command, command_len + 1);
388 cb->send_buffer_fill += command_len;
389 cb->send_buffer_free -= command_len;
391 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
393 cb->send_buffer_fill, cb->send_buffer_size,
394 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
397 /* Check if we have enough space for this command. */
398 pthread_mutex_unlock (&cb->send_lock);
401 } /* }}} int wh_write_command */
403 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
408 pthread_mutex_lock (&cb->send_lock);
410 if (cb->curl == NULL)
412 status = wh_callback_init (cb);
415 ERROR ("write_http plugin: wh_callback_init failed.");
416 pthread_mutex_unlock (&cb->send_lock);
421 status = format_json_value_list (cb->send_buffer,
422 &cb->send_buffer_fill,
423 &cb->send_buffer_free,
424 ds, vl, cb->store_rates);
425 if (status == (-ENOMEM))
427 status = wh_flush_nolock (/* timeout = */ 0, cb);
430 wh_reset_buffer (cb);
431 pthread_mutex_unlock (&cb->send_lock);
435 status = format_json_value_list (cb->send_buffer,
436 &cb->send_buffer_fill,
437 &cb->send_buffer_free,
438 ds, vl, cb->store_rates);
442 pthread_mutex_unlock (&cb->send_lock);
446 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
448 cb->send_buffer_fill, cb->send_buffer_size,
449 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
451 /* Check if we have enough space for this command. */
452 pthread_mutex_unlock (&cb->send_lock);
455 } /* }}} int wh_write_json */
457 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
458 user_data_t *user_data)
463 if (user_data == NULL)
466 cb = user_data->data;
468 if (cb->format == WH_FORMAT_JSON)
469 status = wh_write_json (ds, vl, cb);
471 status = wh_write_command (ds, vl, cb);
474 } /* }}} int wh_write */
476 static int config_set_format (wh_callback_t *cb, /* {{{ */
481 if ((ci->values_num != 1)
482 || (ci->values[0].type != OCONFIG_TYPE_STRING))
484 WARNING ("write_http plugin: The `%s' config option "
485 "needs exactly one string argument.", ci->key);
489 string = ci->values[0].value.string;
490 if (strcasecmp ("Command", string) == 0)
491 cb->format = WH_FORMAT_COMMAND;
492 else if (strcasecmp ("JSON", string) == 0)
493 cb->format = WH_FORMAT_JSON;
496 ERROR ("write_http plugin: Invalid format string: %s",
502 } /* }}} int config_set_format */
504 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
508 user_data_t user_data;
509 char callback_name[DATA_MAX_NAME_LEN];
512 cb = malloc (sizeof (*cb));
515 ERROR ("write_http plugin: malloc failed.");
518 memset (cb, 0, sizeof (*cb));
521 cb->format = WH_FORMAT_COMMAND;
522 cb->sslversion = CURL_SSLVERSION_DEFAULT;
524 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
526 cf_util_get_string (ci, &cb->name);
528 /* FIXME: Remove this legacy mode in version 6. */
529 if (strcasecmp ("URL", ci->key) == 0)
530 cf_util_get_string (ci, &cb->location);
532 for (i = 0; i < ci->children_num; i++)
534 oconfig_item_t *child = ci->children + i;
536 if (strcasecmp ("URL", child->key) == 0)
537 cf_util_get_string (child, &cb->location);
538 else if (strcasecmp ("User", child->key) == 0)
539 cf_util_get_string (child, &cb->user);
540 else if (strcasecmp ("Password", child->key) == 0)
541 cf_util_get_string (child, &cb->pass);
542 else if (strcasecmp ("VerifyPeer", child->key) == 0)
543 cf_util_get_boolean (child, &cb->verify_peer);
544 else if (strcasecmp ("VerifyHost", child->key) == 0)
545 cf_util_get_boolean (child, &cb->verify_host);
546 else if (strcasecmp ("CACert", child->key) == 0)
547 cf_util_get_string (child, &cb->cacert);
548 else if (strcasecmp ("CAPath", child->key) == 0)
549 cf_util_get_string (child, &cb->capath);
550 else if (strcasecmp ("ClientKey", child->key) == 0)
551 cf_util_get_string (child, &cb->clientkey);
552 else if (strcasecmp ("ClientCert", child->key) == 0)
553 cf_util_get_string (child, &cb->clientcert);
554 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
555 cf_util_get_string (child, &cb->clientkeypass);
556 else if (strcasecmp ("SSLVersion", child->key) == 0)
560 cf_util_get_string (child, &value);
562 if (value == NULL || strcasecmp ("default", value) == 0)
563 cb->sslversion = CURL_SSLVERSION_DEFAULT;
564 else if (strcasecmp ("SSLv2", value) == 0)
565 cb->sslversion = CURL_SSLVERSION_SSLv2;
566 else if (strcasecmp ("SSLv3", value) == 0)
567 cb->sslversion = CURL_SSLVERSION_SSLv3;
568 else if (strcasecmp ("TLSv1", value) == 0)
569 cb->sslversion = CURL_SSLVERSION_TLSv1;
570 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
571 else if (strcasecmp ("TLSv1_0", value) == 0)
572 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
573 else if (strcasecmp ("TLSv1_1", value) == 0)
574 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
575 else if (strcasecmp ("TLSv1_2", value) == 0)
576 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
579 ERROR ("write_http plugin: Invalid SSLVersion "
580 "option: %s.", value);
584 else if (strcasecmp ("Format", child->key) == 0)
585 config_set_format (cb, child);
586 else if (strcasecmp ("StoreRates", child->key) == 0)
587 cf_util_get_boolean (child, &cb->store_rates);
588 else if (strcasecmp ("BufferSize", child->key) == 0)
589 cf_util_get_int (child, &buffer_size);
592 ERROR ("write_http plugin: Invalid configuration "
593 "option: %s.", child->key);
597 if (cb->location == NULL)
599 ERROR ("write_http plugin: no URL defined for instance '%s'",
601 wh_callback_free (cb);
605 /* Determine send_buffer_size. */
606 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
607 if (buffer_size >= 1024)
608 cb->send_buffer_size = (size_t) buffer_size;
609 else if (buffer_size != 0)
610 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
613 /* Allocate the buffer. */
614 cb->send_buffer = malloc (cb->send_buffer_size);
615 if (cb->send_buffer == NULL)
617 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
618 wh_callback_free (cb);
621 /* Nulls the buffer and sets ..._free and ..._fill. */
622 wh_reset_buffer (cb);
624 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
626 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
627 callback_name, cb->location);
629 memset (&user_data, 0, sizeof (user_data));
631 user_data.free_func = NULL;
632 plugin_register_flush (callback_name, wh_flush, &user_data);
634 user_data.free_func = wh_callback_free;
635 plugin_register_write (callback_name, wh_write, &user_data);
638 } /* }}} int wh_config_node */
640 static int wh_config (oconfig_item_t *ci) /* {{{ */
644 for (i = 0; i < ci->children_num; i++)
646 oconfig_item_t *child = ci->children + i;
648 if (strcasecmp ("Node", child->key) == 0)
649 wh_config_node (child);
650 /* FIXME: Remove this legacy mode in version 6. */
651 else if (strcasecmp ("URL", child->key) == 0) {
652 WARNING ("write_http plugin: Legacy <URL> block found. "
653 "Please use <Node> instead.");
654 wh_config_node (child);
658 ERROR ("write_http plugin: Invalid configuration "
659 "option: %s.", child->key);
664 } /* }}} int wh_config */
666 static int wh_init (void) /* {{{ */
668 /* Call this while collectd is still single-threaded to avoid
669 * initialization issues in libgcrypt. */
670 curl_global_init (CURL_GLOBAL_SSL);
672 } /* }}} int wh_init */
674 void module_register (void) /* {{{ */
676 plugin_register_complex_config ("write_http", wh_config);
677 plugin_register_init ("write_http", wh_init);
678 } /* }}} void module_register */
680 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */