From 7f65dfc79f036817c89eefcb3ef0b504d96dc99b Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 21 Jun 2020 19:52:21 +0200 Subject: [PATCH] src/utils/cmds/getthreshold.c: Adapt to the new metric_t structure. --- src/utils/cmds/getthreshold.c | 76 +++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/src/utils/cmds/getthreshold.c b/src/utils/cmds/getthreshold.c index b84b286ea..c40495191 100644 --- a/src/utils/cmds/getthreshold.c +++ b/src/utils/cmds/getthreshold.c @@ -42,27 +42,14 @@ } 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); -- 2.47.2