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>
48 #include "configfile.h"
50 #include "utils_cache.h"
51 #include "utils_complain.h"
52 #include "utils_parse_option.h"
53 #include "utils_format_graphite.h"
55 /* Folks without pthread will need to disable this plugin. */
58 #include <sys/socket.h>
61 #ifndef WG_DEFAULT_NODE
62 # define WG_DEFAULT_NODE "localhost"
65 #ifndef WG_DEFAULT_SERVICE
66 # define WG_DEFAULT_SERVICE "2003"
69 #ifndef WG_DEFAULT_PROTOCOL
70 # define WG_DEFAULT_PROTOCOL "tcp"
73 #ifndef WG_DEFAULT_LOG_SEND_ERRORS
74 # define WG_DEFAULT_LOG_SEND_ERRORS 1
77 #ifndef WG_DEFAULT_ESCAPE
78 # define WG_DEFAULT_ESCAPE '_'
81 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
82 #ifndef WG_SEND_BUF_SIZE
83 # define WG_SEND_BUF_SIZE 1428
86 #ifndef WG_MIN_RECONNECT_INTERVAL
87 # define WG_MIN_RECONNECT_INTERVAL TIME_T_TO_CDTIME_T (1)
102 _Bool log_send_errors;
107 unsigned int format_flags;
109 char send_buf[WG_SEND_BUF_SIZE];
110 size_t send_buf_free;
111 size_t send_buf_fill;
112 cdtime_t send_buf_init_time;
114 pthread_mutex_t send_lock;
115 c_complain_t init_complaint;
116 cdtime_t last_connect_time;
123 static void wg_reset_buffer (struct wg_callback *cb)
125 memset (cb->send_buf, 0, sizeof (cb->send_buf));
126 cb->send_buf_free = sizeof (cb->send_buf);
127 cb->send_buf_fill = 0;
128 cb->send_buf_init_time = cdtime ();
131 static int wg_send_buffer (struct wg_callback *cb)
135 status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
138 const char *protocol = cb->protocol ? cb->protocol : WG_DEFAULT_PROTOCOL;
140 if (cb->log_send_errors)
143 ERROR ("write_graphite plugin: send to %s:%s (%s) failed with status %zi (%s)",
144 cb->node, cb->service, protocol,
145 status, sstrerror (errno, errbuf, sizeof (errbuf)));
157 /* NOTE: You must hold cb->send_lock when calling this function! */
158 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
162 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
163 "send_buf_fill = %zu;",
167 /* timeout == 0 => flush unconditionally */
173 if ((cb->send_buf_init_time + timeout) > now)
177 if (cb->send_buf_fill <= 0)
179 cb->send_buf_init_time = cdtime ();
183 status = wg_send_buffer (cb);
184 wg_reset_buffer (cb);
189 static int wg_callback_init (struct wg_callback *cb)
191 struct addrinfo ai_hints;
192 struct addrinfo *ai_list;
193 struct addrinfo *ai_ptr;
197 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
198 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
199 const char *protocol = cb->protocol ? cb->protocol : WG_DEFAULT_PROTOCOL;
204 /* Don't try to reconnect too often. By default, one reconnection attempt
205 * is made per second. */
207 if ((now - cb->last_connect_time) < WG_MIN_RECONNECT_INTERVAL)
209 cb->last_connect_time = now;
211 memset (&ai_hints, 0, sizeof (ai_hints));
213 ai_hints.ai_flags |= AI_ADDRCONFIG;
215 ai_hints.ai_family = AF_UNSPEC;
217 if (0 == strcasecmp ("tcp", protocol))
218 ai_hints.ai_socktype = SOCK_STREAM;
220 ai_hints.ai_socktype = SOCK_DGRAM;
224 status = getaddrinfo (node, service, &ai_hints, &ai_list);
227 ERROR ("write_graphite plugin: getaddrinfo (%s, %s, %s) failed: %s",
228 node, service, protocol, gai_strerror (status));
232 assert (ai_list != NULL);
233 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
235 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
236 ai_ptr->ai_protocol);
240 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
251 freeaddrinfo (ai_list);
256 c_complain (LOG_ERR, &cb->init_complaint,
257 "write_graphite plugin: Connecting to %s:%s via %s failed. "
258 "The last error was: %s", node, service, protocol,
259 sstrerror (errno, errbuf, sizeof (errbuf)));
264 c_release (LOG_INFO, &cb->init_complaint,
265 "write_graphite plugin: Successfully connected to %s:%s via %s.",
266 node, service, protocol);
269 wg_reset_buffer (cb);
274 static void wg_callback_free (void *data)
276 struct wg_callback *cb;
283 pthread_mutex_lock (&cb->send_lock);
285 wg_flush_nolock (/* timeout = */ 0, cb);
287 if (cb->sock_fd >= 0)
300 pthread_mutex_destroy (&cb->send_lock);
305 static int wg_flush (cdtime_t timeout,
306 const char *identifier __attribute__((unused)),
307 user_data_t *user_data)
309 struct wg_callback *cb;
312 if (user_data == NULL)
315 cb = user_data->data;
317 pthread_mutex_lock (&cb->send_lock);
321 status = wg_callback_init (cb);
324 /* An error message has already been printed. */
325 pthread_mutex_unlock (&cb->send_lock);
330 status = wg_flush_nolock (timeout, cb);
331 pthread_mutex_unlock (&cb->send_lock);
336 static int wg_send_message (char const *message, struct wg_callback *cb)
341 message_len = strlen (message);
343 pthread_mutex_lock (&cb->send_lock);
347 status = wg_callback_init (cb);
350 /* An error message has already been printed. */
351 pthread_mutex_unlock (&cb->send_lock);
356 if (message_len >= cb->send_buf_free)
358 status = wg_flush_nolock (/* timeout = */ 0, cb);
361 pthread_mutex_unlock (&cb->send_lock);
366 /* Assert that we have enough space for this message. */
367 assert (message_len < cb->send_buf_free);
369 /* `message_len + 1' because `message_len' does not include the
370 * trailing null byte. Neither does `send_buffer_fill'. */
371 memcpy (cb->send_buf + cb->send_buf_fill,
372 message, message_len + 1);
373 cb->send_buf_fill += message_len;
374 cb->send_buf_free -= message_len;
376 DEBUG ("write_graphite plugin: [%s]:%s (%s) buf %zu/%zu (%.1f %%) \"%s\"",
380 cb->send_buf_fill, sizeof (cb->send_buf),
381 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
384 pthread_mutex_unlock (&cb->send_lock);
389 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
390 struct wg_callback *cb)
392 char buffer[WG_SEND_BUF_SIZE];
395 if (0 != strcmp (ds->type, vl->type))
397 ERROR ("write_graphite plugin: DS type does not match "
402 memset (buffer, 0, sizeof (buffer));
403 status = format_graphite (buffer, sizeof (buffer), ds, vl,
404 cb->prefix, cb->postfix, cb->escape_char, cb->format_flags);
405 if (status != 0) /* error message has been printed already. */
408 /* Send the message to graphite */
409 status = wg_send_message (buffer, cb);
410 if (status != 0) /* error message has been printed already. */
414 } /* int wg_write_messages */
416 static int wg_write (const data_set_t *ds, const value_list_t *vl,
417 user_data_t *user_data)
419 struct wg_callback *cb;
422 if (user_data == NULL)
425 cb = user_data->data;
427 status = wg_write_messages (ds, vl, cb);
432 static int config_set_char (char *dest,
438 memset (buffer, 0, sizeof (buffer));
440 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
446 ERROR ("write_graphite plugin: Cannot use an empty string for the "
447 "\"EscapeCharacter\" option.");
453 WARNING ("write_graphite plugin: Only the first character of the "
454 "\"EscapeCharacter\" option ('%c') will be used.",
463 static int wg_config_node (oconfig_item_t *ci)
465 struct wg_callback *cb;
466 user_data_t user_data;
467 char callback_name[DATA_MAX_NAME_LEN];
471 cb = malloc (sizeof (*cb));
474 ERROR ("write_graphite plugin: malloc failed.");
477 memset (cb, 0, sizeof (*cb));
483 cb->log_send_errors = WG_DEFAULT_LOG_SEND_ERRORS;
486 cb->escape_char = WG_DEFAULT_ESCAPE;
487 cb->format_flags = GRAPHITE_STORE_RATES;
489 /* FIXME: Legacy configuration syntax. */
490 if (strcasecmp ("Carbon", ci->key) != 0)
492 status = cf_util_get_string (ci, &cb->name);
495 wg_callback_free (cb);
500 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
501 C_COMPLAIN_INIT (&cb->init_complaint);
503 for (i = 0; i < ci->children_num; i++)
505 oconfig_item_t *child = ci->children + i;
507 if (strcasecmp ("Host", child->key) == 0)
508 cf_util_get_string (child, &cb->node);
509 else if (strcasecmp ("Port", child->key) == 0)
510 cf_util_get_service (child, &cb->service);
511 else if (strcasecmp ("Protocol", child->key) == 0)
513 cf_util_get_string (child, &cb->protocol);
515 if (strcasecmp ("UDP", cb->protocol) != 0 &&
516 strcasecmp ("TCP", cb->protocol) != 0)
518 ERROR ("write_graphite plugin: Unknown protocol (%s)",
523 else if (strcasecmp ("LogSendErrors", child->key) == 0)
524 cf_util_get_boolean (child, &cb->log_send_errors);
525 else if (strcasecmp ("Prefix", child->key) == 0)
526 cf_util_get_string (child, &cb->prefix);
527 else if (strcasecmp ("Postfix", child->key) == 0)
528 cf_util_get_string (child, &cb->postfix);
529 else if (strcasecmp ("StoreRates", child->key) == 0)
530 cf_util_get_flag (child, &cb->format_flags,
531 GRAPHITE_STORE_RATES);
532 else if (strcasecmp ("SeparateInstances", child->key) == 0)
533 cf_util_get_flag (child, &cb->format_flags,
534 GRAPHITE_SEPARATE_INSTANCES);
535 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
536 cf_util_get_flag (child, &cb->format_flags,
537 GRAPHITE_ALWAYS_APPEND_DS);
538 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
539 config_set_char (&cb->escape_char, child);
542 ERROR ("write_graphite plugin: Invalid configuration "
543 "option: %s.", child->key);
553 wg_callback_free (cb);
557 /* FIXME: Legacy configuration syntax. */
558 if (cb->name == NULL)
559 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s/%s",
560 cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
561 cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE,
562 cb->protocol != NULL ? cb->protocol : WG_DEFAULT_PROTOCOL);
564 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s",
567 memset (&user_data, 0, sizeof (user_data));
569 user_data.free_func = wg_callback_free;
570 plugin_register_write (callback_name, wg_write, &user_data);
572 user_data.free_func = NULL;
573 plugin_register_flush (callback_name, wg_flush, &user_data);
578 static int wg_config (oconfig_item_t *ci)
582 for (i = 0; i < ci->children_num; i++)
584 oconfig_item_t *child = ci->children + i;
586 if (strcasecmp ("Node", child->key) == 0)
587 wg_config_node (child);
588 /* FIXME: Remove this legacy mode in version 6. */
589 else if (strcasecmp ("Carbon", child->key) == 0)
590 wg_config_node (child);
593 ERROR ("write_graphite plugin: Invalid configuration "
594 "option: %s.", child->key);
601 void module_register (void)
603 plugin_register_complex_config ("write_graphite", wg_config);
606 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */