* <Node>
* Host "localhost"
* Port "4242"
- * Prefix "sys"
* HostTags "status=production deviceclass=www"
* </Node>
* </Plugin>
#include "configfile.h"
#include "utils_cache.h"
-#include "utils_parse_option.h"
#include <pthread.h>
-#include <sys/socket.h>
#include <netdb.h>
#ifndef WT_DEFAULT_NODE
char *node;
char *service;
- char *prefix;
char *host_tags;
- char escape_char;
_Bool store_rates;
- _Bool separate_instances;
_Bool always_append_ds;
char send_buf[WT_SEND_BUF_SIZE];
ERROR("write_tsdb plugin: Connecting to %s:%s failed. "
"The last error was: %s", node, service,
sstrerror (errno, errbuf, sizeof(errbuf)));
- close(cb->sock_fd);
return -1;
}
sfree(cb->node);
sfree(cb->service);
- sfree(cb->prefix);
sfree(cb->host_tags);
pthread_mutex_destroy(&cb->send_lock);
} while (0)
if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
- BUFFER_ADD("%f", vl->values[ds_num].gauge);
+ BUFFER_ADD(GAUGE_FORMAT, vl->values[ds_num].gauge);
else if (store_rates)
{
if (rates == NULL)
"uc_get_rate failed.");
return -1;
}
- BUFFER_ADD("%f", rates[ds_num]);
+ BUFFER_ADD(GAUGE_FORMAT, rates[ds_num]);
}
else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
BUFFER_ADD("%llu", vl->values[ds_num].counter);
const struct wt_callback *cb,
const char *ds_name)
{
- char *prefix;
-
- prefix = cb->prefix;
- if (prefix == NULL)
- prefix = "";
+ int status;
+ char *temp = NULL;
+ char *prefix = "";
+ const char *meta_prefix = "tsdb_prefix";
+
+ if (vl->meta) {
+ status = meta_data_get_string(vl->meta, meta_prefix, &temp);
+ if (status == -ENOENT) {
+ /* defaults to empty string */
+ } else if (status < 0) {
+ sfree(temp);
+ return status;
+ } else {
+ prefix = temp;
+ }
+ }
if (ds_name != NULL) {
if (vl->plugin_instance[0] == '\0') {
- ssnprintf(ret, ret_len, "%s.%s.%s",
- prefix, vl->plugin, ds_name);
- } else if (vl->type_instance == '\0') {
- ssnprintf(ret, ret_len, "%s.%s.%s.%s.%s",
- prefix, vl->plugin, vl->plugin_instance,
- vl->type_instance, ds_name);
- } else {
- ssnprintf(ret, ret_len, "%s.%s.%s.%s.%s",
- prefix, vl->plugin, vl->plugin_instance, vl->type,
- ds_name);
+ if (vl->type_instance[0] == '\0') {
+ ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
+ vl->type, ds_name);
+ } else {
+ ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
+ vl->type, vl->type_instance, ds_name);
+ }
+ } else { /* vl->plugin_instance != "" */
+ if (vl->type_instance[0] == '\0') {
+ ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
+ vl->plugin_instance, vl->type, ds_name);
+ } else {
+ ssnprintf(ret, ret_len, "%s%s.%s.%s.%s.%s", prefix,
+ vl->plugin, vl->plugin_instance, vl->type,
+ vl->type_instance, ds_name);
+ }
+ }
+ } else { /* ds_name == NULL */
+ if (vl->plugin_instance[0] == '\0') {
+ if (vl->type_instance[0] == '\0') {
+ ssnprintf(ret, ret_len, "%s%s.%s", prefix, vl->plugin,
+ vl->type);
+ } else {
+ ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
+ vl->type_instance, vl->type);
+ }
+ } else { /* vl->plugin_instance != "" */
+ if (vl->type_instance[0] == '\0') {
+ ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
+ vl->plugin_instance, vl->type);
+ } else {
+ ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
+ vl->plugin_instance, vl->type, vl->type_instance);
+ }
}
- } else if (vl->plugin_instance[0] == '\0') {
- if (vl->type_instance[0] == '\0')
- ssnprintf(ret, ret_len, "%s.%s.%s",
- prefix, vl->plugin, vl->type);
- else
- ssnprintf(ret, ret_len, "%s.%s.%s",
- prefix, vl->plugin, vl->type_instance);
- } else if (vl->type_instance[0] == '\0') {
- ssnprintf(ret, ret_len, "%s.%s.%s.%s",
- prefix, vl->plugin, vl->plugin_instance, vl->type);
- } else {
- ssnprintf(ret, ret_len, "%s.%s.%s.%s",
- prefix, vl->plugin, vl->plugin_instance, vl->type_instance);
}
+ sfree(temp);
return 0;
}
static int wt_send_message (const char* key, const char* value,
cdtime_t time, struct wt_callback *cb,
- const char* host)
+ const char* host, meta_data_t *md)
{
int status;
- int message_len;
- const char *message_fmt;
+ size_t message_len;
+ char *temp = NULL;
+ char *tags = "";
char message[1024];
+ char *host_tags = cb->host_tags ? cb->host_tags : "";
+ const char *meta_tsdb = "tsdb_tags";
/* skip if value is NaN */
if (value[0] == 'n')
return 0;
- message_fmt = "put %s %u %s fqdn=%s %s\r\n";
- message_len = ssnprintf (message, sizeof(message),
- message_fmt,
- key,
- (unsigned int)CDTIME_T_TO_TIME_T(
- time),
- value,
- host,
- cb->host_tags);
+ if (md) {
+ status = meta_data_get_string(md, meta_tsdb, &temp);
+ if (status == -ENOENT) {
+ /* defaults to empty string */
+ } else if (status < 0) {
+ ERROR("write_tsdb plugin: tags metadata get failure");
+ sfree(temp);
+ pthread_mutex_unlock(&cb->send_lock);
+ return status;
+ } else {
+ tags = temp;
+ }
+ }
+
+ status = ssnprintf (message,
+ sizeof(message),
+ "put %s %.0f %s fqdn=%s %s %s\r\n",
+ key,
+ CDTIME_T_TO_DOUBLE(time),
+ value,
+ host,
+ tags,
+ host_tags);
+ sfree(temp);
+ if (status < 0)
+ return -1;
+ message_len = (size_t) status;
+
if (message_len >= sizeof(message)) {
ERROR("write_tsdb plugin: message buffer too small: "
- "Need %d bytes.", message_len + 1);
+ "Need %zu bytes.", message_len + 1);
return -1;
}
char key[10*DATA_MAX_NAME_LEN];
char values[512];
- int status, i;
+ int status;
+ size_t i;
if (0 != strcmp(ds->type, vl->type))
{
}
/* Send the message to tsdb */
- status = wt_send_message(key, values, vl->time, cb, vl->host);
+ status = wt_send_message(key, values, vl->time, cb, vl->host, vl->meta);
if (status != 0)
{
ERROR("write_tsdb plugin: error with "
return status;
}
-static int config_set_char(char *dest,
- oconfig_item_t *ci)
-{
- char buffer[4];
- int status;
-
- memset(buffer, 0, sizeof(buffer));
-
- status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
- if (status != 0)
- return (status);
-
- if (buffer[0] == 0)
- {
- ERROR("write_tsdb plugin: Cannot use an empty string for the "
- "\"EscapeCharacter\" option.");
- return -1;
- }
-
- if (buffer[1] != 0)
- {
- WARNING("write_tsdb plugin: Only the first character of the "
- "\"EscapeCharacter\" option ('%c') will be used.",
- (int) buffer[0]);
- }
-
- *dest = buffer[0];
-
- return 0;
-}
-
static int wt_config_tsd(oconfig_item_t *ci)
{
struct wt_callback *cb;
cb->sock_fd = -1;
cb->node = NULL;
cb->service = NULL;
- cb->prefix = NULL;
cb->host_tags = NULL;
- cb->escape_char = WT_DEFAULT_ESCAPE;
- cb->store_rates = 1;
+ cb->store_rates = 0;
pthread_mutex_init (&cb->send_lock, NULL);
cf_util_get_string(child, &cb->node);
else if (strcasecmp("Port", child->key) == 0)
cf_util_get_service(child, &cb->service);
- else if (strcasecmp("Prefix", child->key) == 0)
- cf_util_get_string(child, &cb->prefix);
else if (strcasecmp("HostTags", child->key) == 0)
cf_util_get_string(child, &cb->host_tags);
else if (strcasecmp("StoreRates", child->key) == 0)
cf_util_get_boolean(child, &cb->store_rates);
- else if (strcasecmp("SeparateInstances", child->key) == 0)
- cf_util_get_boolean(child, &cb->separate_instances);
else if (strcasecmp("AlwaysAppendDS", child->key) == 0)
cf_util_get_boolean(child, &cb->always_append_ds);
- else if (strcasecmp("EscapeCharacter", child->key) == 0)
- config_set_char(&cb->escape_char, child);
else
{
ERROR("write_tsdb plugin: Invalid configuration "