Merge remote-tracking branch 'github/pr/1883'
[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  **/
31
32  /* write_graphite plugin configuation example
33   *
34   * <Plugin write_graphite>
35   *   <Carbon>
36   *     Host "localhost"
37   *     Port "2003"
38   *     Protocol "udp"
39   *     LogSendErrors true
40   *     Prefix "collectd"
41   *   </Carbon>
42   * </Plugin>
43   */
44
45 #include "collectd.h"
46
47 #include "common.h"
48 #include "plugin.h"
49
50 #include "utils_complain.h"
51 #include "utils_format_graphite.h"
52
53 #include <netdb.h>
54
55 #define WG_DEFAULT_NODE "localhost"
56 #define WG_DEFAULT_SERVICE "2003"
57 #define WG_DEFAULT_PROTOCOL "tcp"
58 #define WG_DEFAULT_LOG_SEND_ERRORS 1
59 #define WG_DEFAULT_ESCAPE '_'
60
61 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
62 #define WG_SEND_BUF_SIZE 1428
63
64 #define WG_MIN_RECONNECT_INTERVAL TIME_T_TO_CDTIME_T (1)
65
66 /*
67  * Private variables
68  */
69 struct wg_callback
70 {
71     int      sock_fd;
72
73     char    *name;
74
75     char    *node;
76     char    *service;
77     char    *protocol;
78     _Bool   log_send_errors;
79     char    *prefix;
80     char    *postfix;
81     char     escape_char;
82
83     unsigned int format_flags;
84
85     char     send_buf[WG_SEND_BUF_SIZE];
86     size_t   send_buf_free;
87     size_t   send_buf_fill;
88     cdtime_t send_buf_init_time;
89
90     pthread_mutex_t send_lock;
91     c_complain_t init_complaint;
92     cdtime_t last_connect_time;
93
94     /* Force reconnect useful for load balanced environments */
95     cdtime_t last_reconnect_time;
96     cdtime_t reconnect_interval;
97     _Bool reconnect_interval_reached;
98 };
99
100 /* wg_force_reconnect_check closes cb->sock_fd when it was open for longer
101  * than cb->reconnect_interval. Must hold cb->send_lock when calling. */
102 static void wg_force_reconnect_check (struct wg_callback *cb)
103 {
104     cdtime_t now;
105
106     if (cb->reconnect_interval == 0)
107         return;
108
109     /* check if address changes if addr_timeout */
110     now = cdtime ();
111     if ((now - cb->last_reconnect_time) < cb->reconnect_interval)
112         return;
113
114     /* here we should close connection on next */
115     close (cb->sock_fd);
116     cb->sock_fd = -1;
117     cb->last_reconnect_time = now;
118     cb->reconnect_interval_reached = 1;
119
120     INFO ("write_graphite plugin: Connection closed after %.3f seconds.",
121           CDTIME_T_TO_DOUBLE (now - cb->last_reconnect_time));
122 }
123
124 /*
125  * Functions
126  */
127 static void wg_reset_buffer (struct wg_callback *cb)
128 {
129     memset (cb->send_buf, 0, sizeof (cb->send_buf));
130     cb->send_buf_free = sizeof (cb->send_buf);
131     cb->send_buf_fill = 0;
132     cb->send_buf_init_time = cdtime ();
133 }
134
135 static int wg_send_buffer (struct wg_callback *cb)
136 {
137     ssize_t status;
138
139     if (cb->sock_fd < 0)
140         return (-1);
141
142     status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
143     if (status != 0)
144     {
145         if (cb->log_send_errors)
146         {
147             char errbuf[1024];
148             ERROR ("write_graphite plugin: send to %s:%s (%s) failed with status %zi (%s)",
149                     cb->node, cb->service, cb->protocol,
150                     status, sstrerror (errno, errbuf, sizeof (errbuf)));
151         }
152
153         close (cb->sock_fd);
154         cb->sock_fd = -1;
155
156         return (-1);
157     }
158
159     return (0);
160 }
161
162 /* NOTE: You must hold cb->send_lock when calling this function! */
163 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
164 {
165     int status;
166
167     DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
168             "send_buf_fill = %zu;",
169             (double)timeout,
170             cb->send_buf_fill);
171
172     /* timeout == 0  => flush unconditionally */
173     if (timeout > 0)
174     {
175         cdtime_t now;
176
177         now = cdtime ();
178         if ((cb->send_buf_init_time + timeout) > now)
179             return (0);
180     }
181
182     if (cb->send_buf_fill == 0)
183     {
184         cb->send_buf_init_time = cdtime ();
185         return (0);
186     }
187
188     status = wg_send_buffer (cb);
189     wg_reset_buffer (cb);
190
191     return (status);
192 }
193
194 static int wg_callback_init (struct wg_callback *cb)
195 {
196     struct addrinfo *ai_list;
197     cdtime_t now;
198     int status;
199
200     char connerr[1024] = "";
201
202     if (cb->sock_fd > 0)
203         return (0);
204
205     /* Don't try to reconnect too often. By default, one reconnection attempt
206      * is made per second. */
207     now = cdtime ();
208     if ((now - cb->last_connect_time) < WG_MIN_RECONNECT_INTERVAL)
209         return (EAGAIN);
210     cb->last_connect_time = now;
211
212     struct addrinfo ai_hints = {
213         .ai_family = AF_UNSPEC,
214         .ai_flags  = AI_ADDRCONFIG
215     };
216
217     if (0 == strcasecmp ("tcp", cb->protocol))
218         ai_hints.ai_socktype = SOCK_STREAM;
219     else
220         ai_hints.ai_socktype = SOCK_DGRAM;
221
222     status = getaddrinfo (cb->node, cb->service, &ai_hints, &ai_list);
223     if (status != 0)
224     {
225         ERROR ("write_graphite plugin: getaddrinfo (%s, %s, %s) failed: %s",
226                 cb->node, cb->service, cb->protocol, gai_strerror (status));
227         return (-1);
228     }
229
230     assert (ai_list != NULL);
231     for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
232     {
233         cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
234                 ai_ptr->ai_protocol);
235         if (cb->sock_fd < 0) {
236             char errbuf[1024];
237             snprintf (connerr, sizeof (connerr), "failed to open socket: %s",
238                     sstrerror (errno, errbuf, sizeof (errbuf)));
239             continue;
240         }
241
242         set_sock_opts (cb->sock_fd);
243
244         status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
245         if (status != 0)
246         {
247             char errbuf[1024];
248             snprintf (connerr, sizeof (connerr), "failed to connect to remote "
249                     "host: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
250             close (cb->sock_fd);
251             cb->sock_fd = -1;
252             continue;
253         }
254
255         break;
256     }
257
258     freeaddrinfo (ai_list);
259
260     if (cb->sock_fd < 0)
261     {
262         if (connerr[0] == '\0')
263             /* this should not happen but try to get a message anyway */
264             sstrerror (errno, connerr, sizeof (connerr));
265         c_complain (LOG_ERR, &cb->init_complaint,
266                   "write_graphite plugin: Connecting to %s:%s via %s failed. "
267                   "The last error was: %s", cb->node, cb->service, cb->protocol, connerr);
268         return (-1);
269     }
270     else
271     {
272         c_release (LOG_INFO, &cb->init_complaint,
273                 "write_graphite plugin: Successfully connected to %s:%s via %s.",
274                 cb->node, cb->service, cb->protocol);
275     }
276
277     /* wg_force_reconnect_check does not flush the buffer before closing a
278      * sending socket, so only call wg_reset_buffer() if the socket was closed
279      * for a different reason (tracked in cb->reconnect_interval_reached). */
280     if (!cb->reconnect_interval_reached || (cb->send_buf_free == 0))
281         wg_reset_buffer (cb);
282     else
283         cb->reconnect_interval_reached = 0;
284
285     return (0);
286 }
287
288 static void wg_callback_free (void *data)
289 {
290     struct wg_callback *cb;
291
292     if (data == NULL)
293         return;
294
295     cb = data;
296
297     pthread_mutex_lock (&cb->send_lock);
298
299     wg_flush_nolock (/* timeout = */ 0, cb);
300
301     if (cb->sock_fd >= 0)
302     {
303         close (cb->sock_fd);
304         cb->sock_fd = -1;
305     }
306
307     sfree(cb->name);
308     sfree(cb->node);
309     sfree(cb->protocol);
310     sfree(cb->service);
311     sfree(cb->prefix);
312     sfree(cb->postfix);
313
314     pthread_mutex_destroy (&cb->send_lock);
315
316     sfree(cb);
317 }
318
319 static int wg_flush (cdtime_t timeout,
320         const char *identifier __attribute__((unused)),
321         user_data_t *user_data)
322 {
323     struct wg_callback *cb;
324     int status;
325
326     if (user_data == NULL)
327         return (-EINVAL);
328
329     cb = user_data->data;
330
331     pthread_mutex_lock (&cb->send_lock);
332
333     if (cb->sock_fd < 0)
334     {
335         status = wg_callback_init (cb);
336         if (status != 0)
337         {
338             /* An error message has already been printed. */
339             pthread_mutex_unlock (&cb->send_lock);
340             return (-1);
341         }
342     }
343
344     status = wg_flush_nolock (timeout, cb);
345     pthread_mutex_unlock (&cb->send_lock);
346
347     return (status);
348 }
349
350 static int wg_send_message (char const *message, struct wg_callback *cb)
351 {
352     int status;
353     size_t message_len;
354
355     message_len = strlen (message);
356
357     pthread_mutex_lock (&cb->send_lock);
358
359     wg_force_reconnect_check (cb);
360
361     if (cb->sock_fd < 0)
362     {
363         status = wg_callback_init (cb);
364         if (status != 0)
365         {
366             /* An error message has already been printed. */
367             pthread_mutex_unlock (&cb->send_lock);
368             return (-1);
369         }
370     }
371
372     if (message_len >= cb->send_buf_free)
373     {
374         status = wg_flush_nolock (/* timeout = */ 0, cb);
375         if (status != 0)
376         {
377             pthread_mutex_unlock (&cb->send_lock);
378             return (status);
379         }
380     }
381
382     /* Assert that we have enough space for this message. */
383     assert (message_len < cb->send_buf_free);
384
385     /* `message_len + 1' because `message_len' does not include the
386      * trailing null byte. Neither does `send_buffer_fill'. */
387     memcpy (cb->send_buf + cb->send_buf_fill,
388             message, message_len + 1);
389     cb->send_buf_fill += message_len;
390     cb->send_buf_free -= message_len;
391
392     DEBUG ("write_graphite plugin: [%s]:%s (%s) buf %zu/%zu (%.1f %%) \"%s\"",
393             cb->node, cb->service, cb->protocol,
394             cb->send_buf_fill, sizeof (cb->send_buf),
395             100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
396             message);
397
398     pthread_mutex_unlock (&cb->send_lock);
399
400     return (0);
401 }
402
403 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
404         struct wg_callback *cb)
405 {
406     char buffer[WG_SEND_BUF_SIZE] = { 0 };
407     int status;
408
409     if (0 != strcmp (ds->type, vl->type))
410     {
411         ERROR ("write_graphite plugin: DS type does not match "
412                 "value list type");
413         return -1;
414     }
415
416     status = format_graphite (buffer, sizeof (buffer), ds, vl,
417             cb->prefix, cb->postfix, cb->escape_char, cb->format_flags);
418     if (status != 0) /* error message has been printed already. */
419         return (status);
420
421     /* Send the message to graphite */
422     status = wg_send_message (buffer, cb);
423     if (status != 0) /* error message has been printed already. */
424         return (status);
425
426     return (0);
427 } /* int wg_write_messages */
428
429 static int wg_write (const data_set_t *ds, const value_list_t *vl,
430         user_data_t *user_data)
431 {
432     struct wg_callback *cb;
433     int status;
434
435     if (user_data == NULL)
436         return (EINVAL);
437
438     cb = user_data->data;
439
440     status = wg_write_messages (ds, vl, cb);
441
442     return (status);
443 }
444
445 static int config_set_char (char *dest,
446         oconfig_item_t *ci)
447 {
448     char buffer[4] = { 0 };
449     int status;
450
451     status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
452     if (status != 0)
453         return (status);
454
455     if (buffer[0] == 0)
456     {
457         ERROR ("write_graphite plugin: Cannot use an empty string for the "
458                 "\"EscapeCharacter\" option.");
459         return (-1);
460     }
461
462     if (buffer[1] != 0)
463     {
464         WARNING ("write_graphite plugin: Only the first character of the "
465                 "\"EscapeCharacter\" option ('%c') will be used.",
466                 (int) buffer[0]);
467     }
468
469     *dest = buffer[0];
470
471     return (0);
472 }
473
474 static int wg_config_node (oconfig_item_t *ci)
475 {
476     struct wg_callback *cb;
477     char callback_name[DATA_MAX_NAME_LEN];
478     int status = 0;
479
480     cb = calloc (1, sizeof (*cb));
481     if (cb == NULL)
482     {
483         ERROR ("write_graphite plugin: calloc failed.");
484         return (-1);
485     }
486     cb->sock_fd = -1;
487     cb->name = NULL;
488     cb->node = strdup (WG_DEFAULT_NODE);
489     cb->service = strdup (WG_DEFAULT_SERVICE);
490     cb->protocol = strdup (WG_DEFAULT_PROTOCOL);
491     cb->last_reconnect_time = cdtime();
492     cb->reconnect_interval = 0;
493     cb->reconnect_interval_reached = 0;
494     cb->log_send_errors = WG_DEFAULT_LOG_SEND_ERRORS;
495     cb->prefix = NULL;
496     cb->postfix = NULL;
497     cb->escape_char = WG_DEFAULT_ESCAPE;
498     cb->format_flags = GRAPHITE_STORE_RATES;
499
500     /* FIXME: Legacy configuration syntax. */
501     if (strcasecmp ("Carbon", ci->key) != 0)
502     {
503         status = cf_util_get_string (ci, &cb->name);
504         if (status != 0)
505         {
506             wg_callback_free (cb);
507             return (status);
508         }
509     }
510
511     pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
512     C_COMPLAIN_INIT (&cb->init_complaint);
513
514     for (int i = 0; i < ci->children_num; i++)
515     {
516         oconfig_item_t *child = ci->children + i;
517
518         if (strcasecmp ("Host", child->key) == 0)
519             cf_util_get_string (child, &cb->node);
520         else if (strcasecmp ("Port", child->key) == 0)
521             cf_util_get_service (child, &cb->service);
522         else if (strcasecmp ("Protocol", child->key) == 0)
523         {
524             cf_util_get_string (child, &cb->protocol);
525
526             if (strcasecmp ("UDP", cb->protocol) != 0 &&
527                 strcasecmp ("TCP", cb->protocol) != 0)
528             {
529                 ERROR ("write_graphite plugin: Unknown protocol (%s)",
530                         cb->protocol);
531                 status = -1;
532             }
533         }
534         else if (strcasecmp ("ReconnectInterval", child->key) == 0)
535             cf_util_get_cdtime (child, &cb->reconnect_interval);
536         else if (strcasecmp ("LogSendErrors", child->key) == 0)
537             cf_util_get_boolean (child, &cb->log_send_errors);
538         else if (strcasecmp ("Prefix", child->key) == 0)
539             cf_util_get_string (child, &cb->prefix);
540         else if (strcasecmp ("Postfix", child->key) == 0)
541             cf_util_get_string (child, &cb->postfix);
542         else if (strcasecmp ("StoreRates", child->key) == 0)
543             cf_util_get_flag (child, &cb->format_flags,
544                     GRAPHITE_STORE_RATES);
545         else if (strcasecmp ("SeparateInstances", child->key) == 0)
546             cf_util_get_flag (child, &cb->format_flags,
547                     GRAPHITE_SEPARATE_INSTANCES);
548         else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
549             cf_util_get_flag (child, &cb->format_flags,
550                     GRAPHITE_ALWAYS_APPEND_DS);
551         else if (strcasecmp ("EscapeCharacter", child->key) == 0)
552             config_set_char (&cb->escape_char, child);
553         else
554         {
555             ERROR ("write_graphite plugin: Invalid configuration "
556                         "option: %s.", child->key);
557             status = -1;
558         }
559
560         if (status != 0)
561             break;
562     }
563
564     if (status != 0)
565     {
566         wg_callback_free (cb);
567         return (status);
568     }
569
570     /* FIXME: Legacy configuration syntax. */
571     if (cb->name == NULL)
572         ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s/%s",
573                 cb->node, cb->service, cb->protocol);
574     else
575         ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s",
576                 cb->name);
577
578     user_data_t ud = {
579         .data = cb,
580         .free_func = wg_callback_free
581     };
582
583     plugin_register_write (callback_name, wg_write, &ud);
584
585     ud.free_func = NULL;
586     plugin_register_flush (callback_name, wg_flush, &ud);
587
588     return (0);
589 }
590
591 static int wg_config (oconfig_item_t *ci)
592 {
593     for (int i = 0; i < ci->children_num; i++)
594     {
595         oconfig_item_t *child = ci->children + i;
596
597         if (strcasecmp ("Node", child->key) == 0)
598             wg_config_node (child);
599         /* FIXME: Remove this legacy mode in version 6. */
600         else if (strcasecmp ("Carbon", child->key) == 0)
601             wg_config_node (child);
602         else
603         {
604             ERROR ("write_graphite plugin: Invalid configuration "
605                     "option: %s.", child->key);
606         }
607     }
608
609     return (0);
610 }
611
612 void module_register (void)
613 {
614     plugin_register_complex_config ("write_graphite", wg_config);
615 }
616
617 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */