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 time_t low_speed_time;
66 #define WH_FORMAT_COMMAND 0
67 #define WH_FORMAT_JSON 1
71 char curl_errbuf[CURL_ERROR_SIZE];
74 size_t send_buffer_size;
75 size_t send_buffer_free;
76 size_t send_buffer_fill;
77 cdtime_t send_buffer_init_time;
79 pthread_mutex_t send_lock;
81 typedef struct wh_callback_s wh_callback_t;
83 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
85 memset (cb->send_buffer, 0, cb->send_buffer_size);
86 cb->send_buffer_free = cb->send_buffer_size;
87 cb->send_buffer_fill = 0;
88 cb->send_buffer_init_time = cdtime ();
90 if (cb->format == WH_FORMAT_JSON)
92 format_json_initialize (cb->send_buffer,
93 &cb->send_buffer_fill,
94 &cb->send_buffer_free);
96 } /* }}} wh_reset_buffer */
98 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
102 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
103 status = curl_easy_perform (cb->curl);
104 if (status != CURLE_OK)
106 ERROR ("write_http plugin: curl_easy_perform failed with "
108 status, cb->curl_errbuf);
111 } /* }}} wh_send_buffer */
113 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
115 struct curl_slist *headers;
117 if (cb->curl != NULL)
120 cb->curl = curl_easy_init ();
121 if (cb->curl == NULL)
123 ERROR ("curl plugin: curl_easy_init failed.");
127 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0)
129 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_LIMIT,
130 (long) (cb->low_speed_limit * cb->low_speed_time));
131 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_TIME,
132 (long) cb->low_speed_time);
135 #ifdef HAVE_CURLOPT_TIMEOUT_MS
137 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout);
140 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
141 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
144 headers = curl_slist_append (headers, "Accept: */*");
145 if (cb->format == WH_FORMAT_JSON)
146 headers = curl_slist_append (headers, "Content-Type: application/json");
148 headers = curl_slist_append (headers, "Content-Type: text/plain");
149 headers = curl_slist_append (headers, "Expect:");
150 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
152 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
153 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
154 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
155 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
157 if (cb->user != NULL)
159 #ifdef HAVE_CURLOPT_USERNAME
160 curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user);
161 curl_easy_setopt (cb->curl, CURLOPT_PASSWORD,
162 (cb->pass == NULL) ? "" : cb->pass);
164 size_t credentials_size;
166 credentials_size = strlen (cb->user) + 2;
167 if (cb->pass != NULL)
168 credentials_size += strlen (cb->pass);
170 cb->credentials = (char *) malloc (credentials_size);
171 if (cb->credentials == NULL)
173 ERROR ("curl plugin: malloc failed.");
177 ssnprintf (cb->credentials, credentials_size, "%s:%s",
178 cb->user, (cb->pass == NULL) ? "" : cb->pass);
179 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
181 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
184 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
185 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
186 cb->verify_host ? 2L : 0L);
187 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
188 if (cb->cacert != NULL)
189 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
190 if (cb->capath != NULL)
191 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
193 if (cb->clientkey != NULL && cb->clientcert != NULL)
195 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
196 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
198 if (cb->clientkeypass != NULL)
199 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
202 wh_reset_buffer (cb);
205 } /* }}} int wh_callback_init */
207 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
211 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
212 "send_buffer_fill = %zu;",
213 CDTIME_T_TO_DOUBLE (timeout),
214 cb->send_buffer_fill);
216 /* timeout == 0 => flush unconditionally */
222 if ((cb->send_buffer_init_time + timeout) > now)
226 if (cb->format == WH_FORMAT_COMMAND)
228 if (cb->send_buffer_fill <= 0)
230 cb->send_buffer_init_time = cdtime ();
234 status = wh_send_buffer (cb);
235 wh_reset_buffer (cb);
237 else if (cb->format == WH_FORMAT_JSON)
239 if (cb->send_buffer_fill <= 2)
241 cb->send_buffer_init_time = cdtime ();
245 status = format_json_finalize (cb->send_buffer,
246 &cb->send_buffer_fill,
247 &cb->send_buffer_free);
250 ERROR ("write_http: wh_flush_nolock: "
251 "format_json_finalize failed.");
252 wh_reset_buffer (cb);
256 status = wh_send_buffer (cb);
257 wh_reset_buffer (cb);
261 ERROR ("write_http: wh_flush_nolock: "
262 "Unknown format: %i",
268 } /* }}} wh_flush_nolock */
270 static int wh_flush (cdtime_t timeout, /* {{{ */
271 const char *identifier __attribute__((unused)),
272 user_data_t *user_data)
277 if (user_data == NULL)
280 cb = user_data->data;
282 pthread_mutex_lock (&cb->send_lock);
284 if (cb->curl == NULL)
286 status = wh_callback_init (cb);
289 ERROR ("write_http plugin: wh_callback_init failed.");
290 pthread_mutex_unlock (&cb->send_lock);
295 status = wh_flush_nolock (timeout, cb);
296 pthread_mutex_unlock (&cb->send_lock);
299 } /* }}} int wh_flush */
301 static void wh_callback_free (void *data) /* {{{ */
310 wh_flush_nolock (/* timeout = */ 0, cb);
312 if (cb->curl != NULL)
314 curl_easy_cleanup (cb->curl);
318 sfree (cb->location);
321 sfree (cb->credentials);
324 sfree (cb->clientkey);
325 sfree (cb->clientcert);
326 sfree (cb->clientkeypass);
327 sfree (cb->send_buffer);
330 } /* }}} void wh_callback_free */
332 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
335 char key[10*DATA_MAX_NAME_LEN];
342 if (0 != strcmp (ds->type, vl->type)) {
343 ERROR ("write_http plugin: DS type does not match "
348 /* Copy the identifier to `key' and escape it. */
349 status = FORMAT_VL (key, sizeof (key), vl);
351 ERROR ("write_http plugin: error with format_name");
354 escape_string (key, sizeof (key));
356 /* Convert the values to an ASCII representation and put that into
358 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
360 ERROR ("write_http plugin: error with "
361 "wh_value_list_to_string");
365 command_len = (size_t) ssnprintf (command, sizeof (command),
366 "PUTVAL %s interval=%.3f %s\r\n",
368 CDTIME_T_TO_DOUBLE (vl->interval),
370 if (command_len >= sizeof (command)) {
371 ERROR ("write_http plugin: Command buffer too small: "
372 "Need %zu bytes.", command_len + 1);
376 pthread_mutex_lock (&cb->send_lock);
378 if (cb->curl == NULL)
380 status = wh_callback_init (cb);
383 ERROR ("write_http plugin: wh_callback_init failed.");
384 pthread_mutex_unlock (&cb->send_lock);
389 if (command_len >= cb->send_buffer_free)
391 status = wh_flush_nolock (/* timeout = */ 0, cb);
394 pthread_mutex_unlock (&cb->send_lock);
398 assert (command_len < cb->send_buffer_free);
400 /* `command_len + 1' because `command_len' does not include the
401 * trailing null byte. Neither does `send_buffer_fill'. */
402 memcpy (cb->send_buffer + cb->send_buffer_fill,
403 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\"",
409 cb->send_buffer_fill, cb->send_buffer_size,
410 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
413 /* Check if we have enough space for this command. */
414 pthread_mutex_unlock (&cb->send_lock);
417 } /* }}} int wh_write_command */
419 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
424 pthread_mutex_lock (&cb->send_lock);
426 if (cb->curl == NULL)
428 status = wh_callback_init (cb);
431 ERROR ("write_http plugin: wh_callback_init failed.");
432 pthread_mutex_unlock (&cb->send_lock);
437 status = format_json_value_list (cb->send_buffer,
438 &cb->send_buffer_fill,
439 &cb->send_buffer_free,
440 ds, vl, cb->store_rates);
441 if (status == (-ENOMEM))
443 status = wh_flush_nolock (/* timeout = */ 0, cb);
446 wh_reset_buffer (cb);
447 pthread_mutex_unlock (&cb->send_lock);
451 status = format_json_value_list (cb->send_buffer,
452 &cb->send_buffer_fill,
453 &cb->send_buffer_free,
454 ds, vl, cb->store_rates);
458 pthread_mutex_unlock (&cb->send_lock);
462 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
464 cb->send_buffer_fill, cb->send_buffer_size,
465 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
467 /* Check if we have enough space for this command. */
468 pthread_mutex_unlock (&cb->send_lock);
471 } /* }}} int wh_write_json */
473 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
474 user_data_t *user_data)
479 if (user_data == NULL)
482 cb = user_data->data;
484 if (cb->format == WH_FORMAT_JSON)
485 status = wh_write_json (ds, vl, cb);
487 status = wh_write_command (ds, vl, cb);
490 } /* }}} int wh_write */
492 static int config_set_format (wh_callback_t *cb, /* {{{ */
497 if ((ci->values_num != 1)
498 || (ci->values[0].type != OCONFIG_TYPE_STRING))
500 WARNING ("write_http plugin: The `%s' config option "
501 "needs exactly one string argument.", ci->key);
505 string = ci->values[0].value.string;
506 if (strcasecmp ("Command", string) == 0)
507 cb->format = WH_FORMAT_COMMAND;
508 else if (strcasecmp ("JSON", string) == 0)
509 cb->format = WH_FORMAT_JSON;
512 ERROR ("write_http plugin: Invalid format string: %s",
518 } /* }}} int config_set_format */
520 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
524 user_data_t user_data;
525 char callback_name[DATA_MAX_NAME_LEN];
528 cb = malloc (sizeof (*cb));
531 ERROR ("write_http plugin: malloc failed.");
534 memset (cb, 0, sizeof (*cb));
537 cb->format = WH_FORMAT_COMMAND;
538 cb->sslversion = CURL_SSLVERSION_DEFAULT;
539 cb->low_speed_limit = 0;
542 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
544 cf_util_get_string (ci, &cb->name);
546 /* FIXME: Remove this legacy mode in version 6. */
547 if (strcasecmp ("URL", ci->key) == 0)
548 cf_util_get_string (ci, &cb->location);
550 for (i = 0; i < ci->children_num; i++)
552 oconfig_item_t *child = ci->children + i;
554 if (strcasecmp ("URL", child->key) == 0)
555 cf_util_get_string (child, &cb->location);
556 else if (strcasecmp ("User", child->key) == 0)
557 cf_util_get_string (child, &cb->user);
558 else if (strcasecmp ("Password", child->key) == 0)
559 cf_util_get_string (child, &cb->pass);
560 else if (strcasecmp ("VerifyPeer", child->key) == 0)
561 cf_util_get_boolean (child, &cb->verify_peer);
562 else if (strcasecmp ("VerifyHost", child->key) == 0)
563 cf_util_get_boolean (child, &cb->verify_host);
564 else if (strcasecmp ("CACert", child->key) == 0)
565 cf_util_get_string (child, &cb->cacert);
566 else if (strcasecmp ("CAPath", child->key) == 0)
567 cf_util_get_string (child, &cb->capath);
568 else if (strcasecmp ("ClientKey", child->key) == 0)
569 cf_util_get_string (child, &cb->clientkey);
570 else if (strcasecmp ("ClientCert", child->key) == 0)
571 cf_util_get_string (child, &cb->clientcert);
572 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
573 cf_util_get_string (child, &cb->clientkeypass);
574 else if (strcasecmp ("SSLVersion", child->key) == 0)
578 cf_util_get_string (child, &value);
580 if (value == NULL || strcasecmp ("default", value) == 0)
581 cb->sslversion = CURL_SSLVERSION_DEFAULT;
582 else if (strcasecmp ("SSLv2", value) == 0)
583 cb->sslversion = CURL_SSLVERSION_SSLv2;
584 else if (strcasecmp ("SSLv3", value) == 0)
585 cb->sslversion = CURL_SSLVERSION_SSLv3;
586 else if (strcasecmp ("TLSv1", value) == 0)
587 cb->sslversion = CURL_SSLVERSION_TLSv1;
588 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
589 else if (strcasecmp ("TLSv1_0", value) == 0)
590 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
591 else if (strcasecmp ("TLSv1_1", value) == 0)
592 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
593 else if (strcasecmp ("TLSv1_2", value) == 0)
594 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
597 ERROR ("write_http plugin: Invalid SSLVersion "
598 "option: %s.", value);
602 else if (strcasecmp ("Format", child->key) == 0)
603 config_set_format (cb, child);
604 else if (strcasecmp ("StoreRates", child->key) == 0)
605 cf_util_get_boolean (child, &cb->store_rates);
606 else if (strcasecmp ("BufferSize", child->key) == 0)
607 cf_util_get_int (child, &buffer_size);
608 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
609 cf_util_get_int (child, &cb->low_speed_limit);
610 else if (strcasecmp ("Timeout", child->key) == 0)
611 cf_util_get_int (child, &cb->timeout);
614 ERROR ("write_http plugin: Invalid configuration "
615 "option: %s.", child->key);
619 if (cb->location == NULL)
621 ERROR ("write_http plugin: no URL defined for instance '%s'",
623 wh_callback_free (cb);
627 if (cb->low_speed_limit > 0)
628 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
630 /* Determine send_buffer_size. */
631 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
632 if (buffer_size >= 1024)
633 cb->send_buffer_size = (size_t) buffer_size;
634 else if (buffer_size != 0)
635 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
638 /* Allocate the buffer. */
639 cb->send_buffer = malloc (cb->send_buffer_size);
640 if (cb->send_buffer == NULL)
642 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
643 wh_callback_free (cb);
646 /* Nulls the buffer and sets ..._free and ..._fill. */
647 wh_reset_buffer (cb);
649 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
651 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
652 callback_name, cb->location);
654 memset (&user_data, 0, sizeof (user_data));
656 user_data.free_func = NULL;
657 plugin_register_flush (callback_name, wh_flush, &user_data);
659 user_data.free_func = wh_callback_free;
660 plugin_register_write (callback_name, wh_write, &user_data);
663 } /* }}} int wh_config_node */
665 static int wh_config (oconfig_item_t *ci) /* {{{ */
669 for (i = 0; i < ci->children_num; i++)
671 oconfig_item_t *child = ci->children + i;
673 if (strcasecmp ("Node", child->key) == 0)
674 wh_config_node (child);
675 /* FIXME: Remove this legacy mode in version 6. */
676 else if (strcasecmp ("URL", child->key) == 0) {
677 WARNING ("write_http plugin: Legacy <URL> block found. "
678 "Please use <Node> instead.");
679 wh_config_node (child);
683 ERROR ("write_http plugin: Invalid configuration "
684 "option: %s.", child->key);
689 } /* }}} int wh_config */
691 static int wh_init (void) /* {{{ */
693 /* Call this while collectd is still single-threaded to avoid
694 * initialization issues in libgcrypt. */
695 curl_global_init (CURL_GLOBAL_SSL);
697 } /* }}} int wh_init */
699 void module_register (void) /* {{{ */
701 plugin_register_complex_config ("write_http", wh_config);
702 plugin_register_init ("write_http", wh_init);
703 } /* }}} void module_register */
705 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */