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 time_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 = time (NULL);
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, PACKAGE_NAME"/"PACKAGE_VERSION);
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 (int timeout, wh_callback_t *cb) /* {{{ */
165 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
166 "send_buffer_fill = %zu;",
167 timeout, cb->send_buffer_fill);
174 if ((cb->send_buffer_init_time + timeout) > now)
178 if (cb->format == WH_FORMAT_COMMAND)
180 if (cb->send_buffer_fill <= 0)
182 cb->send_buffer_init_time = time (NULL);
186 status = wh_send_buffer (cb);
187 wh_reset_buffer (cb);
189 else if (cb->format == WH_FORMAT_JSON)
191 if (cb->send_buffer_fill <= 2)
193 cb->send_buffer_init_time = time (NULL);
197 status = format_json_finalize (cb->send_buffer,
198 &cb->send_buffer_fill,
199 &cb->send_buffer_free);
202 ERROR ("write_http: wh_flush_nolock: "
203 "format_json_finalize failed.");
204 wh_reset_buffer (cb);
208 status = wh_send_buffer (cb);
209 wh_reset_buffer (cb);
213 ERROR ("write_http: wh_flush_nolock: "
214 "Unknown format: %i",
220 } /* }}} wh_flush_nolock */
222 static int wh_flush (int timeout, /* {{{ */
223 const char *identifier __attribute__((unused)),
224 user_data_t *user_data)
229 if (user_data == NULL)
232 cb = user_data->data;
234 pthread_mutex_lock (&cb->send_lock);
236 if (cb->curl == NULL)
238 status = wh_callback_init (cb);
241 ERROR ("write_http plugin: wh_callback_init failed.");
242 pthread_mutex_unlock (&cb->send_lock);
247 status = wh_flush_nolock (timeout, cb);
248 pthread_mutex_unlock (&cb->send_lock);
251 } /* }}} int wh_flush */
253 static void wh_callback_free (void *data) /* {{{ */
262 wh_flush_nolock (/* timeout = */ -1, cb);
264 curl_easy_cleanup (cb->curl);
265 sfree (cb->location);
268 sfree (cb->credentials);
272 } /* }}} void wh_callback_free */
274 static int wh_value_list_to_string (char *buffer, /* {{{ */
276 const data_set_t *ds, const value_list_t *vl,
282 gauge_t *rates = NULL;
284 assert (0 == strcmp (ds->type, vl->type));
286 memset (buffer, 0, buffer_size);
288 #define BUFFER_ADD(...) do { \
289 status = ssnprintf (buffer + offset, buffer_size - offset, \
296 else if (((size_t) status) >= (buffer_size - offset)) \
302 offset += ((size_t) status); \
305 BUFFER_ADD ("%lu", (unsigned long) vl->time);
307 for (i = 0; i < ds->ds_num; i++)
309 if (ds->ds[i].type == DS_TYPE_GAUGE)
310 BUFFER_ADD (":%f", vl->values[i].gauge);
311 else if (cb->store_rates)
314 rates = uc_get_rate (ds, vl);
317 WARNING ("write_http plugin: "
318 "uc_get_rate failed.");
321 BUFFER_ADD (":%g", rates[i]);
323 else if (ds->ds[i].type == DS_TYPE_COUNTER)
324 BUFFER_ADD (":%llu", vl->values[i].counter);
325 else if (ds->ds[i].type == DS_TYPE_DERIVE)
326 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
327 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
328 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
331 ERROR ("write_http plugin: Unknown data source type: %i",
336 } /* for ds->ds_num */
342 } /* }}} int wh_value_list_to_string */
344 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
347 char key[10*DATA_MAX_NAME_LEN];
354 if (0 != strcmp (ds->type, vl->type)) {
355 ERROR ("write_http plugin: DS type does not match "
360 /* Copy the identifier to `key' and escape it. */
361 status = FORMAT_VL (key, sizeof (key), vl);
363 ERROR ("write_http plugin: error with format_name");
366 escape_string (key, sizeof (key));
368 /* Convert the values to an ASCII representation and put that into
370 status = wh_value_list_to_string (values, sizeof (values), ds, vl, cb);
372 ERROR ("write_http plugin: error with "
373 "wh_value_list_to_string");
377 command_len = (size_t) ssnprintf (command, sizeof (command),
378 "PUTVAL %s interval=%i %s\r\n",
379 key, vl->interval, values);
380 if (command_len >= sizeof (command)) {
381 ERROR ("write_http plugin: Command buffer too small: "
382 "Need %zu bytes.", command_len + 1);
386 pthread_mutex_lock (&cb->send_lock);
388 if (cb->curl == NULL)
390 status = wh_callback_init (cb);
393 ERROR ("write_http plugin: wh_callback_init failed.");
394 pthread_mutex_unlock (&cb->send_lock);
399 if (command_len >= cb->send_buffer_free)
401 status = wh_flush_nolock (/* timeout = */ -1, cb);
404 pthread_mutex_unlock (&cb->send_lock);
408 assert (command_len < cb->send_buffer_free);
410 /* `command_len + 1' because `command_len' does not include the
411 * trailing null byte. Neither does `send_buffer_fill'. */
412 memcpy (cb->send_buffer + cb->send_buffer_fill,
413 command, command_len + 1);
414 cb->send_buffer_fill += command_len;
415 cb->send_buffer_free -= command_len;
417 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
419 cb->send_buffer_fill, sizeof (cb->send_buffer),
420 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
423 /* Check if we have enough space for this command. */
424 pthread_mutex_unlock (&cb->send_lock);
427 } /* }}} int wh_write_command */
429 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
434 pthread_mutex_lock (&cb->send_lock);
436 if (cb->curl == NULL)
438 status = wh_callback_init (cb);
441 ERROR ("write_http plugin: wh_callback_init failed.");
442 pthread_mutex_unlock (&cb->send_lock);
447 status = format_json_value_list (cb->send_buffer,
448 &cb->send_buffer_fill,
449 &cb->send_buffer_free,
450 ds, vl, cb->store_rates);
451 if (status == (-ENOMEM))
453 status = wh_flush_nolock (/* timeout = */ -1, cb);
456 wh_reset_buffer (cb);
457 pthread_mutex_unlock (&cb->send_lock);
461 status = format_json_value_list (cb->send_buffer,
462 &cb->send_buffer_fill,
463 &cb->send_buffer_free,
464 ds, vl, cb->store_rates);
468 pthread_mutex_unlock (&cb->send_lock);
472 DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
474 cb->send_buffer_fill, sizeof (cb->send_buffer),
475 100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
477 /* Check if we have enough space for this command. */
478 pthread_mutex_unlock (&cb->send_lock);
481 } /* }}} int wh_write_json */
483 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
484 user_data_t *user_data)
489 if (user_data == NULL)
492 cb = user_data->data;
494 if (cb->format == WH_FORMAT_JSON)
495 status = wh_write_json (ds, vl, cb);
497 status = wh_write_command (ds, vl, cb);
500 } /* }}} int wh_write */
502 static int config_set_string (char **ret_string, /* {{{ */
507 if ((ci->values_num != 1)
508 || (ci->values[0].type != OCONFIG_TYPE_STRING))
510 WARNING ("write_http plugin: The `%s' config option "
511 "needs exactly one string argument.", ci->key);
515 string = strdup (ci->values[0].value.string);
518 ERROR ("write_http plugin: strdup failed.");
522 if (*ret_string != NULL)
524 *ret_string = string;
527 } /* }}} int config_set_string */
529 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
531 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
533 WARNING ("write_http plugin: The `%s' config option "
534 "needs exactly one boolean argument.", ci->key);
538 *dest = ci->values[0].value.boolean ? 1 : 0;
541 } /* }}} int config_set_boolean */
543 static int config_set_format (wh_callback_t *cb, /* {{{ */
548 if ((ci->values_num != 1)
549 || (ci->values[0].type != OCONFIG_TYPE_STRING))
551 WARNING ("write_http plugin: The `%s' config option "
552 "needs exactly one string argument.", ci->key);
556 string = ci->values[0].value.string;
557 if (strcasecmp ("Command", string) == 0)
558 cb->format = WH_FORMAT_COMMAND;
559 else if (strcasecmp ("JSON", string) == 0)
560 cb->format = WH_FORMAT_JSON;
563 ERROR ("write_http plugin: Invalid format string: %s",
569 } /* }}} int config_set_string */
571 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
574 user_data_t user_data;
577 cb = malloc (sizeof (*cb));
580 ERROR ("write_http plugin: malloc failed.");
583 memset (cb, 0, sizeof (*cb));
587 cb->credentials = NULL;
591 cb->format = WH_FORMAT_COMMAND;
594 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
596 config_set_string (&cb->location, ci);
597 if (cb->location == NULL)
600 for (i = 0; i < ci->children_num; i++)
602 oconfig_item_t *child = ci->children + i;
604 if (strcasecmp ("User", child->key) == 0)
605 config_set_string (&cb->user, child);
606 else if (strcasecmp ("Password", child->key) == 0)
607 config_set_string (&cb->pass, child);
608 else if (strcasecmp ("VerifyPeer", child->key) == 0)
609 config_set_boolean (&cb->verify_peer, child);
610 else if (strcasecmp ("VerifyHost", child->key) == 0)
611 config_set_boolean (&cb->verify_host, child);
612 else if (strcasecmp ("CACert", child->key) == 0)
613 config_set_string (&cb->cacert, child);
614 else if (strcasecmp ("Format", child->key) == 0)
615 config_set_format (cb, child);
616 else if (strcasecmp ("StoreRates", child->key) == 0)
617 config_set_boolean (&cb->store_rates, child);
620 ERROR ("write_http plugin: Invalid configuration "
621 "option: %s.", child->key);
625 DEBUG ("write_http: Registering write callback with URL %s",
628 memset (&user_data, 0, sizeof (user_data));
630 user_data.free_func = NULL;
631 plugin_register_flush ("write_http", wh_flush, &user_data);
633 user_data.free_func = wh_callback_free;
634 plugin_register_write ("write_http", wh_write, &user_data);
637 } /* }}} int wh_config_url */
639 static int wh_config (oconfig_item_t *ci) /* {{{ */
643 for (i = 0; i < ci->children_num; i++)
645 oconfig_item_t *child = ci->children + i;
647 if (strcasecmp ("URL", child->key) == 0)
648 wh_config_url (child);
651 ERROR ("write_http plugin: Invalid configuration "
652 "option: %s.", child->key);
657 } /* }}} int wh_config */
659 static int wh_init (void) /* {{{ */
661 /* Call this while collectd is still single-threaded to avoid
662 * initialization issues in libgcrypt. */
663 curl_global_init (CURL_GLOBAL_SSL);
665 } /* }}} int wh_init */
667 void module_register (void) /* {{{ */
669 plugin_register_complex_config ("write_http", wh_config);
670 plugin_register_init ("write_http", wh_init);
671 } /* }}} void module_register */
673 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */