From: Florian Forster Date: Thu, 30 Jul 2020 16:21:06 +0000 (+0200) Subject: common: Add a "data_source" return argument to parse_identifier_vl(). X-Git-Tag: 6.0.0-rc0~144^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34ebb31f10b5a17726335a4ced5816671cf1df01;p=thirdparty%2Fcollectd.git common: Add a "data_source" return argument to parse_identifier_vl(). --- diff --git a/src/grpc.cc b/src/grpc.cc index d07acc82c..7161589d0 100644 --- a/src/grpc.cc +++ b/src/grpc.cc @@ -456,7 +456,7 @@ private: char *name = NULL; while (uc_iterator_next(iter, &name) == 0) { value_list_t vl; - if (parse_identifier_vl(name, &vl) != 0) { + if (parse_identifier_vl(name, &vl, NULL) != 0) { status = grpc::Status(grpc::StatusCode::INTERNAL, grpc::string("failed to parse identifier")); break; diff --git a/src/mqtt.c b/src/mqtt.c index b8a1bf32b..ff64d3c2e 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -194,7 +194,7 @@ static void on_message( topic = strdup(msg->topic); name = strip_prefix(topic); - status = parse_identifier_vl(name, &vl); + status = parse_identifier_vl(name, &vl, NULL); if (status != 0) { ERROR("mqtt plugin: Unable to parse topic \"%s\".", topic); sfree(topic); diff --git a/src/utils/cmds/putval.c b/src/utils/cmds/putval.c index 1a6d114de..76078676c 100644 --- a/src/utils/cmds/putval.c +++ b/src/utils/cmds/putval.c @@ -95,7 +95,7 @@ cmd_status_t cmd_parse_putval(size_t argc, char **argv, } char const *identifier = argv[0]; - int status = parse_identifier_vl(identifier, &vl); + int status = parse_identifier_vl(identifier, &vl, NULL); if (status != 0) { DEBUG("cmd_handle_putval: Cannot parse identifier `%s'.", identifier); cmd_error(CMD_PARSE_ERROR, errhndl, "parse_identifier_vl(\"%s\"): %s", diff --git a/src/utils/common/common.c b/src/utils/common/common.c index 7562a243a..3b1d5dd5e 100644 --- a/src/utils/common/common.c +++ b/src/utils/common/common.c @@ -972,8 +972,8 @@ int parse_identifier(char *str, char **ret_host, char **ret_plugin, return 0; } /* int parse_identifier */ -int parse_identifier_vl(const char *str, value_list_t *vl) /* {{{ */ -{ +int parse_identifier_vl(const char *str, value_list_t *vl, + char **ret_data_source) { if ((str == NULL) || (vl == NULL)) return EINVAL; @@ -995,6 +995,13 @@ int parse_identifier_vl(const char *str, value_list_t *vl) /* {{{ */ return status; } + if (data_source != NULL) { + if (ret_data_source == NULL) { + return EINVAL; + } + *ret_data_source = strdup(data_source); + } + char *plugin_instance = strchr(plugin, '-'); if (plugin_instance != NULL) { *plugin_instance = 0; diff --git a/src/utils/common/common.h b/src/utils/common/common.h index 1cb0df764..16cfd076e 100644 --- a/src/utils/common/common.h +++ b/src/utils/common/common.h @@ -328,7 +328,20 @@ int format_values(strbuf_t *buf, metric_t const *m, bool store_rates); int parse_identifier(char *str, char **ret_host, char **ret_plugin, char **ret_type, char **ret_data_source, char *default_host); -int parse_identifier_vl(const char *str, value_list_t *vl); + +/* parse_identifier_vl parses an identifier in the form + * "host/plugin[-inst]/type[-inst]/data_source" and stores the fields in a + * value_list_t. If vl->host is not empty, then it is used as the default value + * if a host name is omitted, i.e. the "plugin/type" format is accepted. If + * ret_data_source is not NULL, a four-part identifier is accepted and a + * pointer to the data source name is (optionally) stored and needs to be freed + * by the caller. If the provided format does not fit the provided arguments, + * e.g. a two-part format but no default host provided, or a four-part format + * but no ret_data_source pointer, then EINVAL is returned. + */ +int parse_identifier_vl(const char *str, value_list_t *vl, + char **ret_data_source); + 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);