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_cache.h"
30 #include "utils_format_json.h"
32 #if HAVE_PTHREAD_H
33 # include <pthread.h>
34 #endif
36 #include <curl/curl.h>
38 #ifndef WRITE_HTTP_DEFAULT_BUFFER_SIZE
39 # define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096
40 #endif
42 /*
43 * Private variables
44 */
45 struct wh_callback_s
46 {
47 char *name;
49 char *location;
50 char *user;
51 char *pass;
52 char *credentials;
53 _Bool verify_peer;
54 _Bool verify_host;
55 char *cacert;
56 char *capath;
57 char *clientkey;
58 char *clientcert;
59 char *clientkeypass;
60 long sslversion;
61 _Bool store_rates;
62 int low_speed_limit;
63 time_t low_speed_time;
64 int timeout;
66 #define WH_FORMAT_COMMAND 0
67 #define WH_FORMAT_JSON 1
68 int format;
70 CURL *curl;
71 char curl_errbuf[CURL_ERROR_SIZE];
73 char *send_buffer;
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;
80 };
81 typedef struct wh_callback_s wh_callback_t;
83 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
84 {
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)
91 {
92 format_json_initialize (cb->send_buffer,
93 &cb->send_buffer_fill,
94 &cb->send_buffer_free);
95 }
96 } /* }}} wh_reset_buffer */
98 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
99 {
100 int status = 0;
102 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
103 status = curl_easy_perform (cb->curl);
104 if (status != CURLE_OK)
105 {
106 ERROR ("write_http plugin: curl_easy_perform failed with "
107 "status %i: %s",
108 status, cb->curl_errbuf);
109 }
110 return (status);
111 } /* }}} wh_send_buffer */
113 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
114 {
115 struct curl_slist *headers;
117 if (cb->curl != NULL)
118 return (0);
120 cb->curl = curl_easy_init ();
121 if (cb->curl == NULL)
122 {
123 ERROR ("curl plugin: curl_easy_init failed.");
124 return (-1);
125 }
127 if (cb->low_speed_limit > 0 && cb->low_speed_time > 0)
128 {
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);
133 }
135 if (cb->timeout > 0)
136 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout);
138 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
139 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
141 headers = NULL;
142 headers = curl_slist_append (headers, "Accept: */*");
143 if (cb->format == WH_FORMAT_JSON)
144 headers = curl_slist_append (headers, "Content-Type: application/json");
145 else
146 headers = curl_slist_append (headers, "Content-Type: text/plain");
147 headers = curl_slist_append (headers, "Expect:");
148 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
150 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
151 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
152 curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
153 curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
155 if (cb->user != NULL)
156 {
157 #ifdef HAVE_CURLOPT_USERNAME
158 curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user);
159 curl_easy_setopt (cb->curl, CURLOPT_PASSWORD,
160 (cb->pass == NULL) ? "" : cb->pass);
161 #else
162 size_t credentials_size;
164 credentials_size = strlen (cb->user) + 2;
165 if (cb->pass != NULL)
166 credentials_size += strlen (cb->pass);
168 cb->credentials = (char *) malloc (credentials_size);
169 if (cb->credentials == NULL)
170 {
171 ERROR ("curl plugin: malloc failed.");
172 return (-1);
173 }
175 ssnprintf (cb->credentials, credentials_size, "%s:%s",
176 cb->user, (cb->pass == NULL) ? "" : cb->pass);
177 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
178 #endif
179 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
180 }
182 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
183 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
184 cb->verify_host ? 2L : 0L);
185 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
186 if (cb->cacert != NULL)
187 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
188 if (cb->capath != NULL)
189 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
191 if (cb->clientkey != NULL && cb->clientcert != NULL)
192 {
193 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
194 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
196 if (cb->clientkeypass != NULL)
197 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
198 }
200 wh_reset_buffer (cb);
202 return (0);
203 } /* }}} int wh_callback_init */
205 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
206 {
207 int status;
209 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
210 "send_buffer_fill = %zu;",
211 CDTIME_T_TO_DOUBLE (timeout),
212 cb->send_buffer_fill);
214 /* timeout == 0 => flush unconditionally */
215 if (timeout > 0)
216 {
217 cdtime_t now;
219 now = cdtime ();
220 if ((cb->send_buffer_init_time + timeout) > now)
221 return (0);
222 }
224 if (cb->format == WH_FORMAT_COMMAND)
225 {
226 if (cb->send_buffer_fill <= 0)
227 {
228 cb->send_buffer_init_time = cdtime ();
229 return (0);
230 }
232 status = wh_send_buffer (cb);
233 wh_reset_buffer (cb);
234 }
235 else if (cb->format == WH_FORMAT_JSON)
236 {
237 if (cb->send_buffer_fill <= 2)
238 {
239 cb->send_buffer_init_time = cdtime ();
240 return (0);
241 }
243 status = format_json_finalize (cb->send_buffer,
244 &cb->send_buffer_fill,
245 &cb->send_buffer_free);
246 if (status != 0)
247 {
248 ERROR ("write_http: wh_flush_nolock: "
249 "format_json_finalize failed.");
250 wh_reset_buffer (cb);
251 return (status);
252 }
254 status = wh_send_buffer (cb);
255 wh_reset_buffer (cb);
256 }
257 else
258 {
259 ERROR ("write_http: wh_flush_nolock: "
260 "Unknown format: %i",
261 cb->format);
262 return (-1);
263 }
265 return (status);
266 } /* }}} wh_flush_nolock */
268 static int wh_flush (cdtime_t timeout, /* {{{ */
269 const char *identifier __attribute__((unused)),
270 user_data_t *user_data)
271 {
272 wh_callback_t *cb;
273 int status;
275 if (user_data == NULL)
276 return (-EINVAL);
278 cb = user_data->data;
280 pthread_mutex_lock (&cb->send_lock);
282 if (cb->curl == NULL)
283 {
284 status = wh_callback_init (cb);
285 if (status != 0)
286 {
287 ERROR ("write_http plugin: wh_callback_init failed.");
288 pthread_mutex_unlock (&cb->send_lock);
289 return (-1);
290 }
291 }
293 status = wh_flush_nolock (timeout, cb);
294 pthread_mutex_unlock (&cb->send_lock);
296 return (status);
297 } /* }}} int wh_flush */
299 static void wh_callback_free (void *data) /* {{{ */
300 {
301 wh_callback_t *cb;
303 if (data == NULL)
304 return;
306 cb = data;
308 wh_flush_nolock (/* timeout = */ 0, cb);
310 if (cb->curl != NULL)
311 {
312 curl_easy_cleanup (cb->curl);
313 cb->curl = NULL;
314 }
315 sfree (cb->name);
316 sfree (cb->location);
317 sfree (cb->user);
318 sfree (cb->pass);
319 sfree (cb->credentials);
320 sfree (cb->cacert);
321 sfree (cb->capath);
322 sfree (cb->clientkey);
323 sfree (cb->clientcert);
324 sfree (cb->clientkeypass);
325 sfree (cb->send_buffer);
327 sfree (cb);
328 } /* }}} void wh_callback_free */
330 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
331 wh_callback_t *cb)
332 {
333 char key[10*DATA_MAX_NAME_LEN];
334 char values[512];
335 char command[1024];
336 size_t command_len;
338 int status;
340 if (0 != strcmp (ds->type, vl->type)) {
341 ERROR ("write_http plugin: DS type does not match "
342 "value list type");
343 return -1;
344 }
346 /* Copy the identifier to `key' and escape it. */
347 status = FORMAT_VL (key, sizeof (key), vl);
348 if (status != 0) {
349 ERROR ("write_http plugin: error with format_name");
350 return (status);
351 }
352 escape_string (key, sizeof (key));
354 /* Convert the values to an ASCII representation and put that into
355 * `values'. */
356 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
357 if (status != 0) {
358 ERROR ("write_http plugin: error with "
359 "wh_value_list_to_string");
360 return (status);
361 }
363 command_len = (size_t) ssnprintf (command, sizeof (command),
364 "PUTVAL %s interval=%.3f %s\r\n",
365 key,
366 CDTIME_T_TO_DOUBLE (vl->interval),
367 values);
368 if (command_len >= sizeof (command)) {
369 ERROR ("write_http plugin: Command buffer too small: "
370 "Need %zu bytes.", command_len + 1);
371 return (-1);
372 }
374 pthread_mutex_lock (&cb->send_lock);
376 if (cb->curl == NULL)
377 {
378 status = wh_callback_init (cb);
379 if (status != 0)
380 {
381 ERROR ("write_http plugin: wh_callback_init failed.");
382 pthread_mutex_unlock (&cb->send_lock);
383 return (-1);
384 }
385 }
387 if (command_len >= cb->send_buffer_free)
388 {
389 status = wh_flush_nolock (/* timeout = */ 0, cb);
390 if (status != 0)
391 {
392 pthread_mutex_unlock (&cb->send_lock);
393 return (status);
394 }
395 }
396 assert (command_len < cb->send_buffer_free);
398 /* `command_len + 1' because `command_len' does not include the
399 * trailing null byte. Neither does `send_buffer_fill'. */
400 memcpy (cb->send_buffer + cb->send_buffer_fill,
401 command, command_len + 1);
402 cb->send_buffer_fill += command_len;
403 cb->send_buffer_free -= command_len;
405 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
406 cb->location,
407 cb->send_buffer_fill, cb->send_buffer_size,
408 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
409 command);
411 /* Check if we have enough space for this command. */
412 pthread_mutex_unlock (&cb->send_lock);
414 return (0);
415 } /* }}} int wh_write_command */
417 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
418 wh_callback_t *cb)
419 {
420 int status;
422 pthread_mutex_lock (&cb->send_lock);
424 if (cb->curl == NULL)
425 {
426 status = wh_callback_init (cb);
427 if (status != 0)
428 {
429 ERROR ("write_http plugin: wh_callback_init failed.");
430 pthread_mutex_unlock (&cb->send_lock);
431 return (-1);
432 }
433 }
435 status = format_json_value_list (cb->send_buffer,
436 &cb->send_buffer_fill,
437 &cb->send_buffer_free,
438 ds, vl, cb->store_rates);
439 if (status == (-ENOMEM))
440 {
441 status = wh_flush_nolock (/* timeout = */ 0, cb);
442 if (status != 0)
443 {
444 wh_reset_buffer (cb);
445 pthread_mutex_unlock (&cb->send_lock);
446 return (status);
447 }
449 status = format_json_value_list (cb->send_buffer,
450 &cb->send_buffer_fill,
451 &cb->send_buffer_free,
452 ds, vl, cb->store_rates);
453 }
454 if (status != 0)
455 {
456 pthread_mutex_unlock (&cb->send_lock);
457 return (status);
458 }
460 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
461 cb->location,
462 cb->send_buffer_fill, cb->send_buffer_size,
463 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
465 /* Check if we have enough space for this command. */
466 pthread_mutex_unlock (&cb->send_lock);
468 return (0);
469 } /* }}} int wh_write_json */
471 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
472 user_data_t *user_data)
473 {
474 wh_callback_t *cb;
475 int status;
477 if (user_data == NULL)
478 return (-EINVAL);
480 cb = user_data->data;
482 if (cb->format == WH_FORMAT_JSON)
483 status = wh_write_json (ds, vl, cb);
484 else
485 status = wh_write_command (ds, vl, cb);
487 return (status);
488 } /* }}} int wh_write */
490 static int config_set_format (wh_callback_t *cb, /* {{{ */
491 oconfig_item_t *ci)
492 {
493 char *string;
495 if ((ci->values_num != 1)
496 || (ci->values[0].type != OCONFIG_TYPE_STRING))
497 {
498 WARNING ("write_http plugin: The `%s' config option "
499 "needs exactly one string argument.", ci->key);
500 return (-1);
501 }
503 string = ci->values[0].value.string;
504 if (strcasecmp ("Command", string) == 0)
505 cb->format = WH_FORMAT_COMMAND;
506 else if (strcasecmp ("JSON", string) == 0)
507 cb->format = WH_FORMAT_JSON;
508 else
509 {
510 ERROR ("write_http plugin: Invalid format string: %s",
511 string);
512 return (-1);
513 }
515 return (0);
516 } /* }}} int config_set_format */
518 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
519 {
520 wh_callback_t *cb;
521 int buffer_size = 0;
522 user_data_t user_data;
523 char callback_name[DATA_MAX_NAME_LEN];
524 int i;
526 cb = malloc (sizeof (*cb));
527 if (cb == NULL)
528 {
529 ERROR ("write_http plugin: malloc failed.");
530 return (-1);
531 }
532 memset (cb, 0, sizeof (*cb));
533 cb->verify_peer = 1;
534 cb->verify_host = 1;
535 cb->format = WH_FORMAT_COMMAND;
536 cb->sslversion = CURL_SSLVERSION_DEFAULT;
537 cb->low_speed_limit = 0;
538 cb->timeout = 0;
540 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
542 cf_util_get_string (ci, &cb->name);
544 /* FIXME: Remove this legacy mode in version 6. */
545 if (strcasecmp ("URL", ci->key) == 0)
546 cf_util_get_string (ci, &cb->location);
548 for (i = 0; i < ci->children_num; i++)
549 {
550 oconfig_item_t *child = ci->children + i;
552 if (strcasecmp ("URL", child->key) == 0)
553 cf_util_get_string (child, &cb->location);
554 else if (strcasecmp ("User", child->key) == 0)
555 cf_util_get_string (child, &cb->user);
556 else if (strcasecmp ("Password", child->key) == 0)
557 cf_util_get_string (child, &cb->pass);
558 else if (strcasecmp ("VerifyPeer", child->key) == 0)
559 cf_util_get_boolean (child, &cb->verify_peer);
560 else if (strcasecmp ("VerifyHost", child->key) == 0)
561 cf_util_get_boolean (child, &cb->verify_host);
562 else if (strcasecmp ("CACert", child->key) == 0)
563 cf_util_get_string (child, &cb->cacert);
564 else if (strcasecmp ("CAPath", child->key) == 0)
565 cf_util_get_string (child, &cb->capath);
566 else if (strcasecmp ("ClientKey", child->key) == 0)
567 cf_util_get_string (child, &cb->clientkey);
568 else if (strcasecmp ("ClientCert", child->key) == 0)
569 cf_util_get_string (child, &cb->clientcert);
570 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
571 cf_util_get_string (child, &cb->clientkeypass);
572 else if (strcasecmp ("SSLVersion", child->key) == 0)
573 {
574 char *value = NULL;
576 cf_util_get_string (child, &value);
578 if (value == NULL || strcasecmp ("default", value) == 0)
579 cb->sslversion = CURL_SSLVERSION_DEFAULT;
580 else if (strcasecmp ("SSLv2", value) == 0)
581 cb->sslversion = CURL_SSLVERSION_SSLv2;
582 else if (strcasecmp ("SSLv3", value) == 0)
583 cb->sslversion = CURL_SSLVERSION_SSLv3;
584 else if (strcasecmp ("TLSv1", value) == 0)
585 cb->sslversion = CURL_SSLVERSION_TLSv1;
586 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
587 else if (strcasecmp ("TLSv1_0", value) == 0)
588 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
589 else if (strcasecmp ("TLSv1_1", value) == 0)
590 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
591 else if (strcasecmp ("TLSv1_2", value) == 0)
592 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
593 #endif
594 else
595 ERROR ("write_http plugin: Invalid SSLVersion "
596 "option: %s.", value);
598 sfree(value);
599 }
600 else if (strcasecmp ("Format", child->key) == 0)
601 config_set_format (cb, child);
602 else if (strcasecmp ("StoreRates", child->key) == 0)
603 cf_util_get_boolean (child, &cb->store_rates);
604 else if (strcasecmp ("BufferSize", child->key) == 0)
605 cf_util_get_int (child, &buffer_size);
606 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
607 cf_util_get_int (child, &cb->low_speed_limit);
608 else if (strcasecmp ("Timeout", child->key) == 0)
609 cf_util_get_int (child, &cb->timeout);
610 else
611 {
612 ERROR ("write_http plugin: Invalid configuration "
613 "option: %s.", child->key);
614 }
615 }
617 if (cb->location == NULL)
618 {
619 ERROR ("write_http plugin: no URL defined for instance '%s'",
620 cb->name);
621 wh_callback_free (cb);
622 return (-1);
623 }
625 if (cb->low_speed_limit > 0)
626 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
628 /* Determine send_buffer_size. */
629 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
630 if (buffer_size >= 1024)
631 cb->send_buffer_size = (size_t) buffer_size;
632 else if (buffer_size != 0)
633 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
634 buffer_size);
636 /* Allocate the buffer. */
637 cb->send_buffer = malloc (cb->send_buffer_size);
638 if (cb->send_buffer == NULL)
639 {
640 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
641 wh_callback_free (cb);
642 return (-1);
643 }
644 /* Nulls the buffer and sets ..._free and ..._fill. */
645 wh_reset_buffer (cb);
647 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
648 cb->name);
649 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
650 callback_name, cb->location);
652 memset (&user_data, 0, sizeof (user_data));
653 user_data.data = cb;
654 user_data.free_func = NULL;
655 plugin_register_flush (callback_name, wh_flush, &user_data);
657 user_data.free_func = wh_callback_free;
658 plugin_register_write (callback_name, wh_write, &user_data);
660 return (0);
661 } /* }}} int wh_config_node */
663 static int wh_config (oconfig_item_t *ci) /* {{{ */
664 {
665 int i;
667 for (i = 0; i < ci->children_num; i++)
668 {
669 oconfig_item_t *child = ci->children + i;
671 if (strcasecmp ("Node", child->key) == 0)
672 wh_config_node (child);
673 /* FIXME: Remove this legacy mode in version 6. */
674 else if (strcasecmp ("URL", child->key) == 0) {
675 WARNING ("write_http plugin: Legacy <URL> block found. "
676 "Please use <Node> instead.");
677 wh_config_node (child);
678 }
679 else
680 {
681 ERROR ("write_http plugin: Invalid configuration "
682 "option: %s.", child->key);
683 }
684 }
686 return (0);
687 } /* }}} int wh_config */
689 static int wh_init (void) /* {{{ */
690 {
691 /* Call this while collectd is still single-threaded to avoid
692 * initialization issues in libgcrypt. */
693 curl_global_init (CURL_GLOBAL_SSL);
694 return (0);
695 } /* }}} int wh_init */
697 void module_register (void) /* {{{ */
698 {
699 plugin_register_complex_config ("write_http", wh_config);
700 plugin_register_init ("write_http", wh_init);
701 } /* }}} void module_register */
703 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */