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, prefix, name) \
55 wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, \
56 (vl)->plugin_instance, (vl)->type, \
57 (vl)->type_instance, prefix, name)
60 #ifndef WG_SEND_BUF_SIZE
61 #define WG_SEND_BUF_SIZE 4096
70 struct hostent *server;
76 char send_buf[WG_SEND_BUF_SIZE];
79 cdtime_t send_buf_init_time;
81 pthread_mutex_t send_lock;
88 static void wg_reset_buffer (struct wg_callback *cb)
90 memset (cb->send_buf, 0, sizeof (cb->send_buf));
91 cb->send_buf_free = sizeof (cb->send_buf);
92 cb->send_buf_fill = 0;
93 cb->send_buf_init_time = cdtime ();
96 static int wg_send_buffer (struct wg_callback *cb)
100 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
103 ERROR ("write_graphite plugin: send failed with "
108 pthread_mutex_lock (&cb->send_lock);
110 DEBUG ("write_graphite plugin: closing socket and restting fd "
111 "so reinit will occur");
115 pthread_mutex_unlock (&cb->send_lock);
122 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
126 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
127 "send_buf_fill = %zu;",
131 /* timeout == 0 => flush unconditionally */
137 if ((cb->send_buf_init_time + timeout) > now)
141 if (cb->send_buf_fill <= 0)
143 cb->send_buf_init_time = cdtime ();
147 status = wg_send_buffer (cb);
148 wg_reset_buffer (cb);
153 static int wg_callback_init (struct wg_callback *cb)
157 struct sockaddr_in serv_addr;
162 cb->sock_fd = socket (AF_INET, SOCK_STREAM, 0);
165 ERROR ("write_graphite plugin: socket failed: %s", strerror (errno));
168 cb->server = gethostbyname(cb->host);
169 if (cb->server == NULL)
171 ERROR ("write_graphite plugin: no such host");
174 memset (&serv_addr, 0, sizeof (serv_addr));
175 serv_addr.sin_family = AF_INET;
176 memcpy (&serv_addr.sin_addr.s_addr,
178 cb->server->h_length);
179 serv_addr.sin_port = htons(cb->port);
181 status = connect(cb->sock_fd,
182 (struct sockaddr *) &serv_addr,
187 sstrerror (errno, errbuf, sizeof (errbuf));
188 ERROR ("write_graphite plugin: connect failed: %s", errbuf);
194 wg_reset_buffer (cb);
199 static void wg_callback_free (void *data)
201 struct wg_callback *cb;
208 wg_flush_nolock (/* timeout = */ 0, cb);
217 static int wg_flush (cdtime_t timeout,
218 const char *identifier __attribute__((unused)),
219 user_data_t *user_data)
221 struct wg_callback *cb;
224 if (user_data == NULL)
227 cb = user_data->data;
229 pthread_mutex_lock (&cb->send_lock);
233 status = wg_callback_init (cb);
236 ERROR ("write_graphite plugin: wg_callback_init failed.");
237 pthread_mutex_unlock (&cb->send_lock);
242 status = wg_flush_nolock (timeout, cb);
243 pthread_mutex_unlock (&cb->send_lock);
248 static int wg_format_values (char *ret, size_t ret_len,
249 int ds_num, const data_set_t *ds, const value_list_t *vl,
254 gauge_t *rates = NULL;
256 assert (0 == strcmp (ds->type, vl->type));
258 memset (ret, 0, ret_len);
260 #define BUFFER_ADD(...) do { \
261 status = ssnprintf (ret + offset, ret_len - offset, \
268 else if (((size_t) status) >= (ret_len - offset)) \
274 offset += ((size_t) status); \
277 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
278 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
279 else if (store_rates)
282 rates = uc_get_rate (ds, vl);
285 WARNING ("format_values: "
286 "uc_get_rate failed.");
289 BUFFER_ADD ("%g", rates[ds_num]);
291 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
292 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
293 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
294 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
295 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
296 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
299 ERROR ("format_values plugin: Unknown data source type: %i",
300 ds->ds[ds_num].type);
311 static int mangle_dots (char *dst, const char *src)
317 for (i = 0; i < strlen(src) ; i++)
332 static int wg_format_name (char *ret, int ret_len,
333 const char *hostname,
334 const char *plugin, const char *plugin_instance,
335 const char *type, const char *type_instance,
336 const char *prefix, const char *ds_name)
339 char *n_hostname = 0;
340 char *n_type_instance = 0;
342 assert (plugin != NULL);
343 assert (type != NULL);
345 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
347 ERROR ("Unable to allocate memory for normalized hostname buffer");
351 if (mangle_dots(n_hostname, hostname) == -1)
353 ERROR ("Unable to normalize hostname");
357 if (type_instance && type_instance[0] != '\0') {
358 if ((n_type_instance = malloc(strlen(type_instance)+1)) == NULL)
360 ERROR ("Unable to allocate memory for normalized datasource name buffer");
363 if (mangle_dots(n_type_instance, type_instance) == -1)
365 ERROR ("Unable to normalize datasource name");
370 if ((plugin_instance == NULL) || (plugin_instance[0] == '\0'))
372 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
374 if ((ds_name == NULL) || (ds_name[0] == '\0'))
375 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s",
376 prefix, n_hostname, plugin, type);
378 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
379 prefix, n_hostname, plugin, type, ds_name);
383 if ((ds_name == NULL) || (ds_name[0] == '\0'))
384 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s",
385 prefix, n_hostname, plugin, type,
388 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s.%s",
389 prefix, n_hostname, plugin, type,
390 n_type_instance, ds_name);
395 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
397 if ((ds_name == NULL) || (ds_name[0] == '\0'))
398 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
399 prefix, n_hostname, plugin,
400 plugin_instance, type);
402 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s.%s",
403 prefix, n_hostname, plugin,
404 plugin_instance, type, ds_name);
408 if ((ds_name == NULL) || (ds_name[0] == '\0'))
409 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s",
410 prefix, n_hostname, plugin,
411 plugin_instance, type, n_type_instance);
413 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s.%s",
414 prefix, n_hostname, plugin,
415 plugin_instance, type, n_type_instance, ds_name);
420 sfree(n_type_instance);
422 if ((status < 1) || (status >= ret_len))
427 static int wg_send_message (const char* key, const char* value,
428 cdtime_t time, struct wg_callback *cb)
434 message_len = (size_t) ssnprintf (message, sizeof (message),
438 CDTIME_T_TO_DOUBLE(time));
439 if (message_len >= sizeof (message)) {
440 ERROR ("write_graphite plugin: message buffer too small: "
441 "Need %zu bytes.", message_len + 1);
446 pthread_mutex_lock (&cb->send_lock);
450 status = wg_callback_init (cb);
453 ERROR ("write_graphite plugin: wg_callback_init failed.");
454 pthread_mutex_unlock (&cb->send_lock);
459 if (message_len >= cb->send_buf_free)
461 status = wg_flush_nolock (/* timeout = */ 0, cb);
464 pthread_mutex_unlock (&cb->send_lock);
468 assert (message_len < cb->send_buf_free);
470 /* `message_len + 1' because `message_len' does not include the
471 * trailing null byte. Neither does `send_buffer_fill'. */
472 memcpy (cb->send_buf + cb->send_buf_fill,
473 message, message_len + 1);
474 cb->send_buf_fill += message_len;
475 cb->send_buf_free -= message_len;
477 DEBUG ("write_graphite plugin: <%s:%d> buf %zu/%zu (%g%%) \"%s\"",
480 cb->send_buf_fill, sizeof (cb->send_buf),
481 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
484 /* Check if we have enough space for this message. */
485 pthread_mutex_unlock (&cb->send_lock);
490 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
491 struct wg_callback *cb)
493 char key[10*DATA_MAX_NAME_LEN];
498 if (0 != strcmp (ds->type, vl->type))
500 ERROR ("write_graphite plugin: DS type does not match "
507 for (i = 0; i < ds->ds_num; i++)
509 /* Copy the identifier to `key' and escape it. */
510 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, \
514 ERROR ("write_graphite plugin: error with format_name");
518 escape_string (key, sizeof (key));
519 /* Convert the values to an ASCII representation and put that
521 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
524 ERROR ("write_graphite plugin: error with "
529 /* Send the message to graphite */
530 status = wg_send_message (key, values, vl->time, cb);
533 ERROR ("write_graphite plugin: error with "
541 /* Copy the identifier to `key' and escape it. */
542 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, NULL);
545 ERROR ("write_graphite plugin: error with format_name");
549 escape_string (key, sizeof (key));
550 /* Convert the values to an ASCII representation and put that into
552 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
555 ERROR ("write_graphite plugin: error with "
560 /* Send the message to graphite */
561 status = wg_send_message (key, values, vl->time, cb);
564 ERROR ("write_graphite plugin: error with "
573 static int wg_write (const data_set_t *ds, const value_list_t *vl,
574 user_data_t *user_data)
576 struct wg_callback *cb;
579 if (user_data == NULL)
582 cb = user_data->data;
584 status = wg_write_messages (ds, vl, cb);
589 static int config_set_number (int *dest,
592 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
594 WARNING ("write_graphite plugin: The `%s' config option "
595 "needs exactly one numeric argument.", ci->key);
599 *dest = ci->values[0].value.number;
604 static int config_set_string (char **ret_string,
609 if ((ci->values_num != 1)
610 || (ci->values[0].type != OCONFIG_TYPE_STRING))
612 WARNING ("write_graphite plugin: The `%s' config option "
613 "needs exactly one string argument.", ci->key);
617 string = strdup (ci->values[0].value.string);
620 ERROR ("write_graphite plugin: strdup failed.");
624 if (*ret_string != NULL)
626 *ret_string = string;
631 static int wg_config_carbon (oconfig_item_t *ci)
633 struct wg_callback *cb;
634 user_data_t user_data;
637 cb = malloc (sizeof (*cb));
640 ERROR ("write_graphite plugin: malloc failed.");
643 memset (cb, 0, sizeof (*cb));
650 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
652 for (i = 0; i < ci->children_num; i++)
654 oconfig_item_t *child = ci->children + i;
656 if (strcasecmp ("Host", child->key) == 0)
657 config_set_string (&cb->host, child);
658 else if (strcasecmp ("Port", child->key) == 0)
659 config_set_number (&cb->port, child);
660 else if (strcasecmp ("Prefix", child->key) == 0)
661 config_set_string (&cb->prefix, child);
664 ERROR ("write_graphite plugin: Invalid configuration "
665 "option: %s.", child->key);
669 DEBUG ("write_graphite: Registering write callback to carbon agent "
670 "%s:%d", cb->host, cb->port);
672 memset (&user_data, 0, sizeof (user_data));
674 user_data.free_func = NULL;
675 plugin_register_flush ("write_graphite", wg_flush, &user_data);
677 user_data.free_func = wg_callback_free;
678 plugin_register_write ("write_graphite", wg_write, &user_data);
683 static int wg_config (oconfig_item_t *ci)
687 for (i = 0; i < ci->children_num; i++)
689 oconfig_item_t *child = ci->children + i;
691 if (strcasecmp ("Carbon", child->key) == 0)
692 wg_config_carbon (child);
695 ERROR ("write_graphite plugin: Invalid configuration "
696 "option: %s.", child->key);
703 void module_register (void)
705 plugin_register_complex_config ("write_graphite", wg_config);
708 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */