2 * collectd - src/write_graphite.c
3 * Copyright (C) 2011 Scott Sanders
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Scott Sanders <scott@jssjr.com>
21 * based on the excellent write_http plugin
24 /* write_graphite plugin configuation example
26 * <Plugin write_graphite>
38 #include "configfile.h"
40 #include "utils_cache.h"
41 #include "utils_parse_option.h"
43 /* Folks without pthread will need to disable this plugin. */
46 #include <sys/socket.h>
49 #ifndef WG_FORMAT_NAME
50 #define WG_FORMAT_NAME(ret, ret_len, vl, cb, ds_name) \
51 wg_format_name (ret, ret_len, (vl)->host, \
52 (vl)->plugin, (vl)->plugin_instance, \
53 (vl)->type, (vl)->type_instance, \
54 (cb)->prefix, (cb)->postfix, \
55 ds_name, (cb)->escape_char)
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_SEND_BUF_SIZE
67 # define WG_SEND_BUF_SIZE 4096
76 struct hostent *server;
84 char send_buf[WG_SEND_BUF_SIZE];
87 cdtime_t send_buf_init_time;
89 pthread_mutex_t send_lock;
96 static void wg_reset_buffer (struct wg_callback *cb)
98 memset (cb->send_buf, 0, sizeof (cb->send_buf));
99 cb->send_buf_free = sizeof (cb->send_buf);
100 cb->send_buf_fill = 0;
101 cb->send_buf_init_time = cdtime ();
104 static int wg_send_buffer (struct wg_callback *cb)
108 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
111 ERROR ("write_graphite plugin: send failed with "
116 pthread_mutex_trylock (&cb->send_lock);
118 DEBUG ("write_graphite plugin: closing socket and restting fd "
119 "so reinit will occur");
123 pthread_mutex_unlock (&cb->send_lock);
130 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
134 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
135 "send_buf_fill = %zu;",
139 /* timeout == 0 => flush unconditionally */
145 if ((cb->send_buf_init_time + timeout) > now)
149 if (cb->send_buf_fill <= 0)
151 cb->send_buf_init_time = cdtime ();
155 status = wg_send_buffer (cb);
156 wg_reset_buffer (cb);
161 static int wg_callback_init (struct wg_callback *cb)
163 struct addrinfo ai_hints;
164 struct addrinfo *ai_list;
165 struct addrinfo *ai_ptr;
168 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
169 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
174 memset (&ai_hints, 0, sizeof (ai_hints));
176 ai_hints.ai_flags |= AI_ADDRCONFIG;
178 ai_hints.ai_family = AF_UNSPEC;
179 ai_hints.ai_socktype = SOCK_STREAM;
183 status = getaddrinfo (node, service, &ai_hints, &ai_list);
186 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
187 node, service, gai_strerror (status));
191 assert (ai_list != NULL);
192 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
194 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
195 ai_ptr->ai_protocol);
199 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
210 freeaddrinfo (ai_list);
215 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
216 "The last error was: %s", node, service,
217 sstrerror (errno, errbuf, sizeof (errbuf)));
222 wg_reset_buffer (cb);
227 static void wg_callback_free (void *data)
229 struct wg_callback *cb;
236 wg_flush_nolock (/* timeout = */ 0, cb);
247 static int wg_flush (cdtime_t timeout,
248 const char *identifier __attribute__((unused)),
249 user_data_t *user_data)
251 struct wg_callback *cb;
254 if (user_data == NULL)
257 cb = user_data->data;
259 pthread_mutex_lock (&cb->send_lock);
263 status = wg_callback_init (cb);
266 ERROR ("write_graphite plugin: wg_callback_init failed.");
267 pthread_mutex_unlock (&cb->send_lock);
272 status = wg_flush_nolock (timeout, cb);
273 pthread_mutex_unlock (&cb->send_lock);
278 static int wg_format_values (char *ret, size_t ret_len,
279 int ds_num, const data_set_t *ds, const value_list_t *vl,
284 gauge_t *rates = NULL;
286 assert (0 == strcmp (ds->type, vl->type));
288 memset (ret, 0, ret_len);
290 #define BUFFER_ADD(...) do { \
291 status = ssnprintf (ret + offset, ret_len - offset, \
298 else if (((size_t) status) >= (ret_len - offset)) \
304 offset += ((size_t) status); \
307 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
308 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
309 else if (store_rates)
312 rates = uc_get_rate (ds, vl);
315 WARNING ("format_values: "
316 "uc_get_rate failed.");
319 BUFFER_ADD ("%g", rates[ds_num]);
321 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
322 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
323 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
324 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
325 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
326 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
329 ERROR ("format_values plugin: Unknown data source type: %i",
330 ds->ds[ds_num].type);
341 static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
346 memset (dst, 0, dst_len);
351 for (i = 0; i < dst_len; i++)
354 || isspace ((int) src[i])
355 || iscntrl ((int) src[i]))
356 dst[i] = escape_char;
365 static int wg_format_name (char *ret, int ret_len,
366 const char *hostname,
367 const char *plugin, const char *plugin_instance,
368 const char *type, const char *type_instance,
369 const char *prefix, const char *postfix,
370 const char *ds_name, char escape_char)
372 char n_hostname[DATA_MAX_NAME_LEN];
373 char n_plugin[DATA_MAX_NAME_LEN];
374 char n_plugin_instance[DATA_MAX_NAME_LEN];
375 char n_type[DATA_MAX_NAME_LEN];
376 char n_type_instance[DATA_MAX_NAME_LEN];
379 assert (hostname != NULL);
380 assert (plugin != NULL);
381 assert (type != NULL);
382 assert (ds_name != NULL);
390 wg_copy_escape_part (n_hostname, hostname,
391 sizeof (n_hostname), escape_char);
392 wg_copy_escape_part (n_plugin, plugin,
393 sizeof (n_plugin), escape_char);
394 wg_copy_escape_part (n_plugin_instance, plugin_instance,
395 sizeof (n_plugin_instance), escape_char);
396 wg_copy_escape_part (n_type, type,
397 sizeof (n_type), escape_char);
398 wg_copy_escape_part (n_type_instance, type_instance,
399 sizeof (n_type_instance), escape_char);
401 if (n_plugin_instance[0] == '\0')
403 if (n_type_instance[0] == '\0')
405 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
406 prefix, n_hostname, postfix, n_plugin, n_type, ds_name);
410 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
411 prefix, n_hostname, postfix, n_plugin, n_type,
412 n_type_instance, ds_name);
417 if (n_type_instance[0] == '\0')
419 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
420 prefix, n_hostname, postfix, n_plugin,
421 n_plugin_instance, n_type, ds_name);
425 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
426 prefix, n_hostname, postfix, n_plugin,
427 n_plugin_instance, n_type, n_type_instance, ds_name);
431 if ((status < 1) || (status >= ret_len))
437 static int wg_send_message (const char* key, const char* value,
438 cdtime_t time, struct wg_callback *cb)
444 message_len = (size_t) ssnprintf (message, sizeof (message),
448 CDTIME_T_TO_DOUBLE(time));
449 if (message_len >= sizeof (message)) {
450 ERROR ("write_graphite plugin: message buffer too small: "
451 "Need %zu bytes.", message_len + 1);
456 pthread_mutex_lock (&cb->send_lock);
460 status = wg_callback_init (cb);
463 ERROR ("write_graphite plugin: wg_callback_init failed.");
464 pthread_mutex_unlock (&cb->send_lock);
469 if (message_len >= cb->send_buf_free)
471 status = wg_flush_nolock (/* timeout = */ 0, cb);
474 pthread_mutex_unlock (&cb->send_lock);
478 assert (message_len < cb->send_buf_free);
480 /* `message_len + 1' because `message_len' does not include the
481 * trailing null byte. Neither does `send_buffer_fill'. */
482 memcpy (cb->send_buf + cb->send_buf_fill,
483 message, message_len + 1);
484 cb->send_buf_fill += message_len;
485 cb->send_buf_free -= message_len;
487 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
490 cb->send_buf_fill, sizeof (cb->send_buf),
491 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
494 /* Check if we have enough space for this message. */
495 pthread_mutex_unlock (&cb->send_lock);
500 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
501 struct wg_callback *cb)
503 char key[10*DATA_MAX_NAME_LEN];
508 if (0 != strcmp (ds->type, vl->type))
510 ERROR ("write_graphite plugin: DS type does not match "
517 for (i = 0; i < ds->ds_num; i++)
519 /* Copy the identifier to `key' and escape it. */
520 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
523 ERROR ("write_graphite plugin: error with format_name");
527 escape_string (key, sizeof (key));
528 /* Convert the values to an ASCII representation and put that
530 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
533 ERROR ("write_graphite plugin: error with "
538 /* Send the message to graphite */
539 status = wg_send_message (key, values, vl->time, cb);
542 ERROR ("write_graphite plugin: error with "
550 /* Copy the identifier to `key' and escape it. */
551 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
554 ERROR ("write_graphite plugin: error with format_name");
558 escape_string (key, sizeof (key));
559 /* Convert the values to an ASCII representation and put that into
561 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
564 ERROR ("write_graphite plugin: error with "
569 /* Send the message to graphite */
570 status = wg_send_message (key, values, vl->time, cb);
573 ERROR ("write_graphite plugin: error with "
582 static int wg_write (const data_set_t *ds, const value_list_t *vl,
583 user_data_t *user_data)
585 struct wg_callback *cb;
588 if (user_data == NULL)
591 cb = user_data->data;
593 status = wg_write_messages (ds, vl, cb);
598 static int config_set_char (char *dest,
601 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
603 WARNING ("write_graphite plugin: The `%s' config option "
604 "needs exactly one string argument.", ci->key);
608 *dest = ci->values[0].value.string[0];
613 static int wg_config_carbon (oconfig_item_t *ci)
615 struct wg_callback *cb;
616 user_data_t user_data;
619 cb = malloc (sizeof (*cb));
622 ERROR ("write_graphite plugin: malloc failed.");
625 memset (cb, 0, sizeof (*cb));
632 cb->escape_char = '_';
634 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
636 for (i = 0; i < ci->children_num; i++)
638 oconfig_item_t *child = ci->children + i;
640 if (strcasecmp ("Host", child->key) == 0)
641 cf_util_get_string (child, &cb->node);
642 else if (strcasecmp ("Port", child->key) == 0)
643 cf_util_get_string (child, &cb->service);
644 else if (strcasecmp ("Prefix", child->key) == 0)
645 cf_util_get_string (child, &cb->prefix);
646 else if (strcasecmp ("Postfix", child->key) == 0)
647 cf_util_get_string (child, &cb->postfix);
648 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
649 config_set_char (&cb->escape_char, child);
652 ERROR ("write_graphite plugin: Invalid configuration "
653 "option: %s.", child->key);
657 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
658 cb->node ? cb->node : WG_DEFAULT_NODE,
659 cb->service ? cb->service : WG_DEFAULT_SERVICE);
661 memset (&user_data, 0, sizeof (user_data));
663 user_data.free_func = NULL;
664 plugin_register_flush ("write_graphite", wg_flush, &user_data);
666 user_data.free_func = wg_callback_free;
667 plugin_register_write ("write_graphite", wg_write, &user_data);
672 static int wg_config (oconfig_item_t *ci)
676 for (i = 0; i < ci->children_num; i++)
678 oconfig_item_t *child = ci->children + i;
680 if (strcasecmp ("Carbon", child->key) == 0)
681 wg_config_carbon (child);
684 ERROR ("write_graphite plugin: Invalid configuration "
685 "option: %s.", child->key);
692 void module_register (void)
694 plugin_register_complex_config ("write_graphite", wg_config);
697 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */