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
64 time_t low_speed_time;
67 #define WH_FORMAT_COMMAND 0
68 #define WH_FORMAT_JSON 1
72 char curl_errbuf[CURL_ERROR_SIZE];
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 typedef struct wh_callback_s wh_callback_t;
84 static void wh_log_http_error (wh_callback_t *cb)
86 if (!cb->log_http_error)
91 curl_easy_getinfo (cb->curl, CURLINFO_RESPONSE_CODE, &http_code);
94 INFO ("write_http plugin: HTTP Error code: %lu", http_code);
97 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
99 memset (cb->send_buffer, 0, cb->send_buffer_size);
100 cb->send_buffer_free = cb->send_buffer_size;
101 cb->send_buffer_fill = 0;
102 cb->send_buffer_init_time = cdtime ();
104 if (cb->format == WH_FORMAT_JSON)
106 format_json_initialize (cb->send_buffer,
107 &cb->send_buffer_fill,
108 &cb->send_buffer_free);
110 } /* }}} wh_reset_buffer */
112 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
116 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
117 status = curl_easy_perform (cb->curl);
119 wh_log_http_error (cb);
121 if (status != CURLE_OK)
123 ERROR ("write_http plugin: curl_easy_perform failed with "
125 status, cb->curl_errbuf);
128 } /* }}} wh_send_buffer */
130 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
132 struct curl_slist *headers;
134 if (cb->curl != NULL)
137 cb->curl = curl_easy_init ();
138 if (cb->curl == NULL)
140 ERROR ("curl plugin: curl_easy_init failed.");
144 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0)
146 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_LIMIT,
147 (long) (cb->low_speed_limit * cb->low_speed_time));
148 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_TIME,
149 (long) cb->low_speed_time);
152 #ifdef HAVE_CURLOPT_TIMEOUT_MS
154 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout);
157 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
158 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
161 headers = curl_slist_append (headers, "Accept: */*");
162 if (cb->format == WH_FORMAT_JSON)
163 headers = curl_slist_append (headers, "Content-Type: application/json");
165 headers = curl_slist_append (headers, "Content-Type: text/plain");
166 headers = curl_slist_append (headers, "Expect:");
167 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
169 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
170 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
171 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
172 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
174 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);
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 = (char *) malloc (credentials_size);
188 if (cb->credentials == NULL)
190 ERROR ("curl plugin: malloc failed.");
194 ssnprintf (cb->credentials, credentials_size, "%s:%s",
195 cb->user, (cb->pass == NULL) ? "" : cb->pass);
196 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
198 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
201 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
202 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
203 cb->verify_host ? 2L : 0L);
204 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
205 if (cb->cacert != NULL)
206 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
207 if (cb->capath != NULL)
208 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
210 if (cb->clientkey != NULL && cb->clientcert != NULL)
212 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
213 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
215 if (cb->clientkeypass != NULL)
216 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
219 wh_reset_buffer (cb);
222 } /* }}} int wh_callback_init */
224 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
228 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
229 "send_buffer_fill = %zu;",
230 CDTIME_T_TO_DOUBLE (timeout),
231 cb->send_buffer_fill);
233 /* timeout == 0 => flush unconditionally */
239 if ((cb->send_buffer_init_time + timeout) > now)
243 if (cb->format == WH_FORMAT_COMMAND)
245 if (cb->send_buffer_fill <= 0)
247 cb->send_buffer_init_time = cdtime ();
251 status = wh_send_buffer (cb);
252 wh_reset_buffer (cb);
254 else if (cb->format == WH_FORMAT_JSON)
256 if (cb->send_buffer_fill <= 2)
258 cb->send_buffer_init_time = cdtime ();
262 status = format_json_finalize (cb->send_buffer,
263 &cb->send_buffer_fill,
264 &cb->send_buffer_free);
267 ERROR ("write_http: wh_flush_nolock: "
268 "format_json_finalize failed.");
269 wh_reset_buffer (cb);
273 status = wh_send_buffer (cb);
274 wh_reset_buffer (cb);
278 ERROR ("write_http: wh_flush_nolock: "
279 "Unknown format: %i",
285 } /* }}} wh_flush_nolock */
287 static int wh_flush (cdtime_t timeout, /* {{{ */
288 const char *identifier __attribute__((unused)),
289 user_data_t *user_data)
294 if (user_data == NULL)
297 cb = user_data->data;
299 pthread_mutex_lock (&cb->send_lock);
301 if (cb->curl == NULL)
303 status = wh_callback_init (cb);
306 ERROR ("write_http plugin: wh_callback_init failed.");
307 pthread_mutex_unlock (&cb->send_lock);
312 status = wh_flush_nolock (timeout, cb);
313 pthread_mutex_unlock (&cb->send_lock);
316 } /* }}} int wh_flush */
318 static void wh_callback_free (void *data) /* {{{ */
327 wh_flush_nolock (/* timeout = */ 0, cb);
329 if (cb->curl != NULL)
331 curl_easy_cleanup (cb->curl);
335 sfree (cb->location);
338 sfree (cb->credentials);
341 sfree (cb->clientkey);
342 sfree (cb->clientcert);
343 sfree (cb->clientkeypass);
344 sfree (cb->send_buffer);
347 } /* }}} void wh_callback_free */
349 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
352 char key[10*DATA_MAX_NAME_LEN];
359 if (0 != strcmp (ds->type, vl->type)) {
360 ERROR ("write_http plugin: DS type does not match "
365 /* Copy the identifier to `key' and escape it. */
366 status = FORMAT_VL (key, sizeof (key), vl);
368 ERROR ("write_http plugin: error with format_name");
371 escape_string (key, sizeof (key));
373 /* Convert the values to an ASCII representation and put that into
375 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
377 ERROR ("write_http plugin: error with "
378 "wh_value_list_to_string");
382 command_len = (size_t) ssnprintf (command, sizeof (command),
383 "PUTVAL %s interval=%.3f %s\r\n",
385 CDTIME_T_TO_DOUBLE (vl->interval),
387 if (command_len >= sizeof (command)) {
388 ERROR ("write_http plugin: Command buffer too small: "
389 "Need %zu bytes.", command_len + 1);
393 pthread_mutex_lock (&cb->send_lock);
395 if (cb->curl == NULL)
397 status = wh_callback_init (cb);
400 ERROR ("write_http plugin: wh_callback_init failed.");
401 pthread_mutex_unlock (&cb->send_lock);
406 if (command_len >= cb->send_buffer_free)
408 status = wh_flush_nolock (/* timeout = */ 0, cb);
411 pthread_mutex_unlock (&cb->send_lock);
415 assert (command_len < cb->send_buffer_free);
417 /* `command_len + 1' because `command_len' does not include the
418 * trailing null byte. Neither does `send_buffer_fill'. */
419 memcpy (cb->send_buffer + cb->send_buffer_fill,
420 command, command_len + 1);
421 cb->send_buffer_fill += command_len;
422 cb->send_buffer_free -= command_len;
424 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
426 cb->send_buffer_fill, cb->send_buffer_size,
427 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
430 /* Check if we have enough space for this command. */
431 pthread_mutex_unlock (&cb->send_lock);
434 } /* }}} int wh_write_command */
436 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
441 pthread_mutex_lock (&cb->send_lock);
443 if (cb->curl == NULL)
445 status = wh_callback_init (cb);
448 ERROR ("write_http plugin: wh_callback_init failed.");
449 pthread_mutex_unlock (&cb->send_lock);
454 status = format_json_value_list (cb->send_buffer,
455 &cb->send_buffer_fill,
456 &cb->send_buffer_free,
457 ds, vl, cb->store_rates);
458 if (status == (-ENOMEM))
460 status = wh_flush_nolock (/* timeout = */ 0, cb);
463 wh_reset_buffer (cb);
464 pthread_mutex_unlock (&cb->send_lock);
468 status = format_json_value_list (cb->send_buffer,
469 &cb->send_buffer_fill,
470 &cb->send_buffer_free,
471 ds, vl, cb->store_rates);
475 pthread_mutex_unlock (&cb->send_lock);
479 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
481 cb->send_buffer_fill, cb->send_buffer_size,
482 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
484 /* Check if we have enough space for this command. */
485 pthread_mutex_unlock (&cb->send_lock);
488 } /* }}} int wh_write_json */
490 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
491 user_data_t *user_data)
496 if (user_data == NULL)
499 cb = user_data->data;
501 if (cb->format == WH_FORMAT_JSON)
502 status = wh_write_json (ds, vl, cb);
504 status = wh_write_command (ds, vl, cb);
507 } /* }}} int wh_write */
509 static int config_set_format (wh_callback_t *cb, /* {{{ */
514 if ((ci->values_num != 1)
515 || (ci->values[0].type != OCONFIG_TYPE_STRING))
517 WARNING ("write_http plugin: The `%s' config option "
518 "needs exactly one string argument.", ci->key);
522 string = ci->values[0].value.string;
523 if (strcasecmp ("Command", string) == 0)
524 cb->format = WH_FORMAT_COMMAND;
525 else if (strcasecmp ("JSON", string) == 0)
526 cb->format = WH_FORMAT_JSON;
529 ERROR ("write_http plugin: Invalid format string: %s",
535 } /* }}} int config_set_format */
537 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
541 user_data_t user_data;
542 char callback_name[DATA_MAX_NAME_LEN];
545 cb = malloc (sizeof (*cb));
548 ERROR ("write_http plugin: malloc failed.");
551 memset (cb, 0, sizeof (*cb));
554 cb->format = WH_FORMAT_COMMAND;
555 cb->sslversion = CURL_SSLVERSION_DEFAULT;
556 cb->low_speed_limit = 0;
558 cb->log_http_error = 0;
560 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
562 cf_util_get_string (ci, &cb->name);
564 /* FIXME: Remove this legacy mode in version 6. */
565 if (strcasecmp ("URL", ci->key) == 0)
566 cf_util_get_string (ci, &cb->location);
568 for (i = 0; i < ci->children_num; i++)
570 oconfig_item_t *child = ci->children + i;
572 if (strcasecmp ("URL", child->key) == 0)
573 cf_util_get_string (child, &cb->location);
574 else if (strcasecmp ("User", child->key) == 0)
575 cf_util_get_string (child, &cb->user);
576 else if (strcasecmp ("Password", child->key) == 0)
577 cf_util_get_string (child, &cb->pass);
578 else if (strcasecmp ("VerifyPeer", child->key) == 0)
579 cf_util_get_boolean (child, &cb->verify_peer);
580 else if (strcasecmp ("VerifyHost", child->key) == 0)
581 cf_util_get_boolean (child, &cb->verify_host);
582 else if (strcasecmp ("CACert", child->key) == 0)
583 cf_util_get_string (child, &cb->cacert);
584 else if (strcasecmp ("CAPath", child->key) == 0)
585 cf_util_get_string (child, &cb->capath);
586 else if (strcasecmp ("ClientKey", child->key) == 0)
587 cf_util_get_string (child, &cb->clientkey);
588 else if (strcasecmp ("ClientCert", child->key) == 0)
589 cf_util_get_string (child, &cb->clientcert);
590 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
591 cf_util_get_string (child, &cb->clientkeypass);
592 else if (strcasecmp ("SSLVersion", child->key) == 0)
596 cf_util_get_string (child, &value);
598 if (value == NULL || strcasecmp ("default", value) == 0)
599 cb->sslversion = CURL_SSLVERSION_DEFAULT;
600 else if (strcasecmp ("SSLv2", value) == 0)
601 cb->sslversion = CURL_SSLVERSION_SSLv2;
602 else if (strcasecmp ("SSLv3", value) == 0)
603 cb->sslversion = CURL_SSLVERSION_SSLv3;
604 else if (strcasecmp ("TLSv1", value) == 0)
605 cb->sslversion = CURL_SSLVERSION_TLSv1;
606 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
607 else if (strcasecmp ("TLSv1_0", value) == 0)
608 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
609 else if (strcasecmp ("TLSv1_1", value) == 0)
610 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
611 else if (strcasecmp ("TLSv1_2", value) == 0)
612 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
615 ERROR ("write_http plugin: Invalid SSLVersion "
616 "option: %s.", value);
620 else if (strcasecmp ("Format", child->key) == 0)
621 config_set_format (cb, child);
622 else if (strcasecmp ("StoreRates", child->key) == 0)
623 cf_util_get_boolean (child, &cb->store_rates);
624 else if (strcasecmp ("BufferSize", child->key) == 0)
625 cf_util_get_int (child, &buffer_size);
626 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
627 cf_util_get_int (child, &cb->low_speed_limit);
628 else if (strcasecmp ("Timeout", child->key) == 0)
629 cf_util_get_int (child, &cb->timeout);
630 else if (strcasecmp ("LogHttpError", child->key) == 0)
631 cf_util_get_boolean (child, &cb->log_http_error);
634 ERROR ("write_http plugin: Invalid configuration "
635 "option: %s.", child->key);
639 if (cb->location == NULL)
641 ERROR ("write_http plugin: no URL defined for instance '%s'",
643 wh_callback_free (cb);
647 if (cb->low_speed_limit > 0)
648 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
650 /* Determine send_buffer_size. */
651 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
652 if (buffer_size >= 1024)
653 cb->send_buffer_size = (size_t) buffer_size;
654 else if (buffer_size != 0)
655 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
658 /* Allocate the buffer. */
659 cb->send_buffer = malloc (cb->send_buffer_size);
660 if (cb->send_buffer == NULL)
662 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
663 wh_callback_free (cb);
666 /* Nulls the buffer and sets ..._free and ..._fill. */
667 wh_reset_buffer (cb);
669 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
671 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
672 callback_name, cb->location);
674 memset (&user_data, 0, sizeof (user_data));
676 user_data.free_func = NULL;
677 plugin_register_flush (callback_name, wh_flush, &user_data);
679 user_data.free_func = wh_callback_free;
680 plugin_register_write (callback_name, wh_write, &user_data);
683 } /* }}} int wh_config_node */
685 static int wh_config (oconfig_item_t *ci) /* {{{ */
689 for (i = 0; i < ci->children_num; i++)
691 oconfig_item_t *child = ci->children + i;
693 if (strcasecmp ("Node", child->key) == 0)
694 wh_config_node (child);
695 /* FIXME: Remove this legacy mode in version 6. */
696 else if (strcasecmp ("URL", child->key) == 0) {
697 WARNING ("write_http plugin: Legacy <URL> block found. "
698 "Please use <Node> instead.");
699 wh_config_node (child);
703 ERROR ("write_http plugin: Invalid configuration "
704 "option: %s.", child->key);
709 } /* }}} int wh_config */
711 static int wh_init (void) /* {{{ */
713 /* Call this while collectd is still single-threaded to avoid
714 * initialization issues in libgcrypt. */
715 curl_global_init (CURL_GLOBAL_SSL);
717 } /* }}} int wh_init */
719 void module_register (void) /* {{{ */
721 plugin_register_complex_config ("write_http", wh_config);
722 plugin_register_init ("write_http", wh_init);
723 } /* }}} void module_register */
725 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */