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_format_graphite.h"
54 /* Folks without pthread will need to disable this plugin. */
57 #include <sys/socket.h>
60 #define WG_DEFAULT_NODE "localhost"
61 #define WG_DEFAULT_SERVICE "2003"
62 #define WG_DEFAULT_PROTOCOL "tcp"
63 #define WG_DEFAULT_LOG_SEND_ERRORS 1
64 #define WG_DEFAULT_ESCAPE '_'
66 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
67 #define WG_SEND_BUF_SIZE 1428
69 #define WG_MIN_RECONNECT_INTERVAL TIME_T_TO_CDTIME_T (1)
83 _Bool log_send_errors;
88 unsigned int format_flags;
90 char send_buf[WG_SEND_BUF_SIZE];
93 cdtime_t send_buf_init_time;
95 pthread_mutex_t send_lock;
96 c_complain_t init_complaint;
97 cdtime_t last_connect_time;
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));
119 const char *protocol = cb->protocol ? cb->protocol : WG_DEFAULT_PROTOCOL;
121 if (cb->log_send_errors)
124 ERROR ("write_graphite plugin: send to %s:%s (%s) failed with status %zi (%s)",
125 cb->node, cb->service, protocol,
126 status, sstrerror (errno, errbuf, sizeof (errbuf)));
138 /* NOTE: You must hold cb->send_lock when calling this function! */
139 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
143 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
144 "send_buf_fill = %zu;",
148 /* timeout == 0 => flush unconditionally */
154 if ((cb->send_buf_init_time + timeout) > now)
158 if (cb->send_buf_fill <= 0)
160 cb->send_buf_init_time = cdtime ();
164 status = wg_send_buffer (cb);
165 wg_reset_buffer (cb);
170 static int wg_callback_init (struct wg_callback *cb)
172 struct addrinfo ai_hints;
173 struct addrinfo *ai_list;
174 struct addrinfo *ai_ptr;
178 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
179 const char *protocol = cb->protocol ? cb->protocol : WG_DEFAULT_PROTOCOL;
181 char connerr[1024] = "";
186 /* Don't try to reconnect too often. By default, one reconnection attempt
187 * is made per second. */
189 if ((now - cb->last_connect_time) < WG_MIN_RECONNECT_INTERVAL)
191 cb->last_connect_time = now;
193 memset (&ai_hints, 0, sizeof (ai_hints));
195 ai_hints.ai_flags |= AI_ADDRCONFIG;
197 ai_hints.ai_family = AF_UNSPEC;
199 if (0 == strcasecmp ("tcp", protocol))
200 ai_hints.ai_socktype = SOCK_STREAM;
202 ai_hints.ai_socktype = SOCK_DGRAM;
206 status = getaddrinfo (cb->node, service, &ai_hints, &ai_list);
209 ERROR ("write_graphite plugin: getaddrinfo (%s, %s, %s) failed: %s",
210 cb->node, service, protocol, gai_strerror (status));
214 assert (ai_list != NULL);
215 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
217 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
218 ai_ptr->ai_protocol);
219 if (cb->sock_fd < 0) {
221 snprintf (connerr, sizeof (connerr), "failed to open socket: %s",
222 sstrerror (errno, errbuf, sizeof (errbuf)));
226 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
230 snprintf (connerr, sizeof (connerr), "failed to connect to remote "
231 "host: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
240 freeaddrinfo (ai_list);
244 if (connerr[0] == '\0')
245 /* this should not happen but try to get a message anyway */
246 sstrerror (errno, connerr, sizeof (connerr));
247 c_complain (LOG_ERR, &cb->init_complaint,
248 "write_graphite plugin: Connecting to %s:%s via %s failed. "
249 "The last error was: %s", cb->node, service, protocol, connerr);
254 c_release (LOG_INFO, &cb->init_complaint,
255 "write_graphite plugin: Successfully connected to %s:%s via %s.",
256 cb->node, service, protocol);
259 wg_reset_buffer (cb);
264 static void wg_callback_free (void *data)
266 struct wg_callback *cb;
273 pthread_mutex_lock (&cb->send_lock);
275 wg_flush_nolock (/* timeout = */ 0, cb);
277 if (cb->sock_fd >= 0)
290 pthread_mutex_destroy (&cb->send_lock);
295 static int wg_flush (cdtime_t timeout,
296 const char *identifier __attribute__((unused)),
297 user_data_t *user_data)
299 struct wg_callback *cb;
302 if (user_data == NULL)
305 cb = user_data->data;
307 pthread_mutex_lock (&cb->send_lock);
311 status = wg_callback_init (cb);
314 /* An error message has already been printed. */
315 pthread_mutex_unlock (&cb->send_lock);
320 status = wg_flush_nolock (timeout, cb);
321 pthread_mutex_unlock (&cb->send_lock);
326 static int wg_send_message (char const *message, struct wg_callback *cb)
331 message_len = strlen (message);
333 pthread_mutex_lock (&cb->send_lock);
337 status = wg_callback_init (cb);
340 /* An error message has already been printed. */
341 pthread_mutex_unlock (&cb->send_lock);
346 if (message_len >= cb->send_buf_free)
348 status = wg_flush_nolock (/* timeout = */ 0, cb);
351 pthread_mutex_unlock (&cb->send_lock);
356 /* Assert that we have enough space for this message. */
357 assert (message_len < cb->send_buf_free);
359 /* `message_len + 1' because `message_len' does not include the
360 * trailing null byte. Neither does `send_buffer_fill'. */
361 memcpy (cb->send_buf + cb->send_buf_fill,
362 message, message_len + 1);
363 cb->send_buf_fill += message_len;
364 cb->send_buf_free -= message_len;
366 DEBUG ("write_graphite plugin: [%s]:%s (%s) buf %zu/%zu (%.1f %%) \"%s\"",
370 cb->send_buf_fill, sizeof (cb->send_buf),
371 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
374 pthread_mutex_unlock (&cb->send_lock);
379 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
380 struct wg_callback *cb)
382 char buffer[WG_SEND_BUF_SIZE];
385 if (0 != strcmp (ds->type, vl->type))
387 ERROR ("write_graphite plugin: DS type does not match "
392 memset (buffer, 0, sizeof (buffer));
393 status = format_graphite (buffer, sizeof (buffer), ds, vl,
394 cb->prefix, cb->postfix, cb->escape_char, cb->format_flags);
395 if (status != 0) /* error message has been printed already. */
398 /* Send the message to graphite */
399 status = wg_send_message (buffer, cb);
400 if (status != 0) /* error message has been printed already. */
404 } /* int wg_write_messages */
406 static int wg_write (const data_set_t *ds, const value_list_t *vl,
407 user_data_t *user_data)
409 struct wg_callback *cb;
412 if (user_data == NULL)
415 cb = user_data->data;
417 status = wg_write_messages (ds, vl, cb);
422 static int config_set_char (char *dest,
428 memset (buffer, 0, sizeof (buffer));
430 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
436 ERROR ("write_graphite plugin: Cannot use an empty string for the "
437 "\"EscapeCharacter\" option.");
443 WARNING ("write_graphite plugin: Only the first character of the "
444 "\"EscapeCharacter\" option ('%c') will be used.",
453 static int wg_config_node (oconfig_item_t *ci)
455 struct wg_callback *cb;
456 user_data_t user_data;
457 char callback_name[DATA_MAX_NAME_LEN];
461 cb = malloc (sizeof (*cb));
464 ERROR ("write_graphite plugin: malloc failed.");
467 memset (cb, 0, sizeof (*cb));
470 cb->node = strdup (WG_DEFAULT_NODE);
473 cb->log_send_errors = WG_DEFAULT_LOG_SEND_ERRORS;
476 cb->escape_char = WG_DEFAULT_ESCAPE;
477 cb->format_flags = GRAPHITE_STORE_RATES;
479 /* FIXME: Legacy configuration syntax. */
480 if (strcasecmp ("Carbon", ci->key) != 0)
482 status = cf_util_get_string (ci, &cb->name);
485 wg_callback_free (cb);
490 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
491 C_COMPLAIN_INIT (&cb->init_complaint);
493 for (i = 0; i < ci->children_num; i++)
495 oconfig_item_t *child = ci->children + i;
497 if (strcasecmp ("Host", child->key) == 0)
498 cf_util_get_string (child, &cb->node);
499 else if (strcasecmp ("Port", child->key) == 0)
500 cf_util_get_service (child, &cb->service);
501 else if (strcasecmp ("Protocol", child->key) == 0)
503 cf_util_get_string (child, &cb->protocol);
505 if (strcasecmp ("UDP", cb->protocol) != 0 &&
506 strcasecmp ("TCP", cb->protocol) != 0)
508 ERROR ("write_graphite plugin: Unknown protocol (%s)",
513 else if (strcasecmp ("LogSendErrors", child->key) == 0)
514 cf_util_get_boolean (child, &cb->log_send_errors);
515 else if (strcasecmp ("Prefix", child->key) == 0)
516 cf_util_get_string (child, &cb->prefix);
517 else if (strcasecmp ("Postfix", child->key) == 0)
518 cf_util_get_string (child, &cb->postfix);
519 else if (strcasecmp ("StoreRates", child->key) == 0)
520 cf_util_get_flag (child, &cb->format_flags,
521 GRAPHITE_STORE_RATES);
522 else if (strcasecmp ("SeparateInstances", child->key) == 0)
523 cf_util_get_flag (child, &cb->format_flags,
524 GRAPHITE_SEPARATE_INSTANCES);
525 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
526 cf_util_get_flag (child, &cb->format_flags,
527 GRAPHITE_ALWAYS_APPEND_DS);
528 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
529 config_set_char (&cb->escape_char, child);
532 ERROR ("write_graphite plugin: Invalid configuration "
533 "option: %s.", child->key);
543 wg_callback_free (cb);
547 /* FIXME: Legacy configuration syntax. */
548 if (cb->name == NULL)
549 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s/%s",
551 cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE,
552 cb->protocol != NULL ? cb->protocol : WG_DEFAULT_PROTOCOL);
554 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s",
557 memset (&user_data, 0, sizeof (user_data));
559 user_data.free_func = wg_callback_free;
560 plugin_register_write (callback_name, wg_write, &user_data);
562 user_data.free_func = NULL;
563 plugin_register_flush (callback_name, wg_flush, &user_data);
568 static int wg_config (oconfig_item_t *ci)
572 for (i = 0; i < ci->children_num; i++)
574 oconfig_item_t *child = ci->children + i;
576 if (strcasecmp ("Node", child->key) == 0)
577 wg_config_node (child);
578 /* FIXME: Remove this legacy mode in version 6. */
579 else if (strcasecmp ("Carbon", child->key) == 0)
580 wg_config_node (child);
583 ERROR ("write_graphite plugin: Invalid configuration "
584 "option: %s.", child->key);
591 void module_register (void)
593 plugin_register_complex_config ("write_graphite", wg_config);
596 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */