]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
value_list: Migrate `parse_values` out of `common`.
authorFlorian Forster <octo@collectd.org>
Mon, 18 Dec 2023 08:38:08 +0000 (09:38 +0100)
committerFlorian Forster <octo@collectd.org>
Mon, 18 Dec 2023 22:29:47 +0000 (23:29 +0100)
Makefile.am
src/daemon/plugin_mock.c
src/utils/common/common.c
src/utils/common/common.h
src/utils/common/common_test.c
src/utils/value_list/value_list.c
src/utils/value_list/value_list.h
src/utils/value_list/value_list_test.c [new file with mode: 0644]

index 2fe922908a641821311d2770a0e08977f680a362..19ed9a253dec85a4b7cbef738b6ee308418df6ff 100644 (file)
@@ -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
index 180c919358bd02d44bef288c4c3be9b3b82e0c8d..4fe205cc23e41e32eab1e271b33fe60586315aae 100644 (file)
@@ -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;
 }
index 46c4d1179f1300fa7f7a42a42a0a956877fcace1..34258f09ef7f8a31d6cd32e1926928a533bb5356 100644 (file)
@@ -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];
index 6b9f8d79ddfbb7c9908a2a5b3a284d4a1ed5b79e..51b4032c0e7bae10237beb5cbb7fbc61fa29c19e 100644 (file)
@@ -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
index f2fd190e4687e518936e9c2609fc8102aaf107f8..d4a667d4c141546e24381469a06127fe1a8a7e69 100644 (file)
@@ -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);
 
index ac49f81c7acdf1d6ffcf1b7a6f3d81cf5aff8fb7..772328a436e4f9ddf71fccd1a30354ec59a04bd9 100644 (file)
@@ -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 */
index 13d69dc7734b97af37b71db913592d55a23e94ec..2b95fb8197f2389ffa02879daff4f44676bf8ee8 100644 (file)
@@ -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 (file)
index 0000000..0168905
--- /dev/null
@@ -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 <octo at collectd.org>
+ */
+
+#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;
+}