2 * collectd - src/write_graphite.c
3 * Copyright (C) 2012 Pierre-Yves Ritschard
4 * Copyright (C) 2011 Scott Sanders
5 * Copyright (C) 2009 Paul Sadauskas
6 * Copyright (C) 2009 Doug MacEachern
7 * Copyright (C) 2007-2013 Florian octo Forster
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; only version 2 of the License is applicable.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Florian octo Forster <octo at collectd.org>
24 * Doug MacEachern <dougm at hyperic.com>
25 * Paul Sadauskas <psadauskas at gmail.com>
26 * Scott Sanders <scott at jssjr.com>
27 * Pierre-Yves Ritschard <pyr at spootnik.org>
29 * Based on the write_http plugin.
32 /* write_graphite plugin configuation example
34 * <Plugin write_graphite>
51 #include "utils_complain.h"
52 #include "utils_format_graphite.h"
56 #ifndef WG_DEFAULT_NODE
57 #define WG_DEFAULT_NODE "localhost"
60 #ifndef WG_DEFAULT_SERVICE
61 #define WG_DEFAULT_SERVICE "2003"
64 #ifndef WG_DEFAULT_PROTOCOL
65 #define WG_DEFAULT_PROTOCOL "tcp"
68 #ifndef WG_DEFAULT_LOG_SEND_ERRORS
69 #define WG_DEFAULT_LOG_SEND_ERRORS true
72 #ifndef WG_DEFAULT_ESCAPE
73 #define WG_DEFAULT_ESCAPE '_'
76 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
77 #ifndef WG_SEND_BUF_SIZE
78 #define WG_SEND_BUF_SIZE 1428
81 #ifndef WG_MIN_RECONNECT_INTERVAL
82 #define WG_MIN_RECONNECT_INTERVAL TIME_T_TO_CDTIME_T(1)
101 unsigned int format_flags;
103 char send_buf[WG_SEND_BUF_SIZE];
104 size_t send_buf_free;
105 size_t send_buf_fill;
106 cdtime_t send_buf_init_time;
108 pthread_mutex_t send_lock;
109 c_complain_t init_complaint;
110 cdtime_t last_connect_time;
112 /* Force reconnect useful for load balanced environments */
113 cdtime_t last_reconnect_time;
114 cdtime_t reconnect_interval;
115 bool reconnect_interval_reached;
118 /* wg_force_reconnect_check closes cb->sock_fd when it was open for longer
119 * than cb->reconnect_interval. Must hold cb->send_lock when calling. */
120 static void wg_force_reconnect_check(struct wg_callback *cb) {
123 if (cb->reconnect_interval == 0)
126 /* check if address changes if addr_timeout */
128 if ((now - cb->last_reconnect_time) < cb->reconnect_interval)
131 /* here we should close connection on next */
134 cb->last_reconnect_time = now;
135 cb->reconnect_interval_reached = true;
137 INFO("write_graphite plugin: Connection closed after %.3f seconds.",
138 CDTIME_T_TO_DOUBLE(now - cb->last_reconnect_time));
144 static void wg_reset_buffer(struct wg_callback *cb) {
145 memset(cb->send_buf, 0, sizeof(cb->send_buf));
146 cb->send_buf_free = sizeof(cb->send_buf);
147 cb->send_buf_fill = 0;
148 cb->send_buf_init_time = cdtime();
151 static int wg_send_buffer(struct wg_callback *cb) {
157 status = swrite(cb->sock_fd, cb->send_buf, strlen(cb->send_buf));
159 if (cb->log_send_errors) {
160 ERROR("write_graphite plugin: send to %s:%s (%s) failed with status %zi "
162 cb->node, cb->service, cb->protocol, status, STRERRNO);
174 /* NOTE: You must hold cb->send_lock when calling this function! */
175 static int wg_flush_nolock(cdtime_t timeout, struct wg_callback *cb) {
178 DEBUG("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
179 "send_buf_fill = %" PRIsz ";",
180 (double)timeout, cb->send_buf_fill);
182 /* timeout == 0 => flush unconditionally */
187 if ((cb->send_buf_init_time + timeout) > now)
191 if (cb->send_buf_fill == 0) {
192 cb->send_buf_init_time = cdtime();
196 status = wg_send_buffer(cb);
202 static int wg_callback_init(struct wg_callback *cb) {
203 struct addrinfo *ai_list;
207 char connerr[1024] = "";
212 /* Don't try to reconnect too often. By default, one reconnection attempt
213 * is made per second. */
215 if ((now - cb->last_connect_time) < WG_MIN_RECONNECT_INTERVAL)
217 cb->last_connect_time = now;
219 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
220 .ai_flags = AI_ADDRCONFIG};
222 if (0 == strcasecmp("tcp", cb->protocol))
223 ai_hints.ai_socktype = SOCK_STREAM;
225 ai_hints.ai_socktype = SOCK_DGRAM;
227 status = getaddrinfo(cb->node, cb->service, &ai_hints, &ai_list);
229 ERROR("write_graphite plugin: getaddrinfo (%s, %s, %s) failed: %s",
230 cb->node, cb->service, cb->protocol, gai_strerror(status));
234 assert(ai_list != NULL);
235 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
236 ai_ptr = ai_ptr->ai_next) {
238 socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
239 if (cb->sock_fd < 0) {
240 snprintf(connerr, sizeof(connerr), "failed to open socket: %s", STRERRNO);
244 set_sock_opts(cb->sock_fd);
246 status = connect(cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
248 snprintf(connerr, sizeof(connerr), "failed to connect to remote host: %s",
258 freeaddrinfo(ai_list);
260 if (cb->sock_fd < 0) {
261 c_complain(LOG_ERR, &cb->init_complaint,
262 "write_graphite plugin: Connecting to %s:%s via %s failed. "
263 "The last error was: %s",
264 cb->node, cb->service, cb->protocol, connerr);
267 c_release(LOG_INFO, &cb->init_complaint,
268 "write_graphite plugin: Successfully connected to %s:%s via %s.",
269 cb->node, cb->service, cb->protocol);
272 /* wg_force_reconnect_check does not flush the buffer before closing a
273 * sending socket, so only call wg_reset_buffer() if the socket was closed
274 * for a different reason (tracked in cb->reconnect_interval_reached). */
275 if (!cb->reconnect_interval_reached || (cb->send_buf_free == 0))
278 cb->reconnect_interval_reached = false;
283 static void wg_callback_free(void *data) {
284 struct wg_callback *cb;
291 pthread_mutex_lock(&cb->send_lock);
293 wg_flush_nolock(/* timeout = */ 0, cb);
295 if (cb->sock_fd >= 0) {
307 pthread_mutex_unlock(&cb->send_lock);
308 pthread_mutex_destroy(&cb->send_lock);
313 static int wg_flush(cdtime_t timeout,
314 const char *identifier __attribute__((unused)),
315 user_data_t *user_data) {
316 struct wg_callback *cb;
319 if (user_data == NULL)
322 cb = user_data->data;
324 pthread_mutex_lock(&cb->send_lock);
326 if (cb->sock_fd < 0) {
327 status = wg_callback_init(cb);
329 /* An error message has already been printed. */
330 pthread_mutex_unlock(&cb->send_lock);
335 status = wg_flush_nolock(timeout, cb);
336 pthread_mutex_unlock(&cb->send_lock);
341 static int wg_send_message(char const *message, struct wg_callback *cb) {
345 message_len = strlen(message);
347 pthread_mutex_lock(&cb->send_lock);
349 wg_force_reconnect_check(cb);
351 if (cb->sock_fd < 0) {
352 status = wg_callback_init(cb);
354 /* An error message has already been printed. */
355 pthread_mutex_unlock(&cb->send_lock);
360 if (message_len >= cb->send_buf_free) {
361 status = wg_flush_nolock(/* timeout = */ 0, cb);
363 pthread_mutex_unlock(&cb->send_lock);
368 /* Assert that we have enough space for this message. */
369 assert(message_len < cb->send_buf_free);
371 /* `message_len + 1' because `message_len' does not include the
372 * trailing null byte. Neither does `send_buffer_fill'. */
373 memcpy(cb->send_buf + cb->send_buf_fill, message, message_len + 1);
374 cb->send_buf_fill += message_len;
375 cb->send_buf_free -= message_len;
377 DEBUG("write_graphite plugin: [%s]:%s (%s) buf %" PRIsz "/%" PRIsz
379 cb->node, cb->service, cb->protocol, cb->send_buf_fill,
380 sizeof(cb->send_buf),
381 100.0 * ((double)cb->send_buf_fill) / ((double)sizeof(cb->send_buf)),
384 pthread_mutex_unlock(&cb->send_lock);
389 static int wg_write_messages(const data_set_t *ds, const value_list_t *vl,
390 struct wg_callback *cb) {
391 char buffer[WG_SEND_BUF_SIZE] = {0};
394 if (0 != strcmp(ds->type, vl->type)) {
395 ERROR("write_graphite plugin: DS type does not match "
400 status = format_graphite(buffer, sizeof(buffer), ds, vl, cb->prefix,
401 cb->postfix, cb->escape_char, cb->format_flags);
402 if (status != 0) /* error message has been printed already. */
405 /* Send the message to graphite */
406 status = wg_send_message(buffer, cb);
407 if (status != 0) /* error message has been printed already. */
411 } /* int wg_write_messages */
413 static int wg_write(const data_set_t *ds, const value_list_t *vl,
414 user_data_t *user_data) {
415 struct wg_callback *cb;
418 if (user_data == NULL)
421 cb = user_data->data;
423 status = wg_write_messages(ds, vl, cb);
428 static int config_set_char(char *dest, oconfig_item_t *ci) {
429 char buffer[4] = {0};
432 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
436 if (buffer[0] == 0) {
437 ERROR("write_graphite plugin: Cannot use an empty string for the "
438 "\"EscapeCharacter\" option.");
442 if (buffer[1] != 0) {
443 WARNING("write_graphite plugin: Only the first character of the "
444 "\"EscapeCharacter\" option ('%c') will be used.",
453 static int wg_config_node(oconfig_item_t *ci) {
454 struct wg_callback *cb;
455 char callback_name[DATA_MAX_NAME_LEN];
458 cb = calloc(1, sizeof(*cb));
460 ERROR("write_graphite plugin: calloc failed.");
465 cb->node = strdup(WG_DEFAULT_NODE);
466 cb->service = strdup(WG_DEFAULT_SERVICE);
467 cb->protocol = strdup(WG_DEFAULT_PROTOCOL);
468 cb->last_reconnect_time = cdtime();
469 cb->reconnect_interval = 0;
470 cb->reconnect_interval_reached = false;
471 cb->log_send_errors = WG_DEFAULT_LOG_SEND_ERRORS;
474 cb->escape_char = WG_DEFAULT_ESCAPE;
475 cb->format_flags = GRAPHITE_STORE_RATES;
477 /* FIXME: Legacy configuration syntax. */
478 if (strcasecmp("Carbon", ci->key) != 0) {
479 status = cf_util_get_string(ci, &cb->name);
481 wg_callback_free(cb);
486 pthread_mutex_init(&cb->send_lock, /* attr = */ NULL);
487 C_COMPLAIN_INIT(&cb->init_complaint);
489 for (int i = 0; i < ci->children_num; i++) {
490 oconfig_item_t *child = ci->children + i;
492 if (strcasecmp("Host", child->key) == 0)
493 cf_util_get_string(child, &cb->node);
494 else if (strcasecmp("Port", child->key) == 0)
495 cf_util_get_service(child, &cb->service);
496 else if (strcasecmp("Protocol", child->key) == 0) {
497 cf_util_get_string(child, &cb->protocol);
499 if (strcasecmp("UDP", cb->protocol) != 0 &&
500 strcasecmp("TCP", cb->protocol) != 0) {
501 ERROR("write_graphite plugin: Unknown protocol (%s)", cb->protocol);
504 } else if (strcasecmp("ReconnectInterval", child->key) == 0)
505 cf_util_get_cdtime(child, &cb->reconnect_interval);
506 else if (strcasecmp("LogSendErrors", child->key) == 0)
507 cf_util_get_boolean(child, &cb->log_send_errors);
508 else if (strcasecmp("Prefix", child->key) == 0)
509 cf_util_get_string(child, &cb->prefix);
510 else if (strcasecmp("Postfix", child->key) == 0)
511 cf_util_get_string(child, &cb->postfix);
512 else if (strcasecmp("StoreRates", child->key) == 0)
513 cf_util_get_flag(child, &cb->format_flags, GRAPHITE_STORE_RATES);
514 else if (strcasecmp("SeparateInstances", child->key) == 0)
515 cf_util_get_flag(child, &cb->format_flags, GRAPHITE_SEPARATE_INSTANCES);
516 else if (strcasecmp("AlwaysAppendDS", child->key) == 0)
517 cf_util_get_flag(child, &cb->format_flags, GRAPHITE_ALWAYS_APPEND_DS);
518 else if (strcasecmp("PreserveSeparator", child->key) == 0)
519 cf_util_get_flag(child, &cb->format_flags, GRAPHITE_PRESERVE_SEPARATOR);
520 else if (strcasecmp("DropDuplicateFields", child->key) == 0)
521 cf_util_get_flag(child, &cb->format_flags, GRAPHITE_DROP_DUPE_FIELDS);
522 else if (strcasecmp("UseTags", child->key) == 0)
523 cf_util_get_flag(child, &cb->format_flags, GRAPHITE_USE_TAGS);
524 else if (strcasecmp("EscapeCharacter", child->key) == 0)
525 config_set_char(&cb->escape_char, child);
527 ERROR("write_graphite plugin: Invalid configuration "
538 wg_callback_free(cb);
542 /* FIXME: Legacy configuration syntax. */
543 if (cb->name == NULL)
544 snprintf(callback_name, sizeof(callback_name), "write_graphite/%s/%s/%s",
545 cb->node, cb->service, cb->protocol);
547 snprintf(callback_name, sizeof(callback_name), "write_graphite/%s",
550 plugin_register_write(callback_name, wg_write,
552 .data = cb, .free_func = wg_callback_free,
555 plugin_register_flush(callback_name, wg_flush, &(user_data_t){.data = cb});
560 static int wg_config(oconfig_item_t *ci) {
561 for (int i = 0; i < ci->children_num; i++) {
562 oconfig_item_t *child = ci->children + i;
564 if (strcasecmp("Node", child->key) == 0)
565 wg_config_node(child);
566 /* FIXME: Remove this legacy mode in version 6. */
567 else if (strcasecmp("Carbon", child->key) == 0)
568 wg_config_node(child);
570 ERROR("write_graphite plugin: Invalid configuration "
579 void module_register(void) {
580 plugin_register_complex_config("write_graphite", wg_config);