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
39 #ifndef WRITE_HTTP_DEFAULT_PREFIX
40 #define WRITE_HTTP_DEFAULT_PREFIX "collectd"
46 struct wh_callback_s {
64 time_t low_speed_time;
67 #define WH_FORMAT_COMMAND 0
68 #define WH_FORMAT_JSON 1
69 #define WH_FORMAT_KAIROSDB 2
72 bool send_notifications;
75 struct curl_slist *headers;
76 char curl_errbuf[CURL_ERROR_SIZE];
79 size_t send_buffer_size;
80 size_t send_buffer_free;
81 size_t send_buffer_fill;
82 cdtime_t send_buffer_init_time;
84 pthread_mutex_t send_lock;
89 typedef struct wh_callback_s wh_callback_t;
91 static char **http_attrs;
92 static size_t http_attrs_num;
94 static void wh_log_http_error(wh_callback_t *cb) {
95 if (!cb->log_http_error)
100 curl_easy_getinfo(cb->curl, CURLINFO_RESPONSE_CODE, &http_code);
102 if (http_code != 200)
103 INFO("write_http plugin: HTTP Error code: %lu", http_code);
106 static void wh_reset_buffer(wh_callback_t *cb) /* {{{ */
108 if ((cb == NULL) || (cb->send_buffer == NULL))
111 memset(cb->send_buffer, 0, cb->send_buffer_size);
112 cb->send_buffer_free = cb->send_buffer_size;
113 cb->send_buffer_fill = 0;
114 cb->send_buffer_init_time = cdtime();
116 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) {
117 format_json_initialize(cb->send_buffer, &cb->send_buffer_fill,
118 &cb->send_buffer_free);
120 } /* }}} wh_reset_buffer */
122 /* must hold cb->send_lock when calling */
123 static int wh_post_nolock(wh_callback_t *cb, char const *data) /* {{{ */
127 curl_easy_setopt(cb->curl, CURLOPT_URL, cb->location);
128 curl_easy_setopt(cb->curl, CURLOPT_POSTFIELDS, data);
129 status = curl_easy_perform(cb->curl);
131 wh_log_http_error(cb);
133 if (status != CURLE_OK) {
134 ERROR("write_http plugin: curl_easy_perform failed with "
136 status, cb->curl_errbuf);
139 } /* }}} wh_post_nolock */
141 static int wh_callback_init(wh_callback_t *cb) /* {{{ */
143 if (cb->curl != NULL)
146 cb->curl = curl_easy_init();
147 if (cb->curl == NULL) {
148 ERROR("curl plugin: curl_easy_init failed.");
152 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0) {
153 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_LIMIT,
154 (long)(cb->low_speed_limit * cb->low_speed_time));
155 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_TIME,
156 (long)cb->low_speed_time);
159 #ifdef HAVE_CURLOPT_TIMEOUT_MS
161 curl_easy_setopt(cb->curl, CURLOPT_TIMEOUT_MS, (long)cb->timeout);
164 curl_easy_setopt(cb->curl, CURLOPT_NOSIGNAL, 1L);
165 curl_easy_setopt(cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
167 cb->headers = curl_slist_append(cb->headers, "Accept: */*");
168 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
170 curl_slist_append(cb->headers, "Content-Type: application/json");
172 cb->headers = curl_slist_append(cb->headers, "Content-Type: text/plain");
173 cb->headers = curl_slist_append(cb->headers, "Expect:");
174 curl_easy_setopt(cb->curl, CURLOPT_HTTPHEADER, cb->headers);
176 curl_easy_setopt(cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
177 curl_easy_setopt(cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
178 curl_easy_setopt(cb->curl, CURLOPT_MAXREDIRS, 50L);
180 if (cb->user != NULL) {
181 #ifdef HAVE_CURLOPT_USERNAME
182 curl_easy_setopt(cb->curl, CURLOPT_USERNAME, cb->user);
183 curl_easy_setopt(cb->curl, CURLOPT_PASSWORD,
184 (cb->pass == NULL) ? "" : cb->pass);
186 size_t credentials_size;
188 credentials_size = strlen(cb->user) + 2;
189 if (cb->pass != NULL)
190 credentials_size += strlen(cb->pass);
192 cb->credentials = malloc(credentials_size);
193 if (cb->credentials == NULL) {
194 ERROR("curl plugin: malloc failed.");
198 snprintf(cb->credentials, credentials_size, "%s:%s", cb->user,
199 (cb->pass == NULL) ? "" : cb->pass);
200 curl_easy_setopt(cb->curl, CURLOPT_USERPWD, cb->credentials);
202 curl_easy_setopt(cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
205 curl_easy_setopt(cb->curl, CURLOPT_SSL_VERIFYPEER, (long)cb->verify_peer);
206 curl_easy_setopt(cb->curl, CURLOPT_SSL_VERIFYHOST, cb->verify_host ? 2L : 0L);
207 curl_easy_setopt(cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
208 if (cb->cacert != NULL)
209 curl_easy_setopt(cb->curl, CURLOPT_CAINFO, cb->cacert);
210 if (cb->capath != NULL)
211 curl_easy_setopt(cb->curl, CURLOPT_CAPATH, cb->capath);
213 if (cb->clientkey != NULL && cb->clientcert != NULL) {
214 curl_easy_setopt(cb->curl, CURLOPT_SSLKEY, cb->clientkey);
215 curl_easy_setopt(cb->curl, CURLOPT_SSLCERT, cb->clientcert);
217 if (cb->clientkeypass != NULL)
218 curl_easy_setopt(cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
224 } /* }}} int wh_callback_init */
226 static int wh_flush_nolock(cdtime_t timeout, wh_callback_t *cb) /* {{{ */
230 DEBUG("write_http plugin: wh_flush_nolock: timeout = %.3f; "
231 "send_buffer_fill = %" PRIsz ";",
232 CDTIME_T_TO_DOUBLE(timeout), cb->send_buffer_fill);
234 /* timeout == 0 => flush unconditionally */
239 if ((cb->send_buffer_init_time + timeout) > now)
243 if (cb->format == WH_FORMAT_COMMAND) {
244 if (cb->send_buffer_fill == 0) {
245 cb->send_buffer_init_time = cdtime();
249 status = wh_post_nolock(cb, cb->send_buffer);
251 } else if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) {
252 if (cb->send_buffer_fill <= 2) {
253 cb->send_buffer_init_time = cdtime();
257 status = format_json_finalize(cb->send_buffer, &cb->send_buffer_fill,
258 &cb->send_buffer_free);
260 ERROR("write_http: wh_flush_nolock: "
261 "format_json_finalize failed.");
266 status = wh_post_nolock(cb, cb->send_buffer);
269 ERROR("write_http: wh_flush_nolock: "
270 "Unknown format: %i",
276 } /* }}} wh_flush_nolock */
278 static int wh_flush(cdtime_t timeout, /* {{{ */
279 const char *identifier __attribute__((unused)),
280 user_data_t *user_data) {
284 if (user_data == NULL)
287 cb = user_data->data;
289 pthread_mutex_lock(&cb->send_lock);
291 if (wh_callback_init(cb) != 0) {
292 ERROR("write_http plugin: wh_callback_init failed.");
293 pthread_mutex_unlock(&cb->send_lock);
297 status = wh_flush_nolock(timeout, cb);
298 pthread_mutex_unlock(&cb->send_lock);
301 } /* }}} int wh_flush */
303 static void wh_callback_free(void *data) /* {{{ */
312 if (cb->send_buffer != NULL)
313 wh_flush_nolock(/* timeout = */ 0, cb);
315 if (cb->curl != NULL) {
316 curl_easy_cleanup(cb->curl);
320 if (cb->headers != NULL) {
321 curl_slist_free_all(cb->headers);
329 sfree(cb->credentials);
332 sfree(cb->clientkey);
333 sfree(cb->clientcert);
334 sfree(cb->clientkeypass);
335 sfree(cb->send_buffer);
336 sfree(cb->metrics_prefix);
339 } /* }}} void wh_callback_free */
341 static int wh_write_command(const data_set_t *ds,
342 const value_list_t *vl, /* {{{ */
344 char key[10 * DATA_MAX_NAME_LEN];
351 /* sanity checks, primarily to make static analyzers happy. */
352 if ((cb == NULL) || (cb->send_buffer == NULL))
355 if (strcmp(ds->type, vl->type) != 0) {
356 ERROR("write_http plugin: DS type does not match "
361 /* Copy the identifier to `key' and escape it. */
362 status = FORMAT_VL(key, sizeof(key), vl);
364 ERROR("write_http plugin: error with format_name");
367 escape_string(key, sizeof(key));
369 /* Convert the values to an ASCII representation and put that into
371 status = format_values(values, sizeof(values), ds, vl, cb->store_rates);
373 ERROR("write_http plugin: error with "
374 "wh_value_list_to_string");
378 command_len = (size_t)snprintf(command, sizeof(command),
379 "PUTVAL %s interval=%.3f %s\r\n", key,
380 CDTIME_T_TO_DOUBLE(vl->interval), values);
381 if (command_len >= sizeof(command)) {
382 ERROR("write_http plugin: Command buffer too small: "
383 "Need %" PRIsz " bytes.",
388 pthread_mutex_lock(&cb->send_lock);
389 if (wh_callback_init(cb) != 0) {
390 ERROR("write_http plugin: wh_callback_init failed.");
391 pthread_mutex_unlock(&cb->send_lock);
395 if (command_len >= cb->send_buffer_free) {
396 status = wh_flush_nolock(/* timeout = */ 0, cb);
398 pthread_mutex_unlock(&cb->send_lock);
402 assert(command_len < cb->send_buffer_free);
404 /* Make scan-build happy. */
405 assert(cb->send_buffer != NULL);
407 /* `command_len + 1' because `command_len' does not include the
408 * trailing null byte. Neither does `send_buffer_fill'. */
409 memcpy(cb->send_buffer + cb->send_buffer_fill, command, command_len + 1);
410 cb->send_buffer_fill += command_len;
411 cb->send_buffer_free -= command_len;
413 DEBUG("write_http plugin: <%s> buffer %" PRIsz "/%" PRIsz " (%g%%) \"%s\"",
414 cb->location, cb->send_buffer_fill, cb->send_buffer_size,
415 100.0 * ((double)cb->send_buffer_fill) / ((double)cb->send_buffer_size),
418 /* Check if we have enough space for this command. */
419 pthread_mutex_unlock(&cb->send_lock);
422 } /* }}} int wh_write_command */
424 static int wh_write_json(const data_set_t *ds, const value_list_t *vl, /* {{{ */
428 pthread_mutex_lock(&cb->send_lock);
429 if (wh_callback_init(cb) != 0) {
430 ERROR("write_http plugin: wh_callback_init failed.");
431 pthread_mutex_unlock(&cb->send_lock);
436 format_json_value_list(cb->send_buffer, &cb->send_buffer_fill,
437 &cb->send_buffer_free, ds, vl, cb->store_rates);
438 if (status == -ENOMEM) {
439 status = wh_flush_nolock(/* timeout = */ 0, cb);
442 pthread_mutex_unlock(&cb->send_lock);
447 format_json_value_list(cb->send_buffer, &cb->send_buffer_fill,
448 &cb->send_buffer_free, ds, vl, cb->store_rates);
451 pthread_mutex_unlock(&cb->send_lock);
455 DEBUG("write_http plugin: <%s> buffer %" PRIsz "/%" PRIsz " (%g%%)",
456 cb->location, cb->send_buffer_fill, cb->send_buffer_size,
457 100.0 * ((double)cb->send_buffer_fill) /
458 ((double)cb->send_buffer_size));
460 /* Check if we have enough space for this command. */
461 pthread_mutex_unlock(&cb->send_lock);
464 } /* }}} int wh_write_json */
466 static int wh_write_kairosdb(const data_set_t *ds,
467 const value_list_t *vl, /* {{{ */
471 pthread_mutex_lock(&cb->send_lock);
473 if (cb->curl == NULL) {
474 status = wh_callback_init(cb);
476 ERROR("write_http plugin: wh_callback_init failed.");
477 pthread_mutex_unlock(&cb->send_lock);
482 status = format_kairosdb_value_list(
483 cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, ds, vl,
484 cb->store_rates, (char const *const *)http_attrs, http_attrs_num,
485 cb->data_ttl, cb->metrics_prefix);
486 if (status == -ENOMEM) {
487 status = wh_flush_nolock(/* timeout = */ 0, cb);
490 pthread_mutex_unlock(&cb->send_lock);
494 status = format_kairosdb_value_list(
495 cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, ds, vl,
496 cb->store_rates, (char const *const *)http_attrs, http_attrs_num,
497 cb->data_ttl, cb->metrics_prefix);
500 pthread_mutex_unlock(&cb->send_lock);
504 DEBUG("write_http plugin: <%s> buffer %" PRIsz "/%" PRIsz " (%g%%)",
505 cb->location, cb->send_buffer_fill, cb->send_buffer_size,
506 100.0 * ((double)cb->send_buffer_fill) /
507 ((double)cb->send_buffer_size));
509 /* Check if we have enough space for this command. */
510 pthread_mutex_unlock(&cb->send_lock);
513 } /* }}} int wh_write_kairosdb */
515 static int wh_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
516 user_data_t *user_data) {
520 if (user_data == NULL)
523 cb = user_data->data;
524 assert(cb->send_metrics);
526 switch (cb->format) {
528 status = wh_write_json(ds, vl, cb);
530 case WH_FORMAT_KAIROSDB:
531 status = wh_write_kairosdb(ds, vl, cb);
534 status = wh_write_command(ds, vl, cb);
538 } /* }}} int wh_write */
540 static int wh_notify(notification_t const *n, user_data_t *ud) /* {{{ */
546 if ((ud == NULL) || (ud->data == NULL))
550 assert(cb->send_notifications);
552 status = format_json_notification(alert, sizeof(alert), n);
554 ERROR("write_http plugin: formatting notification failed");
558 pthread_mutex_lock(&cb->send_lock);
559 if (wh_callback_init(cb) != 0) {
560 ERROR("write_http plugin: wh_callback_init failed.");
561 pthread_mutex_unlock(&cb->send_lock);
565 status = wh_post_nolock(cb, alert);
566 pthread_mutex_unlock(&cb->send_lock);
569 } /* }}} int wh_notify */
571 static int config_set_format(wh_callback_t *cb, /* {{{ */
572 oconfig_item_t *ci) {
575 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
576 WARNING("write_http plugin: The `%s' config option "
577 "needs exactly one string argument.",
582 string = ci->values[0].value.string;
583 if (strcasecmp("Command", string) == 0)
584 cb->format = WH_FORMAT_COMMAND;
585 else if (strcasecmp("JSON", string) == 0)
586 cb->format = WH_FORMAT_JSON;
587 else if (strcasecmp("KAIROSDB", string) == 0)
588 cb->format = WH_FORMAT_KAIROSDB;
590 ERROR("write_http plugin: Invalid format string: %s", string);
595 } /* }}} int config_set_format */
597 static int wh_config_append_string(const char *name,
598 struct curl_slist **dest, /* {{{ */
599 oconfig_item_t *ci) {
600 struct curl_slist *temp = NULL;
601 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
602 WARNING("write_http plugin: `%s' needs exactly one string argument.", name);
606 temp = curl_slist_append(*dest, ci->values[0].value.string);
613 } /* }}} int wh_config_append_string */
615 static int wh_config_node(oconfig_item_t *ci) /* {{{ */
619 char callback_name[DATA_MAX_NAME_LEN];
622 cb = calloc(1, sizeof(*cb));
624 ERROR("write_http plugin: calloc failed.");
627 cb->verify_peer = true;
628 cb->verify_host = true;
629 cb->format = WH_FORMAT_COMMAND;
630 cb->sslversion = CURL_SSLVERSION_DEFAULT;
631 cb->low_speed_limit = 0;
633 cb->log_http_error = false;
635 cb->send_metrics = true;
636 cb->send_notifications = false;
638 cb->metrics_prefix = strdup(WRITE_HTTP_DEFAULT_PREFIX);
640 if (cb->metrics_prefix == NULL) {
641 ERROR("write_http plugin: strdup failed.");
646 pthread_mutex_init(&cb->send_lock, /* attr = */ NULL);
648 cf_util_get_string(ci, &cb->name);
650 /* FIXME: Remove this legacy mode in version 6. */
651 if (strcasecmp("URL", ci->key) == 0)
652 cf_util_get_string(ci, &cb->location);
654 for (int i = 0; i < ci->children_num; i++) {
655 oconfig_item_t *child = ci->children + i;
657 if (strcasecmp("URL", child->key) == 0)
658 status = cf_util_get_string(child, &cb->location);
659 else if (strcasecmp("User", child->key) == 0)
660 status = cf_util_get_string(child, &cb->user);
661 else if (strcasecmp("Password", child->key) == 0)
662 status = cf_util_get_string(child, &cb->pass);
663 else if (strcasecmp("VerifyPeer", child->key) == 0)
664 status = cf_util_get_boolean(child, &cb->verify_peer);
665 else if (strcasecmp("VerifyHost", child->key) == 0)
666 status = cf_util_get_boolean(child, &cb->verify_host);
667 else if (strcasecmp("CACert", child->key) == 0)
668 status = cf_util_get_string(child, &cb->cacert);
669 else if (strcasecmp("CAPath", child->key) == 0)
670 status = cf_util_get_string(child, &cb->capath);
671 else if (strcasecmp("ClientKey", child->key) == 0)
672 status = cf_util_get_string(child, &cb->clientkey);
673 else if (strcasecmp("ClientCert", child->key) == 0)
674 status = cf_util_get_string(child, &cb->clientcert);
675 else if (strcasecmp("ClientKeyPass", child->key) == 0)
676 status = cf_util_get_string(child, &cb->clientkeypass);
677 else if (strcasecmp("SSLVersion", child->key) == 0) {
680 status = cf_util_get_string(child, &value);
684 if (value == NULL || strcasecmp("default", value) == 0)
685 cb->sslversion = CURL_SSLVERSION_DEFAULT;
686 else if (strcasecmp("SSLv2", value) == 0)
687 cb->sslversion = CURL_SSLVERSION_SSLv2;
688 else if (strcasecmp("SSLv3", value) == 0)
689 cb->sslversion = CURL_SSLVERSION_SSLv3;
690 else if (strcasecmp("TLSv1", value) == 0)
691 cb->sslversion = CURL_SSLVERSION_TLSv1;
692 #if (LIBCURL_VERSION_MAJOR > 7) || \
693 (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
694 else if (strcasecmp("TLSv1_0", value) == 0)
695 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
696 else if (strcasecmp("TLSv1_1", value) == 0)
697 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
698 else if (strcasecmp("TLSv1_2", value) == 0)
699 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
702 ERROR("write_http plugin: Invalid SSLVersion "
709 } else if (strcasecmp("Format", child->key) == 0)
710 status = config_set_format(cb, child);
711 else if (strcasecmp("Metrics", child->key) == 0)
712 cf_util_get_boolean(child, &cb->send_metrics);
713 else if (strcasecmp("Notifications", child->key) == 0)
714 cf_util_get_boolean(child, &cb->send_notifications);
715 else if (strcasecmp("StoreRates", child->key) == 0)
716 status = cf_util_get_boolean(child, &cb->store_rates);
717 else if (strcasecmp("BufferSize", child->key) == 0)
718 status = cf_util_get_int(child, &buffer_size);
719 else if (strcasecmp("LowSpeedLimit", child->key) == 0)
720 status = cf_util_get_int(child, &cb->low_speed_limit);
721 else if (strcasecmp("Timeout", child->key) == 0)
722 status = cf_util_get_int(child, &cb->timeout);
723 else if (strcasecmp("LogHttpError", child->key) == 0)
724 status = cf_util_get_boolean(child, &cb->log_http_error);
725 else if (strcasecmp("Header", child->key) == 0)
726 status = wh_config_append_string("Header", &cb->headers, child);
727 else if (strcasecmp("Attribute", child->key) == 0) {
731 if (child->values_num != 2) {
732 WARNING("write_http plugin: Attribute need both a key and a value.");
735 if (child->values[0].type != OCONFIG_TYPE_STRING ||
736 child->values[1].type != OCONFIG_TYPE_STRING) {
737 WARNING("write_http plugin: Attribute needs string arguments.");
740 if ((key = strdup(child->values[0].value.string)) == NULL) {
741 WARNING("cannot allocate memory for attribute key.");
744 if ((val = strdup(child->values[1].value.string)) == NULL) {
745 WARNING("cannot allocate memory for attribute value.");
749 strarray_add(&http_attrs, &http_attrs_num, key);
750 strarray_add(&http_attrs, &http_attrs_num, val);
751 DEBUG("write_http plugin: got attribute: %s => %s", key, val);
754 } else if (strcasecmp("TTL", child->key) == 0) {
755 status = cf_util_get_int(child, &cb->data_ttl);
756 } else if (strcasecmp("Prefix", child->key) == 0) {
757 status = cf_util_get_string(child, &cb->metrics_prefix);
759 ERROR("write_http plugin: Invalid configuration "
770 wh_callback_free(cb);
774 if (cb->location == NULL) {
775 ERROR("write_http plugin: no URL defined for instance '%s'", cb->name);
776 wh_callback_free(cb);
780 if (!cb->send_metrics && !cb->send_notifications) {
781 ERROR("write_http plugin: Neither metrics nor notifications "
782 "are enabled for \"%s\".",
784 wh_callback_free(cb);
788 if (strlen(cb->metrics_prefix) == 0)
789 sfree(cb->metrics_prefix);
791 if (cb->low_speed_limit > 0)
792 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
794 /* Determine send_buffer_size. */
795 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
796 if (buffer_size >= 1024)
797 cb->send_buffer_size = (size_t)buffer_size;
798 else if (buffer_size != 0)
799 ERROR("write_http plugin: Ignoring invalid BufferSize setting (%d).",
802 /* Allocate the buffer. */
803 cb->send_buffer = malloc(cb->send_buffer_size);
804 if (cb->send_buffer == NULL) {
805 ERROR("write_http plugin: malloc(%" PRIsz ") failed.",
806 cb->send_buffer_size);
807 wh_callback_free(cb);
810 /* Nulls the buffer and sets ..._free and ..._fill. */
813 snprintf(callback_name, sizeof(callback_name), "write_http/%s", cb->name);
814 DEBUG("write_http: Registering write callback '%s' with URL '%s'",
815 callback_name, cb->location);
817 user_data_t user_data = {
818 .data = cb, .free_func = wh_callback_free,
821 if (cb->send_metrics) {
822 plugin_register_write(callback_name, wh_write, &user_data);
823 user_data.free_func = NULL;
825 plugin_register_flush(callback_name, wh_flush, &user_data);
828 if (cb->send_notifications) {
829 plugin_register_notification(callback_name, wh_notify, &user_data);
830 user_data.free_func = NULL;
834 } /* }}} int wh_config_node */
836 static int wh_config(oconfig_item_t *ci) /* {{{ */
838 for (int i = 0; i < ci->children_num; i++) {
839 oconfig_item_t *child = ci->children + i;
841 if (strcasecmp("Node", child->key) == 0)
842 wh_config_node(child);
843 /* FIXME: Remove this legacy mode in version 6. */
844 else if (strcasecmp("URL", child->key) == 0) {
845 WARNING("write_http plugin: Legacy <URL> block found. "
846 "Please use <Node> instead.");
847 wh_config_node(child);
849 ERROR("write_http plugin: Invalid configuration "
856 } /* }}} int wh_config */
858 static int wh_init(void) /* {{{ */
860 /* Call this while collectd is still single-threaded to avoid
861 * initialization issues in libgcrypt. */
862 curl_global_init(CURL_GLOBAL_SSL);
864 } /* }}} int wh_init */
866 void module_register(void) /* {{{ */
868 plugin_register_complex_config("write_http", wh_config);
869 plugin_register_init("write_http", wh_init);
870 } /* }}} void module_register */