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>
48 #include <sys/types.h>
50 #include <netinet/in.h>
53 #ifndef WG_FORMAT_NAME
54 #define WG_FORMAT_NAME(ret, ret_len, vl, cb, name) \
55 wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, \
56 (vl)->plugin_instance, (vl)->type, \
57 (vl)->type_instance, (cb)->prefix, (cb)->postfix, \
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_SEND_BUF_SIZE
70 # define WG_SEND_BUF_SIZE 4096
79 struct hostent *server;
87 char send_buf[WG_SEND_BUF_SIZE];
90 cdtime_t send_buf_init_time;
92 pthread_mutex_t send_lock;
99 static void wg_reset_buffer (struct wg_callback *cb)
101 memset (cb->send_buf, 0, sizeof (cb->send_buf));
102 cb->send_buf_free = sizeof (cb->send_buf);
103 cb->send_buf_fill = 0;
104 cb->send_buf_init_time = cdtime ();
107 static int wg_send_buffer (struct wg_callback *cb)
111 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
114 ERROR ("write_graphite plugin: send failed with "
119 pthread_mutex_trylock (&cb->send_lock);
121 DEBUG ("write_graphite plugin: closing socket and restting fd "
122 "so reinit will occur");
126 pthread_mutex_unlock (&cb->send_lock);
133 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
137 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
138 "send_buf_fill = %zu;",
142 /* timeout == 0 => flush unconditionally */
148 if ((cb->send_buf_init_time + timeout) > now)
152 if (cb->send_buf_fill <= 0)
154 cb->send_buf_init_time = cdtime ();
158 status = wg_send_buffer (cb);
159 wg_reset_buffer (cb);
164 static int wg_callback_init (struct wg_callback *cb)
166 struct addrinfo ai_hints;
167 struct addrinfo *ai_list;
168 struct addrinfo *ai_ptr;
171 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
172 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
177 memset (&ai_hints, 0, sizeof (ai_hints));
179 ai_hints.ai_flags |= AI_ADDRCONFIG;
181 ai_hints.ai_family = AF_UNSPEC;
182 ai_hints.ai_socktype = SOCK_STREAM;
186 status = getaddrinfo (node, service, &ai_hints, &ai_list);
189 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
190 node, service, gai_strerror (status));
194 assert (ai_list != NULL);
195 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
197 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
198 ai_ptr->ai_protocol);
202 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
213 freeaddrinfo (ai_list);
218 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
219 "The last error was: %s", node, service,
220 sstrerror (errno, errbuf, sizeof (errbuf)));
225 wg_reset_buffer (cb);
230 static void wg_callback_free (void *data)
232 struct wg_callback *cb;
239 wg_flush_nolock (/* timeout = */ 0, cb);
250 static int wg_flush (cdtime_t timeout,
251 const char *identifier __attribute__((unused)),
252 user_data_t *user_data)
254 struct wg_callback *cb;
257 if (user_data == NULL)
260 cb = user_data->data;
262 pthread_mutex_lock (&cb->send_lock);
266 status = wg_callback_init (cb);
269 ERROR ("write_graphite plugin: wg_callback_init failed.");
270 pthread_mutex_unlock (&cb->send_lock);
275 status = wg_flush_nolock (timeout, cb);
276 pthread_mutex_unlock (&cb->send_lock);
281 static int wg_format_values (char *ret, size_t ret_len,
282 int ds_num, const data_set_t *ds, const value_list_t *vl,
287 gauge_t *rates = NULL;
289 assert (0 == strcmp (ds->type, vl->type));
291 memset (ret, 0, ret_len);
293 #define BUFFER_ADD(...) do { \
294 status = ssnprintf (ret + offset, ret_len - offset, \
301 else if (((size_t) status) >= (ret_len - offset)) \
307 offset += ((size_t) status); \
310 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
311 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
312 else if (store_rates)
315 rates = uc_get_rate (ds, vl);
318 WARNING ("format_values: "
319 "uc_get_rate failed.");
322 BUFFER_ADD ("%g", rates[ds_num]);
324 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
325 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
326 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
327 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
328 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
329 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
332 ERROR ("format_values plugin: Unknown data source type: %i",
333 ds->ds[ds_num].type);
344 static void swap_chars (char *dst, const char *src,
345 const char from, const char to)
349 for (i = 0; i < strlen(src) ; i++)
359 static int wg_format_name (char *ret, int ret_len,
360 const char *hostname,
361 const char *plugin, const char *plugin_instance,
362 const char *type, const char *type_instance,
363 const char *prefix, const char *postfix,
364 const char *ds_name, const char dotchar)
367 char *n_hostname = NULL;
368 char *n_plugin = NULL;
369 char *n_plugin_instance = NULL;
371 char *n_type_instance = NULL;
373 assert (plugin != NULL);
374 assert (type != NULL);
382 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
384 ERROR ("Unable to allocate memory for normalized hostname buffer");
388 if ((n_plugin = malloc(strlen(plugin)+1)) == NULL)
390 ERROR ("Unable to allocate memory for normalized plugin buffer");
394 if ((n_type = malloc(strlen(type)+1)) == NULL)
396 ERROR ("Unable to allocate memory for normalized type buffer");
400 swap_chars(n_hostname, hostname, '.', dotchar);
401 swap_chars(n_plugin, plugin, ' ', '_');
402 swap_chars(n_type, type, ' ', '_');
404 if (type_instance != NULL && type_instance[0] != '\0')
406 if ((n_type_instance = malloc(strlen(type_instance)+1)) == NULL)
408 ERROR ("Unable to allocate memory for normalized datasource name buffer");
411 swap_chars(n_type_instance, type_instance, '.', dotchar);
412 swap_chars(n_type_instance, type_instance, ' ', '_');
415 if (plugin_instance != NULL && plugin_instance[0] != '\0')
417 if ((n_plugin_instance = malloc(strlen(plugin_instance)+1)) == NULL)
419 ERROR ("Unable to allocate memory for normalized plugin instance buffer");
422 swap_chars(n_plugin_instance, plugin_instance, ' ', '_');
425 if ((n_plugin_instance == NULL) || (n_plugin_instance[0] == '\0'))
427 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
429 if ((ds_name == NULL) || (ds_name[0] == '\0'))
430 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
431 prefix, n_hostname, postfix, n_plugin, n_type);
433 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
434 prefix, n_hostname, postfix, n_plugin, n_type, ds_name);
438 if ((ds_name == NULL) || (ds_name[0] == '\0'))
439 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s",
440 prefix, n_hostname, postfix, n_plugin, n_type,
443 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
444 prefix, n_hostname, postfix, n_plugin, n_type,
445 n_type_instance, ds_name);
450 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
452 if ((ds_name == NULL) || (ds_name[0] == '\0'))
453 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
454 prefix, n_hostname, postfix, n_plugin,
455 n_plugin_instance, n_type);
457 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
458 prefix, n_hostname, postfix, n_plugin,
459 n_plugin_instance, n_type, ds_name);
463 if ((ds_name == NULL) || (ds_name[0] == '\0'))
464 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s",
465 prefix, n_hostname, postfix, n_plugin,
466 n_plugin_instance, n_type, n_type_instance);
468 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
469 prefix, n_hostname, postfix, n_plugin,
470 n_plugin_instance, n_type, n_type_instance, ds_name);
475 sfree(n_type_instance);
478 sfree(n_plugin_instance);
480 if ((status < 1) || (status >= ret_len))
485 static int wg_send_message (const char* key, const char* value,
486 cdtime_t time, struct wg_callback *cb)
492 message_len = (size_t) ssnprintf (message, sizeof (message),
496 CDTIME_T_TO_DOUBLE(time));
497 if (message_len >= sizeof (message)) {
498 ERROR ("write_graphite plugin: message buffer too small: "
499 "Need %zu bytes.", message_len + 1);
504 pthread_mutex_lock (&cb->send_lock);
508 status = wg_callback_init (cb);
511 ERROR ("write_graphite plugin: wg_callback_init failed.");
512 pthread_mutex_unlock (&cb->send_lock);
517 if (message_len >= cb->send_buf_free)
519 status = wg_flush_nolock (/* timeout = */ 0, cb);
522 pthread_mutex_unlock (&cb->send_lock);
526 assert (message_len < cb->send_buf_free);
528 /* `message_len + 1' because `message_len' does not include the
529 * trailing null byte. Neither does `send_buffer_fill'. */
530 memcpy (cb->send_buf + cb->send_buf_fill,
531 message, message_len + 1);
532 cb->send_buf_fill += message_len;
533 cb->send_buf_free -= message_len;
535 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
538 cb->send_buf_fill, sizeof (cb->send_buf),
539 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
542 /* Check if we have enough space for this message. */
543 pthread_mutex_unlock (&cb->send_lock);
548 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
549 struct wg_callback *cb)
551 char key[10*DATA_MAX_NAME_LEN];
556 if (0 != strcmp (ds->type, vl->type))
558 ERROR ("write_graphite plugin: DS type does not match "
565 for (i = 0; i < ds->ds_num; i++)
567 /* Copy the identifier to `key' and escape it. */
568 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
571 ERROR ("write_graphite plugin: error with format_name");
575 escape_string (key, sizeof (key));
576 /* Convert the values to an ASCII representation and put that
578 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
581 ERROR ("write_graphite plugin: error with "
586 /* Send the message to graphite */
587 status = wg_send_message (key, values, vl->time, cb);
590 ERROR ("write_graphite plugin: error with "
598 /* Copy the identifier to `key' and escape it. */
599 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
602 ERROR ("write_graphite plugin: error with format_name");
606 escape_string (key, sizeof (key));
607 /* Convert the values to an ASCII representation and put that into
609 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
612 ERROR ("write_graphite plugin: error with "
617 /* Send the message to graphite */
618 status = wg_send_message (key, values, vl->time, cb);
621 ERROR ("write_graphite plugin: error with "
630 static int wg_write (const data_set_t *ds, const value_list_t *vl,
631 user_data_t *user_data)
633 struct wg_callback *cb;
636 if (user_data == NULL)
639 cb = user_data->data;
641 status = wg_write_messages (ds, vl, cb);
646 static int config_set_char (char *dest,
649 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
651 WARNING ("write_graphite plugin: The `%s' config option "
652 "needs exactly one string argument.", ci->key);
656 *dest = ci->values[0].value.string[0];
661 static int wg_config_carbon (oconfig_item_t *ci)
663 struct wg_callback *cb;
664 user_data_t user_data;
667 cb = malloc (sizeof (*cb));
670 ERROR ("write_graphite plugin: malloc failed.");
673 memset (cb, 0, sizeof (*cb));
682 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
684 for (i = 0; i < ci->children_num; i++)
686 oconfig_item_t *child = ci->children + i;
688 if (strcasecmp ("Host", child->key) == 0)
689 cf_util_get_string (child, &cb->node);
690 else if (strcasecmp ("Port", child->key) == 0)
691 cf_util_get_string (child, &cb->service);
692 else if (strcasecmp ("Prefix", child->key) == 0)
693 cf_util_get_string (child, &cb->prefix);
694 else if (strcasecmp ("Postfix", child->key) == 0)
695 cf_util_get_string (child, &cb->postfix);
696 else if (strcasecmp ("DotCharacter", child->key) == 0)
697 config_set_char (&cb->dotchar, child);
700 ERROR ("write_graphite plugin: Invalid configuration "
701 "option: %s.", child->key);
705 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
706 cb->node ? cb->node : WG_DEFAULT_NODE,
707 cb->service ? cb->service : WG_DEFAULT_SERVICE);
709 memset (&user_data, 0, sizeof (user_data));
711 user_data.free_func = NULL;
712 plugin_register_flush ("write_graphite", wg_flush, &user_data);
714 user_data.free_func = wg_callback_free;
715 plugin_register_write ("write_graphite", wg_write, &user_data);
720 static int wg_config (oconfig_item_t *ci)
724 for (i = 0; i < ci->children_num; i++)
726 oconfig_item_t *child = ci->children + i;
728 if (strcasecmp ("Carbon", child->key) == 0)
729 wg_config_carbon (child);
732 ERROR ("write_graphite plugin: Invalid configuration "
733 "option: %s.", child->key);
740 void module_register (void)
742 plugin_register_complex_config ("write_graphite", wg_config);
745 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */