1 /**
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
6 *
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.
10 *
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.
15 *
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
19 *
20 * Authors:
21 * Florian octo Forster <octo at collectd.org>
22 * Doug MacEachern <dougm@hyperic.com>
23 * Paul Sadauskas <psadauskas@gmail.com>
24 **/
26 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
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
37 #endif
39 /*
40 * Private variables
41 */
42 struct wh_callback_s {
43 char *name;
45 char *location;
46 char *user;
47 char *pass;
48 char *credentials;
49 _Bool verify_peer;
50 _Bool verify_host;
51 char *cacert;
52 char *capath;
53 char *clientkey;
54 char *clientcert;
55 char *clientkeypass;
56 long sslversion;
57 _Bool store_rates;
58 _Bool log_http_error;
59 int low_speed_limit;
60 time_t low_speed_time;
61 int timeout;
63 #define WH_FORMAT_COMMAND 0
64 #define WH_FORMAT_JSON 1
65 #define WH_FORMAT_KAIROSDB 2
66 int format;
67 _Bool send_metrics;
68 _Bool send_notifications;
70 CURL *curl;
71 struct curl_slist *headers;
72 char curl_errbuf[CURL_ERROR_SIZE];
74 char *send_buffer;
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 int data_ttl;
83 };
84 typedef struct wh_callback_s wh_callback_t;
86 static char **http_attrs;
87 static size_t http_attrs_num;
89 static void wh_log_http_error(wh_callback_t *cb) {
90 if (!cb->log_http_error)
91 return;
93 long http_code = 0;
95 curl_easy_getinfo(cb->curl, CURLINFO_RESPONSE_CODE, &http_code);
97 if (http_code != 200)
98 INFO("write_http plugin: HTTP Error code: %lu", http_code);
99 }
101 static void wh_reset_buffer(wh_callback_t *cb) /* {{{ */
102 {
103 if ((cb == NULL) || (cb->send_buffer == NULL))
104 return;
106 memset(cb->send_buffer, 0, cb->send_buffer_size);
107 cb->send_buffer_free = cb->send_buffer_size;
108 cb->send_buffer_fill = 0;
109 cb->send_buffer_init_time = cdtime();
111 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) {
112 format_json_initialize(cb->send_buffer, &cb->send_buffer_fill,
113 &cb->send_buffer_free);
114 }
115 } /* }}} wh_reset_buffer */
117 /* must hold cb->send_lock when calling */
118 static int wh_post_nolock(wh_callback_t *cb, char const *data) /* {{{ */
119 {
120 int status = 0;
122 curl_easy_setopt(cb->curl, CURLOPT_POSTFIELDS, data);
123 status = curl_easy_perform(cb->curl);
125 wh_log_http_error(cb);
127 if (status != CURLE_OK) {
128 ERROR("write_http plugin: curl_easy_perform failed with "
129 "status %i: %s",
130 status, cb->curl_errbuf);
131 }
132 return status;
133 } /* }}} wh_post_nolock */
135 static int wh_callback_init(wh_callback_t *cb) /* {{{ */
136 {
137 if (cb->curl != NULL)
138 return 0;
140 cb->curl = curl_easy_init();
141 if (cb->curl == NULL) {
142 ERROR("curl plugin: curl_easy_init failed.");
143 return -1;
144 }
146 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0) {
147 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_LIMIT,
148 (long)(cb->low_speed_limit * cb->low_speed_time));
149 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_TIME,
150 (long)cb->low_speed_time);
151 }
153 #ifdef HAVE_CURLOPT_TIMEOUT_MS
154 if (cb->timeout > 0)
155 curl_easy_setopt(cb->curl, CURLOPT_TIMEOUT_MS, (long)cb->timeout);
156 #endif
158 curl_easy_setopt(cb->curl, CURLOPT_NOSIGNAL, 1L);
159 curl_easy_setopt(cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
161 cb->headers = curl_slist_append(cb->headers, "Accept: */*");
162 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
163 cb->headers =
164 curl_slist_append(cb->headers, "Content-Type: application/json");
165 else
166 cb->headers = curl_slist_append(cb->headers, "Content-Type: text/plain");
167 cb->headers = curl_slist_append(cb->headers, "Expect:");
168 curl_easy_setopt(cb->curl, CURLOPT_HTTPHEADER, cb->headers);
170 curl_easy_setopt(cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
171 curl_easy_setopt(cb->curl, CURLOPT_URL, cb->location);
172 curl_easy_setopt(cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
173 curl_easy_setopt(cb->curl, CURLOPT_MAXREDIRS, 50L);
175 if (cb->user != NULL) {
176 #ifdef HAVE_CURLOPT_USERNAME
177 curl_easy_setopt(cb->curl, CURLOPT_USERNAME, cb->user);
178 curl_easy_setopt(cb->curl, CURLOPT_PASSWORD,
179 (cb->pass == NULL) ? "" : cb->pass);
180 #else
181 size_t credentials_size;
183 credentials_size = strlen(cb->user) + 2;
184 if (cb->pass != NULL)
185 credentials_size += strlen(cb->pass);
187 cb->credentials = malloc(credentials_size);
188 if (cb->credentials == NULL) {
189 ERROR("curl plugin: malloc failed.");
190 return -1;
191 }
193 ssnprintf(cb->credentials, credentials_size, "%s:%s", cb->user,
194 (cb->pass == NULL) ? "" : cb->pass);
195 curl_easy_setopt(cb->curl, CURLOPT_USERPWD, cb->credentials);
196 #endif
197 curl_easy_setopt(cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
198 }
200 curl_easy_setopt(cb->curl, CURLOPT_SSL_VERIFYPEER, (long)cb->verify_peer);
201 curl_easy_setopt(cb->curl, CURLOPT_SSL_VERIFYHOST, 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) {
209 curl_easy_setopt(cb->curl, CURLOPT_SSLKEY, cb->clientkey);
210 curl_easy_setopt(cb->curl, CURLOPT_SSLCERT, cb->clientcert);
212 if (cb->clientkeypass != NULL)
213 curl_easy_setopt(cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
214 }
216 wh_reset_buffer(cb);
218 return 0;
219 } /* }}} int wh_callback_init */
221 static int wh_flush_nolock(cdtime_t timeout, wh_callback_t *cb) /* {{{ */
222 {
223 int status;
225 DEBUG("write_http plugin: wh_flush_nolock: timeout = %.3f; "
226 "send_buffer_fill = %zu;",
227 CDTIME_T_TO_DOUBLE(timeout), cb->send_buffer_fill);
229 /* timeout == 0 => flush unconditionally */
230 if (timeout > 0) {
231 cdtime_t now;
233 now = cdtime();
234 if ((cb->send_buffer_init_time + timeout) > now)
235 return 0;
236 }
238 if (cb->format == WH_FORMAT_COMMAND) {
239 if (cb->send_buffer_fill == 0) {
240 cb->send_buffer_init_time = cdtime();
241 return 0;
242 }
244 status = wh_post_nolock(cb, cb->send_buffer);
245 wh_reset_buffer(cb);
246 } else if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) {
247 if (cb->send_buffer_fill <= 2) {
248 cb->send_buffer_init_time = cdtime();
249 return 0;
250 }
252 status = format_json_finalize(cb->send_buffer, &cb->send_buffer_fill,
253 &cb->send_buffer_free);
254 if (status != 0) {
255 ERROR("write_http: wh_flush_nolock: "
256 "format_json_finalize failed.");
257 wh_reset_buffer(cb);
258 return status;
259 }
261 status = wh_post_nolock(cb, cb->send_buffer);
262 wh_reset_buffer(cb);
263 } else {
264 ERROR("write_http: wh_flush_nolock: "
265 "Unknown format: %i",
266 cb->format);
267 return -1;
268 }
270 return status;
271 } /* }}} wh_flush_nolock */
273 static int wh_flush(cdtime_t timeout, /* {{{ */
274 const char *identifier __attribute__((unused)),
275 user_data_t *user_data) {
276 wh_callback_t *cb;
277 int status;
279 if (user_data == NULL)
280 return -EINVAL;
282 cb = user_data->data;
284 pthread_mutex_lock(&cb->send_lock);
286 if (wh_callback_init(cb) != 0) {
287 ERROR("write_http plugin: wh_callback_init failed.");
288 pthread_mutex_unlock(&cb->send_lock);
289 return -1;
290 }
292 status = wh_flush_nolock(timeout, cb);
293 pthread_mutex_unlock(&cb->send_lock);
295 return status;
296 } /* }}} int wh_flush */
298 static void wh_callback_free(void *data) /* {{{ */
299 {
300 wh_callback_t *cb;
302 if (data == NULL)
303 return;
305 cb = data;
307 if (cb->send_buffer != NULL)
308 wh_flush_nolock(/* timeout = */ 0, cb);
310 if (cb->curl != NULL) {
311 curl_easy_cleanup(cb->curl);
312 cb->curl = NULL;
313 }
315 if (cb->headers != NULL) {
316 curl_slist_free_all(cb->headers);
317 cb->headers = NULL;
318 }
320 sfree(cb->name);
321 sfree(cb->location);
322 sfree(cb->user);
323 sfree(cb->pass);
324 sfree(cb->credentials);
325 sfree(cb->cacert);
326 sfree(cb->capath);
327 sfree(cb->clientkey);
328 sfree(cb->clientcert);
329 sfree(cb->clientkeypass);
330 sfree(cb->send_buffer);
332 sfree(cb);
333 } /* }}} void wh_callback_free */
335 static int wh_write_command(const data_set_t *ds,
336 const value_list_t *vl, /* {{{ */
337 wh_callback_t *cb) {
338 char key[10 * DATA_MAX_NAME_LEN];
339 char values[512];
340 char command[1024];
341 size_t command_len;
343 int status;
345 /* sanity checks, primarily to make static analyzers happy. */
346 if ((cb == NULL) || (cb->send_buffer == NULL))
347 return -1;
349 if (strcmp(ds->type, vl->type) != 0) {
350 ERROR("write_http plugin: DS type does not match "
351 "value list type");
352 return -1;
353 }
355 /* Copy the identifier to `key' and escape it. */
356 status = FORMAT_VL(key, sizeof(key), vl);
357 if (status != 0) {
358 ERROR("write_http plugin: error with format_name");
359 return status;
360 }
361 escape_string(key, sizeof(key));
363 /* Convert the values to an ASCII representation and put that into
364 * `values'. */
365 status = format_values(values, sizeof(values), ds, vl, cb->store_rates);
366 if (status != 0) {
367 ERROR("write_http plugin: error with "
368 "wh_value_list_to_string");
369 return status;
370 }
372 command_len = (size_t)ssnprintf(command, sizeof(command),
373 "PUTVAL %s interval=%.3f %s\r\n", key,
374 CDTIME_T_TO_DOUBLE(vl->interval), values);
375 if (command_len >= sizeof(command)) {
376 ERROR("write_http plugin: Command buffer too small: "
377 "Need %zu bytes.",
378 command_len + 1);
379 return -1;
380 }
382 pthread_mutex_lock(&cb->send_lock);
383 if (wh_callback_init(cb) != 0) {
384 ERROR("write_http plugin: wh_callback_init failed.");
385 pthread_mutex_unlock(&cb->send_lock);
386 return -1;
387 }
389 if (command_len >= cb->send_buffer_free) {
390 status = wh_flush_nolock(/* timeout = */ 0, cb);
391 if (status != 0) {
392 pthread_mutex_unlock(&cb->send_lock);
393 return status;
394 }
395 }
396 assert(command_len < cb->send_buffer_free);
398 /* Make scan-build happy. */
399 assert(cb->send_buffer != NULL);
401 /* `command_len + 1' because `command_len' does not include the
402 * trailing null byte. Neither does `send_buffer_fill'. */
403 memcpy(cb->send_buffer + cb->send_buffer_fill, 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\"", cb->location,
408 cb->send_buffer_fill, cb->send_buffer_size,
409 100.0 * ((double)cb->send_buffer_fill) / ((double)cb->send_buffer_size),
410 command);
412 /* Check if we have enough space for this command. */
413 pthread_mutex_unlock(&cb->send_lock);
415 return 0;
416 } /* }}} int wh_write_command */
418 static int wh_write_json(const data_set_t *ds, const value_list_t *vl, /* {{{ */
419 wh_callback_t *cb) {
420 int status;
422 pthread_mutex_lock(&cb->send_lock);
423 if (wh_callback_init(cb) != 0) {
424 ERROR("write_http plugin: wh_callback_init failed.");
425 pthread_mutex_unlock(&cb->send_lock);
426 return -1;
427 }
429 status =
430 format_json_value_list(cb->send_buffer, &cb->send_buffer_fill,
431 &cb->send_buffer_free, ds, vl, cb->store_rates);
432 if (status == -ENOMEM) {
433 status = wh_flush_nolock(/* timeout = */ 0, cb);
434 if (status != 0) {
435 wh_reset_buffer(cb);
436 pthread_mutex_unlock(&cb->send_lock);
437 return status;
438 }
440 status =
441 format_json_value_list(cb->send_buffer, &cb->send_buffer_fill,
442 &cb->send_buffer_free, ds, vl, cb->store_rates);
443 }
444 if (status != 0) {
445 pthread_mutex_unlock(&cb->send_lock);
446 return status;
447 }
449 DEBUG("write_http plugin: <%s> buffer %zu/%zu (%g%%)", cb->location,
450 cb->send_buffer_fill, cb->send_buffer_size,
451 100.0 * ((double)cb->send_buffer_fill) /
452 ((double)cb->send_buffer_size));
454 /* Check if we have enough space for this command. */
455 pthread_mutex_unlock(&cb->send_lock);
457 return 0;
458 } /* }}} int wh_write_json */
460 static int wh_write_kairosdb(const data_set_t *ds,
461 const value_list_t *vl, /* {{{ */
462 wh_callback_t *cb) {
463 int status;
465 pthread_mutex_lock(&cb->send_lock);
467 if (cb->curl == NULL) {
468 status = wh_callback_init(cb);
469 if (status != 0) {
470 ERROR("write_http plugin: wh_callback_init failed.");
471 pthread_mutex_unlock(&cb->send_lock);
472 return -1;
473 }
474 }
476 status = format_kairosdb_value_list(
477 cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, ds, vl,
478 cb->store_rates, (char const *const *)http_attrs, http_attrs_num,
479 cb->data_ttl);
480 if (status == -ENOMEM) {
481 status = wh_flush_nolock(/* timeout = */ 0, cb);
482 if (status != 0) {
483 wh_reset_buffer(cb);
484 pthread_mutex_unlock(&cb->send_lock);
485 return status;
486 }
488 status = format_kairosdb_value_list(
489 cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, ds, vl,
490 cb->store_rates, (char const *const *)http_attrs, http_attrs_num,
491 cb->data_ttl);
492 }
493 if (status != 0) {
494 pthread_mutex_unlock(&cb->send_lock);
495 return status;
496 }
498 DEBUG("write_http plugin: <%s> buffer %zu/%zu (%g%%)", cb->location,
499 cb->send_buffer_fill, cb->send_buffer_size,
500 100.0 * ((double)cb->send_buffer_fill) /
501 ((double)cb->send_buffer_size));
503 /* Check if we have enough space for this command. */
504 pthread_mutex_unlock(&cb->send_lock);
506 return 0;
507 } /* }}} int wh_write_kairosdb */
509 static int wh_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
510 user_data_t *user_data) {
511 wh_callback_t *cb;
512 int status;
514 if (user_data == NULL)
515 return -EINVAL;
517 cb = user_data->data;
518 assert(cb->send_metrics);
520 switch (cb->format) {
521 case WH_FORMAT_JSON:
522 status = wh_write_json(ds, vl, cb);
523 break;
524 case WH_FORMAT_KAIROSDB:
525 status = wh_write_kairosdb(ds, vl, cb);
526 break;
527 default:
528 status = wh_write_command(ds, vl, cb);
529 break;
530 }
531 return status;
532 } /* }}} int wh_write */
534 static int wh_notify(notification_t const *n, user_data_t *ud) /* {{{ */
535 {
536 wh_callback_t *cb;
537 char alert[4096];
538 int status;
540 if ((ud == NULL) || (ud->data == NULL))
541 return EINVAL;
543 cb = ud->data;
544 assert(cb->send_notifications);
546 status = format_json_notification(alert, sizeof(alert), n);
547 if (status != 0) {
548 ERROR("write_http plugin: formatting notification failed");
549 return status;
550 }
552 pthread_mutex_lock(&cb->send_lock);
553 if (wh_callback_init(cb) != 0) {
554 ERROR("write_http plugin: wh_callback_init failed.");
555 pthread_mutex_unlock(&cb->send_lock);
556 return -1;
557 }
559 status = wh_post_nolock(cb, alert);
560 pthread_mutex_unlock(&cb->send_lock);
562 return status;
563 } /* }}} int wh_notify */
565 static int config_set_format(wh_callback_t *cb, /* {{{ */
566 oconfig_item_t *ci) {
567 char *string;
569 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
570 WARNING("write_http plugin: The `%s' config option "
571 "needs exactly one string argument.",
572 ci->key);
573 return -1;
574 }
576 string = ci->values[0].value.string;
577 if (strcasecmp("Command", string) == 0)
578 cb->format = WH_FORMAT_COMMAND;
579 else if (strcasecmp("JSON", string) == 0)
580 cb->format = WH_FORMAT_JSON;
581 else if (strcasecmp("KAIROSDB", string) == 0)
582 cb->format = WH_FORMAT_KAIROSDB;
583 else {
584 ERROR("write_http plugin: Invalid format string: %s", string);
585 return -1;
586 }
588 return 0;
589 } /* }}} int config_set_format */
591 static int wh_config_append_string(const char *name,
592 struct curl_slist **dest, /* {{{ */
593 oconfig_item_t *ci) {
594 struct curl_slist *temp = NULL;
595 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
596 WARNING("write_http plugin: `%s' needs exactly one string argument.", name);
597 return -1;
598 }
600 temp = curl_slist_append(*dest, ci->values[0].value.string);
601 if (temp == NULL)
602 return -1;
604 *dest = temp;
606 return 0;
607 } /* }}} int wh_config_append_string */
609 static int wh_config_node(oconfig_item_t *ci) /* {{{ */
610 {
611 wh_callback_t *cb;
612 int buffer_size = 0;
613 char callback_name[DATA_MAX_NAME_LEN];
614 int status = 0;
616 cb = calloc(1, sizeof(*cb));
617 if (cb == NULL) {
618 ERROR("write_http plugin: calloc failed.");
619 return -1;
620 }
621 cb->verify_peer = 1;
622 cb->verify_host = 1;
623 cb->format = WH_FORMAT_COMMAND;
624 cb->sslversion = CURL_SSLVERSION_DEFAULT;
625 cb->low_speed_limit = 0;
626 cb->timeout = 0;
627 cb->log_http_error = 0;
628 cb->headers = NULL;
629 cb->send_metrics = 1;
630 cb->send_notifications = 0;
631 cb->data_ttl = 0;
633 pthread_mutex_init(&cb->send_lock, /* attr = */ NULL);
635 cf_util_get_string(ci, &cb->name);
637 /* FIXME: Remove this legacy mode in version 6. */
638 if (strcasecmp("URL", ci->key) == 0)
639 cf_util_get_string(ci, &cb->location);
641 for (int i = 0; i < ci->children_num; i++) {
642 oconfig_item_t *child = ci->children + i;
644 if (strcasecmp("URL", child->key) == 0)
645 status = cf_util_get_string(child, &cb->location);
646 else if (strcasecmp("User", child->key) == 0)
647 status = cf_util_get_string(child, &cb->user);
648 else if (strcasecmp("Password", child->key) == 0)
649 status = cf_util_get_string(child, &cb->pass);
650 else if (strcasecmp("VerifyPeer", child->key) == 0)
651 status = cf_util_get_boolean(child, &cb->verify_peer);
652 else if (strcasecmp("VerifyHost", child->key) == 0)
653 status = cf_util_get_boolean(child, &cb->verify_host);
654 else if (strcasecmp("CACert", child->key) == 0)
655 status = cf_util_get_string(child, &cb->cacert);
656 else if (strcasecmp("CAPath", child->key) == 0)
657 status = cf_util_get_string(child, &cb->capath);
658 else if (strcasecmp("ClientKey", child->key) == 0)
659 status = cf_util_get_string(child, &cb->clientkey);
660 else if (strcasecmp("ClientCert", child->key) == 0)
661 status = cf_util_get_string(child, &cb->clientcert);
662 else if (strcasecmp("ClientKeyPass", child->key) == 0)
663 status = cf_util_get_string(child, &cb->clientkeypass);
664 else if (strcasecmp("SSLVersion", child->key) == 0) {
665 char *value = NULL;
667 status = cf_util_get_string(child, &value);
668 if (status != 0)
669 break;
671 if (value == NULL || strcasecmp("default", value) == 0)
672 cb->sslversion = CURL_SSLVERSION_DEFAULT;
673 else if (strcasecmp("SSLv2", value) == 0)
674 cb->sslversion = CURL_SSLVERSION_SSLv2;
675 else if (strcasecmp("SSLv3", value) == 0)
676 cb->sslversion = CURL_SSLVERSION_SSLv3;
677 else if (strcasecmp("TLSv1", value) == 0)
678 cb->sslversion = CURL_SSLVERSION_TLSv1;
679 #if (LIBCURL_VERSION_MAJOR > 7) || \
680 (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
681 else if (strcasecmp("TLSv1_0", value) == 0)
682 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
683 else if (strcasecmp("TLSv1_1", value) == 0)
684 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
685 else if (strcasecmp("TLSv1_2", value) == 0)
686 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
687 #endif
688 else {
689 ERROR("write_http plugin: Invalid SSLVersion "
690 "option: %s.",
691 value);
692 status = EINVAL;
693 }
695 sfree(value);
696 } else if (strcasecmp("Format", child->key) == 0)
697 status = config_set_format(cb, child);
698 else if (strcasecmp("Metrics", child->key) == 0)
699 cf_util_get_boolean(child, &cb->send_metrics);
700 else if (strcasecmp("Notifications", child->key) == 0)
701 cf_util_get_boolean(child, &cb->send_notifications);
702 else if (strcasecmp("StoreRates", child->key) == 0)
703 status = cf_util_get_boolean(child, &cb->store_rates);
704 else if (strcasecmp("BufferSize", child->key) == 0)
705 status = cf_util_get_int(child, &buffer_size);
706 else if (strcasecmp("LowSpeedLimit", child->key) == 0)
707 status = cf_util_get_int(child, &cb->low_speed_limit);
708 else if (strcasecmp("Timeout", child->key) == 0)
709 status = cf_util_get_int(child, &cb->timeout);
710 else if (strcasecmp("LogHttpError", child->key) == 0)
711 status = cf_util_get_boolean(child, &cb->log_http_error);
712 else if (strcasecmp("Header", child->key) == 0)
713 status = wh_config_append_string("Header", &cb->headers, child);
714 else if (strcasecmp("Attribute", child->key) == 0) {
715 char *key = NULL;
716 char *val = NULL;
718 if (child->values_num != 2) {
719 WARNING("write_http plugin: Attribute need both a key and a value.");
720 break;
721 }
722 if (child->values[0].type != OCONFIG_TYPE_STRING ||
723 child->values[1].type != OCONFIG_TYPE_STRING) {
724 WARNING("write_http plugin: Attribute needs string arguments.");
725 break;
726 }
727 if ((key = strdup(child->values[0].value.string)) == NULL) {
728 WARNING("cannot allocate memory for attribute key.");
729 break;
730 }
731 if ((val = strdup(child->values[1].value.string)) == NULL) {
732 WARNING("cannot allocate memory for attribute value.");
733 sfree(key);
734 break;
735 }
736 strarray_add(&http_attrs, &http_attrs_num, key);
737 strarray_add(&http_attrs, &http_attrs_num, val);
738 DEBUG("write_http plugin: got attribute: %s => %s", key, val);
739 sfree(key);
740 sfree(val);
741 } else if (strcasecmp("TTL", child->key) == 0) {
742 status = cf_util_get_int(child, &cb->data_ttl);
743 } else {
744 ERROR("write_http plugin: Invalid configuration "
745 "option: %s.",
746 child->key);
747 status = EINVAL;
748 }
750 if (status != 0)
751 break;
752 }
754 if (status != 0) {
755 wh_callback_free(cb);
756 return status;
757 }
759 if (cb->location == NULL) {
760 ERROR("write_http plugin: no URL defined for instance '%s'", cb->name);
761 wh_callback_free(cb);
762 return -1;
763 }
765 if (!cb->send_metrics && !cb->send_notifications) {
766 ERROR("write_http plugin: Neither metrics nor notifications "
767 "are enabled for \"%s\".",
768 cb->name);
769 wh_callback_free(cb);
770 return -1;
771 }
773 if (cb->low_speed_limit > 0)
774 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
776 /* Determine send_buffer_size. */
777 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
778 if (buffer_size >= 1024)
779 cb->send_buffer_size = (size_t)buffer_size;
780 else if (buffer_size != 0)
781 ERROR("write_http plugin: Ignoring invalid BufferSize setting (%d).",
782 buffer_size);
784 /* Allocate the buffer. */
785 cb->send_buffer = malloc(cb->send_buffer_size);
786 if (cb->send_buffer == NULL) {
787 ERROR("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
788 wh_callback_free(cb);
789 return -1;
790 }
791 /* Nulls the buffer and sets ..._free and ..._fill. */
792 wh_reset_buffer(cb);
794 ssnprintf(callback_name, sizeof(callback_name), "write_http/%s", cb->name);
795 DEBUG("write_http: Registering write callback '%s' with URL '%s'",
796 callback_name, cb->location);
798 user_data_t user_data = {
799 .data = cb, .free_func = wh_callback_free,
800 };
802 if (cb->send_metrics) {
803 plugin_register_write(callback_name, wh_write, &user_data);
804 user_data.free_func = NULL;
806 plugin_register_flush(callback_name, wh_flush, &user_data);
807 }
809 if (cb->send_notifications) {
810 plugin_register_notification(callback_name, wh_notify, &user_data);
811 user_data.free_func = NULL;
812 }
814 return 0;
815 } /* }}} int wh_config_node */
817 static int wh_config(oconfig_item_t *ci) /* {{{ */
818 {
819 for (int i = 0; i < ci->children_num; i++) {
820 oconfig_item_t *child = ci->children + i;
822 if (strcasecmp("Node", child->key) == 0)
823 wh_config_node(child);
824 /* FIXME: Remove this legacy mode in version 6. */
825 else if (strcasecmp("URL", child->key) == 0) {
826 WARNING("write_http plugin: Legacy <URL> block found. "
827 "Please use <Node> instead.");
828 wh_config_node(child);
829 } else {
830 ERROR("write_http plugin: Invalid configuration "
831 "option: %s.",
832 child->key);
833 }
834 }
836 return 0;
837 } /* }}} int wh_config */
839 static int wh_init(void) /* {{{ */
840 {
841 /* Call this while collectd is still single-threaded to avoid
842 * initialization issues in libgcrypt. */
843 curl_global_init(CURL_GLOBAL_SSL);
844 return 0;
845 } /* }}} int wh_init */
847 void module_register(void) /* {{{ */
848 {
849 plugin_register_complex_config("write_http", wh_config);
850 plugin_register_init("write_http", wh_init);
851 } /* }}} void module_register */