diff options
Diffstat (limited to 'plugins/check_cluster.c')
| -rw-r--r-- | plugins/check_cluster.c | 73 |
1 files changed, 57 insertions, 16 deletions
diff --git a/plugins/check_cluster.c b/plugins/check_cluster.c index 9b695499..1cbdcd60 100644 --- a/plugins/check_cluster.c +++ b/plugins/check_cluster.c | |||
| @@ -26,6 +26,8 @@ const char *progname = "check_cluster"; | |||
| 26 | const char *copyright = "2000-2024"; | 26 | const char *copyright = "2000-2024"; |
| 27 | const char *email = "devel@monitoring-plugins.org"; | 27 | const 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,28 +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 = get_status(total_services_warning + total_services_unknown + total_services_critical, config.thresholds); | 123 | sc_real_test = mp_set_subcheck_state( |
| 116 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", state_text(return_code), | 124 | sc_real_test, |
| 117 | (config.label == NULL) ? "Service cluster" : config.label, total_services_ok, total_services_warning, total_services_unknown, | 125 | get_status(total_services_warning + total_services_unknown + total_services_critical, |
| 118 | total_services_critical); | 126 | config.thresholds)); |
| 127 | xasprintf(&sc_real_test.output, "%s: %d ok, %d warning, %d unknown, %d critical", | ||
| 128 | (config.label == NULL) ? "Service cluster" : config.label, total_services_ok, | ||
| 129 | total_services_warning, total_services_unknown, total_services_critical); | ||
| 119 | } else { | 130 | } else { |
| 120 | return_code = get_status(total_hosts_down + total_hosts_unreachable, config.thresholds); | 131 | sc_real_test = mp_set_subcheck_state( |
| 121 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), | 132 | sc_real_test, |
| 122 | (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up, total_hosts_down, total_hosts_unreachable); | 133 | get_status(total_hosts_down + total_hosts_unreachable, config.thresholds)); |
| 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); | ||
| 123 | } | 137 | } |
| 124 | 138 | ||
| 125 | exit(return_code); | 139 | mp_add_subcheck_to_check(&overall, sc_real_test); |
| 140 | |||
| 141 | mp_exit(overall); | ||
| 126 | } | 142 | } |
| 127 | 143 | ||
| 128 | check_cluster_config_wrapper process_arguments(int argc, char **argv) { | 144 | check_cluster_config_wrapper process_arguments(int argc, char **argv) { |
| 129 | static struct option longopts[] = {{"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, | 145 | enum { |
| 130 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, | 146 | output_format_index = CHAR_MAX + 1, |
| 131 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, | 147 | }; |
| 132 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 148 | |
| 133 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; | 149 | static struct option longopts[] = {{"data", required_argument, 0, 'd'}, |
| 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}}; | ||
| 134 | 160 | ||
| 135 | check_cluster_config_wrapper result = { | 161 | check_cluster_config_wrapper result = { |
| 136 | .errorcode = OK, | 162 | .errorcode = OK, |
| @@ -197,6 +223,18 @@ check_cluster_config_wrapper process_arguments(int argc, char **argv) { | |||
| 197 | print_help(); | 223 | print_help(); |
| 198 | exit(STATE_UNKNOWN); | 224 | exit(STATE_UNKNOWN); |
| 199 | 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 | } | ||
| 200 | default: | 238 | default: |
| 201 | result.errorcode = ERROR; | 239 | result.errorcode = ERROR; |
| 202 | return result; | 240 | return result; |
| @@ -244,6 +282,8 @@ void print_help(void) { | |||
| 244 | 282 | ||
| 245 | printf(UT_VERBOSE); | 283 | printf(UT_VERBOSE); |
| 246 | 284 | ||
| 285 | printf(UT_OUTPUT_FORMAT); | ||
| 286 | |||
| 247 | printf("\n"); | 287 | printf("\n"); |
| 248 | printf("%s\n", _("Notes:")); | 288 | printf("%s\n", _("Notes:")); |
| 249 | printf(UT_THRESHOLDS_NOTES); | 289 | printf(UT_THRESHOLDS_NOTES); |
| @@ -251,7 +291,8 @@ void print_help(void) { | |||
| 251 | printf("\n"); | 291 | printf("\n"); |
| 252 | printf("%s\n", _("Examples:")); | 292 | printf("%s\n", _("Examples:")); |
| 253 | printf(" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:"); | 293 | printf(" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:"); |
| 254 | printf(" %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK")); | 294 | printf(" %s\n", |
| 295 | _("Will alert critical if there are 3 or more service data points in a non-OK")); | ||
| 255 | printf(" %s\n", _("state.")); | 296 | printf(" %s\n", _("state.")); |
| 256 | 297 | ||
| 257 | printf(UT_SUPPORT); | 298 | printf(UT_SUPPORT); |
