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_FORMAT_NAME
58 #define WG_FORMAT_NAME(ret, ret_len, vl, cb, ds_name) \
59 wg_format_name (ret, ret_len, (vl)->host, \
60 (vl)->plugin, (vl)->plugin_instance, \
61 (vl)->type, (vl)->type_instance, \
62 (cb)->prefix, (cb)->postfix, \
63 ds_name, (cb)->escape_char)
66 #ifndef WG_DEFAULT_NODE
67 # define WG_DEFAULT_NODE "localhost"
70 #ifndef WG_DEFAULT_SERVICE
71 # define WG_DEFAULT_SERVICE "2003"
74 #ifndef WG_DEFAULT_ESCAPE
75 # define WG_DEFAULT_ESCAPE '_'
78 #ifndef WG_SEND_BUF_SIZE
79 # define WG_SEND_BUF_SIZE 4096
97 char send_buf[WG_SEND_BUF_SIZE];
100 cdtime_t send_buf_init_time;
102 pthread_mutex_t send_lock;
109 static void wg_reset_buffer (struct wg_callback *cb)
111 memset (cb->send_buf, 0, sizeof (cb->send_buf));
112 cb->send_buf_free = sizeof (cb->send_buf);
113 cb->send_buf_fill = 0;
114 cb->send_buf_init_time = cdtime ();
117 static int wg_send_buffer (struct wg_callback *cb)
121 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
124 ERROR ("write_graphite plugin: send failed with "
137 /* NOTE: You must hold cb->send_lock when calling this function! */
138 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
142 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
143 "send_buf_fill = %zu;",
147 /* timeout == 0 => flush unconditionally */
153 if ((cb->send_buf_init_time + timeout) > now)
157 if (cb->send_buf_fill <= 0)
159 cb->send_buf_init_time = cdtime ();
163 status = wg_send_buffer (cb);
164 wg_reset_buffer (cb);
169 static int wg_callback_init (struct wg_callback *cb)
171 struct addrinfo ai_hints;
172 struct addrinfo *ai_list;
173 struct addrinfo *ai_ptr;
176 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
177 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
182 memset (&ai_hints, 0, sizeof (ai_hints));
184 ai_hints.ai_flags |= AI_ADDRCONFIG;
186 ai_hints.ai_family = AF_UNSPEC;
187 ai_hints.ai_socktype = SOCK_STREAM;
191 status = getaddrinfo (node, service, &ai_hints, &ai_list);
194 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
195 node, service, gai_strerror (status));
199 assert (ai_list != NULL);
200 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
202 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
203 ai_ptr->ai_protocol);
207 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
218 freeaddrinfo (ai_list);
223 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
224 "The last error was: %s", node, service,
225 sstrerror (errno, errbuf, sizeof (errbuf)));
230 wg_reset_buffer (cb);
235 static void wg_callback_free (void *data)
237 struct wg_callback *cb;
244 pthread_mutex_lock (&cb->send_lock);
246 wg_flush_nolock (/* timeout = */ 0, cb);
256 pthread_mutex_destroy (&cb->send_lock);
261 static int wg_flush (cdtime_t timeout,
262 const char *identifier __attribute__((unused)),
263 user_data_t *user_data)
265 struct wg_callback *cb;
268 if (user_data == NULL)
271 cb = user_data->data;
273 pthread_mutex_lock (&cb->send_lock);
277 status = wg_callback_init (cb);
280 ERROR ("write_graphite plugin: wg_callback_init failed.");
281 pthread_mutex_unlock (&cb->send_lock);
286 status = wg_flush_nolock (timeout, cb);
287 pthread_mutex_unlock (&cb->send_lock);
292 static int wg_format_values (char *ret, size_t ret_len,
293 int ds_num, const data_set_t *ds, const value_list_t *vl,
298 gauge_t *rates = NULL;
300 assert (0 == strcmp (ds->type, vl->type));
302 memset (ret, 0, ret_len);
304 #define BUFFER_ADD(...) do { \
305 status = ssnprintf (ret + offset, ret_len - offset, \
312 else if (((size_t) status) >= (ret_len - offset)) \
318 offset += ((size_t) status); \
321 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
322 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
323 else if (store_rates)
326 rates = uc_get_rate (ds, vl);
329 WARNING ("format_values: "
330 "uc_get_rate failed.");
333 BUFFER_ADD ("%g", rates[ds_num]);
335 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
336 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
337 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
338 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
339 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
340 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
343 ERROR ("format_values plugin: Unknown data source type: %i",
344 ds->ds[ds_num].type);
355 static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
360 memset (dst, 0, dst_len);
365 for (i = 0; i < dst_len; i++)
368 || isspace ((int) src[i])
369 || iscntrl ((int) src[i]))
370 dst[i] = escape_char;
379 static int wg_format_name (char *ret, int ret_len,
380 const char *hostname,
381 const char *plugin, const char *plugin_instance,
382 const char *type, const char *type_instance,
383 const char *prefix, const char *postfix,
384 const char *ds_name, char escape_char)
386 char n_hostname[DATA_MAX_NAME_LEN];
387 char n_plugin[DATA_MAX_NAME_LEN];
388 char n_plugin_instance[DATA_MAX_NAME_LEN];
389 char n_type[DATA_MAX_NAME_LEN];
390 char n_type_instance[DATA_MAX_NAME_LEN];
393 assert (hostname != NULL);
394 assert (plugin != NULL);
395 assert (type != NULL);
396 assert (ds_name != NULL);
404 wg_copy_escape_part (n_hostname, hostname,
405 sizeof (n_hostname), escape_char);
406 wg_copy_escape_part (n_plugin, plugin,
407 sizeof (n_plugin), escape_char);
408 wg_copy_escape_part (n_plugin_instance, plugin_instance,
409 sizeof (n_plugin_instance), escape_char);
410 wg_copy_escape_part (n_type, type,
411 sizeof (n_type), escape_char);
412 wg_copy_escape_part (n_type_instance, type_instance,
413 sizeof (n_type_instance), escape_char);
415 if (n_plugin_instance[0] == '\0')
417 if (n_type_instance[0] == '\0')
419 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
420 prefix, n_hostname, postfix, n_plugin, n_type, ds_name);
424 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
425 prefix, n_hostname, postfix, n_plugin, n_type,
426 n_type_instance, ds_name);
431 if (n_type_instance[0] == '\0')
433 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
434 prefix, n_hostname, postfix, n_plugin,
435 n_plugin_instance, n_type, ds_name);
439 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
440 prefix, n_hostname, postfix, n_plugin,
441 n_plugin_instance, n_type, n_type_instance, ds_name);
445 if ((status < 1) || (status >= ret_len))
451 static int wg_send_message (const char* key, const char* value,
452 cdtime_t time, struct wg_callback *cb)
458 message_len = (size_t) ssnprintf (message, sizeof (message),
462 CDTIME_T_TO_DOUBLE(time));
463 if (message_len >= sizeof (message)) {
464 ERROR ("write_graphite plugin: message buffer too small: "
465 "Need %zu bytes.", message_len + 1);
470 pthread_mutex_lock (&cb->send_lock);
474 status = wg_callback_init (cb);
477 ERROR ("write_graphite plugin: wg_callback_init failed.");
478 pthread_mutex_unlock (&cb->send_lock);
483 if (message_len >= cb->send_buf_free)
485 status = wg_flush_nolock (/* timeout = */ 0, cb);
488 pthread_mutex_unlock (&cb->send_lock);
492 assert (message_len < cb->send_buf_free);
494 /* `message_len + 1' because `message_len' does not include the
495 * trailing null byte. Neither does `send_buffer_fill'. */
496 memcpy (cb->send_buf + cb->send_buf_fill,
497 message, message_len + 1);
498 cb->send_buf_fill += message_len;
499 cb->send_buf_free -= message_len;
501 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
504 cb->send_buf_fill, sizeof (cb->send_buf),
505 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
508 /* Check if we have enough space for this message. */
509 pthread_mutex_unlock (&cb->send_lock);
514 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
515 struct wg_callback *cb)
517 char key[10*DATA_MAX_NAME_LEN];
522 if (0 != strcmp (ds->type, vl->type))
524 ERROR ("write_graphite plugin: DS type does not match "
531 for (i = 0; i < ds->ds_num; i++)
533 /* Copy the identifier to `key' and escape it. */
534 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
537 ERROR ("write_graphite plugin: error with format_name");
541 escape_string (key, sizeof (key));
542 /* Convert the values to an ASCII representation and put that
544 status = wg_format_values (values, sizeof (values), i, ds, vl,
548 ERROR ("write_graphite plugin: error with "
553 /* Send the message to graphite */
554 status = wg_send_message (key, values, vl->time, cb);
557 ERROR ("write_graphite plugin: error with "
565 /* Copy the identifier to `key' and escape it. */
566 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
569 ERROR ("write_graphite plugin: error with format_name");
573 escape_string (key, sizeof (key));
574 /* Convert the values to an ASCII representation and put that into
576 status = wg_format_values (values, sizeof (values), 0, ds, vl,
580 ERROR ("write_graphite plugin: error with "
585 /* Send the message to graphite */
586 status = wg_send_message (key, values, vl->time, cb);
589 ERROR ("write_graphite plugin: error with "
598 static int wg_write (const data_set_t *ds, const value_list_t *vl,
599 user_data_t *user_data)
601 struct wg_callback *cb;
604 if (user_data == NULL)
607 cb = user_data->data;
609 status = wg_write_messages (ds, vl, cb);
614 static int config_set_char (char *dest,
617 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
619 WARNING ("write_graphite plugin: The `%s' config option "
620 "needs exactly one string argument.", ci->key);
624 *dest = ci->values[0].value.string[0];
629 static int wg_config_carbon (oconfig_item_t *ci)
631 struct wg_callback *cb;
632 user_data_t user_data;
635 cb = malloc (sizeof (*cb));
638 ERROR ("write_graphite plugin: malloc failed.");
641 memset (cb, 0, sizeof (*cb));
647 cb->escape_char = WG_DEFAULT_ESCAPE;
649 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
651 for (i = 0; i < ci->children_num; i++)
653 oconfig_item_t *child = ci->children + i;
655 if (strcasecmp ("Host", child->key) == 0)
656 cf_util_get_string (child, &cb->node);
657 else if (strcasecmp ("Port", child->key) == 0)
658 cf_util_get_service (child, &cb->service);
659 else if (strcasecmp ("Prefix", child->key) == 0)
660 cf_util_get_string (child, &cb->prefix);
661 else if (strcasecmp ("Postfix", child->key) == 0)
662 cf_util_get_string (child, &cb->postfix);
663 else if (strcasecmp ("StoreRates", child->key) == 0)
664 cf_util_get_boolean (child, &cb->store_rates);
665 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
666 config_set_char (&cb->escape_char, child);
669 ERROR ("write_graphite plugin: Invalid configuration "
670 "option: %s.", child->key);
674 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
675 cb->node ? cb->node : WG_DEFAULT_NODE,
676 cb->service ? cb->service : WG_DEFAULT_SERVICE);
678 memset (&user_data, 0, sizeof (user_data));
680 user_data.free_func = NULL;
681 plugin_register_flush ("write_graphite", wg_flush, &user_data);
683 user_data.free_func = wg_callback_free;
684 plugin_register_write ("write_graphite", wg_write, &user_data);
689 static int wg_config (oconfig_item_t *ci)
693 for (i = 0; i < ci->children_num; i++)
695 oconfig_item_t *child = ci->children + i;
697 if (strcasecmp ("Carbon", child->key) == 0)
698 wg_config_carbon (child);
701 ERROR ("write_graphite plugin: Invalid configuration "
702 "option: %s.", child->key);
709 void module_register (void)
711 plugin_register_complex_config ("write_graphite", wg_config);
714 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */