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-2012 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 * Prefix "collectd"
39 * </Carbon>
40 * </Plugin>
41 */
43 #include "collectd.h"
44 #include "common.h"
45 #include "plugin.h"
46 #include "configfile.h"
48 #include "utils_cache.h"
49 #include "utils_parse_option.h"
51 /* Folks without pthread will need to disable this plugin. */
52 #include <pthread.h>
54 #include <sys/socket.h>
55 #include <netdb.h>
57 #ifndef WG_FORMAT_NAME
58 #define WG_FORMAT_NAME(ret, ret_len, vl, cb, ds_name) \
59 wg_format_name (ret, ret_len, (vl)->host, \
60 (vl)->plugin, (vl)->plugin_instance, \
61 (vl)->type, (vl)->type_instance, \
62 (cb)->prefix, (cb)->postfix, \
63 ds_name, (cb)->escape_char)
64 #endif
66 #ifndef WG_DEFAULT_NODE
67 # define WG_DEFAULT_NODE "localhost"
68 #endif
70 #ifndef WG_DEFAULT_SERVICE
71 # define WG_DEFAULT_SERVICE "2003"
72 #endif
74 #ifndef WG_DEFAULT_ESCAPE
75 # define WG_DEFAULT_ESCAPE '_'
76 #endif
78 #ifndef WG_SEND_BUF_SIZE
79 # define WG_SEND_BUF_SIZE 4096
80 #endif
82 /*
83 * Private variables
84 */
85 struct wg_callback
86 {
87 int sock_fd;
89 char *node;
90 char *service;
91 char *prefix;
92 char *postfix;
93 char escape_char;
95 _Bool store_rates;
97 char send_buf[WG_SEND_BUF_SIZE];
98 size_t send_buf_free;
99 size_t send_buf_fill;
100 cdtime_t send_buf_init_time;
102 pthread_mutex_t send_lock;
103 };
106 /*
107 * Functions
108 */
109 static void wg_reset_buffer (struct wg_callback *cb)
110 {
111 memset (cb->send_buf, 0, sizeof (cb->send_buf));
112 cb->send_buf_free = sizeof (cb->send_buf);
113 cb->send_buf_fill = 0;
114 cb->send_buf_init_time = cdtime ();
115 }
117 static int wg_send_buffer (struct wg_callback *cb)
118 {
119 int status = 0;
121 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
122 if (status < 0)
123 {
124 ERROR ("write_graphite plugin: send failed with "
125 "status %i (%s)",
126 status,
127 strerror (errno));
129 close (cb->sock_fd);
130 cb->sock_fd = -1;
132 return (-1);
133 }
134 return (0);
135 }
137 /* NOTE: You must hold cb->send_lock when calling this function! */
138 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
139 {
140 int status;
142 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
143 "send_buf_fill = %zu;",
144 (double)timeout,
145 cb->send_buf_fill);
147 /* timeout == 0 => flush unconditionally */
148 if (timeout > 0)
149 {
150 cdtime_t now;
152 now = cdtime ();
153 if ((cb->send_buf_init_time + timeout) > now)
154 return (0);
155 }
157 if (cb->send_buf_fill <= 0)
158 {
159 cb->send_buf_init_time = cdtime ();
160 return (0);
161 }
163 status = wg_send_buffer (cb);
164 wg_reset_buffer (cb);
166 return (status);
167 }
169 static int wg_callback_init (struct wg_callback *cb)
170 {
171 struct addrinfo ai_hints;
172 struct addrinfo *ai_list;
173 struct addrinfo *ai_ptr;
174 int status;
176 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
177 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
179 if (cb->sock_fd > 0)
180 return (0);
182 memset (&ai_hints, 0, sizeof (ai_hints));
183 #ifdef AI_ADDRCONFIG
184 ai_hints.ai_flags |= AI_ADDRCONFIG;
185 #endif
186 ai_hints.ai_family = AF_UNSPEC;
187 ai_hints.ai_socktype = SOCK_STREAM;
189 ai_list = NULL;
191 status = getaddrinfo (node, service, &ai_hints, &ai_list);
192 if (status != 0)
193 {
194 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
195 node, service, gai_strerror (status));
196 return (-1);
197 }
199 assert (ai_list != NULL);
200 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
201 {
202 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
203 ai_ptr->ai_protocol);
204 if (cb->sock_fd < 0)
205 continue;
207 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
208 if (status != 0)
209 {
210 close (cb->sock_fd);
211 cb->sock_fd = -1;
212 continue;
213 }
215 break;
216 }
218 freeaddrinfo (ai_list);
220 if (cb->sock_fd < 0)
221 {
222 char errbuf[1024];
223 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
224 "The last error was: %s", node, service,
225 sstrerror (errno, errbuf, sizeof (errbuf)));
226 close (cb->sock_fd);
227 return (-1);
228 }
230 wg_reset_buffer (cb);
232 return (0);
233 }
235 static void wg_callback_free (void *data)
236 {
237 struct wg_callback *cb;
239 if (data == NULL)
240 return;
242 cb = data;
244 pthread_mutex_lock (&cb->send_lock);
246 wg_flush_nolock (/* timeout = */ 0, cb);
248 close(cb->sock_fd);
249 cb->sock_fd = -1;
251 sfree(cb->node);
252 sfree(cb->service);
253 sfree(cb->prefix);
254 sfree(cb->postfix);
256 pthread_mutex_destroy (&cb->send_lock);
258 sfree(cb);
259 }
261 static int wg_flush (cdtime_t timeout,
262 const char *identifier __attribute__((unused)),
263 user_data_t *user_data)
264 {
265 struct wg_callback *cb;
266 int status;
268 if (user_data == NULL)
269 return (-EINVAL);
271 cb = user_data->data;
273 pthread_mutex_lock (&cb->send_lock);
275 if (cb->sock_fd < 0)
276 {
277 status = wg_callback_init (cb);
278 if (status != 0)
279 {
280 ERROR ("write_graphite plugin: wg_callback_init failed.");
281 pthread_mutex_unlock (&cb->send_lock);
282 return (-1);
283 }
284 }
286 status = wg_flush_nolock (timeout, cb);
287 pthread_mutex_unlock (&cb->send_lock);
289 return (status);
290 }
292 static int wg_format_values (char *ret, size_t ret_len,
293 int ds_num, const data_set_t *ds, const value_list_t *vl,
294 _Bool store_rates)
295 {
296 size_t offset = 0;
297 int status;
298 gauge_t *rates = NULL;
300 assert (0 == strcmp (ds->type, vl->type));
302 memset (ret, 0, ret_len);
304 #define BUFFER_ADD(...) do { \
305 status = ssnprintf (ret + offset, ret_len - offset, \
306 __VA_ARGS__); \
307 if (status < 1) \
308 { \
309 sfree (rates); \
310 return (-1); \
311 } \
312 else if (((size_t) status) >= (ret_len - offset)) \
313 { \
314 sfree (rates); \
315 return (-1); \
316 } \
317 else \
318 offset += ((size_t) status); \
319 } while (0)
321 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
322 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
323 else if (store_rates)
324 {
325 if (rates == NULL)
326 rates = uc_get_rate (ds, vl);
327 if (rates == NULL)
328 {
329 WARNING ("format_values: "
330 "uc_get_rate failed.");
331 return (-1);
332 }
333 BUFFER_ADD ("%g", rates[ds_num]);
334 }
335 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
336 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
337 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
338 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
339 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
340 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
341 else
342 {
343 ERROR ("format_values plugin: Unknown data source type: %i",
344 ds->ds[ds_num].type);
345 sfree (rates);
346 return (-1);
347 }
349 #undef BUFFER_ADD
351 sfree (rates);
352 return (0);
353 }
355 static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
356 char escape_char)
357 {
358 size_t i;
360 memset (dst, 0, dst_len);
362 if (src == NULL)
363 return;
365 for (i = 0; i < dst_len; i++)
366 {
367 if (src[i] == 0)
368 {
369 dst[i] = 0;
370 break;
371 }
373 if ((src[i] == '.')
374 || isspace ((int) src[i])
375 || iscntrl ((int) src[i]))
376 dst[i] = escape_char;
377 else
378 dst[i] = src[i];
379 }
380 }
382 static int wg_format_name (char *ret, int ret_len,
383 const char *hostname,
384 const char *plugin, const char *plugin_instance,
385 const char *type, const char *type_instance,
386 const char *prefix, const char *postfix,
387 const char *ds_name, char escape_char)
388 {
389 char n_hostname[DATA_MAX_NAME_LEN];
390 char n_plugin[DATA_MAX_NAME_LEN];
391 char n_plugin_instance[DATA_MAX_NAME_LEN];
392 char n_type[DATA_MAX_NAME_LEN];
393 char n_type_instance[DATA_MAX_NAME_LEN];
394 int status;
396 assert (hostname != NULL);
397 assert (plugin != NULL);
398 assert (type != NULL);
399 assert (ds_name != NULL);
401 if (prefix == NULL)
402 prefix = "";
404 if (postfix == NULL)
405 postfix = "";
407 wg_copy_escape_part (n_hostname, hostname,
408 sizeof (n_hostname), escape_char);
409 wg_copy_escape_part (n_plugin, plugin,
410 sizeof (n_plugin), escape_char);
411 wg_copy_escape_part (n_plugin_instance, plugin_instance,
412 sizeof (n_plugin_instance), escape_char);
413 wg_copy_escape_part (n_type, type,
414 sizeof (n_type), escape_char);
415 wg_copy_escape_part (n_type_instance, type_instance,
416 sizeof (n_type_instance), escape_char);
418 if (n_plugin_instance[0] == '\0')
419 {
420 if (n_type_instance[0] == '\0')
421 {
422 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
423 prefix, n_hostname, postfix, n_plugin, n_type, ds_name);
424 }
425 else
426 {
427 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
428 prefix, n_hostname, postfix, n_plugin, n_type,
429 n_type_instance, ds_name);
430 }
431 }
432 else
433 {
434 if (n_type_instance[0] == '\0')
435 {
436 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
437 prefix, n_hostname, postfix, n_plugin,
438 n_plugin_instance, n_type, ds_name);
439 }
440 else
441 {
442 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
443 prefix, n_hostname, postfix, n_plugin,
444 n_plugin_instance, n_type, n_type_instance, ds_name);
445 }
446 }
448 if ((status < 1) || (status >= ret_len))
449 return (-1);
451 return (0);
452 }
454 static int wg_send_message (const char* key, const char* value,
455 cdtime_t time, struct wg_callback *cb)
456 {
457 int status;
458 size_t message_len;
459 char message[1024];
461 message_len = (size_t) ssnprintf (message, sizeof (message),
462 "%s %s %.0f\n",
463 key,
464 value,
465 CDTIME_T_TO_DOUBLE(time));
466 if (message_len >= sizeof (message)) {
467 ERROR ("write_graphite plugin: message buffer too small: "
468 "Need %zu bytes.", message_len + 1);
469 return (-1);
470 }
473 pthread_mutex_lock (&cb->send_lock);
475 if (cb->sock_fd < 0)
476 {
477 status = wg_callback_init (cb);
478 if (status != 0)
479 {
480 ERROR ("write_graphite plugin: wg_callback_init failed.");
481 pthread_mutex_unlock (&cb->send_lock);
482 return (-1);
483 }
484 }
486 if (message_len >= cb->send_buf_free)
487 {
488 status = wg_flush_nolock (/* timeout = */ 0, cb);
489 if (status != 0)
490 {
491 pthread_mutex_unlock (&cb->send_lock);
492 return (status);
493 }
494 }
495 assert (message_len < cb->send_buf_free);
497 /* `message_len + 1' because `message_len' does not include the
498 * trailing null byte. Neither does `send_buffer_fill'. */
499 memcpy (cb->send_buf + cb->send_buf_fill,
500 message, message_len + 1);
501 cb->send_buf_fill += message_len;
502 cb->send_buf_free -= message_len;
504 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
505 cb->node,
506 cb->service,
507 cb->send_buf_fill, sizeof (cb->send_buf),
508 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
509 message);
511 /* Check if we have enough space for this message. */
512 pthread_mutex_unlock (&cb->send_lock);
514 return (0);
515 }
517 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
518 struct wg_callback *cb)
519 {
520 char key[10*DATA_MAX_NAME_LEN];
521 char values[512];
523 int status, i;
525 if (0 != strcmp (ds->type, vl->type))
526 {
527 ERROR ("write_graphite plugin: DS type does not match "
528 "value list type");
529 return -1;
530 }
532 if (ds->ds_num > 1)
533 {
534 for (i = 0; i < ds->ds_num; i++)
535 {
536 /* Copy the identifier to `key' and escape it. */
537 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
538 if (status != 0)
539 {
540 ERROR ("write_graphite plugin: error with format_name");
541 return (status);
542 }
544 escape_string (key, sizeof (key));
545 /* Convert the values to an ASCII representation and put that
546 * into `values'. */
547 status = wg_format_values (values, sizeof (values), i, ds, vl,
548 cb->store_rates);
549 if (status != 0)
550 {
551 ERROR ("write_graphite plugin: error with "
552 "wg_format_values");
553 return (status);
554 }
556 /* Send the message to graphite */
557 status = wg_send_message (key, values, vl->time, cb);
558 if (status != 0)
559 {
560 ERROR ("write_graphite plugin: error with "
561 "wg_send_message");
562 return (status);
563 }
564 }
565 }
566 else
567 {
568 /* Copy the identifier to `key' and escape it. */
569 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
570 if (status != 0)
571 {
572 ERROR ("write_graphite plugin: error with format_name");
573 return (status);
574 }
576 escape_string (key, sizeof (key));
577 /* Convert the values to an ASCII representation and put that into
578 * `values'. */
579 status = wg_format_values (values, sizeof (values), 0, ds, vl,
580 cb->store_rates);
581 if (status != 0)
582 {
583 ERROR ("write_graphite plugin: error with "
584 "wg_format_values");
585 return (status);
586 }
588 /* Send the message to graphite */
589 status = wg_send_message (key, values, vl->time, cb);
590 if (status != 0)
591 {
592 ERROR ("write_graphite plugin: error with "
593 "wg_send_message");
594 return (status);
595 }
596 }
598 return (0);
599 }
601 static int wg_write (const data_set_t *ds, const value_list_t *vl,
602 user_data_t *user_data)
603 {
604 struct wg_callback *cb;
605 int status;
607 if (user_data == NULL)
608 return (-EINVAL);
610 cb = user_data->data;
612 status = wg_write_messages (ds, vl, cb);
614 return (status);
615 }
617 static int config_set_char (char *dest,
618 oconfig_item_t *ci)
619 {
620 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
621 {
622 WARNING ("write_graphite plugin: The `%s' config option "
623 "needs exactly one string argument.", ci->key);
624 return (-1);
625 }
627 *dest = ci->values[0].value.string[0];
629 return (0);
630 }
632 static int wg_config_carbon (oconfig_item_t *ci)
633 {
634 struct wg_callback *cb;
635 user_data_t user_data;
636 int i;
638 cb = malloc (sizeof (*cb));
639 if (cb == NULL)
640 {
641 ERROR ("write_graphite plugin: malloc failed.");
642 return (-1);
643 }
644 memset (cb, 0, sizeof (*cb));
645 cb->sock_fd = -1;
646 cb->node = NULL;
647 cb->service = NULL;
648 cb->prefix = NULL;
649 cb->postfix = NULL;
650 cb->escape_char = WG_DEFAULT_ESCAPE;
652 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
654 for (i = 0; i < ci->children_num; i++)
655 {
656 oconfig_item_t *child = ci->children + i;
658 if (strcasecmp ("Host", child->key) == 0)
659 cf_util_get_string (child, &cb->node);
660 else if (strcasecmp ("Port", child->key) == 0)
661 cf_util_get_service (child, &cb->service);
662 else if (strcasecmp ("Prefix", child->key) == 0)
663 cf_util_get_string (child, &cb->prefix);
664 else if (strcasecmp ("Postfix", child->key) == 0)
665 cf_util_get_string (child, &cb->postfix);
666 else if (strcasecmp ("StoreRates", child->key) == 0)
667 cf_util_get_boolean (child, &cb->store_rates);
668 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
669 config_set_char (&cb->escape_char, child);
670 else
671 {
672 ERROR ("write_graphite plugin: Invalid configuration "
673 "option: %s.", child->key);
674 }
675 }
677 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
678 cb->node ? cb->node : WG_DEFAULT_NODE,
679 cb->service ? cb->service : WG_DEFAULT_SERVICE);
681 memset (&user_data, 0, sizeof (user_data));
682 user_data.data = cb;
683 user_data.free_func = NULL;
684 plugin_register_flush ("write_graphite", wg_flush, &user_data);
686 user_data.free_func = wg_callback_free;
687 plugin_register_write ("write_graphite", wg_write, &user_data);
689 return (0);
690 }
692 static int wg_config (oconfig_item_t *ci)
693 {
694 int i;
696 for (i = 0; i < ci->children_num; i++)
697 {
698 oconfig_item_t *child = ci->children + i;
700 if (strcasecmp ("Carbon", child->key) == 0)
701 wg_config_carbon (child);
702 else
703 {
704 ERROR ("write_graphite plugin: Invalid configuration "
705 "option: %s.", child->key);
706 }
707 }
709 return (0);
710 }
712 void module_register (void)
713 {
714 plugin_register_complex_config ("write_graphite", wg_config);
715 }
717 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */