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 = (char *) 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 = malloc (sizeof (*cb));
573 ERROR ("write_http plugin: malloc failed.");
576 memset (cb, 0, sizeof (*cb));
579 cb->format = WH_FORMAT_COMMAND;
580 cb->sslversion = CURL_SSLVERSION_DEFAULT;
581 cb->low_speed_limit = 0;
583 cb->log_http_error = 0;
587 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
589 cf_util_get_string (ci, &cb->name);
591 /* FIXME: Remove this legacy mode in version 6. */
592 if (strcasecmp ("URL", ci->key) == 0)
593 cf_util_get_string (ci, &cb->location);
595 for (i = 0; i < ci->children_num; i++)
597 oconfig_item_t *child = ci->children + i;
599 if (strcasecmp ("URL", child->key) == 0)
600 status = cf_util_get_string (child, &cb->location);
601 else if (strcasecmp ("User", child->key) == 0)
602 status = cf_util_get_string (child, &cb->user);
603 else if (strcasecmp ("Password", child->key) == 0)
604 status = cf_util_get_string (child, &cb->pass);
605 else if (strcasecmp ("VerifyPeer", child->key) == 0)
606 status = cf_util_get_boolean (child, &cb->verify_peer);
607 else if (strcasecmp ("VerifyHost", child->key) == 0)
608 status = cf_util_get_boolean (child, &cb->verify_host);
609 else if (strcasecmp ("CACert", child->key) == 0)
610 status = cf_util_get_string (child, &cb->cacert);
611 else if (strcasecmp ("CAPath", child->key) == 0)
612 status = cf_util_get_string (child, &cb->capath);
613 else if (strcasecmp ("ClientKey", child->key) == 0)
614 status = cf_util_get_string (child, &cb->clientkey);
615 else if (strcasecmp ("ClientCert", child->key) == 0)
616 status = cf_util_get_string (child, &cb->clientcert);
617 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
618 status = cf_util_get_string (child, &cb->clientkeypass);
619 else if (strcasecmp ("SSLVersion", child->key) == 0)
623 status = cf_util_get_string (child, &value);
627 if (value == NULL || strcasecmp ("default", value) == 0)
628 cb->sslversion = CURL_SSLVERSION_DEFAULT;
629 else if (strcasecmp ("SSLv2", value) == 0)
630 cb->sslversion = CURL_SSLVERSION_SSLv2;
631 else if (strcasecmp ("SSLv3", value) == 0)
632 cb->sslversion = CURL_SSLVERSION_SSLv3;
633 else if (strcasecmp ("TLSv1", value) == 0)
634 cb->sslversion = CURL_SSLVERSION_TLSv1;
635 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
636 else if (strcasecmp ("TLSv1_0", value) == 0)
637 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
638 else if (strcasecmp ("TLSv1_1", value) == 0)
639 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
640 else if (strcasecmp ("TLSv1_2", value) == 0)
641 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
645 ERROR ("write_http plugin: Invalid SSLVersion "
646 "option: %s.", value);
652 else if (strcasecmp ("Format", child->key) == 0)
653 status = config_set_format (cb, child);
654 else if (strcasecmp ("StoreRates", child->key) == 0)
655 status = cf_util_get_boolean (child, &cb->store_rates);
656 else if (strcasecmp ("BufferSize", child->key) == 0)
657 status = cf_util_get_int (child, &buffer_size);
658 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
659 status = cf_util_get_int (child, &cb->low_speed_limit);
660 else if (strcasecmp ("Timeout", child->key) == 0)
661 status = cf_util_get_int (child, &cb->timeout);
662 else if (strcasecmp ("LogHttpError", child->key) == 0)
663 status = cf_util_get_boolean (child, &cb->log_http_error);
664 else if (strcasecmp ("Header", child->key) == 0)
665 status = wh_config_append_string ("Header", &cb->headers, child);
668 ERROR ("write_http plugin: Invalid configuration "
669 "option: %s.", child->key);
679 wh_callback_free (cb);
683 if (cb->location == NULL)
685 ERROR ("write_http plugin: no URL defined for instance '%s'",
687 wh_callback_free (cb);
691 if (cb->low_speed_limit > 0)
692 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
694 /* Determine send_buffer_size. */
695 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
696 if (buffer_size >= 1024)
697 cb->send_buffer_size = (size_t) buffer_size;
698 else if (buffer_size != 0)
699 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
702 /* Allocate the buffer. */
703 cb->send_buffer = malloc (cb->send_buffer_size);
704 if (cb->send_buffer == NULL)
706 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
707 wh_callback_free (cb);
710 /* Nulls the buffer and sets ..._free and ..._fill. */
711 wh_reset_buffer (cb);
713 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
715 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
716 callback_name, cb->location);
718 memset (&user_data, 0, sizeof (user_data));
720 user_data.free_func = NULL;
721 plugin_register_flush (callback_name, wh_flush, &user_data);
723 user_data.free_func = wh_callback_free;
724 plugin_register_write (callback_name, wh_write, &user_data);
727 } /* }}} int wh_config_node */
729 static int wh_config (oconfig_item_t *ci) /* {{{ */
733 for (i = 0; i < ci->children_num; i++)
735 oconfig_item_t *child = ci->children + i;
737 if (strcasecmp ("Node", child->key) == 0)
738 wh_config_node (child);
739 /* FIXME: Remove this legacy mode in version 6. */
740 else if (strcasecmp ("URL", child->key) == 0) {
741 WARNING ("write_http plugin: Legacy <URL> block found. "
742 "Please use <Node> instead.");
743 wh_config_node (child);
747 ERROR ("write_http plugin: Invalid configuration "
748 "option: %s.", child->key);
753 } /* }}} int wh_config */
755 static int wh_init (void) /* {{{ */
757 /* Call this while collectd is still single-threaded to avoid
758 * initialization issues in libgcrypt. */
759 curl_global_init (CURL_GLOBAL_SSL);
761 } /* }}} int wh_init */
763 void module_register (void) /* {{{ */
765 plugin_register_complex_config ("write_http", wh_config);
766 plugin_register_init ("write_http", wh_init);
767 } /* }}} void module_register */
769 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */