2 * collectd - src/write_graphite.c
3 * Copyright (C) 2012 Pierre-Yves Ritschard
4 * Copyright (C) 2011 Scott Sanders
5 * Copyright (C) 2009 Paul Sadauskas
6 * Copyright (C) 2009 Doug MacEachern
7 * Copyright (C) 2007-2012 Florian octo Forster
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; only version 2 of the License is applicable.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Florian octo Forster <octo at collectd.org>
24 * Doug MacEachern <dougm at hyperic.com>
25 * Paul Sadauskas <psadauskas at gmail.com>
26 * Scott Sanders <scott at jssjr.com>
27 * Pierre-Yves Ritschard <pyr at spootnik.org>
29 * Based on the write_http plugin.
32 /* write_graphite plugin configuation example
34 * <Plugin write_graphite>
46 #include "configfile.h"
48 #include "utils_cache.h"
49 #include "utils_parse_option.h"
51 /* Folks without pthread will need to disable this plugin. */
54 #include <sys/socket.h>
57 #ifndef WG_DEFAULT_NODE
58 # define WG_DEFAULT_NODE "localhost"
61 #ifndef WG_DEFAULT_SERVICE
62 # define WG_DEFAULT_SERVICE "2003"
65 #ifndef WG_DEFAULT_ESCAPE
66 # define WG_DEFAULT_ESCAPE '_'
69 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
70 #ifndef WG_SEND_BUF_SIZE
71 # define WG_SEND_BUF_SIZE 1428
88 _Bool always_append_ds;
90 char send_buf[WG_SEND_BUF_SIZE];
93 cdtime_t send_buf_init_time;
95 pthread_mutex_t send_lock;
102 static void wg_reset_buffer (struct wg_callback *cb)
104 memset (cb->send_buf, 0, sizeof (cb->send_buf));
105 cb->send_buf_free = sizeof (cb->send_buf);
106 cb->send_buf_fill = 0;
107 cb->send_buf_init_time = cdtime ();
110 static int wg_send_buffer (struct wg_callback *cb)
114 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
117 ERROR ("write_graphite plugin: send failed with "
130 /* NOTE: You must hold cb->send_lock when calling this function! */
131 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
135 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
136 "send_buf_fill = %zu;",
140 /* timeout == 0 => flush unconditionally */
146 if ((cb->send_buf_init_time + timeout) > now)
150 if (cb->send_buf_fill <= 0)
152 cb->send_buf_init_time = cdtime ();
156 status = wg_send_buffer (cb);
157 wg_reset_buffer (cb);
162 static int wg_callback_init (struct wg_callback *cb)
164 struct addrinfo ai_hints;
165 struct addrinfo *ai_list;
166 struct addrinfo *ai_ptr;
169 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
170 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
175 memset (&ai_hints, 0, sizeof (ai_hints));
177 ai_hints.ai_flags |= AI_ADDRCONFIG;
179 ai_hints.ai_family = AF_UNSPEC;
180 ai_hints.ai_socktype = SOCK_STREAM;
184 status = getaddrinfo (node, service, &ai_hints, &ai_list);
187 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
188 node, service, gai_strerror (status));
192 assert (ai_list != NULL);
193 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
195 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
196 ai_ptr->ai_protocol);
200 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
211 freeaddrinfo (ai_list);
216 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
217 "The last error was: %s", node, service,
218 sstrerror (errno, errbuf, sizeof (errbuf)));
223 wg_reset_buffer (cb);
228 static void wg_callback_free (void *data)
230 struct wg_callback *cb;
237 pthread_mutex_lock (&cb->send_lock);
239 wg_flush_nolock (/* timeout = */ 0, cb);
249 pthread_mutex_destroy (&cb->send_lock);
254 static int wg_flush (cdtime_t timeout,
255 const char *identifier __attribute__((unused)),
256 user_data_t *user_data)
258 struct wg_callback *cb;
261 if (user_data == NULL)
264 cb = user_data->data;
266 pthread_mutex_lock (&cb->send_lock);
270 status = wg_callback_init (cb);
273 ERROR ("write_graphite plugin: wg_callback_init failed.");
274 pthread_mutex_unlock (&cb->send_lock);
279 status = wg_flush_nolock (timeout, cb);
280 pthread_mutex_unlock (&cb->send_lock);
285 static int wg_format_values (char *ret, size_t ret_len,
286 int ds_num, const data_set_t *ds, const value_list_t *vl,
291 gauge_t *rates = NULL;
293 assert (0 == strcmp (ds->type, vl->type));
295 memset (ret, 0, ret_len);
297 #define BUFFER_ADD(...) do { \
298 status = ssnprintf (ret + offset, ret_len - offset, \
305 else if (((size_t) status) >= (ret_len - offset)) \
311 offset += ((size_t) status); \
314 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
315 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
316 else if (store_rates)
319 rates = uc_get_rate (ds, vl);
322 WARNING ("format_values: "
323 "uc_get_rate failed.");
326 BUFFER_ADD ("%g", rates[ds_num]);
328 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
329 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
330 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
331 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
332 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
333 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
336 ERROR ("format_values plugin: Unknown data source type: %i",
337 ds->ds[ds_num].type);
348 static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
353 memset (dst, 0, dst_len);
358 for (i = 0; i < dst_len; i++)
367 || isspace ((int) src[i])
368 || iscntrl ((int) src[i]))
369 dst[i] = escape_char;
375 static int wg_format_name (char *ret, int ret_len,
376 const value_list_t *vl,
377 const struct wg_callback *cb,
380 char n_host[DATA_MAX_NAME_LEN];
381 char n_plugin[DATA_MAX_NAME_LEN];
382 char n_plugin_instance[DATA_MAX_NAME_LEN];
383 char n_type[DATA_MAX_NAME_LEN];
384 char n_type_instance[DATA_MAX_NAME_LEN];
389 char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
390 char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
396 postfix = cb->postfix;
400 wg_copy_escape_part (n_host, vl->host,
401 sizeof (n_host), cb->escape_char);
402 wg_copy_escape_part (n_plugin, vl->plugin,
403 sizeof (n_plugin), cb->escape_char);
404 wg_copy_escape_part (n_plugin_instance, vl->plugin_instance,
405 sizeof (n_plugin_instance), cb->escape_char);
406 wg_copy_escape_part (n_type, vl->type,
407 sizeof (n_type), cb->escape_char);
408 wg_copy_escape_part (n_type_instance, vl->type_instance,
409 sizeof (n_type_instance), cb->escape_char);
411 if (n_plugin_instance[0] != '\0')
412 ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s-%s",
413 n_plugin, n_plugin_instance);
415 sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
417 if (n_type_instance[0] != '\0')
418 ssnprintf (tmp_type, sizeof (tmp_type), "%s-%s",
419 n_type, n_type_instance);
421 sstrncpy (tmp_type, n_type, sizeof (tmp_type));
424 ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
425 prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
427 ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
428 prefix, n_host, postfix, tmp_plugin, tmp_type);
433 static int wg_send_message (const char* key, const char* value,
434 cdtime_t time, struct wg_callback *cb)
440 message_len = (size_t) ssnprintf (message, sizeof (message),
444 CDTIME_T_TO_DOUBLE(time));
445 if (message_len >= sizeof (message)) {
446 ERROR ("write_graphite plugin: message buffer too small: "
447 "Need %zu bytes.", message_len + 1);
452 pthread_mutex_lock (&cb->send_lock);
456 status = wg_callback_init (cb);
459 ERROR ("write_graphite plugin: wg_callback_init failed.");
460 pthread_mutex_unlock (&cb->send_lock);
465 if (message_len >= cb->send_buf_free)
467 status = wg_flush_nolock (/* timeout = */ 0, cb);
470 pthread_mutex_unlock (&cb->send_lock);
474 assert (message_len < cb->send_buf_free);
476 /* `message_len + 1' because `message_len' does not include the
477 * trailing null byte. Neither does `send_buffer_fill'. */
478 memcpy (cb->send_buf + cb->send_buf_fill,
479 message, message_len + 1);
480 cb->send_buf_fill += message_len;
481 cb->send_buf_free -= message_len;
483 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
486 cb->send_buf_fill, sizeof (cb->send_buf),
487 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
490 /* Check if we have enough space for this message. */
491 pthread_mutex_unlock (&cb->send_lock);
496 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
497 struct wg_callback *cb)
499 char key[10*DATA_MAX_NAME_LEN];
504 if (0 != strcmp (ds->type, vl->type))
506 ERROR ("write_graphite plugin: DS type does not match "
513 for (i = 0; i < ds->ds_num; i++)
515 /* Copy the identifier to `key' and escape it. */
516 status = wg_format_name (key, sizeof (key), vl, cb, ds->ds[i].name);
519 ERROR ("write_graphite plugin: error with format_name");
523 escape_string (key, sizeof (key));
524 /* Convert the values to an ASCII representation and put that
526 status = wg_format_values (values, sizeof (values), i, ds, vl,
530 ERROR ("write_graphite plugin: error with "
535 /* Send the message to graphite */
536 status = wg_send_message (key, values, vl->time, cb);
539 ERROR ("write_graphite plugin: error with "
547 /* Copy the identifier to `key' and escape it. */
548 status = wg_format_name (key, sizeof (key), vl, cb,
549 cb->always_append_ds ? ds->ds[0].name : NULL);
552 ERROR ("write_graphite plugin: error with format_name");
556 escape_string (key, sizeof (key));
557 /* Convert the values to an ASCII representation and put that into
559 status = wg_format_values (values, sizeof (values), 0, ds, vl,
563 ERROR ("write_graphite plugin: error with "
568 /* Send the message to graphite */
569 status = wg_send_message (key, values, vl->time, cb);
572 ERROR ("write_graphite plugin: error with "
581 static int wg_write (const data_set_t *ds, const value_list_t *vl,
582 user_data_t *user_data)
584 struct wg_callback *cb;
587 if (user_data == NULL)
590 cb = user_data->data;
592 status = wg_write_messages (ds, vl, cb);
597 static int config_set_char (char *dest,
600 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
602 WARNING ("write_graphite plugin: The `%s' config option "
603 "needs exactly one string argument.", ci->key);
607 *dest = ci->values[0].value.string[0];
612 static int wg_config_carbon (oconfig_item_t *ci)
614 struct wg_callback *cb;
615 user_data_t user_data;
618 cb = malloc (sizeof (*cb));
621 ERROR ("write_graphite plugin: malloc failed.");
624 memset (cb, 0, sizeof (*cb));
630 cb->escape_char = WG_DEFAULT_ESCAPE;
632 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
634 for (i = 0; i < ci->children_num; i++)
636 oconfig_item_t *child = ci->children + i;
638 if (strcasecmp ("Host", child->key) == 0)
639 cf_util_get_string (child, &cb->node);
640 else if (strcasecmp ("Port", child->key) == 0)
641 cf_util_get_service (child, &cb->service);
642 else if (strcasecmp ("Prefix", child->key) == 0)
643 cf_util_get_string (child, &cb->prefix);
644 else if (strcasecmp ("Postfix", child->key) == 0)
645 cf_util_get_string (child, &cb->postfix);
646 else if (strcasecmp ("StoreRates", child->key) == 0)
647 cf_util_get_boolean (child, &cb->store_rates);
648 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
649 cf_util_get_boolean (child, &cb->always_append_ds);
650 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
651 config_set_char (&cb->escape_char, child);
654 ERROR ("write_graphite plugin: Invalid configuration "
655 "option: %s.", child->key);
659 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
660 cb->node ? cb->node : WG_DEFAULT_NODE,
661 cb->service ? cb->service : WG_DEFAULT_SERVICE);
663 memset (&user_data, 0, sizeof (user_data));
665 user_data.free_func = NULL;
666 plugin_register_flush ("write_graphite", wg_flush, &user_data);
668 user_data.free_func = wg_callback_free;
669 plugin_register_write ("write_graphite", wg_write, &user_data);
674 static int wg_config (oconfig_item_t *ci)
678 for (i = 0; i < ci->children_num; i++)
680 oconfig_item_t *child = ci->children + i;
682 if (strcasecmp ("Carbon", child->key) == 0)
683 wg_config_carbon (child);
686 ERROR ("write_graphite plugin: Invalid configuration "
687 "option: %s.", child->key);
694 void module_register (void)
696 plugin_register_complex_config ("write_graphite", wg_config);
699 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */