diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-10 22:49:38 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-10 22:49:38 +0100 |
commit | 2ce110bf57f5a412a48fd8baf4d5e498e0acc6c6 (patch) | |
tree | 1b61f8d49490476b02334c84b65f7b1a4a064125 | |
parent | 1a97496a83562b1d354609226b05857717c5ab5a (diff) | |
download | monitoring-plugins-2ce110bf57f5a412a48fd8baf4d5e498e0acc6c6.tar.gz |
refactor check_cluster
-rw-r--r-- | plugins/Makefile.am | 3 | ||||
-rw-r--r-- | plugins/check_cluster.c | 100 | ||||
-rw-r--r-- | plugins/check_cluster.d/config.h | 27 |
3 files changed, 81 insertions, 49 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 41487131..be650089 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -57,7 +57,8 @@ EXTRA_DIST = t \ | |||
57 | check_apt.d \ | 57 | check_apt.d \ |
58 | check_by_ssh.d \ | 58 | check_by_ssh.d \ |
59 | check_smtp.d \ | 59 | check_smtp.d \ |
60 | check_dig.d | 60 | check_dig.d \ |
61 | check_cluster.d | ||
61 | 62 | ||
62 | PLUGINHDRS = common.h | 63 | PLUGINHDRS = common.h |
63 | 64 | ||
diff --git a/plugins/check_cluster.c b/plugins/check_cluster.c index 72acde2e..9b695499 100644 --- a/plugins/check_cluster.c +++ b/plugins/check_cluster.c | |||
@@ -29,35 +29,18 @@ const char *email = "devel@monitoring-plugins.org"; | |||
29 | #include "common.h" | 29 | #include "common.h" |
30 | #include "utils.h" | 30 | #include "utils.h" |
31 | #include "utils_base.h" | 31 | #include "utils_base.h" |
32 | 32 | #include "check_cluster.d/config.h" | |
33 | enum { | ||
34 | CHECK_SERVICES = 1, | ||
35 | CHECK_HOSTS = 2 | ||
36 | }; | ||
37 | 33 | ||
38 | static void print_help(void); | 34 | static void print_help(void); |
39 | void print_usage(void); | 35 | void print_usage(void); |
40 | 36 | ||
41 | static int total_services_ok = 0; | ||
42 | static int total_services_warning = 0; | ||
43 | static int total_services_unknown = 0; | ||
44 | static int total_services_critical = 0; | ||
45 | |||
46 | static int total_hosts_up = 0; | ||
47 | static int total_hosts_down = 0; | ||
48 | static int total_hosts_unreachable = 0; | ||
49 | |||
50 | static char *warn_threshold; | ||
51 | static char *crit_threshold; | ||
52 | |||
53 | static int check_type = CHECK_SERVICES; | ||
54 | |||
55 | static char *data_vals = NULL; | ||
56 | static char *label = NULL; | ||
57 | |||
58 | static int verbose = 0; | 37 | static int verbose = 0; |
59 | 38 | ||
60 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 39 | typedef struct { |
40 | int errorcode; | ||
41 | check_cluster_config config; | ||
42 | } check_cluster_config_wrapper; | ||
43 | static check_cluster_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
61 | 44 | ||
62 | int main(int argc, char **argv) { | 45 | int main(int argc, char **argv) { |
63 | setlocale(LC_ALL, ""); | 46 | setlocale(LC_ALL, ""); |
@@ -67,24 +50,32 @@ int main(int argc, char **argv) { | |||
67 | /* Parse extra opts if any */ | 50 | /* Parse extra opts if any */ |
68 | argv = np_extra_opts(&argc, argv, progname); | 51 | argv = np_extra_opts(&argc, argv, progname); |
69 | 52 | ||
70 | if (process_arguments(argc, argv) == ERROR) { | 53 | check_cluster_config_wrapper tmp_config = process_arguments(argc, argv); |
54 | if (tmp_config.errorcode == ERROR) { | ||
71 | usage(_("Could not parse arguments")); | 55 | usage(_("Could not parse arguments")); |
72 | } | 56 | } |
73 | 57 | ||
74 | thresholds *thresholds = NULL; | 58 | const check_cluster_config config = tmp_config.config; |
59 | |||
75 | /* Initialize the thresholds */ | 60 | /* Initialize the thresholds */ |
76 | set_thresholds(&thresholds, warn_threshold, crit_threshold); | ||
77 | if (verbose) { | 61 | if (verbose) { |
78 | print_thresholds("check_cluster", thresholds); | 62 | print_thresholds("check_cluster", config.thresholds); |
79 | } | 63 | } |
80 | 64 | ||
81 | int data_val; | 65 | int data_val; |
66 | int total_services_ok = 0; | ||
67 | int total_services_warning = 0; | ||
68 | int total_services_unknown = 0; | ||
69 | int total_services_critical = 0; | ||
70 | int total_hosts_up = 0; | ||
71 | int total_hosts_down = 0; | ||
72 | int total_hosts_unreachable = 0; | ||
82 | /* check the data values */ | 73 | /* check the data values */ |
83 | for (char *ptr = strtok(data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) { | 74 | for (char *ptr = strtok(config.data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) { |
84 | 75 | ||
85 | data_val = atoi(ptr); | 76 | data_val = atoi(ptr); |
86 | 77 | ||
87 | if (check_type == CHECK_SERVICES) { | 78 | if (config.check_type == CHECK_SERVICES) { |
88 | switch (data_val) { | 79 | switch (data_val) { |
89 | case 0: | 80 | case 0: |
90 | total_services_ok++; | 81 | total_services_ok++; |
@@ -120,33 +111,41 @@ int main(int argc, char **argv) { | |||
120 | 111 | ||
121 | int return_code = STATE_OK; | 112 | int return_code = STATE_OK; |
122 | /* return the status of the cluster */ | 113 | /* return the status of the cluster */ |
123 | if (check_type == CHECK_SERVICES) { | 114 | if (config.check_type == CHECK_SERVICES) { |
124 | return_code = get_status(total_services_warning + total_services_unknown + total_services_critical, thresholds); | 115 | return_code = get_status(total_services_warning + total_services_unknown + total_services_critical, config.thresholds); |
125 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", state_text(return_code), | 116 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", state_text(return_code), |
126 | (label == NULL) ? "Service cluster" : label, total_services_ok, total_services_warning, total_services_unknown, | 117 | (config.label == NULL) ? "Service cluster" : config.label, total_services_ok, total_services_warning, total_services_unknown, |
127 | total_services_critical); | 118 | total_services_critical); |
128 | } else { | 119 | } else { |
129 | return_code = get_status(total_hosts_down + total_hosts_unreachable, thresholds); | 120 | return_code = get_status(total_hosts_down + total_hosts_unreachable, config.thresholds); |
130 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), (label == NULL) ? "Host cluster" : label, | 121 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), |
131 | total_hosts_up, total_hosts_down, total_hosts_unreachable); | 122 | (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up, total_hosts_down, total_hosts_unreachable); |
132 | } | 123 | } |
133 | 124 | ||
134 | exit(return_code); | 125 | exit(return_code); |
135 | } | 126 | } |
136 | 127 | ||
137 | int process_arguments(int argc, char **argv) { | 128 | check_cluster_config_wrapper process_arguments(int argc, char **argv) { |
138 | static struct option longopts[] = {{"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, | 129 | static struct option longopts[] = {{"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, |
139 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, | 130 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, |
140 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, | 131 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, |
141 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 132 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
142 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; | 133 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; |
143 | 134 | ||
135 | check_cluster_config_wrapper result = { | ||
136 | .errorcode = OK, | ||
137 | .config = check_cluster_config_init(), | ||
138 | }; | ||
139 | |||
144 | /* no options were supplied */ | 140 | /* no options were supplied */ |
145 | if (argc < 2) { | 141 | if (argc < 2) { |
146 | return ERROR; | 142 | result.errorcode = ERROR; |
143 | return result; | ||
147 | } | 144 | } |
148 | 145 | ||
149 | int option = 0; | 146 | int option = 0; |
147 | char *warn_threshold = NULL; | ||
148 | char *crit_threshold = NULL; | ||
150 | while (true) { | 149 | while (true) { |
151 | int option_index = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option); | 150 | int option_index = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option); |
152 | 151 | ||
@@ -156,10 +155,10 @@ int process_arguments(int argc, char **argv) { | |||
156 | 155 | ||
157 | switch (option_index) { | 156 | switch (option_index) { |
158 | case 'h': /* host cluster */ | 157 | case 'h': /* host cluster */ |
159 | check_type = CHECK_HOSTS; | 158 | result.config.check_type = CHECK_HOSTS; |
160 | break; | 159 | break; |
161 | case 's': /* service cluster */ | 160 | case 's': /* service cluster */ |
162 | check_type = CHECK_SERVICES; | 161 | result.config.check_type = CHECK_SERVICES; |
163 | break; | 162 | break; |
164 | case 'w': /* warning threshold */ | 163 | case 'w': /* warning threshold */ |
165 | warn_threshold = strdup(optarg); | 164 | warn_threshold = strdup(optarg); |
@@ -168,22 +167,24 @@ int process_arguments(int argc, char **argv) { | |||
168 | crit_threshold = strdup(optarg); | 167 | crit_threshold = strdup(optarg); |
169 | break; | 168 | break; |
170 | case 'd': /* data values */ | 169 | case 'd': /* data values */ |
171 | data_vals = strdup(optarg); | 170 | result.config.data_vals = strdup(optarg); |
172 | /* validate data */ | 171 | /* validate data */ |
173 | for (char *ptr = data_vals; ptr != NULL; ptr += 2) { | 172 | for (char *ptr = result.config.data_vals; ptr != NULL; ptr += 2) { |
174 | if (ptr[0] < '0' || ptr[0] > '3') { | 173 | if (ptr[0] < '0' || ptr[0] > '3') { |
175 | return ERROR; | 174 | result.errorcode = ERROR; |
175 | return result; | ||
176 | } | 176 | } |
177 | if (ptr[1] == '\0') { | 177 | if (ptr[1] == '\0') { |
178 | break; | 178 | break; |
179 | } | 179 | } |
180 | if (ptr[1] != ',') { | 180 | if (ptr[1] != ',') { |
181 | return ERROR; | 181 | result.errorcode = ERROR; |
182 | return result; | ||
182 | } | 183 | } |
183 | } | 184 | } |
184 | break; | 185 | break; |
185 | case 'l': /* text label */ | 186 | case 'l': /* text label */ |
186 | label = strdup(optarg); | 187 | result.config.label = strdup(optarg); |
187 | break; | 188 | break; |
188 | case 'v': /* verbose */ | 189 | case 'v': /* verbose */ |
189 | verbose++; | 190 | verbose++; |
@@ -197,16 +198,19 @@ int process_arguments(int argc, char **argv) { | |||
197 | exit(STATE_UNKNOWN); | 198 | exit(STATE_UNKNOWN); |
198 | break; | 199 | break; |
199 | default: | 200 | default: |
200 | return ERROR; | 201 | result.errorcode = ERROR; |
202 | return result; | ||
201 | break; | 203 | break; |
202 | } | 204 | } |
203 | } | 205 | } |
204 | 206 | ||
205 | if (data_vals == NULL) { | 207 | if (result.config.data_vals == NULL) { |
206 | return ERROR; | 208 | result.errorcode = ERROR; |
209 | return result; | ||
207 | } | 210 | } |
208 | 211 | ||
209 | return OK; | 212 | set_thresholds(&result.config.thresholds, warn_threshold, crit_threshold); |
213 | return result; | ||
210 | } | 214 | } |
211 | 215 | ||
212 | void print_help(void) { | 216 | void print_help(void) { |
diff --git a/plugins/check_cluster.d/config.h b/plugins/check_cluster.d/config.h new file mode 100644 index 00000000..fc386415 --- /dev/null +++ b/plugins/check_cluster.d/config.h | |||
@@ -0,0 +1,27 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include "../../lib/thresholds.h" | ||
5 | #include <stddef.h> | ||
6 | |||
7 | enum { | ||
8 | CHECK_SERVICES = 1, | ||
9 | CHECK_HOSTS = 2 | ||
10 | }; | ||
11 | |||
12 | typedef struct { | ||
13 | char *data_vals; | ||
14 | thresholds *thresholds; | ||
15 | int check_type; | ||
16 | char *label; | ||
17 | } check_cluster_config; | ||
18 | |||
19 | check_cluster_config check_cluster_config_init() { | ||
20 | check_cluster_config tmp = { | ||
21 | .data_vals = NULL, | ||
22 | .thresholds = NULL, | ||
23 | .check_type = CHECK_SERVICES, | ||
24 | .label = NULL, | ||
25 | }; | ||
26 | return tmp; | ||
27 | } | ||