]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
cmds: Use metric_t internally
authorFlorian Forster <octo@google.com>
Fri, 17 Jul 2020 10:26:16 +0000 (12:26 +0200)
committerFlorian Forster <octo@google.com>
Wed, 29 Jul 2020 11:36:39 +0000 (13:36 +0200)
This is a WIP

This commit is where the major transform takes palce; the internal
representation is changed, and the read plugins use that indirectly
when they submit a value list transparently.

The baromerter read plugin has been converted to using metric_t
representation natively.

The write plugins have to be adapted to use these new single data
source metrics.

Signed-off-by: Manoj Srivastava <srivasta@google.com>
Change-Id: I9bdeaa59de5c58d11c0ae0e7b55d68d9e4d77fa1

# Conflicts:
# src/daemon/metrics_list_test.c

src/utils/cmds/cmds.h
src/utils/cmds/flush.c
src/utils/cmds/getthreshold.c
src/utils/cmds/getval.c
src/utils/cmds/putval.c
src/utils/cmds/putval.h

index f3882f544feaf3bef9c071cc2ae401fde3055a79..21539d1ae8a4b6457171c78e25a25912ec8dee3d 100644 (file)
@@ -65,10 +65,9 @@ typedef struct {
   /* The raw identifier as provided by the user. */
   char *raw_identifier;
 
-  /* An array of the fully parsed identifier and all value lists, and their
+  /* An array of the fully parsed identifier and all metrics, and their
    * options as provided by the user. */
-  value_list_t *vl;
-  size_t vl_num;
+  metrics_list_t *ml;
 } cmd_putval_t;
 
 /*
index d080d48119c1bde80b855f8ef0ec253a34113464..0259954519ac60140ffdb0c9761caad14ff01fef 100644 (file)
@@ -73,7 +73,7 @@ cmd_status_t cmd_parse_flush(size_t argc, char **argv, cmd_flush_t *ret_flush,
       id = ret_flush->identifiers + ret_flush->identifiers_num;
       ret_flush->identifiers_num++;
       if (parse_identifier(opt_value, &id->host, &id->plugin,
-                           &id->plugin_instance, &id->type, &id->type_instance,
+                           &id->type, &id->data_source,
                            opts->identifier_default_host) != 0) {
         cmd_error(CMD_PARSE_ERROR, err, "Invalid identifier `%s'.", opt_value);
         cmd_destroy_flush(ret_flush);
@@ -140,9 +140,11 @@ cmd_status_t cmd_handle_flush(FILE *fh, char *buffer) {
 
       if (cmd.cmd.flush.identifiers_num != 0) {
         identifier_t *id = cmd.cmd.flush.identifiers + j;
-        if (format_name(buf, sizeof(buf), id->host, id->plugin,
-                        id->plugin_instance, id->type,
-                        id->type_instance) != 0) {
+       if (snprintf(
+               buf, sizeof(buf), "%s/%s/%s/%s", (id->host == NULL) ? "" : id->host,
+               (id->plugin == NULL) ? "" : id->plugin,
+               (id->type == NULL) ? "" : id->type,
+               (id->data_source == NULL) ? "" : id->data_source) > sizeof(buf)) {
           error++;
           continue;
         }
index 060cbd8655c18cd2365d9cf9c92a31e4593b0dac..73ce83bf4f9189ea3f6485fa2249bf754e366a86 100644 (file)
   }
 
 int handle_getthreshold(FILE *fh, char *buffer) {
-  char *command;
-  char *identifier;
-  char *identifier_copy;
+  char *command = NULL;
+  char *identifier = NULL;
+  char *identifier_copy = NULL;
 
-  char *host;
-  char *plugin;
-  char *plugin_instance;
-  char *type;
-  char *type_instance;
+  char *host = NULL;
+  char *plugin = NULL;
+  char *type = NULL;
+  char *data_source = NULL;
 
   threshold_t threshold;
 
@@ -63,7 +62,6 @@ int handle_getthreshold(FILE *fh, char *buffer) {
   DEBUG("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);",
         (void *)fh, buffer);
 
-  command = NULL;
   status = parse_string(&buffer, &command);
   if (status != 0) {
     print_to_socket(fh, "-1 Cannot parse command.\n");
@@ -76,7 +74,6 @@ int handle_getthreshold(FILE *fh, char *buffer) {
     return -1;
   }
 
-  identifier = NULL;
   status = parse_string(&buffer, &identifier);
   if (status != 0) {
     print_to_socket(fh, "-1 Cannot parse identifier.\n");
@@ -93,8 +90,8 @@ int handle_getthreshold(FILE *fh, char *buffer) {
    * returning pointers into it */
   identifier_copy = sstrdup(identifier);
 
