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-2013 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_complain.h"
50 #include "utils_parse_option.h"
51 #include "utils_format_graphite.h"
53 /* Folks without pthread will need to disable this plugin. */
56 #include <sys/socket.h>
59 #ifndef WG_DEFAULT_NODE
60 # define WG_DEFAULT_NODE "localhost"
63 #ifndef WG_DEFAULT_SERVICE
64 # define WG_DEFAULT_SERVICE "2003"
67 #ifndef WG_DEFAULT_ESCAPE
68 # define WG_DEFAULT_ESCAPE '_'
71 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
72 #ifndef WG_SEND_BUF_SIZE
73 # define WG_SEND_BUF_SIZE 1428
89 unsigned int format_flags;
91 char send_buf[WG_SEND_BUF_SIZE];
94 cdtime_t send_buf_init_time;
96 pthread_mutex_t send_lock;
97 c_complain_t init_complaint;
104 static void wg_reset_buffer (struct wg_callback *cb)
106 memset (cb->send_buf, 0, sizeof (cb->send_buf));
107 cb->send_buf_free = sizeof (cb->send_buf);
108 cb->send_buf_fill = 0;
109 cb->send_buf_init_time = cdtime ();
112 static int wg_send_buffer (struct wg_callback *cb)
116 status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
120 ERROR ("write_graphite plugin: send failed with status %zi (%s)",
121 status, sstrerror (errno, errbuf, sizeof (errbuf)));
133 /* NOTE: You must hold cb->send_lock when calling this function! */
134 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
138 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
139 "send_buf_fill = %zu;",
143 /* timeout == 0 => flush unconditionally */
149 if ((cb->send_buf_init_time + timeout) > now)
153 if (cb->send_buf_fill <= 0)
155 cb->send_buf_init_time = cdtime ();
159 status = wg_send_buffer (cb);
160 wg_reset_buffer (cb);
165 static int wg_callback_init (struct wg_callback *cb)
167 struct addrinfo ai_hints;
168 struct addrinfo *ai_list;
169 struct addrinfo *ai_ptr;
172 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
173 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
178 memset (&ai_hints, 0, sizeof (ai_hints));
180 ai_hints.ai_flags |= AI_ADDRCONFIG;
182 ai_hints.ai_family = AF_UNSPEC;
183 ai_hints.ai_socktype = SOCK_STREAM;
187 status = getaddrinfo (node, service, &ai_hints, &ai_list);
190 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
191 node, service, gai_strerror (status));
195 assert (ai_list != NULL);
196 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
198 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
199 ai_ptr->ai_protocol);
203 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
214 freeaddrinfo (ai_list);
219 c_complain (LOG_ERR, &cb->init_complaint,
220 "write_graphite plugin: Connecting to %s:%s failed. "
221 "The last error was: %s", node, service,
222 sstrerror (errno, errbuf, sizeof (errbuf)));
227 c_release (LOG_INFO, &cb->init_complaint,
228 "write_graphite plugin: Successfully connected to %s:%s.",
232 wg_reset_buffer (cb);
237 static void wg_callback_free (void *data)
239 struct wg_callback *cb;
246 pthread_mutex_lock (&cb->send_lock);
248 wg_flush_nolock (/* timeout = */ 0, cb);
250 if (cb->sock_fd >= 0)
261 pthread_mutex_destroy (&cb->send_lock);
266 static int wg_flush (cdtime_t timeout,
267 const char *identifier __attribute__((unused)),
268 user_data_t *user_data)
270 struct wg_callback *cb;
273 if (user_data == NULL)
276 cb = user_data->data;
278 pthread_mutex_lock (&cb->send_lock);
282 status = wg_callback_init (cb);
285 /* An error message has already been printed. */
286 pthread_mutex_unlock (&cb->send_lock);
291 status = wg_flush_nolock (timeout, cb);
292 pthread_mutex_unlock (&cb->send_lock);
297 static int wg_send_message (char const *message, struct wg_callback *cb)
302 message_len = strlen (message);
304 pthread_mutex_lock (&cb->send_lock);
308 status = wg_callback_init (cb);
311 /* An error message has already been printed. */
312 pthread_mutex_unlock (&cb->send_lock);
317 if (message_len >= cb->send_buf_free)
319 status = wg_flush_nolock (/* timeout = */ 0, cb);
322 pthread_mutex_unlock (&cb->send_lock);
327 /* Assert that we have enough space for this message. */
328 assert (message_len < cb->send_buf_free);
330 /* `message_len + 1' because `message_len' does not include the
331 * trailing null byte. Neither does `send_buffer_fill'. */
332 memcpy (cb->send_buf + cb->send_buf_fill,
333 message, message_len + 1);
334 cb->send_buf_fill += message_len;
335 cb->send_buf_free -= message_len;
337 DEBUG ("write_graphite plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
340 cb->send_buf_fill, sizeof (cb->send_buf),
341 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
344 pthread_mutex_unlock (&cb->send_lock);
349 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
350 struct wg_callback *cb)
352 char buffer[WG_SEND_BUF_SIZE];
355 if (0 != strcmp (ds->type, vl->type))
357 ERROR ("write_graphite plugin: DS type does not match "
362 memset (buffer, 0, sizeof (buffer));
363 status = format_graphite (buffer, sizeof (buffer), ds, vl,
364 cb->prefix, cb->postfix, cb->escape_char, cb->format_flags);
365 if (status != 0) /* error message has been printed already. */
368 /* Send the message to graphite */
369 status = wg_send_message (buffer, cb);
370 if (status != 0) /* error message has been printed already. */
374 } /* int wg_write_messages */
376 static int wg_write (const data_set_t *ds, const value_list_t *vl,
377 user_data_t *user_data)
379 struct wg_callback *cb;
382 if (user_data == NULL)
385 cb = user_data->data;
387 status = wg_write_messages (ds, vl, cb);
392 static int config_set_char (char *dest,
398 memset (buffer, 0, sizeof (buffer));
400 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
406 ERROR ("write_graphite plugin: Cannot use an empty string for the "
407 "\"EscapeCharacter\" option.");
413 WARNING ("write_graphite plugin: Only the first character of the "
414 "\"EscapeCharacter\" option ('%c') will be used.",
423 static int wg_config_carbon (oconfig_item_t *ci)
425 struct wg_callback *cb;
426 user_data_t user_data;
427 char callback_name[DATA_MAX_NAME_LEN];
430 cb = malloc (sizeof (*cb));
433 ERROR ("write_graphite plugin: malloc failed.");
436 memset (cb, 0, sizeof (*cb));
442 cb->escape_char = WG_DEFAULT_ESCAPE;
443 cb->format_flags = GRAPHITE_STORE_RATES;
445 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
446 C_COMPLAIN_INIT (&cb->init_complaint);
448 for (i = 0; i < ci->children_num; i++)
450 oconfig_item_t *child = ci->children + i;
452 if (strcasecmp ("Host", child->key) == 0)
453 cf_util_get_string (child, &cb->node);
454 else if (strcasecmp ("Port", child->key) == 0)
455 cf_util_get_service (child, &cb->service);
456 else if (strcasecmp ("Prefix", child->key) == 0)
457 cf_util_get_string (child, &cb->prefix);
458 else if (strcasecmp ("Postfix", child->key) == 0)
459 cf_util_get_string (child, &cb->postfix);
460 else if (strcasecmp ("StoreRates", child->key) == 0)
461 cf_util_get_flag (child, &cb->format_flags,
462 GRAPHITE_STORE_RATES);
463 else if (strcasecmp ("SeparateInstances", child->key) == 0)
464 cf_util_get_flag (child, &cb->format_flags,
465 GRAPHITE_SEPARATE_INSTANCES);
466 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
467 cf_util_get_flag (child, &cb->format_flags,
468 GRAPHITE_ALWAYS_APPEND_DS);
469 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
470 config_set_char (&cb->escape_char, child);
473 ERROR ("write_graphite plugin: Invalid configuration "
474 "option: %s.", child->key);
478 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s",
479 cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
480 cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE);
482 memset (&user_data, 0, sizeof (user_data));
484 user_data.free_func = wg_callback_free;
485 plugin_register_write (callback_name, wg_write, &user_data);
487 user_data.free_func = NULL;
488 plugin_register_flush (callback_name, wg_flush, &user_data);
493 static int wg_config (oconfig_item_t *ci)
497 for (i = 0; i < ci->children_num; i++)
499 oconfig_item_t *child = ci->children + i;
501 if (strcasecmp ("Carbon", child->key) == 0)
502 wg_config_carbon (child);
505 ERROR ("write_graphite plugin: Invalid configuration "
506 "option: %s.", child->key);
513 void module_register (void)
515 plugin_register_complex_config ("write_graphite", wg_config);
518 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */