]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
common: Implement parse_legacy_identifier().
authorFlorian Forster <octo@google.com>
Thu, 30 Jul 2020 16:31:19 +0000 (18:31 +0200)
committerFlorian Forster <octo@google.com>
Mon, 10 Aug 2020 06:48:03 +0000 (08:48 +0200)
src/utils/common/common.c
src/utils/common/common.h

index 3b1d5dd5e34279ac99ee79dc8d5a888feb3f4d3f..a4597d8536ca07b32411320b83fda6694811a710 100644 (file)
@@ -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;
index 16cfd076e6f6d832d8027fb416c630d592c97912..d7fd83e0508c265f80c00eb6f61a55b6279afa28 100644 (file)
@@ -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);