summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-09-17 13:51:22 +0200
committerGitHub <noreply@github.com>2025-09-17 13:51:22 +0200
commita51ff78f83c822f0a809ee3dbb6a1856b1fcc589 (patch)
tree4520e80a9c22b02d5777c2027b934247eadfa19f /plugins
parent5ce7b57c74ecb2cf1b53f75764382ec26e818551 (diff)
parent88f316bb2721921f2f7fa12e196fc299db60c2f8 (diff)
downloadmonitoring-plugins-refs/heads/master.tar.gz
Merge pull request #2154 from RincewindsHat/new-output/check_clusterHEADmaster
check_cluster: new output functionality
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_cluster.c73
-rw-r--r--plugins/check_cluster.d/config.h6
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";
26const char *copyright = "2000-2024"; 26const char *copyright = "2000-2024";
27const char *email = "devel@monitoring-plugins.org"; 27const char *email = "devel@monitoring-plugins.org";
28 28
29#include "output.h"
30#include "states.h"
29#include "common.h" 31#include "common.h"
30#include "utils.h" 32#include "utils.h"
31#include "utils_base.h" 33#include "utils_base.h"
@@ -57,6 +59,10 @@ int main(int argc, char **argv) {
57 59
58 const check_cluster_config config = tmp_config.config; 60 const check_cluster_config config = tmp_config.config;
59 61
62 if (config.output_format_is_set) {
63 mp_set_format(config.output_format);
64 }
65
60 /* Initialize the thresholds */ 66 /* Initialize the thresholds */
61 if (verbose) { 67 if (verbose) {
62 print_thresholds("check_cluster", config.thresholds); 68 print_thresholds("check_cluster", config.thresholds);
@@ -72,7 +78,6 @@ int main(int argc, char **argv) {
72 int total_hosts_unreachable = 0; 78 int total_hosts_unreachable = 0;
73 /* check the data values */ 79 /* check the data values */
74 for (char *ptr = strtok(config.data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) { 80 for (char *ptr = strtok(config.data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
75
76 data_val = atoi(ptr); 81 data_val = atoi(ptr);
77 82
78 if (config.check_type == CHECK_SERVICES) { 83 if (config.check_type == CHECK_SERVICES) {
@@ -109,33 +114,49 @@ int main(int argc, char **argv) {
109 } 114 }
110 } 115 }
111 116
112 int return_code = STATE_OK; 117 mp_check overall = mp_check_init();
118 mp_subcheck sc_real_test = mp_subcheck_init();
119 sc_real_test = mp_set_subcheck_default_state(sc_real_test, STATE_OK);
120
113 /* return the status of the cluster */ 121 /* return the status of the cluster */
114 if (config.check_type == CHECK_SERVICES) { 122 if (config.check_type == CHECK_SERVICES) {
115 return_code = 123 sc_real_test = mp_set_subcheck_state(
124 sc_real_test,
116 get_status(total_services_warning + total_services_unknown + total_services_critical, 125 get_status(total_services_warning + total_services_unknown + total_services_critical,
117 config.thresholds); 126 config.thresholds));
118 printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", 127 xasprintf(&sc_real_test.output, "%s: %d ok, %d warning, %d unknown, %d critical",
119 state_text(return_code), (config.label == NULL) ? "Service cluster" : config.label, 128 (config.label == NULL) ? "Service cluster" : config.label, total_services_ok,
120 total_services_ok, total_services_warning, total_services_unknown, 129 total_services_warning, total_services_unknown, total_services_critical);
121 total_services_critical);
122 } else { 130 } else {
123 return_code = get_status(total_hosts_down + total_hosts_unreachable, config.thresholds); 131 sc_real_test = mp_set_subcheck_state(
124 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), 132 sc_real_test,
125 (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up, 133 get_status(total_hosts_down + total_hosts_unreachable, config.thresholds));
126 total_hosts_down, total_hosts_unreachable); 134 xasprintf(&sc_real_test.output, "%s: %d up, %d down, %d unreachable\n",
135 (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up,
136 total_hosts_down, total_hosts_unreachable);
127 } 137 }
128 138
129 exit(return_code); 139 mp_add_subcheck_to_check(&overall, sc_real_test);
140
141 mp_exit(overall);
130} 142}
131 143
132check_cluster_config_wrapper process_arguments(int argc, char **argv) { 144check_cluster_config_wrapper process_arguments(int argc, char **argv) {
133 static struct option longopts[] = { 145 enum {
134 {"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, 146 output_format_index = CHAR_MAX + 1,
135 {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, 147 };
136 {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, 148
137 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, 149 static struct option longopts[] = {{"data", required_argument, 0, 'd'},
138 {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; 150 {"warning", required_argument, 0, 'w'},
151 {"critical", required_argument, 0, 'c'},
152 {"label", required_argument, 0, 'l'},
153 {"host", no_argument, 0, 'h'},
154 {"service", no_argument, 0, 's'},
155 {"verbose", no_argument, 0, 'v'},
156 {"version", no_argument, 0, 'V'},
157 {"help", no_argument, 0, 'H'},
158 {"output-format", required_argument, 0, output_format_index},
159 {0, 0, 0, 0}};
139 160
140 check_cluster_config_wrapper result = { 161 check_cluster_config_wrapper result = {
141 .errorcode = OK, 162 .errorcode = OK,
@@ -202,6 +223,18 @@ check_cluster_config_wrapper process_arguments(int argc, char **argv) {
202 print_help(); 223 print_help();
203 exit(STATE_UNKNOWN); 224 exit(STATE_UNKNOWN);
204 break; 225 break;
226 case output_format_index: {
227 parsed_output_format parser = mp_parse_output_format(optarg);
228 if (!parser.parsing_success) {
229 // TODO List all available formats here, maybe add anothoer usage function
230 printf("Invalid output format: %s\n", optarg);
231 exit(STATE_UNKNOWN);
232 }
233
234 result.config.output_format_is_set = true;
235 result.config.output_format = parser.output_format;
236 break;
237 }
205 default: 238 default:
206 result.errorcode = ERROR; 239 result.errorcode = ERROR;
207 return result; 240 return result;
@@ -249,6 +282,8 @@ void print_help(void) {
249 282
250 printf(UT_VERBOSE); 283 printf(UT_VERBOSE);
251 284
285 printf(UT_OUTPUT_FORMAT);
286
252 printf("\n"); 287 printf("\n");
253 printf("%s\n", _("Notes:")); 288 printf("%s\n", _("Notes:"));
254 printf(UT_THRESHOLDS_NOTES); 289 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 @@
2 2
3#include "../../config.h" 3#include "../../config.h"
4#include "../../lib/thresholds.h" 4#include "../../lib/thresholds.h"
5#include "output.h"
5#include <stddef.h> 6#include <stddef.h>
6 7
7enum { 8enum {
@@ -14,6 +15,9 @@ typedef struct {
14 thresholds *thresholds; 15 thresholds *thresholds;
15 int check_type; 16 int check_type;
16 char *label; 17 char *label;
18
19 mp_output_format output_format;
20 bool output_format_is_set;
17} check_cluster_config; 21} check_cluster_config;
18 22
19check_cluster_config check_cluster_config_init() { 23check_cluster_config check_cluster_config_init() {
@@ -22,6 +26,8 @@ check_cluster_config check_cluster_config_init() {
22 .thresholds = NULL, 26 .thresholds = NULL,
23 .check_type = CHECK_SERVICES, 27 .check_type = CHECK_SERVICES,
24 .label = NULL, 28 .label = NULL,
29
30 .output_format_is_set = false,
25 }; 31 };
26 return tmp; 32 return tmp;
27} 33}