57933e8dc3c518f77fa5f1aa72b6ba82f12b8436
1 /**
2 * collectd - src/write_graphite.c
3 * Copyright (C) 2011 Scott Sanders
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author:
19 * Scott Sanders <scott@jssjr.com>
20 *
21 * based on the excellent write_http plugin
22 **/
24 /* write_graphite plugin configuation example
25 *
26 * <Plugin write_graphite>
27 * <Carbon>
28 * Host "localhost"
29 * Port 2003
30 * Prefix "collectd"
31 * </Carbon>
32 * </Plugin>
33 */
35 #include "collectd.h"
36 #include "common.h"
37 #include "plugin.h"
38 #include "configfile.h"
40 #include "utils_cache.h"
41 #include "utils_parse_option.h"
43 /* Folks without pthread will need to disable this plugin. */
44 #include <pthread.h>
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 #include <sys/types.h>
50 #include <netinet/in.h>
51 #include <netdb.h>
53 #ifndef WG_FORMAT_NAME
54 #define WG_FORMAT_NAME(ret, ret_len, vl, cb, name) \
55 wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, \
56 (vl)->plugin_instance, (vl)->type, \
57 (vl)->type_instance, (cb)->prefix, (cb)->postfix, \
58 name, (cb)->dotchar)
59 #endif
61 #ifndef WG_SEND_BUF_SIZE
62 #define WG_SEND_BUF_SIZE 4096
63 #endif
65 /*
66 * Private variables
67 */
68 struct wg_callback
69 {
70 int sock_fd;
71 struct hostent *server;
73 char *host;
74 int port;
75 char *prefix;
76 char *postfix;
77 char dotchar;
79 char send_buf[WG_SEND_BUF_SIZE];
80 size_t send_buf_free;
81 size_t send_buf_fill;
82 cdtime_t send_buf_init_time;
84 pthread_mutex_t send_lock;
85 };
88 /*
89 * Functions
90 */
91 static void wg_reset_buffer (struct wg_callback *cb)
92 {
93 memset (cb->send_buf, 0, sizeof (cb->send_buf));
94 cb->send_buf_free = sizeof (cb->send_buf);
95 cb->send_buf_fill = 0;
96 cb->send_buf_init_time = cdtime ();
97 }
99 static int wg_send_buffer (struct wg_callback *cb)
100 {
101 int status = 0;
103 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
104 if (status < 0)
105 {
106 ERROR ("write_graphite plugin: send failed with "
107 "status %i (%s)",
108 status,
109 strerror (errno));
111 pthread_mutex_lock (&cb->send_lock);
113 DEBUG ("write_graphite plugin: closing socket and restting fd "
114 "so reinit will occur");
115 close (cb->sock_fd);
116 cb->sock_fd = -1;
118 pthread_mutex_unlock (&cb->send_lock);
120 return (-1);
121 }
122 return (0);
123 }
125 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
126 {
127 int status;
129 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
130 "send_buf_fill = %zu;",
131 (double)timeout,
132 cb->send_buf_fill);
134 /* timeout == 0 => flush unconditionally */
135 if (timeout > 0)
136 {
137 cdtime_t now;
139 now = cdtime ();
140 if ((cb->send_buf_init_time + timeout) > now)
141 return (0);
142 }
144 if (cb->send_buf_fill <= 0)
145 {
146 cb->send_buf_init_time = cdtime ();
147 return (0);
148 }
150 status = wg_send_buffer (cb);
151 wg_reset_buffer (cb);
153 return (status);
154 }
156 static int wg_callback_init (struct wg_callback *cb)
157 {
158 int status;
160 struct sockaddr_in serv_addr;
162 if (cb->sock_fd > 0)
163 return (0);
165 cb->sock_fd = socket (AF_INET, SOCK_STREAM, 0);
166 if (cb->sock_fd < 0)
167 {
168 ERROR ("write_graphite plugin: socket failed: %s", strerror (errno));
169 return (-1);
170 }
171 cb->server = gethostbyname(cb->host);
172 if (cb->server == NULL)
173 {
174 ERROR ("write_graphite plugin: no such host");
175 return (-1);
176 }
177 memset (&serv_addr, 0, sizeof (serv_addr));
178 serv_addr.sin_family = AF_INET;
179 memcpy (&serv_addr.sin_addr.s_addr,
180 cb->server->h_addr,
181 cb->server->h_length);
182 serv_addr.sin_port = htons(cb->port);
184 status = connect(cb->sock_fd,
185 (struct sockaddr *) &serv_addr,
186 sizeof(serv_addr));
187 if (status < 0)
188 {
189 char errbuf[1024];
190 sstrerror (errno, errbuf, sizeof (errbuf));
191 ERROR ("write_graphite plugin: connect failed: %s", errbuf);
192 close (cb->sock_fd);
193 cb->sock_fd = -1;
194 return (-1);
195 }
197 wg_reset_buffer (cb);
199 return (0);
200 }
202 static void wg_callback_free (void *data)
203 {
204 struct wg_callback *cb;
206 if (data == NULL)
207 return;
209 cb = data;
211 wg_flush_nolock (/* timeout = */ 0, cb);
213 close(cb->sock_fd);
214 sfree(cb->host);
215 sfree(cb->prefix);
216 sfree(cb->postfix);
218 sfree(cb);
219 }
221 static int wg_flush (cdtime_t timeout,
222 const char *identifier __attribute__((unused)),
223 user_data_t *user_data)
224 {
225 struct wg_callback *cb;
226 int status;
228 if (user_data == NULL)
229 return (-EINVAL);
231 cb = user_data->data;
233 pthread_mutex_lock (&cb->send_lock);
235 if (cb->sock_fd < 0)
236 {
237 status = wg_callback_init (cb);
238 if (status != 0)
239 {
240 ERROR ("write_graphite plugin: wg_callback_init failed.");
241 pthread_mutex_unlock (&cb->send_lock);
242 return (-1);
243 }
244 }
246 status = wg_flush_nolock (timeout, cb);
247 pthread_mutex_unlock (&cb->send_lock);
249 return (status);
250 }
252 static int wg_format_values (char *ret, size_t ret_len,
253 int ds_num, const data_set_t *ds, const value_list_t *vl,
254 _Bool store_rates)
255 {
256 size_t offset = 0;
257 int status;
258 gauge_t *rates = NULL;
260 assert (0 == strcmp (ds->type, vl->type));
262 memset (ret, 0, ret_len);
264 #define BUFFER_ADD(...) do { \
265 status = ssnprintf (ret + offset, ret_len - offset, \
266 __VA_ARGS__); \
267 if (status < 1) \
268 { \
269 sfree (rates); \
270 return (-1); \
271 } \
272 else if (((size_t) status) >= (ret_len - offset)) \
273 { \
274 sfree (rates); \
275 return (-1); \
276 } \
277 else \
278 offset += ((size_t) status); \
279 } while (0)
281 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
282 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
283 else if (store_rates)
284 {
285 if (rates == NULL)
286 rates = uc_get_rate (ds, vl);
287 if (rates == NULL)
288 {
289 WARNING ("format_values: "
290 "uc_get_rate failed.");
291 return (-1);
292 }
293 BUFFER_ADD ("%g", rates[ds_num]);
294 }
295 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
296 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
297 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
298 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
299 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
300 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
301 else
302 {
303 ERROR ("format_values plugin: Unknown data source type: %i",
304 ds->ds[ds_num].type);
305 sfree (rates);
306 return (-1);
307 }
309 #undef BUFFER_ADD
311 sfree (rates);
312 return (0);
313 }
315 static int swap_chars (char *dst, const char *src,
316 const char from, const char to)
317 {
318 size_t i;
320 int reps = 0;
322 for (i = 0; i < strlen(src) ; i++)
323 {
324 if (src[i] == from)
325 {
326 dst[i] = to;
327 ++reps;
328 }
329 else
330 dst[i] = src[i];
331 }
332 dst[i] = '\0';
334 return reps;
335 }
337 static int wg_format_name (char *ret, int ret_len,
338 const char *hostname,
339 const char *plugin, const char *plugin_instance,
340 const char *type, const char *type_instance,
341 const char *prefix, const char *postfix,
342 const char *ds_name, const char dotchar)
343 {
344 int status;
345 char *n_hostname = 0;
346 char *n_type_instance = 0;
348 assert (plugin != NULL);
349 assert (type != NULL);
351 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
352 {
353 ERROR ("Unable to allocate memory for normalized hostname buffer");
354 return (-1);
355 }
357 if (swap_chars(n_hostname, hostname, '.', dotchar) == -1)
358 {
359 ERROR ("Unable to normalize hostname");
360 return (-1);
361 }
363 if (type_instance && type_instance[0] != '\0') {
364 if ((n_type_instance = malloc(strlen(type_instance)+1)) == NULL)
365 {
366 ERROR ("Unable to allocate memory for normalized datasource name buffer");
367 return (-1);
368 }
369 if (swap_chars(n_type_instance, type_instance, '.', dotchar) == -1)
370 {
371 ERROR ("Unable to normalize datasource name");
372 return (-1);
373 }
374 }
376 if ((plugin_instance == NULL) || (plugin_instance[0] == '\0'))
377 {
378 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
379 {
380 if ((ds_name == NULL) || (ds_name[0] == '\0'))
381 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
382 prefix, n_hostname, postfix, plugin, type);
383 else
384 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
385 prefix, n_hostname, postfix, plugin, type, ds_name);
386 }
387 else
388 {
389 if ((ds_name == NULL) || (ds_name[0] == '\0'))
390 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s",
391 prefix, n_hostname, postfix, plugin, type,
392 n_type_instance);
393 else
394 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
395 prefix, n_hostname, postfix, plugin, type,
396 n_type_instance, ds_name);
397 }
398 }
399 else
400 {
401 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
402 {
403 if ((ds_name == NULL) || (ds_name[0] == '\0'))
404 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
405 prefix, n_hostname, postfix, plugin,
406 plugin_instance, type);
407 else
408 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
409 prefix, n_hostname, postfix, plugin,
410 plugin_instance, type, ds_name);
411 }
412 else
413 {
414 if ((ds_name == NULL) || (ds_name[0] == '\0'))
415 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s",
416 prefix, n_hostname, postfix, plugin,
417 plugin_instance, type, n_type_instance);
418 else
419 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
420 prefix, n_hostname, postfix, plugin,
421 plugin_instance, type, n_type_instance, ds_name);
422 }
423 }
425 sfree(n_hostname);
426 sfree(n_type_instance);
428 if ((status < 1) || (status >= ret_len))
429 return (-1);
430 return (0);
431 }
433 static int wg_send_message (const char* key, const char* value,
434 cdtime_t time, struct wg_callback *cb)
435 {
436 int status;
437 size_t message_len;
438 char message[1024];
440 message_len = (size_t) ssnprintf (message, sizeof (message),
441 "%s %s %.0f\n",
442 key,
443 value,
444 CDTIME_T_TO_DOUBLE(time));
445 if (message_len >= sizeof (message)) {
446 ERROR ("write_graphite plugin: message buffer too small: "
447 "Need %zu bytes.", message_len + 1);
448 return (-1);
449 }
452 pthread_mutex_lock (&cb->send_lock);
454 if (cb->sock_fd < 0)
455 {
456 status = wg_callback_init (cb);
457 if (status != 0)
458 {
459 ERROR ("write_graphite plugin: wg_callback_init failed.");
460 pthread_mutex_unlock (&cb->send_lock);
461 return (-1);
462 }
463 }
465 if (message_len >= cb->send_buf_free)
466 {
467 status = wg_flush_nolock (/* timeout = */ 0, cb);
468 if (status != 0)
469 {
470 pthread_mutex_unlock (&cb->send_lock);
471 return (status);
472 }
473 }
474 assert (message_len < cb->send_buf_free);
476 /* `message_len + 1' because `message_len' does not include the
477 * trailing null byte. Neither does `send_buffer_fill'. */
478 memcpy (cb->send_buf + cb->send_buf_fill,
479 message, message_len + 1);
480 cb->send_buf_fill += message_len;
481 cb->send_buf_free -= message_len;
483 DEBUG ("write_graphite plugin: <%s:%d> buf %zu/%zu (%g%%) \"%s\"",
484 cb->host,
485 cb->port,
486 cb->send_buf_fill, sizeof (cb->send_buf),
487 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
488 message);
490 /* Check if we have enough space for this message. */
491 pthread_mutex_unlock (&cb->send_lock);
493 return (0);
494 }
496 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
497 struct wg_callback *cb)
498 {
499 char key[10*DATA_MAX_NAME_LEN];
500 char values[512];
502 int status, i;
504 if (0 != strcmp (ds->type, vl->type))
505 {
506 ERROR ("write_graphite plugin: DS type does not match "
507 "value list type");
508 return -1;
509 }
511 if (ds->ds_num > 1)
512 {
513 for (i = 0; i < ds->ds_num; i++)
514 {
515 /* Copy the identifier to `key' and escape it. */
516 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
517 if (status != 0)
518 {
519 ERROR ("write_graphite plugin: error with format_name");
520 return (status);
521 }
523 escape_string (key, sizeof (key));
524 /* Convert the values to an ASCII representation and put that
525 * into `values'. */
526 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
527 if (status != 0)
528 {
529 ERROR ("write_graphite plugin: error with "
530 "wg_format_values");
531 return (status);
532 }
534 /* Send the message to graphite */
535 status = wg_send_message (key, values, vl->time, cb);
536 if (status != 0)
537 {
538 ERROR ("write_graphite plugin: error with "
539 "wg_send_message");
540 return (status);
541 }
542 }
543 }
544 else
545 {
546 /* Copy the identifier to `key' and escape it. */
547 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
548 if (status != 0)
549 {
550 ERROR ("write_graphite plugin: error with format_name");
551 return (status);
552 }
554 escape_string (key, sizeof (key));
555 /* Convert the values to an ASCII representation and put that into
556 * `values'. */
557 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
558 if (status != 0)
559 {
560 ERROR ("write_graphite plugin: error with "
561 "wg_format_values");
562 return (status);
563 }
565 /* Send the message to graphite */
566 status = wg_send_message (key, values, vl->time, cb);
567 if (status != 0)
568 {
569 ERROR ("write_graphite plugin: error with "
570 "wg_send_message");
571 return (status);
572 }
573 }
575 return (0);
576 }
578 static int wg_write (const data_set_t *ds, const value_list_t *vl,
579 user_data_t *user_data)
580 {
581 struct wg_callback *cb;
582 int status;
584 if (user_data == NULL)
585 return (-EINVAL);
587 cb = user_data->data;
589 status = wg_write_messages (ds, vl, cb);
591 return (status);
592 }
594 static int config_set_number (int *dest,
595 oconfig_item_t *ci)
596 {
597 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
598 {
599 WARNING ("write_graphite plugin: The `%s' config option "
600 "needs exactly one numeric argument.", ci->key);
601 return (-1);
602 }
604 *dest = ci->values[0].value.number;
606 return (0);
607 }
609 static int config_set_char (char *dest,
610 oconfig_item_t *ci)
611 {
612 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
613 {
614 WARNING ("write_graphite plugin: The `%s' config option "
615 "needs exactly one string argument.", ci->key);
616 return (-1);
617 }
619 *dest = ci->values[0].value.string[0];
621 return (0);
622 }
624 static int config_set_string (char **ret_string,
625 oconfig_item_t *ci)
626 {
627 char *string;
629 if ((ci->values_num != 1)
630 || (ci->values[0].type != OCONFIG_TYPE_STRING))
631 {
632 WARNING ("write_graphite plugin: The `%s' config option "
633 "needs exactly one string argument.", ci->key);
634 return (-1);
635 }
637 string = strdup (ci->values[0].value.string);
638 if (string == NULL)
639 {
640 ERROR ("write_graphite plugin: strdup failed.");
641 return (-1);
642 }
644 if (*ret_string != NULL)
645 sfree (*ret_string);
646 *ret_string = string;
648 return (0);
649 }
651 static int wg_config_carbon (oconfig_item_t *ci)
652 {
653 struct wg_callback *cb;
654 user_data_t user_data;
655 int i;
657 cb = malloc (sizeof (*cb));
658 if (cb == NULL)
659 {
660 ERROR ("write_graphite plugin: malloc failed.");
661 return (-1);
662 }
663 memset (cb, 0, sizeof (*cb));
664 cb->sock_fd = -1;
665 cb->host = NULL;
666 cb->port = 2003;
667 cb->prefix = NULL;
668 cb->postfix = NULL;
669 cb->server = NULL;
670 cb->dotchar = '_';
672 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
674 for (i = 0; i < ci->children_num; i++)
675 {
676 oconfig_item_t *child = ci->children + i;
678 if (strcasecmp ("Host", child->key) == 0)
679 config_set_string (&cb->host, child);
680 else if (strcasecmp ("Port", child->key) == 0)
681 config_set_number (&cb->port, child);
682 else if (strcasecmp ("Prefix", child->key) == 0)
683 config_set_string (&cb->prefix, child);
684 else if (strcasecmp ("Postfix", child->key) == 0)
685 config_set_string (&cb->postfix, child);
686 else if (strcasecmp ("DotCharacter", child->key) == 0)
687 config_set_char (&cb->dotchar, child);
688 else
689 {
690 ERROR ("write_graphite plugin: Invalid configuration "
691 "option: %s.", child->key);
692 }
693 }
695 if (cb->prefix == NULL) {
696 if ((cb->prefix = malloc((int)sizeof(char))) == NULL)
697 {
698 ERROR ("Unable to allocate memory for hostname prefix buffer");
699 return (-1);
700 }
701 cb->postfix[0] = '\0';
702 }
704 if (cb->postfix == NULL) {
705 if ((cb->postfix = malloc((int)sizeof(char))) == NULL)
706 {
707 ERROR ("Unable to allocate memory for hostname postfix buffer");
708 return (-1);
709 }
710 cb->postfix[0] = '\0';
711 }
713 DEBUG ("write_graphite: Registering write callback to carbon agent "
714 "%s:%d", cb->host, cb->port);
716 memset (&user_data, 0, sizeof (user_data));
717 user_data.data = cb;
718 user_data.free_func = NULL;
719 plugin_register_flush ("write_graphite", wg_flush, &user_data);
721 user_data.free_func = wg_callback_free;
722 plugin_register_write ("write_graphite", wg_write, &user_data);
724 return (0);
725 }
727 static int wg_config (oconfig_item_t *ci)
728 {
729 int i;
731 for (i = 0; i < ci->children_num; i++)
732 {
733 oconfig_item_t *child = ci->children + i;
735 if (strcasecmp ("Carbon", child->key) == 0)
736 wg_config_carbon (child);
737 else
738 {
739 ERROR ("write_graphite plugin: Invalid configuration "
740 "option: %s.", child->key);
741 }
742 }
744 return (0);
745 }
747 void module_register (void)
748 {
749 plugin_register_complex_config ("write_graphite", wg_config);
750 }
752 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */