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
27 #include "configfile.h"
29 #include "utils_cache.h"
30 #include "utils_parse_option.h"
32 /* Folks without pthread will need to disable this plugin. */
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <netinet/in.h>
42 #ifndef WG_FORMAT_NAME
43 #define WG_FORMAT_NAME(ret, ret_len, vl, prefix, name) \
44 wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
45 (vl)->type, (vl)->type_instance, prefix, name)
54 struct hostent *server;
63 cdtime_t send_buf_init_time;
65 pthread_mutex_t send_lock;
72 static void wg_reset_buffer (struct wg_callback *cb) /* {{{ */
74 memset (cb->send_buf, 0, sizeof (cb->send_buf));
75 cb->send_buf_free = sizeof (cb->send_buf);
76 cb->send_buf_fill = 0;
77 cb->send_buf_init_time = cdtime ();
78 } /* }}} wg_reset_buffer */
80 static int wg_send_buffer (struct wg_callback *cb) /* {{{ */
84 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
87 ERROR ("write_graphite plugin: send failed with "
92 pthread_mutex_lock (&cb->send_lock);
94 DEBUG ("write_graphite plugin: closing socket and restting fd "
95 "so reinit will occur");
99 pthread_mutex_unlock (&cb->send_lock);
104 } /* }}} wg_send_buffer */
106 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb) /* {{{ */
110 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
111 "send_buf_fill = %zu;",
115 /* timeout == 0 => flush unconditionally */
121 if ((cb->send_buf_init_time + timeout) > now)
125 if (cb->send_buf_fill <= 0)
127 cb->send_buf_init_time = cdtime ();
131 status = wg_send_buffer (cb);
132 wg_reset_buffer (cb);
135 } /* }}} wg_flush_nolock */
137 static int wg_callback_init (struct wg_callback *cb) /* {{{ */
141 struct sockaddr_in serv_addr;
146 cb->sock_fd = socket (AF_INET, SOCK_STREAM, 0);
149 ERROR ("write_graphite plugin: socket failed: %s", strerror (errno));
152 cb->server = gethostbyname(cb->host);
153 if (cb->server == NULL)
155 ERROR ("write_graphite plugin: no such host");
158 memset (&serv_addr, 0, sizeof (serv_addr));
159 serv_addr.sin_family = AF_INET;
160 memcpy (&serv_addr.sin_addr.s_addr,
162 cb->server->h_length);
163 serv_addr.sin_port = htons(cb->port);
165 status = connect(cb->sock_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
169 sstrerror (errno, errbuf, sizeof (errbuf));
170 ERROR ("write_graphite plugin: connect failed: %s", errbuf);
176 wg_reset_buffer (cb);
179 } /* }}} int wg_callback_init */
181 static void wg_callback_free (void *data) /* {{{ */
183 struct wg_callback *cb;
190 wg_flush_nolock (/* timeout = */ 0, cb);
197 } /* }}} void wg_callback_free */
199 static int wg_flush (cdtime_t timeout, /* {{{ */
200 const char *identifier __attribute__((unused)),
201 user_data_t *user_data)
203 struct wg_callback *cb;
206 if (user_data == NULL)
209 cb = user_data->data;
211 pthread_mutex_lock (&cb->send_lock);
215 status = wg_callback_init (cb);
218 ERROR ("write_graphite plugin: wg_callback_init failed.");
219 pthread_mutex_unlock (&cb->send_lock);
224 status = wg_flush_nolock (timeout, cb);
225 pthread_mutex_unlock (&cb->send_lock);
228 } /* }}} int wg_flush */
230 static int wg_format_values (char *ret, size_t ret_len, /* {{{ */
231 int ds_num, const data_set_t *ds, const value_list_t *vl,
236 gauge_t *rates = NULL;
238 assert (0 == strcmp (ds->type, vl->type));
240 memset (ret, 0, ret_len);
242 #define BUFFER_ADD(...) do { \
243 status = ssnprintf (ret + offset, ret_len - offset, \
250 else if (((size_t) status) >= (ret_len - offset)) \
256 offset += ((size_t) status); \
259 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
260 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
261 else if (store_rates)
264 rates = uc_get_rate (ds, vl);
267 WARNING ("format_values: "
268 "uc_get_rate failed.");
271 BUFFER_ADD ("%g", rates[ds_num]);
273 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
274 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
275 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
276 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
277 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
278 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
281 ERROR ("format_values plugin: Unknown data source type: %i",
282 ds->ds[ds_num].type);
291 } /* }}} int wg_format_values */
293 static int normalize_hostname (char *dst, const char *src) /* {{{ */
299 for (i = 0; i < strlen(src) ; i++)
312 } /* }}} int normalize_hostname */
314 static int wg_format_name (char *ret, int ret_len, /* {{{ */
315 const char *hostname,
316 const char *plugin, const char *plugin_instance,
317 const char *type, const char *type_instance,
318 const char *prefix, const char *ds_name)
323 assert (plugin != NULL);
324 assert (type != NULL);
326 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
328 ERROR ("Unable to allocate memory for normalized hostname buffer");
332 if (normalize_hostname(n_hostname, hostname) == -1)
334 ERROR ("Unable to normalize hostname");
338 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
340 if ((type_instance == NULL) || (strlen (type_instance) == 0))
342 if ((ds_name == NULL) || (strlen (ds_name) == 0))
343 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s",
344 prefix, n_hostname, plugin, type);
346 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
347 prefix, n_hostname, plugin, type, ds_name);
351 if ((ds_name == NULL) || (strlen (ds_name) == 0))
352 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s",
353 prefix, n_hostname, plugin, type,
356 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s.%s",
357 prefix, n_hostname, plugin, type,
358 type_instance, ds_name);
363 if ((type_instance == NULL) || (strlen (type_instance) == 0))
365 if ((ds_name == NULL) || (strlen (ds_name) == 0))
366 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
367 prefix, n_hostname, plugin,
368 plugin_instance, type);
370 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s.%s",
371 prefix, n_hostname, plugin,
372 plugin_instance, type, ds_name);
376 if ((ds_name == NULL) || (strlen (ds_name) == 0))
377 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s",
378 prefix, n_hostname, plugin,
379 plugin_instance, type, type_instance);
381 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s.%s",
382 prefix, n_hostname, plugin,
383 plugin_instance, type, type_instance, ds_name);
389 if ((status < 1) || (status >= ret_len))
392 } /* }}} int wg_format_name */
394 static int wg_send_message (const char* key, const char* value, cdtime_t time, struct wg_callback *cb) /* {{{ */
400 message_len = (size_t) ssnprintf (message, sizeof (message),
404 CDTIME_T_TO_DOUBLE(time));
405 if (message_len >= sizeof (message)) {
406 ERROR ("write_graphite plugin: message buffer too small: "
407 "Need %zu bytes.", message_len + 1);
412 pthread_mutex_lock (&cb->send_lock);
416 status = wg_callback_init (cb);
419 ERROR ("write_graphite plugin: wg_callback_init failed.");
420 pthread_mutex_unlock (&cb->send_lock);
425 if (message_len >= cb->send_buf_free)
427 status = wg_flush_nolock (/* timeout = */ 0, cb);
430 pthread_mutex_unlock (&cb->send_lock);
434 assert (message_len < cb->send_buf_free);
436 /* `message_len + 1' because `message_len' does not include the
437 * trailing null byte. Neither does `send_buffer_fill'. */
438 memcpy (cb->send_buf + cb->send_buf_fill,
439 message, message_len + 1);
440 cb->send_buf_fill += message_len;
441 cb->send_buf_free -= message_len;
443 DEBUG ("write_graphite plugin: <%s:%d> buf %zu/%zu (%g%%) \"%s\"",
446 cb->send_buf_fill, sizeof (cb->send_buf),
447 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
450 /* Check if we have enough space for this message. */
451 pthread_mutex_unlock (&cb->send_lock);
454 } /* }}} int wg_send_message */
456 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl, /* {{{ */
457 struct wg_callback *cb)
459 char key[10*DATA_MAX_NAME_LEN];
464 if (0 != strcmp (ds->type, vl->type))
466 ERROR ("write_graphite plugin: DS type does not match "
473 for (i = 0; i < ds->ds_num; i++)
475 /* Copy the identifier to `key' and escape it. */
476 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, ds->ds[i].name);
479 ERROR ("write_graphite plugin: error with format_name");
483 escape_string (key, sizeof (key));
484 /* Convert the values to an ASCII representation and put that into
486 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
489 ERROR ("write_graphite plugin: error with "
494 /* Send the message to graphite */
495 status = wg_send_message (key, values, vl->time, cb);
498 ERROR ("write_graphite plugin: error with "
506 /* Copy the identifier to `key' and escape it. */
507 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, NULL);
510 ERROR ("write_graphite plugin: error with format_name");
514 escape_string (key, sizeof (key));
515 /* Convert the values to an ASCII representation and put that into
517 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
520 ERROR ("write_graphite plugin: error with "
525 /* Send the message to graphite */
526 status = wg_send_message (key, values, vl->time, cb);
529 ERROR ("write_graphite plugin: error with "
536 } /* }}} int wg_write_messages */
538 static int wg_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
539 user_data_t *user_data)
541 struct wg_callback *cb;
544 if (user_data == NULL)
547 cb = user_data->data;
549 status = wg_write_messages (ds, vl, cb);
552 } /* }}} int wg_write */
554 static int config_set_number (int *dest, /* {{{ */
557 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
559 WARNING ("write_graphite plugin: The `%s' config option "
560 "needs exactly one numeric argument.", ci->key);
564 *dest = ci->values[0].value.number;
567 } /* }}} int config_set_number */
569 static int config_set_string (char **ret_string, /* {{{ */
574 if ((ci->values_num != 1)
575 || (ci->values[0].type != OCONFIG_TYPE_STRING))
577 WARNING ("write_graphite plugin: The `%s' config option "
578 "needs exactly one string argument.", ci->key);
582 string = strdup (ci->values[0].value.string);
585 ERROR ("write_graphite plugin: strdup failed.");
589 if (*ret_string != NULL)
591 *ret_string = string;
594 } /* }}} int config_set_string */
596 static int wg_config_carbon (oconfig_item_t *ci) /* {{{ */
598 struct wg_callback *cb;
599 user_data_t user_data;
602 cb = malloc (sizeof (*cb));
605 ERROR ("write_graphite plugin: malloc failed.");
608 memset (cb, 0, sizeof (*cb));
615 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
617 config_set_string (&cb->host, ci);
618 if (cb->host == NULL)
621 for (i = 0; i < ci->children_num; i++)
623 oconfig_item_t *child = ci->children + i;
625 if (strcasecmp ("Port", child->key) == 0)
626 config_set_number (&cb->port, child);
627 else if (strcasecmp ("Prefix", child->key) == 0)
628 config_set_string (&cb->prefix, child);
631 ERROR ("write_graphite plugin: Invalid configuration "
632 "option: %s.", child->key);
636 DEBUG ("write_graphite: Registering write callback to carbon agent "
637 "%s:%d", cb->host, cb->port);
639 memset (&user_data, 0, sizeof (user_data));
641 user_data.free_func = NULL;
642 plugin_register_flush ("write_graphite", wg_flush, &user_data);
644 user_data.free_func = wg_callback_free;
645 plugin_register_write ("write_graphite", wg_write, &user_data);
648 } /* }}} int wg_config_carbon */
650 static int wg_config (oconfig_item_t *ci) /* {{{ */
654 for (i = 0; i < ci->children_num; i++)
656 oconfig_item_t *child = ci->children + i;
658 if (strcasecmp ("Carbon", child->key) == 0)
659 wg_config_carbon (child);
662 ERROR ("write_graphite plugin: Invalid configuration "
663 "option: %s.", child->key);
668 } /* }}} int wg_config */
670 void module_register (void) /* {{{ */
672 plugin_register_complex_config ("write_graphite", wg_config);
673 } /* }}} void module_register */
675 /* vim: set fdm=marker sw=4 ts=4 sts=4 tw=78 et : */