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 #ifndef WG_SEND_BUF_SIZE
70 # define WG_SEND_BUF_SIZE 4096
87 _Bool always_append_ds;
89 char send_buf[WG_SEND_BUF_SIZE];
92 cdtime_t send_buf_init_time;
94 pthread_mutex_t send_lock;
101 static void wg_reset_buffer (struct wg_callback *cb)
103 memset (cb->send_buf, 0, sizeof (cb->send_buf));
104 cb->send_buf_free = sizeof (cb->send_buf);
105 cb->send_buf_fill = 0;
106 cb->send_buf_init_time = cdtime ();
109 static int wg_send_buffer (struct wg_callback *cb)
113 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
116 ERROR ("write_graphite plugin: send failed with "
129 /* NOTE: You must hold cb->send_lock when calling this function! */
130 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
134 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
135 "send_buf_fill = %zu;",
139 /* timeout == 0 => flush unconditionally */
145 if ((cb->send_buf_init_time + timeout) > now)
149 if (cb->send_buf_fill <= 0)
151 cb->send_buf_init_time = cdtime ();
155 status = wg_send_buffer (cb);
156 wg_reset_buffer (cb);
161 static int wg_callback_init (struct wg_callback *cb)
163 struct addrinfo ai_hints;
164 struct addrinfo *ai_list;
165 struct addrinfo *ai_ptr;
168 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
169 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
174 memset (&ai_hints, 0, sizeof (ai_hints));
176 ai_hints.ai_flags |= AI_ADDRCONFIG;
178 ai_hints.ai_family = AF_UNSPEC;
179 ai_hints.ai_socktype = SOCK_STREAM;
183 status = getaddrinfo (node, service, &ai_hints, &ai_list);
186 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
187 node, service, gai_strerror (status));
191 assert (ai_list != NULL);
192 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
194 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
195 ai_ptr->ai_protocol);
199 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
210 freeaddrinfo (ai_list);
215 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
216 "The last error was: %s", node, service,
217 sstrerror (errno, errbuf, sizeof (errbuf)));
222 wg_reset_buffer (cb);
227 static void wg_callback_free (void *data)
229 struct wg_callback *cb;
236 pthread_mutex_lock (&cb->send_lock);
238 wg_flush_nolock (/* timeout = */ 0, cb);
248 pthread_mutex_destroy (&cb->send_lock);
253 static int wg_flush (cdtime_t timeout,
254 const char *identifier __attribute__((unused)),
255 user_data_t *user_data)
257 struct wg_callback *cb;
260 if (user_data == NULL)
263 cb = user_data->data;
265 pthread_mutex_lock (&cb->send_lock);
269 status = wg_callback_init (cb);
272 ERROR ("write_graphite plugin: wg_callback_init failed.");
273 pthread_mutex_unlock (&cb->send_lock);
278 status = wg_flush_nolock (timeout, cb);
279 pthread_mutex_unlock (&cb->send_lock);
284 static int wg_format_values (char *ret, size_t ret_len,
285 int ds_num, const data_set_t *ds, const value_list_t *vl,
290 gauge_t *rates = NULL;
292 assert (0 == strcmp (ds->type, vl->type));
294 memset (ret, 0, ret_len);
296 #define BUFFER_ADD(...) do { \
297 status = ssnprintf (ret + offset, ret_len - offset, \
304 else if (((size_t) status) >= (ret_len - offset)) \
310 offset += ((size_t) status); \
313 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
314 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
315 else if (store_rates)
318 rates = uc_get_rate (ds, vl);
321 WARNING ("format_values: "
322 "uc_get_rate failed.");
325 BUFFER_ADD ("%g", rates[ds_num]);
327 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
328 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
329 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
330 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
331 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
332 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
335 ERROR ("format_values plugin: Unknown data source type: %i",
336 ds->ds[ds_num].type);
347 static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
352 memset (dst, 0, dst_len);
357 for (i = 0; i < dst_len; i++)
366 || isspace ((int) src[i])
367 || iscntrl ((int) src[i]))
368 dst[i] = escape_char;
374 static int wg_format_name (char *ret, int ret_len,
375 const value_list_t *vl,
376 const struct wg_callback *cb,
379 char n_host[DATA_MAX_NAME_LEN];
380 char n_plugin[DATA_MAX_NAME_LEN];
381 char n_plugin_instance[DATA_MAX_NAME_LEN];
382 char n_type[DATA_MAX_NAME_LEN];
383 char n_type_instance[DATA_MAX_NAME_LEN];
388 char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
389 char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
395 postfix = cb->postfix;
399 wg_copy_escape_part (n_host, vl->host,
400 sizeof (n_host), cb->escape_char);
401 wg_copy_escape_part (n_plugin, vl->plugin,
402 sizeof (n_plugin), cb->escape_char);
403 wg_copy_escape_part (n_plugin_instance, vl->plugin_instance,
404 sizeof (n_plugin_instance), cb->escape_char);
405 wg_copy_escape_part (n_type, vl->type,
406 sizeof (n_type), cb->escape_char);
407 wg_copy_escape_part (n_type_instance, vl->type_instance,
408 sizeof (n_type_instance), cb->escape_char);
410 if (n_plugin_instance[0] != '\0')
411 ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s-%s",
412 n_plugin, n_plugin_instance);
414 sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
416 if (n_type_instance[0] != '\0')
417 ssnprintf (tmp_type, sizeof (tmp_type), "%s-%s",
418 n_type, n_type_instance);
420 sstrncpy (tmp_type, n_type, sizeof (tmp_type));
423 ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
424 prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
426 ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
427 prefix, n_host, postfix, tmp_plugin, tmp_type);
432 static int wg_send_message (const char* key, const char* value,
433 cdtime_t time, struct wg_callback *cb)
439 message_len = (size_t) ssnprintf (message, sizeof (message),
443 CDTIME_T_TO_DOUBLE(time));
444 if (message_len >= sizeof (message)) {
445 ERROR ("write_graphite plugin: message buffer too small: "
446 "Need %zu bytes.", message_len + 1);
451 pthread_mutex_lock (&cb->send_lock);
455 status = wg_callback_init (cb);
458 ERROR ("write_graphite plugin: wg_callback_init failed.");
459 pthread_mutex_unlock (&cb->send_lock);
464 if (message_len >= cb->send_buf_free)
466 status = wg_flush_nolock (/* timeout = */ 0, cb);
469 pthread_mutex_unlock (&cb->send_lock);
473 assert (message_len < cb->send_buf_free);
475 /* `message_len + 1' because `message_len' does not include the
476 * trailing null byte. Neither does `send_buffer_fill'. */
477 memcpy (cb->send_buf + cb->send_buf_fill,
478 message, message_len + 1);
479 cb->send_buf_fill += message_len;
480 cb->send_buf_free -= message_len;
482 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
485 cb->send_buf_fill, sizeof (cb->send_buf),
486 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
489 /* Check if we have enough space for this message. */
490 pthread_mutex_unlock (&cb->send_lock);
495 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
496 struct wg_callback *cb)
498 char key[10*DATA_MAX_NAME_LEN];
503 if (0 != strcmp (ds->type, vl->type))
505 ERROR ("write_graphite plugin: DS type does not match "
512 for (i = 0; i < ds->ds_num; i++)
514 /* Copy the identifier to `key' and escape it. */
515 status = wg_format_name (key, sizeof (key), vl, cb, ds->ds[i].name);
518 ERROR ("write_graphite plugin: error with format_name");
522 escape_string (key, sizeof (key));
523 /* Convert the values to an ASCII representation and put that
525 status = wg_format_values (values, sizeof (values), i, ds, vl,
529 ERROR ("write_graphite plugin: error with "
534 /* Send the message to graphite */
535 status = wg_send_message (key, values, vl->time, cb);
538 ERROR ("write_graphite plugin: error with "
546 /* Copy the identifier to `key' and escape it. */
547 status = wg_format_name (key, sizeof (key), vl, cb,
548 cb->always_append_ds ? ds->ds[0].name : NULL);
551 ERROR ("write_graphite plugin: error with format_name");
555 escape_string (key, sizeof (key));
556 /* Convert the values to an ASCII representation and put that into
558 status = wg_format_values (values, sizeof (values), 0, ds, vl,
562 ERROR ("write_graphite plugin: error with "
567 /* Send the message to graphite */
568 status = wg_send_message (key, values, vl->time, cb);
571 ERROR ("write_graphite plugin: error with "
580 static int wg_write (const data_set_t *ds, const value_list_t *vl,
581 user_data_t *user_data)
583 struct wg_callback *cb;
586 if (user_data == NULL)
589 cb = user_data->data;
591 status = wg_write_messages (ds, vl, cb);
596 static int config_set_char (char *dest,
599 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
601 WARNING ("write_graphite plugin: The `%s' config option "
602 "needs exactly one string argument.", ci->key);
606 *dest = ci->values[0].value.string[0];
611 static int wg_config_carbon (oconfig_item_t *ci)
613 struct wg_callback *cb;
614 user_data_t user_data;
617 cb = malloc (sizeof (*cb));
620 ERROR ("write_graphite plugin: malloc failed.");
623 memset (cb, 0, sizeof (*cb));
629 cb->escape_char = WG_DEFAULT_ESCAPE;
631 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
633 for (i = 0; i < ci->children_num; i++)
635 oconfig_item_t *child = ci->children + i;
637 if (strcasecmp ("Host", child->key) == 0)
638 cf_util_get_string (child, &cb->node);
639 else if (strcasecmp ("Port", child->key) == 0)
640 cf_util_get_service (child, &cb->service);
641 else if (strcasecmp ("Prefix", child->key) == 0)
642 cf_util_get_string (child, &cb->prefix);
643 else if (strcasecmp ("Postfix", child->key) == 0)
644 cf_util_get_string (child, &cb->postfix);
645 else if (strcasecmp ("StoreRates", child->key) == 0)
646 cf_util_get_boolean (child, &cb->store_rates);
647 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
648 cf_util_get_boolean (child, &cb->always_append_ds);
649 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
650 config_set_char (&cb->escape_char, child);
653 ERROR ("write_graphite plugin: Invalid configuration "
654 "option: %s.", child->key);
658 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
659 cb->node ? cb->node : WG_DEFAULT_NODE,
660 cb->service ? cb->service : WG_DEFAULT_SERVICE);
662 memset (&user_data, 0, sizeof (user_data));
664 user_data.free_func = NULL;
665 plugin_register_flush ("write_graphite", wg_flush, &user_data);
667 user_data.free_func = wg_callback_free;
668 plugin_register_write ("write_graphite", wg_write, &user_data);
673 static int wg_config (oconfig_item_t *ci)
677 for (i = 0; i < ci->children_num; i++)
679 oconfig_item_t *child = ci->children + i;
681 if (strcasecmp ("Carbon", child->key) == 0)
682 wg_config_carbon (child);
685 ERROR ("write_graphite plugin: Invalid configuration "
686 "option: %s.", child->key);
693 void module_register (void)
695 plugin_register_complex_config ("write_graphite", wg_config);
698 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */