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"
27 #include "plugin.h"
28 #include "common.h"
29 #include "utils_format_json.h"
30 #include "utils_format_kairosdb.h"
32 #include <curl/curl.h>
34 #ifndef WRITE_HTTP_DEFAULT_BUFFER_SIZE
35 # define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096
36 #endif
38 /*
39 * Private variables
40 */
41 struct wh_callback_s
42 {
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;
68 CURL *curl;
69 struct curl_slist *headers;
70 char curl_errbuf[CURL_ERROR_SIZE];
72 char *send_buffer;
73 size_t send_buffer_size;
74 size_t send_buffer_free;
75 size_t send_buffer_fill;
76 cdtime_t send_buffer_init_time;
78 pthread_mutex_t send_lock;
79 };
80 typedef struct wh_callback_s wh_callback_t;
82 static void wh_log_http_error (wh_callback_t *cb)
83 {
84 if (!cb->log_http_error)
85 return;
87 long http_code = 0;
89 curl_easy_getinfo (cb->curl, CURLINFO_RESPONSE_CODE, &http_code);
91 if (http_code != 200)
92 INFO ("write_http plugin: HTTP Error code: %lu", http_code);
93 }
95 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
96 {
97 memset (cb->send_buffer, 0, cb->send_buffer_size);
98 cb->send_buffer_free = cb->send_buffer_size;
99 cb->send_buffer_fill = 0;
100 cb->send_buffer_init_time = cdtime ();
102 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
103 {
104 format_json_initialize (cb->send_buffer,
105 &cb->send_buffer_fill,
106 &cb->send_buffer_free);
107 }
108 } /* }}} wh_reset_buffer */
110 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
111 {
112 int status = 0;
114 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
115 status = curl_easy_perform (cb->curl);
117 wh_log_http_error (cb);
119 if (status != CURLE_OK)
120 {
121 ERROR ("write_http plugin: curl_easy_perform failed with "
122 "status %i: %s",
123 status, cb->curl_errbuf);
124 }
125 return (status);
126 } /* }}} wh_send_buffer */
128 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
129 {
130 if (cb->curl != NULL)
131 return (0);
133 cb->curl = curl_easy_init ();
134 if (cb->curl == NULL)
135 {
136 ERROR ("curl plugin: curl_easy_init failed.");
137 return (-1);
138 }
140 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0)
141 {
142 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_LIMIT,
143 (long) (cb->low_speed_limit * cb->low_speed_time));
144 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_TIME,
145 (long) cb->low_speed_time);
146 }
148 #ifdef HAVE_CURLOPT_TIMEOUT_MS
149 if (cb->timeout > 0)
150 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout);
151 #endif
153 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
154 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
156 cb->headers = curl_slist_append (cb->headers, "Accept: */*");
157 if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
158 cb->headers = curl_slist_append (cb->headers, "Content-Type: application/json");
159 else
160 cb->headers = curl_slist_append (cb->headers, "Content-Type: text/plain");
161 cb->headers = curl_slist_append (cb->headers, "Expect:");
162 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, cb->headers);
164 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
165 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
166 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
167 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
169 if (cb->user != NULL)
170 {
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);
175 #else
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 {
185 ERROR ("curl plugin: malloc failed.");
186 return (-1);
187 }
189 ssnprintf (cb->credentials, credentials_size, "%s:%s",
190 cb->user, (cb->pass == NULL) ? "" : cb->pass);
191 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
192 #endif
193 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
194 }
196 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
197 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
198 cb->verify_host ? 2L : 0L);
199 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
200 if (cb->cacert != NULL)
201 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
202 if (cb->capath != NULL)
203 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
205 if (cb->clientkey != NULL && cb->clientcert != NULL)
206 {
207 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
208 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
210 if (cb->clientkeypass != NULL)
211 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
212 }
214 wh_reset_buffer (cb);
216 return (0);
217 } /* }}} int wh_callback_init */
219 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
220 {
221 int status;
223 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
224 "send_buffer_fill = %zu;",
225 CDTIME_T_TO_DOUBLE (timeout),
226 cb->send_buffer_fill);
228 /* timeout == 0 => flush unconditionally */
229 if (timeout > 0)
230 {
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 {
240 if (cb->send_buffer_fill == 0)
241 {
242 cb->send_buffer_init_time = cdtime ();
243 return (0);
244 }
246 status = wh_send_buffer (cb);
247 wh_reset_buffer (cb);
248 }
249 else if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
250 {
251 if (cb->send_buffer_fill <= 2)
252 {
253 cb->send_buffer_init_time = cdtime ();
254 return (0);
255 }
257 status = format_json_finalize (cb->send_buffer,
258 &cb->send_buffer_fill,
259 &cb->send_buffer_free);
260 if (status != 0)
261 {
262 ERROR ("write_http: wh_flush_nolock: "
263 "format_json_finalize failed.");
264 wh_reset_buffer (cb);
265 return (status);
266 }
268 status = wh_send_buffer (cb);
269 wh_reset_buffer (cb);
270 }
271 else
272 {
273 ERROR ("write_http: wh_flush_nolock: "
274 "Unknown format: %i",
275 cb->format);
276 return (-1);
277 }
279 return (status);
280 } /* }}} wh_flush_nolock */
282 static int wh_flush (cdtime_t timeout, /* {{{ */
283 const char *identifier __attribute__((unused)),
284 user_data_t *user_data)
285 {
286 wh_callback_t *cb;
287 int status;
289 if (user_data == NULL)
290 return (-EINVAL);
292 cb = user_data->data;
294 pthread_mutex_lock (&cb->send_lock);
296 if (cb->curl == NULL)
297 {
298 status = wh_callback_init (cb);
299 if (status != 0)
300 {
301 ERROR ("write_http plugin: wh_callback_init failed.");
302 pthread_mutex_unlock (&cb->send_lock);
303 return (-1);
304 }
305 }
307 status = wh_flush_nolock (timeout, cb);
308 pthread_mutex_unlock (&cb->send_lock);
310 return (status);
311 } /* }}} int wh_flush */
313 static void wh_callback_free (void *data) /* {{{ */
314 {
315 wh_callback_t *cb;
317 if (data == NULL)
318 return;
320 cb = data;
322 wh_flush_nolock (/* timeout = */ 0, cb);
324 if (cb->curl != NULL)
325 {
326 curl_easy_cleanup (cb->curl);
327 cb->curl = NULL;
328 }
330 if (cb->headers != NULL)
331 {
332 curl_slist_free_all (cb->headers);
333 cb->headers = NULL;
334 }
336 sfree (cb->name);
337 sfree (cb->location);
338 sfree (cb->user);
339 sfree (cb->pass);
340 sfree (cb->credentials);
341 sfree (cb->cacert);
342 sfree (cb->capath);
343 sfree (cb->clientkey);
344 sfree (cb->clientcert);
345 sfree (cb->clientkeypass);
346 sfree (cb->send_buffer);
348 sfree (cb);
349 } /* }}} void wh_callback_free */
351 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
352 wh_callback_t *cb)
353 {
354 char key[10*DATA_MAX_NAME_LEN];
355 char values[512];
356 char command[1024];
357 size_t command_len;
359 int status;
361 if (0 != strcmp (ds->type, vl->type)) {
362 ERROR ("write_http plugin: DS type does not match "
363 "value list type");
364 return -1;
365 }
367 /* Copy the identifier to `key' and escape it. */
368 status = FORMAT_VL (key, sizeof (key), vl);
369 if (status != 0) {
370 ERROR ("write_http plugin: error with format_name");
371 return (status);
372 }
373 escape_string (key, sizeof (key));
375 /* Convert the values to an ASCII representation and put that into
376 * `values'. */
377 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
378 if (status != 0) {
379 ERROR ("write_http plugin: error with "
380 "wh_value_list_to_string");
381 return (status);
382 }
384 command_len = (size_t) ssnprintf (command, sizeof (command),
385 "PUTVAL %s interval=%.3f %s\r\n",
386 key,
387 CDTIME_T_TO_DOUBLE (vl->interval),
388 values);
389 if (command_len >= sizeof (command)) {
390 ERROR ("write_http plugin: Command buffer too small: "
391 "Need %zu bytes.", command_len + 1);
392 return (-1);
393 }
395 pthread_mutex_lock (&cb->send_lock);
397 if (cb->curl == NULL)
398 {
399 status = wh_callback_init (cb);
400 if (status != 0)
401 {
402 ERROR ("write_http plugin: wh_callback_init failed.");
403 pthread_mutex_unlock (&cb->send_lock);
404 return (-1);
405 }
406 }
408 if (command_len >= cb->send_buffer_free)
409 {
410 status = wh_flush_nolock (/* timeout = */ 0, cb);
411 if (status != 0)
412 {
413 pthread_mutex_unlock (&cb->send_lock);
414 return (status);
415 }
416 }
417 assert (command_len < cb->send_buffer_free);
419 /* `command_len + 1' because `command_len' does not include the
420 * trailing null byte. Neither does `send_buffer_fill'. */
421 memcpy (cb->send_buffer + cb->send_buffer_fill,
422 command, command_len + 1);
423 cb->send_buffer_fill += command_len;
424 cb->send_buffer_free -= command_len;
426 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
427 cb->location,
428 cb->send_buffer_fill, cb->send_buffer_size,
429 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
430 command);
432 /* Check if we have enough space for this command. */
433 pthread_mutex_unlock (&cb->send_lock);
435 return (0);
436 } /* }}} int wh_write_command */
438 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
439 wh_callback_t *cb)
440 {
441 int status;
443 pthread_mutex_lock (&cb->send_lock);
445 if (cb->curl == NULL)
446 {
447 status = wh_callback_init (cb);
448 if (status != 0)
449 {
450 ERROR ("write_http plugin: wh_callback_init failed.");
451 pthread_mutex_unlock (&cb->send_lock);
452 return (-1);
453 }
454 }
456 status = format_json_value_list (cb->send_buffer,
457 &cb->send_buffer_fill,
458 &cb->send_buffer_free,
459 ds, vl, cb->store_rates);
460 if (status == -ENOMEM)
461 {
462 status = wh_flush_nolock (/* timeout = */ 0, cb);
463 if (status != 0)
464 {
465 wh_reset_buffer (cb);
466 pthread_mutex_unlock (&cb->send_lock);
467 return (status);
468 }
470 status = format_json_value_list (cb->send_buffer,
471 &cb->send_buffer_fill,
472 &cb->send_buffer_free,
473 ds, vl, cb->store_rates);
474 }
475 if (status != 0)
476 {
477 pthread_mutex_unlock (&cb->send_lock);
478 return (status);
479 }
481 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
482 cb->location,
483 cb->send_buffer_fill, cb->send_buffer_size,
484 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
486 /* Check if we have enough space for this command. */
487 pthread_mutex_unlock (&cb->send_lock);
489 return (0);
490 } /* }}} int wh_write_json */
492 static int wh_write_kairosdb (const data_set_t *ds, const value_list_t *vl, /* {{{ */
493 wh_callback_t *cb)
494 {
495 int status;
497 pthread_mutex_lock (&cb->send_lock);
499 if (cb->curl == NULL)
500 {
501 status = wh_callback_init (cb);
502 if (status != 0)
503 {
504 ERROR ("write_http plugin: wh_callback_init failed.");
505 pthread_mutex_unlock (&cb->send_lock);
506 return (-1);
507 }
508 }
510 status = format_kairosdb_value_list (cb->send_buffer,
511 &cb->send_buffer_fill,
512 &cb->send_buffer_free,
513 ds, vl, cb->store_rates);
514 if (status == -ENOMEM)
515 {
516 status = wh_flush_nolock (/* timeout = */ 0, cb);
517 if (status != 0)
518 {
519 wh_reset_buffer (cb);
520 pthread_mutex_unlock (&cb->send_lock);
521 return (status);
522 }
524 status = format_kairosdb_value_list (cb->send_buffer,
525 &cb->send_buffer_fill,
526 &cb->send_buffer_free,
527 ds, vl, cb->store_rates);
528 }
529 if (status != 0)
530 {
531 pthread_mutex_unlock (&cb->send_lock);
532 return (status);
533 }
535 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
536 cb->location,
537 cb->send_buffer_fill, cb->send_buffer_size,
538 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
540 /* Check if we have enough space for this command. */
541 pthread_mutex_unlock (&cb->send_lock);
543 return (0);
544 } /* }}} int wh_write_kairosdb */
546 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
547 user_data_t *user_data)
548 {
549 wh_callback_t *cb;
550 int status;
552 if (user_data == NULL)
553 return (-EINVAL);
555 cb = user_data->data;
557 switch(cb->format) {
558 case WH_FORMAT_JSON:
559 status = wh_write_json (ds, vl, cb);
560 break;
561 case WH_FORMAT_KAIROSDB:
562 status = wh_write_kairosdb (ds, vl, cb);
563 break;
564 default:
565 status = wh_write_command (ds, vl, cb);
566 break;
567 }
568 return (status);
569 } /* }}} int wh_write */
571 static int config_set_format (wh_callback_t *cb, /* {{{ */
572 oconfig_item_t *ci)
573 {
574 char *string;
576 if ((ci->values_num != 1)
577 || (ci->values[0].type != OCONFIG_TYPE_STRING))
578 {
579 WARNING ("write_http plugin: The `%s' config option "
580 "needs exactly one string argument.", ci->key);
581 return (-1);
582 }
584 string = ci->values[0].value.string;
585 if (strcasecmp ("Command", string) == 0)
586 cb->format = WH_FORMAT_COMMAND;
587 else if (strcasecmp ("JSON", string) == 0)
588 cb->format = WH_FORMAT_JSON;
589 else if (strcasecmp ("KAIROSDB", string) == 0)
590 cb->format = WH_FORMAT_KAIROSDB;
591 else
592 {
593 ERROR ("write_http plugin: Invalid format string: %s",
594 string);
595 return (-1);
596 }
598 return (0);
599 } /* }}} int config_set_format */
601 static int wh_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
602 oconfig_item_t *ci)
603 {
604 struct curl_slist *temp = NULL;
605 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
606 {
607 WARNING ("write_http plugin: `%s' needs exactly one string argument.", name);
608 return (-1);
609 }
611 temp = curl_slist_append(*dest, ci->values[0].value.string);
612 if (temp == NULL)
613 return (-1);
615 *dest = temp;
617 return (0);
618 } /* }}} int wh_config_append_string */
620 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
621 {
622 wh_callback_t *cb;
623 int buffer_size = 0;
624 user_data_t user_data;
625 char callback_name[DATA_MAX_NAME_LEN];
626 int status = 0;
627 int i;
629 cb = calloc (1, sizeof (*cb));
630 if (cb == NULL)
631 {
632 ERROR ("write_http plugin: calloc failed.");
633 return (-1);
634 }
635 cb->verify_peer = 1;
636 cb->verify_host = 1;
637 cb->format = WH_FORMAT_COMMAND;
638 cb->sslversion = CURL_SSLVERSION_DEFAULT;
639 cb->low_speed_limit = 0;
640 cb->timeout = 0;
641 cb->log_http_error = 0;
642 cb->headers = NULL;
645 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
647 cf_util_get_string (ci, &cb->name);
649 /* FIXME: Remove this legacy mode in version 6. */
650 if (strcasecmp ("URL", ci->key) == 0)
651 cf_util_get_string (ci, &cb->location);
653 for (i = 0; i < ci->children_num; i++)
654 {
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)
678 {
679 char *value = NULL;
681 status = cf_util_get_string (child, &value);
682 if (status != 0)
683 break;
685 if (value == NULL || strcasecmp ("default", value) == 0)
686 cb->sslversion = CURL_SSLVERSION_DEFAULT;
687 else if (strcasecmp ("SSLv2", value) == 0)
688 cb->sslversion = CURL_SSLVERSION_SSLv2;
689 else if (strcasecmp ("SSLv3", value) == 0)
690 cb->sslversion = CURL_SSLVERSION_SSLv3;
691 else if (strcasecmp ("TLSv1", value) == 0)
692 cb->sslversion = CURL_SSLVERSION_TLSv1;
693 #if (LIBCURL_VERSION_MAJOR > 7) || (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;
700 #endif
701 else
702 {
703 ERROR ("write_http plugin: Invalid SSLVersion "
704 "option: %s.", value);
705 status = EINVAL;
706 }
708 sfree(value);
709 }
710 else if (strcasecmp ("Format", child->key) == 0)
711 status = config_set_format (cb, child);
712 else if (strcasecmp ("StoreRates", child->key) == 0)
713 status = cf_util_get_boolean (child, &cb->store_rates);
714 else if (strcasecmp ("BufferSize", child->key) == 0)
715 status = cf_util_get_int (child, &buffer_size);
716 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
717 status = cf_util_get_int (child, &cb->low_speed_limit);
718 else if (strcasecmp ("Timeout", child->key) == 0)
719 status = cf_util_get_int (child, &cb->timeout);
720 else if (strcasecmp ("LogHttpError", child->key) == 0)
721 status = cf_util_get_boolean (child, &cb->log_http_error);
722 else if (strcasecmp ("Header", child->key) == 0)
723 status = wh_config_append_string ("Header", &cb->headers, child);
724 else
725 {
726 ERROR ("write_http plugin: Invalid configuration "
727 "option: %s.", child->key);
728 status = EINVAL;
729 }
731 if (status != 0)
732 break;
733 }
735 if (status != 0)
736 {
737 wh_callback_free (cb);
738 return (status);
739 }
741 if (cb->location == NULL)
742 {
743 ERROR ("write_http plugin: no URL defined for instance '%s'",
744 cb->name);
745 wh_callback_free (cb);
746 return (-1);
747 }
749 if (cb->low_speed_limit > 0)
750 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
752 /* Determine send_buffer_size. */
753 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
754 if (buffer_size >= 1024)
755 cb->send_buffer_size = (size_t) buffer_size;
756 else if (buffer_size != 0)
757 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
758 buffer_size);
760 /* Allocate the buffer. */
761 cb->send_buffer = malloc (cb->send_buffer_size);
762 if (cb->send_buffer == NULL)
763 {
764 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
765 wh_callback_free (cb);
766 return (-1);
767 }
768 /* Nulls the buffer and sets ..._free and ..._fill. */
769 wh_reset_buffer (cb);
771 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
772 cb->name);
773 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
774 callback_name, cb->location);
776 memset (&user_data, 0, sizeof (user_data));
777 user_data.data = cb;
778 user_data.free_func = NULL;
779 plugin_register_flush (callback_name, wh_flush, &user_data);
781 user_data.free_func = wh_callback_free;
782 plugin_register_write (callback_name, wh_write, &user_data);
784 return (0);
785 } /* }}} int wh_config_node */
787 static int wh_config (oconfig_item_t *ci) /* {{{ */
788 {
789 int i;
791 for (i = 0; i < ci->children_num; i++)
792 {
793 oconfig_item_t *child = ci->children + i;
795 if (strcasecmp ("Node", child->key) == 0)
796 wh_config_node (child);
797 /* FIXME: Remove this legacy mode in version 6. */
798 else if (strcasecmp ("URL", child->key) == 0) {
799 WARNING ("write_http plugin: Legacy <URL> block found. "
800 "Please use <Node> instead.");
801 wh_config_node (child);
802 }
803 else
804 {
805 ERROR ("write_http plugin: Invalid configuration "
806 "option: %s.", child->key);
807 }
808 }
810 return (0);
811 } /* }}} int wh_config */
813 static int wh_init (void) /* {{{ */
814 {
815 /* Call this while collectd is still single-threaded to avoid
816 * initialization issues in libgcrypt. */
817 curl_global_init (CURL_GLOBAL_SSL);
818 return (0);
819 } /* }}} int wh_init */
821 void module_register (void) /* {{{ */
822 {
823 plugin_register_complex_config ("write_http", wh_config);
824 plugin_register_init ("write_http", wh_init);
825 } /* }}} void module_register */
827 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */