Code

8ae15ba0c5a2536f332b5de0358a7a13ed127d56
[collectd.git] / src / write_graphite.c
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    *node;
83     char    *service;
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 };
99 /*
100  * Functions
101  */
102 static void wg_reset_buffer (struct wg_callback *cb)
104     memset (cb->send_buf, 0, sizeof (cb->send_buf));
105     cb->send_buf_free = sizeof (cb->send_buf);
106     cb->send_buf_fill = 0;
107     cb->send_buf_init_time = cdtime ();
110 static int wg_send_buffer (struct wg_callback *cb)
112     ssize_t status = 0;
114     status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
115     if (status < 0)
116     {
117         char errbuf[1024];
118         ERROR ("write_graphite plugin: send failed with status %zi (%s)",
119                 status, sstrerror (errno, errbuf, sizeof (errbuf)));
122         close (cb->sock_fd);
123         cb->sock_fd = -1;
125         return (-1);
126     }
128     return (0);
131 /* NOTE: You must hold cb->send_lock when calling this function! */
132 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
134     int status;
136     DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
137             "send_buf_fill = %zu;",
138             (double)timeout,
139             cb->send_buf_fill);
141     /* timeout == 0  => flush unconditionally */
142     if (timeout > 0)
143     {
144         cdtime_t now;
146         now = cdtime ();
147         if ((cb->send_buf_init_time + timeout) > now)
148             return (0);
149     }
151     if (cb->send_buf_fill <= 0)
152     {
153         cb->send_buf_init_time = cdtime ();
154         return (0);
155     }
157     status = wg_send_buffer (cb);
158     wg_reset_buffer (cb);
160     return (status);
163 static int wg_callback_init (struct wg_callback *cb)
165     struct addrinfo ai_hints;
166     struct addrinfo *ai_list;
167     struct addrinfo *ai_ptr;
168     int status;
170     const char *node = cb->node ? cb->node : WG_DEFAULT_NODE;
171     const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE;
173     if (cb->sock_fd > 0)
174         return (0);
176     memset (&ai_hints, 0, sizeof (ai_hints));
177 #ifdef AI_ADDRCONFIG
178     ai_hints.ai_flags |= AI_ADDRCONFIG;
179 #endif
180     ai_hints.ai_family = AF_UNSPEC;
181     ai_hints.ai_socktype = SOCK_STREAM;
183     ai_list = NULL;
185     status = getaddrinfo (node, service, &ai_hints, &ai_list);
186     if (status != 0)
187     {
188         ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s",
189                 node, service, gai_strerror (status));
190         return (-1);
191     }
193     assert (ai_list != NULL);
194     for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
195     {
196         cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
197                 ai_ptr->ai_protocol);
198         if (cb->sock_fd < 0)
199             continue;
201         status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
202         if (status != 0)
203         {
204             close (cb->sock_fd);
205             cb->sock_fd = -1;
206             continue;
207         }
209         break;
210     }
212     freeaddrinfo (ai_list);
214     if (cb->sock_fd < 0)
215     {
216         char errbuf[1024];
217         ERROR ("write_graphite plugin: Connecting to %s:%s failed. "
218                 "The last error was: %s", node, service,
219                 sstrerror (errno, errbuf, sizeof (errbuf)));
220         close (cb->sock_fd);
221         return (-1);
222     }
224     wg_reset_buffer (cb);
226     return (0);
229 static void wg_callback_free (void *data)
231     struct wg_callback *cb;
233     if (data == NULL)
234         return;
236     cb = data;
238     pthread_mutex_lock (&cb->send_lock);
240     wg_flush_nolock (/* timeout = */ 0, cb);
242     close(cb->sock_fd);
243     cb->sock_fd = -1;
245     sfree(cb->node);
246     sfree(cb->service);
247     sfree(cb->prefix);
248     sfree(cb->postfix);
250     pthread_mutex_destroy (&cb->send_lock);
252     sfree(cb);
255 static int wg_flush (cdtime_t timeout,
256         const char *identifier __attribute__((unused)),
257         user_data_t *user_data)
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);
286 static int wg_send_message (char const *message, struct wg_callback *cb)
288     int status;
289     size_t message_len;
291     message_len = strlen (message);
293     pthread_mutex_lock (&cb->send_lock);
295     if (cb->sock_fd < 0)
296     {
297         status = wg_callback_init (cb);
298         if (status != 0)
299         {
300             ERROR ("write_graphite plugin: wg_callback_init failed.");
301             pthread_mutex_unlock (&cb->send_lock);
302             return (-1);
303         }
304     }
306     if (message_len >= cb->send_buf_free)
307     {
308         status = wg_flush_nolock (/* timeout = */ 0, cb);
309         if (status != 0)
310         {
311             pthread_mutex_unlock (&cb->send_lock);
312             return (status);
313         }
314     }
316     /* Assert that we have enough space for this message. */
317     assert (message_len < cb->send_buf_free);
319     /* `message_len + 1' because `message_len' does not include the
320      * trailing null byte. Neither does `send_buffer_fill'. */
321     memcpy (cb->send_buf + cb->send_buf_fill,
322             message, message_len + 1);
323     cb->send_buf_fill += message_len;
324     cb->send_buf_free -= message_len;
326     DEBUG ("write_graphite plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
327             cb->node,
328             cb->service,
329             cb->send_buf_fill, sizeof (cb->send_buf),
330             100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
331             message);
333     pthread_mutex_unlock (&cb->send_lock);
335     return (0);
338 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
339         struct wg_callback *cb)
341     char buffer[4096];
342     int status;
344     if (0 != strcmp (ds->type, vl->type))
345     {
346         ERROR ("write_graphite plugin: DS type does not match "
347                 "value list type");
348         return -1;
349     }
351     memset (buffer, 0, sizeof (buffer));
352     status = format_graphite (buffer, sizeof (buffer), ds, vl,
353             cb->prefix, cb->postfix, cb->escape_char, cb->format_flags);
354     if (status != 0) /* error message has been printed already. */
355         return (status);
357     wg_send_message (buffer, cb);
358     if (status != 0)
359     {
360         ERROR ("write_graphite plugin: wg_send_message failed "
361                 "with status %i.", status);
362         return (status);
363     }
365     return (0);
366 } /* int wg_write_messages */
368 static int wg_write (const data_set_t *ds, const value_list_t *vl,
369         user_data_t *user_data)
371     struct wg_callback *cb;
372     int status;
374     if (user_data == NULL)
375         return (EINVAL);
377     cb = user_data->data;
379     status = wg_write_messages (ds, vl, cb);
381     return (status);
384 static int config_set_char (char *dest,
385         oconfig_item_t *ci)
387     char buffer[4];
388     int status;
390     memset (buffer, 0, sizeof (buffer));
392     status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
393     if (status != 0)
394         return (status);
396     if (buffer[0] == 0)
397     {
398         ERROR ("write_graphite plugin: Cannot use an empty string for the "
399                 "\"EscapeCharacter\" option.");
400         return (-1);
401     }
403     if (buffer[1] != 0)
404     {
405         WARNING ("write_graphite plugin: Only the first character of the "
406                 "\"EscapeCharacter\" option ('%c') will be used.",
407                 (int) buffer[0]);
408     }
410     *dest = buffer[0];
412     return (0);
415 static int wg_config_carbon (oconfig_item_t *ci)
417     struct wg_callback *cb;
418     user_data_t user_data;
419     char callback_name[DATA_MAX_NAME_LEN];
420     int i;
422     cb = malloc (sizeof (*cb));
423     if (cb == NULL)
424     {
425         ERROR ("write_graphite plugin: malloc failed.");
426         return (-1);
427     }
428     memset (cb, 0, sizeof (*cb));
429     cb->sock_fd = -1;
430     cb->node = NULL;
431     cb->service = NULL;
432     cb->prefix = NULL;
433     cb->postfix = NULL;
434     cb->escape_char = WG_DEFAULT_ESCAPE;
435     cb->format_flags = GRAPHITE_STORE_RATES;
437     pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
439     for (i = 0; i < ci->children_num; i++)
440     {
441         oconfig_item_t *child = ci->children + i;
443         if (strcasecmp ("Host", child->key) == 0)
444             cf_util_get_string (child, &cb->node);
445         else if (strcasecmp ("Port", child->key) == 0)
446             cf_util_get_service (child, &cb->service);
447         else if (strcasecmp ("Prefix", child->key) == 0)
448             cf_util_get_string (child, &cb->prefix);
449         else if (strcasecmp ("Postfix", child->key) == 0)
450             cf_util_get_string (child, &cb->postfix);
451         else if (strcasecmp ("StoreRates", child->key) == 0)
452             cf_util_get_flag (child, &cb->format_flags,
453                     GRAPHITE_STORE_RATES);
454         else if (strcasecmp ("SeparateInstances", child->key) == 0)
455             cf_util_get_flag (child, &cb->format_flags,
456                     GRAPHITE_SEPARATE_INSTANCES);
457         else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
458             cf_util_get_flag (child, &cb->format_flags,
459                     GRAPHITE_ALWAYS_APPEND_DS);
460         else if (strcasecmp ("EscapeCharacter", child->key) == 0)
461             config_set_char (&cb->escape_char, child);
462         else
463         {
464             ERROR ("write_graphite plugin: Invalid configuration "
465                         "option: %s.", child->key);
466         }
467     }
469     ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s",
470             cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
471             cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE);
473     memset (&user_data, 0, sizeof (user_data));
474     user_data.data = cb;
475     user_data.free_func = wg_callback_free;
476     plugin_register_write (callback_name, wg_write, &user_data);
478     user_data.free_func = NULL;
479     plugin_register_flush (callback_name, wg_flush, &user_data);
481     return (0);
484 static int wg_config (oconfig_item_t *ci)
486     int i;
488     for (i = 0; i < ci->children_num; i++)
489     {
490         oconfig_item_t *child = ci->children + i;
492         if (strcasecmp ("Carbon", child->key) == 0)
493             wg_config_carbon (child);
494         else
495         {
496             ERROR ("write_graphite plugin: Invalid configuration "
497                     "option: %s.", child->key);
498         }
499     }
501     return (0);
504 void module_register (void)
506     plugin_register_complex_config ("write_graphite", wg_config);
509 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */