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>
30 #include "utils_format_json.h"
31 #include "utils_format_kairosdb.h"
33 #include <curl/curl.h>
35 #ifndef WRITE_HTTP_DEFAULT_BUFFER_SIZE
36 #define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096
42 struct wh_callback_s {
60 time_t low_speed_time;
63 #define WH_FORMAT_COMMAND 0
64 #define WH_FORMAT_JSON 1
65 #define WH_FORMAT_KAIROSDB 2
68 _Bool send_notifications;
71 struct curl_slist *headers;
72 char curl_errbuf[CURL_ERROR_SIZE];
75 size_t send_buffer_size;
76 size_t send_buffer_free;
77 size_t send_buffer_fill;
78 cdtime_t send_buffer_init_time;
80 pthread_mutex_t send_lock;
82 typedef struct wh_callback_s wh_callback_t;
84 static void wh_log_http_error(wh_callback_t *cb) {
85 if (!cb->log_http_error)
90 curl_easy_getinfo(cb->curl, CURLINFO_RESPONSE_CODE, &http_code);
93 INFO("write_http plugin: HTTP Error code: %lu", http_code);
96 static void wh_reset_buffer(wh_callback_t *cb) /* {{{ */
98 if ((cb == NULL) || (cb->send_buffer == NULL))
101 memset(cb->send_buffer, 0, cb->send_buffer_size);
102 cb->send_buffer_free = cb->send_buffer_size;
103 cb->send_buffer_fill = 0;
104 cb->send_buffer_init_time = cdtime();
106 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) {
107 format_json_initialize(cb->send_buffer, &cb->send_buffer_fill,
108 &cb->send_buffer_free);
110 } /* }}} wh_reset_buffer */
112 /* must hold cb->send_lock when calling */
113 static int wh_post_nolock(wh_callback_t *cb, char const *data) /* {{{ */
117 curl_easy_setopt(cb->curl, CURLOPT_URL, cb->location);
118 curl_easy_setopt(cb->curl, CURLOPT_POSTFIELDS, data);
119 status = curl_easy_perform(cb->curl);
121 wh_log_http_error(cb);
123 if (status != CURLE_OK) {
124 ERROR("write_http plugin: curl_easy_perform failed with "
126 status, cb->curl_errbuf);
129 } /* }}} wh_post_nolock */
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) {
138 ERROR("curl plugin: curl_easy_init failed.");
142 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0) {
143 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_LIMIT,
144 (long)(cb->low_speed_limit * cb->low_speed_time));
145 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_TIME,
146 (long)cb->low_speed_time);
149 #ifdef HAVE_CURLOPT_TIMEOUT_MS
151 curl_easy_setopt(cb->curl, CURLOPT_TIMEOUT_MS, (long)cb->timeout);
154 curl_easy_setopt(cb->curl, CURLOPT_NOSIGNAL, 1L);
155 curl_easy_setopt(cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
157 cb->headers = curl_slist_append(cb->headers, "Accept: */*");
158 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
160 curl_slist_append(cb->headers, "Content-Type: application/json");
162 cb->headers = curl_slist_append(cb->headers, "Content-Type: text/plain");
163 cb->headers = curl_slist_append(cb->headers, "Expect:");
164 curl_easy_setopt(cb->curl, CURLOPT_HTTPHEADER, cb->headers);
166 curl_easy_setopt(cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
167 curl_easy_setopt(cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
168 curl_easy_setopt(cb->curl, CURLOPT_MAXREDIRS, 50L);
170 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) {
184 ERROR("curl plugin: malloc failed.");
188 ssnprintf(cb->credentials, credentials_size, "%s:%s", cb->user,
189 (cb->pass == NULL) ? "" : cb->pass);
190 curl_easy_setopt(cb->curl, CURLOPT_USERPWD, cb->credentials);
192 curl_easy_setopt(cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
195 curl_easy_setopt(cb->curl, CURLOPT_SSL_VERIFYPEER, (long)cb->verify_peer);
196 curl_easy_setopt(cb->curl, CURLOPT_SSL_VERIFYHOST, cb->verify_host ? 2L : 0L);
197 curl_easy_setopt(cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
198 if (cb->cacert != NULL)
199 curl_easy_setopt(cb->curl, CURLOPT_CAINFO, cb->cacert);
200 if (cb->capath != NULL)
201 curl_easy_setopt(cb->curl, CURLOPT_CAPATH, cb->capath);
203 if (cb->clientkey != NULL && cb->clientcert != NULL) {
204 curl_easy_setopt(cb->curl, CURLOPT_SSLKEY, cb->clientkey);
205 curl_easy_setopt(cb->curl, CURLOPT_SSLCERT, cb->clientcert);
207 if (cb->clientkeypass != NULL)
208 curl_easy_setopt(cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
214 } /* }}} int wh_callback_init */
216 static int wh_flush_nolock(cdtime_t timeout, wh_callback_t *cb) /* {{{ */
220 DEBUG("write_http plugin: wh_flush_nolock: timeout = %.3f; "
221 "send_buffer_fill = %zu;",
222 CDTIME_T_TO_DOUBLE(timeout), cb->send_buffer_fill);
224 /* timeout == 0 => flush unconditionally */
229 if ((cb->send_buffer_init_time + timeout) > now)
233 if (cb->format == WH_FORMAT_COMMAND) {
234 if (cb->send_buffer_fill == 0) {
235 cb->send_buffer_init_time = cdtime();
239 status = wh_post_nolock(cb, cb->send_buffer);
241 } else if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) {
242 if (cb->send_buffer_fill <= 2) {
243 cb->send_buffer_init_time = cdtime();
247 status = format_json_finalize(cb->send_buffer, &cb->send_buffer_fill,
248 &cb->send_buffer_free);
250 ERROR("write_http: wh_flush_nolock: "
251 "format_json_finalize failed.");
256 status = wh_post_nolock(cb, cb->send_buffer);
259 ERROR("write_http: wh_flush_nolock: "
260 "Unknown format: %i",
266 } /* }}} wh_flush_nolock */
268 static int wh_flush(cdtime_t timeout, /* {{{ */
269 const char *identifier __attribute__((unused)),
270 user_data_t *user_data) {
274 if (user_data == NULL)
277 cb = user_data->data;
279 pthread_mutex_lock(&cb->send_lock);
281 if (wh_callback_init(cb) != 0) {
282 ERROR("write_http plugin: wh_callback_init failed.");
283 pthread_mutex_unlock(&cb->send_lock);
287 status = wh_flush_nolock(timeout, cb);
288 pthread_mutex_unlock(&cb->send_lock);
291 } /* }}} int wh_flush */
293 static void wh_callback_free(void *data) /* {{{ */
302 if (cb->send_buffer != NULL)
303 wh_flush_nolock(/* timeout = */ 0, cb);
305 if (cb->curl != NULL) {
306 curl_easy_cleanup(cb->curl);
310 if (cb->headers != NULL) {
311 curl_slist_free_all(cb->headers);
319 sfree(cb->credentials);
322 sfree(cb->clientkey);
323 sfree(cb->clientcert);
324 sfree(cb->clientkeypass);
325 sfree(cb->send_buffer);
328 } /* }}} void wh_callback_free */
330 static int wh_write_command(const data_set_t *ds,
331 const value_list_t *vl, /* {{{ */
333 char key[10 * DATA_MAX_NAME_LEN];
340 /* sanity checks, primarily to make static analyzers happy. */
341 if ((cb == NULL) || (cb->send_buffer == NULL))
344 if (strcmp(ds->type, vl->type) != 0) {
345 ERROR("write_http plugin: DS type does not match "
350 /* Copy the identifier to `key' and escape it. */
351 status = FORMAT_VL(key, sizeof(key), vl);
353 ERROR("write_http plugin: error with format_name");
356 escape_string(key, sizeof(key));
358 /* Convert the values to an ASCII representation and put that into
360 status = format_values(values, sizeof(values), ds, vl, cb->store_rates);
362 ERROR("write_http plugin: error with "
363 "wh_value_list_to_string");
367 command_len = (size_t)ssnprintf(command, sizeof(command),
368 "PUTVAL %s interval=%.3f %s\r\n", key,
369 CDTIME_T_TO_DOUBLE(vl->interval), values);
370 if (command_len >= sizeof(command)) {
371 ERROR("write_http plugin: Command buffer too small: "
377 pthread_mutex_lock(&cb->send_lock);
378 if (wh_callback_init(cb) != 0) {
379 ERROR("write_http plugin: wh_callback_init failed.");
380 pthread_mutex_unlock(&cb->send_lock);
384 if (command_len >= cb->send_buffer_free) {
385 status = wh_flush_nolock(/* timeout = */ 0, cb);
387 pthread_mutex_unlock(&cb->send_lock);
391 assert(command_len < cb->send_buffer_free);
393 /* `command_len + 1' because `command_len' does not include the
394 * trailing null byte. Neither does `send_buffer_fill'. */
395 memcpy(cb->send_buffer + cb->send_buffer_fill, command, command_len + 1);
396 cb->send_buffer_fill += command_len;
397 cb->send_buffer_free -= command_len;
399 DEBUG("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"", cb->location,
400 cb->send_buffer_fill, cb->send_buffer_size,
401 100.0 * ((double)cb->send_buffer_fill) / ((double)cb->send_buffer_size),
404 /* Check if we have enough space for this command. */
405 pthread_mutex_unlock(&cb->send_lock);
408 } /* }}} int wh_write_command */
410 static int wh_write_json(const data_set_t *ds, const value_list_t *vl, /* {{{ */
414 pthread_mutex_lock(&cb->send_lock);
415 if (wh_callback_init(cb) != 0) {
416 ERROR("write_http plugin: wh_callback_init failed.");
417 pthread_mutex_unlock(&cb->send_lock);
422 format_json_value_list(cb->send_buffer, &cb->send_buffer_fill,
423 &cb->send_buffer_free, ds, vl, cb->store_rates);
424 if (status == -ENOMEM) {
425 status = wh_flush_nolock(/* timeout = */ 0, cb);
428 pthread_mutex_unlock(&cb->send_lock);
433 format_json_value_list(cb->send_buffer, &cb->send_buffer_fill,
434 &cb->send_buffer_free, ds, vl, cb->store_rates);
437 pthread_mutex_unlock(&cb->send_lock);
441 DEBUG("write_http plugin: <%s> buffer %zu/%zu (%g%%)", cb->location,
442 cb->send_buffer_fill, cb->send_buffer_size,
443 100.0 * ((double)cb->send_buffer_fill) /
444 ((double)cb->send_buffer_size));
446 /* Check if we have enough space for this command. */
447 pthread_mutex_unlock(&cb->send_lock);
450 } /* }}} int wh_write_json */
452 static int wh_write_kairosdb(const data_set_t *ds,
453 const value_list_t *vl, /* {{{ */
457 pthread_mutex_lock(&cb->send_lock);
459 if (cb->curl == NULL) {
460 status = wh_callback_init(cb);
462 ERROR("write_http plugin: wh_callback_init failed.");
463 pthread_mutex_unlock(&cb->send_lock);
468 status = format_kairosdb_value_list(cb->send_buffer, &cb->send_buffer_fill,
469 &cb->send_buffer_free, ds, vl,
471 if (status == -ENOMEM) {
472 status = wh_flush_nolock(/* timeout = */ 0, cb);
475 pthread_mutex_unlock(&cb->send_lock);
479 status = format_kairosdb_value_list(cb->send_buffer, &cb->send_buffer_fill,
480 &cb->send_buffer_free, ds, vl,
484 pthread_mutex_unlock(&cb->send_lock);
488 DEBUG("write_http plugin: <%s> buffer %zu/%zu (%g%%)", cb->location,
489 cb->send_buffer_fill, cb->send_buffer_size,
490 100.0 * ((double)cb->send_buffer_fill) /
491 ((double)cb->send_buffer_size));
493 /* Check if we have enough space for this command. */
494 pthread_mutex_unlock(&cb->send_lock);
497 } /* }}} int wh_write_kairosdb */
499 static int wh_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
500 user_data_t *user_data) {
504 if (user_data == NULL)
507 cb = user_data->data;
508 assert(cb->send_metrics);
510 switch (cb->format) {
512 status = wh_write_json(ds, vl, cb);
514 case WH_FORMAT_KAIROSDB:
515 status = wh_write_kairosdb(ds, vl, cb);
518 status = wh_write_command(ds, vl, cb);
522 } /* }}} int wh_write */
524 static int wh_notify(notification_t const *n, user_data_t *ud) /* {{{ */
530 if ((ud == NULL) || (ud->data == NULL))
534 assert(cb->send_notifications);
536 status = format_json_notification(alert, sizeof(alert), n);
538 ERROR("write_http plugin: formatting notification failed");
542 pthread_mutex_lock(&cb->send_lock);
543 if (wh_callback_init(cb) != 0) {
544 ERROR("write_http plugin: wh_callback_init failed.");
545 pthread_mutex_unlock(&cb->send_lock);
549 status = wh_post_nolock(cb, alert);
550 pthread_mutex_unlock(&cb->send_lock);
553 } /* }}} int wh_notify */
555 static int config_set_format(wh_callback_t *cb, /* {{{ */
556 oconfig_item_t *ci) {
559 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
560 WARNING("write_http plugin: The `%s' config option "
561 "needs exactly one string argument.",
566 string = ci->values[0].value.string;
567 if (strcasecmp("Command", string) == 0)
568 cb->format = WH_FORMAT_COMMAND;
569 else if (strcasecmp("JSON", string) == 0)
570 cb->format = WH_FORMAT_JSON;
571 else if (strcasecmp("KAIROSDB", string) == 0)
572 cb->format = WH_FORMAT_KAIROSDB;
574 ERROR("write_http plugin: Invalid format string: %s", string);
579 } /* }}} int config_set_format */
581 static int wh_config_append_string(const char *name,
582 struct curl_slist **dest, /* {{{ */
583 oconfig_item_t *ci) {
584 struct curl_slist *temp = NULL;
585 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
586 WARNING("write_http plugin: `%s' needs exactly one string argument.", name);
590 temp = curl_slist_append(*dest, ci->values[0].value.string);
597 } /* }}} int wh_config_append_string */
599 static int wh_config_node(oconfig_item_t *ci) /* {{{ */
603 char callback_name[DATA_MAX_NAME_LEN];
606 cb = calloc(1, sizeof(*cb));
608 ERROR("write_http plugin: calloc failed.");
613 cb->format = WH_FORMAT_COMMAND;
614 cb->sslversion = CURL_SSLVERSION_DEFAULT;
615 cb->low_speed_limit = 0;
617 cb->log_http_error = 0;
619 cb->send_metrics = 1;
620 cb->send_notifications = 0;
622 pthread_mutex_init(&cb->send_lock, /* attr = */ NULL);
624 cf_util_get_string(ci, &cb->name);
626 /* FIXME: Remove this legacy mode in version 6. */
627 if (strcasecmp("URL", ci->key) == 0)
628 cf_util_get_string(ci, &cb->location);
630 for (int i = 0; i < ci->children_num; i++) {
631 oconfig_item_t *child = ci->children + i;
633 if (strcasecmp("URL", child->key) == 0)
634 status = cf_util_get_string(child, &cb->location);
635 else if (strcasecmp("User", child->key) == 0)
636 status = cf_util_get_string(child, &cb->user);
637 else if (strcasecmp("Password", child->key) == 0)
638 status = cf_util_get_string(child, &cb->pass);
639 else if (strcasecmp("VerifyPeer", child->key) == 0)
640 status = cf_util_get_boolean(child, &cb->verify_peer);
641 else if (strcasecmp("VerifyHost", child->key) == 0)
642 status = cf_util_get_boolean(child, &cb->verify_host);
643 else if (strcasecmp("CACert", child->key) == 0)
644 status = cf_util_get_string(child, &cb->cacert);
645 else if (strcasecmp("CAPath", child->key) == 0)
646 status = cf_util_get_string(child, &cb->capath);
647 else if (strcasecmp("ClientKey", child->key) == 0)
648 status = cf_util_get_string(child, &cb->clientkey);
649 else if (strcasecmp("ClientCert", child->key) == 0)
650 status = cf_util_get_string(child, &cb->clientcert);
651 else if (strcasecmp("ClientKeyPass", child->key) == 0)
652 status = cf_util_get_string(child, &cb->clientkeypass);
653 else if (strcasecmp("SSLVersion", child->key) == 0) {
656 status = cf_util_get_string(child, &value);
660 if (value == NULL || strcasecmp("default", value) == 0)
661 cb->sslversion = CURL_SSLVERSION_DEFAULT;
662 else if (strcasecmp("SSLv2", value) == 0)
663 cb->sslversion = CURL_SSLVERSION_SSLv2;
664 else if (strcasecmp("SSLv3", value) == 0)
665 cb->sslversion = CURL_SSLVERSION_SSLv3;
666 else if (strcasecmp("TLSv1", value) == 0)
667 cb->sslversion = CURL_SSLVERSION_TLSv1;
668 #if (LIBCURL_VERSION_MAJOR > 7) || \
669 (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
670 else if (strcasecmp("TLSv1_0", value) == 0)
671 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
672 else if (strcasecmp("TLSv1_1", value) == 0)
673 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
674 else if (strcasecmp("TLSv1_2", value) == 0)
675 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
678 ERROR("write_http plugin: Invalid SSLVersion "
685 } else if (strcasecmp("Format", child->key) == 0)
686 status = config_set_format(cb, child);
687 else if (strcasecmp("Metrics", child->key) == 0)
688 cf_util_get_boolean(child, &cb->send_metrics);
689 else if (strcasecmp("Notifications", child->key) == 0)
690 cf_util_get_boolean(child, &cb->send_notifications);
691 else if (strcasecmp("StoreRates", child->key) == 0)
692 status = cf_util_get_boolean(child, &cb->store_rates);
693 else if (strcasecmp("BufferSize", child->key) == 0)
694 status = cf_util_get_int(child, &buffer_size);
695 else if (strcasecmp("LowSpeedLimit", child->key) == 0)
696 status = cf_util_get_int(child, &cb->low_speed_limit);
697 else if (strcasecmp("Timeout", child->key) == 0)
698 status = cf_util_get_int(child, &cb->timeout);
699 else if (strcasecmp("LogHttpError", child->key) == 0)
700 status = cf_util_get_boolean(child, &cb->log_http_error);
701 else if (strcasecmp("Header", child->key) == 0)
702 status = wh_config_append_string("Header", &cb->headers, child);
704 ERROR("write_http plugin: Invalid configuration "
715 wh_callback_free(cb);
719 if (cb->location == NULL) {
720 ERROR("write_http plugin: no URL defined for instance '%s'", cb->name);
721 wh_callback_free(cb);
725 if (!cb->send_metrics && !cb->send_notifications) {
726 ERROR("write_http plugin: Neither metrics nor notifications "
727 "are enabled for \"%s\".",
729 wh_callback_free(cb);
733 if (cb->low_speed_limit > 0)
734 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
736 /* Determine send_buffer_size. */
737 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
738 if (buffer_size >= 1024)
739 cb->send_buffer_size = (size_t)buffer_size;
740 else if (buffer_size != 0)
741 ERROR("write_http plugin: Ignoring invalid BufferSize setting (%d).",
744 /* Allocate the buffer. */
745 cb->send_buffer = malloc(cb->send_buffer_size);
746 if (cb->send_buffer == NULL) {
747 ERROR("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
748 wh_callback_free(cb);
751 /* Nulls the buffer and sets ..._free and ..._fill. */
754 ssnprintf(callback_name, sizeof(callback_name), "write_http/%s", cb->name);
755 DEBUG("write_http: Registering write callback '%s' with URL '%s'",
756 callback_name, cb->location);
758 user_data_t user_data = {
759 .data = cb, .free_func = wh_callback_free,
762 if (cb->send_metrics) {
763 plugin_register_write(callback_name, wh_write, &user_data);
764 user_data.free_func = NULL;
766 plugin_register_flush(callback_name, wh_flush, &user_data);
769 if (cb->send_notifications) {
770 plugin_register_notification(callback_name, wh_notify, &user_data);
771 user_data.free_func = NULL;
775 } /* }}} int wh_config_node */
777 static int wh_config(oconfig_item_t *ci) /* {{{ */
779 for (int i = 0; i < ci->children_num; i++) {
780 oconfig_item_t *child = ci->children + i;
782 if (strcasecmp("Node", child->key) == 0)
783 wh_config_node(child);
784 /* FIXME: Remove this legacy mode in version 6. */
785 else if (strcasecmp("URL", child->key) == 0) {
786 WARNING("write_http plugin: Legacy <URL> block found. "
787 "Please use <Node> instead.");
788 wh_config_node(child);
790 ERROR("write_http plugin: Invalid configuration "
797 } /* }}} int wh_config */
799 static int wh_init(void) /* {{{ */
801 /* Call this while collectd is still single-threaded to avoid
802 * initialization issues in libgcrypt. */
803 curl_global_init(CURL_GLOBAL_SSL);
805 } /* }}} int wh_init */
807 void module_register(void) /* {{{ */
809 plugin_register_complex_config("write_http", wh_config);
810 plugin_register_init("write_http", wh_init);
811 } /* }}} void module_register */
813 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */