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 = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
118 ERROR ("write_graphite plugin: send failed with status %zi (%s)",
119 status, sstrerror (errno, errbuf, sizeof (errbuf)));
131 /* NOTE: You must hold cb->send_lock when calling this function! */
132 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
136 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
137 "send_buf_fill = %zu;",
141 /* timeout == 0 => flush unconditionally */
147 if ((cb->send_buf_init_time + timeout) > now)
151 if (cb->send_buf_fill <= 0)
153 cb->send_buf_init_time = cdtime ();
157 status = wg_send_buffer (cb);
158 wg_reset_buffer (cb);
163 static int wg_callback_init (struct wg_callback *cb)
165 struct addrinfo ai_hints;
166 struct addrinfo *ai_list;
167 struct addrinfo *ai_ptr;
170 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
171 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
176 memset (&ai_hints, 0, sizeof (ai_hints));
178 ai_hints.ai_flags |= AI_ADDRCONFIG;
180 ai_hints.ai_family = AF_UNSPEC;
181 ai_hints.ai_socktype = SOCK_STREAM;
185 status = getaddrinfo (node, service, &ai_hints, &ai_list);
188 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
189 node, service, gai_strerror (status));
193 assert (ai_list != NULL);
194 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
196 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
197 ai_ptr->ai_protocol);
201 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
212 freeaddrinfo (ai_list);
217 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
218 "The last error was: %s", node, service,
219 sstrerror (errno, errbuf, sizeof (errbuf)));
224 wg_reset_buffer (cb);
229 static void wg_callback_free (void *data)
231 struct wg_callback *cb;
238 pthread_mutex_lock (&cb->send_lock);
240 wg_flush_nolock (/* timeout = */ 0, cb);
250 pthread_mutex_destroy (&cb->send_lock);
255 static int wg_flush (cdtime_t timeout,
256 const char *identifier __attribute__((unused)),
257 user_data_t *user_data)
259 struct wg_callback *cb;
262 if (user_data == NULL)
265 cb = user_data->data;
267 pthread_mutex_lock (&cb->send_lock);
271 status = wg_callback_init (cb);
274 ERROR ("write_graphite plugin: wg_callback_init failed.");
275 pthread_mutex_unlock (&cb->send_lock);
280 status = wg_flush_nolock (timeout, cb);
281 pthread_mutex_unlock (&cb->send_lock);
286 static int wg_format_values (char *ret, size_t ret_len,
287 int ds_num, const data_set_t *ds, const value_list_t *vl,
292 gauge_t *rates = NULL;
294 assert (0 == strcmp (ds->type, vl->type));
296 memset (ret, 0, ret_len);
298 #define BUFFER_ADD(...) do { \
299 status = ssnprintf (ret + offset, ret_len - offset, \
306 else if (((size_t) status) >= (ret_len - offset)) \
312 offset += ((size_t) status); \
315 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
316 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
317 else if (store_rates)
320 rates = uc_get_rate (ds, vl);
323 WARNING ("format_values: "
324 "uc_get_rate failed.");
327 BUFFER_ADD ("%g", rates[ds_num]);
329 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
330 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
331 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
332 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
333 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
334 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
337 ERROR ("format_values plugin: Unknown data source type: %i",
338 ds->ds[ds_num].type);
349 static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
354 memset (dst, 0, dst_len);
359 for (i = 0; i < dst_len; i++)
368 || isspace ((int) src[i])
369 || iscntrl ((int) src[i]))
370 dst[i] = escape_char;
376 static int wg_format_name (char *ret, int ret_len,
377 const value_list_t *vl,
378 const struct wg_callback *cb,
381 char n_host[DATA_MAX_NAME_LEN];
382 char n_plugin[DATA_MAX_NAME_LEN];
383 char n_plugin_instance[DATA_MAX_NAME_LEN];
384 char n_type[DATA_MAX_NAME_LEN];
385 char n_type_instance[DATA_MAX_NAME_LEN];
390 char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
391 char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
397 postfix = cb->postfix;
401 wg_copy_escape_part (n_host, vl->host,
402 sizeof (n_host), cb->escape_char);
403 wg_copy_escape_part (n_plugin, vl->plugin,
404 sizeof (n_plugin), cb->escape_char);
405 wg_copy_escape_part (n_plugin_instance, vl->plugin_instance,
406 sizeof (n_plugin_instance), cb->escape_char);
407 wg_copy_escape_part (n_type, vl->type,
408 sizeof (n_type), cb->escape_char);
409 wg_copy_escape_part (n_type_instance, vl->type_instance,
410 sizeof (n_type_instance), cb->escape_char);
412 if (n_plugin_instance[0] != '\0')
413 ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s-%s",
414 n_plugin, n_plugin_instance);
416 sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
418 if (n_type_instance[0] != '\0')
419 ssnprintf (tmp_type, sizeof (tmp_type), "%s-%s",
420 n_type, n_type_instance);
422 sstrncpy (tmp_type, n_type, sizeof (tmp_type));
425 ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
426 prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
428 ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
429 prefix, n_host, postfix, tmp_plugin, tmp_type);
434 static int wg_send_message (const char* key, const char* value,
435 cdtime_t time, struct wg_callback *cb)
441 message_len = (size_t) ssnprintf (message, sizeof (message),
445 (unsigned int) CDTIME_T_TO_TIME_T (time));
446 if (message_len >= sizeof (message)) {
447 ERROR ("write_graphite plugin: message buffer too small: "
448 "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);
475 /* Assert that we have enough space for this message. */
476 assert (message_len < cb->send_buf_free);
478 /* `message_len + 1' because `message_len' does not include the
479 * trailing null byte. Neither does `send_buffer_fill'. */
480 memcpy (cb->send_buf + cb->send_buf_fill,
481 message, message_len + 1);
482 cb->send_buf_fill += message_len;
483 cb->send_buf_free -= message_len;
485 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
488 cb->send_buf_fill, sizeof (cb->send_buf),
489 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
492 pthread_mutex_unlock (&cb->send_lock);
497 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
498 struct wg_callback *cb)
500 char key[10*DATA_MAX_NAME_LEN];
505 if (0 != strcmp (ds->type, vl->type))
507 ERROR ("write_graphite plugin: DS type does not match "
512 for (i = 0; i < ds->ds_num; i++)
514 const char *ds_name = NULL;
516 if (cb->always_append_ds || (ds->ds_num > 1))
517 ds_name = ds->ds[i].name;
519 /* Copy the identifier to `key' and escape it. */
520 status = wg_format_name (key, sizeof (key), vl, cb, ds_name);
523 ERROR ("write_graphite plugin: error with format_name");
527 escape_string (key, sizeof (key));
528 /* Convert the values to an ASCII representation and put that into
530 status = wg_format_values (values, sizeof (values), i, ds, vl,
534 ERROR ("write_graphite plugin: error with "
539 /* Send the message to graphite */
540 status = wg_send_message (key, values, vl->time, cb);
543 ERROR ("write_graphite plugin: error with "
552 static int wg_write (const data_set_t *ds, const value_list_t *vl,
553 user_data_t *user_data)
555 struct wg_callback *cb;
558 if (user_data == NULL)
561 cb = user_data->data;
563 status = wg_write_messages (ds, vl, cb);
568 static int config_set_char (char *dest,
574 memset (buffer, 0, sizeof (buffer));
576 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
582 ERROR ("write_graphite plugin: Cannot use an empty string for the "
583 "\"EscapeCharacter\" option.");
589 WARNING ("write_graphite plugin: Only the first character of the "
590 "\"EscapeCharacter\" option ('%c') will be used.",
599 static int wg_config_carbon (oconfig_item_t *ci)
601 struct wg_callback *cb;
602 user_data_t user_data;
605 cb = malloc (sizeof (*cb));
608 ERROR ("write_graphite plugin: malloc failed.");
611 memset (cb, 0, sizeof (*cb));
617 cb->escape_char = WG_DEFAULT_ESCAPE;
619 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
621 for (i = 0; i < ci->children_num; i++)
623 oconfig_item_t *child = ci->children + i;
625 if (strcasecmp ("Host", child->key) == 0)
626 cf_util_get_string (child, &cb->node);
627 else if (strcasecmp ("Port", child->key) == 0)
628 cf_util_get_service (child, &cb->service);
629 else if (strcasecmp ("Prefix", child->key) == 0)
630 cf_util_get_string (child, &cb->prefix);
631 else if (strcasecmp ("Postfix", child->key) == 0)
632 cf_util_get_string (child, &cb->postfix);
633 else if (strcasecmp ("StoreRates", child->key) == 0)
634 cf_util_get_boolean (child, &cb->store_rates);
635 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
636 cf_util_get_boolean (child, &cb->always_append_ds);
637 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
638 config_set_char (&cb->escape_char, child);
641 ERROR ("write_graphite plugin: Invalid configuration "
642 "option: %s.", child->key);
646 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
647 cb->node ? cb->node : WG_DEFAULT_NODE,
648 cb->service ? cb->service : WG_DEFAULT_SERVICE);
650 memset (&user_data, 0, sizeof (user_data));
652 user_data.free_func = NULL;
653 plugin_register_flush ("write_graphite", wg_flush, &user_data);
655 user_data.free_func = wg_callback_free;
656 plugin_register_write ("write_graphite", wg_write, &user_data);
661 static int wg_config (oconfig_item_t *ci)
665 for (i = 0; i < ci->children_num; i++)
667 oconfig_item_t *child = ci->children + i;
669 if (strcasecmp ("Carbon", child->key) == 0)
670 wg_config_carbon (child);
673 ERROR ("write_graphite plugin: Invalid configuration "
674 "option: %s.", child->key);
681 void module_register (void)
683 plugin_register_complex_config ("write_graphite", wg_config);
686 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */