f6c73ac80d8f616e698ec26893dbe489834b13e8
1 /**
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
8 *
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.
12 *
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.
17 *
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
21 *
22 * Authors:
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>
28 *
29 * Based on the write_http plugin.
30 **/
32 /* write_graphite plugin configuation example
33 *
34 * <Plugin write_graphite>
35 * <Carbon>
36 * Host "localhost"
37 * Port "2003"
38 * Protocol "udp"
39 * LogSendErrors true
40 * Prefix "collectd"
41 * </Carbon>
42 * </Plugin>
43 */
45 #include "collectd.h"
46 #include "common.h"
47 #include "plugin.h"
48 #include "configfile.h"
50 #include "utils_cache.h"
51 #include "utils_complain.h"
52 #include "utils_format_graphite.h"
54 /* Folks without pthread will need to disable this plugin. */
55 #include <pthread.h>
57 #include <sys/socket.h>
58 #include <netdb.h>
60 #define WG_DEFAULT_NODE "localhost"
61 #define WG_DEFAULT_SERVICE "2003"
62 #define WG_DEFAULT_PROTOCOL "tcp"
63 #define WG_DEFAULT_LOG_SEND_ERRORS 1
64 #define WG_DEFAULT_ESCAPE '_'
66 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
67 #define WG_SEND_BUF_SIZE 1428
69 #define WG_MIN_RECONNECT_INTERVAL TIME_T_TO_CDTIME_T (1)
71 /*
72 * Private variables
73 */
74 struct wg_callback
75 {
76 int sock_fd;
78 char *name;
80 char *node;
81 char *service;
82 char *protocol;
83 _Bool log_send_errors;
84 char *prefix;
85 char *postfix;
86 char escape_char;
88 unsigned int format_flags;
90 char send_buf[WG_SEND_BUF_SIZE];
91 size_t send_buf_free;
92 size_t send_buf_fill;
93 cdtime_t send_buf_init_time;
95 pthread_mutex_t send_lock;
96 c_complain_t init_complaint;
97 cdtime_t last_connect_time;
98 };
101 /*
102 * Functions
103 */
104 static void wg_reset_buffer (struct wg_callback *cb)
105 {
106 memset (cb->send_buf, 0, sizeof (cb->send_buf));
107 cb->send_buf_free = sizeof (cb->send_buf);
108 cb->send_buf_fill = 0;
109 cb->send_buf_init_time = cdtime ();
110 }
112 static int wg_send_buffer (struct wg_callback *cb)
113 {
114 ssize_t status = 0;
116 status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
117 if (status < 0)
118 {
119 const char *protocol = cb->protocol ? cb->protocol : WG_DEFAULT_PROTOCOL;
121 if (cb->log_send_errors)
122 {
123 char errbuf[1024];
124 ERROR ("write_graphite plugin: send to %s:%s (%s) failed with status %zi (%s)",
125 cb->node, cb->service, protocol,
126 status, sstrerror (errno, errbuf, sizeof (errbuf)));
127 }
129 close (cb->sock_fd);
130 cb->sock_fd = -1;
132 return (-1);
133 }
135 return (0);
136 }
138 /* NOTE: You must hold cb->send_lock when calling this function! */
139 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
140 {
141 int status;
143 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
144 "send_buf_fill = %zu;",
145 (double)timeout,
146 cb->send_buf_fill);
148 /* timeout == 0 => flush unconditionally */
149 if (timeout > 0)
150 {
151 cdtime_t now;
153 now = cdtime ();
154 if ((cb->send_buf_init_time + timeout) > now)
155 return (0);
156 }
158 if (cb->send_buf_fill <= 0)
159 {
160 cb->send_buf_init_time = cdtime ();
161 return (0);
162 }
164 status = wg_send_buffer (cb);
165 wg_reset_buffer (cb);
167 return (status);
168 }
170 static int wg_callback_init (struct wg_callback *cb)
171 {
172 struct addrinfo ai_hints;
173 struct addrinfo *ai_list;
174 struct addrinfo *ai_ptr;
175 cdtime_t now;
176 int status;
178 const char *protocol = cb->protocol ? cb->protocol : WG_DEFAULT_PROTOCOL;
180 char connerr[1024] = "";
182 if (cb->sock_fd > 0)
183 return (0);
185 /* Don't try to reconnect too often. By default, one reconnection attempt
186 * is made per second. */
187 now = cdtime ();
188 if ((now - cb->last_connect_time) < WG_MIN_RECONNECT_INTERVAL)
189 return (EAGAIN);
190 cb->last_connect_time = now;
192 memset (&ai_hints, 0, sizeof (ai_hints));
193 #ifdef AI_ADDRCONFIG
194 ai_hints.ai_flags |= AI_ADDRCONFIG;
195 #endif
196 ai_hints.ai_family = AF_UNSPEC;
198 if (0 == strcasecmp ("tcp", protocol))
199 ai_hints.ai_socktype = SOCK_STREAM;
200 else
201 ai_hints.ai_socktype = SOCK_DGRAM;
203 ai_list = NULL;
205 status = getaddrinfo (cb->node, cb->service, &ai_hints, &ai_list);
206 if (status != 0)
207 {
208 ERROR ("write_graphite plugin: getaddrinfo (%s, %s, %s) failed: %s",
209 cb->node, cb->service, protocol, gai_strerror (status));
210 return (-1);
211 }
213 assert (ai_list != NULL);
214 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
215 {
216 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
217 ai_ptr->ai_protocol);
218 if (cb->sock_fd < 0) {
219 char errbuf[1024];
220 snprintf (connerr, sizeof (connerr), "failed to open socket: %s",
221 sstrerror (errno, errbuf, sizeof (errbuf)));
222 continue;
223 }
225 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
226 if (status != 0)
227 {
228 char errbuf[1024];
229 snprintf (connerr, sizeof (connerr), "failed to connect to remote "
230 "host: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
231 close (cb->sock_fd);
232 cb->sock_fd = -1;
233 continue;
234 }
236 break;
237 }
239 freeaddrinfo (ai_list);
241 if (cb->sock_fd < 0)
242 {
243 if (connerr[0] == '\0')
244 /* this should not happen but try to get a message anyway */
245 sstrerror (errno, connerr, sizeof (connerr));
246 c_complain (LOG_ERR, &cb->init_complaint,
247 "write_graphite plugin: Connecting to %s:%s via %s failed. "
248 "The last error was: %s", cb->node, cb->service, protocol, connerr);
249 return (-1);
250 }
251 else
252 {
253 c_release (LOG_INFO, &cb->init_complaint,
254 "write_graphite plugin: Successfully connected to %s:%s via %s.",
255 cb->node, cb->service, protocol);
256 }
258 wg_reset_buffer (cb);
260 return (0);
261 }
263 static void wg_callback_free (void *data)
264 {
265 struct wg_callback *cb;
267 if (data == NULL)
268 return;
270 cb = data;
272 pthread_mutex_lock (&cb->send_lock);
274 wg_flush_nolock (/* timeout = */ 0, cb);
276 if (cb->sock_fd >= 0)
277 {
278 close (cb->sock_fd);
279 cb->sock_fd = -1;
280 }
282 sfree(cb->name);
283 sfree(cb->node);
284 sfree(cb->protocol);
285 sfree(cb->service);
286 sfree(cb->prefix);
287 sfree(cb->postfix);
289 pthread_mutex_destroy (&cb->send_lock);
291 sfree(cb);
292 }
294 static int wg_flush (cdtime_t timeout,
295 const char *identifier __attribute__((unused)),
296 user_data_t *user_data)
297 {
298 struct wg_callback *cb;
299 int status;
301 if (user_data == NULL)
302 return (-EINVAL);
304 cb = user_data->data;
306 pthread_mutex_lock (&cb->send_lock);
308 if (cb->sock_fd < 0)
309 {
310 status = wg_callback_init (cb);
311 if (status != 0)
312 {
313 /* An error message has already been printed. */
314 pthread_mutex_unlock (&cb->send_lock);
315 return (-1);
316 }
317 }
319 status = wg_flush_nolock (timeout, cb);
320 pthread_mutex_unlock (&cb->send_lock);
322 return (status);
323 }
325 static int wg_send_message (char const *message, struct wg_callback *cb)
326 {
327 int status;
328 size_t message_len;
330 message_len = strlen (message);
332 pthread_mutex_lock (&cb->send_lock);
334 if (cb->sock_fd < 0)
335 {
336 status = wg_callback_init (cb);
337 if (status != 0)
338 {
339 /* An error message has already been printed. */
340 pthread_mutex_unlock (&cb->send_lock);
341 return (-1);
342 }
343 }
345 if (message_len >= cb->send_buf_free)
346 {
347 status = wg_flush_nolock (/* timeout = */ 0, cb);
348 if (status != 0)
349 {
350 pthread_mutex_unlock (&cb->send_lock);
351 return (status);
352 }
353 }
355 /* Assert that we have enough space for this message. */
356 assert (message_len < cb->send_buf_free);
358 /* `message_len + 1' because `message_len' does not include the
359 * trailing null byte. Neither does `send_buffer_fill'. */
360 memcpy (cb->send_buf + cb->send_buf_fill,
361 message, message_len + 1);
362 cb->send_buf_fill += message_len;
363 cb->send_buf_free -= message_len;
365 DEBUG ("write_graphite plugin: [%s]:%s (%s) buf %zu/%zu (%.1f %%) \"%s\"",
366 cb->node,
367 cb->service,
368 cb->protocol,
369 cb->send_buf_fill, sizeof (cb->send_buf),
370 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
371 message);
373 pthread_mutex_unlock (&cb->send_lock);
375 return (0);
376 }
378 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
379 struct wg_callback *cb)
380 {
381 char buffer[WG_SEND_BUF_SIZE];
382 int status;
384 if (0 != strcmp (ds->type, vl->type))
385 {
386 ERROR ("write_graphite plugin: DS type does not match "
387 "value list type");
388 return -1;
389 }
391 memset (buffer, 0, sizeof (buffer));
392 status = format_graphite (buffer, sizeof (buffer), ds, vl,
393 cb->prefix, cb->postfix, cb->escape_char, cb->format_flags);
394 if (status != 0) /* error message has been printed already. */
395 return (status);
397 /* Send the message to graphite */
398 status = wg_send_message (buffer, cb);
399 if (status != 0) /* error message has been printed already. */
400 return (status);
402 return (0);
403 } /* int wg_write_messages */
405 static int wg_write (const data_set_t *ds, const value_list_t *vl,
406 user_data_t *user_data)
407 {
408 struct wg_callback *cb;
409 int status;
411 if (user_data == NULL)
412 return (EINVAL);
414 cb = user_data->data;
416 status = wg_write_messages (ds, vl, cb);
418 return (status);
419 }
421 static int config_set_char (char *dest,
422 oconfig_item_t *ci)
423 {
424 char buffer[4];
425 int status;
427 memset (buffer, 0, sizeof (buffer));
429 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
430 if (status != 0)
431 return (status);
433 if (buffer[0] == 0)
434 {
435 ERROR ("write_graphite plugin: Cannot use an empty string for the "
436 "\"EscapeCharacter\" option.");
437 return (-1);
438 }
440 if (buffer[1] != 0)
441 {
442 WARNING ("write_graphite plugin: Only the first character of the "
443 "\"EscapeCharacter\" option ('%c') will be used.",
444 (int) buffer[0]);
445 }
447 *dest = buffer[0];
449 return (0);
450 }
452 static int wg_config_node (oconfig_item_t *ci)
453 {
454 struct wg_callback *cb;
455 user_data_t user_data;
456 char callback_name[DATA_MAX_NAME_LEN];
457 int i;
458 int status = 0;
460 cb = malloc (sizeof (*cb));
461 if (cb == NULL)
462 {
463 ERROR ("write_graphite plugin: malloc failed.");
464 return (-1);
465 }
466 memset (cb, 0, sizeof (*cb));
467 cb->sock_fd = -1;
468 cb->name = NULL;
469 cb->node = strdup (WG_DEFAULT_NODE);
470 cb->service = strdup (WG_DEFAULT_SERVICE);
471 cb->protocol = NULL;
472 cb->log_send_errors = WG_DEFAULT_LOG_SEND_ERRORS;
473 cb->prefix = NULL;
474 cb->postfix = NULL;
475 cb->escape_char = WG_DEFAULT_ESCAPE;
476 cb->format_flags = GRAPHITE_STORE_RATES;
478 /* FIXME: Legacy configuration syntax. */
479 if (strcasecmp ("Carbon", ci->key) != 0)
480 {
481 status = cf_util_get_string (ci, &cb->name);
482 if (status != 0)
483 {
484 wg_callback_free (cb);
485 return (status);
486 }
487 }
489 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
490 C_COMPLAIN_INIT (&cb->init_complaint);
492 for (i = 0; i < ci->children_num; i++)
493 {
494 oconfig_item_t *child = ci->children + i;
496 if (strcasecmp ("Host", child->key) == 0)
497 cf_util_get_string (child, &cb->node);
498 else if (strcasecmp ("Port", child->key) == 0)
499 cf_util_get_service (child, &cb->service);
500 else if (strcasecmp ("Protocol", child->key) == 0)
501 {
502 cf_util_get_string (child, &cb->protocol);
504 if (strcasecmp ("UDP", cb->protocol) != 0 &&
505 strcasecmp ("TCP", cb->protocol) != 0)
506 {
507 ERROR ("write_graphite plugin: Unknown protocol (%s)",
508 cb->protocol);
509 status = -1;
510 }
511 }
512 else if (strcasecmp ("LogSendErrors", child->key) == 0)
513 cf_util_get_boolean (child, &cb->log_send_errors);
514 else if (strcasecmp ("Prefix", child->key) == 0)
515 cf_util_get_string (child, &cb->prefix);
516 else if (strcasecmp ("Postfix", child->key) == 0)
517 cf_util_get_string (child, &cb->postfix);
518 else if (strcasecmp ("StoreRates", child->key) == 0)
519 cf_util_get_flag (child, &cb->format_flags,
520 GRAPHITE_STORE_RATES);
521 else if (strcasecmp ("SeparateInstances", child->key) == 0)
522 cf_util_get_flag (child, &cb->format_flags,
523 GRAPHITE_SEPARATE_INSTANCES);
524 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
525 cf_util_get_flag (child, &cb->format_flags,
526 GRAPHITE_ALWAYS_APPEND_DS);
527 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
528 config_set_char (&cb->escape_char, child);
529 else
530 {
531 ERROR ("write_graphite plugin: Invalid configuration "
532 "option: %s.", child->key);
533 status = -1;
534 }
536 if (status != 0)
537 break;
538 }
540 if (status != 0)
541 {
542 wg_callback_free (cb);
543 return (status);
544 }
546 /* FIXME: Legacy configuration syntax. */
547 if (cb->name == NULL)
548 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s/%s",
549 cb->node,
550 cb->service,
551 cb->protocol != NULL ? cb->protocol : WG_DEFAULT_PROTOCOL);
552 else
553 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s",
554 cb->name);
556 memset (&user_data, 0, sizeof (user_data));
557 user_data.data = cb;
558 user_data.free_func = wg_callback_free;
559 plugin_register_write (callback_name, wg_write, &user_data);
561 user_data.free_func = NULL;
562 plugin_register_flush (callback_name, wg_flush, &user_data);
564 return (0);
565 }
567 static int wg_config (oconfig_item_t *ci)
568 {
569 int i;
571 for (i = 0; i < ci->children_num; i++)
572 {
573 oconfig_item_t *child = ci->children + i;
575 if (strcasecmp ("Node", child->key) == 0)
576 wg_config_node (child);
577 /* FIXME: Remove this legacy mode in version 6. */
578 else if (strcasecmp ("Carbon", child->key) == 0)
579 wg_config_node (child);
580 else
581 {
582 ERROR ("write_graphite plugin: Invalid configuration "
583 "option: %s.", child->key);
584 }
585 }
587 return (0);
588 }
590 void module_register (void)
591 {
592 plugin_register_complex_config ("write_graphite", wg_config);
593 }
595 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */