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"
50 #include "utils_format_graphite.h"
52 /* Folks without pthread will need to disable this plugin. */
53 #include <pthread.h>
55 #include <sys/socket.h>
56 #include <netdb.h>
58 #ifndef WG_DEFAULT_NODE
59 # define WG_DEFAULT_NODE "localhost"
60 #endif
62 #ifndef WG_DEFAULT_SERVICE
63 # define WG_DEFAULT_SERVICE "2003"
64 #endif
66 #ifndef WG_DEFAULT_ESCAPE
67 # define WG_DEFAULT_ESCAPE '_'
68 #endif
70 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
71 #ifndef WG_SEND_BUF_SIZE
72 # define WG_SEND_BUF_SIZE 1428
73 #endif
75 /*
76 * Private variables
77 */
78 struct wg_callback
79 {
80 int sock_fd;
82 char *name;
84 char *node;
85 char *service;
86 char *prefix;
87 char *postfix;
88 char escape_char;
90 _Bool store_rates;
91 _Bool separate_instances;
92 _Bool always_append_ds;
94 char send_buf[WG_SEND_BUF_SIZE];
95 size_t send_buf_free;
96 size_t send_buf_fill;
97 cdtime_t send_buf_init_time;
99 pthread_mutex_t send_lock;
100 };
103 /*
104 * Functions
105 */
106 static void wg_reset_buffer (struct wg_callback *cb)
107 {
108 memset (cb->send_buf, 0, sizeof (cb->send_buf));
109 cb->send_buf_free = sizeof (cb->send_buf);
110 cb->send_buf_fill = 0;
111 cb->send_buf_init_time = cdtime ();
112 }
114 static int wg_send_buffer (struct wg_callback *cb)
115 {
116 ssize_t status = 0;
118 status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
119 if (status < 0)
120 {
121 char errbuf[1024];
122 ERROR ("write_graphite plugin: send failed with status %zi (%s)",
123 status, sstrerror (errno, errbuf, sizeof (errbuf)));
126 close (cb->sock_fd);
127 cb->sock_fd = -1;
129 return (-1);
130 }
132 return (0);
133 }
135 /* NOTE: You must hold cb->send_lock when calling this function! */
136 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
137 {
138 int status;
140 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
141 "send_buf_fill = %zu;",
142 (double)timeout,
143 cb->send_buf_fill);
145 /* timeout == 0 => flush unconditionally */
146 if (timeout > 0)
147 {
148 cdtime_t now;
150 now = cdtime ();
151 if ((cb->send_buf_init_time + timeout) > now)
152 return (0);
153 }
155 if (cb->send_buf_fill <= 0)
156 {
157 cb->send_buf_init_time = cdtime ();
158 return (0);
159 }
161 status = wg_send_buffer (cb);
162 wg_reset_buffer (cb);
164 return (status);
165 }
167 static int wg_callback_init (struct wg_callback *cb)
168 {
169 struct addrinfo ai_hints;
170 struct addrinfo *ai_list;
171 struct addrinfo *ai_ptr;
172 int status;
174 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
175 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
177 if (cb->sock_fd > 0)
178 return (0);
180 memset (&ai_hints, 0, sizeof (ai_hints));
181 #ifdef AI_ADDRCONFIG
182 ai_hints.ai_flags |= AI_ADDRCONFIG;
183 #endif
184 ai_hints.ai_family = AF_UNSPEC;
185 ai_hints.ai_socktype = SOCK_STREAM;
187 ai_list = NULL;
189 status = getaddrinfo (node, service, &ai_hints, &ai_list);
190 if (status != 0)
191 {
192 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
193 node, service, gai_strerror (status));
194 return (-1);
195 }
197 assert (ai_list != NULL);
198 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
199 {
200 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
201 ai_ptr->ai_protocol);
202 if (cb->sock_fd < 0)
203 continue;
205 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
206 if (status != 0)
207 {
208 close (cb->sock_fd);
209 cb->sock_fd = -1;
210 continue;
211 }
213 break;
214 }
216 freeaddrinfo (ai_list);
218 if (cb->sock_fd < 0)
219 {
220 char errbuf[1024];
221 ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
222 "The last error was: %s", node, service,
223 sstrerror (errno, errbuf, sizeof (errbuf)));
224 close (cb->sock_fd);
225 return (-1);
226 }
228 wg_reset_buffer (cb);
230 return (0);
231 }
233 static void wg_callback_free (void *data)
234 {
235 struct wg_callback *cb;
237 if (data == NULL)
238 return;
240 cb = data;
242 pthread_mutex_lock (&cb->send_lock);
244 wg_flush_nolock (/* timeout = */ 0, cb);
246 close(cb->sock_fd);
247 cb->sock_fd = -1;
249 sfree(cb->name);
250 sfree(cb->node);
251 sfree(cb->service);
252 sfree(cb->prefix);
253 sfree(cb->postfix);
255 pthread_mutex_destroy (&cb->send_lock);
257 sfree(cb);
258 }
260 static int wg_flush (cdtime_t timeout,
261 const char *identifier __attribute__((unused)),
262 user_data_t *user_data)
263 {
264 struct wg_callback *cb;
265 int status;
267 if (user_data == NULL)
268 return (-EINVAL);
270 cb = user_data->data;
272 pthread_mutex_lock (&cb->send_lock);
274 if (cb->sock_fd < 0)
275 {
276 status = wg_callback_init (cb);
277 if (status != 0)
278 {
279 ERROR ("write_graphite plugin: wg_callback_init failed.");
280 pthread_mutex_unlock (&cb->send_lock);
281 return (-1);
282 }
283 }
285 status = wg_flush_nolock (timeout, cb);
286 pthread_mutex_unlock (&cb->send_lock);
288 return (status);
289 }
291 static int wg_send_message (char const *message, struct wg_callback *cb)
292 {
293 int status;
294 size_t message_len;
296 message_len = strlen (message);
298 pthread_mutex_lock (&cb->send_lock);
300 if (cb->sock_fd < 0)
301 {
302 status = wg_callback_init (cb);
303 if (status != 0)
304 {
305 ERROR ("write_graphite plugin: wg_callback_init failed.");
306 pthread_mutex_unlock (&cb->send_lock);
307 return (-1);
308 }
309 }
311 if (message_len >= cb->send_buf_free)
312 {
313 status = wg_flush_nolock (/* timeout = */ 0, cb);
314 if (status != 0)
315 {
316 pthread_mutex_unlock (&cb->send_lock);
317 return (status);
318 }
319 }
321 /* Assert that we have enough space for this message. */
322 assert (message_len < cb->send_buf_free);
324 /* `message_len + 1' because `message_len' does not include the
325 * trailing null byte. Neither does `send_buffer_fill'. */
326 memcpy (cb->send_buf + cb->send_buf_fill,
327 message, message_len + 1);
328 cb->send_buf_fill += message_len;
329 cb->send_buf_free -= message_len;
331 DEBUG ("write_graphite plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
332 cb->node,
333 cb->service,
334 cb->send_buf_fill, sizeof (cb->send_buf),
335 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
336 message);
338 pthread_mutex_unlock (&cb->send_lock);
340 return (0);
341 }
343 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
344 struct wg_callback *cb)
345 {
346 char buffer[4096];
347 int status;
349 if (0 != strcmp (ds->type, vl->type))
350 {
351 ERROR ("write_graphite plugin: DS type does not match "
352 "value list type");
353 return -1;
354 }
356 memset (buffer, 0, sizeof (buffer));
357 status = format_graphite (buffer, sizeof (buffer), ds, vl,
358 cb->prefix, cb->postfix, cb->escape_char, cb->store_rates);
359 if (status != 0) /* error message has been printed already. */
360 return (status);
362 wg_send_message (buffer, cb);
363 if (status != 0)
364 {
365 ERROR ("write_graphite plugin: wg_send_message failed "
366 "with status %i.", status);
367 return (status);
368 }
370 return (0);
371 } /* int wg_write_messages */
373 static int wg_write (const data_set_t *ds, const value_list_t *vl,
374 user_data_t *user_data)
375 {
376 struct wg_callback *cb;
377 int status;
379 if (user_data == NULL)
380 return (EINVAL);
382 cb = user_data->data;
384 status = wg_write_messages (ds, vl, cb);
386 return (status);
387 }
389 static int config_set_char (char *dest,
390 oconfig_item_t *ci)
391 {
392 char buffer[4];
393 int status;
395 memset (buffer, 0, sizeof (buffer));
397 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
398 if (status != 0)
399 return (status);
401 if (buffer[0] == 0)
402 {
403 ERROR ("write_graphite plugin: Cannot use an empty string for the "
404 "\"EscapeCharacter\" option.");
405 return (-1);
406 }
408 if (buffer[1] != 0)
409 {
410 WARNING ("write_graphite plugin: Only the first character of the "
411 "\"EscapeCharacter\" option ('%c') will be used.",
412 (int) buffer[0]);
413 }
415 *dest = buffer[0];
417 return (0);
418 }
420 static int wg_config_node (oconfig_item_t *ci)
421 {
422 struct wg_callback *cb;
423 user_data_t user_data;
424 char callback_name[DATA_MAX_NAME_LEN];
425 int i;
427 cb = malloc (sizeof (*cb));
428 if (cb == NULL)
429 {
430 ERROR ("write_graphite plugin: malloc failed.");
431 return (-1);
432 }
433 memset (cb, 0, sizeof (*cb));
434 cb->sock_fd = -1;
435 cb->name = NULL;
436 cb->node = NULL;
437 cb->service = NULL;
438 cb->prefix = NULL;
439 cb->postfix = NULL;
440 cb->escape_char = WG_DEFAULT_ESCAPE;
441 cb->store_rates = 1;
443 /* FIXME: Legacy configuration syntax. */
444 if (strcasecmp ("Carbon", ci->key) != 0)
445 {
446 int status = cf_util_get_string (ci, &cb->name);
447 if (status != 0)
448 {
449 wg_callback_free (cb);
450 return (status);
451 }
452 }
454 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
456 for (i = 0; i < ci->children_num; i++)
457 {
458 oconfig_item_t *child = ci->children + i;
460 if (strcasecmp ("Host", child->key) == 0)
461 cf_util_get_string (child, &cb->node);
462 else if (strcasecmp ("Port", child->key) == 0)
463 cf_util_get_service (child, &cb->service);
464 else if (strcasecmp ("Prefix", child->key) == 0)
465 cf_util_get_string (child, &cb->prefix);
466 else if (strcasecmp ("Postfix", child->key) == 0)
467 cf_util_get_string (child, &cb->postfix);
468 else if (strcasecmp ("StoreRates", child->key) == 0)
469 cf_util_get_boolean (child, &cb->store_rates);
470 else if (strcasecmp ("SeparateInstances", child->key) == 0)
471 cf_util_get_boolean (child, &cb->separate_instances);
472 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
473 cf_util_get_boolean (child, &cb->always_append_ds);
474 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
475 config_set_char (&cb->escape_char, child);
476 else
477 {
478 ERROR ("write_graphite plugin: Invalid configuration "
479 "option: %s.", child->key);
480 }
481 }
483 /* FIXME: Legacy configuration syntax. */
484 if (cb->name == NULL)
485 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s",
486 cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
487 cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE);
488 else
489 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s",
490 cb->name);
492 memset (&user_data, 0, sizeof (user_data));
493 user_data.data = cb;
494 user_data.free_func = wg_callback_free;
495 plugin_register_write (callback_name, wg_write, &user_data);
497 user_data.free_func = NULL;
498 plugin_register_flush (callback_name, wg_flush, &user_data);
500 return (0);
501 }
503 static int wg_config (oconfig_item_t *ci)
504 {
505 int i;
507 for (i = 0; i < ci->children_num; i++)
508 {
509 oconfig_item_t *child = ci->children + i;
511 if (strcasecmp ("Node", child->key) == 0)
512 wg_config_node (child);
513 /* FIXME: Remove this legacy mode in version 6. */
514 else if (strcasecmp ("Carbon", child->key) == 0)
515 wg_config_node (child);
516 else
517 {
518 ERROR ("write_graphite plugin: Invalid configuration "
519 "option: %s.", child->key);
520 }
521 }
523 return (0);
524 }
526 void module_register (void)
527 {
528 plugin_register_complex_config ("write_graphite", wg_config);
529 }
531 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */