]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
common: Use metric_t internally
authorFlorian Forster <octo@google.com>
Fri, 17 Jul 2020 10:26:37 +0000 (12:26 +0200)
committerFlorian Forster <octo@google.com>
Wed, 29 Jul 2020 11:37:12 +0000 (13:37 +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/common/common.c
src/utils/common/common.h

index 6bb151d084a48d4e3b4ff3e8e586ce1d592232fc..000c090781a1b339b29066c722af6bd674fff8a7 100644 (file)
@@ -25,6 +25,7 @@
  *   Niki W. Waibel <niki.waibel@gmx.net>
  *   Sebastian Harl <sh at tokkee.org>
  *   Michał Mirosław <mirq-linux at rere.qmqm.pl>
+ *   Manoj Srivastava <srivasta at google.com>
  **/
 
 #include "collectd.h"
@@ -904,50 +905,66 @@ int format_name(char *ret, int ret_len, const char *hostname,
 } /* int format_name */
 
 int format_values(char *ret, size_t ret_len, /* {{{ */
-                  const data_set_t *ds, const value_list_t *vl,
-                  bool store_rates) {
-  assert(0 == strcmp(ds->type, vl->type));
-
+                  const metric_t *metric_p, bool store_rates) {
   ret[0] = 0;
   strbuf_t buf = STRBUF_CREATE_FIXED(ret, ret_len);
 
-  strbuf_printf(&buf, "%.3f", CDTIME_T_TO_DOUBLE(vl->time));
-
-  gauge_t *rates = NULL;
-  for (size_t i = 0; i < ds->ds_num; i++) {
-    if (ds->ds[i].type == DS_TYPE_GAUGE) {
-      strbuf_printf(&buf, ":" GAUGE_FORMAT, vl->values[i].gauge);
-    } else if (store_rates) {
-      if (rates == NULL)
-        rates = uc_get_rate_vl(ds, vl);
-      if (rates == NULL) {
-        WARNING("format_values: uc_get_rate_vl failed.");
-        return -1;
-      }
-      strbuf_printf(&buf, ":" GAUGE_FORMAT, rates[i]);
-    } else if (ds->ds[i].type == DS_TYPE_COUNTER)
-      strbuf_printf(&buf, ":%" PRIu64, (uint64_t)vl->values[i].counter);
-    else if (ds->ds[i].type == DS_TYPE_DERIVE)
-      strbuf_printf(&buf, ":%" PRIi64, vl->values[i].derive);
-    else {
-      ERROR("format_values: Unknown data source type: %i", ds->ds[i].type);
-      sfree(rates);
-      return -1;
+  strbuf_printf(&buf, "%.3f", CDTIME_T_TO_DOUBLE(metric_p->time));
+
+  if (metric_p->value_ds_type == DS_TYPE_GAUGE)
+    strbuf_printf(&buf, ":" GAUGE_FORMAT, metric_p->value.gauge);
+  else if (store_rates) {
+    gauge_t rates = NAN;
+    int status = uc_get_rate(metric_p, &rates);
+    if (status != 0) {
+      WARNING("format_values: uc_get_rate failed.");
+      return status;
     }
-  } /* for ds->ds_num */
+    strbuf_printf(&buf, ":" GAUGE_FORMAT, rates);
+  } else if (metric_p->value_ds_type == DS_TYPE_COUNTER)
+    strbuf_printf(&buf, ":%" PRIu64, (uint64_t)metric_p->value.counter);
+  else if (metric_p->value_ds_type == DS_TYPE_DERIVE)
+    strbuf_printf(&buf, ":%" PRIi64, metric_p->value.derive);
+  else {
+    ERROR("format_values: Unknown data source type: %i",
+          metric_p->value_ds_type);
+    return -1;
+  }
 
-  sfree(rates);
   return 0;
 } /* }}} int format_values */
 
+int format_values_vl(char *ret, size_t ret_len, /* {{{ */
+                     const data_set_t *ds, const value_list_t *vl,
+                     bool store_rates) {
+  metrics_list_t *ml = NULL;
+  assert(0 == strcmp(ds->type, vl->type));
+  int retval = plugin_convert_values_to_metrics(vl, &ml);
+  if (retval != 0) {
+    return retval;
+  }
+  metrics_list_t *index_p = ml;
+  while (index_p != NULL) {
+    retval = format_values(ret, ret_len, &index_p->metric, store_rates);
+    if (retval != 0) {
+      destroy_metrics_list(ml);
+      return retval;
+    }
+    ret[strlen(ret)] = '\n';
+    ret_len -= strlen(ret) + 1;
+    index_p = index_p->next_p;
+  }
+  destroy_metrics_list(ml);
+  return 0;
+}
+
 int parse_identifier(char *str, char **ret_host, char **ret_plugin,
-                     char **ret_plugin_instance, char **ret_type,
-                     char **ret_type_instance, char *default_host) {
+                     char **ret_type, char **ret_data_source,
+                     char *default_host) {
   char *hostname = NULL;
   char *plugin = NULL;
-  char *plugin_instance = NULL;
   char *type = NULL;
-  char *type_instance = NULL;
+  char *data_source = NULL;
 
   hostname = str;
   if (hostname == NULL)
@@ -960,35 +977,29 @@ int parse_identifier(char *str, char **ret_host, char **ret_plugin,
   plugin++;
 
   type = strchr(plugin, '/');
-  if (type == NULL) {
+  if (type == NULL)
+  return -1;
+  *type = '\0';
+  type++;
+
+  data_source = strchr(type, '/');
+  if (data_source == NULL) {
     if (default_host == NULL)
       return -1;
     /* else: no host specified; use default */
+    data_source = type;
     type = plugin;
     plugin = hostname;
     hostname = default_host;
   } else {
-    *type = '\0';
-    type++;
-  }
-
-  plugin_instance = strchr(plugin, '-');
-  if (plugin_instance != NULL) {
-    *plugin_instance = '\0';
-    plugin_instance++;
-  }
-
-  type_instance = strchr(type, '-');
-  if (type_instance != NULL) {
-    *type_instance = '\0';
-    type_instance++;
+    *data_source = '\0';
+    data_source++;
   }
 
   *ret_host = hostname;
   *ret_plugin = plugin;
-  *ret_plugin_instance = plugin_instance;
   *ret_type = type;
-  *ret_type_instance = type_instance;
+  *ret_data_source = data_source;
   return 0;
 } /* int parse_identifier */
 
@@ -997,9 +1008,8 @@ int parse_identifier_vl(const char *str, value_list_t *vl) /* {{{ */
   char str_copy[6 * DATA_MAX_NAME_LEN];
   char *host = NULL;
   char *plugin = NULL;
-  char *plugin_instance = NULL;
   char *type = NULL;
-  char *type_instance = NULL;
+  char *data_source = NULL;
   int status;
 
   if ((str == NULL) || (vl == NULL))
@@ -1007,20 +1017,17 @@ int parse_identifier_vl(const char *str, value_list_t *vl) /* {{{ */
 
   sstrncpy(str_copy, str, sizeof(str_copy));
 
-  status = parse_identifier(str_copy, &host, &plugin, &plugin_instance, &type,
-                            &type_instance,
+  status = parse_identifier(str_copy, &host, &plugin, &type,
+                            &data_source,
                             /* default_host = */ NULL);
   if (status != 0)
     return status;
 
   sstrncpy(vl->host, host, sizeof(vl->host));
   sstrncpy(vl->plugin, plugin, sizeof(vl->plugin));
-  sstrncpy(vl->plugin_instance,
-           (plugin_instance != NULL) ? plugin_instance : "",
-           sizeof(vl->plugin_instance));
+
   sstrncpy(vl->type, type, sizeof(vl->type));
-  sstrncpy(vl->type_instance, (type_instance != NULL) ? type_instance : "",
-           sizeof(vl->type_instance));
+
 
   return 0;
 } /* }}} int parse_identifier_vl */
@@ -1228,6 +1235,48 @@ int notification_init(notification_t *n, int severity, const char *message,
   return 0;
 } /* int notification_init */
 
+int notification_init_metric(notification_t *n, int severity,
+                             const char *message, const metric_t *metric_p) {
+  int status = -1;
+  if ((metric_p == NULL) || (metric_p->identity == NULL) ||
+      (metric_p->identity->name == NULL) || (metric_p->ds == NULL)) {
+    return status;
+  }
+  char *name_p = strdup(metric_p->identity->name);
+  if (name_p == NULL) {
+    return status;
+  }
+
+  char *save_p = NULL;
+  char *plugin_p = strtok_r(name_p, "/", &save_p);
+  char *host_p = NULL;
+  char *plugin_instance = NULL;
+  char *type_instance = NULL;
+  char *key_p = NULL;
+  status = identity_get_label(metric_p->identity, "_host", &key_p, &host_p);
+  if (status) {
+    sfree(name_p);
+    return status;
+  }
+  status = identity_get_label(metric_p->identity, "plugin_instance", &key_p,
+                              &plugin_instance);
+  if (status) {
+    sfree(name_p);
+    return status;
+  }
+  status = identity_get_label(metric_p->identity, "type_instance", &key_p,
+                              &type_instance);
+  if (status) {
+    sfree(name_p);
+    return status;
+  }
+
+  status = notification_init(n, severity, message, host_p, plugin_p,
+                             plugin_instance, metric_p->type, type_instance);
+  sfree(name_p);
+  return status;
+}
+
 int walk_directory(const char *dir, dirwalk_callback_f callback,
                    void *user_data, int include_hidden) {
   struct dirent *ent;
index fce2d12bbbc611e2e0683055b297869d05faf3c3..5ec8c631b914849abaa0a9c776529a163cf43fd9 100644 (file)
@@ -323,12 +323,14 @@ int format_name(char *ret, int ret_len, const char *hostname,
 #define FORMAT_VL(ret, ret_len, vl)                                            \
   format_name(ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance,   \
               (vl)->type, (vl)->type_instance)
-int format_values(char *ret, size_t ret_len, const data_set_t *ds,
-                  const value_list_t *vl, bool store_rates);
+int format_values(char *ret, size_t ret_len, const metric_t *metric_p,
+                  bool store_rates);
+int format_values_vl(char *ret, size_t ret_len, const data_set_t *ds,
+                     const value_list_t *vl, bool store_rates);
 
 int parse_identifier(char *str, char **ret_host, char **ret_plugin,
-                     char **ret_plugin_instance, char **ret_type,
-                     char **ret_type_instance, char *default_host);
+                     char **ret_type, char **ret_data_source,
+                     char *default_host);
 int parse_identifier_vl(const char *str, value_list_t *vl);
 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);
@@ -345,6 +347,9 @@ int getpwnam_r(const char *name, struct passwd *pwbuf, char *buf, size_t buflen,
                struct passwd **pwbufp);
 #endif
 
+int notification_init_metric(notification_t *n, int severity,
+                             const char *message, const metric_t *metric_p);
+
 int notification_init(notification_t *n, int severity, const char *message,
                       const char *host, const char *plugin,
                       const char *plugin_instance, const char *type,