]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
src/utils/cmds/getthreshold.c: Adapt to the new metric_t structure.
authorFlorian Forster <octo@collectd.org>
Sun, 21 Jun 2020 17:52:21 +0000 (19:52 +0200)
committerFlorian Forster <octo@google.com>
Wed, 29 Jul 2020 11:40:03 +0000 (13:40 +0200)
src/utils/cmds/getthreshold.c

index b84b286ea63e6f5c4000f4829ba9624a65c56db9..c40495191a557954d13984d162306938e0d0072c 100644 (file)
   }
 
 int handle_getthreshold(FILE *fh, char *buffer) {
-  char *command = NULL;
-  char *identifier = NULL;
-  char *identifier_copy = NULL;
-
-  char *host = NULL;
-  char *plugin = NULL;
-  char *type = NULL;
-  char *data_source = NULL;
-
-  threshold_t threshold;
-
-  int status;
-  size_t i;
-
   if ((fh == NULL) || (buffer == NULL))
     return -1;
 
   DEBUG("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);",
         (void *)fh, buffer);
 
-  status = parse_string(&buffer, &command);
+  char *command = NULL;
+  int status = parse_string(&buffer, &command);
   if (status != 0) {
     print_to_socket(fh, "-1 Cannot parse command.\n");
     return -1;
@@ -74,6 +61,7 @@ int handle_getthreshold(FILE *fh, char *buffer) {
     return -1;
   }
 
+  char *identifier = NULL;
   status = parse_string(&buffer, &identifier);
   if (status != 0) {
     print_to_socket(fh, "-1 Cannot parse identifier.\n");
@@ -88,8 +76,12 @@ int handle_getthreshold(FILE *fh, char *buffer) {
 
   /* parse_identifier() modifies its first argument,
    * returning pointers into it */
-  identifier_copy = sstrdup(identifier);
+  char *identifier_copy = sstrdup(identifier);
 
+  char *host = NULL;
+  char *plugin = NULL;
+  char *type = NULL;
+  char *data_source = NULL;
   status = parse_identifier(identifier_copy, &host, &plugin,
                             &type, &data_source,
                             /* default_host = */ NULL);
@@ -100,15 +92,31 @@ int handle_getthreshold(FILE *fh, char *buffer) {
     return -1;
   }
 
-  metric_t metric = METRIC_STRUCT_INIT;
-  metric.identity = identity_create_legacy(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);
+  char *plugin_instance = strchr(plugin, '-');
+  if (plugin_instance != NULL) {
+         *plugin_instance = 0;
+         plugin_instance++;
+  }
+  char *type_instance = strchr(type, '-');
+  if (type_instance != NULL) {
+         *type_instance = 0;
+         type_instance++;
+  }
+
+  metric_t m = {
+         .identity = identity_create_legacy(plugin, type, data_source, host),
+  };
+  if (plugin_instance != NULL) {
+         identity_add_label(m.identity, "plugin_instance", plugin_instance);
+  }
+  if (type_instance != NULL) {
+         identity_add_label(m.identity, "type_instance", type_instance);
+  }
 
   sfree(identifier_copy);
 
-  status = ut_search_threshold(&metric, &threshold);
+  threshold_t threshold;
+  status = ut_search_threshold(&m, &threshold);
   if (status == ENOENT) {
     print_to_socket(fh, "-1 No threshold found for identifier %s\n",
                     identifier);
@@ -119,30 +127,30 @@ int handle_getthreshold(FILE *fh, char *buffer) {
   }
 
   /* Lets count the number of lines we'll return. */
-  i = 0;
+  int lines_num = 0;
   if (threshold.host[0] != 0)
-    i++;
+    lines_num++;
   if (threshold.plugin[0] != 0)
-    i++;
+    lines_num++;
   if (threshold.type[0] != 0)
-    i++;
+    lines_num++;
   if (threshold.data_source[0] != 0)
-    i++;
+    lines_num++;
   if (!isnan(threshold.warning_min))
-    i++;
+    lines_num++;
   if (!isnan(threshold.warning_max))
-    i++;
+    lines_num++;
   if (!isnan(threshold.failure_min))
-    i++;
+    lines_num++;
   if (!isnan(threshold.failure_max))
-    i++;
+    lines_num++;
   if (threshold.hysteresis > 0.0)
-    i++;
+    lines_num++;
   if (threshold.hits > 1)
-    i++;
+    lines_num++;
 
   /* Print the response */
-  print_to_socket(fh, "%" PRIsz " Threshold found\n", i);
+  print_to_socket(fh, "%d Threshold found\n", lines_num);
 
   if (threshold.host[0] != 0)
     print_to_socket(fh, "Host: %s\n", threshold.host);