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_SEND_BUF_SIZE
62 #define WG_SEND_BUF_SIZE 4096
71 struct hostent *server;
79 char send_buf[WG_SEND_BUF_SIZE];
82 cdtime_t send_buf_init_time;
84 pthread_mutex_t send_lock;
91 static void wg_reset_buffer (struct wg_callback *cb)
93 memset (cb->send_buf, 0, sizeof (cb->send_buf));
94 cb->send_buf_free = sizeof (cb->send_buf);
95 cb->send_buf_fill = 0;
96 cb->send_buf_init_time = cdtime ();
99 static int wg_send_buffer (struct wg_callback *cb)
103 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
106 ERROR ("write_graphite plugin: send failed with "
111 pthread_mutex_lock (&cb->send_lock);
113 DEBUG ("write_graphite plugin: closing socket and restting fd "
114 "so reinit will occur");
118 pthread_mutex_unlock (&cb->send_lock);
125 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
129 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
130 "send_buf_fill = %zu;",
134 /* timeout == 0 => flush unconditionally */
140 if ((cb->send_buf_init_time + timeout) > now)
144 if (cb->send_buf_fill <= 0)
146 cb->send_buf_init_time = cdtime ();
150 status = wg_send_buffer (cb);
151 wg_reset_buffer (cb);
156 static int wg_callback_init (struct wg_callback *cb)
160 struct sockaddr_in serv_addr;
165 cb->sock_fd = socket (AF_INET, SOCK_STREAM, 0);
168 ERROR ("write_graphite plugin: socket failed: %s", strerror (errno));
171 cb->server = gethostbyname(cb->host);
172 if (cb->server == NULL)
174 ERROR ("write_graphite plugin: no such host");
177 memset (&serv_addr, 0, sizeof (serv_addr));
178 serv_addr.sin_family = AF_INET;
179 memcpy (&serv_addr.sin_addr.s_addr,
181 cb->server->h_length);
182 serv_addr.sin_port = htons(cb->port);
184 status = connect(cb->sock_fd,
185 (struct sockaddr *) &serv_addr,
190 sstrerror (errno, errbuf, sizeof (errbuf));
191 ERROR ("write_graphite plugin: connect failed: %s", errbuf);
197 wg_reset_buffer (cb);
202 static void wg_callback_free (void *data)
204 struct wg_callback *cb;
211 wg_flush_nolock (/* timeout = */ 0, cb);
221 static int wg_flush (cdtime_t timeout,
222 const char *identifier __attribute__((unused)),
223 user_data_t *user_data)
225 struct wg_callback *cb;
228 if (user_data == NULL)
231 cb = user_data->data;
233 pthread_mutex_lock (&cb->send_lock);
237 status = wg_callback_init (cb);
240 ERROR ("write_graphite plugin: wg_callback_init failed.");
241 pthread_mutex_unlock (&cb->send_lock);
246 status = wg_flush_nolock (timeout, cb);
247 pthread_mutex_unlock (&cb->send_lock);
252 static int wg_format_values (char *ret, size_t ret_len,
253 int ds_num, const data_set_t *ds, const value_list_t *vl,
258 gauge_t *rates = NULL;
260 assert (0 == strcmp (ds->type, vl->type));
262 memset (ret, 0, ret_len);
264 #define BUFFER_ADD(...) do { \
265 status = ssnprintf (ret + offset, ret_len - offset, \
272 else if (((size_t) status) >= (ret_len - offset)) \
278 offset += ((size_t) status); \
281 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
282 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
283 else if (store_rates)
286 rates = uc_get_rate (ds, vl);
289 WARNING ("format_values: "
290 "uc_get_rate failed.");
293 BUFFER_ADD ("%g", rates[ds_num]);
295 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
296 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
297 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
298 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
299 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
300 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
303 ERROR ("format_values plugin: Unknown data source type: %i",
304 ds->ds[ds_num].type);
315 static int swap_chars (char *dst, const char *src,
316 const char from, const char to)
322 for (i = 0; i < strlen(src) ; i++)
337 static int wg_format_name (char *ret, int ret_len,
338 const char *hostname,
339 const char *plugin, const char *plugin_instance,
340 const char *type, const char *type_instance,
341 const char *prefix, const char *postfix,
342 const char *ds_name, const char dotchar)
345 char *n_hostname = 0;
346 char *n_type_instance = 0;
348 assert (plugin != NULL);
349 assert (type != NULL);
351 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
353 ERROR ("Unable to allocate memory for normalized hostname buffer");
357 if (swap_chars(n_hostname, hostname, '.', dotchar) == -1)
359 ERROR ("Unable to normalize hostname");
363 if (type_instance && type_instance[0] != '\0') {
364 if ((n_type_instance = malloc(strlen(type_instance)+1)) == NULL)
366 ERROR ("Unable to allocate memory for normalized datasource name buffer");
369 if (swap_chars(n_type_instance, type_instance, '.', dotchar) == -1)
371 ERROR ("Unable to normalize datasource name");
376 if ((plugin_instance == NULL) || (plugin_instance[0] == '\0'))
378 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
380 if ((ds_name == NULL) || (ds_name[0] == '\0'))
381 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
382 prefix, n_hostname, postfix, plugin, type);
384 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
385 prefix, n_hostname, postfix, plugin, type, ds_name);
389 if ((ds_name == NULL) || (ds_name[0] == '\0'))
390 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s",
391 prefix, n_hostname, postfix, plugin, type,
394 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
395 prefix, n_hostname, postfix, plugin, type,
396 n_type_instance, ds_name);
401 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
403 if ((ds_name == NULL) || (ds_name[0] == '\0'))
404 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
405 prefix, n_hostname, postfix, plugin,
406 plugin_instance, type);
408 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
409 prefix, n_hostname, postfix, plugin,
410 plugin_instance, type, ds_name);
414 if ((ds_name == NULL) || (ds_name[0] == '\0'))
415 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s",
416 prefix, n_hostname, postfix, plugin,
417 plugin_instance, type, n_type_instance);
419 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
420 prefix, n_hostname, postfix, plugin,
421 plugin_instance, type, n_type_instance, ds_name);
426 sfree(n_type_instance);
428 if ((status < 1) || (status >= ret_len))
433 static int wg_send_message (const char* key, const char* value,
434 cdtime_t time, struct wg_callback *cb)
440 message_len = (size_t) ssnprintf (message, sizeof (message),
444 CDTIME_T_TO_DOUBLE(time));
445 if (message_len >= sizeof (message)) {
446 ERROR ("write_graphite plugin: message buffer too small: "
447 "Need %zu bytes.", message_len + 1);
452 pthread_mutex_lock (&cb->send_lock);
456 status = wg_callback_init (cb);
459 ERROR ("write_graphite plugin: wg_callback_init failed.");
460 pthread_mutex_unlock (&cb->send_lock);
465 if (message_len >= cb->send_buf_free)
467 status = wg_flush_nolock (/* timeout = */ 0, cb);
470 pthread_mutex_unlock (&cb->send_lock);
474 assert (message_len < cb->send_buf_free);
476 /* `message_len + 1' because `message_len' does not include the
477 * trailing null byte. Neither does `send_buffer_fill'. */
478 memcpy (cb->send_buf + cb->send_buf_fill,
479 message, message_len + 1);
480 cb->send_buf_fill += message_len;
481 cb->send_buf_free -= message_len;
483 DEBUG ("write_graphite plugin: <%s:%d> buf %zu/%zu (%g%%) \"%s\"",
486 cb->send_buf_fill, sizeof (cb->send_buf),
487 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
490 /* Check if we have enough space for this message. */
491 pthread_mutex_unlock (&cb->send_lock);
496 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
497 struct wg_callback *cb)
499 char key[10*DATA_MAX_NAME_LEN];
504 if (0 != strcmp (ds->type, vl->type))
506 ERROR ("write_graphite plugin: DS type does not match "
513 for (i = 0; i < ds->ds_num; i++)
515 /* Copy the identifier to `key' and escape it. */
516 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
519 ERROR ("write_graphite plugin: error with format_name");
523 escape_string (key, sizeof (key));
524 /* Convert the values to an ASCII representation and put that
526 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
529 ERROR ("write_graphite plugin: error with "
534 /* Send the message to graphite */
535 status = wg_send_message (key, values, vl->time, cb);
538 ERROR ("write_graphite plugin: error with "
546 /* Copy the identifier to `key' and escape it. */
547 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
550 ERROR ("write_graphite plugin: error with format_name");
554 escape_string (key, sizeof (key));
555 /* Convert the values to an ASCII representation and put that into
557 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
560 ERROR ("write_graphite plugin: error with "
565 /* Send the message to graphite */
566 status = wg_send_message (key, values, vl->time, cb);
569 ERROR ("write_graphite plugin: error with "
578 static int wg_write (const data_set_t *ds, const value_list_t *vl,
579 user_data_t *user_data)
581 struct wg_callback *cb;
584 if (user_data == NULL)
587 cb = user_data->data;
589 status = wg_write_messages (ds, vl, cb);
594 static int config_set_number (int *dest,
597 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
599 WARNING ("write_graphite plugin: The `%s' config option "
600 "needs exactly one numeric argument.", ci->key);
604 *dest = ci->values[0].value.number;
609 static int config_set_char (char *dest,
612 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
614 WARNING ("write_graphite plugin: The `%s' config option "
615 "needs exactly one string argument.", ci->key);
619 *dest = ci->values[0].value.string[0];
624 static int config_set_string (char **ret_string,
629 if ((ci->values_num != 1)
630 || (ci->values[0].type != OCONFIG_TYPE_STRING))
632 WARNING ("write_graphite plugin: The `%s' config option "
633 "needs exactly one string argument.", ci->key);
637 string = strdup (ci->values[0].value.string);
640 ERROR ("write_graphite plugin: strdup failed.");
644 if (*ret_string != NULL)
646 *ret_string = string;
651 static int wg_config_carbon (oconfig_item_t *ci)
653 struct wg_callback *cb;
654 user_data_t user_data;
657 cb = malloc (sizeof (*cb));
660 ERROR ("write_graphite plugin: malloc failed.");
663 memset (cb, 0, sizeof (*cb));
672 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
674 for (i = 0; i < ci->children_num; i++)
676 oconfig_item_t *child = ci->children + i;
678 if (strcasecmp ("Host", child->key) == 0)
679 config_set_string (&cb->host, child);
680 else if (strcasecmp ("Port", child->key) == 0)
681 config_set_number (&cb->port, child);
682 else if (strcasecmp ("Prefix", child->key) == 0)
683 config_set_string (&cb->prefix, child);
684 else if (strcasecmp ("Postfix", child->key) == 0)
685 config_set_string (&cb->postfix, child);
686 else if (strcasecmp ("DotCharacter", child->key) == 0)
687 config_set_char (&cb->dotchar, child);
690 ERROR ("write_graphite plugin: Invalid configuration "
691 "option: %s.", child->key);
695 if (cb->prefix == NULL) {
696 if ((cb->prefix = malloc((int)sizeof(char))) == NULL)
698 ERROR ("Unable to allocate memory for hostname prefix buffer");
701 cb->postfix[0] = '\0';
704 if (cb->postfix == NULL) {
705 if ((cb->postfix = malloc((int)sizeof(char))) == NULL)
707 ERROR ("Unable to allocate memory for hostname postfix buffer");
710 cb->postfix[0] = '\0';
713 DEBUG ("write_graphite: Registering write callback to carbon agent "
714 "%s:%d", cb->host, cb->port);
716 memset (&user_data, 0, sizeof (user_data));
718 user_data.free_func = NULL;
719 plugin_register_flush ("write_graphite", wg_flush, &user_data);
721 user_data.free_func = wg_callback_free;
722 plugin_register_write ("write_graphite", wg_write, &user_data);
727 static int wg_config (oconfig_item_t *ci)
731 for (i = 0; i < ci->children_num; i++)
733 oconfig_item_t *child = ci->children + i;
735 if (strcasecmp ("Carbon", child->key) == 0)
736 wg_config_carbon (child);
739 ERROR ("write_graphite plugin: Invalid configuration "
740 "option: %s.", child->key);
747 void module_register (void)
749 plugin_register_complex_config ("write_graphite", wg_config);
752 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */