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
63 #define WH_FORMAT_COMMAND 0
64 #define WH_FORMAT_JSON 1
68 char curl_errbuf[CURL_ERROR_SIZE];
71 size_t send_buffer_size;
72 size_t send_buffer_free;
73 size_t send_buffer_fill;
74 cdtime_t send_buffer_init_time;
76 pthread_mutex_t send_lock;
78 typedef struct wh_callback_s wh_callback_t;
80 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
82 memset (cb->send_buffer, 0, cb->send_buffer_size);
83 cb->send_buffer_free = cb->send_buffer_size;
84 cb->send_buffer_fill = 0;
85 cb->send_buffer_init_time = cdtime ();
87 if (cb->format == WH_FORMAT_JSON)
89 format_json_initialize (cb->send_buffer,
90 &cb->send_buffer_fill,
91 &cb->send_buffer_free);
93 } /* }}} wh_reset_buffer */
95 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
99 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
100 status = curl_easy_perform (cb->curl);
101 if (status != CURLE_OK)
103 ERROR ("write_http plugin: curl_easy_perform failed with "
105 status, cb->curl_errbuf);
108 } /* }}} wh_send_buffer */
110 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
112 struct curl_slist *headers;
114 if (cb->curl != NULL)
117 cb->curl = curl_easy_init ();
118 if (cb->curl == NULL)
120 ERROR ("curl plugin: curl_easy_init failed.");
124 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
125 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
128 headers = curl_slist_append (headers, "Accept: */*");
129 if (cb->format == WH_FORMAT_JSON)
130 headers = curl_slist_append (headers, "Content-Type: application/json");
132 headers = curl_slist_append (headers, "Content-Type: text/plain");
133 headers = curl_slist_append (headers, "Expect:");
134 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
136 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
137 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
138 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
139 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
141 if (cb->user != NULL)
143 size_t credentials_size;
145 credentials_size = strlen (cb->user) + 2;
146 if (cb->pass != NULL)
147 credentials_size += strlen (cb->pass);
149 cb->credentials = (char *) malloc (credentials_size);
150 if (cb->credentials == NULL)
152 ERROR ("curl plugin: malloc failed.");
156 ssnprintf (cb->credentials, credentials_size, "%s:%s",
157 cb->user, (cb->pass == NULL) ? "" : cb->pass);
158 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
159 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
162 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
163 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
164 cb->verify_host ? 2L : 0L);
165 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
166 if (cb->cacert != NULL)
167 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
168 if (cb->capath != NULL)
169 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
171 if (cb->clientkey != NULL && cb->clientcert != NULL)
173 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
174 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
176 if (cb->clientkeypass != NULL)
177 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
180 wh_reset_buffer (cb);
183 } /* }}} int wh_callback_init */
185 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
189 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
190 "send_buffer_fill = %zu;",
191 CDTIME_T_TO_DOUBLE (timeout),
192 cb->send_buffer_fill);
194 /* timeout == 0 => flush unconditionally */
200 if ((cb->send_buffer_init_time + timeout) > now)
204 if (cb->format == WH_FORMAT_COMMAND)
206 if (cb->send_buffer_fill <= 0)
208 cb->send_buffer_init_time = cdtime ();
212 status = wh_send_buffer (cb);
213 wh_reset_buffer (cb);
215 else if (cb->format == WH_FORMAT_JSON)
217 if (cb->send_buffer_fill <= 2)
219 cb->send_buffer_init_time = cdtime ();
223 status = format_json_finalize (cb->send_buffer,
224 &cb->send_buffer_fill,
225 &cb->send_buffer_free);
228 ERROR ("write_http: wh_flush_nolock: "
229 "format_json_finalize failed.");
230 wh_reset_buffer (cb);
234 status = wh_send_buffer (cb);
235 wh_reset_buffer (cb);
239 ERROR ("write_http: wh_flush_nolock: "
240 "Unknown format: %i",
246 } /* }}} wh_flush_nolock */
248 static int wh_flush (cdtime_t timeout, /* {{{ */
249 const char *identifier __attribute__((unused)),
250 user_data_t *user_data)
255 if (user_data == NULL)
258 cb = user_data->data;
260 pthread_mutex_lock (&cb->send_lock);
262 if (cb->curl == NULL)
264 status = wh_callback_init (cb);
267 ERROR ("write_http plugin: wh_callback_init failed.");
268 pthread_mutex_unlock (&cb->send_lock);
273 status = wh_flush_nolock (timeout, cb);
274 pthread_mutex_unlock (&cb->send_lock);
277 } /* }}} int wh_flush */
279 static void wh_callback_free (void *data) /* {{{ */
288 wh_flush_nolock (/* timeout = */ 0, cb);
290 if (cb->curl != NULL)
292 curl_easy_cleanup (cb->curl);
296 sfree (cb->location);
299 sfree (cb->credentials);
302 sfree (cb->clientkey);
303 sfree (cb->clientcert);
304 sfree (cb->clientkeypass);
305 sfree (cb->send_buffer);
308 } /* }}} void wh_callback_free */
310 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
313 char key[10*DATA_MAX_NAME_LEN];
320 if (0 != strcmp (ds->type, vl->type)) {
321 ERROR ("write_http plugin: DS type does not match "
326 /* Copy the identifier to `key' and escape it. */
327 status = FORMAT_VL (key, sizeof (key), vl);
329 ERROR ("write_http plugin: error with format_name");
332 escape_string (key, sizeof (key));
334 /* Convert the values to an ASCII representation and put that into
336 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
338 ERROR ("write_http plugin: error with "
339 "wh_value_list_to_string");
343 command_len = (size_t) ssnprintf (command, sizeof (command),
344 "PUTVAL %s interval=%.3f %s\r\n",
346 CDTIME_T_TO_DOUBLE (vl->interval),
348 if (command_len >= sizeof (command)) {
349 ERROR ("write_http plugin: Command buffer too small: "
350 "Need %zu bytes.", command_len + 1);
354 pthread_mutex_lock (&cb->send_lock);
356 if (cb->curl == NULL)
358 status = wh_callback_init (cb);
361 ERROR ("write_http plugin: wh_callback_init failed.");
362 pthread_mutex_unlock (&cb->send_lock);
367 if (command_len >= cb->send_buffer_free)
369 status = wh_flush_nolock (/* timeout = */ 0, cb);
372 pthread_mutex_unlock (&cb->send_lock);
376 assert (command_len < cb->send_buffer_free);
378 /* `command_len + 1' because `command_len' does not include the
379 * trailing null byte. Neither does `send_buffer_fill'. */
380 memcpy (cb->send_buffer + cb->send_buffer_fill,
381 command, command_len + 1);
382 cb->send_buffer_fill += command_len;
383 cb->send_buffer_free -= command_len;
385 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
387 cb->send_buffer_fill, cb->send_buffer_size,
388 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
391 /* Check if we have enough space for this command. */
392 pthread_mutex_unlock (&cb->send_lock);
395 } /* }}} int wh_write_command */
397 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
402 pthread_mutex_lock (&cb->send_lock);
404 if (cb->curl == NULL)
406 status = wh_callback_init (cb);
409 ERROR ("write_http plugin: wh_callback_init failed.");
410 pthread_mutex_unlock (&cb->send_lock);
415 status = format_json_value_list (cb->send_buffer,
416 &cb->send_buffer_fill,
417 &cb->send_buffer_free,
418 ds, vl, cb->store_rates);
419 if (status == (-ENOMEM))
421 status = wh_flush_nolock (/* timeout = */ 0, cb);
424 wh_reset_buffer (cb);
425 pthread_mutex_unlock (&cb->send_lock);
429 status = format_json_value_list (cb->send_buffer,
430 &cb->send_buffer_fill,
431 &cb->send_buffer_free,
432 ds, vl, cb->store_rates);
436 pthread_mutex_unlock (&cb->send_lock);
440 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
442 cb->send_buffer_fill, cb->send_buffer_size,
443 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
445 /* Check if we have enough space for this command. */
446 pthread_mutex_unlock (&cb->send_lock);
449 } /* }}} int wh_write_json */
451 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
452 user_data_t *user_data)
457 if (user_data == NULL)
460 cb = user_data->data;
462 if (cb->format == WH_FORMAT_JSON)
463 status = wh_write_json (ds, vl, cb);
465 status = wh_write_command (ds, vl, cb);
468 } /* }}} int wh_write */
470 static int config_set_format (wh_callback_t *cb, /* {{{ */
475 if ((ci->values_num != 1)
476 || (ci->values[0].type != OCONFIG_TYPE_STRING))
478 WARNING ("write_http plugin: The `%s' config option "
479 "needs exactly one string argument.", ci->key);
483 string = ci->values[0].value.string;
484 if (strcasecmp ("Command", string) == 0)
485 cb->format = WH_FORMAT_COMMAND;
486 else if (strcasecmp ("JSON", string) == 0)
487 cb->format = WH_FORMAT_JSON;
490 ERROR ("write_http plugin: Invalid format string: %s",
496 } /* }}} int config_set_format */
498 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
502 user_data_t user_data;
503 char callback_name[DATA_MAX_NAME_LEN];
506 cb = malloc (sizeof (*cb));
509 ERROR ("write_http plugin: malloc failed.");
512 memset (cb, 0, sizeof (*cb));
515 cb->format = WH_FORMAT_COMMAND;
516 cb->sslversion = CURL_SSLVERSION_DEFAULT;
518 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
520 cf_util_get_string (ci, &cb->name);
522 /* FIXME: Remove this legacy mode in version 6. */
523 if (strcasecmp ("URL", ci->key) == 0)
524 cf_util_get_string (ci, &cb->location);
526 for (i = 0; i < ci->children_num; i++)
528 oconfig_item_t *child = ci->children + i;
530 if (strcasecmp ("URL", child->key) == 0)
531 cf_util_get_string (child, &cb->location);
532 else if (strcasecmp ("User", child->key) == 0)
533 cf_util_get_string (child, &cb->user);
534 else if (strcasecmp ("Password", child->key) == 0)
535 cf_util_get_string (child, &cb->pass);
536 else if (strcasecmp ("VerifyPeer", child->key) == 0)
537 cf_util_get_boolean (child, &cb->verify_peer);
538 else if (strcasecmp ("VerifyHost", child->key) == 0)
539 cf_util_get_boolean (child, &cb->verify_host);
540 else if (strcasecmp ("CACert", child->key) == 0)
541 cf_util_get_string (child, &cb->cacert);
542 else if (strcasecmp ("CAPath", child->key) == 0)
543 cf_util_get_string (child, &cb->capath);
544 else if (strcasecmp ("ClientKey", child->key) == 0)
545 cf_util_get_string (child, &cb->clientkey);
546 else if (strcasecmp ("ClientCert", child->key) == 0)
547 cf_util_get_string (child, &cb->clientcert);
548 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
549 cf_util_get_string (child, &cb->clientkeypass);
550 else if (strcasecmp ("SSLVersion", child->key) == 0)
554 cf_util_get_string (child, &value);
556 if (value == NULL || strcasecmp ("default", value) == 0)
557 cb->sslversion = CURL_SSLVERSION_DEFAULT;
558 else if (strcasecmp ("SSLv2", value) == 0)
559 cb->sslversion = CURL_SSLVERSION_SSLv2;
560 else if (strcasecmp ("SSLv3", value) == 0)
561 cb->sslversion = CURL_SSLVERSION_SSLv3;
562 else if (strcasecmp ("TLSv1", value) == 0)
563 cb->sslversion = CURL_SSLVERSION_TLSv1;
564 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
565 else if (strcasecmp ("TLSv1_0", value) == 0)
566 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
567 else if (strcasecmp ("TLSv1_1", value) == 0)
568 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
569 else if (strcasecmp ("TLSv1_2", value) == 0)
570 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
573 ERROR ("write_http plugin: Invalid SSLVersion "
574 "option: %s.", value);
578 else if (strcasecmp ("Format", child->key) == 0)
579 config_set_format (cb, child);
580 else if (strcasecmp ("StoreRates", child->key) == 0)
581 cf_util_get_boolean (child, &cb->store_rates);
582 else if (strcasecmp ("BufferSize", child->key) == 0)
583 cf_util_get_int (child, &buffer_size);
586 ERROR ("write_http plugin: Invalid configuration "
587 "option: %s.", child->key);
591 if (cb->location == NULL)
593 ERROR ("write_http plugin: no URL defined for instance '%s'",
595 wh_callback_free (cb);
599 /* Determine send_buffer_size. */
600 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
601 if (buffer_size >= 1024)
602 cb->send_buffer_size = (size_t) buffer_size;
603 else if (buffer_size != 0)
604 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
607 /* Allocate the buffer. */
608 cb->send_buffer = malloc (cb->send_buffer_size);
609 if (cb->send_buffer == NULL)
611 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
612 wh_callback_free (cb);
615 /* Nulls the buffer and sets ..._free and ..._fill. */
616 wh_reset_buffer (cb);
618 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
620 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
621 callback_name, cb->location);
623 memset (&user_data, 0, sizeof (user_data));
625 user_data.free_func = NULL;
626 plugin_register_flush (callback_name, wh_flush, &user_data);
628 user_data.free_func = wh_callback_free;
629 plugin_register_write (callback_name, wh_write, &user_data);
632 } /* }}} int wh_config_node */
634 static int wh_config (oconfig_item_t *ci) /* {{{ */
638 for (i = 0; i < ci->children_num; i++)
640 oconfig_item_t *child = ci->children + i;
642 if (strcasecmp ("Node", child->key) == 0)
643 wh_config_node (child);
644 /* FIXME: Remove this legacy mode in version 6. */
645 else if (strcasecmp ("URL", child->key) == 0) {
646 WARNING ("write_http plugin: Legacy <URL> block found. "
647 "Please use <Node> instead.");
648 wh_config_node (child);
652 ERROR ("write_http plugin: Invalid configuration "
653 "option: %s.", child->key);
658 } /* }}} int wh_config */
660 static int wh_init (void) /* {{{ */
662 /* Call this while collectd is still single-threaded to avoid
663 * initialization issues in libgcrypt. */
664 curl_global_init (CURL_GLOBAL_SSL);
666 } /* }}} int wh_init */
668 void module_register (void) /* {{{ */
670 plugin_register_complex_config ("write_http", wh_config);
671 plugin_register_init ("write_http", wh_init);
672 } /* }}} void module_register */
674 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */