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
62 #define WH_FORMAT_COMMAND 0
63 #define WH_FORMAT_JSON 1
67 char curl_errbuf[CURL_ERROR_SIZE];
70 size_t send_buffer_size;
71 size_t send_buffer_free;
72 size_t send_buffer_fill;
73 cdtime_t send_buffer_init_time;
75 pthread_mutex_t send_lock;
77 typedef struct wh_callback_s wh_callback_t;
79 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
81 memset (cb->send_buffer, 0, cb->send_buffer_size);
82 cb->send_buffer_free = cb->send_buffer_size;
83 cb->send_buffer_fill = 0;
84 cb->send_buffer_init_time = cdtime ();
86 if (cb->format == WH_FORMAT_JSON)
88 format_json_initialize (cb->send_buffer,
89 &cb->send_buffer_fill,
90 &cb->send_buffer_free);
92 } /* }}} wh_reset_buffer */
94 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
98 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
99 status = curl_easy_perform (cb->curl);
100 if (status != CURLE_OK)
102 ERROR ("write_http plugin: curl_easy_perform failed with "
104 status, cb->curl_errbuf);
107 } /* }}} wh_send_buffer */
109 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
111 struct curl_slist *headers;
113 if (cb->curl != NULL)
116 cb->curl = curl_easy_init ();
117 if (cb->curl == NULL)
119 ERROR ("curl plugin: curl_easy_init failed.");
123 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
124 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
127 headers = curl_slist_append (headers, "Accept: */*");
128 if (cb->format == WH_FORMAT_JSON)
129 headers = curl_slist_append (headers, "Content-Type: application/json");
131 headers = curl_slist_append (headers, "Content-Type: text/plain");
132 headers = curl_slist_append (headers, "Expect:");
133 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
135 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
136 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
138 if (cb->user != NULL)
140 size_t credentials_size;
142 credentials_size = strlen (cb->user) + 2;
143 if (cb->pass != NULL)
144 credentials_size += strlen (cb->pass);
146 cb->credentials = (char *) malloc (credentials_size);
147 if (cb->credentials == NULL)
149 ERROR ("curl plugin: malloc failed.");
153 ssnprintf (cb->credentials, credentials_size, "%s:%s",
154 cb->user, (cb->pass == NULL) ? "" : cb->pass);
155 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
156 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
159 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
160 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
161 cb->verify_host ? 2L : 0L);
162 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
163 if (cb->cacert != NULL)
164 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
165 if (cb->capath != NULL)
166 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
168 if (cb->clientkey != NULL && cb->clientcert != NULL)
170 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
171 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
173 if (cb->clientkeypass != NULL)
174 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
177 wh_reset_buffer (cb);
180 } /* }}} int wh_callback_init */
182 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
186 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
187 "send_buffer_fill = %zu;",
188 CDTIME_T_TO_DOUBLE (timeout),
189 cb->send_buffer_fill);
191 /* timeout == 0 => flush unconditionally */
197 if ((cb->send_buffer_init_time + timeout) > now)
201 if (cb->format == WH_FORMAT_COMMAND)
203 if (cb->send_buffer_fill <= 0)
205 cb->send_buffer_init_time = cdtime ();
209 status = wh_send_buffer (cb);
210 wh_reset_buffer (cb);
212 else if (cb->format == WH_FORMAT_JSON)
214 if (cb->send_buffer_fill <= 2)
216 cb->send_buffer_init_time = cdtime ();
220 status = format_json_finalize (cb->send_buffer,
221 &cb->send_buffer_fill,
222 &cb->send_buffer_free);
225 ERROR ("write_http: wh_flush_nolock: "
226 "format_json_finalize failed.");
227 wh_reset_buffer (cb);
231 status = wh_send_buffer (cb);
232 wh_reset_buffer (cb);
236 ERROR ("write_http: wh_flush_nolock: "
237 "Unknown format: %i",
243 } /* }}} wh_flush_nolock */
245 static int wh_flush (cdtime_t timeout, /* {{{ */
246 const char *identifier __attribute__((unused)),
247 user_data_t *user_data)
252 if (user_data == NULL)
255 cb = user_data->data;
257 pthread_mutex_lock (&cb->send_lock);
259 if (cb->curl == NULL)
261 status = wh_callback_init (cb);
264 ERROR ("write_http plugin: wh_callback_init failed.");
265 pthread_mutex_unlock (&cb->send_lock);
270 status = wh_flush_nolock (timeout, cb);
271 pthread_mutex_unlock (&cb->send_lock);
274 } /* }}} int wh_flush */
276 static void wh_callback_free (void *data) /* {{{ */
285 wh_flush_nolock (/* timeout = */ 0, cb);
287 if (cb->curl != NULL)
289 curl_easy_cleanup (cb->curl);
292 sfree (cb->location);
295 sfree (cb->credentials);
298 sfree (cb->clientkey);
299 sfree (cb->clientcert);
300 sfree (cb->clientkeypass);
301 sfree (cb->send_buffer);
304 } /* }}} void wh_callback_free */
306 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
309 char key[10*DATA_MAX_NAME_LEN];
316 if (0 != strcmp (ds->type, vl->type)) {
317 ERROR ("write_http plugin: DS type does not match "
322 /* Copy the identifier to `key' and escape it. */
323 status = FORMAT_VL (key, sizeof (key), vl);
325 ERROR ("write_http plugin: error with format_name");
328 escape_string (key, sizeof (key));
330 /* Convert the values to an ASCII representation and put that into
332 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
334 ERROR ("write_http plugin: error with "
335 "wh_value_list_to_string");
339 command_len = (size_t) ssnprintf (command, sizeof (command),
340 "PUTVAL %s interval=%.3f %s\r\n",
342 CDTIME_T_TO_DOUBLE (vl->interval),
344 if (command_len >= sizeof (command)) {
345 ERROR ("write_http plugin: Command buffer too small: "
346 "Need %zu bytes.", command_len + 1);
350 pthread_mutex_lock (&cb->send_lock);
352 if (cb->curl == NULL)
354 status = wh_callback_init (cb);
357 ERROR ("write_http plugin: wh_callback_init failed.");
358 pthread_mutex_unlock (&cb->send_lock);
363 if (command_len >= cb->send_buffer_free)
365 status = wh_flush_nolock (/* timeout = */ 0, cb);
368 pthread_mutex_unlock (&cb->send_lock);
372 assert (command_len < cb->send_buffer_free);
374 /* `command_len + 1' because `command_len' does not include the
375 * trailing null byte. Neither does `send_buffer_fill'. */
376 memcpy (cb->send_buffer + cb->send_buffer_fill,
377 command, command_len + 1);
378 cb->send_buffer_fill += command_len;
379 cb->send_buffer_free -= command_len;
381 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
383 cb->send_buffer_fill, cb->send_buffer_size,
384 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
387 /* Check if we have enough space for this command. */
388 pthread_mutex_unlock (&cb->send_lock);
391 } /* }}} int wh_write_command */
393 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
398 pthread_mutex_lock (&cb->send_lock);
400 if (cb->curl == NULL)
402 status = wh_callback_init (cb);
405 ERROR ("write_http plugin: wh_callback_init failed.");
406 pthread_mutex_unlock (&cb->send_lock);
411 status = format_json_value_list (cb->send_buffer,
412 &cb->send_buffer_fill,
413 &cb->send_buffer_free,
414 ds, vl, cb->store_rates);
415 if (status == (-ENOMEM))
417 status = wh_flush_nolock (/* timeout = */ 0, cb);
420 wh_reset_buffer (cb);
421 pthread_mutex_unlock (&cb->send_lock);
425 status = format_json_value_list (cb->send_buffer,
426 &cb->send_buffer_fill,
427 &cb->send_buffer_free,
428 ds, vl, cb->store_rates);
432 pthread_mutex_unlock (&cb->send_lock);
436 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
438 cb->send_buffer_fill, cb->send_buffer_size,
439 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
441 /* Check if we have enough space for this command. */
442 pthread_mutex_unlock (&cb->send_lock);
445 } /* }}} int wh_write_json */
447 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
448 user_data_t *user_data)
453 if (user_data == NULL)
456 cb = user_data->data;
458 if (cb->format == WH_FORMAT_JSON)
459 status = wh_write_json (ds, vl, cb);
461 status = wh_write_command (ds, vl, cb);
464 } /* }}} int wh_write */
466 static int config_set_format (wh_callback_t *cb, /* {{{ */
471 if ((ci->values_num != 1)
472 || (ci->values[0].type != OCONFIG_TYPE_STRING))
474 WARNING ("write_http plugin: The `%s' config option "
475 "needs exactly one string argument.", ci->key);
479 string = ci->values[0].value.string;
480 if (strcasecmp ("Command", string) == 0)
481 cb->format = WH_FORMAT_COMMAND;
482 else if (strcasecmp ("JSON", string) == 0)
483 cb->format = WH_FORMAT_JSON;
486 ERROR ("write_http plugin: Invalid format string: %s",
492 } /* }}} int config_set_format */
494 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
498 user_data_t user_data;
501 cb = malloc (sizeof (*cb));
504 ERROR ("write_http plugin: malloc failed.");
507 memset (cb, 0, sizeof (*cb));
510 cb->format = WH_FORMAT_COMMAND;
511 cb->sslversion = CURL_SSLVERSION_DEFAULT;
513 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
515 cf_util_get_string (ci, &cb->location);
516 if (cb->location == NULL)
519 for (i = 0; i < ci->children_num; i++)
521 oconfig_item_t *child = ci->children + i;
523 if (strcasecmp ("User", child->key) == 0)
524 cf_util_get_string (child, &cb->user);
525 else if (strcasecmp ("Password", child->key) == 0)
526 cf_util_get_string (child, &cb->pass);
527 else if (strcasecmp ("VerifyPeer", child->key) == 0)
528 cf_util_get_boolean (child, &cb->verify_peer);
529 else if (strcasecmp ("VerifyHost", child->key) == 0)
530 cf_util_get_boolean (child, &cb->verify_host);
531 else if (strcasecmp ("CACert", child->key) == 0)
532 cf_util_get_string (child, &cb->cacert);
533 else if (strcasecmp ("CAPath", child->key) == 0)
534 cf_util_get_string (child, &cb->capath);
535 else if (strcasecmp ("ClientKey", child->key) == 0)
536 cf_util_get_string (child, &cb->clientkey);
537 else if (strcasecmp ("ClientCert", child->key) == 0)
538 cf_util_get_string (child, &cb->clientcert);
539 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
540 cf_util_get_string (child, &cb->clientkeypass);
541 else if (strcasecmp ("SSLVersion", child->key) == 0)
545 cf_util_get_string (child, &value);
547 if (value == NULL || strcasecmp ("default", value) == 0)
548 cb->sslversion = CURL_SSLVERSION_DEFAULT;
549 else if (strcasecmp ("SSLv2", value) == 0)
550 cb->sslversion = CURL_SSLVERSION_SSLv2;
551 else if (strcasecmp ("SSLv3", value) == 0)
552 cb->sslversion = CURL_SSLVERSION_SSLv3;
553 else if (strcasecmp ("TLSv1", value) == 0)
554 cb->sslversion = CURL_SSLVERSION_TLSv1;
555 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
556 else if (strcasecmp ("TLSv1_0", value) == 0)
557 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
558 else if (strcasecmp ("TLSv1_1", value) == 0)
559 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
560 else if (strcasecmp ("TLSv1_2", value) == 0)
561 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
564 ERROR ("write_http plugin: Invalid SSLVersion "
565 "option: %s.", value);
569 else if (strcasecmp ("Format", child->key) == 0)
570 config_set_format (cb, child);
571 else if (strcasecmp ("StoreRates", child->key) == 0)
572 cf_util_get_boolean (child, &cb->store_rates);
573 else if (strcasecmp ("BufferSize", child->key) == 0)
574 cf_util_get_int (child, &buffer_size);
577 ERROR ("write_http plugin: Invalid configuration "
578 "option: %s.", child->key);
582 /* Determine send_buffer_size. */
583 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
584 if (buffer_size >= 1024)
585 cb->send_buffer_size = (size_t) buffer_size;
586 else if (buffer_size != 0)
587 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
590 /* Allocate the buffer. */
591 cb->send_buffer = malloc (cb->send_buffer_size);
592 if (cb->send_buffer == NULL)
594 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
595 wh_callback_free (cb);
598 /* Nulls the buffer and sets ..._free and ..._fill. */
599 wh_reset_buffer (cb);
601 DEBUG ("write_http: Registering write callback with URL %s",
604 memset (&user_data, 0, sizeof (user_data));
606 user_data.free_func = NULL;
607 plugin_register_flush ("write_http", wh_flush, &user_data);
609 user_data.free_func = wh_callback_free;
610 plugin_register_write ("write_http", wh_write, &user_data);
613 } /* }}} int wh_config_url */
615 static int wh_config (oconfig_item_t *ci) /* {{{ */
619 for (i = 0; i < ci->children_num; i++)
621 oconfig_item_t *child = ci->children + i;
623 if (strcasecmp ("URL", child->key) == 0)
624 wh_config_url (child);
627 ERROR ("write_http plugin: Invalid configuration "
628 "option: %s.", child->key);
633 } /* }}} int wh_config */
635 static int wh_init (void) /* {{{ */
637 /* Call this while collectd is still single-threaded to avoid
638 * initialization issues in libgcrypt. */
639 curl_global_init (CURL_GLOBAL_SSL);
641 } /* }}} int wh_init */
643 void module_register (void) /* {{{ */
645 plugin_register_complex_config ("write_http", wh_config);
646 plugin_register_init ("write_http", wh_init);
647 } /* }}} void module_register */
649 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */