0de419f2b635960d99e89c61c4737b35a0d5b312
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, name, (cb)->dotchar)
58 #endif
60 #ifndef WG_SEND_BUF_SIZE
61 #define WG_SEND_BUF_SIZE 4096
62 #endif
64 /*
65 * Private variables
66 */
67 struct wg_callback
68 {
69 int sock_fd;
70 struct hostent *server;
72 char *host;
73 int port;
74 char *prefix;
75 char dotchar;
77 char send_buf[WG_SEND_BUF_SIZE];
78 size_t send_buf_free;
79 size_t send_buf_fill;
80 cdtime_t send_buf_init_time;
82 pthread_mutex_t send_lock;
83 };
86 /*
87 * Functions
88 */
89 static void wg_reset_buffer (struct wg_callback *cb)
90 {
91 memset (cb->send_buf, 0, sizeof (cb->send_buf));
92 cb->send_buf_free = sizeof (cb->send_buf);
93 cb->send_buf_fill = 0;
94 cb->send_buf_init_time = cdtime ();
95 }
97 static int wg_send_buffer (struct wg_callback *cb)
98 {
99 int status = 0;
101 status = write (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
102 if (status < 0)
103 {
104 ERROR ("write_graphite plugin: send failed with "
105 "status %i (%s)",
106 status,
107 strerror (errno));
109 pthread_mutex_lock (&cb->send_lock);
111 DEBUG ("write_graphite plugin: closing socket and restting fd "
112 "so reinit will occur");
113 close (cb->sock_fd);
114 cb->sock_fd = -1;
116 pthread_mutex_unlock (&cb->send_lock);
118 return (-1);
119 }
120 return (0);
121 }
123 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
124 {
125 int status;
127 DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; "
128 "send_buf_fill = %zu;",
129 (double)timeout,
130 cb->send_buf_fill);
132 /* timeout == 0 => flush unconditionally */
133 if (timeout > 0)
134 {
135 cdtime_t now;
137 now = cdtime ();
138 if ((cb->send_buf_init_time + timeout) > now)
139 return (0);
140 }
142 if (cb->send_buf_fill <= 0)
143 {
144 cb->send_buf_init_time = cdtime ();
145 return (0);
146 }
148 status = wg_send_buffer (cb);
149 wg_reset_buffer (cb);
151 return (status);
152 }
154 static int wg_callback_init (struct wg_callback *cb)
155 {
156 int status;
158 struct sockaddr_in serv_addr;
160 if (cb->sock_fd > 0)
161 return (0);
163 cb->sock_fd = socket (AF_INET, SOCK_STREAM, 0);
164 if (cb->sock_fd < 0)
165 {
166 ERROR ("write_graphite plugin: socket failed: %s", strerror (errno));
167 return (-1);
168 }
169 cb->server = gethostbyname(cb->host);
170 if (cb->server == NULL)
171 {
172 ERROR ("write_graphite plugin: no such host");
173 return (-1);
174 }
175 memset (&serv_addr, 0, sizeof (serv_addr));
176 serv_addr.sin_family = AF_INET;
177 memcpy (&serv_addr.sin_addr.s_addr,
178 cb->server->h_addr,
179 cb->server->h_length);
180 serv_addr.sin_port = htons(cb->port);
182 status = connect(cb->sock_fd,
183 (struct sockaddr *) &serv_addr,
184 sizeof(serv_addr));
185 if (status < 0)
186 {
187 char errbuf[1024];
188 sstrerror (errno, errbuf, sizeof (errbuf));
189 ERROR ("write_graphite plugin: connect failed: %s", errbuf);
190 close (cb->sock_fd);
191 cb->sock_fd = -1;
192 return (-1);
193 }
195 wg_reset_buffer (cb);
197 return (0);
198 }
200 static void wg_callback_free (void *data)
201 {
202 struct wg_callback *cb;
204 if (data == NULL)
205 return;
207 cb = data;
209 wg_flush_nolock (/* timeout = */ 0, cb);
211 close(cb->sock_fd);
212 sfree(cb->host);
213 sfree(cb->prefix);
215 sfree(cb);
216 }
218 static int wg_flush (cdtime_t timeout,
219 const char *identifier __attribute__((unused)),
220 user_data_t *user_data)
221 {
222 struct wg_callback *cb;
223 int status;
225 if (user_data == NULL)
226 return (-EINVAL);
228 cb = user_data->data;
230 pthread_mutex_lock (&cb->send_lock);
232 if (cb->sock_fd < 0)
233 {
234 status = wg_callback_init (cb);
235 if (status != 0)
236 {
237 ERROR ("write_graphite plugin: wg_callback_init failed.");
238 pthread_mutex_unlock (&cb->send_lock);
239 return (-1);
240 }
241 }
243 status = wg_flush_nolock (timeout, cb);
244 pthread_mutex_unlock (&cb->send_lock);
246 return (status);
247 }
249 static int wg_format_values (char *ret, size_t ret_len,
250 int ds_num, const data_set_t *ds, const value_list_t *vl,
251 _Bool store_rates)
252 {
253 size_t offset = 0;
254 int status;
255 gauge_t *rates = NULL;
257 assert (0 == strcmp (ds->type, vl->type));
259 memset (ret, 0, ret_len);
261 #define BUFFER_ADD(...) do { \
262 status = ssnprintf (ret + offset, ret_len - offset, \
263 __VA_ARGS__); \
264 if (status < 1) \
265 { \
266 sfree (rates); \
267 return (-1); \
268 } \
269 else if (((size_t) status) >= (ret_len - offset)) \
270 { \
271 sfree (rates); \
272 return (-1); \
273 } \
274 else \
275 offset += ((size_t) status); \
276 } while (0)
278 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
279 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
280 else if (store_rates)
281 {
282 if (rates == NULL)
283 rates = uc_get_rate (ds, vl);
284 if (rates == NULL)
285 {
286 WARNING ("format_values: "
287 "uc_get_rate failed.");
288 return (-1);
289 }
290 BUFFER_ADD ("%g", rates[ds_num]);
291 }
292 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
293 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
294 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
295 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
296 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
297 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
298 else
299 {
300 ERROR ("format_values plugin: Unknown data source type: %i",
301 ds->ds[ds_num].type);
302 sfree (rates);
303 return (-1);
304 }
306 #undef BUFFER_ADD
308 sfree (rates);
309 return (0);
310 }
312 static int swap_chars (char *dst, const char *src,
313 const char from, const char to)
314 {
315 size_t i;
317 int reps = 0;
319 for (i = 0; i < strlen(src) ; i++)
320 {
321 if (src[i] == from)
322 {
323 dst[i] = to;
324 ++reps;
325 }
326 else
327 dst[i] = src[i];
328 }
329 dst[i] = '\0';
331 return reps;
332 }
334 static int wg_format_name (char *ret, int ret_len,
335 const char *hostname,
336 const char *plugin, const char *plugin_instance,
337 const char *type, const char *type_instance,
338 const char *prefix, const char *ds_name, const char dotchar)
339 {
340 int status;
341 char *n_hostname = 0;
342 char *n_type_instance = 0;
344 assert (plugin != NULL);
345 assert (type != NULL);
347 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
348 {
349 ERROR ("Unable to allocate memory for normalized hostname buffer");
350 return (-1);
351 }
353 if (swap_chars(n_hostname, hostname, '.', dotchar) == -1)
354 {
355 ERROR ("Unable to normalize hostname");
356 return (-1);
357 }
359 if (type_instance && type_instance[0] != '\0') {
360 if ((n_type_instance = malloc(strlen(type_instance)+1)) == NULL)
361 {
362 ERROR ("Unable to allocate memory for normalized datasource name buffer");
363 return (-1);
364 }
365 if (swap_chars(n_type_instance, type_instance, '.', dotchar) == -1)
366 {
367 ERROR ("Unable to normalize datasource name");
368 return (-1);
369 }
370 }
372 if ((plugin_instance == NULL) || (plugin_instance[0] == '\0'))
373 {
374 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
375 {
376 if ((ds_name == NULL) || (ds_name[0] == '\0'))
377 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s",
378 prefix, n_hostname, plugin, type);
379 else
380 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
381 prefix, n_hostname, plugin, type, ds_name);
382 }
383 else
384 {
385 if ((ds_name == NULL) || (ds_name[0] == '\0'))
386 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s",
387 prefix, n_hostname, plugin, type,
388 n_type_instance);
389 else
390 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s.%s",
391 prefix, n_hostname, plugin, type,
392 n_type_instance, ds_name);
393 }
394 }
395 else
396 {
397 if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
398 {
399 if ((ds_name == NULL) || (ds_name[0] == '\0'))
400 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
401 prefix, n_hostname, plugin,
402 plugin_instance, type);
403 else
404 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s.%s",
405 prefix, n_hostname, plugin,
406 plugin_instance, type, ds_name);
407 }
408 else
409 {
410 if ((ds_name == NULL) || (ds_name[0] == '\0'))
411 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s",
412 prefix, n_hostname, plugin,
413 plugin_instance, type, n_type_instance);
414 else
415 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s.%s",
416 prefix, n_hostname, plugin,
417 plugin_instance, type, n_type_instance, ds_name);
418 }
419 }
421 sfree(n_hostname);
422 sfree(n_type_instance);
424 if ((status < 1) || (status >= ret_len))
425 return (-1);
426 return (0);
427 }
429 static int wg_send_message (const char* key, const char* value,
430 cdtime_t time, struct wg_callback *cb)
431 {
432 int status;
433 size_t message_len;
434 char message[1024];
436 message_len = (size_t) ssnprintf (message, sizeof (message),
437 "%s %s %.0f\n",
438 key,
439 value,
440 CDTIME_T_TO_DOUBLE(time));
441 if (message_len >= sizeof (message)) {
442 ERROR ("write_graphite plugin: message buffer too small: "
443 "Need %zu bytes.", message_len + 1);
444 return (-1);
445 }
448 pthread_mutex_lock (&cb->send_lock);
450 if (cb->sock_fd < 0)
451 {
452 status = wg_callback_init (cb);
453 if (status != 0)
454 {
455 ERROR ("write_graphite plugin: wg_callback_init failed.");
456 pthread_mutex_unlock (&cb->send_lock);
457 return (-1);
458 }
459 }
461 if (message_len >= cb->send_buf_free)
462 {
463 status = wg_flush_nolock (/* timeout = */ 0, cb);
464 if (status != 0)
465 {
466 pthread_mutex_unlock (&cb->send_lock);
467 return (status);
468 }
469 }
470 assert (message_len < cb->send_buf_free);
472 /* `message_len + 1' because `message_len' does not include the
473 * trailing null byte. Neither does `send_buffer_fill'. */
474 memcpy (cb->send_buf + cb->send_buf_fill,
475 message, message_len + 1);
476 cb->send_buf_fill += message_len;
477 cb->send_buf_free -= message_len;
479 DEBUG ("write_graphite plugin: <%s:%d> buf %zu/%zu (%g%%) \"%s\"",
480 cb->host,
481 cb->port,
482 cb->send_buf_fill, sizeof (cb->send_buf),
483 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
484 message);
486 /* Check if we have enough space for this message. */
487 pthread_mutex_unlock (&cb->send_lock);
489 return (0);
490 }
492 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
493 struct wg_callback *cb)
494 {
495 char key[10*DATA_MAX_NAME_LEN];
496 char values[512];
498 int status, i;
500 if (0 != strcmp (ds->type, vl->type))
501 {
502 ERROR ("write_graphite plugin: DS type does not match "
503 "value list type");
504 return -1;
505 }
507 if (ds->ds_num > 1)
508 {
509 for (i = 0; i < ds->ds_num; i++)
510 {
511 /* Copy the identifier to `key' and escape it. */
512 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, ds->ds[i].name);
513 if (status != 0)
514 {
515 ERROR ("write_graphite plugin: error with format_name");
516 return (status);
517 }
519 escape_string (key, sizeof (key));
520 /* Convert the values to an ASCII representation and put that
521 * into `values'. */
522 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
523 if (status != 0)
524 {
525 ERROR ("write_graphite plugin: error with "
526 "wg_format_values");
527 return (status);
528 }
530 /* Send the message to graphite */
531 status = wg_send_message (key, values, vl->time, cb);
532 if (status != 0)
533 {
534 ERROR ("write_graphite plugin: error with "
535 "wg_send_message");
536 return (status);
537 }
538 }
539 }
540 else
541 {
542 /* Copy the identifier to `key' and escape it. */
543 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
544 if (status != 0)
545 {
546 ERROR ("write_graphite plugin: error with format_name");
547 return (status);
548 }
550 escape_string (key, sizeof (key));
551 /* Convert the values to an ASCII representation and put that into
552 * `values'. */
553 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
554 if (status != 0)
555 {
556 ERROR ("write_graphite plugin: error with "
557 "wg_format_values");
558 return (status);
559 }
561 /* Send the message to graphite */
562 status = wg_send_message (key, values, vl->time, cb);
563 if (status != 0)
564 {
565 ERROR ("write_graphite plugin: error with "
566 "wg_send_message");
567 return (status);
568 }
569 }
571 return (0);
572 }
574 static int wg_write (const data_set_t *ds, const value_list_t *vl,
575 user_data_t *user_data)
576 {
577 struct wg_callback *cb;
578 int status;
580 if (user_data == NULL)
581 return (-EINVAL);
583 cb = user_data->data;
585 status = wg_write_messages (ds, vl, cb);
587 return (status);
588 }
590 static int config_set_number (int *dest,
591 oconfig_item_t *ci)
592 {
593 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
594 {
595 WARNING ("write_graphite plugin: The `%s' config option "
596 "needs exactly one numeric argument.", ci->key);
597 return (-1);
598 }
600 *dest = ci->values[0].value.number;
602 return (0);
603 }
605 static int config_set_char (char *dest,
606 oconfig_item_t *ci)
607 {
608 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
609 {
610 WARNING ("write_graphite plugin: The `%s' config option "
611 "needs exactly one string argument.", ci->key);
612 return (-1);
613 }
615 *dest = ci->values[0].value.string[0];
617 return (0);
618 }
620 static int config_set_string (char **ret_string,
621 oconfig_item_t *ci)
622 {
623 char *string;
625 if ((ci->values_num != 1)
626 || (ci->values[0].type != OCONFIG_TYPE_STRING))
627 {
628 WARNING ("write_graphite plugin: The `%s' config option "
629 "needs exactly one string argument.", ci->key);
630 return (-1);
631 }
633 string = strdup (ci->values[0].value.string);
634 if (string == NULL)
635 {
636 ERROR ("write_graphite plugin: strdup failed.");
637 return (-1);
638 }
640 if (*ret_string != NULL)
641 sfree (*ret_string);
642 *ret_string = string;
644 return (0);
645 }
647 static int wg_config_carbon (oconfig_item_t *ci)
648 {
649 struct wg_callback *cb;
650 user_data_t user_data;
651 int i;
653 cb = malloc (sizeof (*cb));
654 if (cb == NULL)
655 {
656 ERROR ("write_graphite plugin: malloc failed.");
657 return (-1);
658 }
659 memset (cb, 0, sizeof (*cb));
660 cb->sock_fd = -1;
661 cb->host = NULL;
662 cb->port = 2003;
663 cb->prefix = NULL;
664 cb->server = NULL;
665 cb->dotchar = '_';
667 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
669 for (i = 0; i < ci->children_num; i++)
670 {
671 oconfig_item_t *child = ci->children + i;
673 if (strcasecmp ("Host", child->key) == 0)
674 config_set_string (&cb->host, child);
675 else if (strcasecmp ("Port", child->key) == 0)
676 config_set_number (&cb->port, child);
677 else if (strcasecmp ("Prefix", child->key) == 0)
678 config_set_string (&cb->prefix, child);
679 else if (strcasecmp ("DotCharacter", child->key) == 0)
680 config_set_char (&cb->dotchar, child);
681 else
682 {
683 ERROR ("write_graphite plugin: Invalid configuration "
684 "option: %s.", child->key);
685 }
686 }
688 DEBUG ("write_graphite: Registering write callback to carbon agent "
689 "%s:%d", cb->host, cb->port);
691 memset (&user_data, 0, sizeof (user_data));
692 user_data.data = cb;
693 user_data.free_func = NULL;
694 plugin_register_flush ("write_graphite", wg_flush, &user_data);
696 user_data.free_func = wg_callback_free;
697 plugin_register_write ("write_graphite", wg_write, &user_data);
699 return (0);
700 }
702 static int wg_config (oconfig_item_t *ci)
703 {
704 int i;
706 for (i = 0; i < ci->children_num; i++)
707 {
708 oconfig_item_t *child = ci->children + i;
710 if (strcasecmp ("Carbon", child->key) == 0)
711 wg_config_carbon (child);
712 else
713 {
714 ERROR ("write_graphite plugin: Invalid configuration "
715 "option: %s.", child->key);
716 }
717 }
719 return (0);
720 }
722 void module_register (void)
723 {
724 plugin_register_complex_config ("write_graphite", wg_config);
725 }
727 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */