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>
29 * Modified by Brett Hawn <bhawn at llnw.com>
30 * Based on the write_graphite plugin.
33 /* write_tsdb plugin configuation example
47 #include "configfile.h"
49 #include "utils_cache.h"
50 #include "utils_parse_option.h"
52 /* Folks without pthread will need to disable this plugin. */
55 #include <sys/socket.h>
58 #ifndef WT_DEFAULT_NODE
59 # define WT_DEFAULT_NODE "localhost"
62 #ifndef WT_DEFAULT_SERVICE
63 # define WT_DEFAULT_SERVICE "4242"
66 #ifndef WT_DEFAULT_ESCAPE
67 # define WT_DEFAULT_ESCAPE '.'
70 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
71 #ifndef WT_SEND_BUF_SIZE
72 # define WT_SEND_BUF_SIZE 1428
89 _Bool separate_instances;
90 _Bool always_append_ds;
92 char send_buf[WT_SEND_BUF_SIZE];
95 cdtime_t send_buf_init_time;
97 pthread_mutex_t send_lock;
104 static void wt_reset_buffer (struct wt_callback *cb)
106 memset (cb->send_buf, 0, sizeof (cb->send_buf));
107 cb->send_buf_free = sizeof (cb->send_buf);
108 cb->send_buf_fill = 0;
109 cb->send_buf_init_time = cdtime ();
112 static int wt_send_buffer (struct wt_callback *cb)
116 status = swrite (cb->sock_fd, cb->send_buf, strlen (cb->send_buf));
120 ERROR ("write_tsdb plugin: send failed with status %zi (%s)",
121 status, sstrerror (errno, errbuf, sizeof (errbuf)));
133 /* NOTE: You must hold cb->send_lock when calling this function! */
134 static int wt_flush_nolock (cdtime_t timeout, struct wt_callback *cb)
138 DEBUG ("write_tsdb plugin: wt_flush_nolock: timeout = %.3f; "
139 "send_buf_fill = %zu;",
143 /* timeout == 0 => flush unconditionally */
149 if ((cb->send_buf_init_time + timeout) > now)
153 if (cb->send_buf_fill <= 0)
155 cb->send_buf_init_time = cdtime ();
159 status = wt_send_buffer (cb);
160 wt_reset_buffer (cb);
165 static int wt_callback_init (struct wt_callback *cb)
167 struct addrinfo ai_hints;
168 struct addrinfo *ai_list;
169 struct addrinfo *ai_ptr;
172 const char *node = cb->node ? cb->node : WT_DEFAULT_NODE;
173 const char *service = cb->service ? cb->service : WT_DEFAULT_SERVICE;
178 memset (&ai_hints, 0, sizeof (ai_hints));
180 ai_hints.ai_flags |= AI_ADDRCONFIG;
182 ai_hints.ai_family = AF_UNSPEC;
183 ai_hints.ai_socktype = SOCK_STREAM;
187 status = getaddrinfo (node, service, &ai_hints, &ai_list);
190 ERROR ("write_tsdb plugin: getaddrinfo (%s, %s) failed: %s",
191 node, service, gai_strerror (status));
195 assert (ai_list != NULL);
196 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
198 cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
199 ai_ptr->ai_protocol);
203 status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
214 freeaddrinfo (ai_list);
219 ERROR ("write_tsdb plugin: Connecting to %s:%s failed. "
220 "The last error was: %s", node, service,
221 sstrerror (errno, errbuf, sizeof (errbuf)));
226 wt_reset_buffer (cb);
231 static void wt_callback_free (void *data)
233 struct wt_callback *cb;
240 pthread_mutex_lock (&cb->send_lock);
242 wt_flush_nolock (/* timeout = */ 0, cb);
252 pthread_mutex_destroy (&cb->send_lock);
257 static int wt_flush (cdtime_t timeout,
258 const char *identifier __attribute__((unused)),
259 user_data_t *user_data)
261 struct wt_callback *cb;
264 if (user_data == NULL)
267 cb = user_data->data;
269 pthread_mutex_lock (&cb->send_lock);
273 status = wt_callback_init (cb);
276 ERROR ("write_tsdb plugin: wt_callback_init failed.");
277 pthread_mutex_unlock (&cb->send_lock);
282 status = wt_flush_nolock (timeout, cb);
283 pthread_mutex_unlock (&cb->send_lock);
288 static int wt_format_values (char *ret, size_t ret_len,
289 int ds_num, const data_set_t *ds, const value_list_t *vl,
294 gauge_t *rates = NULL;
296 assert (0 == strcmp (ds->type, vl->type));
298 memset (ret, 0, ret_len);
300 #define BUFFER_ADD(...) do { \
301 status = ssnprintf (ret + offset, ret_len - offset, \
308 else if (((size_t) status) >= (ret_len - offset)) \
314 offset += ((size_t) status); \
317 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
318 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
319 else if (store_rates)
322 rates = uc_get_rate (ds, vl);
325 WARNING ("format_values: "
326 "uc_get_rate failed.");
329 BUFFER_ADD ("%f", rates[ds_num]);
331 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
332 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
333 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
334 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
335 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
336 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
339 ERROR ("format_values plugin: Unknown data source type: %i",
340 ds->ds[ds_num].type);
351 static int wt_format_name (char *ret, int ret_len,
352 const value_list_t *vl,
353 const struct wt_callback *cb,
363 postfix = cb->postfix;
367 if (ds_name != NULL) {
368 if (vl->plugin_instance[0] == '\0') {
369 ssnprintf(ret, ret_len, "%s.%s.%s",
370 prefix, vl->plugin, ds_name);
371 } else if (vl->type_instance == '\0') {
372 ssnprintf(ret, ret_len, "%s.%s.%s.%s.%s",
373 prefix, vl->plugin, vl->plugin_instance, vl->type_instance, ds_name);
375 ssnprintf(ret, ret_len, "%s.%s.%s.%s.%s",
376 prefix, vl->plugin, vl->plugin_instance, vl->type, ds_name);
378 } else if (vl->plugin_instance[0] == '\0') {
379 if (vl->type_instance[0] == '\0')
380 ssnprintf(ret, ret_len, "%s.%s.%s",
381 prefix, vl->plugin, vl->type);
383 ssnprintf(ret, ret_len, "%s.%s.%s",
384 prefix, vl->plugin, vl->type_instance);
385 } else if (vl->type_instance[0] == '\0') {
386 ssnprintf(ret, ret_len, "%s.%s.%s.%s",
387 prefix, vl->plugin, vl->plugin_instance, vl->type);
389 ssnprintf(ret, ret_len, "%s.%s.%s.%s",
390 prefix, vl->plugin, vl->plugin_instance, vl->type_instance);
396 static int wt_send_message (const char* key, const char* value,
397 cdtime_t time, struct wt_callback *cb, const char* host)
403 /* skip if value is NaN */
407 message_len = (size_t) ssnprintf (message, sizeof (message),
408 "put %s %u %s fqdn=%s\r\n",
410 (unsigned int) CDTIME_T_TO_TIME_T (time),
413 if (message_len >= sizeof (message)) {
414 ERROR ("write_tsdb plugin: message buffer too small: "
415 "Need %zu bytes.", message_len + 1);
419 pthread_mutex_lock (&cb->send_lock);
423 status = wt_callback_init (cb);
426 ERROR ("write_tsdb plugin: wt_callback_init failed.");
427 pthread_mutex_unlock (&cb->send_lock);
432 if (message_len >= cb->send_buf_free)
434 status = wt_flush_nolock (/* timeout = */ 0, cb);
437 pthread_mutex_unlock (&cb->send_lock);
442 /* Assert that we have enough space for this message. */
443 assert (message_len < cb->send_buf_free);
445 /* `message_len + 1' because `message_len' does not include the
446 * trailing null byte. Neither does `send_buffer_fill'. */
447 memcpy (cb->send_buf + cb->send_buf_fill,
448 message, message_len + 1);
449 cb->send_buf_fill += message_len;
450 cb->send_buf_free -= message_len;
452 DEBUG ("write_tsdb plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
455 cb->send_buf_fill, sizeof (cb->send_buf),
456 100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
459 pthread_mutex_unlock (&cb->send_lock);
464 static int wt_write_messages (const data_set_t *ds, const value_list_t *vl,
465 struct wt_callback *cb)
467 char key[10*DATA_MAX_NAME_LEN];
472 if (0 != strcmp (ds->type, vl->type))
474 ERROR ("write_tsdb plugin: DS type does not match "
479 for (i = 0; i < ds->ds_num; i++)
481 const char *ds_name = NULL;
483 if (cb->always_append_ds || (ds->ds_num > 1))
484 ds_name = ds->ds[i].name;
486 /* Copy the identifier to `key' and escape it. */
487 status = wt_format_name (key, sizeof (key), vl, cb, ds_name);
490 ERROR ("write_tsdb plugin: error with format_name");
494 escape_string (key, sizeof (key));
495 /* Convert the values to an ASCII representation and put that into
497 status = wt_format_values (values, sizeof (values), i, ds, vl,
501 ERROR ("write_tsdb plugin: error with "
506 /* Send the message to tsdb */
507 status = wt_send_message (key, values, vl->time, cb, vl->host);
510 ERROR ("write_tsdb plugin: error with "
519 static int wt_write (const data_set_t *ds, const value_list_t *vl,
520 user_data_t *user_data)
522 struct wt_callback *cb;
525 if (user_data == NULL)
528 cb = user_data->data;
530 status = wt_write_messages (ds, vl, cb);
535 static int config_set_char (char *dest,
541 memset (buffer, 0, sizeof (buffer));
543 status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
549 ERROR ("write_tsdb plugin: Cannot use an empty string for the "
550 "\"EscapeCharacter\" option.");
556 WARNING ("write_tsdb plugin: Only the first character of the "
557 "\"EscapeCharacter\" option ('%c') will be used.",
566 static int wt_config_tsd (oconfig_item_t *ci)
568 struct wt_callback *cb;
569 user_data_t user_data;
570 char callback_name[DATA_MAX_NAME_LEN];
573 cb = malloc (sizeof (*cb));
576 ERROR ("write_tsdb plugin: malloc failed.");
579 memset (cb, 0, sizeof (*cb));
585 cb->escape_char = WT_DEFAULT_ESCAPE;
588 pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
590 for (i = 0; i < ci->children_num; i++)
592 oconfig_item_t *child = ci->children + i;
594 if (strcasecmp ("Host", child->key) == 0)
595 cf_util_get_string (child, &cb->node);
596 else if (strcasecmp ("Port", child->key) == 0)
597 cf_util_get_service (child, &cb->service);
598 else if (strcasecmp ("Prefix", child->key) == 0)
599 cf_util_get_string (child, &cb->prefix);
600 else if (strcasecmp ("Postfix", child->key) == 0)
601 cf_util_get_string (child, &cb->postfix);
602 else if (strcasecmp ("StoreRates", child->key) == 0)
603 cf_util_get_boolean (child, &cb->store_rates);
604 else if (strcasecmp ("SeparateInstances", child->key) == 0)
605 cf_util_get_boolean (child, &cb->separate_instances);
606 else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
607 cf_util_get_boolean (child, &cb->always_append_ds);
608 else if (strcasecmp ("EscapeCharacter", child->key) == 0)
609 config_set_char (&cb->escape_char, child);
612 ERROR ("write_tsdb plugin: Invalid configuration "
613 "option: %s.", child->key);
617 ssnprintf (callback_name, sizeof (callback_name), "write_tsdb/%s/%s",
618 cb->node != NULL ? cb->node : WT_DEFAULT_NODE,
619 cb->service != NULL ? cb->service : WT_DEFAULT_SERVICE);
621 memset (&user_data, 0, sizeof (user_data));
623 user_data.free_func = wt_callback_free;
624 plugin_register_write (callback_name, wt_write, &user_data);
626 user_data.free_func = NULL;
627 plugin_register_flush (callback_name, wt_flush, &user_data);
632 static int wt_config (oconfig_item_t *ci)
636 for (i = 0; i < ci->children_num; i++)
638 oconfig_item_t *child = ci->children + i;
640 if (strcasecmp ("Node", child->key) == 0)
641 wt_config_tsd (child);
644 ERROR ("write_tsdb plugin: Invalid configuration "
645 "option: %s.", child->key);
652 void module_register (void)
654 plugin_register_complex_config ("write_tsdb", wt_config);
657 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */