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 separate_instances;
89 _Bool always_append_ds;
91 char send_buf[WG_SEND_BUF_SIZE];
94 cdtime_t send_buf_init_time;
96 pthread_mutex_t send_lock;
103 static void wg_reset_buffer (struct wg_callback *cb)
105 memset (cb->send_buf, 0, sizeof (cb->send_buf));
106 cb->send_buf_free = sizeof (cb->send_buf);
107 cb->send_buf_fill = 0;
108 cb->send_buf_init_time = cdtime ();
111 static int wg_send_buffer (struct wg_callback *cb)
115 status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
119 ERROR ("write_graphite plugin: send failed with status %zi (%s)",
120 status, sstrerror (errno, errbuf, sizeof (errbuf)));
132 /* NOTE: You must hold cb->send_lock when calling this function! */
133 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
137 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
138 "send_buf_fill = %zu;",
142 /* timeout == 0 => flush unconditionally */
148 if ((cb->send_buf_init_time + timeout) > now)
152 if (cb->send_buf_fill <= 0)
154 cb->send_buf_init_time = cdtime ();
158 status = wg_send_buffer (cb);
159 wg_reset_buffer (cb);
164 static int wg_callback_init (struct wg_callback *cb)
166 struct addrinfo ai_hints;
167 struct addrinfo *ai_list;
168 struct addrinfo *ai_ptr;
171 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
172 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
177 memset (&ai_hints, 0, sizeof (ai_hints));
179 ai_hints.ai_flags |= AI_ADDRCONFIG;
181 ai_hints.ai_family = AF_UNSPEC;
182 ai_hints.ai_socktype = SOCK_STREAM;
186 status = getaddrinfo (node, service, &ai_hints, &ai_list);
189 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
190 node, service, gai_strerror (status));
194 assert (ai_list != NULL);
195 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
197 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
198 ai_ptr->ai_protocol);
202 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
213 freeaddrinfo (ai_list);
218 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
219 "The last error was: %s", node, service,
220 sstrerror (errno, errbuf, sizeof (errbuf)));
225 wg_reset_buffer (cb);
230 static void wg_callback_free (void *data)
232 struct wg_callback *cb;
239 pthread_mutex_lock (&cb->send_lock);
241 wg_flush_nolock (/* timeout = */ 0, cb);
251 pthread_mutex_destroy (&cb->send_lock);
256 static int wg_flush (cdtime_t timeout,
257 const char *identifier __attribute__((unused)),
258 user_data_t *user_data)
260 struct wg_callback *cb;
263 if (user_data == NULL)
266 cb = user_data->data;
268 pthread_mutex_lock (&cb->send_lock);
272 status = wg_callback_init (cb);
275 ERROR ("write_graphite plugin: wg_callback_init failed.");
276 pthread_mutex_unlock (&cb->send_lock);
281 status = wg_flush_nolock (timeout, cb);
282 pthread_mutex_unlock (&cb->send_lock);
287 static int wg_format_values (char *ret, size_t ret_len,
288 int ds_num, const data_set_t *ds, const value_list_t *vl,
293 gauge_t *rates = NULL;
295 assert (0 == strcmp (ds->type, vl->type));
297 memset (ret, 0, ret_len);
299 #define BUFFER_ADD(...) do { \
300 status = ssnprintf (ret + offset, ret_len - offset, \
307 else if (((size_t) status) >= (ret_len - offset)) \
313 offset += ((size_t) status); \
316 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
317 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
318 else if (store_rates)
321 rates = uc_get_rate (ds, vl);
324 WARNING ("format_values: "
325 "uc_get_rate failed.");
328 BUFFER_ADD ("%g", rates[ds_num]);
330 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
331 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
332 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
333 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
334 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
335 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
338 ERROR ("format_values plugin: Unknown data source type: %i",
339 ds->ds[ds_num].type);
350 static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
355 memset (dst, 0, dst_len);
360 for (i = 0; i < dst_len; i++)
369 || isspace ((int) src[i])
370 || iscntrl ((int) src[i]))
371 dst[i] = escape_char;
377 static int wg_format_name (char *ret, int ret_len,
378 const value_list_t *vl,
379 const struct wg_callback *cb,
382 char n_host[DATA_MAX_NAME_LEN];
383 char n_plugin[DATA_MAX_NAME_LEN];
384 char n_plugin_instance[DATA_MAX_NAME_LEN];
385 char n_type[DATA_MAX_NAME_LEN];
386 char n_type_instance[DATA_MAX_NAME_LEN];
391 char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
392 char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
398 postfix = cb->postfix;
402 wg_copy_escape_part (n_host, vl->host,
403 sizeof (n_host), cb->escape_char);
404 wg_copy_escape_part (n_plugin, vl->plugin,
405 sizeof (n_plugin), cb->escape_char);
406 wg_copy_escape_part (n_plugin_instance, vl->plugin_instance,
407 sizeof (n_plugin_instance), cb->escape_char);
408 wg_copy_escape_part (n_type, vl->type,
409 sizeof (n_type), cb->escape_char);
410 wg_copy_escape_part (n_type_instance, vl->type_instance,
411 sizeof (n_type_instance), cb->escape_char);
413 if (n_plugin_instance[0] != '\0')
414 ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s%c%s",
416 cb->separate_instances ? '.' : '-',
419 sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
421 if (n_type_instance[0] != '\0')
422 ssnprintf (tmp_type, sizeof (tmp_type), "%s%c%s",
424 cb->separate_instances ? '.' : '-',
427 sstrncpy (tmp_type, n_type, sizeof (tmp_type));
430 ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
431 prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
433 ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
434 prefix, n_host, postfix, tmp_plugin, tmp_type);
439 static int wg_send_message (const char* key, const char* value,
440 cdtime_t time, struct wg_callback *cb)
446 message_len = (size_t) ssnprintf (message, sizeof (message),
450 (unsigned int) CDTIME_T_TO_TIME_T (time));
451 if (message_len >= sizeof (message)) {
452 ERROR ("write_graphite plugin: message buffer too small: "
453 "Need %zu bytes.", message_len + 1);
457 pthread_mutex_lock (&cb->send_lock);
461 status = wg_callback_init (cb);
464 ERROR ("write_graphite plugin: wg_callback_init failed.");
465 pthread_mutex_unlock (&cb->send_lock);
470 if (message_len >= cb->send_buf_free)
472 status = wg_flush_nolock (/* timeout = */ 0, cb);
475 pthread_mutex_unlock (&cb->send_lock);
480 /* Assert that we have enough space for this message. */
481 assert (message_len < cb->send_buf_free);
483 /* `message_len + 1' because `message_len' does not include the
484 * trailing null byte. Neither does `send_buffer_fill'. */
485 memcpy (cb->send_buf + cb->send_buf_fill,
486 message, message_len + 1);
487 cb->send_buf_fill += message_len;
488 cb->send_buf_free -= message_len;
490 DEBUG ("write_graphite plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
493 cb->send_buf_fill, sizeof (cb->send_buf),
494 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
497 pthread_mutex_unlock (&cb->send_lock);
502 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
503 struct wg_callback *cb)
505 char key[10*DATA_MAX_NAME_LEN];
510 if (0 != strcmp (ds->type, vl->type))
512 ERROR ("write_graphite plugin: DS type does not match "
517 for (i = 0; i < ds->ds_num; i++)
519 const char *ds_name = NULL;
521 if (cb->always_append_ds || (ds->ds_num > 1))
522 ds_name = ds->ds[i].name;
524 /* Copy the identifier to `key' and escape it. */
525 status = wg_format_name (key, sizeof (key), vl, cb, ds_name);
528 ERROR ("write_graphite plugin: error with format_name");
532 escape_string (key, sizeof (key));
533 /* Convert the values to an ASCII representation and put that into
535 status = wg_format_values (values, sizeof (values), i, ds, vl,
539 ERROR ("write_graphite plugin: error with "
544 /* Send the message to graphite */
545 status = wg_send_message (key, values, vl->time, cb);
548 ERROR ("write_graphite plugin: error with "
557 static int wg_write (const data_set_t *ds, const value_list_t *vl,
558 user_data_t *user_data)
560 struct wg_callback *cb;
563 if (user_data == NULL)
566 cb = user_data->data;
568 status = wg_write_messages (ds, vl, cb);
573 static int config_set_char (char *dest,
579 memset (buffer, 0, sizeof (buffer));
581 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
587 ERROR ("write_graphite plugin: Cannot use an empty string for the "
588 "\"EscapeCharacter\" option.");
594 WARNING ("write_graphite plugin: Only the first character of the "
595 "\"EscapeCharacter\" option ('%c') will be used.",
604 static int wg_config_carbon (oconfig_item_t *ci)
606 struct wg_callback *cb;
607 user_data_t user_data;
608 char callback_name[DATA_MAX_NAME_LEN];
611 cb = malloc (sizeof (*cb));
614 ERROR ("write_graphite plugin: malloc failed.");
617 memset (cb, 0, sizeof (*cb));
623 cb->escape_char = WG_DEFAULT_ESCAPE;
626 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
628 for (i = 0; i < ci->children_num; i++)
630 oconfig_item_t *child = ci->children + i;
632 if (strcasecmp ("Host", child->key) == 0)
633 cf_util_get_string (child, &cb->node);
634 else if (strcasecmp ("Port", child->key) == 0)
635 cf_util_get_service (child, &cb->service);
636 else if (strcasecmp ("Prefix", child->key) == 0)
637 cf_util_get_string (child, &cb->prefix);
638 else if (strcasecmp ("Postfix", child->key) == 0)
639 cf_util_get_string (child, &cb->postfix);
640 else if (strcasecmp ("StoreRates", child->key) == 0)
641 cf_util_get_boolean (child, &cb->store_rates);
642 else if (strcasecmp ("SeparateInstances", child->key) == 0)
643 cf_util_get_boolean (child, &cb->separate_instances);
644 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
645 cf_util_get_boolean (child, &cb->always_append_ds);
646 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
647 config_set_char (&cb->escape_char, child);
650 ERROR ("write_graphite plugin: Invalid configuration "
651 "option: %s.", child->key);
655 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s",
656 cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
657 cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE);
659 memset (&user_data, 0, sizeof (user_data));
661 user_data.free_func = wg_callback_free;
662 plugin_register_write (callback_name, wg_write, &user_data);
664 user_data.free_func = NULL;
665 plugin_register_flush (callback_name, wg_flush, &user_data);
670 static int wg_config (oconfig_item_t *ci)
674 for (i = 0; i < ci->children_num; i++)
676 oconfig_item_t *child = ci->children + i;
678 if (strcasecmp ("Carbon", child->key) == 0)
679 wg_config_carbon (child);
682 ERROR ("write_graphite plugin: Invalid configuration "
683 "option: %s.", child->key);
690 void module_register (void)
692 plugin_register_complex_config ("write_graphite", wg_config);
695 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */