Code

949a8425178f700b952787f15bc0426f4f6d7299
[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-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    *name;
85     char    *node;
86     char    *service;
87     char    *prefix;
88     char    *postfix;
89     char     escape_char;
91     unsigned int format_flags;
93     char     send_buf[WG_SEND_BUF_SIZE];
94     size_t   send_buf_free;
95     size_t   send_buf_fill;
96     cdtime_t send_buf_init_time;
98     pthread_mutex_t send_lock;
99     c_complain_t init_complaint;
100 };
103 /*
104  * Functions
105  */
106 static void wg_reset_buffer (struct wg_callback *cb)
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 ();
114 static int wg_send_buffer (struct wg_callback *cb)
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);
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)
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);
167 static int wg_callback_init (struct wg_callback *cb)
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         c_complain (LOG_ERR, &cb->init_complaint,
222                 "write_graphite plugin: Connecting to %s:%s failed. "
223                 "The last error was: %s", node, service,
224                 sstrerror (errno, errbuf, sizeof (errbuf)));
225         close (cb->sock_fd);
226         return (-1);
227     }
228     else
229     {
230         c_release (LOG_INFO, &cb->init_complaint,
231                 "write_graphite plugin: Successfully connected to %s:%s.",
232                 node, service);
233     }
235     wg_reset_buffer (cb);
237     return (0);
240 static void wg_callback_free (void *data)
242     struct wg_callback *cb;
244     if (data == NULL)
245         return;
247     cb = data;
249     pthread_mutex_lock (&cb->send_lock);
251     wg_flush_nolock (/* timeout = */ 0, cb);
253     close(cb->sock_fd);
254     cb->sock_fd = -1;
256     sfree(cb->name);
257     sfree(cb->node);
258     sfree(cb->service);
259     sfree(cb->prefix);
260     sfree(cb->postfix);
262     pthread_mutex_destroy (&cb->send_lock);
264     sfree(cb);
267 static int wg_flush (cdtime_t timeout,
268         const char *identifier __attribute__((unused)),
269         user_data_t *user_data)
271     struct wg_callback *cb;
272     int status;
274     if (user_data == NULL)
275         return (-EINVAL);
277     cb = user_data->data;
279     pthread_mutex_lock (&cb->send_lock);
281     if (cb->sock_fd < 0)
282     {
283         status = wg_callback_init (cb);
284         if (status != 0)
285         {
286             /* An error message has already been printed. */
287             pthread_mutex_unlock (&cb->send_lock);
288             return (-1);
289         }
290     }
292     status = wg_flush_nolock (timeout, cb);
293     pthread_mutex_unlock (&cb->send_lock);
295     return (status);
298 static int wg_send_message (char const *message, struct wg_callback *cb)
300     int status;
301     size_t message_len;
303     message_len = strlen (message);
305     pthread_mutex_lock (&cb->send_lock);
307     if (cb->sock_fd < 0)
308     {
309         status = wg_callback_init (cb);
310         if (status != 0)
311         {
312             /* An error message has already been printed. */
313             pthread_mutex_unlock (&cb->send_lock);
314             return (-1);
315         }
316     }
318     if (message_len >= cb->send_buf_free)
319     {
320         status = wg_flush_nolock (/* timeout = */ 0, cb);
321         if (status != 0)
322         {
323             pthread_mutex_unlock (&cb->send_lock);
324             return (status);
325         }
326     }
328     /* Assert that we have enough space for this message. */
329     assert (message_len < cb->send_buf_free);
331     /* `message_len + 1' because `message_len' does not include the
332      * trailing null byte. Neither does `send_buffer_fill'. */
333     memcpy (cb->send_buf + cb->send_buf_fill,
334             message, message_len + 1);
335     cb->send_buf_fill += message_len;
336     cb->send_buf_free -= message_len;
338     DEBUG ("write_graphite plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
339             cb->node,
340             cb->service,
341             cb->send_buf_fill, sizeof (cb->send_buf),
342             100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
343             message);
345     pthread_mutex_unlock (&cb->send_lock);
347     return (0);
350 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
351         struct wg_callback *cb)
353     char buffer[WG_SEND_BUF_SIZE];
354     int status;
356     if (0 != strcmp (ds->type, vl->type))
357     {
358         ERROR ("write_graphite plugin: DS type does not match "
359                 "value list type");
360         return -1;
361     }
363     memset (buffer, 0, sizeof (buffer));
364     status = format_graphite (buffer, sizeof (buffer), ds, vl,
365             cb->prefix, cb->postfix, cb->escape_char, cb->format_flags);
366     if (status != 0) /* error message has been printed already. */
367         return (status);
369     /* Send the message to graphite */
370     wg_send_message (buffer, cb);
371     if (status != 0)
372     {
373         /* An error message has already been printed. */
374         return (status);
375     }
377     return (0);
378 } /* int wg_write_messages */
380 static int wg_write (const data_set_t *ds, const value_list_t *vl,
381         user_data_t *user_data)
383     struct wg_callback *cb;
384     int status;
386     if (user_data == NULL)
387         return (EINVAL);
389     cb = user_data->data;
391     status = wg_write_messages (ds, vl, cb);
393     return (status);
396 static int config_set_char (char *dest,
397         oconfig_item_t *ci)
399     char buffer[4];
400     int status;
402     memset (buffer, 0, sizeof (buffer));
404     status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
405     if (status != 0)
406         return (status);
408     if (buffer[0] == 0)
409     {
410         ERROR ("write_graphite plugin: Cannot use an empty string for the "
411                 "\"EscapeCharacter\" option.");
412         return (-1);
413     }
415     if (buffer[1] != 0)
416     {
417         WARNING ("write_graphite plugin: Only the first character of the "
418                 "\"EscapeCharacter\" option ('%c') will be used.",
419                 (int) buffer[0]);
420     }
422     *dest = buffer[0];
424     return (0);
427 static int wg_config_node (oconfig_item_t *ci)
429     struct wg_callback *cb;
430     user_data_t user_data;
431     char callback_name[DATA_MAX_NAME_LEN];
432     int i;
434     cb = malloc (sizeof (*cb));
435     if (cb == NULL)
436     {
437         ERROR ("write_graphite plugin: malloc failed.");
438         return (-1);
439     }
440     memset (cb, 0, sizeof (*cb));
441     cb->sock_fd = -1;
442     cb->name = NULL;
443     cb->node = NULL;
444     cb->service = NULL;
445     cb->prefix = NULL;
446     cb->postfix = NULL;
447     cb->escape_char = WG_DEFAULT_ESCAPE;
448     cb->format_flags = GRAPHITE_STORE_RATES;
450     /* FIXME: Legacy configuration syntax. */
451     if (strcasecmp ("Carbon", ci->key) != 0)
452     {
453         int status = cf_util_get_string (ci, &cb->name);
454         if (status != 0)
455         {
456             wg_callback_free (cb);
457             return (status);
458         }
459     }
461     pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
462     C_COMPLAIN_INIT (&cb->init_complaint);
464     for (i = 0; i < ci->children_num; i++)
465     {
466         oconfig_item_t *child = ci->children + i;
468         if (strcasecmp ("Host", child->key) == 0)
469             cf_util_get_string (child, &cb->node);
470         else if (strcasecmp ("Port", child->key) == 0)
471             cf_util_get_service (child, &cb->service);
472         else if (strcasecmp ("Prefix", child->key) == 0)
473             cf_util_get_string (child, &cb->prefix);
474         else if (strcasecmp ("Postfix", child->key) == 0)
475             cf_util_get_string (child, &cb->postfix);
476         else if (strcasecmp ("StoreRates", child->key) == 0)
477             cf_util_get_flag (child, &cb->format_flags,
478                     GRAPHITE_STORE_RATES);
479         else if (strcasecmp ("SeparateInstances", child->key) == 0)
480             cf_util_get_flag (child, &cb->format_flags,
481                     GRAPHITE_SEPARATE_INSTANCES);
482         else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
483             cf_util_get_flag (child, &cb->format_flags,
484                     GRAPHITE_ALWAYS_APPEND_DS);
485         else if (strcasecmp ("EscapeCharacter", child->key) == 0)
486             config_set_char (&cb->escape_char, child);
487         else
488         {
489             ERROR ("write_graphite plugin: Invalid configuration "
490                         "option: %s.", child->key);
491         }
492     }
494     /* FIXME: Legacy configuration syntax. */
495     if (cb->name == NULL)
496         ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s",
497                 cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
498                 cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE);
499     else
500         ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s",
501                 cb->name);
503     memset (&user_data, 0, sizeof (user_data));
504     user_data.data = cb;
505     user_data.free_func = wg_callback_free;
506     plugin_register_write (callback_name, wg_write, &user_data);
508     user_data.free_func = NULL;
509     plugin_register_flush (callback_name, wg_flush, &user_data);
511     return (0);
514 static int wg_config (oconfig_item_t *ci)
516     int i;
518     for (i = 0; i < ci->children_num; i++)
519     {
520         oconfig_item_t *child = ci->children + i;
522         if (strcasecmp ("Node", child->key) == 0)
523             wg_config_node (child);
524         /* FIXME: Remove this legacy mode in version 6. */
525         else if (strcasecmp ("Carbon", child->key) == 0)
526             wg_config_node (child);
527         else
528         {
529             ERROR ("write_graphite plugin: Invalid configuration "
530                     "option: %s.", child->key);
531         }
532     }
534     return (0);
537 void module_register (void)
539     plugin_register_complex_config ("write_graphite", wg_config);
542 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */