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
68 #define WH_FORMAT_COMMAND 0
69 #define WH_FORMAT_JSON 1
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_reset_buffer (wh_callback_t *cb) /* {{{ */
87 memset (cb->send_buffer, 0, cb->send_buffer_size);
88 cb->send_buffer_free = cb->send_buffer_size;
89 cb->send_buffer_fill = 0;
90 cb->send_buffer_init_time = cdtime ();
92 if (cb->format == WH_FORMAT_JSON)
94 format_json_initialize (cb->send_buffer,
95 &cb->send_buffer_fill,
96 &cb->send_buffer_free);
98 } /* }}} wh_reset_buffer */
100 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
104 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
105 status = curl_easy_perform (cb->curl);
106 if (status != CURLE_OK)
108 ERROR ("write_http plugin: curl_easy_perform failed with "
110 status, cb->curl_errbuf);
113 } /* }}} wh_send_buffer */
115 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
117 struct curl_slist *headers;
119 if (cb->curl != NULL)
122 cb->curl = curl_easy_init ();
123 if (cb->curl == NULL)
125 ERROR ("curl plugin: curl_easy_init failed.");
129 if (cb->abort_on_slow && cb->interval > 0)
131 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_LIMIT,
132 (cb->low_limit_bytes ? cb->low_limit_bytes : WH_DEFAULT_LOW_LIMIT_BYTES_PER_SEC));
133 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_TIME, cb->interval);
136 if (cb->post_timeout > 0)
137 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT, cb->post_timeout);
139 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
140 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
143 headers = curl_slist_append (headers, "Accept: */*");
144 if (cb->format == WH_FORMAT_JSON)
145 headers = curl_slist_append (headers, "Content-Type: application/json");
147 headers = curl_slist_append (headers, "Content-Type: text/plain");
148 headers = curl_slist_append (headers, "Expect:");
149 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
151 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
152 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
153 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
154 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
156 if (cb->user != NULL)
158 #ifdef HAVE_CURLOPT_USERNAME
159 curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user);
160 curl_easy_setopt (cb->curl, CURLOPT_PASSWORD,
161 (cb->pass == NULL) ? "" : cb->pass);
163 size_t credentials_size;
165 credentials_size = strlen (cb->user) + 2;
166 if (cb->pass != NULL)
167 credentials_size += strlen (cb->pass);
169 cb->credentials = (char *) malloc (credentials_size);
170 if (cb->credentials == NULL)
172 ERROR ("curl plugin: malloc failed.");
176 ssnprintf (cb->credentials, credentials_size, "%s:%s",
177 cb->user, (cb->pass == NULL) ? "" : cb->pass);
178 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
180 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
183 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
184 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
185 cb->verify_host ? 2L : 0L);
186 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
187 if (cb->cacert != NULL)
188 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
189 if (cb->capath != NULL)
190 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
192 if (cb->clientkey != NULL && cb->clientcert != NULL)
194 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
195 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
197 if (cb->clientkeypass != NULL)
198 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
201 wh_reset_buffer (cb);
204 } /* }}} int wh_callback_init */
206 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
210 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
211 "send_buffer_fill = %zu;",
212 CDTIME_T_TO_DOUBLE (timeout),
213 cb->send_buffer_fill);
215 /* timeout == 0 => flush unconditionally */
221 if ((cb->send_buffer_init_time + timeout) > now)
225 if (cb->format == WH_FORMAT_COMMAND)
227 if (cb->send_buffer_fill <= 0)
229 cb->send_buffer_init_time = cdtime ();
233 status = wh_send_buffer (cb);
234 wh_reset_buffer (cb);
236 else if (cb->format == WH_FORMAT_JSON)
238 if (cb->send_buffer_fill <= 2)
240 cb->send_buffer_init_time = cdtime ();
244 status = format_json_finalize (cb->send_buffer,
245 &cb->send_buffer_fill,
246 &cb->send_buffer_free);
249 ERROR ("write_http: wh_flush_nolock: "
250 "format_json_finalize failed.");
251 wh_reset_buffer (cb);
255 status = wh_send_buffer (cb);
256 wh_reset_buffer (cb);
260 ERROR ("write_http: wh_flush_nolock: "
261 "Unknown format: %i",
267 } /* }}} wh_flush_nolock */
269 static int wh_flush (cdtime_t timeout, /* {{{ */
270 const char *identifier __attribute__((unused)),
271 user_data_t *user_data)
276 if (user_data == NULL)
279 cb = user_data->data;
281 pthread_mutex_lock (&cb->send_lock);
283 if (cb->curl == NULL)
285 status = wh_callback_init (cb);
288 ERROR ("write_http plugin: wh_callback_init failed.");
289 pthread_mutex_unlock (&cb->send_lock);
294 status = wh_flush_nolock (timeout, cb);
295 pthread_mutex_unlock (&cb->send_lock);
298 } /* }}} int wh_flush */
300 static void wh_callback_free (void *data) /* {{{ */
309 wh_flush_nolock (/* timeout = */ 0, cb);
311 if (cb->curl != NULL)
313 curl_easy_cleanup (cb->curl);
317 sfree (cb->location);
320 sfree (cb->credentials);
323 sfree (cb->clientkey);
324 sfree (cb->clientcert);
325 sfree (cb->clientkeypass);
326 sfree (cb->send_buffer);
329 } /* }}} void wh_callback_free */
331 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
334 char key[10*DATA_MAX_NAME_LEN];
341 if (0 != strcmp (ds->type, vl->type)) {
342 ERROR ("write_http plugin: DS type does not match "
347 /* Copy the identifier to `key' and escape it. */
348 status = FORMAT_VL (key, sizeof (key), vl);
350 ERROR ("write_http plugin: error with format_name");
353 escape_string (key, sizeof (key));
355 /* Convert the values to an ASCII representation and put that into
357 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
359 ERROR ("write_http plugin: error with "
360 "wh_value_list_to_string");
364 command_len = (size_t) ssnprintf (command, sizeof (command),
365 "PUTVAL %s interval=%.3f %s\r\n",
367 CDTIME_T_TO_DOUBLE (vl->interval),
369 if (command_len >= sizeof (command)) {
370 ERROR ("write_http plugin: Command buffer too small: "
371 "Need %zu bytes.", command_len + 1);
375 pthread_mutex_lock (&cb->send_lock);
377 if (cb->curl == NULL)
379 cb->interval = CDTIME_T_TO_TIME_T(vl->interval);
380 status = wh_callback_init (cb);
383 ERROR ("write_http plugin: wh_callback_init failed.");
384 pthread_mutex_unlock (&cb->send_lock);
389 if (command_len >= cb->send_buffer_free)
391 status = wh_flush_nolock (/* timeout = */ 0, cb);
394 pthread_mutex_unlock (&cb->send_lock);
398 assert (command_len < cb->send_buffer_free);
400 /* `command_len + 1' because `command_len' does not include the
401 * trailing null byte. Neither does `send_buffer_fill'. */
402 memcpy (cb->send_buffer + cb->send_buffer_fill,
403 command, command_len + 1);
404 cb->send_buffer_fill += command_len;
405 cb->send_buffer_free -= command_len;
407 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
409 cb->send_buffer_fill, cb->send_buffer_size,
410 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
413 /* Check if we have enough space for this command. */
414 pthread_mutex_unlock (&cb->send_lock);
417 } /* }}} int wh_write_command */
419 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
424 pthread_mutex_lock (&cb->send_lock);
426 if (cb->curl == NULL)
428 cb->interval = CDTIME_T_TO_TIME_T(vl->interval);
430 status = wh_callback_init (cb);
433 ERROR ("write_http plugin: wh_callback_init failed.");
434 pthread_mutex_unlock (&cb->send_lock);
439 status = format_json_value_list (cb->send_buffer,
440 &cb->send_buffer_fill,
441 &cb->send_buffer_free,
442 ds, vl, cb->store_rates);
443 if (status == (-ENOMEM))
445 status = wh_flush_nolock (/* timeout = */ 0, cb);
448 wh_reset_buffer (cb);
449 pthread_mutex_unlock (&cb->send_lock);
453 status = format_json_value_list (cb->send_buffer,
454 &cb->send_buffer_fill,
455 &cb->send_buffer_free,
456 ds, vl, cb->store_rates);
460 pthread_mutex_unlock (&cb->send_lock);
464 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
466 cb->send_buffer_fill, cb->send_buffer_size,
467 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
469 /* Check if we have enough space for this command. */
470 pthread_mutex_unlock (&cb->send_lock);
473 } /* }}} int wh_write_json */
475 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
476 user_data_t *user_data)
481 if (user_data == NULL)
484 cb = user_data->data;
486 if (cb->format == WH_FORMAT_JSON)
487 status = wh_write_json (ds, vl, cb);
489 status = wh_write_command (ds, vl, cb);
492 } /* }}} int wh_write */
494 static int config_set_format (wh_callback_t *cb, /* {{{ */
499 if ((ci->values_num != 1)
500 || (ci->values[0].type != OCONFIG_TYPE_STRING))
502 WARNING ("write_http plugin: The `%s' config option "
503 "needs exactly one string argument.", ci->key);
507 string = ci->values[0].value.string;
508 if (strcasecmp ("Command", string) == 0)
509 cb->format = WH_FORMAT_COMMAND;
510 else if (strcasecmp ("JSON", string) == 0)
511 cb->format = WH_FORMAT_JSON;
514 ERROR ("write_http plugin: Invalid format string: %s",
520 } /* }}} int config_set_format */
522 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
526 user_data_t user_data;
527 char callback_name[DATA_MAX_NAME_LEN];
530 cb = malloc (sizeof (*cb));
533 ERROR ("write_http plugin: malloc failed.");
536 memset (cb, 0, sizeof (*cb));
539 cb->format = WH_FORMAT_COMMAND;
540 cb->sslversion = CURL_SSLVERSION_DEFAULT;
541 cb->low_limit_bytes = WH_DEFAULT_LOW_LIMIT_BYTES_PER_SEC;
542 cb->post_timeout = 0;
544 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
546 cf_util_get_string (ci, &cb->name);
548 /* FIXME: Remove this legacy mode in version 6. */
549 if (strcasecmp ("URL", ci->key) == 0)
550 cf_util_get_string (ci, &cb->location);
552 for (i = 0; i < ci->children_num; i++)
554 oconfig_item_t *child = ci->children + i;
556 if (strcasecmp ("URL", child->key) == 0)
557 cf_util_get_string (child, &cb->location);
558 else if (strcasecmp ("User", child->key) == 0)
559 cf_util_get_string (child, &cb->user);
560 else if (strcasecmp ("Password", child->key) == 0)
561 cf_util_get_string (child, &cb->pass);
562 else if (strcasecmp ("VerifyPeer", child->key) == 0)
563 cf_util_get_boolean (child, &cb->verify_peer);
564 else if (strcasecmp ("VerifyHost", child->key) == 0)
565 cf_util_get_boolean (child, &cb->verify_host);
566 else if (strcasecmp ("CACert", child->key) == 0)
567 cf_util_get_string (child, &cb->cacert);
568 else if (strcasecmp ("CAPath", child->key) == 0)
569 cf_util_get_string (child, &cb->capath);
570 else if (strcasecmp ("ClientKey", child->key) == 0)
571 cf_util_get_string (child, &cb->clientkey);
572 else if (strcasecmp ("ClientCert", child->key) == 0)
573 cf_util_get_string (child, &cb->clientcert);
574 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
575 cf_util_get_string (child, &cb->clientkeypass);
576 else if (strcasecmp ("SSLVersion", child->key) == 0)
580 cf_util_get_string (child, &value);
582 if (value == NULL || strcasecmp ("default", value) == 0)
583 cb->sslversion = CURL_SSLVERSION_DEFAULT;
584 else if (strcasecmp ("SSLv2", value) == 0)
585 cb->sslversion = CURL_SSLVERSION_SSLv2;
586 else if (strcasecmp ("SSLv3", value) == 0)
587 cb->sslversion = CURL_SSLVERSION_SSLv3;
588 else if (strcasecmp ("TLSv1", value) == 0)
589 cb->sslversion = CURL_SSLVERSION_TLSv1;
590 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
591 else if (strcasecmp ("TLSv1_0", value) == 0)
592 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
593 else if (strcasecmp ("TLSv1_1", value) == 0)
594 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
595 else if (strcasecmp ("TLSv1_2", value) == 0)
596 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
599 ERROR ("write_http plugin: Invalid SSLVersion "
600 "option: %s.", value);
604 else if (strcasecmp ("Format", child->key) == 0)
605 config_set_format (cb, child);
606 else if (strcasecmp ("StoreRates", child->key) == 0)
607 cf_util_get_boolean (child, &cb->store_rates);
608 else if (strcasecmp ("BufferSize", child->key) == 0)
609 cf_util_get_int (child, &buffer_size);
610 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
611 cf_util_get_boolean (child,&cb->abort_on_slow);
612 else if (strcasecmp ("LowLimitBytesPerSec", child->key) == 0)
613 cf_util_get_int (child, &cb->low_limit_bytes);
614 else if (strcasecmp ("PostTimeoutSec", child->key) == 0)
615 cf_util_get_int (child, &cb->post_timeout);
618 ERROR ("write_http plugin: Invalid configuration "
619 "option: %s.", child->key);
623 if (cb->location == NULL)
625 ERROR ("write_http plugin: no URL defined for instance '%s'",
627 wh_callback_free (cb);
631 if (cb->abort_on_slow)
632 cb->interval = CDTIME_T_TO_TIME_T(plugin_get_interval());
634 if (cb->post_timeout == 0)
635 //setting default timeout to plugin interval.
636 cb->post_timeout = CDTIME_T_TO_TIME_T(plugin_get_interval());
638 /* Determine send_buffer_size. */
639 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
640 if (buffer_size >= 1024)
641 cb->send_buffer_size = (size_t) buffer_size;
642 else if (buffer_size != 0)
643 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
646 /* Allocate the buffer. */
647 cb->send_buffer = malloc (cb->send_buffer_size);
648 if (cb->send_buffer == NULL)
650 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
651 wh_callback_free (cb);
654 /* Nulls the buffer and sets ..._free and ..._fill. */
655 wh_reset_buffer (cb);
657 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
659 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
660 callback_name, cb->location);
662 memset (&user_data, 0, sizeof (user_data));
664 user_data.free_func = NULL;
665 plugin_register_flush (callback_name, wh_flush, &user_data);
667 user_data.free_func = wh_callback_free;
668 plugin_register_write (callback_name, wh_write, &user_data);
671 } /* }}} int wh_config_node */
673 static int wh_config (oconfig_item_t *ci) /* {{{ */
677 for (i = 0; i < ci->children_num; i++)
679 oconfig_item_t *child = ci->children + i;
681 if (strcasecmp ("Node", child->key) == 0)
682 wh_config_node (child);
683 /* FIXME: Remove this legacy mode in version 6. */
684 else if (strcasecmp ("URL", child->key) == 0) {
685 WARNING ("write_http plugin: Legacy <URL> block found. "
686 "Please use <Node> instead.");
687 wh_config_node (child);
691 ERROR ("write_http plugin: Invalid configuration "
692 "option: %s.", child->key);
697 } /* }}} int wh_config */
699 static int wh_init (void) /* {{{ */
701 /* Call this while collectd is still single-threaded to avoid
702 * initialization issues in libgcrypt. */
703 curl_global_init (CURL_GLOBAL_SSL);
705 } /* }}} int wh_init */
707 void module_register (void) /* {{{ */
709 plugin_register_complex_config ("write_http", wh_config);
710 plugin_register_init ("write_http", wh_init);
711 } /* }}} void module_register */
713 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */