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"
50 #include "utils_format_graphite.h"
52 /* Folks without pthread will need to disable this plugin. */
55 #include <sys/socket.h>
58 #ifndef WG_DEFAULT_NODE
59 # define WG_DEFAULT_NODE "localhost"
62 #ifndef WG_DEFAULT_SERVICE
63 # define WG_DEFAULT_SERVICE "2003"
66 #ifndef WG_DEFAULT_ESCAPE
67 # define WG_DEFAULT_ESCAPE '_'
70 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
71 #ifndef WG_SEND_BUF_SIZE
72 # define WG_SEND_BUF_SIZE 1428
91 _Bool separate_instances;
92 _Bool always_append_ds;
94 char send_buf[WG_SEND_BUF_SIZE];
97 cdtime_t send_buf_init_time;
99 pthread_mutex_t send_lock;
106 static void wg_reset_buffer (struct wg_callback *cb)
108 memset (cb->send_buf, 0, sizeof (cb->send_buf));
109 cb->send_buf_free = sizeof (cb->send_buf);
110 cb->send_buf_fill = 0;
111 cb->send_buf_init_time = cdtime ();
114 static int wg_send_buffer (struct wg_callback *cb)
118 status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
122 ERROR ("write_graphite plugin: send failed with status %zi (%s)",
123 status, sstrerror (errno, errbuf, sizeof (errbuf)));
135 /* NOTE: You must hold cb->send_lock when calling this function! */
136 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
140 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
141 "send_buf_fill = %zu;",
145 /* timeout == 0 => flush unconditionally */
151 if ((cb->send_buf_init_time + timeout) > now)
155 if (cb->send_buf_fill <= 0)
157 cb->send_buf_init_time = cdtime ();
161 status = wg_send_buffer (cb);
162 wg_reset_buffer (cb);
167 static int wg_callback_init (struct wg_callback *cb)
169 struct addrinfo ai_hints;
170 struct addrinfo *ai_list;
171 struct addrinfo *ai_ptr;
174 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
175 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
180 memset (&ai_hints, 0, sizeof (ai_hints));
182 ai_hints.ai_flags |= AI_ADDRCONFIG;
184 ai_hints.ai_family = AF_UNSPEC;
185 ai_hints.ai_socktype = SOCK_STREAM;
189 status = getaddrinfo (node, service, &ai_hints, &ai_list);
192 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
193 node, service, gai_strerror (status));
197 assert (ai_list != NULL);
198 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
200 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
201 ai_ptr->ai_protocol);
205 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
216 freeaddrinfo (ai_list);
221 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
222 "The last error was: %s", node, service,
223 sstrerror (errno, errbuf, sizeof (errbuf)));
228 wg_reset_buffer (cb);
233 static void wg_callback_free (void *data)
235 struct wg_callback *cb;
242 pthread_mutex_lock (&cb->send_lock);
244 wg_flush_nolock (/* timeout = */ 0, cb);
255 pthread_mutex_destroy (&cb->send_lock);
260 static int wg_flush (cdtime_t timeout,
261 const char *identifier __attribute__((unused)),
262 user_data_t *user_data)
264 struct wg_callback *cb;
267 if (user_data == NULL)
270 cb = user_data->data;
272 pthread_mutex_lock (&cb->send_lock);
276 status = wg_callback_init (cb);
279 ERROR ("write_graphite plugin: wg_callback_init failed.");
280 pthread_mutex_unlock (&cb->send_lock);
285 status = wg_flush_nolock (timeout, cb);
286 pthread_mutex_unlock (&cb->send_lock);
291 static int wg_send_message (char const *message, struct wg_callback *cb)
296 message_len = strlen (message);
298 pthread_mutex_lock (&cb->send_lock);
302 status = wg_callback_init (cb);
305 ERROR ("write_graphite plugin: wg_callback_init failed.");
306 pthread_mutex_unlock (&cb->send_lock);
311 if (message_len >= cb->send_buf_free)
313 status = wg_flush_nolock (/* timeout = */ 0, cb);
316 pthread_mutex_unlock (&cb->send_lock);
321 /* Assert that we have enough space for this message. */
322 assert (message_len < cb->send_buf_free);
324 /* `message_len + 1' because `message_len' does not include the
325 * trailing null byte. Neither does `send_buffer_fill'. */
326 memcpy (cb->send_buf + cb->send_buf_fill,
327 message, message_len + 1);
328 cb->send_buf_fill += message_len;
329 cb->send_buf_free -= message_len;
331 DEBUG ("write_graphite plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
334 cb->send_buf_fill, sizeof (cb->send_buf),
335 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
338 pthread_mutex_unlock (&cb->send_lock);
343 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
344 struct wg_callback *cb)
349 if (0 != strcmp (ds->type, vl->type))
351 ERROR ("write_graphite plugin: DS type does not match "
356 memset (buffer, 0, sizeof (buffer));
357 status = format_graphite (buffer, sizeof (buffer), ds, vl,
358 cb->prefix, cb->postfix, cb->escape_char, cb->store_rates);
359 if (status != 0) /* error message has been printed already. */
362 wg_send_message (buffer, cb);
365 ERROR ("write_graphite plugin: wg_send_message failed "
366 "with status %i.", status);
371 } /* int wg_write_messages */
373 static int wg_write (const data_set_t *ds, const value_list_t *vl,
374 user_data_t *user_data)
376 struct wg_callback *cb;
379 if (user_data == NULL)
382 cb = user_data->data;
384 status = wg_write_messages (ds, vl, cb);
389 static int config_set_char (char *dest,
395 memset (buffer, 0, sizeof (buffer));
397 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
403 ERROR ("write_graphite plugin: Cannot use an empty string for the "
404 "\"EscapeCharacter\" option.");
410 WARNING ("write_graphite plugin: Only the first character of the "
411 "\"EscapeCharacter\" option ('%c') will be used.",
420 static int wg_config_node (oconfig_item_t *ci)
422 struct wg_callback *cb;
423 user_data_t user_data;
424 char callback_name[DATA_MAX_NAME_LEN];
427 cb = malloc (sizeof (*cb));
430 ERROR ("write_graphite plugin: malloc failed.");
433 memset (cb, 0, sizeof (*cb));
440 cb->escape_char = WG_DEFAULT_ESCAPE;
443 /* FIXME: Legacy configuration syntax. */
444 if (strcasecmp ("Carbon", ci->key) != 0)
446 int status = cf_util_get_string (ci, &cb->name);
449 wg_callback_free (cb);
454 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
456 for (i = 0; i < ci->children_num; i++)
458 oconfig_item_t *child = ci->children + i;
460 if (strcasecmp ("Host", child->key) == 0)
461 cf_util_get_string (child, &cb->node);
462 else if (strcasecmp ("Port", child->key) == 0)
463 cf_util_get_service (child, &cb->service);
464 else if (strcasecmp ("Prefix", child->key) == 0)
465 cf_util_get_string (child, &cb->prefix);
466 else if (strcasecmp ("Postfix", child->key) == 0)
467 cf_util_get_string (child, &cb->postfix);
468 else if (strcasecmp ("StoreRates", child->key) == 0)
469 cf_util_get_boolean (child, &cb->store_rates);
470 else if (strcasecmp ("SeparateInstances", child->key) == 0)
471 cf_util_get_boolean (child, &cb->separate_instances);
472 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
473 cf_util_get_boolean (child, &cb->always_append_ds);
474 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
475 config_set_char (&cb->escape_char, child);
478 ERROR ("write_graphite plugin: Invalid configuration "
479 "option: %s.", child->key);
483 /* FIXME: Legacy configuration syntax. */
484 if (cb->name == NULL)
485 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s",
486 cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
487 cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE);
489 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s",
492 memset (&user_data, 0, sizeof (user_data));
494 user_data.free_func = wg_callback_free;
495 plugin_register_write (callback_name, wg_write, &user_data);
497 user_data.free_func = NULL;
498 plugin_register_flush (callback_name, wg_flush, &user_data);
503 static int wg_config (oconfig_item_t *ci)
507 for (i = 0; i < ci->children_num; i++)
509 oconfig_item_t *child = ci->children + i;
511 if (strcasecmp ("Node", child->key) == 0)
512 wg_config_node (child);
513 /* FIXME: Remove this legacy mode in version 6. */
514 else if (strcasecmp ("Carbon", child->key) == 0)
515 wg_config_node (child);
518 ERROR ("write_graphite plugin: Invalid configuration "
519 "option: %s.", child->key);
526 void module_register (void)
528 plugin_register_complex_config ("write_graphite", wg_config);
531 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */