X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fdaemon%2Fmeta_data.c;h=583d81966d157587ab3c63a708360d9afef53286;hb=98e38add333bfcb893fcde408ecf3a0c43d99e31;hp=18c4e220c9c1d04f9452d0660353277b02e99eff;hpb=79963d13c1884d1d92667cc502ad20758b084a12;p=collectd.git diff --git a/src/daemon/meta_data.c b/src/daemon/meta_data.c index 18c4e220..583d8196 100644 --- a/src/daemon/meta_data.c +++ b/src/daemon/meta_data.c @@ -26,9 +26,12 @@ #include "collectd.h" +#include "common.h" #include "meta_data.h" #include "plugin.h" +#define MD_MAX_NONSTRING_CHARS 128 + /* * Data types */ @@ -684,4 +687,64 @@ int meta_data_get_boolean(meta_data_t *md, /* {{{ */ return (0); } /* }}} int meta_data_get_boolean */ +int meta_data_as_string(meta_data_t *md, /* {{{ */ + const char *key, char **value) { + meta_entry_t *e; + char *actual; + char buffer[MD_MAX_NONSTRING_CHARS]; /* For non-string types. */ + char *temp; + int type; + + if ((md == NULL) || (key == NULL) || (value == NULL)) + return (-EINVAL); + + pthread_mutex_lock(&md->lock); + + e = md_entry_lookup(md, key); + if (e == NULL) { + pthread_mutex_unlock(&md->lock); + return (-ENOENT); + } + + type = e->type; + + switch (type) { + case MD_TYPE_STRING: + actual = e->value.mv_string; + break; + case MD_TYPE_SIGNED_INT: + ssnprintf(buffer, sizeof(buffer), "%" PRIi64, e->value.mv_signed_int); + actual = buffer; + break; + case MD_TYPE_UNSIGNED_INT: + ssnprintf(buffer, sizeof(buffer), "%" PRIu64, e->value.mv_unsigned_int); + actual = buffer; + break; + case MD_TYPE_DOUBLE: + ssnprintf(buffer, sizeof(buffer), GAUGE_FORMAT, e->value.mv_double); + actual = buffer; + break; + case MD_TYPE_BOOLEAN: + actual = e->value.mv_boolean ? "true" : "false"; + break; + default: + pthread_mutex_unlock(&md->lock); + ERROR("meta_data_as_string: unknown type %d for key `%s'", type, key); + return (-ENOENT); + } + + pthread_mutex_unlock(&md->lock); + + temp = md_strdup(actual); + if (temp == NULL) { + pthread_mutex_unlock(&md->lock); + ERROR("meta_data_as_string: md_strdup failed for key `%s'.", key); + return (-ENOMEM); + } + + *value = temp; + + return (0); +} /* }}} int meta_data_as_string */ + /* vim: set sw=2 sts=2 et fdm=marker : */