[monitoring-plugins] check_cluster: new output functionality

Lorenz Kästle git at monitoring-plugins.org
Wed Sep 17 14:00:15 CEST 2025


 Module: monitoring-plugins
 Branch: master
 Commit: 88f316bb2721921f2f7fa12e196fc299db60c2f8
 Author: Lorenz Kästle <12514511+RincewindsHat at users.noreply.github.com>
   Date: Wed Sep 17 11:25:13 2025 +0200
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=88f316bb

check_cluster: new output functionality

---

 plugins/check_cluster.c          | 73 +++++++++++++++++++++++++++++-----------
 plugins/check_cluster.d/config.h |  6 ++++
 2 files changed, 60 insertions(+), 19 deletions(-)

diff --git a/plugins/check_cluster.c b/plugins/check_cluster.c
index 373520ee..1cbdcd60 100644
--- a/plugins/check_cluster.c
+++ b/plugins/check_cluster.c
@@ -26,6 +26,8 @@ const char *progname = "check_cluster";
 const char *copyright = "2000-2024";
 const char *email = "devel at monitoring-plugins.org";
 
+#include "output.h"
+#include "states.h"
 #include "common.h"
 #include "utils.h"
 #include "utils_base.h"
@@ -57,6 +59,10 @@ int main(int argc, char **argv) {
 
 	const check_cluster_config config = tmp_config.config;
 
+	if (config.output_format_is_set) {
+		mp_set_format(config.output_format);
+	}
+
 	/* Initialize the thresholds */
 	if (verbose) {
 		print_thresholds("check_cluster", config.thresholds);
@@ -72,7 +78,6 @@ int main(int argc, char **argv) {
 	int total_hosts_unreachable = 0;
 	/* check the data values */
 	for (char *ptr = strtok(config.data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
-
 		data_val = atoi(ptr);
 
 		if (config.check_type == CHECK_SERVICES) {
@@ -109,33 +114,49 @@ int main(int argc, char **argv) {
 		}
 	}
 
-	int return_code = STATE_OK;
+	mp_check overall = mp_check_init();
+	mp_subcheck sc_real_test = mp_subcheck_init();
+	sc_real_test = mp_set_subcheck_default_state(sc_real_test, STATE_OK);
+
 	/* return the status of the cluster */
 	if (config.check_type == CHECK_SERVICES) {
-		return_code =
+		sc_real_test = mp_set_subcheck_state(
+			sc_real_test,
 			get_status(total_services_warning + total_services_unknown + total_services_critical,
-					   config.thresholds);
-		printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
-			   state_text(return_code), (config.label == NULL) ? "Service cluster" : config.label,
-			   total_services_ok, total_services_warning, total_services_unknown,
-			   total_services_critical);
+					   config.thresholds));
+		xasprintf(&sc_real_test.output, "%s: %d ok, %d warning, %d unknown, %d critical",
+				  (config.label == NULL) ? "Service cluster" : config.label, total_services_ok,
+				  total_services_warning, total_services_unknown, total_services_critical);
 	} else {
-		return_code = get_status(total_hosts_down + total_hosts_unreachable, config.thresholds);
-		printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code),
-			   (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up,
-			   total_hosts_down, total_hosts_unreachable);
+		sc_real_test = mp_set_subcheck_state(
+			sc_real_test,
+			get_status(total_hosts_down + total_hosts_unreachable, config.thresholds));
+		xasprintf(&sc_real_test.output, "%s: %d up, %d down, %d unreachable\n",
+				  (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up,
+				  total_hosts_down, total_hosts_unreachable);
 	}
 
-	exit(return_code);
+	mp_add_subcheck_to_check(&overall, sc_real_test);
+
+	mp_exit(overall);
 }
 
 check_cluster_config_wrapper process_arguments(int argc, char **argv) {
-	static struct option longopts[] = {
-		{"data", required_argument, 0, 'd'},     {"warning", required_argument, 0, 'w'},
-		{"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'},
-		{"host", no_argument, 0, 'h'},           {"service", no_argument, 0, 's'},
-		{"verbose", no_argument, 0, 'v'},        {"version", no_argument, 0, 'V'},
-		{"help", no_argument, 0, 'H'},           {0, 0, 0, 0}};
+	enum {
+		output_format_index = CHAR_MAX + 1,
+	};
+
+	static struct option longopts[] = {{"data", required_argument, 0, 'd'},
+									   {"warning", required_argument, 0, 'w'},
+									   {"critical", required_argument, 0, 'c'},
+									   {"label", required_argument, 0, 'l'},
+									   {"host", no_argument, 0, 'h'},
+									   {"service", no_argument, 0, 's'},
+									   {"verbose", no_argument, 0, 'v'},
+									   {"version", no_argument, 0, 'V'},
+									   {"help", no_argument, 0, 'H'},
+									   {"output-format", required_argument, 0, output_format_index},
+									   {0, 0, 0, 0}};
 
 	check_cluster_config_wrapper result = {
 		.errorcode = OK,
@@ -202,6 +223,18 @@ check_cluster_config_wrapper process_arguments(int argc, char **argv) {
 			print_help();
 			exit(STATE_UNKNOWN);
 			break;
+		case output_format_index: {
+			parsed_output_format parser = mp_parse_output_format(optarg);
+			if (!parser.parsing_success) {
+				// TODO List all available formats here, maybe add anothoer usage function
+				printf("Invalid output format: %s\n", optarg);
+				exit(STATE_UNKNOWN);
+			}
+
+			result.config.output_format_is_set = true;
+			result.config.output_format = parser.output_format;
+			break;
+		}
 		default:
 			result.errorcode = ERROR;
 			return result;
@@ -249,6 +282,8 @@ void print_help(void) {
 
 	printf(UT_VERBOSE);
 
+	printf(UT_OUTPUT_FORMAT);
+
 	printf("\n");
 	printf("%s\n", _("Notes:"));
 	printf(UT_THRESHOLDS_NOTES);
diff --git a/plugins/check_cluster.d/config.h b/plugins/check_cluster.d/config.h
index fc386415..054657b0 100644
--- a/plugins/check_cluster.d/config.h
+++ b/plugins/check_cluster.d/config.h
@@ -2,6 +2,7 @@
 
 #include "../../config.h"
 #include "../../lib/thresholds.h"
+#include "output.h"
 #include <stddef.h>
 
 enum {
@@ -14,6 +15,9 @@ typedef struct {
 	thresholds *thresholds;
 	int check_type;
 	char *label;
+
+	mp_output_format output_format;
+	bool output_format_is_set;
 } check_cluster_config;
 
 check_cluster_config check_cluster_config_init() {
@@ -22,6 +26,8 @@ check_cluster_config check_cluster_config_init() {
 		.thresholds = NULL,
 		.check_type = CHECK_SERVICES,
 		.label = NULL,
+
+		.output_format_is_set = false,
 	};
 	return tmp;
 }



More information about the Commits mailing list