2 * collectd - src/write_http.c
3 * Copyright (C) 2009 Paul Sadauskas
4 * Copyright (C) 2009 Doug MacEachern
5 * Copyright (C) 2007-2009 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 verplant.org>
22 * Doug MacEachern <dougm@hyperic.com>
23 * Paul Sadauskas <psadauskas@gmail.com>
29 #include "utils_cache.h"
30 #include "utils_parse_option.h"
31 #include "utils_format_json.h"
37 #include <curl/curl.h>
54 #define WH_FORMAT_COMMAND 0
55 #define WH_FORMAT_JSON 1
59 struct curl_slist *headers;
60 char curl_errbuf[CURL_ERROR_SIZE];
62 char send_buffer[4096];
63 size_t send_buffer_free;
64 size_t send_buffer_fill;
65 cdtime_t send_buffer_init_time;
67 pthread_mutex_t send_lock;
69 typedef struct wh_callback_s wh_callback_t;
71 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
73 memset (cb->send_buffer, 0, sizeof (cb->send_buffer));
74 cb->send_buffer_free = sizeof (cb->send_buffer);
75 cb->send_buffer_fill = 0;
76 cb->send_buffer_init_time = cdtime ();
78 if (cb->format == WH_FORMAT_JSON)
80 format_json_initialize (cb->send_buffer,
81 &cb->send_buffer_fill,
82 &cb->send_buffer_free);
84 } /* }}} wh_reset_buffer */
86 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
90 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
91 status = curl_easy_perform (cb->curl);
92 if (status != CURLE_OK)
94 ERROR ("write_http plugin: curl_easy_perform failed with "
96 status, cb->curl_errbuf);
99 } /* }}} wh_send_buffer */
101 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
103 if (cb->curl != NULL)
106 cb->curl = curl_easy_init ();
107 if (cb->curl == NULL)
109 ERROR ("curl plugin: curl_easy_init failed.");
113 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
114 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
117 cb->headers = curl_slist_append (cb->headers, "Accept: */*");
118 if (cb->format == WH_FORMAT_JSON)
119 cb->headers = curl_slist_append (cb->headers, "Content-Type: application/json");
121 cb->headers = curl_slist_append (cb->headers, "Content-Type: text/plain");
122 cb->headers = curl_slist_append (cb->headers, "Expect:");
123 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, cb->headers);
125 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
126 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
128 if (cb->user != NULL)
130 size_t credentials_size;
132 credentials_size = strlen (cb->user) + 2;
133 if (cb->pass != NULL)
134 credentials_size += strlen (cb->pass);
136 cb->credentials = (char *) malloc (credentials_size);
137 if (cb->credentials == NULL)
139 ERROR ("curl plugin: malloc failed.");
143 ssnprintf (cb->credentials, credentials_size, "%s:%s",
144 cb->user, (cb->pass == NULL) ? "" : cb->pass);
145 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
146 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
149 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
150 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
151 cb->verify_host ? 2L : 0L);
152 if (cb->cacert != NULL)
153 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
155 wh_reset_buffer (cb);
158 } /* }}} int wh_callback_init */
160 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
164 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
165 "send_buffer_fill = %zu;",
166 CDTIME_T_TO_DOUBLE (timeout),
167 cb->send_buffer_fill);
169 /* timeout == 0 => flush unconditionally */
175 if ((cb->send_buffer_init_time + timeout) > now)
179 if (cb->format == WH_FORMAT_COMMAND)
181 if (cb->send_buffer_fill <= 0)
183 cb->send_buffer_init_time = cdtime ();
187 status = wh_send_buffer (cb);
188 wh_reset_buffer (cb);
190 else if (cb->format == WH_FORMAT_JSON)
192 if (cb->send_buffer_fill <= 2)
194 cb->send_buffer_init_time = cdtime ();
198 status = format_json_finalize (cb->send_buffer,
199 &cb->send_buffer_fill,
200 &cb->send_buffer_free);
203 ERROR ("write_http: wh_flush_nolock: "
204 "format_json_finalize failed.");
205 wh_reset_buffer (cb);
209 status = wh_send_buffer (cb);
210 wh_reset_buffer (cb);
214 ERROR ("write_http: wh_flush_nolock: "
215 "Unknown format: %i",
221 } /* }}} wh_flush_nolock */
223 static int wh_flush (cdtime_t timeout, /* {{{ */
224 const char *identifier __attribute__((unused)),
225 user_data_t *user_data)
230 if (user_data == NULL)
233 cb = user_data->data;
235 pthread_mutex_lock (&cb->send_lock);
237 if (cb->curl == NULL)
239 status = wh_callback_init (cb);
242 ERROR ("write_http plugin: wh_callback_init failed.");
243 pthread_mutex_unlock (&cb->send_lock);
248 status = wh_flush_nolock (timeout, cb);
249 pthread_mutex_unlock (&cb->send_lock);
252 } /* }}} int wh_flush */
254 static void wh_callback_free (void *data) /* {{{ */
263 wh_flush_nolock (/* timeout = */ 0, cb);
265 curl_easy_cleanup (cb->curl);
267 if (cb->headers != NULL)
269 curl_slist_free_all (cb->headers);
273 sfree (cb->location);
276 sfree (cb->credentials);
280 } /* }}} void wh_callback_free */
282 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
285 char key[10*DATA_MAX_NAME_LEN];
292 if (0 != strcmp (ds->type, vl->type)) {
293 ERROR ("write_http plugin: DS type does not match "
298 /* Copy the identifier to `key' and escape it. */
299 status = FORMAT_VL (key, sizeof (key), vl);
301 ERROR ("write_http plugin: error with format_name");
304 escape_string (key, sizeof (key));
306 /* Convert the values to an ASCII representation and put that into
308 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
310 ERROR ("write_http plugin: error with "
311 "wh_value_list_to_string");
315 command_len = (size_t) ssnprintf (command, sizeof (command),
316 "PUTVAL %s interval=%.3f %s\r\n",
318 CDTIME_T_TO_DOUBLE (vl->interval),
320 if (command_len >= sizeof (command)) {
321 ERROR ("write_http plugin: Command buffer too small: "
322 "Need %zu bytes.", command_len + 1);
326 pthread_mutex_lock (&cb->send_lock);
328 if (cb->curl == NULL)
330 status = wh_callback_init (cb);
333 ERROR ("write_http plugin: wh_callback_init failed.");
334 pthread_mutex_unlock (&cb->send_lock);
339 if (command_len >= cb->send_buffer_free)
341 status = wh_flush_nolock (/* timeout = */ 0, cb);
344 pthread_mutex_unlock (&cb->send_lock);
348 assert (command_len < cb->send_buffer_free);
350 /* `command_len + 1' because `command_len' does not include the
351 * trailing null byte. Neither does `send_buffer_fill'. */
352 memcpy (cb->send_buffer + cb->send_buffer_fill,
353 command, command_len + 1);
354 cb->send_buffer_fill += command_len;
355 cb->send_buffer_free -= command_len;
357 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
359 cb->send_buffer_fill, sizeof (cb->send_buffer),
360 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
363 /* Check if we have enough space for this command. */
364 pthread_mutex_unlock (&cb->send_lock);
367 } /* }}} int wh_write_command */
369 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
374 pthread_mutex_lock (&cb->send_lock);
376 if (cb->curl == NULL)
378 status = wh_callback_init (cb);
381 ERROR ("write_http plugin: wh_callback_init failed.");
382 pthread_mutex_unlock (&cb->send_lock);
387 status = format_json_value_list (cb->send_buffer,
388 &cb->send_buffer_fill,
389 &cb->send_buffer_free,
390 ds, vl, cb->store_rates);
391 if (status == (-ENOMEM))
393 status = wh_flush_nolock (/* timeout = */ 0, cb);
396 wh_reset_buffer (cb);
397 pthread_mutex_unlock (&cb->send_lock);
401 status = format_json_value_list (cb->send_buffer,
402 &cb->send_buffer_fill,
403 &cb->send_buffer_free,
404 ds, vl, cb->store_rates);
408 pthread_mutex_unlock (&cb->send_lock);
412 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
414 cb->send_buffer_fill, sizeof (cb->send_buffer),
415 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
417 /* Check if we have enough space for this command. */
418 pthread_mutex_unlock (&cb->send_lock);
421 } /* }}} int wh_write_json */
423 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
424 user_data_t *user_data)
429 if (user_data == NULL)
432 cb = user_data->data;
434 if (cb->format == WH_FORMAT_JSON)
435 status = wh_write_json (ds, vl, cb);
437 status = wh_write_command (ds, vl, cb);
440 } /* }}} int wh_write */
442 static int config_set_string (char **ret_string, /* {{{ */
447 if ((ci->values_num != 1)
448 || (ci->values[0].type != OCONFIG_TYPE_STRING))
450 WARNING ("write_http plugin: The `%s' config option "
451 "needs exactly one string argument.", ci->key);
455 string = strdup (ci->values[0].value.string);
458 ERROR ("write_http plugin: strdup failed.");
462 if (*ret_string != NULL)
464 *ret_string = string;
467 } /* }}} int config_set_string */
469 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
471 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
473 WARNING ("write_http plugin: The `%s' config option "
474 "needs exactly one boolean argument.", ci->key);
478 *dest = ci->values[0].value.boolean ? 1 : 0;
481 } /* }}} int config_set_boolean */
483 static int config_set_format (wh_callback_t *cb, /* {{{ */
488 if ((ci->values_num != 1)
489 || (ci->values[0].type != OCONFIG_TYPE_STRING))
491 WARNING ("write_http plugin: The `%s' config option "
492 "needs exactly one string argument.", ci->key);
496 string = ci->values[0].value.string;
497 if (strcasecmp ("Command", string) == 0)
498 cb->format = WH_FORMAT_COMMAND;
499 else if (strcasecmp ("JSON", string) == 0)
500 cb->format = WH_FORMAT_JSON;
503 ERROR ("write_http plugin: Invalid format string: %s",
509 } /* }}} int config_set_string */
511 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
514 user_data_t user_data;
515 char callback_name[DATA_MAX_NAME_LEN];
518 cb = malloc (sizeof (*cb));
521 ERROR ("write_http plugin: malloc failed.");
524 memset (cb, 0, sizeof (*cb));
528 cb->credentials = NULL;
532 cb->format = WH_FORMAT_COMMAND;
536 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
538 config_set_string (&cb->location, ci);
539 if (cb->location == NULL)
542 for (i = 0; i < ci->children_num; i++)
544 oconfig_item_t *child = ci->children + i;
546 if (strcasecmp ("User", child->key) == 0)
547 config_set_string (&cb->user, child);
548 else if (strcasecmp ("Password", child->key) == 0)
549 config_set_string (&cb->pass, child);
550 else if (strcasecmp ("VerifyPeer", child->key) == 0)
551 config_set_boolean (&cb->verify_peer, child);
552 else if (strcasecmp ("VerifyHost", child->key) == 0)
553 config_set_boolean (&cb->verify_host, child);
554 else if (strcasecmp ("CACert", child->key) == 0)
555 config_set_string (&cb->cacert, child);
556 else if (strcasecmp ("Format", child->key) == 0)
557 config_set_format (cb, child);
558 else if (strcasecmp ("StoreRates", child->key) == 0)
559 config_set_boolean (&cb->store_rates, child);
562 ERROR ("write_http plugin: Invalid configuration "
563 "option: %s.", child->key);
567 ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
569 DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
570 callback_name, cb->location);
572 memset (&user_data, 0, sizeof (user_data));
574 user_data.free_func = NULL;
575 plugin_register_flush (callback_name, wh_flush, &user_data);
577 user_data.free_func = wh_callback_free;
578 plugin_register_write (callback_name, wh_write, &user_data);
581 } /* }}} int wh_config_url */
583 static int wh_config (oconfig_item_t *ci) /* {{{ */
587 for (i = 0; i < ci->children_num; i++)
589 oconfig_item_t *child = ci->children + i;
591 if (strcasecmp ("URL", child->key) == 0)
592 wh_config_url (child);
595 ERROR ("write_http plugin: Invalid configuration "
596 "option: %s.", child->key);
601 } /* }}} int wh_config */
603 static int wh_init (void) /* {{{ */
605 /* Call this while collectd is still single-threaded to avoid
606 * initialization issues in libgcrypt. */
607 curl_global_init (CURL_GLOBAL_SSL);
609 } /* }}} int wh_init */
611 void module_register (void) /* {{{ */
613 plugin_register_complex_config ("write_http", wh_config);
614 plugin_register_init ("write_http", wh_init);
615 } /* }}} void module_register */
617 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */