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>
53 #define WH_FORMAT_COMMAND 0
54 #define WH_FORMAT_JSON 1
58 char curl_errbuf[CURL_ERROR_SIZE];
60 char send_buffer[4096];
61 size_t send_buffer_free;
62 size_t send_buffer_fill;
63 time_t send_buffer_init_time;
65 pthread_mutex_t send_lock;
67 typedef struct wh_callback_s wh_callback_t;
69 static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */
71 memset (cb->send_buffer, 0, sizeof (cb->send_buffer));
72 cb->send_buffer_free = sizeof (cb->send_buffer);
73 cb->send_buffer_fill = 0;
74 cb->send_buffer_init_time = time (NULL);
76 if (cb->format == WH_FORMAT_JSON)
78 format_json_initialize (cb->send_buffer,
79 &cb->send_buffer_fill,
80 &cb->send_buffer_free);
82 } /* }}} wh_reset_buffer */
84 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
88 curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
89 status = curl_easy_perform (cb->curl);
92 ERROR ("write_http plugin: curl_easy_perform failed with "
94 status, cb->curl_errbuf);
97 } /* }}} wh_send_buffer */
99 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
101 struct curl_slist *headers;
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_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
116 headers = curl_slist_append (headers, "Accept: */*");
117 if (cb->format == WH_FORMAT_JSON)
118 headers = curl_slist_append (headers, "Content-Type: application/json");
120 headers = curl_slist_append (headers, "Content-Type: text/plain");
121 headers = curl_slist_append (headers, "Expect:");
122 curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
124 curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
125 curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
127 if (cb->user != NULL)
129 size_t credentials_size;
131 credentials_size = strlen (cb->user) + 2;
132 if (cb->pass != NULL)
133 credentials_size += strlen (cb->pass);
135 cb->credentials = (char *) malloc (credentials_size);
136 if (cb->credentials == NULL)
138 ERROR ("curl plugin: malloc failed.");
142 ssnprintf (cb->credentials, credentials_size, "%s:%s",
143 cb->user, (cb->pass == NULL) ? "" : cb->pass);
144 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
145 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
148 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, cb->verify_peer);
149 curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
150 cb->verify_host ? 2 : 0);
151 if (cb->cacert != NULL)
152 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
154 wh_reset_buffer (cb);
157 } /* }}} int wh_callback_init */
159 static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */
163 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
164 "send_buffer_fill = %zu;",
165 timeout, cb->send_buffer_fill);
172 if ((cb->send_buffer_init_time + timeout) > now)
176 if (cb->format == WH_FORMAT_COMMAND)
178 if (cb->send_buffer_fill <= 0)
180 cb->send_buffer_init_time = time (NULL);
184 status = wh_send_buffer (cb);
185 wh_reset_buffer (cb);
187 else if (cb->format == WH_FORMAT_JSON)
189 if (cb->send_buffer_fill <= 2)
191 cb->send_buffer_init_time = time (NULL);
195 status = format_json_finalize (cb->send_buffer,
196 &cb->send_buffer_fill,
197 &cb->send_buffer_free);
200 ERROR ("write_http: wh_flush_nolock: "
201 "format_json_finalize failed.");
202 wh_reset_buffer (cb);
206 status = wh_send_buffer (cb);
207 wh_reset_buffer (cb);
211 ERROR ("write_http: wh_flush_nolock: "
212 "Unknown format: %i",
218 } /* }}} wh_flush_nolock */
220 static int wh_flush (int timeout, /* {{{ */
221 const char *identifier __attribute__((unused)),
222 user_data_t *user_data)
227 if (user_data == NULL)
230 cb = user_data->data;
232 pthread_mutex_lock (&cb->send_lock);
234 if (cb->curl == NULL)
236 status = wh_callback_init (cb);
239 ERROR ("write_http plugin: wh_callback_init failed.");
240 pthread_mutex_unlock (&cb->send_lock);
245 status = wh_flush_nolock (timeout, cb);
246 pthread_mutex_unlock (&cb->send_lock);
249 } /* }}} int wh_flush */
251 static void wh_callback_free (void *data) /* {{{ */
260 wh_flush_nolock (/* timeout = */ -1, cb);
262 curl_easy_cleanup (cb->curl);
263 sfree (cb->location);
266 sfree (cb->credentials);
270 } /* }}} void wh_callback_free */
272 static int wh_value_list_to_string (char *buffer, /* {{{ */
274 const data_set_t *ds, const value_list_t *vl)
280 assert (0 == strcmp (ds->type, vl->type));
282 memset (buffer, 0, buffer_size);
284 #define BUFFER_ADD(...) do { \
285 status = ssnprintf (buffer + offset, buffer_size - offset, \
289 else if (((size_t) status) >= (buffer_size - offset)) \
292 offset += ((size_t) status); \
295 BUFFER_ADD ("%lu", (unsigned long) vl->time);
297 for (i = 0; i < ds->ds_num; i++)
299 if (ds->ds[i].type == DS_TYPE_GAUGE)
300 BUFFER_ADD (":%f", vl->values[i].gauge);
301 else if (ds->ds[i].type == DS_TYPE_COUNTER)
302 BUFFER_ADD (":%llu", vl->values[i].counter);
303 else if (ds->ds[i].type == DS_TYPE_DERIVE)
304 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
305 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
306 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
309 ERROR ("write_http plugin: Unknown data source type: %i",
313 } /* for ds->ds_num */
318 } /* }}} int wh_value_list_to_string */
320 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
323 char key[10*DATA_MAX_NAME_LEN];
330 if (0 != strcmp (ds->type, vl->type)) {
331 ERROR ("write_http plugin: DS type does not match "
336 /* Copy the identifier to `key' and escape it. */
337 status = FORMAT_VL (key, sizeof (key), vl);
339 ERROR ("write_http plugin: error with format_name");
342 escape_string (key, sizeof (key));
344 /* Convert the values to an ASCII representation and put that into
346 status = wh_value_list_to_string (values, sizeof (values), ds, vl);
348 ERROR ("write_http plugin: error with "
349 "wh_value_list_to_string");
353 command_len = (size_t) ssnprintf (command, sizeof (command),
354 "PUTVAL %s interval=%i %s\r\n",
355 key, vl->interval, values);
356 if (command_len >= sizeof (command)) {
357 ERROR ("write_http plugin: Command buffer too small: "
358 "Need %zu bytes.", command_len + 1);
362 pthread_mutex_lock (&cb->send_lock);
364 if (cb->curl == NULL)
366 status = wh_callback_init (cb);
369 ERROR ("write_http plugin: wh_callback_init failed.");
370 pthread_mutex_unlock (&cb->send_lock);
375 if (command_len >= cb->send_buffer_free)
377 status = wh_flush_nolock (/* timeout = */ -1, cb);
380 pthread_mutex_unlock (&cb->send_lock);
384 assert (command_len < cb->send_buffer_free);
386 /* `command_len + 1' because `command_len' does not include the
387 * trailing null byte. Neither does `send_buffer_fill'. */
388 memcpy (cb->send_buffer + cb->send_buffer_fill,
389 command, command_len + 1);
390 cb->send_buffer_fill += command_len;
391 cb->send_buffer_free -= command_len;
393 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
395 cb->send_buffer_fill, sizeof (cb->send_buffer),
396 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
399 /* Check if we have enough space for this command. */
400 pthread_mutex_unlock (&cb->send_lock);
403 } /* }}} int wh_write_command */
405 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
410 pthread_mutex_lock (&cb->send_lock);
412 if (cb->curl == NULL)
414 status = wh_callback_init (cb);
417 ERROR ("write_http plugin: wh_callback_init failed.");
418 pthread_mutex_unlock (&cb->send_lock);
423 status = format_json_value_list (cb->send_buffer,
424 &cb->send_buffer_fill,
425 &cb->send_buffer_free,
427 if (status == (-ENOMEM))
429 status = wh_flush_nolock (/* timeout = */ -1, cb);
432 wh_reset_buffer (cb);
433 pthread_mutex_unlock (&cb->send_lock);
437 status = format_json_value_list (cb->send_buffer,
438 &cb->send_buffer_fill,
439 &cb->send_buffer_free,
444 pthread_mutex_unlock (&cb->send_lock);
448 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
450 cb->send_buffer_fill, sizeof (cb->send_buffer),
451 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
453 /* Check if we have enough space for this command. */
454 pthread_mutex_unlock (&cb->send_lock);
457 } /* }}} int wh_write_json */
459 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
460 user_data_t *user_data)
465 if (user_data == NULL)
468 cb = user_data->data;
470 if (cb->format == WH_FORMAT_JSON)
471 status = wh_write_json (ds, vl, cb);
473 status = wh_write_command (ds, vl, cb);
476 } /* }}} int wh_write */
478 static int config_set_string (char **ret_string, /* {{{ */
483 if ((ci->values_num != 1)
484 || (ci->values[0].type != OCONFIG_TYPE_STRING))
486 WARNING ("write_http plugin: The `%s' config option "
487 "needs exactly one string argument.", ci->key);
491 string = strdup (ci->values[0].value.string);
494 ERROR ("write_http plugin: strdup failed.");
498 if (*ret_string != NULL)
500 *ret_string = string;
503 } /* }}} int config_set_string */
505 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
507 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
509 WARNING ("write_http plugin: The `%s' config option "
510 "needs exactly one boolean argument.", ci->key);
514 *dest = ci->values[0].value.boolean ? 1 : 0;
517 } /* }}} int config_set_boolean */
519 static int config_set_format (wh_callback_t *cb, /* {{{ */
524 if ((ci->values_num != 1)
525 || (ci->values[0].type != OCONFIG_TYPE_STRING))
527 WARNING ("write_http plugin: The `%s' config option "
528 "needs exactly one string argument.", ci->key);
532 string = ci->values[0].value.string;
533 if (strcasecmp ("Command", string) == 0)
534 cb->format = WH_FORMAT_COMMAND;
535 else if (strcasecmp ("JSON", string) == 0)
536 cb->format = WH_FORMAT_JSON;
539 ERROR ("write_http plugin: Invalid format string: %s",
545 } /* }}} int config_set_string */
547 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
550 user_data_t user_data;
553 cb = malloc (sizeof (*cb));
556 ERROR ("write_http plugin: malloc failed.");
559 memset (cb, 0, sizeof (*cb));
563 cb->credentials = NULL;
567 cb->format = WH_FORMAT_COMMAND;
570 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
572 config_set_string (&cb->location, ci);
573 if (cb->location == NULL)
576 for (i = 0; i < ci->children_num; i++)
578 oconfig_item_t *child = ci->children + i;
580 if (strcasecmp ("User", child->key) == 0)
581 config_set_string (&cb->user, child);
582 else if (strcasecmp ("Password", child->key) == 0)
583 config_set_string (&cb->pass, child);
584 else if (strcasecmp ("VerifyPeer", child->key) == 0)
585 config_set_boolean (&cb->verify_peer, child);
586 else if (strcasecmp ("VerifyHost", child->key) == 0)
587 config_set_boolean (&cb->verify_host, child);
588 else if (strcasecmp ("CACert", child->key) == 0)
589 config_set_string (&cb->cacert, child);
590 else if (strcasecmp ("Format", child->key) == 0)
591 config_set_format (cb, child);
594 ERROR ("write_http plugin: Invalid configuration "
595 "option: %s.", child->key);
599 DEBUG ("write_http: Registering write callback with URL %s",
602 memset (&user_data, 0, sizeof (user_data));
604 user_data.free_func = NULL;
605 plugin_register_flush ("write_http", wh_flush, &user_data);
607 user_data.free_func = wh_callback_free;
608 plugin_register_write ("write_http", wh_write, &user_data);
611 } /* }}} int wh_config_url */
613 static int wh_config (oconfig_item_t *ci) /* {{{ */
617 for (i = 0; i < ci->children_num; i++)
619 oconfig_item_t *child = ci->children + i;
621 if (strcasecmp ("URL", child->key) == 0)
622 wh_config_url (child);
625 ERROR ("write_http plugin: Invalid configuration "
626 "option: %s.", child->key);
631 } /* }}} int wh_config */
633 void module_register (void) /* {{{ */
635 plugin_register_complex_config ("write_http", wh_config);
636 } /* }}} void module_register */
638 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */