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 #ifdef HAVE_CURLOPT_TIMEOUT_MS
136 if (cb->timeout > 0)
137 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout);
138 #endif
140 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
141 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
143 headers = NULL;
144 headers = curl_slist_append (headers, "Accept: */*");
145 if (cb->format == WH_FORMAT_JSON)
146 headers = curl_slist_append (headers, "Content-Type: application/json");
147 else
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)
158 {
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);
163 #else
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)
172 {
173 ERROR ("curl plugin: malloc failed.");
174 return (-1);
175 }
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);
180 #endif
181 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
182 }
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)
194 {
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);
200 }
202 wh_reset_buffer (cb);
204 return (0);
205 } /* }}} int wh_callback_init */
207 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
208 {
209 int status;
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 */
217 if (timeout > 0)
218 {
219 cdtime_t now;
221 now = cdtime ();
222 if ((cb->send_buffer_init_time + timeout) > now)
223 return (0);
224 }
226 if (cb->format == WH_FORMAT_COMMAND)
227 {
228 if (cb->send_buffer_fill <= 0)
229 {
230 cb->send_buffer_init_time = cdtime ();
231 return (0);
232 }
234 status = wh_send_buffer (cb);
235 wh_reset_buffer (cb);
236 }
237 else if (cb->format == WH_FORMAT_JSON)
238 {
239 if (cb->send_buffer_fill <= 2)
240 {
241 cb->send_buffer_init_time = cdtime ();
242 return (0);
243 }
245 status = format_json_finalize (cb->send_buffer,
246 &cb->send_buffer_fill,
247 &cb->send_buffer_free);
248 if (status != 0)
249 {
250 ERROR ("write_http: wh_flush_nolock: "
251 "format_json_finalize failed.");
252 wh_reset_buffer (cb);
253 return (status);
254 }
256 status = wh_send_buffer (cb);
257 wh_reset_buffer (cb);
258 }
259 else
260 {
261 ERROR ("write_http: wh_flush_nolock: "
262 "Unknown format: %i",
263 cb->format);
264 return (-1);
265 }
267 return (status);
268 } /* }}} wh_flush_nolock */
270 static int wh_flush (cdtime_t timeout, /* {{{ */
271 const char *identifier __attribute__((unused)),
272 user_data_t *user_data)
273 {
274 wh_callback_t *cb;
275 int status;
277 if (user_data == NULL)
278 return (-EINVAL);
280 cb = user_data->data;
282 pthread_mutex_lock (&cb->send_lock);
284 if (cb->curl == NULL)
285 {
286 status = wh_callback_init (cb);
287 if (status != 0)
288 {
289 ERROR ("write_http plugin: wh_callback_init failed.");
290 pthread_mutex_unlock (&cb->send_lock);
291 return (-1);
292 }
293 }
295 status = wh_flush_nolock (timeout, cb);
296 pthread_mutex_unlock (&cb->send_lock);
298 return (status);
299 } /* }}} int wh_flush */
301 static void wh_callback_free (void *data) /* {{{ */
302 {
303 wh_callback_t *cb;
305 if (data == NULL)
306 return;
308 cb = data;
310 wh_flush_nolock (/* timeout = */ 0, cb);
312 if (cb->curl != NULL)
313 {
314 curl_easy_cleanup (cb->curl);
315 cb->curl = NULL;
316 }
317 sfree (cb->name);
318 sfree (cb->location);
319 sfree (cb->user);
320 sfree (cb->pass);
321 sfree (cb->credentials);
322 sfree (cb->cacert);
323 sfree (cb->capath);
324 sfree (cb->clientkey);
325 sfree (cb->clientcert);
326 sfree (cb->clientkeypass);
327 sfree (cb->send_buffer);
329 sfree (cb);
330 } /* }}} void wh_callback_free */
332 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
333 wh_callback_t *cb)
334 {
335 char key[10*DATA_MAX_NAME_LEN];
336 char values[512];
337 char command[1024];
338 size_t command_len;
340 int status;
342 if (0 != strcmp (ds->type, vl->type)) {
343 ERROR ("write_http plugin: DS type does not match "
344 "value list type");
345 return -1;
346 }
348 /* Copy the identifier to `key' and escape it. */
349 status = FORMAT_VL (key, sizeof (key), vl);
350 if (status != 0) {
351 ERROR ("write_http plugin: error with format_name");
352 return (status);
353 }
354 escape_string (key, sizeof (key));
356 /* Convert the values to an ASCII representation and put that into
357 * `values'. */
358 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
359 if (status != 0) {
360 ERROR ("write_http plugin: error with "
361 "wh_value_list_to_string");
362 return (status);
363 }
365 command_len = (size_t) ssnprintf (command, sizeof (command),
366 "PUTVAL %s interval=%.3f %s\r\n",
367 key,
368 CDTIME_T_TO_DOUBLE (vl->interval),
369 values);
370 if (command_len >= sizeof (command)) {
371 ERROR ("write_http plugin: Command buffer too small: "
372 "Need %zu bytes.", command_len + 1);
373 return (-1);
374 }
376 pthread_mutex_lock (&cb->send_lock);
378 if (cb->curl == NULL)
379 {
380 status = wh_callback_init (cb);
381 if (status != 0)
382 {
383 ERROR ("write_http plugin: wh_callback_init failed.");
384 pthread_mutex_unlock (&cb->send_lock);
385 return (-1);
386 }
387 }
389 if (command_len >= cb->send_buffer_free)
390 {
391 status = wh_flush_nolock (/* timeout = */ 0, cb);
392 if (status != 0)
393 {
394 pthread_mutex_unlock (&cb->send_lock);
395 return (status);
396 }
397 }
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\"",
408 cb->location,
409 cb->send_buffer_fill, cb->send_buffer_size,
410 100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
411 command);
413 /* Check if we have enough space for this command. */
414 pthread_mutex_unlock (&cb->send_lock);
416 return (0);
417 } /* }}} int wh_write_command */
419 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
420 wh_callback_t *cb)
421 {
422 int status;
424 pthread_mutex_lock (&cb->send_lock);
426 if (cb->curl == NULL)
427 {
428 status = wh_callback_init (cb);
429 if (status != 0)
430 {
431 ERROR ("write_http plugin: wh_callback_init failed.");
432 pthread_mutex_unlock (&cb->send_lock);
433 return (-1);
434 }
435 }
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))
442 {
443 status = wh_flush_nolock (/* timeout = */ 0, cb);
444 if (status != 0)
445 {
446 wh_reset_buffer (cb);
447 pthread_mutex_unlock (&cb->send_lock);
448 return (status);
449 }
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);
455 }
456 if (status != 0)
457 {
458 pthread_mutex_unlock (&cb->send_lock);
459 return (status);
460 }
462 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
463 cb->location,
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);
470 return (0);
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)
475 {
476 wh_callback_t *cb;
477 int status;
479 if (user_data == NULL)
480 return (-EINVAL);
482 cb = user_data->data;
484 if (cb->format == WH_FORMAT_JSON)
485 status = wh_write_json (ds, vl, cb);
486 else
487 status = wh_write_command (ds, vl, cb);
489 return (status);
490 } /* }}} int wh_write */
492 static int config_set_format (wh_callback_t *cb, /* {{{ */
493 oconfig_item_t *ci)
494 {
495 char *string;
497 if ((ci->values_num != 1)
498 || (ci->values[0].type != OCONFIG_TYPE_STRING))
499 {
500 WARNING ("write_http plugin: The `%s' config option "
501 "needs exactly one string argument.", ci->key);
502 return (-1);
503 }
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;
510 else
511 {
512 ERROR ("write_http plugin: Invalid format string: %s",
513 string);
514 return (-1);
515 }
517 return (0);
518 } /* }}} int config_set_format */
520 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
521 {
522 wh_callback_t *cb;
523 int buffer_size = 0;
524 user_data_t user_data;
525 char callback_name[DATA_MAX_NAME_LEN];
526 int i;
528 cb = malloc (sizeof (*cb));
529 if (cb == NULL)
530 {
531 ERROR ("write_http plugin: malloc failed.");
532 return (-1);
533 }
534 memset (cb, 0, sizeof (*cb));
535 cb->verify_peer = 1;
536 cb->verify_host = 1;
537 cb->format = WH_FORMAT_COMMAND;
538 cb->sslversion = CURL_SSLVERSION_DEFAULT;
539 cb->low_speed_limit = 0;
540 cb->timeout = 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++)
551 {
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)
575 {
576 char *value = NULL;
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;
595 #endif
596 else
597 ERROR ("write_http plugin: Invalid SSLVersion "
598 "option: %s.", value);
600 sfree(value);
601 }
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);
612 else
613 {
614 ERROR ("write_http plugin: Invalid configuration "
615 "option: %s.", child->key);
616 }
617 }
619 if (cb->location == NULL)
620 {
621 ERROR ("write_http plugin: no URL defined for instance '%s'",
622 cb->name);
623 wh_callback_free (cb);
624 return (-1);
625 }
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).",
636 buffer_size);
638 /* Allocate the buffer. */
639 cb->send_buffer = malloc (cb->send_buffer_size);
640 if (cb->send_buffer == NULL)
641 {
642 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
643 wh_callback_free (cb);
644 return (-1);
645 }
646 /* Nulls the buffer and sets ..._free and ..._fill. */
647 wh_reset_buffer (cb);
649 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
650 cb->name);
651 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
652 callback_name, cb->location);
654 memset (&user_data, 0, sizeof (user_data));
655 user_data.data = cb;
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);
662 return (0);
663 } /* }}} int wh_config_node */
665 static int wh_config (oconfig_item_t *ci) /* {{{ */
666 {
667 int i;
669 for (i = 0; i < ci->children_num; i++)
670 {
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);
680 }
681 else
682 {
683 ERROR ("write_http plugin: Invalid configuration "
684 "option: %s.", child->key);
685 }
686 }
688 return (0);
689 } /* }}} int wh_config */
691 static int wh_init (void) /* {{{ */
692 {
693 /* Call this while collectd is still single-threaded to avoid
694 * initialization issues in libgcrypt. */
695 curl_global_init (CURL_GLOBAL_SSL);
696 return (0);
697 } /* }}} int wh_init */
699 void module_register (void) /* {{{ */
700 {
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 : */