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
42 #define WH_DEFAULT_LOW_LIMIT_BYTES_PER_SEC 100
66 #define WH_FORMAT_COMMAND 0
67 #define WH_FORMAT_JSON 1
71 char curl_errbuf[CURL_ERROR_SIZE];
74 size_t send_buffer_size;
75 size_t send_buffer_free;
76 size_t send_buffer_fill;
77 cdtime_t send_buffer_init_time;
79 pthread_mutex_t send_lock;
81 typedef struct wh_callback_s wh_callback_t;
83 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
85 memset (cb->send_buffer, 0, cb->send_buffer_size);
86 cb->send_buffer_free = cb->send_buffer_size;
87 cb->send_buffer_fill = 0;
88 cb->send_buffer_init_time = cdtime ();
90 if (cb->format == WH_FORMAT_JSON)
92 format_json_initialize (cb->send_buffer,
93 &cb->send_buffer_fill,
94 &cb->send_buffer_free);
96 } /* }}} wh_reset_buffer */
98 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
102 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
103 status = curl_easy_perform (cb->curl);
104 if (status != CURLE_OK)
106 ERROR ("write_http plugin: curl_easy_perform failed with "
108 status, cb->curl_errbuf);
111 } /* }}} wh_send_buffer */
113 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
115 struct curl_slist *headers;
117 if (cb->curl != NULL)
120 cb->curl = curl_easy_init ();
121 if (cb->curl == NULL)
123 ERROR ("curl plugin: curl_easy_init failed.");
127 if(cb->abort_on_slow && cb->interval > 0)
129 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_LIMIT, (cb->low_limit_bytes?cb->low_limit_bytes:WH_DEFAULT_LOW_LIMIT_BYTES_PER_SEC));
130 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_TIME, cb->interval);
133 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
134 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
137 headers = curl_slist_append (headers, "Accept: */*");
138 if (cb->format == WH_FORMAT_JSON)
139 headers = curl_slist_append (headers, "Content-Type: application/json");
141 headers = curl_slist_append (headers, "Content-Type: text/plain");
142 headers = curl_slist_append (headers, "Expect:");
143 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
145 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
146 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
148 if (cb->user != NULL)
150 size_t credentials_size;
152 credentials_size = strlen (cb->user) + 2;
153 if (cb->pass != NULL)
154 credentials_size += strlen (cb->pass);
156 cb->credentials = (char *) malloc (credentials_size);
157 if (cb->credentials == NULL)
159 ERROR ("curl plugin: malloc failed.");
163 ssnprintf (cb->credentials, credentials_size, "%s:%s",
164 cb->user, (cb->pass == NULL) ? "" : cb->pass);
165 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
166 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
169 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
170 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
171 cb->verify_host ? 2L : 0L);
172 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
173 if (cb->cacert != NULL)
174 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
175 if (cb->capath != NULL)
176 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
178 if (cb->clientkey != NULL && cb->clientcert != NULL)
180 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
181 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
183 if (cb->clientkeypass != NULL)
184 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
187 wh_reset_buffer (cb);
190 } /* }}} int wh_callback_init */
192 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
196 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
197 "send_buffer_fill = %zu;",
198 CDTIME_T_TO_DOUBLE (timeout),
199 cb->send_buffer_fill);
201 /* timeout == 0 => flush unconditionally */
207 if ((cb->send_buffer_init_time + timeout) > now)
211 if (cb->format == WH_FORMAT_COMMAND)
213 if (cb->send_buffer_fill <= 0)
215 cb->send_buffer_init_time = cdtime ();
219 status = wh_send_buffer (cb);
220 wh_reset_buffer (cb);
222 else if (cb->format == WH_FORMAT_JSON)
224 if (cb->send_buffer_fill <= 2)
226 cb->send_buffer_init_time = cdtime ();
230 status = format_json_finalize (cb->send_buffer,
231 &cb->send_buffer_fill,
232 &cb->send_buffer_free);
235 ERROR ("write_http: wh_flush_nolock: "
236 "format_json_finalize failed.");
237 wh_reset_buffer (cb);
241 status = wh_send_buffer (cb);
242 wh_reset_buffer (cb);
246 ERROR ("write_http: wh_flush_nolock: "
247 "Unknown format: %i",
253 } /* }}} wh_flush_nolock */
255 static int wh_flush (cdtime_t timeout, /* {{{ */
256 const char *identifier __attribute__((unused)),
257 user_data_t *user_data)
262 if (user_data == NULL)
265 cb = user_data->data;
267 pthread_mutex_lock (&cb->send_lock);
269 if (cb->curl == NULL)
271 status = wh_callback_init (cb);
274 ERROR ("write_http plugin: wh_callback_init failed.");
275 pthread_mutex_unlock (&cb->send_lock);
280 status = wh_flush_nolock (timeout, cb);
281 pthread_mutex_unlock (&cb->send_lock);
284 } /* }}} int wh_flush */
286 static void wh_callback_free (void *data) /* {{{ */
295 wh_flush_nolock (/* timeout = */ 0, cb);
297 if (cb->curl != NULL)
299 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 cb->interval = CDTIME_T_TO_TIME_T(vl->interval);
365 status = wh_callback_init (cb);
368 ERROR ("write_http plugin: wh_callback_init failed.");
369 pthread_mutex_unlock (&cb->send_lock);
374 if (command_len >= cb->send_buffer_free)
376 status = wh_flush_nolock (/* timeout = */ 0, cb);
379 pthread_mutex_unlock (&cb->send_lock);
383 assert (command_len < cb->send_buffer_free);
385 /* `command_len + 1' because `command_len' does not include the
386 * trailing null byte. Neither does `send_buffer_fill'. */
387 memcpy (cb->send_buffer + cb->send_buffer_fill,
388 command, command_len + 1);
389 cb->send_buffer_fill += command_len;
390 cb->send_buffer_free -= command_len;
392 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
394 cb->send_buffer_fill, cb->send_buffer_size,
395 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
398 /* Check if we have enough space for this command. */
399 pthread_mutex_unlock (&cb->send_lock);
402 } /* }}} int wh_write_command */
404 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
409 pthread_mutex_lock (&cb->send_lock);
411 if (cb->curl == NULL)
413 cb->interval = CDTIME_T_TO_TIME_T(vl->interval);
415 status = wh_callback_init (cb);
418 ERROR ("write_http plugin: wh_callback_init failed.");
419 pthread_mutex_unlock (&cb->send_lock);
424 status = format_json_value_list (cb->send_buffer,
425 &cb->send_buffer_fill,
426 &cb->send_buffer_free,
427 ds, vl, cb->store_rates);
428 if (status == (-ENOMEM))
430 status = wh_flush_nolock (/* timeout = */ 0, cb);
433 wh_reset_buffer (cb);
434 pthread_mutex_unlock (&cb->send_lock);
438 status = format_json_value_list (cb->send_buffer,
439 &cb->send_buffer_fill,
440 &cb->send_buffer_free,
441 ds, vl, cb->store_rates);
445 pthread_mutex_unlock (&cb->send_lock);
449 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
451 cb->send_buffer_fill, cb->send_buffer_size,
452 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
454 /* Check if we have enough space for this command. */
455 pthread_mutex_unlock (&cb->send_lock);
458 } /* }}} int wh_write_json */
460 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
461 user_data_t *user_data)
466 if (user_data == NULL)
469 cb = user_data->data;
471 if (cb->format == WH_FORMAT_JSON)
472 status = wh_write_json (ds, vl, cb);
474 status = wh_write_command (ds, vl, cb);
477 } /* }}} int wh_write */
479 static int config_set_format (wh_callback_t *cb, /* {{{ */
484 if ((ci->values_num != 1)
485 || (ci->values[0].type != OCONFIG_TYPE_STRING))
487 WARNING ("write_http plugin: The `%s' config option "
488 "needs exactly one string argument.", ci->key);
492 string = ci->values[0].value.string;
493 if (strcasecmp ("Command", string) == 0)
494 cb->format = WH_FORMAT_COMMAND;
495 else if (strcasecmp ("JSON", string) == 0)
496 cb->format = WH_FORMAT_JSON;
499 ERROR ("write_http plugin: Invalid format string: %s",
505 } /* }}} int config_set_format */
507 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
511 user_data_t user_data;
514 cb = malloc (sizeof (*cb));
517 ERROR ("write_http plugin: malloc failed.");
520 memset (cb, 0, sizeof (*cb));
523 cb->format = WH_FORMAT_COMMAND;
524 cb->sslversion = CURL_SSLVERSION_DEFAULT;
525 cb->low_limit_bytes = WH_DEFAULT_LOW_LIMIT_BYTES_PER_SEC;
527 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
529 cf_util_get_string (ci, &cb->location);
530 if (cb->location == NULL)
533 for (i = 0; i < ci->children_num; i++)
535 oconfig_item_t *child = ci->children + i;
537 if (strcasecmp ("User", child->key) == 0)
538 cf_util_get_string (child, &cb->user);
539 else if (strcasecmp ("Password", child->key) == 0)
540 cf_util_get_string (child, &cb->pass);
541 else if (strcasecmp ("VerifyPeer", child->key) == 0)
542 cf_util_get_boolean (child, &cb->verify_peer);
543 else if (strcasecmp ("VerifyHost", child->key) == 0)
544 cf_util_get_boolean (child, &cb->verify_host);
545 else if (strcasecmp ("CACert", child->key) == 0)
546 cf_util_get_string (child, &cb->cacert);
547 else if (strcasecmp ("CAPath", child->key) == 0)
548 cf_util_get_string (child, &cb->capath);
549 else if (strcasecmp ("ClientKey", child->key) == 0)
550 cf_util_get_string (child, &cb->clientkey);
551 else if (strcasecmp ("ClientCert", child->key) == 0)
552 cf_util_get_string (child, &cb->clientcert);
553 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
554 cf_util_get_string (child, &cb->clientkeypass);
555 else if (strcasecmp ("SSLVersion", child->key) == 0)
559 cf_util_get_string (child, &value);
561 if (value == NULL || strcasecmp ("default", value) == 0)
562 cb->sslversion = CURL_SSLVERSION_DEFAULT;
563 else if (strcasecmp ("SSLv2", value) == 0)
564 cb->sslversion = CURL_SSLVERSION_SSLv2;
565 else if (strcasecmp ("SSLv3", value) == 0)
566 cb->sslversion = CURL_SSLVERSION_SSLv3;
567 else if (strcasecmp ("TLSv1", value) == 0)
568 cb->sslversion = CURL_SSLVERSION_TLSv1;
569 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
570 else if (strcasecmp ("TLSv1_0", value) == 0)
571 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
572 else if (strcasecmp ("TLSv1_1", value) == 0)
573 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
574 else if (strcasecmp ("TLSv1_2", value) == 0)
575 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
578 ERROR ("write_http plugin: Invalid SSLVersion "
579 "option: %s.", value);
583 else if (strcasecmp ("Format", child->key) == 0)
584 config_set_format (cb, child);
585 else if (strcasecmp ("StoreRates", child->key) == 0)
586 cf_util_get_boolean (child, &cb->store_rates);
587 else if (strcasecmp ("BufferSize", child->key) == 0)
588 cf_util_get_int (child, &buffer_size);
589 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
590 cf_util_get_boolean (child,&cb->abort_on_slow);
591 else if (strcasecmp ("LowLimitBytesPerSec", child->key) == 0)
592 cf_util_get_int (child, &cb->low_limit_bytes);
595 ERROR ("write_http plugin: Invalid configuration "
596 "option: %s.", child->key);
600 if(cb->abort_on_slow)
602 cb->interval = CDTIME_T_TO_TIME_T(cf_get_default_interval ());
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 DEBUG ("write_http: Registering write callback with URL %s",
627 memset (&user_data, 0, sizeof (user_data));
629 user_data.free_func = NULL;
630 plugin_register_flush ("write_http", wh_flush, &user_data);
632 user_data.free_func = wh_callback_free;
633 plugin_register_write ("write_http", wh_write, &user_data);
636 } /* }}} int wh_config_url */
638 static int wh_config (oconfig_item_t *ci) /* {{{ */
642 for (i = 0; i < ci->children_num; i++)
644 oconfig_item_t *child = ci->children + i;
646 if (strcasecmp ("URL", child->key) == 0)
647 wh_config_url (child);
650 ERROR ("write_http plugin: Invalid configuration "
651 "option: %s.", child->key);
656 } /* }}} int wh_config */
658 static int wh_init (void) /* {{{ */
660 /* Call this while collectd is still single-threaded to avoid
661 * initialization issues in libgcrypt. */
662 curl_global_init (CURL_GLOBAL_SSL);
664 } /* }}} int wh_init */
666 void module_register (void) /* {{{ */
668 plugin_register_complex_config ("write_http", wh_config);
669 plugin_register_init ("write_http", wh_init);
670 } /* }}} void module_register */
672 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */