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 * 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_complain.h"
50 #include "utils_parse_option.h"
51 #include "utils_format_graphite.h"
53 /* Folks without pthread will need to disable this plugin. */
54 #include <pthread.h>
56 #include <sys/socket.h>
57 #include <netdb.h>
59 #ifndef WG_DEFAULT_NODE
60 # define WG_DEFAULT_NODE "localhost"
61 #endif
63 #ifndef WG_DEFAULT_SERVICE
64 # define WG_DEFAULT_SERVICE "2003"
65 #endif
67 #ifndef WG_DEFAULT_ESCAPE
68 # define WG_DEFAULT_ESCAPE '_'
69 #endif
71 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
72 #ifndef WG_SEND_BUF_SIZE
73 # define WG_SEND_BUF_SIZE 1428
74 #endif
76 /*
77 * Private variables
78 */
79 struct wg_callback
80 {
81 int sock_fd;
83 char *node;
84 char *service;
85 char *prefix;
86 char *postfix;
87 char escape_char;
89 unsigned int format_flags;
91 char send_buf[WG_SEND_BUF_SIZE];
92 size_t send_buf_free;
93 size_t send_buf_fill;
94 cdtime_t send_buf_init_time;
96 pthread_mutex_t send_lock;
97 c_complain_t init_complaint;
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 char errbuf[1024];
120 ERROR ("write_graphite plugin: send failed with status %zi (%s)",
121 status, sstrerror (errno, errbuf, sizeof (errbuf)));
124 close (cb->sock_fd);
125 cb->sock_fd = -1;
127 return (-1);
128 }
130 return (0);
131 }
133 /* NOTE: You must hold cb->send_lock when calling this function! */
134 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
135 {
136 int status;
138 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
139 "send_buf_fill = %zu;",
140 (double)timeout,
141 cb->send_buf_fill);
143 /* timeout == 0 => flush unconditionally */
144 if (timeout > 0)
145 {
146 cdtime_t now;
148 now = cdtime ();
149 if ((cb->send_buf_init_time + timeout) > now)
150 return (0);
151 }
153 if (cb->send_buf_fill <= 0)
154 {
155 cb->send_buf_init_time = cdtime ();
156 return (0);
157 }
159 status = wg_send_buffer (cb);
160 wg_reset_buffer (cb);
162 return (status);
163 }
165 static int wg_callback_init (struct wg_callback *cb)
166 {
167 struct addrinfo ai_hints;
168 struct addrinfo *ai_list;
169 struct addrinfo *ai_ptr;
170 int status;
172 const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
173 const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
175 if (cb->sock_fd > 0)
176 return (0);
178 memset (&ai_hints, 0, sizeof (ai_hints));
179 #ifdef AI_ADDRCONFIG
180 ai_hints.ai_flags |= AI_ADDRCONFIG;
181 #endif
182 ai_hints.ai_family = AF_UNSPEC;
183 ai_hints.ai_socktype = SOCK_STREAM;
185 ai_list = NULL;
187 status = getaddrinfo (node, service, &ai_hints, &ai_list);
188 if (status != 0)
189 {
190 ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
191 node, service, gai_strerror (status));
192 return (-1);
193 }
195 assert (ai_list != NULL);
196 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
197 {
198 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
199 ai_ptr->ai_protocol);
200 if (cb->sock_fd < 0)
201 continue;
203 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
204 if (status != 0)
205 {
206 close (cb->sock_fd);
207 cb->sock_fd = -1;
208 continue;
209 }
211 break;
212 }
214 freeaddrinfo (ai_list);
216 if (cb->sock_fd < 0)
217 {
218 char errbuf[1024];
219 c_complain (LOG_ERR, &cb->init_complaint,
220 "write_graphite plugin: Connecting to %s:%s failed. "
221 "The last error was: %s", node, service,
222 sstrerror (errno, errbuf, sizeof (errbuf)));
223 close (cb->sock_fd);
224 return (-1);
225 }
226 else
227 {
228 c_release (LOG_INFO, &cb->init_complaint,
229 "write_graphite plugin: Successfully connected to %s:%s.",
230 node, service);
231 }
233 wg_reset_buffer (cb);
235 return (0);
236 }
238 static void wg_callback_free (void *data)
239 {
240 struct wg_callback *cb;
242 if (data == NULL)
243 return;
245 cb = data;
247 pthread_mutex_lock (&cb->send_lock);
249 wg_flush_nolock (/* timeout = */ 0, cb);
251 close(cb->sock_fd);
252 cb->sock_fd = -1;
254 sfree(cb->node);
255 sfree(cb->service);
256 sfree(cb->prefix);
257 sfree(cb->postfix);
259 pthread_mutex_destroy (&cb->send_lock);
261 sfree(cb);
262 }
264 static int wg_flush (cdtime_t timeout,
265 const char *identifier __attribute__((unused)),
266 user_data_t *user_data)
267 {
268 struct wg_callback *cb;
269 int status;
271 if (user_data == NULL)
272 return (-EINVAL);
274 cb = user_data->data;
276 pthread_mutex_lock (&cb->send_lock);
278 if (cb->sock_fd < 0)
279 {
280 status = wg_callback_init (cb);
281 if (status != 0)
282 {
283 /* An error message has already been printed. */
284 pthread_mutex_unlock (&cb->send_lock);
285 return (-1);
286 }
287 }
289 status = wg_flush_nolock (timeout, cb);
290 pthread_mutex_unlock (&cb->send_lock);
292 return (status);
293 }
295 static int wg_send_message (char const *message, struct wg_callback *cb)
296 {
297 int status;
298 size_t message_len;
300 message_len = strlen (message);
302 pthread_mutex_lock (&cb->send_lock);
304 if (cb->sock_fd < 0)
305 {
306 status = wg_callback_init (cb);
307 if (status != 0)
308 {
309 /* An error message has already been printed. */
310 pthread_mutex_unlock (&cb->send_lock);
311 return (-1);
312 }
313 }
315 if (message_len >= cb->send_buf_free)
316 {
317 status = wg_flush_nolock (/* timeout = */ 0, cb);
318 if (status != 0)
319 {
320 pthread_mutex_unlock (&cb->send_lock);
321 return (status);
322 }
323 }
325 /* Assert that we have enough space for this message. */
326 assert (message_len < cb->send_buf_free);
328 /* `message_len + 1' because `message_len' does not include the
329 * trailing null byte. Neither does `send_buffer_fill'. */
330 memcpy (cb->send_buf + cb->send_buf_fill,
331 message, message_len + 1);
332 cb->send_buf_fill += message_len;
333 cb->send_buf_free -= message_len;
335 DEBUG ("write_graphite plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
336 cb->node,
337 cb->service,
338 cb->send_buf_fill, sizeof (cb->send_buf),
339 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
340 message);
342 pthread_mutex_unlock (&cb->send_lock);
344 return (0);
345 }
347 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
348 struct wg_callback *cb)
349 {
350 char buffer[WG_SEND_BUF_SIZE];
351 int status;
353 if (0 != strcmp (ds->type, vl->type))
354 {
355 ERROR ("write_graphite plugin: DS type does not match "
356 "value list type");
357 return -1;
358 }
360 memset (buffer, 0, sizeof (buffer));
361 status = format_graphite (buffer, sizeof (buffer), ds, vl,
362 cb->prefix, cb->postfix, cb->escape_char, cb->format_flags);
363 if (status != 0) /* error message has been printed already. */
364 return (status);
366 /* Send the message to graphite */
367 wg_send_message (buffer, cb);
368 if (status != 0)
369 {
370 /* An error message has already been printed. */
371 return (status);
372 }
374 return (0);
375 } /* int wg_write_messages */
377 static int wg_write (const data_set_t *ds, const value_list_t *vl,
378 user_data_t *user_data)
379 {
380 struct wg_callback *cb;
381 int status;
383 if (user_data == NULL)
384 return (EINVAL);
386 cb = user_data->data;
388 status = wg_write_messages (ds, vl, cb);
390 return (status);
391 }
393 static int config_set_char (char *dest,
394 oconfig_item_t *ci)
395 {
396 char buffer[4];
397 int status;
399 memset (buffer, 0, sizeof (buffer));
401 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
402 if (status != 0)
403 return (status);
405 if (buffer[0] == 0)
406 {
407 ERROR ("write_graphite plugin: Cannot use an empty string for the "
408 "\"EscapeCharacter\" option.");
409 return (-1);
410 }
412 if (buffer[1] != 0)
413 {
414 WARNING ("write_graphite plugin: Only the first character of the "
415 "\"EscapeCharacter\" option ('%c') will be used.",
416 (int) buffer[0]);
417 }
419 *dest = buffer[0];
421 return (0);
422 }
424 static int wg_config_carbon (oconfig_item_t *ci)
425 {
426 struct wg_callback *cb;
427 user_data_t user_data;
428 char callback_name[DATA_MAX_NAME_LEN];
429 int i;
431 cb = malloc (sizeof (*cb));
432 if (cb == NULL)
433 {
434 ERROR ("write_graphite plugin: malloc failed.");
435 return (-1);
436 }
437 memset (cb, 0, sizeof (*cb));
438 cb->sock_fd = -1;
439 cb->node = NULL;
440 cb->service = NULL;
441 cb->prefix = NULL;
442 cb->postfix = NULL;
443 cb->escape_char = WG_DEFAULT_ESCAPE;
444 cb->format_flags = GRAPHITE_STORE_RATES;
446 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
447 C_COMPLAIN_INIT (&cb->init_complaint);
449 for (i = 0; i < ci->children_num; i++)
450 {
451 oconfig_item_t *child = ci->children + i;
453 if (strcasecmp ("Host", child->key) == 0)
454 cf_util_get_string (child, &cb->node);
455 else if (strcasecmp ("Port", child->key) == 0)
456 cf_util_get_service (child, &cb->service);
457 else if (strcasecmp ("Prefix", child->key) == 0)
458 cf_util_get_string (child, &cb->prefix);
459 else if (strcasecmp ("Postfix", child->key) == 0)
460 cf_util_get_string (child, &cb->postfix);
461 else if (strcasecmp ("StoreRates", child->key) == 0)
462 cf_util_get_flag (child, &cb->format_flags,
463 GRAPHITE_STORE_RATES);
464 else if (strcasecmp ("SeparateInstances", child->key) == 0)
465 cf_util_get_flag (child, &cb->format_flags,
466 GRAPHITE_SEPARATE_INSTANCES);
467 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
468 cf_util_get_flag (child, &cb->format_flags,
469 GRAPHITE_ALWAYS_APPEND_DS);
470 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
471 config_set_char (&cb->escape_char, child);
472 else
473 {
474 ERROR ("write_graphite plugin: Invalid configuration "
475 "option: %s.", child->key);
476 }
477 }
479 ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s",
480 cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
481 cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE);
483 memset (&user_data, 0, sizeof (user_data));
484 user_data.data = cb;
485 user_data.free_func = wg_callback_free;
486 plugin_register_write (callback_name, wg_write, &user_data);
488 user_data.free_func = NULL;
489 plugin_register_flush (callback_name, wg_flush, &user_data);
491 return (0);
492 }
494 static int wg_config (oconfig_item_t *ci)
495 {
496 int i;
498 for (i = 0; i < ci->children_num; i++)
499 {
500 oconfig_item_t *child = ci->children + i;
502 if (strcasecmp ("Carbon", child->key) == 0)
503 wg_config_carbon (child);
504 else
505 {
506 ERROR ("write_graphite plugin: Invalid configuration "
507 "option: %s.", child->key);
508 }
509 }
511 return (0);
512 }
514 void module_register (void)
515 {
516 plugin_register_complex_config ("write_graphite", wg_config);
517 }
519 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */