502e22c31fa2ebfc47c8d172f41be3e2d1645c62
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_SEND_BUF_SIZE
75 # define WG_SEND_BUF_SIZE 4096
76 #endif
78 /*
79 * Private variables
80 */
81 struct wg_callback
82 {
83 int sock_fd;
84 struct hostent *server;
86 char *node;
87 char *service;
88 char *prefix;
89 char *postfix;
90 char escape_char;
92 char send_buf[WG_SEND_BUF_SIZE];
93 size_t send_buf_free;
94 size_t send_buf_fill;
95 cdtime_t send_buf_init_time;
97 pthread_mutex_t send_lock;
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 int status = 0;
116 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
117 if (status < 0)
118 {
119 ERROR ("write_graphite plugin: send failed with "
120 "status %i (%s)",
121 status,
122 strerror (errno));
124 pthread_mutex_trylock (&cb->send_lock);
126 DEBUG ("write_graphite plugin: closing socket and restting fd "
127 "so reinit will occur");
128 close (cb->sock_fd);
129 cb->sock_fd = -1;
131 pthread_mutex_unlock (&cb->send_lock);
133 return (-1);
134 }
135 return (0);
136 }
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 wg_flush_nolock (/* timeout = */ 0, cb);
246 close(cb->sock_fd);
247 sfree(cb->node);
248 sfree(cb->service);
249 sfree(cb->prefix);
250 sfree(cb->postfix);
252 sfree(cb);
253 }
255 static int wg_flush (cdtime_t timeout,
256 const char *identifier __attribute__((unused)),
257 user_data_t *user_data)
258 {
259 struct wg_callback *cb;
260 int status;
262 if (user_data == NULL)
263 return (-EINVAL);
265 cb = user_data->data;
267 pthread_mutex_lock (&cb->send_lock);
269 if (cb->sock_fd < 0)
270 {
271 status = wg_callback_init (cb);
272 if (status != 0)
273 {
274 ERROR ("write_graphite plugin: wg_callback_init failed.");
275 pthread_mutex_unlock (&cb->send_lock);
276 return (-1);
277 }
278 }
280 status = wg_flush_nolock (timeout, cb);
281 pthread_mutex_unlock (&cb->send_lock);
283 return (status);
284 }
286 static int wg_format_values (char *ret, size_t ret_len,
287 int ds_num, const data_set_t *ds, const value_list_t *vl,
288 _Bool store_rates)
289 {
290 size_t offset = 0;
291 int status;
292 gauge_t *rates = NULL;
294 assert (0 == strcmp (ds->type, vl->type));
296 memset (ret, 0, ret_len);
298 #define BUFFER_ADD(...) do { \
299 status = ssnprintf (ret + offset, ret_len - offset, \
300 __VA_ARGS__); \
301 if (status < 1) \
302 { \
303 sfree (rates); \
304 return (-1); \
305 } \
306 else if (((size_t) status) >= (ret_len - offset)) \
307 { \
308 sfree (rates); \
309 return (-1); \
310 } \
311 else \
312 offset += ((size_t) status); \
313 } while (0)
315 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
316 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
317 else if (store_rates)
318 {
319 if (rates == NULL)
320 rates = uc_get_rate (ds, vl);
321 if (rates == NULL)
322 {
323 WARNING ("format_values: "
324 "uc_get_rate failed.");
325 return (-1);
326 }
327 BUFFER_ADD ("%g", rates[ds_num]);
328 }
329 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
330 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
331 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
332 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
333 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
334 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
335 else
336 {
337 ERROR ("format_values plugin: Unknown data source type: %i",
338 ds->ds[ds_num].type);
339 sfree (rates);
340 return (-1);
341 }
343 #undef BUFFER_ADD
345 sfree (rates);
346 return (0);
347 }
349 static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
350 char escape_char)
351 {
352 size_t i;
354 memset (dst, 0, dst_len);
356 if (src == NULL)
357 return;
359 for (i = 0; i < dst_len; i++)
360 {
361 if ((src[i] == '.')
362 || isspace ((int) src[i])
363 || iscntrl ((int) src[i]))
364 dst[i] = escape_char;
365 else
366 dst[i] = src[i];
368 if (src[i] == 0)
369 break;
370 }
371 }
373 static int wg_format_name (char *ret, int ret_len,
374 const char *hostname,
375 const char *plugin, const char *plugin_instance,
376 const char *type, const char *type_instance,
377 const char *prefix, const char *postfix,
378 const char *ds_name, char escape_char)
379 {
380 char n_hostname[DATA_MAX_NAME_LEN];
381 char n_plugin[DATA_MAX_NAME_LEN];
382 char n_plugin_instance[DATA_MAX_NAME_LEN];
383 char n_type[DATA_MAX_NAME_LEN];
384 char n_type_instance[DATA_MAX_NAME_LEN];
385 int status;
387 assert (hostname != NULL);
388 assert (plugin != NULL);
389 assert (type != NULL);
390 assert (ds_name != NULL);
392 if (prefix == NULL)
393 prefix = "";
395 if (postfix == NULL)
396 postfix = "";
398 wg_copy_escape_part (n_hostname, hostname,
399 sizeof (n_hostname), escape_char);
400 wg_copy_escape_part (n_plugin, plugin,
401 sizeof (n_plugin), escape_char);
402 wg_copy_escape_part (n_plugin_instance, plugin_instance,
403 sizeof (n_plugin_instance), escape_char);
404 wg_copy_escape_part (n_type, type,
405 sizeof (n_type), escape_char);
406 wg_copy_escape_part (n_type_instance, type_instance,
407 sizeof (n_type_instance), escape_char);
409 if (n_plugin_instance[0] == '\0')
410 {
411 if (n_type_instance[0] == '\0')
412 {
413 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
414 prefix, n_hostname, postfix, n_plugin, n_type, ds_name);
415 }
416 else
417 {
418 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
419 prefix, n_hostname, postfix, n_plugin, n_type,
420 n_type_instance, ds_name);
421 }
422 }
423 else
424 {
425 if (n_type_instance[0] == '\0')
426 {
427 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
428 prefix, n_hostname, postfix, n_plugin,
429 n_plugin_instance, n_type, ds_name);
430 }
431 else
432 {
433 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
434 prefix, n_hostname, postfix, n_plugin,
435 n_plugin_instance, n_type, n_type_instance, ds_name);
436 }
437 }
439 if ((status < 1) || (status >= ret_len))
440 return (-1);
442 return (0);
443 }
445 static int wg_send_message (const char* key, const char* value,
446 cdtime_t time, struct wg_callback *cb)
447 {
448 int status;
449 size_t message_len;
450 char message[1024];
452 message_len = (size_t) ssnprintf (message, sizeof (message),
453 "%s %s %.0f\n",
454 key,
455 value,
456 CDTIME_T_TO_DOUBLE(time));
457 if (message_len >= sizeof (message)) {
458 ERROR ("write_graphite plugin: message buffer too small: "
459 "Need %zu bytes.", message_len + 1);
460 return (-1);
461 }
464 pthread_mutex_lock (&cb->send_lock);
466 if (cb->sock_fd < 0)
467 {
468 status = wg_callback_init (cb);
469 if (status != 0)
470 {
471 ERROR ("write_graphite plugin: wg_callback_init failed.");
472 pthread_mutex_unlock (&cb->send_lock);
473 return (-1);
474 }
475 }
477 if (message_len >= cb->send_buf_free)
478 {
479 status = wg_flush_nolock (/* timeout = */ 0, cb);
480 if (status != 0)
481 {
482 pthread_mutex_unlock (&cb->send_lock);
483 return (status);
484 }
485 }
486 assert (message_len < cb->send_buf_free);
488 /* `message_len + 1' because `message_len' does not include the
489 * trailing null byte. Neither does `send_buffer_fill'. */
490 memcpy (cb->send_buf + cb->send_buf_fill,
491 message, message_len + 1);
492 cb->send_buf_fill += message_len;
493 cb->send_buf_free -= message_len;
495 DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
496 cb->node,
497 cb->service,
498 cb->send_buf_fill, sizeof (cb->send_buf),
499 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
500 message);
502 /* Check if we have enough space for this message. */
503 pthread_mutex_unlock (&cb->send_lock);
505 return (0);
506 }
508 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
509 struct wg_callback *cb)
510 {
511 char key[10*DATA_MAX_NAME_LEN];
512 char values[512];
514 int status, i;
516 if (0 != strcmp (ds->type, vl->type))
517 {
518 ERROR ("write_graphite plugin: DS type does not match "
519 "value list type");
520 return -1;
521 }
523 if (ds->ds_num > 1)
524 {
525 for (i = 0; i < ds->ds_num; i++)
526 {
527 /* Copy the identifier to `key' and escape it. */
528 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
529 if (status != 0)
530 {
531 ERROR ("write_graphite plugin: error with format_name");
532 return (status);
533 }
535 escape_string (key, sizeof (key));
536 /* Convert the values to an ASCII representation and put that
537 * into `values'. */
538 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
539 if (status != 0)
540 {
541 ERROR ("write_graphite plugin: error with "
542 "wg_format_values");
543 return (status);
544 }
546 /* Send the message to graphite */
547 status = wg_send_message (key, values, vl->time, cb);
548 if (status != 0)
549 {
550 ERROR ("write_graphite plugin: error with "
551 "wg_send_message");
552 return (status);
553 }
554 }
555 }
556 else
557 {
558 /* Copy the identifier to `key' and escape it. */
559 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
560 if (status != 0)
561 {
562 ERROR ("write_graphite plugin: error with format_name");
563 return (status);
564 }
566 escape_string (key, sizeof (key));
567 /* Convert the values to an ASCII representation and put that into
568 * `values'. */
569 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
570 if (status != 0)
571 {
572 ERROR ("write_graphite plugin: error with "
573 "wg_format_values");
574 return (status);
575 }
577 /* Send the message to graphite */
578 status = wg_send_message (key, values, vl->time, cb);
579 if (status != 0)
580 {
581 ERROR ("write_graphite plugin: error with "
582 "wg_send_message");
583 return (status);
584 }
585 }
587 return (0);
588 }
590 static int wg_write (const data_set_t *ds, const value_list_t *vl,
591 user_data_t *user_data)
592 {
593 struct wg_callback *cb;
594 int status;
596 if (user_data == NULL)
597 return (-EINVAL);
599 cb = user_data->data;
601 status = wg_write_messages (ds, vl, cb);
603 return (status);
604 }
606 static int config_set_char (char *dest,
607 oconfig_item_t *ci)
608 {
609 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
610 {
611 WARNING ("write_graphite plugin: The `%s' config option "
612 "needs exactly one string argument.", ci->key);
613 return (-1);
614 }
616 *dest = ci->values[0].value.string[0];
618 return (0);
619 }
621 static int wg_config_carbon (oconfig_item_t *ci)
622 {
623 struct wg_callback *cb;
624 user_data_t user_data;
625 int i;
627 cb = malloc (sizeof (*cb));
628 if (cb == NULL)
629 {
630 ERROR ("write_graphite plugin: malloc failed.");
631 return (-1);
632 }
633 memset (cb, 0, sizeof (*cb));
634 cb->sock_fd = -1;
635 cb->node = NULL;
636 cb->service = NULL;
637 cb->prefix = NULL;
638 cb->postfix = NULL;
639 cb->server = NULL;
640 cb->escape_char = '_';
642 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
644 for (i = 0; i < ci->children_num; i++)
645 {
646 oconfig_item_t *child = ci->children + i;
648 if (strcasecmp ("Host", child->key) == 0)
649 cf_util_get_string (child, &cb->node);
650 else if (strcasecmp ("Port", child->key) == 0)
651 cf_util_get_string (child, &cb->service);
652 else if (strcasecmp ("Prefix", child->key) == 0)
653 cf_util_get_string (child, &cb->prefix);
654 else if (strcasecmp ("Postfix", child->key) == 0)
655 cf_util_get_string (child, &cb->postfix);
656 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
657 config_set_char (&cb->escape_char, child);
658 else
659 {
660 ERROR ("write_graphite plugin: Invalid configuration "
661 "option: %s.", child->key);
662 }
663 }
665 DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
666 cb->node ? cb->node : WG_DEFAULT_NODE,
667 cb->service ? cb->service : WG_DEFAULT_SERVICE);
669 memset (&user_data, 0, sizeof (user_data));
670 user_data.data = cb;
671 user_data.free_func = NULL;
672 plugin_register_flush ("write_graphite", wg_flush, &user_data);
674 user_data.free_func = wg_callback_free;
675 plugin_register_write ("write_graphite", wg_write, &user_data);
677 return (0);
678 }
680 static int wg_config (oconfig_item_t *ci)
681 {
682 int i;
684 for (i = 0; i < ci->children_num; i++)
685 {
686 oconfig_item_t *child = ci->children + i;
688 if (strcasecmp ("Carbon", child->key) == 0)
689 wg_config_carbon (child);
690 else
691 {
692 ERROR ("write_graphite plugin: Invalid configuration "
693 "option: %s.", child->key);
694 }
695 }
697 return (0);
698 }
700 void module_register (void)
701 {
702 plugin_register_complex_config ("write_graphite", wg_config);
703 }
705 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */