mirror of
https://github.com/monitoring-plugins/monitoring-plugins.git
synced 2026-05-28 04:35:40 -04:00
Merge pull request #2083 from RincewindsHat/refactor/check_cluster
Refactor/check cluster
This commit is contained in:
commit
df0d6ee5e0
3 changed files with 102 additions and 77 deletions
|
|
@ -57,7 +57,8 @@ EXTRA_DIST = t \
|
|||
check_apt.d \
|
||||
check_by_ssh.d \
|
||||
check_smtp.d \
|
||||
check_dig.d
|
||||
check_dig.d \
|
||||
check_cluster.d
|
||||
|
||||
PLUGINHDRS = common.h
|
||||
|
||||
|
|
|
|||
|
|
@ -29,42 +29,20 @@ const char *email = "devel@monitoring-plugins.org";
|
|||
#include "common.h"
|
||||
#include "utils.h"
|
||||
#include "utils_base.h"
|
||||
|
||||
enum {
|
||||
CHECK_SERVICES = 1,
|
||||
CHECK_HOSTS = 2
|
||||
};
|
||||
#include "check_cluster.d/config.h"
|
||||
|
||||
static void print_help(void);
|
||||
void print_usage(void);
|
||||
|
||||
static int total_services_ok = 0;
|
||||
static int total_services_warning = 0;
|
||||
static int total_services_unknown = 0;
|
||||
static int total_services_critical = 0;
|
||||
|
||||
static int total_hosts_up = 0;
|
||||
static int total_hosts_down = 0;
|
||||
static int total_hosts_unreachable = 0;
|
||||
|
||||
static char *warn_threshold;
|
||||
static char *crit_threshold;
|
||||
|
||||
static int check_type = CHECK_SERVICES;
|
||||
|
||||
static char *data_vals = NULL;
|
||||
static char *label = NULL;
|
||||
|
||||
static int verbose = 0;
|
||||
|
||||
static int process_arguments(int /*argc*/, char ** /*argv*/);
|
||||
typedef struct {
|
||||
int errorcode;
|
||||
check_cluster_config config;
|
||||
} check_cluster_config_wrapper;
|
||||
static check_cluster_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char *ptr;
|
||||
int data_val;
|
||||
int return_code = STATE_OK;
|
||||
thresholds *thresholds = NULL;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
bindtextdomain(PACKAGE, LOCALEDIR);
|
||||
textdomain(PACKAGE);
|
||||
|
|
@ -72,20 +50,32 @@ int main(int argc, char **argv) {
|
|||
/* Parse extra opts if any */
|
||||
argv = np_extra_opts(&argc, argv, progname);
|
||||
|
||||
if (process_arguments(argc, argv) == ERROR)
|
||||
check_cluster_config_wrapper tmp_config = process_arguments(argc, argv);
|
||||
if (tmp_config.errorcode == ERROR) {
|
||||
usage(_("Could not parse arguments"));
|
||||
}
|
||||
|
||||
const check_cluster_config config = tmp_config.config;
|
||||
|
||||
/* Initialize the thresholds */
|
||||
set_thresholds(&thresholds, warn_threshold, crit_threshold);
|
||||
if (verbose)
|
||||
print_thresholds("check_cluster", thresholds);
|
||||
if (verbose) {
|
||||
print_thresholds("check_cluster", config.thresholds);
|
||||
}
|
||||
|
||||
int data_val;
|
||||
int total_services_ok = 0;
|
||||
int total_services_warning = 0;
|
||||
int total_services_unknown = 0;
|
||||
int total_services_critical = 0;
|
||||
int total_hosts_up = 0;
|
||||
int total_hosts_down = 0;
|
||||
int total_hosts_unreachable = 0;
|
||||
/* check the data values */
|
||||
for (ptr = strtok(data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
|
||||
for (char *ptr = strtok(config.data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
|
||||
|
||||
data_val = atoi(ptr);
|
||||
|
||||
if (check_type == CHECK_SERVICES) {
|
||||
if (config.check_type == CHECK_SERVICES) {
|
||||
switch (data_val) {
|
||||
case 0:
|
||||
total_services_ok++;
|
||||
|
|
@ -119,101 +109,108 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
}
|
||||
|
||||
int return_code = STATE_OK;
|
||||
/* return the status of the cluster */
|
||||
if (check_type == CHECK_SERVICES) {
|
||||
return_code = get_status(total_services_warning + total_services_unknown + total_services_critical, thresholds);
|
||||
if (config.check_type == CHECK_SERVICES) {
|
||||
return_code = 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),
|
||||
(label == NULL) ? "Service cluster" : label, total_services_ok, total_services_warning, total_services_unknown,
|
||||
(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, thresholds);
|
||||
printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), (label == NULL) ? "Host cluster" : label,
|
||||
total_hosts_up, total_hosts_down, total_hosts_unreachable);
|
||||
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);
|
||||
}
|
||||
|
||||
return return_code;
|
||||
exit(return_code);
|
||||
}
|
||||
|
||||
int process_arguments(int argc, char **argv) {
|
||||
int c;
|
||||
char *ptr;
|
||||
int option = 0;
|
||||
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}};
|
||||
|
||||
check_cluster_config_wrapper result = {
|
||||
.errorcode = OK,
|
||||
.config = check_cluster_config_init(),
|
||||
};
|
||||
|
||||
/* no options were supplied */
|
||||
if (argc < 2)
|
||||
return ERROR;
|
||||
if (argc < 2) {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
int option = 0;
|
||||
char *warn_threshold = NULL;
|
||||
char *crit_threshold = NULL;
|
||||
while (true) {
|
||||
int option_index = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option);
|
||||
|
||||
c = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option);
|
||||
|
||||
if (c == -1 || c == EOF || c == 1)
|
||||
if (option_index == -1 || option_index == EOF || option_index == 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
|
||||
switch (option_index) {
|
||||
case 'h': /* host cluster */
|
||||
check_type = CHECK_HOSTS;
|
||||
result.config.check_type = CHECK_HOSTS;
|
||||
break;
|
||||
|
||||
case 's': /* service cluster */
|
||||
check_type = CHECK_SERVICES;
|
||||
result.config.check_type = CHECK_SERVICES;
|
||||
break;
|
||||
|
||||
case 'w': /* warning threshold */
|
||||
warn_threshold = strdup(optarg);
|
||||
break;
|
||||
|
||||
case 'c': /* warning threshold */
|
||||
crit_threshold = strdup(optarg);
|
||||
break;
|
||||
|
||||
case 'd': /* data values */
|
||||
data_vals = (char *)strdup(optarg);
|
||||
result.config.data_vals = strdup(optarg);
|
||||
/* validate data */
|
||||
for (ptr = data_vals; ptr != NULL; ptr += 2) {
|
||||
if (ptr[0] < '0' || ptr[0] > '3')
|
||||
return ERROR;
|
||||
if (ptr[1] == '\0')
|
||||
for (char *ptr = result.config.data_vals; ptr != NULL; ptr += 2) {
|
||||
if (ptr[0] < '0' || ptr[0] > '3') {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
if (ptr[1] == '\0') {
|
||||
break;
|
||||
if (ptr[1] != ',')
|
||||
return ERROR;
|
||||
}
|
||||
if (ptr[1] != ',') {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'l': /* text label */
|
||||
label = (char *)strdup(optarg);
|
||||
result.config.label = strdup(optarg);
|
||||
break;
|
||||
|
||||
case 'v': /* verbose */
|
||||
verbose++;
|
||||
break;
|
||||
|
||||
case 'V': /* version */
|
||||
print_revision(progname, NP_VERSION);
|
||||
exit(STATE_UNKNOWN);
|
||||
break;
|
||||
|
||||
case 'H': /* help */
|
||||
print_help();
|
||||
exit(STATE_UNKNOWN);
|
||||
break;
|
||||
|
||||
default:
|
||||
return ERROR;
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (data_vals == NULL)
|
||||
return ERROR;
|
||||
if (result.config.data_vals == NULL) {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
|
||||
return OK;
|
||||
set_thresholds(&result.config.thresholds, warn_threshold, crit_threshold);
|
||||
return result;
|
||||
}
|
||||
|
||||
void print_help(void) {
|
||||
|
|
|
|||
27
plugins/check_cluster.d/config.h
Normal file
27
plugins/check_cluster.d/config.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include "../../config.h"
|
||||
#include "../../lib/thresholds.h"
|
||||
#include <stddef.h>
|
||||
|
||||
enum {
|
||||
CHECK_SERVICES = 1,
|
||||
CHECK_HOSTS = 2
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
char *data_vals;
|
||||
thresholds *thresholds;
|
||||
int check_type;
|
||||
char *label;
|
||||
} check_cluster_config;
|
||||
|
||||
check_cluster_config check_cluster_config_init() {
|
||||
check_cluster_config tmp = {
|
||||
.data_vals = NULL,
|
||||
.thresholds = NULL,
|
||||
.check_type = CHECK_SERVICES,
|
||||
.label = NULL,
|
||||
};
|
||||
return tmp;
|
||||
}
|
||||
Loading…
Reference in a new issue