-  status = parse_identifier(identifier_copy, &host, &plugin, &plugin_instance,
-                            &type, &type_instance,
+  status = parse_identifier(identifier_copy, &host, &plugin,
+                            &type, &data_source,
                             /* default_host = */ NULL);
   if (status != 0) {
     DEBUG("handle_getthreshold: Cannot parse identifier `%s'.", identifier);
@@ -103,18 +100,15 @@ int handle_getthreshold(FILE *fh, char *buffer) {
     return -1;
   }
 
-  value_list_t vl = {.values = NULL};
-  sstrncpy(vl.host, host, sizeof(vl.host));
-  sstrncpy(vl.plugin, plugin, sizeof(vl.plugin));
-  if (plugin_instance != NULL)
-    sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
-  sstrncpy(vl.type, type, sizeof(vl.type));
-  if (type_instance != NULL)
-    sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
+  metric_t metric = METRIC_STRUCT_INIT;
+  metric.identity = create_identity(plugin, type, data_source, host);
+  sstrncpy(metric.plugin, plugin, sizeof(metric.plugin));
+  sstrncpy(metric.type, type, sizeof(metric.type));
+  metric.ds = plugin_get_ds(metric.type);
+
   sfree(identifier_copy);
 
-  /* TODO(octo): pass a metric_t* to ut_search_threshold(). */
-  status = ut_search_threshold(NULL, &threshold);
+  status = ut_search_threshold(&metric, &threshold);
   if (status == ENOENT) {
     print_to_socket(fh, "-1 No threshold found for identifier %s\n",
                     identifier);
@@ -130,12 +124,8 @@ int handle_getthreshold(FILE *fh, char *buffer) {
     i++;
   if (threshold.plugin[0] != 0)
     i++;
-  if (threshold.plugin_instance[0] != 0)
-    i++;
   if (threshold.type[0] != 0)
     i++;
-  if (threshold.type_instance[0] != 0)
-    i++;
   if (threshold.data_source[0] != 0)
     i++;
   if (!isnan(threshold.warning_min))
@@ -158,12 +148,8 @@ int handle_getthreshold(FILE *fh, char *buffer) {
     print_to_socket(fh, "Host: %s\n", threshold.host);
   if (threshold.plugin[0] != 0)
     print_to_socket(fh, "Plugin: %s\n", threshold.plugin);
-  if (threshold.plugin_instance[0] != 0)
-    print_to_socket(fh, "Plugin Instance: %s\n", threshold.plugin_instance);
   if (threshold.type[0] != 0)
     print_to_socket(fh, "Type: %s\n", threshold.type);
-  if (threshold.type_instance[0] != 0)
-    print_to_socket(fh, "Type Instance: %s\n", threshold.type_instance);
   if (threshold.data_source[0] != 0)
     print_to_socket(fh, "Data Source: %s\n", threshold.data_source);
   if (!isnan(threshold.warning_min))
index bf8c327ecb642d7f8a8664efcb7748110d1a5396..0581ba13d9e4d6cdc8b51f05246b361bd7caee31 100644 (file)
@@ -61,8 +61,8 @@ cmd_status_t cmd_parse_getval(size_t argc, char **argv,
 
   status = parse_identifier(
       argv[0], &ret_getval->identifier.host, &ret_getval->identifier.plugin,
-      &ret_getval->identifier.plugin_instance, &ret_getval->identifier.type,
-      &ret_getval->identifier.type_instance, opts->identifier_default_host);
+      &ret_getval->identifier.type, &ret_getval->identifier.data_source,
+      opts->identifier_default_host);
   if (status != 0) {
     DEBUG("cmd_parse_getval: Cannot parse identifier `%s'.", identifier_copy);
     cmd_error(CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
@@ -118,33 +118,19 @@ cmd_status_t cmd_handle_getval(FILE *fh, char *buffer) {
   }
 
   gauge_t value;
-  size_t values_num = 1;
-  status =
-      uc_get_rate_by_name(cmd.cmd.getval.raw_identifier, &value);
+  status = uc_get_rate_by_name(cmd.cmd.getval.raw_identifier, &value);
   if (status != 0) {
     cmd_error(CMD_ERROR, &err, "No such value.");
     cmd_destroy(&cmd);
     return CMD_ERROR;
   }
 
-  if (ds->ds_num != values_num) {
-    ERROR("ds[%s]->ds_num = %" PRIsz ", "
-          "but uc_get_rate_by_name returned %" PRIsz " values.",
-          ds->type, ds->ds_num, values_num);
-    cmd_error(CMD_ERROR, &err, "Error reading value from cache.");
-    cmd_destroy(&cmd);
-    return CMD_ERROR;
-  }
-
-  print_to_socket(fh, "%" PRIsz " Value%s found\n", values_num,
-                  (values_num == 1) ? "" : "s");
-  for (size_t i = 0; i < values_num; i++) {
-    print_to_socket(fh, "%s=", ds->ds[i].name);
-    if (isnan(value)) {
-      print_to_socket(fh, "NaN\n");
-    } else {
-      print_to_socket(fh, "%12e\n", value);
-    }
+  print_to_socket(fh, "Value found\n");
+  print_to_socket(fh, "%s=", cmd.cmd.getval.identifier.data_source);
+  if (isnan(value)) {
+    print_to_socket(fh, "NaN\n");
+  } else {
+    print_to_socket(fh, "%12e\n", value);
   }
 
   cmd_destroy(&cmd);
index 17c538baf632c3d3a59bd13867ff357f95a6efee..e4ae49ae31b1e2dc1956d05b69977daeae96b275 100644 (file)
@@ -43,9 +43,9 @@ static int is_quoted(const char *str, size_t len) {
   return (str[0] == '"' && str[len - 1] == '"');
 }
 
-static int set_option(value_list_t *vl, const char *key, const char *value,
+static int set_option(metric_t *metric_p, const char *key, const char *value,
                       cmd_error_handler_t *err) {
-  if ((vl == NULL) || (key == NULL) || (value == NULL))
+  if ((metric_p == NULL) || (key == NULL) || (value == NULL))
     return -1;
 
   if (strcasecmp("interval", key) == 0) {
@@ -57,14 +57,14 @@ static int set_option(value_list_t *vl, const char *key, const char *value,
     tmp = strtod(value, &endptr);
 
     if ((errno == 0) && (endptr != NULL) && (endptr != value) && (tmp > 0.0))
-      vl->interval = DOUBLE_TO_CDTIME_T(tmp);
+      metric_p->interval = DOUBLE_TO_CDTIME_T(tmp);
   } else if (strncasecmp("meta:", key, 5) == 0) {
     const char *meta_key = key + 5;
     size_t value_len = strlen(value);
 
-    if (vl->meta == NULL) {
-      vl->meta = meta_data_create();
-      if (vl->meta == NULL) {
+    if (metric_p->meta == NULL) {
+      metric_p->meta = meta_data_create();
+      if (metric_p->meta == NULL) {
         return CMD_ERROR;
       }
     }
@@ -75,7 +75,7 @@ static int set_option(value_list_t *vl, const char *key, const char *value,
       if (value_str == NULL) {
         return CMD_ERROR;
       }
-      metadata_err = meta_data_add_string(vl->meta, meta_key, value_str);
+      metadata_err = meta_data_add_string(metric_p->meta, meta_key, value_str);
       free((void *)value_str);
       if (metadata_err != 0) {
         return CMD_ERROR;
@@ -104,15 +104,14 @@ cmd_status_t cmd_parse_putval(size_t argc, char **argv,
   char *identifier;
   char *hostname;
   char *plugin;
-  char *plugin_instance;
   char *type;
-  char *type_instance;
+  char *data_source;
   int status;
 
   char *identifier_copy;
 
   const data_set_t *ds;
-  value_list_t vl = VALUE_LIST_INIT;
+  metric_t metric = METRIC_STRUCT_INIT;
 
   if ((ret_putval == NULL) || (opts == NULL)) {
     errno = EINVAL;
@@ -132,8 +131,8 @@ cmd_status_t cmd_parse_putval(size_t argc, char **argv,
   identifier_copy = sstrdup(identifier);
 
   status =
-      parse_identifier(identifier, &hostname, &plugin, &plugin_instance, &type,
-                       &type_instance, opts->identifier_default_host);
+      parse_identifier(identifier, &hostname, &plugin, &type,
+                       &data_source, opts->identifier_default_host);
   if (status != 0) {
     DEBUG("cmd_handle_putval: Cannot parse identifier `%s'.", identifier_copy);
     cmd_error(CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
@@ -142,43 +141,42 @@ cmd_status_t cmd_parse_putval(size_t argc, char **argv,
     return CMD_PARSE_ERROR;
   }
 
-  if ((strlen(hostname) >= sizeof(vl.host)) ||
-      (strlen(plugin) >= sizeof(vl.plugin)) ||
-      ((plugin_instance != NULL) &&
-       (strlen(plugin_instance) >= sizeof(vl.plugin_instance))) ||
-      ((type_instance != NULL) &&
-       (strlen(type_instance) >= sizeof(vl.type_instance)))) {
+  if ((strlen(type) >= sizeof(metric.type)) ||
+      (strlen(plugin) >= sizeof(metric.plugin))
+      ) {
     cmd_error(CMD_PARSE_ERROR, err, "Identifier too long.");
     sfree(identifier_copy);
     return CMD_PARSE_ERROR;
   }
 
-  sstrncpy(vl.host, hostname, sizeof(vl.host));
-  sstrncpy(vl.plugin, plugin, sizeof(vl.plugin));
-  sstrncpy(vl.type, type, sizeof(vl.type));
-  if (plugin_instance != NULL)
-    sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
-  if (type_instance != NULL)
-    sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
+  metric.identity = create_identity(plugin, type, data_source, hostname);
+  if (metric.identity == NULL) {
+    sfree(identifier_copy);
+    return CMD_PARSE_ERROR;
+  }
 
-  ds = plugin_get_ds(type);
-  if (ds == NULL) {
+  sstrncpy(metric.plugin, plugin, sizeof(metric.plugin));
+  sstrncpy(metric.type, type, sizeof(metric.type));
+
+
+  metric.ds = plugin_get_ds(type);
+  if (metric.ds == NULL) {
     cmd_error(CMD_PARSE_ERROR, err, "1 Type `%s' isn't defined.", type);
+    destroy_identity(metric.identity);
     sfree(identifier_copy);
     return CMD_PARSE_ERROR;
   }
 
   hostname = NULL;
   plugin = NULL;
-  plugin_instance = NULL;
   type = NULL;
-  type_instance = NULL;
+  data_source = NULL;
 
   ret_putval->raw_identifier = identifier_copy;
   if (ret_putval->raw_identifier == NULL) {
     cmd_error(CMD_ERROR, err, "malloc failed.");
     cmd_destroy_putval(ret_putval);
-    sfree(vl.values);
+    destroy_identity(metric.identity);
     return CMD_ERROR;
   }
 
@@ -192,11 +190,9 @@ cmd_status_t cmd_parse_putval(size_t argc, char **argv,
 
     status = cmd_parse_option(argv[i], &key, &value, err);
     if (status == CMD_OK) {
-      int option_err;
-
       assert(key != NULL);
       assert(value != NULL);
-      option_err = set_option(&vl, key, value, err);
+      int option_err = set_option(&metric, key, value, err);
       if (option_err != CMD_OK && option_err != CMD_NO_OPTION) {
         result = option_err;
         break;
@@ -209,22 +205,12 @@ cmd_status_t cmd_parse_putval(size_t argc, char **argv,
       break;
     }
     /* else: cmd_parse_option did not find an option; treat this as a
-     * value list. */
+     * value. */
 
-    vl.values_len = ds->ds_num;
-    vl.values = calloc(vl.values_len, sizeof(*vl.values));
-    if (vl.values == NULL) {
-      cmd_error(CMD_ERROR, err, "malloc failed.");
-      result = CMD_ERROR;
-      break;
-    }
-
-    status = parse_values(argv[i], &vl, ds);
+    status = parse_values(argv[i], &metric);
     if (status != 0) {
       cmd_error(CMD_PARSE_ERROR, err, "Parsing the values string failed.");
       result = CMD_PARSE_ERROR;
-      vl.values_len = 0;
-      sfree(vl.values);
       break;
     }
 
@@ -234,8 +220,6 @@ cmd_status_t cmd_parse_putval(size_t argc, char **argv,
       cmd_error(CMD_ERROR, err, "realloc failed.");
       cmd_destroy_putval(ret_putval);
       result = CMD_ERROR;
-      vl.values_len = 0;
-      sfree(vl.values);
       break;
     }
 
@@ -260,15 +244,8 @@ void cmd_destroy_putval(cmd_putval_t *putval) {
     return;
 
   sfree(putval->raw_identifier);
-
-  for (size_t i = 0; i < putval->vl_num; ++i) {
-    sfree(putval->vl[i].values);
-    meta_data_destroy(putval->vl[i].meta);
-    putval->vl[i].meta = NULL;
-  }
-  sfree(putval->vl);
-  putval->vl = NULL;
-  putval->vl_num = 0;
+  destroy_metrics_list(putval->ml);
+  putval->ml = NULL;
 } /* void cmd_destroy_putval */
 
 cmd_status_t cmd_handle_putval(FILE *fh, char *buffer) {
@@ -302,7 +279,7 @@ cmd_status_t cmd_handle_putval(FILE *fh, char *buffer) {
 } /* int cmd_handle_putval */
 
 int cmd_create_putval(char *ret, size_t ret_len, /* {{{ */
-                      const data_set_t *ds, const value_list_t *vl) {
+                      const metric_t *metric_p) {
   char buffer_ident[6 * DATA_MAX_NAME_LEN];
   char buffer_values[1024];
   int status;
@@ -312,14 +289,14 @@ int cmd_create_putval(char *ret, size_t ret_len, /* {{{ */
     return status;
   escape_string(buffer_ident, sizeof(buffer_ident));
 
-  status = format_values(buffer_values, sizeof(buffer_values), ds, vl,
+  status = format_values(buffer_values, sizeof(buffer_values), metric_p,
                          /* store rates = */ false);
   if (status != 0)
     return status;
   escape_string(buffer_values, sizeof(buffer_values));
 
   snprintf(ret, ret_len, "PUTVAL %s interval=%.3f %s", buffer_ident,
-           (vl->interval > 0) ? CDTIME_T_TO_DOUBLE(vl->interval)
+           (metric_p->interval > 0) ? CDTIME_T_TO_DOUBLE(metric_p->interval)
                               : CDTIME_T_TO_DOUBLE(plugin_get_interval()),
            buffer_values);
 
index f6c3e3b87cbbb17470df53189288436b19516660..3d0b16501a9807da948f73fa21f6ae7cc4fa10e8 100644 (file)
@@ -41,7 +41,6 @@ cmd_status_t cmd_handle_putval(FILE *fh, char *buffer);
 
 void cmd_destroy_putval(cmd_putval_t *putval);
 
-int cmd_create_putval(char *ret, size_t ret_len, const data_set_t *ds,
-                      const value_list_t *vl);
+int cmd_create_putval(char *ret, size_t ret_len, const metric_t *metric_p);
 
 #endif /* UTILS_CMD_PUTVAL_H */