From: Florian Forster Date: Mon, 18 Dec 2023 08:38:08 +0000 (+0100) Subject: value_list: Migrate `parse_values` out of `common`. X-Git-Tag: 6.0.0-rc0~33^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6817f227007e74b76ae0bd9153e21370079b9de9;p=thirdparty%2Fcollectd.git value_list: Migrate `parse_values` out of `common`. --- diff --git a/Makefile.am b/Makefile.am index 2fe922908..19ed9a253 100644 --- a/Makefile.am +++ b/Makefile.am @@ -384,7 +384,8 @@ test_utils_message_parser_SOURCES = \ src/utils/tail/tail.c src/utils/tail/tail.h \ src/utils/match/match.c src/utils/match/match.h \ src/utils/latency/latency.c src/utils/latency/latency.h \ - src/utils/latency/latency_config.c src/utils/latency/latency_config.h + src/utils/latency/latency_config.c src/utils/latency/latency_config.h \ + src/utils/value_list/value_list.c src/utils/value_list/value_list.h test_utils_message_parser_CPPFLAGS = $(AM_CPPFLAGS) test_utils_message_parser_LDADD = liboconfig.la libplugin_mock.la -lm @@ -497,7 +498,9 @@ test_format_json_LDADD = \ endif if BUILD_PLUGIN_CEPH -test_plugin_ceph_SOURCES = src/ceph_test.c +test_plugin_ceph_SOURCES = src/ceph_test.c \ + src/utils/value_list/value_list.c \ + src/utils/value_list/value_list.h test_plugin_ceph_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBYAJL_CPPFLAGS) test_plugin_ceph_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBYAJL_LDFLAGS) test_plugin_ceph_LDADD = libplugin_mock.la $(BUILD_WITH_LIBYAJL_LIBS) @@ -544,6 +547,8 @@ libcmds_la_LIBADD = -lm test_utils_cmds_SOURCES = \ src/utils/cmds/cmds_test.c \ + src/utils/value_list/value_list.c \ + src/utils/value_list/value_list.h \ src/testing.h test_utils_cmds_LDADD = \ libcmds.la \ @@ -937,16 +942,20 @@ pkglib_LTLIBRARIES += curl_json.la curl_json_la_SOURCES = \ src/curl_json.c \ src/utils/curl_stats/curl_stats.c \ - src/utils/curl_stats/curl_stats.h + src/utils/curl_stats/curl_stats.h \ + src/utils/value_list/value_list.c \ + src/utils/value_list/value_list.h curl_json_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBCURL_CFLAGS) curl_json_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBYAJL_CPPFLAGS) curl_json_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBYAJL_LDFLAGS) curl_json_la_LIBADD = $(BUILD_WITH_LIBCURL_LIBS) $(BUILD_WITH_LIBYAJL_LIBS) test_plugin_curl_json_SOURCES = src/curl_json_test.c \ - src/utils/curl_stats/curl_stats.c \ src/daemon/configfile.c \ - src/daemon/types_list.c + src/daemon/types_list.c \ + src/utils/curl_stats/curl_stats.c \ + src/utils/value_list/value_list.c \ + src/utils/value_list/value_list.h test_plugin_curl_json_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBYAJL_CPPFLAGS) test_plugin_curl_json_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBYAJL_LDFLAGS) test_plugin_curl_json_LDADD = libavltree.la liboconfig.la libplugin_mock.la $(BUILD_WITH_LIBCURL_LIBS) $(BUILD_WITH_LIBYAJL_LIBS) @@ -1340,12 +1349,14 @@ logparser_la_CPPFLAGS = $(AM_CPPFLAGS) logparser_la_LDFLAGS = $(PLUGIN_LDFLAGS) -lm test_plugin_logparser_SOURCES = src/logparser_test.c \ + src/daemon/configfile.c \ + src/daemon/types_list.c \ + src/utils/match/match.c src/utils/match/match.h \ src/utils/message_parser/message_parser.c \ src/utils_tail_match.c src/utils_tail_match.h \ src/utils/tail/tail.c src/utils/tail/tail.h \ - src/utils/match/match.c src/utils/match/match.h \ - src/daemon/configfile.c \ - src/daemon/types_list.c + src/utils/value_list/value_list.c \ + src/utils/value_list/value_list.h test_plugin_logparser_CPPFLAGS = $(AM_CPPFLAGS) test_plugin_logparser_LDFLAGS = $(PLUGIN_LDFLAGS) test_plugin_logparser_LDADD = liboconfig.la libplugin_mock.la liblatency.la diff --git a/src/daemon/plugin_mock.c b/src/daemon/plugin_mock.c index 180c91935..4fe205cc2 100644 --- a/src/daemon/plugin_mock.c +++ b/src/daemon/plugin_mock.c @@ -119,8 +119,6 @@ DECLARE_UNREGISTER(data_set) DECLARE_UNREGISTER(log) DECLARE_UNREGISTER(notification) -int plugin_dispatch_values(value_list_t const *vl) { return ENOTSUP; } - int plugin_dispatch_metric_family(metric_family_t const *fam) { return ENOTSUP; } diff --git a/src/utils/common/common.c b/src/utils/common/common.c index 46c4d1179..34258f09e 100644 --- a/src/utils/common/common.c +++ b/src/utils/common/common.c @@ -999,62 +999,6 @@ int parse_value(const char *value_orig, value_t *ret_value, int ds_type) { return 0; } /* int parse_value */ -int parse_values(char *buffer, value_list_t *vl, const data_set_t *ds) { - size_t i; - char *dummy; - char *ptr; - char *saveptr; - - if ((buffer == NULL) || (vl == NULL) || (ds == NULL)) - return EINVAL; - - i = 0; - dummy = buffer; - saveptr = NULL; - vl->time = 0; - while ((ptr = strtok_r(dummy, ":", &saveptr)) != NULL) { - dummy = NULL; - - if (i >= vl->values_len) { - /* Make sure i is invalid. */ - i = 0; - break; - } - - if (vl->time == 0) { - if (strcmp("N", ptr) == 0) - vl->time = cdtime(); - else { - char *endptr = NULL; - double tmp; - - errno = 0; - tmp = strtod(ptr, &endptr); - if ((errno != 0) /* Overflow */ - || (endptr == ptr) /* Invalid string */ - || (endptr == NULL) /* This should not happen */ - || (*endptr != 0)) /* Trailing chars */ - return -1; - - vl->time = DOUBLE_TO_CDTIME_T(tmp); - } - - continue; - } - - if ((strcmp("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE)) - vl->values[i].gauge = NAN; - else if (0 != parse_value(ptr, &vl->values[i], ds->ds[i].type)) - return -1; - - i++; - } /* while (strtok_r) */ - - if ((ptr != NULL) || (i == 0)) - return -1; - return 0; -} /* int parse_values */ - int parse_value_file(char const *path, value_t *ret_value, int ds_type) { FILE *fh; char buffer[256]; diff --git a/src/utils/common/common.h b/src/utils/common/common.h index 6b9f8d79d..51b4032c0 100644 --- a/src/utils/common/common.h +++ b/src/utils/common/common.h @@ -331,7 +331,6 @@ int format_name(char *ret, int ret_len, const char *hostname, int format_values(strbuf_t *buf, metric_t const *m, bool store_rates); 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); /* parse_value_file reads "path" and parses its content as an integer or * floating point, depending on "ds_type". On success, the value is stored in diff --git a/src/utils/common/common_test.c b/src/utils/common/common_test.c index f2fd190e4..d4a667d4c 100644 --- a/src/utils/common/common_test.c +++ b/src/utils/common/common_test.c @@ -272,56 +272,6 @@ DEF_TEST(strunescape) { return 0; } -DEF_TEST(parse_values) { - struct { - char buffer[64]; - int status; - gauge_t value; - } cases[] = { - {"1435044576:42", 0, 42.0}, {"1435044576:42:23", -1, NAN}, - {"1435044576:U", 0, NAN}, {"N:12.3", 0, 12.3}, - {"N:42.0:23", -1, NAN}, {"N:U", 0, NAN}, - {"T:42.0", -1, NAN}, - }; - - for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) { - data_source_t dsrc = { - .name = "value", - .type = DS_TYPE_GAUGE, - .min = 0.0, - .max = NAN, - }; - data_set_t ds = { - .type = "example", - .ds_num = 1, - .ds = &dsrc, - }; - - value_t v = { - .gauge = NAN, - }; - value_list_t vl = { - .values = &v, - .values_len = 1, - .time = 0, - .interval = 0, - .host = "example.com", - .plugin = "common_test", - .type = "example", - .meta = NULL, - }; - - int status = parse_values(cases[i].buffer, &vl, &ds); - EXPECT_EQ_INT(cases[i].status, status); - if (status != 0) - continue; - - EXPECT_EQ_DOUBLE(cases[i].value, vl.values[0].gauge); - } - - return 0; -} - DEF_TEST(value_to_rate) { struct { time_t t0; @@ -419,7 +369,6 @@ int main(void) { RUN_TEST(escape_slashes); RUN_TEST(escape_string); RUN_TEST(strunescape); - RUN_TEST(parse_values); RUN_TEST(value_to_rate); RUN_TEST(format_values); diff --git a/src/utils/value_list/value_list.c b/src/utils/value_list/value_list.c index ac49f81c7..772328a43 100644 --- a/src/utils/value_list/value_list.c +++ b/src/utils/value_list/value_list.c @@ -428,3 +428,59 @@ plugin_value_list_to_metric_family(value_list_t const *vl, data_set_t const *ds, metric_reset(&m); return fam; } + +int parse_values(char *buffer, value_list_t *vl, const data_set_t *ds) { + size_t i; + char *dummy; + char *ptr; + char *saveptr; + + if ((buffer == NULL) || (vl == NULL) || (ds == NULL)) + return EINVAL; + + i = 0; + dummy = buffer; + saveptr = NULL; + vl->time = 0; + while ((ptr = strtok_r(dummy, ":", &saveptr)) != NULL) { + dummy = NULL; + + if (i >= vl->values_len) { + /* Make sure i is invalid. */ + i = 0; + break; + } + + if (vl->time == 0) { + if (strcmp("N", ptr) == 0) + vl->time = cdtime(); + else { + char *endptr = NULL; + double tmp; + + errno = 0; + tmp = strtod(ptr, &endptr); + if ((errno != 0) /* Overflow */ + || (endptr == ptr) /* Invalid string */ + || (endptr == NULL) /* This should not happen */ + || (*endptr != 0)) /* Trailing chars */ + return -1; + + vl->time = DOUBLE_TO_CDTIME_T(tmp); + } + + continue; + } + + if ((strcmp("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE)) + vl->values[i].gauge = NAN; + else if (0 != parse_value(ptr, &vl->values[i], ds->ds[i].type)) + return -1; + + i++; + } /* while (strtok_r) */ + + if ((ptr != NULL) || (i == 0)) + return -1; + return 0; +} /* int parse_values */ diff --git a/src/utils/value_list/value_list.h b/src/utils/value_list/value_list.h index 13d69dc77..2b95fb819 100644 --- a/src/utils/value_list/value_list.h +++ b/src/utils/value_list/value_list.h @@ -128,4 +128,6 @@ metric_family_t *plugin_value_list_to_metric_family(value_list_t const *vl, data_set_t const *ds, size_t index); +int parse_values(char *buffer, value_list_t *vl, const data_set_t *ds); + #endif diff --git a/src/utils/value_list/value_list_test.c b/src/utils/value_list/value_list_test.c new file mode 100644 index 000000000..016890551 --- /dev/null +++ b/src/utils/value_list/value_list_test.c @@ -0,0 +1,84 @@ +/** + * collectd - src/utils/value_list/value_list_test.c + * Copyright (C) 2013-2023 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +#include "utils/value_list/value_list.h" +#include "testing.h" + +DEF_TEST(parse_values) { + struct { + char buffer[64]; + int status; + gauge_t value; + } cases[] = { + {"1435044576:42", 0, 42.0}, {"1435044576:42:23", -1, NAN}, + {"1435044576:U", 0, NAN}, {"N:12.3", 0, 12.3}, + {"N:42.0:23", -1, NAN}, {"N:U", 0, NAN}, + {"T:42.0", -1, NAN}, + }; + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) { + data_source_t dsrc = { + .name = "value", + .type = DS_TYPE_GAUGE, + .min = 0.0, + .max = NAN, + }; + data_set_t ds = { + .type = "example", + .ds_num = 1, + .ds = &dsrc, + }; + + value_t v = { + .gauge = NAN, + }; + value_list_t vl = { + .values = &v, + .values_len = 1, + .time = 0, + .interval = 0, + .host = "example.com", + .plugin = "common_test", + .type = "example", + .meta = NULL, + }; + + int status = parse_values(cases[i].buffer, &vl, &ds); + EXPECT_EQ_INT(cases[i].status, status); + if (status != 0) + continue; + + EXPECT_EQ_DOUBLE(cases[i].value, vl.values[0].gauge); + } + + return 0; +} + +int main(void) { + RUN_TEST(parse_values); + + END_TEST; +}