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 char curl_errbuf[CURL_ERROR_SIZE];
61 char send_buffer[4096];
62 size_t send_buffer_free;
63 size_t send_buffer_fill;
64 cdtime_t send_buffer_init_time;
66 pthread_mutex_t send_lock;
68 typedef struct wh_callback_s wh_callback_t;
70 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
72 memset (cb->send_buffer, 0, sizeof (cb->send_buffer));
73 cb->send_buffer_free = sizeof (cb->send_buffer);
74 cb->send_buffer_fill = 0;
75 cb->send_buffer_init_time = cdtime ();
77 if (cb->format == WH_FORMAT_JSON)
79 format_json_initialize (cb->send_buffer,
80 &cb->send_buffer_fill,
81 &cb->send_buffer_free);
83 } /* }}} wh_reset_buffer */
85 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
89 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
90 status = curl_easy_perform (cb->curl);
91 if (status != CURLE_OK)
93 ERROR ("write_http plugin: curl_easy_perform failed with "
95 status, cb->curl_errbuf);
98 } /* }}} wh_send_buffer */
100 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
102 struct curl_slist *headers;
104 if (cb->curl != NULL)
107 cb->curl = curl_easy_init ();
108 if (cb->curl == NULL)
110 ERROR ("curl plugin: curl_easy_init failed.");
114 curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
115 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
118 headers = curl_slist_append (headers, "Accept: */*");
119 if (cb->format == WH_FORMAT_JSON)
120 headers = curl_slist_append (headers, "Content-Type: application/json");
122 headers = curl_slist_append (headers, "Content-Type: text/plain");
123 headers = curl_slist_append (headers, "Expect:");
124 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
126 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
127 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
129 if (cb->user != NULL)
131 size_t credentials_size;
133 credentials_size = strlen (cb->user) + 2;
134 if (cb->pass != NULL)
135 credentials_size += strlen (cb->pass);
137 cb->credentials = (char *) malloc (credentials_size);
138 if (cb->credentials == NULL)
140 ERROR ("curl plugin: malloc failed.");
144 ssnprintf (cb->credentials, credentials_size, "%s:%s",
145 cb->user, (cb->pass == NULL) ? "" : cb->pass);
146 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
147 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
150 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
151 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
152 cb->verify_host ? 2L : 0L);
153 if (cb->cacert != NULL)
154 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
156 wh_reset_buffer (cb);
159 } /* }}} int wh_callback_init */
161 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
165 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
166 "send_buffer_fill = %zu;",
167 CDTIME_T_TO_DOUBLE (timeout),
168 cb->send_buffer_fill);
170 /* timeout == 0 => flush unconditionally */
176 if ((cb->send_buffer_init_time + timeout) > now)
180 if (cb->format == WH_FORMAT_COMMAND)
182 if (cb->send_buffer_fill <= 0)
184 cb->send_buffer_init_time = cdtime ();
188 status = wh_send_buffer (cb);
189 wh_reset_buffer (cb);
191 else if (cb->format == WH_FORMAT_JSON)
193 if (cb->send_buffer_fill <= 2)
195 cb->send_buffer_init_time = cdtime ();
199 status = format_json_finalize (cb->send_buffer,
200 &cb->send_buffer_fill,
201 &cb->send_buffer_free);
204 ERROR ("write_http: wh_flush_nolock: "
205 "format_json_finalize failed.");
206 wh_reset_buffer (cb);
210 status = wh_send_buffer (cb);
211 wh_reset_buffer (cb);
215 ERROR ("write_http: wh_flush_nolock: "
216 "Unknown format: %i",
222 } /* }}} wh_flush_nolock */
224 static int wh_flush (cdtime_t timeout, /* {{{ */
225 const char *identifier __attribute__((unused)),
226 user_data_t *user_data)
231 if (user_data == NULL)
234 cb = user_data->data;
236 pthread_mutex_lock (&cb->send_lock);
238 if (cb->curl == NULL)
240 status = wh_callback_init (cb);
243 ERROR ("write_http plugin: wh_callback_init failed.");
244 pthread_mutex_unlock (&cb->send_lock);
249 status = wh_flush_nolock (timeout, cb);
250 pthread_mutex_unlock (&cb->send_lock);
253 } /* }}} int wh_flush */
255 static void wh_callback_free (void *data) /* {{{ */
264 wh_flush_nolock (/* timeout = */ 0, cb);
266 curl_easy_cleanup (cb->curl);
267 sfree (cb->location);
270 sfree (cb->credentials);
274 } /* }}} void wh_callback_free */
276 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
279 char key[10*DATA_MAX_NAME_LEN];
286 if (0 != strcmp (ds->type, vl->type)) {
287 ERROR ("write_http plugin: DS type does not match "
292 /* Copy the identifier to `key' and escape it. */
293 status = FORMAT_VL (key, sizeof (key), vl);
295 ERROR ("write_http plugin: error with format_name");
298 escape_string (key, sizeof (key));
300 /* Convert the values to an ASCII representation and put that into
302 status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
304 ERROR ("write_http plugin: error with "
305 "wh_value_list_to_string");
309 command_len = (size_t) ssnprintf (command, sizeof (command),
310 "PUTVAL %s interval=%.3f %s\r\n",
312 CDTIME_T_TO_DOUBLE (vl->interval),
314 if (command_len >= sizeof (command)) {
315 ERROR ("write_http plugin: Command buffer too small: "
316 "Need %zu bytes.", command_len + 1);
320 pthread_mutex_lock (&cb->send_lock);
322 if (cb->curl == NULL)
324 status = wh_callback_init (cb);
327 ERROR ("write_http plugin: wh_callback_init failed.");
328 pthread_mutex_unlock (&cb->send_lock);
333 if (command_len >= cb->send_buffer_free)
335 status = wh_flush_nolock (/* timeout = */ 0, cb);
338 pthread_mutex_unlock (&cb->send_lock);
342 assert (command_len < cb->send_buffer_free);
344 /* `command_len + 1' because `command_len' does not include the
345 * trailing null byte. Neither does `send_buffer_fill'. */
346 memcpy (cb->send_buffer + cb->send_buffer_fill,
347 command, command_len + 1);
348 cb->send_buffer_fill += command_len;
349 cb->send_buffer_free -= command_len;
351 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
353 cb->send_buffer_fill, sizeof (cb->send_buffer),
354 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
357 /* Check if we have enough space for this command. */
358 pthread_mutex_unlock (&cb->send_lock);
361 } /* }}} int wh_write_command */
363 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
368 pthread_mutex_lock (&cb->send_lock);
370 if (cb->curl == NULL)
372 status = wh_callback_init (cb);
375 ERROR ("write_http plugin: wh_callback_init failed.");
376 pthread_mutex_unlock (&cb->send_lock);
381 status = format_json_value_list (cb->send_buffer,
382 &cb->send_buffer_fill,
383 &cb->send_buffer_free,
384 ds, vl, cb->store_rates);
385 if (status == (-ENOMEM))
387 status = wh_flush_nolock (/* timeout = */ 0, cb);
390 wh_reset_buffer (cb);
391 pthread_mutex_unlock (&cb->send_lock);
395 status = format_json_value_list (cb->send_buffer,
396 &cb->send_buffer_fill,
397 &cb->send_buffer_free,
398 ds, vl, cb->store_rates);
402 pthread_mutex_unlock (&cb->send_lock);
406 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
408 cb->send_buffer_fill, sizeof (cb->send_buffer),
409 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
411 /* Check if we have enough space for this command. */
412 pthread_mutex_unlock (&cb->send_lock);
415 } /* }}} int wh_write_json */
417 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
418 user_data_t *user_data)
423 if (user_data == NULL)
426 cb = user_data->data;
428 if (cb->format == WH_FORMAT_JSON)
429 status = wh_write_json (ds, vl, cb);
431 status = wh_write_command (ds, vl, cb);
434 } /* }}} int wh_write */
436 static int config_set_string (char **ret_string, /* {{{ */
441 if ((ci->values_num != 1)
442 || (ci->values[0].type != OCONFIG_TYPE_STRING))
444 WARNING ("write_http plugin: The `%s' config option "
445 "needs exactly one string argument.", ci->key);
449 string = strdup (ci->values[0].value.string);
452 ERROR ("write_http plugin: strdup failed.");
456 if (*ret_string != NULL)
458 *ret_string = string;
461 } /* }}} int config_set_string */
463 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
465 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
467 WARNING ("write_http plugin: The `%s' config option "
468 "needs exactly one boolean argument.", ci->key);
472 *dest = ci->values[0].value.boolean ? 1 : 0;
475 } /* }}} int config_set_boolean */
477 static int config_set_format (wh_callback_t *cb, /* {{{ */
482 if ((ci->values_num != 1)
483 || (ci->values[0].type != OCONFIG_TYPE_STRING))
485 WARNING ("write_http plugin: The `%s' config option "
486 "needs exactly one string argument.", ci->key);
490 string = ci->values[0].value.string;
491 if (strcasecmp ("Command", string) == 0)
492 cb->format = WH_FORMAT_COMMAND;
493 else if (strcasecmp ("JSON", string) == 0)
494 cb->format = WH_FORMAT_JSON;
497 ERROR ("write_http plugin: Invalid format string: %s",
503 } /* }}} int config_set_string */
505 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
508 user_data_t user_data;
511 cb = malloc (sizeof (*cb));
514 ERROR ("write_http plugin: malloc failed.");
517 memset (cb, 0, sizeof (*cb));
521 cb->credentials = NULL;
525 cb->format = WH_FORMAT_COMMAND;
528 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
530 config_set_string (&cb->location, ci);
531 if (cb->location == NULL)
534 for (i = 0; i < ci->children_num; i++)
536 oconfig_item_t *child = ci->children + i;
538 if (strcasecmp ("User", child->key) == 0)
539 config_set_string (&cb->user, child);
540 else if (strcasecmp ("Password", child->key) == 0)
541 config_set_string (&cb->pass, child);
542 else if (strcasecmp ("VerifyPeer", child->key) == 0)
543 config_set_boolean (&cb->verify_peer, child);
544 else if (strcasecmp ("VerifyHost", child->key) == 0)
545 config_set_boolean (&cb->verify_host, child);
546 else if (strcasecmp ("CACert", child->key) == 0)
547 config_set_string (&cb->cacert, child);
548 else if (strcasecmp ("Format", child->key) == 0)
549 config_set_format (cb, child);
550 else if (strcasecmp ("StoreRates", child->key) == 0)
551 config_set_boolean (&cb->store_rates, child);
554 ERROR ("write_http plugin: Invalid configuration "
555 "option: %s.", child->key);
559 DEBUG ("write_http: Registering write callback with URL %s",
562 memset (&user_data, 0, sizeof (user_data));
564 user_data.free_func = NULL;
565 plugin_register_flush ("write_http", wh_flush, &user_data);
567 user_data.free_func = wh_callback_free;
568 plugin_register_write ("write_http", wh_write, &user_data);
571 } /* }}} int wh_config_url */
573 static int wh_config (oconfig_item_t *ci) /* {{{ */
577 for (i = 0; i < ci->children_num; i++)
579 oconfig_item_t *child = ci->children + i;
581 if (strcasecmp ("URL", child->key) == 0)
582 wh_config_url (child);
585 ERROR ("write_http plugin: Invalid configuration "
586 "option: %s.", child->key);
591 } /* }}} int wh_config */
593 void module_register (void) /* {{{ */
595 plugin_register_complex_config ("write_http", wh_config);
596 } /* }}} void module_register */
598 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */