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, name) \
51 wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, \
52 (vl)->plugin_instance, (vl)->type, \
53 (vl)->type_instance, (cb)->prefix, (cb)->postfix, \
57 #ifndef WG_DEFAULT_NODE
58 # define WG_DEFAULT_NODE "localhost"
61 #ifndef WG_DEFAULT_SERVICE
62 # define WG_DEFAULT_SERVICE "2003"
65 #ifndef WG_SEND_BUF_SIZE
66 # define WG_SEND_BUF_SIZE 4096
75 struct hostent *server;
83 char send_buf[WG_SEND_BUF_SIZE];
86 cdtime_t send_buf_init_time;
88 pthread_mutex_t send_lock;
95 static void wg_reset_buffer (struct wg_callback *cb)
97 memset (cb->send_buf, 0, sizeof (cb->send_buf));
98 cb->send_buf_free = sizeof (cb->send_buf);
99 cb->send_buf_fill = 0;
100 cb->send_buf_init_time = cdtime ();
103 static int wg_send_buffer (struct wg_callback *cb)
107 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
110 ERROR ("write_graphite plugin: send failed with "
115 pthread_mutex_trylock (&cb->send_lock);
117 DEBUG ("write_graphite plugin: closing socket and restting fd "
118 "so reinit will occur");
122 pthread_mutex_unlock (&cb->send_lock);
129 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
133 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
134 "send_buf_fill = %zu;",
138 /* timeout == 0 => flush unconditionally */
144 if ((cb->send_buf_init_time + timeout) > now)
148 if (cb->send_buf_fill <= 0)
150 cb->send_buf_init_time = cdtime ();
154 status = wg_send_buffer (cb);
155 wg_reset_buffer (cb);
160 static int wg_callback_init (struct wg_callback *cb)
162 struct addrinfo ai_hints;
163 struct addrinfo *ai_list;
164 struct addrinfo *ai_ptr;
167 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
168 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
173 memset (&ai_hints, 0, sizeof (ai_hints));
175 ai_hints.ai_flags |= AI_ADDRCONFIG;
177 ai_hints.ai_family = AF_UNSPEC;
178 ai_hints.ai_socktype = SOCK_STREAM;
182 status = getaddrinfo (node, service, &ai_hints, &ai_list);
185 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
186 node, service, gai_strerror (status));
190 assert (ai_list != NULL);
191 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
193 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
194 ai_ptr->ai_protocol);
198 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
209 freeaddrinfo (ai_list);
214 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
215 "The last error was: %s", node, service,
216 sstrerror (errno, errbuf, sizeof (errbuf)));
221 wg_reset_buffer (cb);
226 static void wg_callback_free (void *data)
228 struct wg_callback *cb;
235 wg_flush_nolock (/* timeout = */ 0, cb);
246 static int wg_flush (cdtime_t timeout,
247 const char *identifier __attribute__((unused)),
248 user_data_t *user_data)
250 struct wg_callback *cb;
253 if (user_data == NULL)
256 cb = user_data->data;
258 pthread_mutex_lock (&cb->send_lock);
262 status = wg_callback_init (cb);
265 ERROR ("write_graphite plugin: wg_callback_init failed.");
266 pthread_mutex_unlock (&cb->send_lock);
271 status = wg_flush_nolock (timeout, cb);
272 pthread_mutex_unlock (&cb->send_lock);
277 static int wg_format_values (char *ret, size_t ret_len,
278 int ds_num, const data_set_t *ds, const value_list_t *vl,
283 gauge_t *rates = NULL;
285 assert (0 == strcmp (ds->type, vl->type));
287 memset (ret, 0, ret_len);
289 #define BUFFER_ADD(...) do { \
290 status = ssnprintf (ret + offset, ret_len - offset, \
297 else if (((size_t) status) >= (ret_len - offset)) \
303 offset += ((size_t) status); \
306 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
307 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
308 else if (store_rates)
311 rates = uc_get_rate (ds, vl);
314 WARNING ("format_values: "
315 "uc_get_rate failed.");
318 BUFFER_ADD ("%g", rates[ds_num]);
320 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
321 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
322 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
323 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
324 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
325 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
328 ERROR ("format_values plugin: Unknown data source type: %i",
329 ds->ds[ds_num].type);
340 static void swap_chars (char *dst, const char *src,
341 const char from, const char to)
345 for (i = 0; i < strlen(src) ; i++)
355 static int wg_format_name (char *ret, int ret_len,
356 const char *hostname,
357 const char *plugin, const char *plugin_instance,
358 const char *type, const char *type_instance,
359 const char *prefix, const char *postfix,
360 const char *ds_name, const char dotchar)
363 char *n_hostname = NULL;
364 char *n_plugin = NULL;
365 char *n_plugin_instance = NULL;
367 char *n_type_instance = NULL;
369 assert (plugin != NULL);
370 assert (type != NULL);
378 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
380 ERROR ("Unable to allocate memory for normalized hostname buffer");
384 if ((n_plugin = malloc(strlen(plugin)+1)) == NULL)
386 ERROR ("Unable to allocate memory for normalized plugin buffer");
390 if ((n_type = malloc(strlen(type)+1)) == NULL)
392 ERROR ("Unable to allocate memory for normalized type buffer");
396 swap_chars(n_hostname, hostname, '.', dotchar);
397 swap_chars(n_plugin, plugin, ' ', '_');
398 swap_chars(n_type, type, ' ', '_');
400 if (type_instance != NULL && type_instance[0] != '\0')
402 if ((n_type_instance = malloc(strlen(type_instance)+1)) == NULL)
404 ERROR ("Unable to allocate memory for normalized datasource name buffer");
407 swap_chars(n_type_instance, type_instance, '.', dotchar);
408 swap_chars(n_type_instance, type_instance, ' ', '_');
411 if (plugin_instance != NULL && plugin_instance[0] != '\0')
413 if ((n_plugin_instance = malloc(strlen(plugin_instance)+1)) == NULL)
415 ERROR ("Unable to allocate memory for normalized plugin instance buffer");
418 swap_chars(n_plugin_instance, plugin_instance, ' ', '_');
421 if ((n_plugin_instance == NULL) || (n_plugin_instance[0] == '\0'))
423 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
425 if ((ds_name == NULL) || (ds_name[0] == '\0'))
426 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
427 prefix, n_hostname, postfix, n_plugin, n_type);
429 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
430 prefix, n_hostname, postfix, n_plugin, n_type, ds_name);
434 if ((ds_name == NULL) || (ds_name[0] == '\0'))
435 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s",
436 prefix, n_hostname, postfix, n_plugin, n_type,
439 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
440 prefix, n_hostname, postfix, n_plugin, n_type,
441 n_type_instance, ds_name);
446 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
448 if ((ds_name == NULL) || (ds_name[0] == '\0'))
449 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
450 prefix, n_hostname, postfix, n_plugin,
451 n_plugin_instance, n_type);
453 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
454 prefix, n_hostname, postfix, n_plugin,
455 n_plugin_instance, n_type, ds_name);
459 if ((ds_name == NULL) || (ds_name[0] == '\0'))
460 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s",
461 prefix, n_hostname, postfix, n_plugin,
462 n_plugin_instance, n_type, n_type_instance);
464 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
465 prefix, n_hostname, postfix, n_plugin,
466 n_plugin_instance, n_type, n_type_instance, ds_name);
471 sfree(n_type_instance);
474 sfree(n_plugin_instance);
476 if ((status < 1) || (status >= ret_len))
481 static int wg_send_message (const char* key, const char* value,
482 cdtime_t time, struct wg_callback *cb)
488 message_len = (size_t) ssnprintf (message, sizeof (message),
492 CDTIME_T_TO_DOUBLE(time));
493 if (message_len >= sizeof (message)) {
494 ERROR ("write_graphite plugin: message buffer too small: "
495 "Need %zu bytes.", message_len + 1);
500 pthread_mutex_lock (&cb->send_lock);
504 status = wg_callback_init (cb);
507 ERROR ("write_graphite plugin: wg_callback_init failed.");
508 pthread_mutex_unlock (&cb->send_lock);
513 if (message_len >= cb->send_buf_free)
515 status = wg_flush_nolock (/* timeout = */ 0, cb);
518 pthread_mutex_unlock (&cb->send_lock);
522 assert (message_len < cb->send_buf_free);
524 /* `message_len + 1' because `message_len' does not include the
525 * trailing null byte. Neither does `send_buffer_fill'. */
526 memcpy (cb->send_buf + cb->send_buf_fill,
527 message, message_len + 1);
528 cb->send_buf_fill += message_len;
529 cb->send_buf_free -= message_len;
531 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
534 cb->send_buf_fill, sizeof (cb->send_buf),
535 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
538 /* Check if we have enough space for this message. */
539 pthread_mutex_unlock (&cb->send_lock);
544 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
545 struct wg_callback *cb)
547 char key[10*DATA_MAX_NAME_LEN];
552 if (0 != strcmp (ds->type, vl->type))
554 ERROR ("write_graphite plugin: DS type does not match "
561 for (i = 0; i < ds->ds_num; i++)
563 /* Copy the identifier to `key' and escape it. */
564 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
567 ERROR ("write_graphite plugin: error with format_name");
571 escape_string (key, sizeof (key));
572 /* Convert the values to an ASCII representation and put that
574 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
577 ERROR ("write_graphite plugin: error with "
582 /* Send the message to graphite */
583 status = wg_send_message (key, values, vl->time, cb);
586 ERROR ("write_graphite plugin: error with "
594 /* Copy the identifier to `key' and escape it. */
595 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
598 ERROR ("write_graphite plugin: error with format_name");
602 escape_string (key, sizeof (key));
603 /* Convert the values to an ASCII representation and put that into
605 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
608 ERROR ("write_graphite plugin: error with "
613 /* Send the message to graphite */
614 status = wg_send_message (key, values, vl->time, cb);
617 ERROR ("write_graphite plugin: error with "
626 static int wg_write (const data_set_t *ds, const value_list_t *vl,
627 user_data_t *user_data)
629 struct wg_callback *cb;
632 if (user_data == NULL)
635 cb = user_data->data;
637 status = wg_write_messages (ds, vl, cb);
642 static int config_set_char (char *dest,
645 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
647 WARNING ("write_graphite plugin: The `%s' config option "
648 "needs exactly one string argument.", ci->key);
652 *dest = ci->values[0].value.string[0];
657 static int wg_config_carbon (oconfig_item_t *ci)
659 struct wg_callback *cb;
660 user_data_t user_data;
663 cb = malloc (sizeof (*cb));
666 ERROR ("write_graphite plugin: malloc failed.");
669 memset (cb, 0, sizeof (*cb));
678 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
680 for (i = 0; i < ci->children_num; i++)
682 oconfig_item_t *child = ci->children + i;
684 if (strcasecmp ("Host", child->key) == 0)
685 cf_util_get_string (child, &cb->node);
686 else if (strcasecmp ("Port", child->key) == 0)
687 cf_util_get_string (child, &cb->service);
688 else if (strcasecmp ("Prefix", child->key) == 0)
689 cf_util_get_string (child, &cb->prefix);
690 else if (strcasecmp ("Postfix", child->key) == 0)
691 cf_util_get_string (child, &cb->postfix);
692 else if (strcasecmp ("DotCharacter", child->key) == 0)
693 config_set_char (&cb->dotchar, child);
696 ERROR ("write_graphite plugin: Invalid configuration "
697 "option: %s.", child->key);
701 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
702 cb->node ? cb->node : WG_DEFAULT_NODE,
703 cb->service ? cb->service : WG_DEFAULT_SERVICE);
705 memset (&user_data, 0, sizeof (user_data));
707 user_data.free_func = NULL;
708 plugin_register_flush ("write_graphite", wg_flush, &user_data);
710 user_data.free_func = wg_callback_free;
711 plugin_register_write ("write_graphite", wg_write, &user_data);
716 static int wg_config (oconfig_item_t *ci)
720 for (i = 0; i < ci->children_num; i++)
722 oconfig_item_t *child = ci->children + i;
724 if (strcasecmp ("Carbon", child->key) == 0)
725 wg_config_carbon (child);
728 ERROR ("write_graphite plugin: Invalid configuration "
729 "option: %s.", child->key);
736 void module_register (void)
738 plugin_register_complex_config ("write_graphite", wg_config);
741 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */