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 headers = curl_slist_append (headers, "Expect:");
107 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
109 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
110 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
112 if (cb->user != NULL)
114 size_t credentials_size;
116 credentials_size = strlen (cb->user) + 2;
117 if (cb->pass != NULL)
118 credentials_size += strlen (cb->pass);
120 cb->credentials = (char *) malloc (credentials_size);
121 if (cb->credentials == NULL)
123 ERROR ("curl plugin: malloc failed.");
127 ssnprintf (cb->credentials, credentials_size, "%s:%s",
128 cb->user, (cb->pass == NULL) ? "" : cb->pass);
129 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
130 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
133 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, cb->verify_peer);
134 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
135 cb->verify_host ? 2 : 0);
136 if (cb->cacert != NULL)
137 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
139 wh_reset_buffer (cb);
142 } /* }}} int wh_callback_init */
144 static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */
148 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
149 "send_buffer_fill = %zu;",
150 timeout, cb->send_buffer_fill);
157 if ((cb->send_buffer_init_time + timeout) > now)
161 if (cb->send_buffer_fill <= 0)
163 cb->send_buffer_init_time = time (NULL);
167 status = wh_send_buffer (cb);
168 wh_reset_buffer (cb);
171 } /* }}} wh_flush_nolock */
173 static int wh_flush (int timeout, /* {{{ */
174 const char *identifier __attribute__((unused)),
175 user_data_t *user_data)
180 if (user_data == NULL)
183 cb = user_data->data;
185 pthread_mutex_lock (&cb->send_lock);
187 if (cb->curl == NULL)
189 status = wh_callback_init (cb);
192 ERROR ("write_http plugin: wh_callback_init failed.");
193 pthread_mutex_unlock (&cb->send_lock);
198 status = wh_flush_nolock (timeout, cb);
199 pthread_mutex_unlock (&cb->send_lock);
202 } /* }}} int wh_flush */
204 static void wh_callback_free (void *data) /* {{{ */
213 wh_flush_nolock (/* timeout = */ -1, cb);
215 curl_easy_cleanup (cb->curl);
216 sfree (cb->location);
219 sfree (cb->credentials);
223 } /* }}} void wh_callback_free */
225 static int wh_value_list_to_string (char *buffer, /* {{{ */
227 const data_set_t *ds, const value_list_t *vl)
233 assert (0 == strcmp (ds->type, vl->type));
235 memset (buffer, 0, buffer_size);
237 #define BUFFER_ADD(...) do { \
238 status = ssnprintf (buffer + offset, buffer_size - offset, \
242 else if (((size_t) status) >= (buffer_size - offset)) \
245 offset += ((size_t) status); \
248 BUFFER_ADD ("%lu", (unsigned long) vl->time);
250 for (i = 0; i < ds->ds_num; i++)
252 if (ds->ds[i].type == DS_TYPE_GAUGE)
253 BUFFER_ADD (":%f", vl->values[i].gauge);
254 else if (ds->ds[i].type == DS_TYPE_COUNTER)
255 BUFFER_ADD (":%llu", vl->values[i].counter);
256 else if (ds->ds[i].type == DS_TYPE_DERIVE)
257 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
258 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
259 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
262 ERROR ("write_http plugin: Unknown data source type: %i",
266 } /* for ds->ds_num */
271 } /* }}} int wh_value_list_to_string */
273 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
276 char key[10*DATA_MAX_NAME_LEN];
283 if (0 != strcmp (ds->type, vl->type)) {
284 ERROR ("write_http plugin: DS type does not match "
289 /* Copy the identifier to `key' and escape it. */
290 status = FORMAT_VL (key, sizeof (key), vl);
292 ERROR ("write_http plugin: error with format_name");
295 escape_string (key, sizeof (key));
297 /* Convert the values to an ASCII representation and put that into
299 status = wh_value_list_to_string (values, sizeof (values), ds, vl);
301 ERROR ("write_http plugin: error with "
302 "wh_value_list_to_string");
306 command_len = (size_t) ssnprintf (command, sizeof (command),
307 "PUTVAL %s interval=%i %s\n",
308 key, vl->interval, values);
309 if (command_len >= sizeof (command)) {
310 ERROR ("write_http plugin: Command buffer too small: "
311 "Need %zu bytes.", command_len + 1);
315 pthread_mutex_lock (&cb->send_lock);
317 if (cb->curl == NULL)
319 status = wh_callback_init (cb);
322 ERROR ("write_http plugin: wh_callback_init failed.");
323 pthread_mutex_unlock (&cb->send_lock);
328 if (command_len >= cb->send_buffer_free)
330 status = wh_flush_nolock (/* timeout = */ -1, cb);
333 pthread_mutex_unlock (&cb->send_lock);
337 assert (command_len < cb->send_buffer_free);
339 /* `command_len + 1' because `command_len' does not include the
340 * trailing null byte. Neither does `send_buffer_fill'. */
341 memcpy (cb->send_buffer + cb->send_buffer_fill,
342 command, command_len + 1);
343 cb->send_buffer_fill += command_len;
344 cb->send_buffer_free -= command_len;
346 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
348 cb->send_buffer_fill, sizeof (cb->send_buffer),
349 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
352 /* Check if we have enough space for this command. */
353 pthread_mutex_unlock (&cb->send_lock);
356 } /* }}} int wh_write_command */
358 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
359 user_data_t *user_data)
364 if (user_data == NULL)
367 cb = user_data->data;
369 status = wh_write_command (ds, vl, cb);
371 } /* }}} int wh_write */
373 static int config_set_string (char **ret_string, /* {{{ */
378 if ((ci->values_num != 1)
379 || (ci->values[0].type != OCONFIG_TYPE_STRING))
381 WARNING ("write_http plugin: The `%s' config option "
382 "needs exactly one string argument.", ci->key);
386 string = strdup (ci->values[0].value.string);
389 ERROR ("write_http plugin: strdup failed.");
393 if (*ret_string != NULL)
395 *ret_string = string;
398 } /* }}} int config_set_string */
400 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
402 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
404 WARNING ("write_http plugin: The `%s' config option "
405 "needs exactly one boolean argument.", ci->key);
409 *dest = ci->values[0].value.boolean ? 1 : 0;
412 } /* }}} int config_set_boolean */
414 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
417 user_data_t user_data;
420 cb = malloc (sizeof (*cb));
423 ERROR ("write_http plugin: malloc failed.");
426 memset (cb, 0, sizeof (*cb));
430 cb->credentials = NULL;
436 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
438 config_set_string (&cb->location, ci);
439 if (cb->location == NULL)
442 for (i = 0; i < ci->children_num; i++)
444 oconfig_item_t *child = ci->children + i;
446 if (strcasecmp ("User", child->key) == 0)
447 config_set_string (&cb->user, child);
448 else if (strcasecmp ("Password", child->key) == 0)
449 config_set_string (&cb->pass, child);
450 else if (strcasecmp ("VerifyPeer", child->key) == 0)
451 config_set_boolean (&cb->verify_peer, child);
452 else if (strcasecmp ("VerifyHost", child->key) == 0)
453 config_set_boolean (&cb->verify_host, child);
454 else if (strcasecmp ("CACert", child->key) == 0)
455 config_set_string (&cb->cacert, child);
458 ERROR ("write_http plugin: Invalid configuration "
459 "option: %s.", child->key);
463 DEBUG ("write_http: Registering write callback with URL %s",
466 memset (&user_data, 0, sizeof (user_data));
468 user_data.free_func = NULL;
469 plugin_register_flush ("write_http", wh_flush, &user_data);
471 user_data.free_func = wh_callback_free;
472 plugin_register_write ("write_http", wh_write, &user_data);
475 } /* }}} int wh_config_url */
477 static int wh_config (oconfig_item_t *ci) /* {{{ */
481 for (i = 0; i < ci->children_num; i++)
483 oconfig_item_t *child = ci->children + i;
485 if (strcasecmp ("URL", child->key) == 0)
486 wh_config_url (child);
489 ERROR ("write_http plugin: Invalid configuration "
490 "option: %s.", child->key);
495 } /* }}} int wh_config */
497 void module_register (void) /* {{{ */
499 plugin_register_complex_config ("write_http", wh_config);
500 } /* }}} void module_register */
502 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */