42fb6950737abe22cb8a9e28b0724c8e1529e9bc
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 "local-agent">
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, prefix, name) \
55 wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
56 (vl)->type, (vl)->type_instance, prefix, name)
57 #endif
59 #ifndef WG_SEND_BUF_SIZE
60 #define WG_SEND_BUF_SIZE 4096
61 #endif
63 /*
64 * Private variables
65 */
66 struct wg_callback
67 {
68 char *name;
70 int sock_fd;
71 struct hostent *server;
73 char *host;
74 int port;
75 char *prefix;
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, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
183 if (status < 0)
184 {
185 char errbuf[1024];
186 sstrerror (errno, errbuf, sizeof (errbuf));
187 ERROR ("write_graphite plugin: connect failed: %s", errbuf);
188 close (cb->sock_fd);
189 cb->sock_fd = -1;
190 return (-1);
191 }
193 wg_reset_buffer (cb);
195 return (0);
196 }
198 static void wg_callback_free (void *data)
199 {
200 struct wg_callback *cb;
202 if (data == NULL)
203 return;
205 cb = data;
207 wg_flush_nolock (/* timeout = */ 0, cb);
209 close(cb->sock_fd);
210 sfree(cb->name);
211 sfree(cb->host);
212 sfree(cb->prefix);
214 sfree(cb);
215 }
217 static int wg_flush (cdtime_t timeout,
218 const char *identifier __attribute__((unused)),
219 user_data_t *user_data)
220 {
221 struct wg_callback *cb;
222 int status;
224 if (user_data == NULL)
225 return (-EINVAL);
227 cb = user_data->data;
229 pthread_mutex_lock (&cb->send_lock);
231 if (cb->sock_fd < 0)
232 {
233 status = wg_callback_init (cb);
234 if (status != 0)
235 {
236 ERROR ("write_graphite plugin: wg_callback_init failed.");
237 pthread_mutex_unlock (&cb->send_lock);
238 return (-1);
239 }
240 }
242 status = wg_flush_nolock (timeout, cb);
243 pthread_mutex_unlock (&cb->send_lock);
245 return (status);
246 }
248 static int wg_format_values (char *ret, size_t ret_len,
249 int ds_num, const data_set_t *ds, const value_list_t *vl,
250 _Bool store_rates)
251 {
252 size_t offset = 0;
253 int status;
254 gauge_t *rates = NULL;
256 assert (0 == strcmp (ds->type, vl->type));
258 memset (ret, 0, ret_len);
260 #define BUFFER_ADD(...) do { \
261 status = ssnprintf (ret + offset, ret_len - offset, \
262 __VA_ARGS__); \
263 if (status < 1) \
264 { \
265 sfree (rates); \
266 return (-1); \
267 } \
268 else if (((size_t) status) >= (ret_len - offset)) \
269 { \
270 sfree (rates); \
271 return (-1); \
272 } \
273 else \
274 offset += ((size_t) status); \
275 } while (0)
277 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
278 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
279 else if (store_rates)
280 {
281 if (rates == NULL)
282 rates = uc_get_rate (ds, vl);
283 if (rates == NULL)
284 {
285 WARNING ("format_values: "
286 "uc_get_rate failed.");
287 return (-1);
288 }
289 BUFFER_ADD ("%g", rates[ds_num]);
290 }
291 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
292 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
293 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
294 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
295 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
296 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
297 else
298 {
299 ERROR ("format_values plugin: Unknown data source type: %i",
300 ds->ds[ds_num].type);
301 sfree (rates);
302 return (-1);
303 }
305 #undef BUFFER_ADD
307 sfree (rates);
308 return (0);
309 }
311 static int normalize_hostname (char *dst, const char *src)
312 {
313 size_t i;
315 int reps = 0;
317 for (i = 0; i < strlen(src) ; i++)
318 {
319 if (src[i] == '.')
320 {
321 dst[i] = '_';
322 ++reps;
323 }
324 else
325 dst[i] = src[i];
326 }
327 dst[i] = '\0';
329 return reps;
330 }
332 static int wg_format_name (char *ret, int ret_len,
333 const char *hostname,
334 const char *plugin, const char *plugin_instance,
335 const char *type, const char *type_instance,
336 const char *prefix, const char *ds_name)
337 {
338 int status;
339 char *n_hostname;
341 assert (plugin != NULL);
342 assert (type != NULL);
344 if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
345 {
346 ERROR ("Unable to allocate memory for normalized hostname buffer");
347 return (-1);
348 }
350 if (normalize_hostname(n_hostname, hostname) == -1)
351 {
352 ERROR ("Unable to normalize hostname");
353 return (-1);
354 }
356 if ((plugin_instance == NULL) || (plugin_instance[0] == '\0'))
357 {
358 if ((type_instance == NULL) || (type_instance[0] == '\0'))
359 {
360 if ((ds_name == NULL) || (ds_name[0] == '\0'))
361 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s",
362 prefix, n_hostname, plugin, type);
363 else
364 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
365 prefix, n_hostname, plugin, type, ds_name);
366 }
367 else
368 {
369 if ((ds_name == NULL) || (ds_name[0] == '\0'))
370 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s",
371 prefix, n_hostname, plugin, type,
372 type_instance);
373 else
374 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s.%s",
375 prefix, n_hostname, plugin, type,
376 type_instance, ds_name);
377 }
378 }
379 else
380 {
381 if ((type_instance == NULL) || (type_instance[0] == '\0'))
382 {
383 if ((ds_name == NULL) || (ds_name[0] == '\0'))
384 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
385 prefix, n_hostname, plugin,
386 plugin_instance, type);
387 else
388 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s.%s",
389 prefix, n_hostname, plugin,
390 plugin_instance, type, ds_name);
391 }
392 else
393 {
394 if ((ds_name == NULL) || (ds_name[0] == '\0'))
395 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s",
396 prefix, n_hostname, plugin,
397 plugin_instance, type, type_instance);
398 else
399 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s.%s",
400 prefix, n_hostname, plugin,
401 plugin_instance, type, type_instance, ds_name);
402 }
403 }
405 sfree(n_hostname);
407 if ((status < 1) || (status >= ret_len))
408 return (-1);
409 return (0);
410 }
412 static int wg_send_message (const char* key, const char* value, cdtime_t time, struct wg_callback *cb)
413 {
414 int status;
415 size_t message_len;
416 char message[1024];
418 message_len = (size_t) ssnprintf (message, sizeof (message),
419 "%s %s %.0f\n",
420 key,
421 value,
422 CDTIME_T_TO_DOUBLE(time));
423 if (message_len >= sizeof (message)) {
424 ERROR ("write_graphite plugin: message buffer too small: "
425 "Need %zu bytes.", message_len + 1);
426 return (-1);
427 }
430 pthread_mutex_lock (&cb->send_lock);
432 if (cb->sock_fd < 0)
433 {
434 status = wg_callback_init (cb);
435 if (status != 0)
436 {
437 ERROR ("write_graphite plugin: wg_callback_init failed.");
438 pthread_mutex_unlock (&cb->send_lock);
439 return (-1);
440 }
441 }
443 if (message_len >= cb->send_buf_free)
444 {
445 status = wg_flush_nolock (/* timeout = */ 0, cb);
446 if (status != 0)
447 {
448 pthread_mutex_unlock (&cb->send_lock);
449 return (status);
450 }
451 }
452 assert (message_len < cb->send_buf_free);
454 /* `message_len + 1' because `message_len' does not include the
455 * trailing null byte. Neither does `send_buffer_fill'. */
456 memcpy (cb->send_buf + cb->send_buf_fill,
457 message, message_len + 1);
458 cb->send_buf_fill += message_len;
459 cb->send_buf_free -= message_len;
461 DEBUG ("write_graphite plugin: <%s:%d> buf %zu/%zu (%g%%) \"%s\"",
462 cb->host,
463 cb->port,
464 cb->send_buf_fill, sizeof (cb->send_buf),
465 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
466 message);
468 /* Check if we have enough space for this message. */
469 pthread_mutex_unlock (&cb->send_lock);
471 return (0);
472 }
474 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
475 struct wg_callback *cb)
476 {
477 char key[10*DATA_MAX_NAME_LEN];
478 char values[512];
480 int status, i;
482 if (0 != strcmp (ds->type, vl->type))
483 {
484 ERROR ("write_graphite plugin: DS type does not match "
485 "value list type");
486 return -1;
487 }
489 if (ds->ds_num > 1)
490 {
491 for (i = 0; i < ds->ds_num; i++)
492 {
493 /* Copy the identifier to `key' and escape it. */
494 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, ds->ds[i].name);
495 if (status != 0)
496 {
497 ERROR ("write_graphite plugin: error with format_name");
498 return (status);
499 }
501 escape_string (key, sizeof (key));
502 /* Convert the values to an ASCII representation and put that into
503 * `values'. */
504 status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
505 if (status != 0)
506 {
507 ERROR ("write_graphite plugin: error with "
508 "wg_format_values");
509 return (status);
510 }
512 /* Send the message to graphite */
513 status = wg_send_message (key, values, vl->time, cb);
514 if (status != 0)
515 {
516 ERROR ("write_graphite plugin: error with "
517 "wg_send_message");
518 return (status);
519 }
520 }
521 }
522 else
523 {
524 /* Copy the identifier to `key' and escape it. */
525 status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, NULL);
526 if (status != 0)
527 {
528 ERROR ("write_graphite plugin: error with format_name");
529 return (status);
530 }
532 escape_string (key, sizeof (key));
533 /* Convert the values to an ASCII representation and put that into
534 * `values'. */
535 status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
536 if (status != 0)
537 {
538 ERROR ("write_graphite plugin: error with "
539 "wg_format_values");
540 return (status);
541 }
543 /* Send the message to graphite */
544 status = wg_send_message (key, values, vl->time, cb);
545 if (status != 0)
546 {
547 ERROR ("write_graphite plugin: error with "
548 "wg_send_message");
549 return (status);
550 }
551 }
553 return (0);
554 }
556 static int wg_write (const data_set_t *ds, const value_list_t *vl,
557 user_data_t *user_data)
558 {
559 struct wg_callback *cb;
560 int status;
562 if (user_data == NULL)
563 return (-EINVAL);
565 cb = user_data->data;
567 status = wg_write_messages (ds, vl, cb);
569 return (status);
570 }
572 static int config_set_number (int *dest,
573 oconfig_item_t *ci)
574 {
575 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
576 {
577 WARNING ("write_graphite plugin: The `%s' config option "
578 "needs exactly one numeric argument.", ci->key);
579 return (-1);
580 }
582 *dest = ci->values[0].value.number;
584 return (0);
585 }
587 static int config_set_string (char **ret_string,
588 oconfig_item_t *ci)
589 {
590 char *string;
592 if ((ci->values_num != 1)
593 || (ci->values[0].type != OCONFIG_TYPE_STRING))
594 {
595 WARNING ("write_graphite plugin: The `%s' config option "
596 "needs exactly one string argument.", ci->key);
597 return (-1);
598 }
600 string = strdup (ci->values[0].value.string);
601 if (string == NULL)
602 {
603 ERROR ("write_graphite plugin: strdup failed.");
604 return (-1);
605 }
607 if (*ret_string != NULL)
608 sfree (*ret_string);
609 *ret_string = string;
611 return (0);
612 }
614 static int wg_config_carbon (oconfig_item_t *ci)
615 {
616 struct wg_callback *cb;
617 user_data_t user_data;
618 int i;
620 cb = malloc (sizeof (*cb));
621 if (cb == NULL)
622 {
623 ERROR ("write_graphite plugin: malloc failed.");
624 return (-1);
625 }
626 memset (cb, 0, sizeof (*cb));
627 cb->sock_fd = -1;
628 cb->host = NULL;
629 cb->name = NULL;
630 cb->port = 2003;
631 cb->prefix = NULL;
632 cb->server = NULL;
634 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
636 config_set_string (&cb->name, ci);
637 if (cb->name == NULL)
638 return (-1);
640 for (i = 0; i < ci->children_num; i++)
641 {
642 oconfig_item_t *child = ci->children + i;
644 if (strcasecmp ("Host", child->key) == 0)
645 config_set_string (&cb->host, child);
646 else if (strcasecmp ("Port", child->key) == 0)
647 config_set_number (&cb->port, child);
648 else if (strcasecmp ("Prefix", child->key) == 0)
649 config_set_string (&cb->prefix, child);
650 else
651 {
652 ERROR ("write_graphite plugin: Invalid configuration "
653 "option: %s.", child->key);
654 }
655 }
657 DEBUG ("write_graphite: Registering write callback to carbon agent "
658 "%s:%d", cb->host, cb->port);
660 memset (&user_data, 0, sizeof (user_data));
661 user_data.data = cb;
662 user_data.free_func = NULL;
663 plugin_register_flush ("write_graphite", wg_flush, &user_data);
665 user_data.free_func = wg_callback_free;
666 plugin_register_write ("write_graphite", wg_write, &user_data);
668 return (0);
669 }
671 static int wg_config (oconfig_item_t *ci)
672 {
673 int i;
675 for (i = 0; i < ci->children_num; i++)
676 {
677 oconfig_item_t *child = ci->children + i;
679 if (strcasecmp ("Carbon", child->key) == 0)
680 wg_config_carbon (child);
681 else
682 {
683 ERROR ("write_graphite plugin: Invalid configuration "
684 "option: %s.", child->key);
685 }
686 }
688 return (0);
689 }
691 void module_register (void)
692 {
693 plugin_register_complex_config ("write_graphite", wg_config);
694 }
696 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */