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>
27 * <Carbon "local-agent">
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, (vl)->plugin_instance, \
56 (vl)->type, (vl)->type_instance, prefix, name)
67 struct hostent *server;
76 cdtime_t send_buf_init_time;
78 pthread_mutex_t send_lock;
85 static void wg_reset_buffer (struct wg_callback *cb)
87 memset (cb->send_buf, 0, sizeof (cb->send_buf));
88 cb->send_buf_free = sizeof (cb->send_buf);
89 cb->send_buf_fill = 0;
90 cb->send_buf_init_time = cdtime ();
93 static int wg_send_buffer (struct wg_callback *cb)
97 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
100 ERROR ("write_graphite plugin: send failed with "
105 pthread_mutex_lock (&cb->send_lock);
107 DEBUG ("write_graphite plugin: closing socket and restting fd "
108 "so reinit will occur");
112 pthread_mutex_unlock (&cb->send_lock);
119 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
123 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
124 "send_buf_fill = %zu;",
128 /* timeout == 0 => flush unconditionally */
134 if ((cb->send_buf_init_time + timeout) > now)
138 if (cb->send_buf_fill <= 0)
140 cb->send_buf_init_time = cdtime ();
144 status = wg_send_buffer (cb);
145 wg_reset_buffer (cb);
150 static int wg_callback_init (struct wg_callback *cb)
154 struct sockaddr_in serv_addr;
159 cb->sock_fd = socket (AF_INET, SOCK_STREAM, 0);
162 ERROR ("write_graphite plugin: socket failed: %s", strerror (errno));
165 cb->server = gethostbyname(cb->host);
166 if (cb->server == NULL)
168 ERROR ("write_graphite plugin: no such host");
171 memset (&serv_addr, 0, sizeof (serv_addr));
172 serv_addr.sin_family = AF_INET;
173 memcpy (&serv_addr.sin_addr.s_addr,
175 cb->server->h_length);
176 serv_addr.sin_port = htons(cb->port);
178 status = connect(cb->sock_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
182 sstrerror (errno, errbuf, sizeof (errbuf));
183 ERROR ("write_graphite plugin: connect failed: %s", errbuf);
189 wg_reset_buffer (cb);
194 static void wg_callback_free (void *data)
196 struct wg_callback *cb;
203 wg_flush_nolock (/* timeout = */ 0, cb);
213 static int wg_flush (cdtime_t timeout,
214 const char *identifier __attribute__((unused)),
215 user_data_t *user_data)
217 struct wg_callback *cb;
220 if (user_data == NULL)
223 cb = user_data->data;
225 pthread_mutex_lock (&cb->send_lock);
229 status = wg_callback_init (cb);
232 ERROR ("write_graphite plugin: wg_callback_init failed.");
233 pthread_mutex_unlock (&cb->send_lock);
238 status = wg_flush_nolock (timeout, cb);
239 pthread_mutex_unlock (&cb->send_lock);
244 static int wg_format_values (char *ret, size_t ret_len,
245 int ds_num, const data_set_t *ds, const value_list_t *vl,
250 gauge_t *rates = NULL;
252 assert (0 == strcmp (ds->type, vl->type));
254 memset (ret, 0, ret_len);
256 #define BUFFER_ADD(...) do { \
257 status = ssnprintf (ret + offset, ret_len - offset, \
264 else if (((size_t) status) >= (ret_len - offset)) \
270 offset += ((size_t) status); \
273 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
274 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
275 else if (store_rates)
278 rates = uc_get_rate (ds, vl);
281 WARNING ("format_values: "
282 "uc_get_rate failed.");
285 BUFFER_ADD ("%g", rates[ds_num]);
287 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
288 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
289 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
290 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
291 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
292 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
295 ERROR ("format_values plugin: Unknown data source type: %i",
296 ds->ds[ds_num].type);
307 static int normalize_hostname (char *dst, const char *src)
313 for (i = 0; i < strlen(src) ; i++)
328 static int wg_format_name (char *ret, int ret_len,
329 const char *hostname,
330 const char *plugin, const char *plugin_instance,
331 const char *type, const char *type_instance,
332 const char *prefix, const char *ds_name)
337 assert (plugin != NULL);
338 assert (type != NULL);
340 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
342 ERROR ("Unable to allocate memory for normalized hostname buffer");
346 if (normalize_hostname(n_hostname, hostname) == -1)
348 ERROR ("Unable to normalize hostname");
352 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
354 if ((type_instance == NULL) || (strlen (type_instance) == 0))
356 if ((ds_name == NULL) || (strlen (ds_name) == 0))
357 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s",
358 prefix, n_hostname, plugin, type);
360 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
361 prefix, n_hostname, plugin, type, ds_name);
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, type,
370 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s.%s",
371 prefix, n_hostname, plugin, type,
372 type_instance, ds_name);
377 if ((type_instance == NULL) || (strlen (type_instance) == 0))
379 if ((ds_name == NULL) || (strlen (ds_name) == 0))
380 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
381 prefix, n_hostname, plugin,
382 plugin_instance, type);
384 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s.%s",
385 prefix, n_hostname, plugin,
386 plugin_instance, type, ds_name);
390 if ((ds_name == NULL) || (strlen (ds_name) == 0))
391 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s",
392 prefix, n_hostname, plugin,
393 plugin_instance, type, type_instance);
395 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s.%s",
396 prefix, n_hostname, plugin,
397 plugin_instance, type, type_instance, ds_name);
403 if ((status < 1) || (status >= ret_len))
408 static int wg_send_message (const char* key, const char* value, cdtime_t time, struct wg_callback *cb)
414 message_len = (size_t) ssnprintf (message, sizeof (message),
418 CDTIME_T_TO_DOUBLE(time));
419 if (message_len >= sizeof (message)) {
420 ERROR ("write_graphite plugin: message buffer too small: "
421 "Need %zu bytes.", message_len + 1);
426 pthread_mutex_lock (&cb->send_lock);
430 status = wg_callback_init (cb);
433 ERROR ("write_graphite plugin: wg_callback_init failed.");
434 pthread_mutex_unlock (&cb->send_lock);
439 if (message_len >= cb->send_buf_free)
441 status = wg_flush_nolock (/* timeout = */ 0, cb);
444 pthread_mutex_unlock (&cb->send_lock);
448 assert (message_len < cb->send_buf_free);
450 /* `message_len + 1' because `message_len' does not include the
451 * trailing null byte. Neither does `send_buffer_fill'. */
452 memcpy (cb->send_buf + cb->send_buf_fill,
453 message, message_len + 1);
454 cb->send_buf_fill += message_len;
455 cb->send_buf_free -= message_len;
457 DEBUG ("write_graphite plugin: <%s:%d> buf %zu/%zu (%g%%) \"%s\"",
460 cb->send_buf_fill, sizeof (cb->send_buf),
461 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
464 /* Check if we have enough space for this message. */
465 pthread_mutex_unlock (&cb->send_lock);
470 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
471 struct wg_callback *cb)
473 char key[10*DATA_MAX_NAME_LEN];
478 if (0 != strcmp (ds->type, vl->type))
480 ERROR ("write_graphite plugin: DS type does not match "
487 for (i = 0; i < ds->ds_num; i++)
489 /* Copy the identifier to `key' and escape it. */
490 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, ds->ds[i].name);
493 ERROR ("write_graphite plugin: error with format_name");
497 escape_string (key, sizeof (key));
498 /* Convert the values to an ASCII representation and put that into
500 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
503 ERROR ("write_graphite plugin: error with "
508 /* Send the message to graphite */
509 status = wg_send_message (key, values, vl->time, cb);
512 ERROR ("write_graphite plugin: error with "
520 /* Copy the identifier to `key' and escape it. */
521 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, NULL);
524 ERROR ("write_graphite plugin: error with format_name");
528 escape_string (key, sizeof (key));
529 /* Convert the values to an ASCII representation and put that into
531 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
534 ERROR ("write_graphite plugin: error with "
539 /* Send the message to graphite */
540 status = wg_send_message (key, values, vl->time, cb);
543 ERROR ("write_graphite plugin: error with "
552 static int wg_write (const data_set_t *ds, const value_list_t *vl,
553 user_data_t *user_data)
555 struct wg_callback *cb;
558 if (user_data == NULL)
561 cb = user_data->data;
563 status = wg_write_messages (ds, vl, cb);
568 static int config_set_number (int *dest,
571 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
573 WARNING ("write_graphite plugin: The `%s' config option "
574 "needs exactly one numeric argument.", ci->key);
578 *dest = ci->values[0].value.number;
583 static int config_set_string (char **ret_string,
588 if ((ci->values_num != 1)
589 || (ci->values[0].type != OCONFIG_TYPE_STRING))
591 WARNING ("write_graphite plugin: The `%s' config option "
592 "needs exactly one string argument.", ci->key);
596 string = strdup (ci->values[0].value.string);
599 ERROR ("write_graphite plugin: strdup failed.");
603 if (*ret_string != NULL)
605 *ret_string = string;
610 static int wg_config_carbon (oconfig_item_t *ci)
612 struct wg_callback *cb;
613 user_data_t user_data;
616 cb = malloc (sizeof (*cb));
619 ERROR ("write_graphite plugin: malloc failed.");
622 memset (cb, 0, sizeof (*cb));
630 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
632 config_set_string (&cb->name, ci);
633 if (cb->name == NULL)
636 for (i = 0; i < ci->children_num; i++)
638 oconfig_item_t *child = ci->children + i;
640 if (strcasecmp ("Host", child->key) == 0)
641 config_set_string (&cb->host, child);
642 else if (strcasecmp ("Port", child->key) == 0)
643 config_set_number (&cb->port, child);
644 else if (strcasecmp ("Prefix", child->key) == 0)
645 config_set_string (&cb->prefix, child);
648 ERROR ("write_graphite plugin: Invalid configuration "
649 "option: %s.", child->key);
653 DEBUG ("write_graphite: Registering write callback to carbon agent "
654 "%s:%d", cb->host, cb->port);
656 memset (&user_data, 0, sizeof (user_data));
658 user_data.free_func = NULL;
659 plugin_register_flush ("write_graphite", wg_flush, &user_data);
661 user_data.free_func = wg_callback_free;
662 plugin_register_write ("write_graphite", wg_write, &user_data);
667 static int wg_config (oconfig_item_t *ci)
671 for (i = 0; i < ci->children_num; i++)
673 oconfig_item_t *child = ci->children + i;
675 if (strcasecmp ("Carbon", child->key) == 0)
676 wg_config_carbon (child);
679 ERROR ("write_graphite plugin: Invalid configuration "
680 "option: %s.", child->key);
687 void module_register (void)
689 plugin_register_complex_config ("write_graphite", wg_config);
692 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */