2 * collectd - src/write_tsdb.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-2012 Florian octo Forster
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.
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.
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
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>
30 * Brett Hawn <bhawn at llnw.com>
31 * Kevin Bowling <kbowling@llnw.com>
33 * Based on the write_graphite plugin.
36 /* write_tsdb plugin configuation example
50 #include "configfile.h"
52 #include "utils_cache.h"
53 #include "utils_parse_option.h"
55 /* Folks without pthread will need to disable this plugin. */
58 #include <sys/socket.h>
61 #ifndef WT_DEFAULT_NODE
62 # define WT_DEFAULT_NODE "localhost"
65 #ifndef WT_DEFAULT_SERVICE
66 # define WT_DEFAULT_SERVICE "4242"
69 #ifndef WT_DEFAULT_ESCAPE
70 # define WT_DEFAULT_ESCAPE '.'
73 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
74 #ifndef WT_SEND_BUF_SIZE
75 # define WT_SEND_BUF_SIZE 1428
92 _Bool separate_instances;
93 _Bool always_append_ds;
95 char send_buf[WT_SEND_BUF_SIZE];
98 cdtime_t send_buf_init_time;
100 pthread_mutex_t send_lock;
107 static void wt_reset_buffer (struct wt_callback *cb)
109 memset (cb->send_buf, 0, sizeof (cb->send_buf));
110 cb->send_buf_free = sizeof (cb->send_buf);
111 cb->send_buf_fill = 0;
112 cb->send_buf_init_time = cdtime ();
115 static int wt_send_buffer (struct wt_callback *cb)
119 status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
123 ERROR ("write_tsdb plugin: send failed with status %zi (%s)",
124 status, sstrerror (errno, errbuf, sizeof (errbuf)));
136 /* NOTE: You must hold cb->send_lock when calling this function! */
137 static int wt_flush_nolock (cdtime_t timeout, struct wt_callback *cb)
141 DEBUG ("write_tsdb plugin: wt_flush_nolock: timeout = %.3f; "
142 "send_buf_fill = %zu;",
146 /* timeout == 0 => flush unconditionally */
152 if ((cb->send_buf_init_time + timeout) > now)
156 if (cb->send_buf_fill <= 0)
158 cb->send_buf_init_time = cdtime ();
162 status = wt_send_buffer (cb);
163 wt_reset_buffer (cb);
168 static int wt_callback_init (struct wt_callback *cb)
170 struct addrinfo ai_hints;
171 struct addrinfo *ai_list;
172 struct addrinfo *ai_ptr;
175 const char *node = cb->node ? cb->node : WT_DEFAULT_NODE;
176 const char *service = cb->service ? cb->service : WT_DEFAULT_SERVICE;
181 memset (&ai_hints, 0, sizeof (ai_hints));
183 ai_hints.ai_flags |= AI_ADDRCONFIG;
185 ai_hints.ai_family = AF_UNSPEC;
186 ai_hints.ai_socktype = SOCK_STREAM;
190 status = getaddrinfo (node, service, &ai_hints, &ai_list);
193 ERROR ("write_tsdb plugin: getaddrinfo (%s, %s) failed: %s",
194 node, service, gai_strerror (status));
198 assert (ai_list != NULL);
199 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
201 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
202 ai_ptr->ai_protocol);
206 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
217 freeaddrinfo (ai_list);
222 ERROR ("write_tsdb plugin: Connecting to %s:%s failed. "
223 "The last error was: %s", node, service,
224 sstrerror (errno, errbuf, sizeof (errbuf)));
229 wt_reset_buffer (cb);
234 static void wt_callback_free (void *data)
236 struct wt_callback *cb;
243 pthread_mutex_lock (&cb->send_lock);
245 wt_flush_nolock (/* timeout = */ 0, cb);
255 pthread_mutex_destroy (&cb->send_lock);
260 static int wt_flush (cdtime_t timeout,
261 const char *identifier __attribute__((unused)),
262 user_data_t *user_data)
264 struct wt_callback *cb;
267 if (user_data == NULL)
270 cb = user_data->data;
272 pthread_mutex_lock (&cb->send_lock);
276 status = wt_callback_init (cb);
279 ERROR ("write_tsdb plugin: wt_callback_init failed.");
280 pthread_mutex_unlock (&cb->send_lock);
285 status = wt_flush_nolock (timeout, cb);
286 pthread_mutex_unlock (&cb->send_lock);
291 static int wt_format_values (char *ret, size_t ret_len,
292 int ds_num, const data_set_t *ds,
293 const value_list_t *vl,
298 gauge_t *rates = NULL;
300 assert (0 == strcmp (ds->type, vl->type));
302 memset (ret, 0, ret_len);
304 #define BUFFER_ADD(...) do { \
305 status = ssnprintf (ret + offset, ret_len - offset, \
312 else if (((size_t) status) >= (ret_len - offset)) \
318 offset += ((size_t) status); \
321 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
322 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
323 else if (store_rates)
326 rates = uc_get_rate (ds, vl);
329 WARNING ("format_values: "
330 "uc_get_rate failed.");
333 BUFFER_ADD ("%f", rates[ds_num]);
335 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
336 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
337 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
338 BUFFER_ADD ("%" PRIi64, vl->values[ds_num].derive);
339 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
340 BUFFER_ADD ("%" PRIu64, vl->values[ds_num].absolute);
343 ERROR ("format_values plugin: Unknown data source type: %i",
344 ds->ds[ds_num].type);
355 static int wt_format_name (char *ret, int ret_len,
356 const value_list_t *vl,
357 const struct wt_callback *cb,
367 postfix = cb->postfix;
371 if (ds_name != NULL) {
372 if (vl->plugin_instance[0] == '\0') {
373 ssnprintf(ret, ret_len, "%s.%s.%s",
374 prefix, vl->plugin, ds_name);
375 } else if (vl->type_instance == '\0') {
376 ssnprintf(ret, ret_len, "%s.%s.%s.%s.%s",
377 prefix, vl->plugin, vl->plugin_instance,
378 vl->type_instance, ds_name);
380 ssnprintf(ret, ret_len, "%s.%s.%s.%s.%s",
381 prefix, vl->plugin, vl->plugin_instance, vl->type,
384 } else if (vl->plugin_instance[0] == '\0') {
385 if (vl->type_instance[0] == '\0')
386 ssnprintf(ret, ret_len, "%s.%s.%s",
387 prefix, vl->plugin, vl->type);
389 ssnprintf(ret, ret_len, "%s.%s.%s",
390 prefix, vl->plugin, vl->type_instance);
391 } else if (vl->type_instance[0] == '\0') {
392 ssnprintf(ret, ret_len, "%s.%s.%s.%s",
393 prefix, vl->plugin, vl->plugin_instance, vl->type);
395 ssnprintf(ret, ret_len, "%s.%s.%s.%s",
396 prefix, vl->plugin, vl->plugin_instance, vl->type_instance);
402 static int wt_send_message (const char* key, const char* value,
403 cdtime_t time, struct wt_callback *cb,
410 /* skip if value is NaN */
414 message_len = (size_t) ssnprintf (message, sizeof (message),
415 "put %s %u %s fqdn=%s\r\n",
417 (unsigned int) CDTIME_T_TO_TIME_T (
421 if (message_len >= sizeof (message)) {
422 ERROR ("write_tsdb plugin: message buffer too small: "
423 "Need %zu bytes.", message_len + 1);
427 pthread_mutex_lock (&cb->send_lock);
431 status = wt_callback_init (cb);
434 ERROR ("write_tsdb plugin: wt_callback_init failed.");
435 pthread_mutex_unlock (&cb->send_lock);
440 if (message_len >= cb->send_buf_free)
442 status = wt_flush_nolock (/* timeout = */ 0, cb);
445 pthread_mutex_unlock (&cb->send_lock);
450 /* Assert that we have enough space for this message. */
451 assert (message_len < cb->send_buf_free);
453 /* `message_len + 1' because `message_len' does not include the
454 * trailing null byte. Neither does `send_buffer_fill'. */
455 memcpy (cb->send_buf + cb->send_buf_fill,
456 message, message_len + 1);
457 cb->send_buf_fill += message_len;
458 cb->send_buf_free -= message_len;
460 DEBUG ("write_tsdb plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
463 cb->send_buf_fill, sizeof (cb->send_buf),
464 100.0 * ((double) cb->send_buf_fill) /
465 ((double) sizeof (cb->send_buf)),
468 pthread_mutex_unlock (&cb->send_lock);
473 static int wt_write_messages (const data_set_t *ds, const value_list_t *vl,
474 struct wt_callback *cb)
476 char key[10*DATA_MAX_NAME_LEN];
481 if (0 != strcmp (ds->type, vl->type))
483 ERROR ("write_tsdb plugin: DS type does not match "
488 for (i = 0; i < ds->ds_num; i++)
490 const char *ds_name = NULL;
492 if (cb->always_append_ds || (ds->ds_num > 1))
493 ds_name = ds->ds[i].name;
495 /* Copy the identifier to `key' and escape it. */
496 status = wt_format_name (key, sizeof (key), vl, cb, ds_name);
499 ERROR ("write_tsdb plugin: error with format_name");
503 escape_string (key, sizeof (key));
504 /* Convert the values to an ASCII representation and put that into
506 status = wt_format_values (values, sizeof (values), i, ds, vl,
510 ERROR ("write_tsdb plugin: error with "
515 /* Send the message to tsdb */
516 status = wt_send_message (key, values, vl->time, cb, vl->host);
519 ERROR ("write_tsdb plugin: error with "
528 static int wt_write (const data_set_t *ds, const value_list_t *vl,
529 user_data_t *user_data)
531 struct wt_callback *cb;
534 if (user_data == NULL)
537 cb = user_data->data;
539 status = wt_write_messages (ds, vl, cb);
544 static int config_set_char (char *dest,
550 memset (buffer, 0, sizeof (buffer));
552 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
558 ERROR ("write_tsdb plugin: Cannot use an empty string for the "
559 "\"EscapeCharacter\" option.");
565 WARNING ("write_tsdb plugin: Only the first character of the "
566 "\"EscapeCharacter\" option ('%c') will be used.",
575 static int wt_config_tsd (oconfig_item_t *ci)
577 struct wt_callback *cb;
578 user_data_t user_data;
579 char callback_name[DATA_MAX_NAME_LEN];
582 cb = malloc (sizeof (*cb));
585 ERROR ("write_tsdb plugin: malloc failed.");
588 memset (cb, 0, sizeof (*cb));
594 cb->escape_char = WT_DEFAULT_ESCAPE;
597 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
599 for (i = 0; i < ci->children_num; i++)
601 oconfig_item_t *child = ci->children + i;
603 if (strcasecmp ("Host", child->key) == 0)
604 cf_util_get_string (child, &cb->node);
605 else if (strcasecmp ("Port", child->key) == 0)
606 cf_util_get_service (child, &cb->service);
607 else if (strcasecmp ("Prefix", child->key) == 0)
608 cf_util_get_string (child, &cb->prefix);
609 else if (strcasecmp ("Postfix", child->key) == 0)
610 cf_util_get_string (child, &cb->postfix);
611 else if (strcasecmp ("StoreRates", child->key) == 0)
612 cf_util_get_boolean (child, &cb->store_rates);
613 else if (strcasecmp ("SeparateInstances", child->key) == 0)
614 cf_util_get_boolean (child, &cb->separate_instances);
615 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
616 cf_util_get_boolean (child, &cb->always_append_ds);
617 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
618 config_set_char (&cb->escape_char, child);
621 ERROR ("write_tsdb plugin: Invalid configuration "
622 "option: %s.", child->key);
626 ssnprintf (callback_name, sizeof (callback_name), "write_tsdb/%s/%s",
627 cb->node != NULL ? cb->node : WT_DEFAULT_NODE,
628 cb->service != NULL ? cb->service : WT_DEFAULT_SERVICE);
630 memset (&user_data, 0, sizeof (user_data));
632 user_data.free_func = wt_callback_free;
633 plugin_register_write (callback_name, wt_write, &user_data);
635 user_data.free_func = NULL;
636 plugin_register_flush (callback_name, wt_flush, &user_data);
641 static int wt_config (oconfig_item_t *ci)
645 for (i = 0; i < ci->children_num; i++)
647 oconfig_item_t *child = ci->children + i;
649 if (strcasecmp ("Node", child->key) == 0)
650 wt_config_tsd (child);
653 ERROR ("write_tsdb plugin: Invalid configuration "
654 "option: %s.", child->key);
661 void module_register (void)
663 plugin_register_complex_config ("write_tsdb", wt_config);
666 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */