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 #define WH_DEFAULT_LOW_LIMIT_BYTES_PER_SEC 100
43 /*
44 * Private variables
45 */
46 struct wh_callback_s
47 {
48 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 _Bool abort_on_slow;
63 int low_limit_bytes;
64 time_t interval;
65 int post_timeout;
67 #define WH_FORMAT_COMMAND 0
68 #define WH_FORMAT_JSON 1
69 int format;
71 CURL *curl;
72 char curl_errbuf[CURL_ERROR_SIZE];
74 char *send_buffer;
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;
81 };
82 typedef struct wh_callback_s wh_callback_t;
84 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
85 {
86 memset (cb->send_buffer, 0, cb->send_buffer_size);
87 cb->send_buffer_free = cb->send_buffer_size;
88 cb->send_buffer_fill = 0;
89 cb->send_buffer_init_time = cdtime ();
91 if (cb->format == WH_FORMAT_JSON)
92 {
93 format_json_initialize (cb->send_buffer,
94 &cb->send_buffer_fill,
95 &cb->send_buffer_free);
96 }
97 } /* }}} wh_reset_buffer */
99 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
100 {
101 int status = 0;
103 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
104 status = curl_easy_perform (cb->curl);
105 if (status != CURLE_OK)
106 {
107 ERROR ("write_http plugin: curl_easy_perform failed with "
108 "status %i: %s",
109 status, cb->curl_errbuf);
110 }
111 return (status);
112 } /* }}} wh_send_buffer */
114 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
115 {
116 struct curl_slist *headers;
118 if (cb->curl != NULL)
119 return (0);
121 cb->curl = curl_easy_init ();
122 if (cb->curl == NULL)
123 {
124 ERROR ("curl plugin: curl_easy_init failed.");
125 return (-1);
126 }
128 if(cb->abort_on_slow && cb->interval > 0)
129 {
130 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_LIMIT, (cb->low_limit_bytes?cb->low_limit_bytes:WH_DEFAULT_LOW_LIMIT_BYTES_PER_SEC));
131 curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_TIME, cb->interval);
132 }
133 if(cb->post_timeout >0)
134 {
135 curl_easy_setopt(cb->curl, CURLOPT_TIMEOUT, cb->post_timeout);
136 }
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);
153 if (cb->user != NULL)
154 {
155 size_t credentials_size;
157 credentials_size = strlen (cb->user) + 2;
158 if (cb->pass != NULL)
159 credentials_size += strlen (cb->pass);
161 cb->credentials = (char *) malloc (credentials_size);
162 if (cb->credentials == NULL)
163 {
164 ERROR ("curl plugin: malloc failed.");
165 return (-1);
166 }
168 ssnprintf (cb->credentials, credentials_size, "%s:%s",
169 cb->user, (cb->pass == NULL) ? "" : cb->pass);
170 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
171 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
172 }
174 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
175 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
176 cb->verify_host ? 2L : 0L);
177 curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
178 if (cb->cacert != NULL)
179 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
180 if (cb->capath != NULL)
181 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
183 if (cb->clientkey != NULL && cb->clientcert != NULL)
184 {
185 curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
186 curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
188 if (cb->clientkeypass != NULL)
189 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
190 }
192 wh_reset_buffer (cb);
194 return (0);
195 } /* }}} int wh_callback_init */
197 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
198 {
199 int status;
201 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
202 "send_buffer_fill = %zu;",
203 CDTIME_T_TO_DOUBLE (timeout),
204 cb->send_buffer_fill);
206 /* timeout == 0 => flush unconditionally */
207 if (timeout > 0)
208 {
209 cdtime_t now;
211 now = cdtime ();
212 if ((cb->send_buffer_init_time + timeout) > now)
213 return (0);
214 }
216 if (cb->format == WH_FORMAT_COMMAND)
217 {
218 if (cb->send_buffer_fill <= 0)
219 {
220 cb->send_buffer_init_time = cdtime ();
221 return (0);
222 }
224 status = wh_send_buffer (cb);
225 wh_reset_buffer (cb);
226 }
227 else if (cb->format == WH_FORMAT_JSON)
228 {
229 if (cb->send_buffer_fill <= 2)
230 {
231 cb->send_buffer_init_time = cdtime ();
232 return (0);
233 }
235 status = format_json_finalize (cb->send_buffer,
236 &cb->send_buffer_fill,
237 &cb->send_buffer_free);
238 if (status != 0)
239 {
240 ERROR ("write_http: wh_flush_nolock: "
241 "format_json_finalize failed.");
242 wh_reset_buffer (cb);
243 return (status);
244 }
246 status = wh_send_buffer (cb);
247 wh_reset_buffer (cb);
248 }
249 else
250 {
251 ERROR ("write_http: wh_flush_nolock: "
252 "Unknown format: %i",
253 cb->format);
254 return (-1);
255 }
257 return (status);
258 } /* }}} wh_flush_nolock */
260 static int wh_flush (cdtime_t timeout, /* {{{ */
261 const char *identifier __attribute__((unused)),
262 user_data_t *user_data)
263 {
264 wh_callback_t *cb;
265 int status;
267 if (user_data == NULL)
268 return (-EINVAL);
270 cb = user_data->data;
272 pthread_mutex_lock (&cb->send_lock);
274 if (cb->curl == NULL)
275 {
276 status = wh_callback_init (cb);
277 if (status != 0)
278 {
279 ERROR ("write_http plugin: wh_callback_init failed.");
280 pthread_mutex_unlock (&cb->send_lock);
281 return (-1);
282 }
283 }
285 status = wh_flush_nolock (timeout, cb);
286 pthread_mutex_unlock (&cb->send_lock);
288 return (status);
289 } /* }}} int wh_flush */
291 static void wh_callback_free (void *data) /* {{{ */
292 {
293 wh_callback_t *cb;
295 if (data == NULL)
296 return;
298 cb = data;
300 wh_flush_nolock (/* timeout = */ 0, cb);
302 if (cb->curl != NULL)
303 {
304 curl_easy_cleanup (cb->curl);
305 cb->curl = NULL;
306 }
307 sfree (cb->location);
308 sfree (cb->user);
309 sfree (cb->pass);
310 sfree (cb->credentials);
311 sfree (cb->cacert);
312 sfree (cb->capath);
313 sfree (cb->clientkey);
314 sfree (cb->clientcert);
315 sfree (cb->clientkeypass);
316 sfree (cb->send_buffer);
318 sfree (cb);
319 } /* }}} void wh_callback_free */
321 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
322 wh_callback_t *cb)
323 {
324 char key[10*DATA_MAX_NAME_LEN];
325 char values[512];
326 char command[1024];
327 size_t command_len;
329 int status;
331 if (0 != strcmp (ds->type, vl->type)) {
332 ERROR ("write_http plugin: DS type does not match "
333 "value list type");
334 return -1;
335 }
337 /* Copy the identifier to `key' and escape it. */
338 status = FORMAT_VL (key, sizeof (key), vl);
339 if (status != 0) {
340 ERROR ("write_http plugin: error with format_name");
341 return (status);
342 }
343 escape_string (key, sizeof (key));
345 /* Convert the values to an ASCII representation and put that into
346 * `values'. */
347 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
348 if (status != 0) {
349 ERROR ("write_http plugin: error with "
350 "wh_value_list_to_string");
351 return (status);
352 }
354 command_len = (size_t) ssnprintf (command, sizeof (command),
355 "PUTVAL %s interval=%.3f %s\r\n",
356 key,
357 CDTIME_T_TO_DOUBLE (vl->interval),
358 values);
359 if (command_len >= sizeof (command)) {
360 ERROR ("write_http plugin: Command buffer too small: "
361 "Need %zu bytes.", command_len + 1);
362 return (-1);
363 }
365 pthread_mutex_lock (&cb->send_lock);
367 if (cb->curl == NULL)
368 {
369 cb->interval = CDTIME_T_TO_TIME_T(vl->interval);
370 status = wh_callback_init (cb);
371 if (status != 0)
372 {
373 ERROR ("write_http plugin: wh_callback_init failed.");
374 pthread_mutex_unlock (&cb->send_lock);
375 return (-1);
376 }
377 }
379 if (command_len >= cb->send_buffer_free)
380 {
381 status = wh_flush_nolock (/* timeout = */ 0, cb);
382 if (status != 0)
383 {
384 pthread_mutex_unlock (&cb->send_lock);
385 return (status);
386 }
387 }
388 assert (command_len < cb->send_buffer_free);
390 /* `command_len + 1' because `command_len' does not include the
391 * trailing null byte. Neither does `send_buffer_fill'. */
392 memcpy (cb->send_buffer + cb->send_buffer_fill,
393 command, command_len + 1);
394 cb->send_buffer_fill += command_len;
395 cb->send_buffer_free -= command_len;
397 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
398 cb->location,
399 cb->send_buffer_fill, cb->send_buffer_size,
400 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
401 command);
403 /* Check if we have enough space for this command. */
404 pthread_mutex_unlock (&cb->send_lock);
406 return (0);
407 } /* }}} int wh_write_command */
409 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
410 wh_callback_t *cb)
411 {
412 int status;
414 pthread_mutex_lock (&cb->send_lock);
416 if (cb->curl == NULL)
417 {
418 cb->interval = CDTIME_T_TO_TIME_T(vl->interval);
420 status = wh_callback_init (cb);
421 if (status != 0)
422 {
423 ERROR ("write_http plugin: wh_callback_init failed.");
424 pthread_mutex_unlock (&cb->send_lock);
425 return (-1);
426 }
427 }
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);
433 if (status == (-ENOMEM))
434 {
435 status = wh_flush_nolock (/* timeout = */ 0, cb);
436 if (status != 0)
437 {
438 wh_reset_buffer (cb);
439 pthread_mutex_unlock (&cb->send_lock);
440 return (status);
441 }
443 status = format_json_value_list (cb->send_buffer,
444 &cb->send_buffer_fill,
445 &cb->send_buffer_free,
446 ds, vl, cb->store_rates);
447 }
448 if (status != 0)
449 {
450 pthread_mutex_unlock (&cb->send_lock);
451 return (status);
452 }
454 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
455 cb->location,
456 cb->send_buffer_fill, cb->send_buffer_size,
457 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
459 /* Check if we have enough space for this command. */
460 pthread_mutex_unlock (&cb->send_lock);
462 return (0);
463 } /* }}} int wh_write_json */
465 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
466 user_data_t *user_data)
467 {
468 wh_callback_t *cb;
469 int status;
471 if (user_data == NULL)
472 return (-EINVAL);
474 cb = user_data->data;
476 if (cb->format == WH_FORMAT_JSON)
477 status = wh_write_json (ds, vl, cb);
478 else
479 status = wh_write_command (ds, vl, cb);
481 return (status);
482 } /* }}} int wh_write */
484 static int config_set_format (wh_callback_t *cb, /* {{{ */
485 oconfig_item_t *ci)
486 {
487 char *string;
489 if ((ci->values_num != 1)
490 || (ci->values[0].type != OCONFIG_TYPE_STRING))
491 {
492 WARNING ("write_http plugin: The `%s' config option "
493 "needs exactly one string argument.", ci->key);
494 return (-1);
495 }
497 string = ci->values[0].value.string;
498 if (strcasecmp ("Command", string) == 0)
499 cb->format = WH_FORMAT_COMMAND;
500 else if (strcasecmp ("JSON", string) == 0)
501 cb->format = WH_FORMAT_JSON;
502 else
503 {
504 ERROR ("write_http plugin: Invalid format string: %s",
505 string);
506 return (-1);
507 }
509 return (0);
510 } /* }}} int config_set_format */
512 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
513 {
514 wh_callback_t *cb;
515 int buffer_size = 0;
516 user_data_t user_data;
517 int i;
519 cb = malloc (sizeof (*cb));
520 if (cb == NULL)
521 {
522 ERROR ("write_http plugin: malloc failed.");
523 return (-1);
524 }
525 memset (cb, 0, sizeof (*cb));
526 cb->verify_peer = 1;
527 cb->verify_host = 1;
528 cb->format = WH_FORMAT_COMMAND;
529 cb->sslversion = CURL_SSLVERSION_DEFAULT;
530 cb->low_limit_bytes = WH_DEFAULT_LOW_LIMIT_BYTES_PER_SEC;
531 cb->post_timeout = 0;
533 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
535 cf_util_get_string (ci, &cb->location);
536 if (cb->location == NULL)
537 return (-1);
539 for (i = 0; i < ci->children_num; i++)
540 {
541 oconfig_item_t *child = ci->children + i;
543 if (strcasecmp ("User", child->key) == 0)
544 cf_util_get_string (child, &cb->user);
545 else if (strcasecmp ("Password", child->key) == 0)
546 cf_util_get_string (child, &cb->pass);
547 else if (strcasecmp ("VerifyPeer", child->key) == 0)
548 cf_util_get_boolean (child, &cb->verify_peer);
549 else if (strcasecmp ("VerifyHost", child->key) == 0)
550 cf_util_get_boolean (child, &cb->verify_host);
551 else if (strcasecmp ("CACert", child->key) == 0)
552 cf_util_get_string (child, &cb->cacert);
553 else if (strcasecmp ("CAPath", child->key) == 0)
554 cf_util_get_string (child, &cb->capath);
555 else if (strcasecmp ("ClientKey", child->key) == 0)
556 cf_util_get_string (child, &cb->clientkey);
557 else if (strcasecmp ("ClientCert", child->key) == 0)
558 cf_util_get_string (child, &cb->clientcert);
559 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
560 cf_util_get_string (child, &cb->clientkeypass);
561 else if (strcasecmp ("SSLVersion", child->key) == 0)
562 {
563 char *value = NULL;
565 cf_util_get_string (child, &value);
567 if (value == NULL || strcasecmp ("default", value) == 0)
568 cb->sslversion = CURL_SSLVERSION_DEFAULT;
569 else if (strcasecmp ("SSLv2", value) == 0)
570 cb->sslversion = CURL_SSLVERSION_SSLv2;
571 else if (strcasecmp ("SSLv3", value) == 0)
572 cb->sslversion = CURL_SSLVERSION_SSLv3;
573 else if (strcasecmp ("TLSv1", value) == 0)
574 cb->sslversion = CURL_SSLVERSION_TLSv1;
575 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
576 else if (strcasecmp ("TLSv1_0", value) == 0)
577 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
578 else if (strcasecmp ("TLSv1_1", value) == 0)
579 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
580 else if (strcasecmp ("TLSv1_2", value) == 0)
581 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
582 #endif
583 else
584 ERROR ("write_http plugin: Invalid SSLVersion "
585 "option: %s.", value);
587 sfree(value);
588 }
589 else if (strcasecmp ("Format", child->key) == 0)
590 config_set_format (cb, child);
591 else if (strcasecmp ("StoreRates", child->key) == 0)
592 cf_util_get_boolean (child, &cb->store_rates);
593 else if (strcasecmp ("BufferSize", child->key) == 0)
594 cf_util_get_int (child, &buffer_size);
595 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
596 cf_util_get_boolean (child,&cb->abort_on_slow);
597 else if (strcasecmp ("LowLimitBytesPerSec", child->key) == 0)
598 cf_util_get_int (child, &cb->low_limit_bytes);
599 else if (strcasecmp ("PostTimeoutSec", child->key) == 0)
600 cf_util_get_int (child, &cb->post_timeout);
601 else
602 {
603 ERROR ("write_http plugin: Invalid configuration "
604 "option: %s.", child->key);
605 }
606 }
608 if(cb->abort_on_slow)
609 {
610 cb->interval = CDTIME_T_TO_TIME_T(plugin_get_interval());
611 }
612 if(cb->post_timeout == 0)
613 {
614 //setting default timeout to plugin interval.
615 cb->post_timeout = CDTIME_T_TO_TIME_T(plugin_get_interval());
616 }
618 /* Determine send_buffer_size. */
619 cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
620 if (buffer_size >= 1024)
621 cb->send_buffer_size = (size_t) buffer_size;
622 else if (buffer_size != 0)
623 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
624 buffer_size);
626 /* Allocate the buffer. */
627 cb->send_buffer = malloc (cb->send_buffer_size);
628 if (cb->send_buffer == NULL)
629 {
630 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
631 wh_callback_free (cb);
632 return (-1);
633 }
634 /* Nulls the buffer and sets ..._free and ..._fill. */
635 wh_reset_buffer (cb);
637 DEBUG ("write_http: Registering write callback with URL %s",
638 cb->location);
640 memset (&user_data, 0, sizeof (user_data));
641 user_data.data = cb;
642 user_data.free_func = NULL;
643 plugin_register_flush ("write_http", wh_flush, &user_data);
645 user_data.free_func = wh_callback_free;
646 plugin_register_write ("write_http", wh_write, &user_data);
648 return (0);
649 } /* }}} int wh_config_url */
651 static int wh_config (oconfig_item_t *ci) /* {{{ */
652 {
653 int i;
655 for (i = 0; i < ci->children_num; i++)
656 {
657 oconfig_item_t *child = ci->children + i;
659 if (strcasecmp ("URL", child->key) == 0)
660 wh_config_url (child);
661 else
662 {
663 ERROR ("write_http plugin: Invalid configuration "
664 "option: %s.", child->key);
665 }
666 }
668 return (0);
669 } /* }}} int wh_config */
671 static int wh_init (void) /* {{{ */
672 {
673 /* Call this while collectd is still single-threaded to avoid
674 * initialization issues in libgcrypt. */
675 curl_global_init (CURL_GLOBAL_SSL);
676 return (0);
677 } /* }}} int wh_init */
679 void module_register (void) /* {{{ */
680 {
681 plugin_register_complex_config ("write_http", wh_config);
682 plugin_register_init ("write_http", wh_init);
683 } /* }}} void module_register */
685 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */