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"
36 #include <curl/curl.h>
53 char curl_errbuf[CURL_ERROR_SIZE];
55 char send_buffer[4096];
56 size_t send_buffer_free;
57 size_t send_buffer_fill;
58 time_t send_buffer_init_time;
60 pthread_mutex_t send_lock;
62 typedef struct wh_callback_s wh_callback_t;
64 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
66 memset (cb->send_buffer, 0, sizeof (cb->send_buffer));
67 cb->send_buffer_free = sizeof (cb->send_buffer);
68 cb->send_buffer_fill = 0;
69 cb->send_buffer_init_time = time (NULL);
70 } /* }}} wh_reset_buffer */
72 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
76 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
77 status = curl_easy_perform (cb->curl);
80 ERROR ("write_http plugin: curl_easy_perform failed with "
82 status, cb->curl_errbuf);
85 } /* }}} wh_send_buffer */
87 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
89 struct curl_slist *headers;
94 cb->curl = curl_easy_init ();
97 ERROR ("curl plugin: curl_easy_init failed.");
101 curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
104 headers = curl_slist_append (headers, "Accept: */*");
105 headers = curl_slist_append (headers, "Content-Type: text/plain");
106 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
108 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
109 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
111 if (cb->user != NULL)
113 size_t credentials_size;
115 credentials_size = strlen (cb->user) + 2;
116 if (cb->pass != NULL)
117 credentials_size += strlen (cb->pass);
119 cb->credentials = (char *) malloc (credentials_size);
120 if (cb->credentials == NULL)
122 ERROR ("curl plugin: malloc failed.");
126 ssnprintf (cb->credentials, credentials_size, "%s:%s",
127 cb->user, (cb->pass == NULL) ? "" : cb->pass);
128 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
129 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
132 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, cb->verify_peer);
133 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
134 cb->verify_host ? 2 : 0);
135 if (cb->cacert != NULL)
136 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
138 wh_reset_buffer (cb);
141 } /* }}} int wh_callback_init */
143 static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */
147 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
148 "send_buffer_fill = %zu;",
149 timeout, cb->send_buffer_fill);
156 if ((cb->send_buffer_init_time + timeout) > now)
160 if (cb->send_buffer_fill <= 0)
162 cb->send_buffer_init_time = time (NULL);
166 status = wh_send_buffer (cb);
167 wh_reset_buffer (cb);
170 } /* }}} wh_flush_nolock */
172 static int wh_flush (int timeout, /* {{{ */
173 const char *identifier __attribute__((unused)),
174 user_data_t *user_data)
179 if (user_data == NULL)
182 cb = user_data->data;
184 pthread_mutex_lock (&cb->send_lock);
186 if (cb->curl == NULL)
188 status = wh_callback_init (cb);
191 ERROR ("write_http plugin: wh_callback_init failed.");
192 pthread_mutex_unlock (&cb->send_lock);
197 status = wh_flush_nolock (timeout, cb);
198 pthread_mutex_unlock (&cb->send_lock);
201 } /* }}} int wh_flush */
203 static void wh_callback_free (void *data) /* {{{ */
212 wh_flush_nolock (/* timeout = */ -1, cb);
214 curl_easy_cleanup (cb->curl);
215 sfree (cb->location);
218 sfree (cb->credentials);
222 } /* }}} void wh_callback_free */
224 static int wh_value_list_to_string (char *buffer, /* {{{ */
226 const data_set_t *ds, const value_list_t *vl)
232 assert (0 == strcmp (ds->type, vl->type));
234 memset (buffer, 0, buffer_size);
236 #define BUFFER_ADD(...) do { \
237 status = ssnprintf (buffer + offset, buffer_size - offset, \
241 else if (((size_t) status) >= (buffer_size - offset)) \
244 offset += ((size_t) status); \
247 BUFFER_ADD ("%lu", (unsigned long) vl->time);
249 for (i = 0; i < ds->ds_num; i++)
251 if (ds->ds[i].type == DS_TYPE_GAUGE)
252 BUFFER_ADD (":%f", vl->values[i].gauge);
253 else if (ds->ds[i].type == DS_TYPE_COUNTER)
254 BUFFER_ADD (":%llu", vl->values[i].counter);
255 else if (ds->ds[i].type == DS_TYPE_DERIVE)
256 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
257 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
258 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
261 ERROR ("write_http plugin: Unknown data source type: %i",
265 } /* for ds->ds_num */
270 } /* }}} int wh_value_list_to_string */
272 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
275 char key[10*DATA_MAX_NAME_LEN];
282 if (0 != strcmp (ds->type, vl->type)) {
283 ERROR ("write_http plugin: DS type does not match "
288 /* Copy the identifier to `key' and escape it. */
289 status = FORMAT_VL (key, sizeof (key), vl);
291 ERROR ("write_http plugin: error with format_name");
294 escape_string (key, sizeof (key));
296 /* Convert the values to an ASCII representation and put that into
298 status = wh_value_list_to_string (values, sizeof (values), ds, vl);
300 ERROR ("write_http plugin: error with "
301 "wh_value_list_to_string");
305 command_len = (size_t) ssnprintf (command, sizeof (command),
306 "PUTVAL %s interval=%i %s\n",
307 key, vl->interval, values);
308 if (command_len >= sizeof (command)) {
309 ERROR ("write_http plugin: Command buffer too small: "
310 "Need %zu bytes.", command_len + 1);
314 pthread_mutex_lock (&cb->send_lock);
316 if (cb->curl == NULL)
318 status = wh_callback_init (cb);
321 ERROR ("write_http plugin: wh_callback_init failed.");
322 pthread_mutex_unlock (&cb->send_lock);
327 if (command_len >= cb->send_buffer_free)
329 status = wh_flush_nolock (/* timeout = */ -1, cb);
332 pthread_mutex_unlock (&cb->send_lock);
336 assert (command_len < cb->send_buffer_free);
338 /* `command_len + 1' because `command_len' does not include the
339 * trailing null byte. Neither does `send_buffer_fill'. */
340 memcpy (cb->send_buffer + cb->send_buffer_fill,
341 command, command_len + 1);
342 cb->send_buffer_fill += command_len;
343 cb->send_buffer_free -= command_len;
345 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
347 cb->send_buffer_fill, sizeof (cb->send_buffer),
348 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
351 /* Check if we have enough space for this command. */
352 pthread_mutex_unlock (&cb->send_lock);
355 } /* }}} int wh_write_command */
357 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
358 user_data_t *user_data)
363 if (user_data == NULL)
366 cb = user_data->data;
368 status = wh_write_command (ds, vl, cb);
370 } /* }}} int wh_write */
372 static int config_set_string (char **ret_string, /* {{{ */
377 if ((ci->values_num != 1)
378 || (ci->values[0].type != OCONFIG_TYPE_STRING))
380 WARNING ("write_http plugin: The `%s' config option "
381 "needs exactly one string argument.", ci->key);
385 string = strdup (ci->values[0].value.string);
388 ERROR ("write_http plugin: strdup failed.");
392 if (*ret_string != NULL)
394 *ret_string = string;
397 } /* }}} int config_set_string */
399 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
401 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
403 WARNING ("write_http plugin: The `%s' config option "
404 "needs exactly one boolean argument.", ci->key);
408 *dest = ci->values[0].value.boolean ? 1 : 0;
411 } /* }}} int config_set_boolean */
413 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
416 user_data_t user_data;
419 cb = malloc (sizeof (*cb));
422 ERROR ("write_http plugin: malloc failed.");
425 memset (cb, 0, sizeof (*cb));
429 cb->credentials = NULL;
435 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
437 config_set_string (&cb->location, ci);
438 if (cb->location == NULL)
441 for (i = 0; i < ci->children_num; i++)
443 oconfig_item_t *child = ci->children + i;
445 if (strcasecmp ("User", child->key) == 0)
446 config_set_string (&cb->user, child);
447 else if (strcasecmp ("Password", child->key) == 0)
448 config_set_string (&cb->pass, child);
449 else if (strcasecmp ("VerifyPeer", child->key) == 0)
450 config_set_boolean (&cb->verify_peer, child);
451 else if (strcasecmp ("VerifyHost", child->key) == 0)
452 config_set_boolean (&cb->verify_host, child);
453 else if (strcasecmp ("CACert", child->key) == 0)
454 config_set_string (&cb->cacert, child);
457 ERROR ("write_http plugin: Invalid configuration "
458 "option: %s.", child->key);
462 DEBUG ("write_http: Registering write callback with URL %s",
465 memset (&user_data, 0, sizeof (user_data));
467 user_data.free_func = NULL;
468 plugin_register_flush ("write_http", wh_flush, &user_data);
470 user_data.free_func = wh_callback_free;
471 plugin_register_write ("write_http", wh_write, &user_data);
474 } /* }}} int wh_config_url */
476 static int wh_config (oconfig_item_t *ci) /* {{{ */
480 for (i = 0; i < ci->children_num; i++)
482 oconfig_item_t *child = ci->children + i;
484 if (strcasecmp ("URL", child->key) == 0)
485 wh_config_url (child);
488 ERROR ("write_http plugin: Invalid configuration "
489 "option: %s.", child->key);
494 } /* }}} int wh_config */
496 void module_register (void) /* {{{ */
498 plugin_register_complex_config ("write_http", wh_config);
499 } /* }}} void module_register */
501 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */