From: Florian Forster Date: Thu, 30 Jul 2020 16:31:19 +0000 (+0200) Subject: common: Implement parse_legacy_identifier(). X-Git-Tag: 6.0.0-rc0~144^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a512439629eb34fa6bc2d560681138e6bf85d8d;p=thirdparty%2Fcollectd.git common: Implement parse_legacy_identifier(). --- diff --git a/src/utils/common/common.c b/src/utils/common/common.c index 3b1d5dd5e..a4597d853 100644 --- a/src/utils/common/common.c +++ b/src/utils/common/common.c @@ -1028,6 +1028,65 @@ int parse_identifier_vl(const char *str, value_list_t *vl, return 0; } /* }}} int parse_identifier_vl */ +metric_t *parse_legacy_identifier(char const *s) { + value_list_t vl = VALUE_LIST_INIT; + + char *data_source = NULL; + int status = parse_identifier_vl(s, &vl, &data_source); + if (status != 0) { + errno = status; + return NULL; + } + + data_set_t const *ds = plugin_get_ds(vl.type); + if (ds == NULL) { + errno = ENOENT; + return NULL; + } + + if ((ds->ds_num != 1) && (data_source == NULL)) { + DEBUG("parse_legacy_identifier: data set \"%s\" has multiple data sources, " + "but \"%s\" does not specify a data source", + ds->type, s); + errno = EINVAL; + return NULL; + } + + value_t values[ds->ds_num]; + memset(values, 0, sizeof(values)); + vl.values = values; + vl.values_len = ds->ds_num; + + size_t ds_index = 0; + if (data_source != NULL) { + bool found = 0; + for (size_t i = 0; i < ds->ds_num; i++) { + if (strcasecmp(data_source, ds->ds[i].name) == 0) { + ds_index = i; + found = true; + } + } + + if (!found) { + DEBUG("parse_legacy_identifier: data set \"%s\" does not have a \"%s\" " + "data source", + ds->type, data_source); + free(data_source); + errno = EINVAL; + return NULL; + } + } + free(data_source); + data_source = NULL; + + metric_family_t *fam = plugin_value_list_to_metric_family(&vl, ds, ds_index); + if (fam == NULL) { + return NULL; + } + + return fam->metric.ptr; +} + int parse_value(const char *value_orig, value_t *ret_value, int ds_type) { char *value; char *endptr = NULL; diff --git a/src/utils/common/common.h b/src/utils/common/common.h index 16cfd076e..d7fd83e05 100644 --- a/src/utils/common/common.h +++ b/src/utils/common/common.h @@ -342,6 +342,10 @@ int parse_identifier(char *str, char **ret_host, char **ret_plugin, int parse_identifier_vl(const char *str, value_list_t *vl, char **ret_data_source); +/* parse_legacy_identifier parses a legacy identifier in the form + * "host/plugin/type" and converts it to a metric_t. */ +metric_t *parse_legacy_identifier(char const *s); + int parse_value(const char *value, value_t *ret_value, int ds_type); int parse_values(char *buffer, value_list_t *vl, const data_set_t *ds);