diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 10:43:31 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 10:43:31 +0100 |
commit | 7acfac740975f633ae83883535590dffb2650c88 (patch) | |
tree | 3d05b28f6d900ebe116f0a3b5d7e8afdea230e65 /plugins/check_cluster.c | |
parent | cf48162487d6f042af084ee436acdd7a2db0cbfd (diff) | |
parent | df0d6ee5e0c870d50934eb095a88587547697953 (diff) | |
download | monitoring-plugins-7acfac740975f633ae83883535590dffb2650c88.tar.gz |
Merge branch 'master' into refactor/check_fping
Diffstat (limited to 'plugins/check_cluster.c')
-rw-r--r-- | plugins/check_cluster.c | 149 |
1 files changed, 73 insertions, 76 deletions
diff --git a/plugins/check_cluster.c b/plugins/check_cluster.c index b40c38c7..9b695499 100644 --- a/plugins/check_cluster.c +++ b/plugins/check_cluster.c | |||
@@ -29,42 +29,20 @@ 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 | char *ptr; | ||
64 | int data_val; | ||
65 | int return_code = STATE_OK; | ||
66 | thresholds *thresholds = NULL; | ||
67 | |||
68 | setlocale(LC_ALL, ""); | 46 | setlocale(LC_ALL, ""); |
69 | bindtextdomain(PACKAGE, LOCALEDIR); | 47 | bindtextdomain(PACKAGE, LOCALEDIR); |
70 | textdomain(PACKAGE); | 48 | textdomain(PACKAGE); |
@@ -72,20 +50,32 @@ int main(int argc, char **argv) { | |||
72 | /* Parse extra opts if any */ | 50 | /* Parse extra opts if any */ |
73 | argv = np_extra_opts(&argc, argv, progname); | 51 | argv = np_extra_opts(&argc, argv, progname); |
74 | 52 | ||
75 | if (process_arguments(argc, argv) == ERROR) | 53 | check_cluster_config_wrapper tmp_config = process_arguments(argc, argv); |
54 | if (tmp_config.errorcode == ERROR) { | ||
76 | usage(_("Could not parse arguments")); | 55 | usage(_("Could not parse arguments")); |
56 | } | ||
57 | |||
58 | const check_cluster_config config = tmp_config.config; | ||
77 | 59 | ||
78 | /* Initialize the thresholds */ | 60 | /* Initialize the thresholds */ |
79 | set_thresholds(&thresholds, warn_threshold, crit_threshold); | 61 | if (verbose) { |
80 | if (verbose) | 62 | print_thresholds("check_cluster", config.thresholds); |
81 | print_thresholds("check_cluster", thresholds); | 63 | } |
82 | 64 | ||
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; | ||
83 | /* check the data values */ | 73 | /* check the data values */ |
84 | for (ptr = strtok(data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) { | 74 | for (char *ptr = strtok(config.data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) { |
85 | 75 | ||
86 | data_val = atoi(ptr); | 76 | data_val = atoi(ptr); |
87 | 77 | ||
88 | if (check_type == CHECK_SERVICES) { | 78 | if (config.check_type == CHECK_SERVICES) { |
89 | switch (data_val) { | 79 | switch (data_val) { |
90 | case 0: | 80 | case 0: |
91 | total_services_ok++; | 81 | total_services_ok++; |
@@ -119,101 +109,108 @@ int main(int argc, char **argv) { | |||
119 | } | 109 | } |
120 | } | 110 | } |
121 | 111 | ||
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 | return 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 | int c; | ||
139 | char *ptr; | ||
140 | int option = 0; | ||
141 | 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'}, |
142 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, | 130 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, |
143 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, | 131 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, |
144 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 132 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
145 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; | 133 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; |
146 | 134 | ||
147 | /* no options were supplied */ | 135 | check_cluster_config_wrapper result = { |
148 | if (argc < 2) | 136 | .errorcode = OK, |
149 | return ERROR; | 137 | .config = check_cluster_config_init(), |
138 | }; | ||
150 | 139 | ||
151 | while (1) { | 140 | /* no options were supplied */ |
141 | if (argc < 2) { | ||
142 | result.errorcode = ERROR; | ||
143 | return result; | ||
144 | } | ||
152 | 145 | ||
153 | c = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option); | 146 | int option = 0; |
147 | char *warn_threshold = NULL; | ||
148 | char *crit_threshold = NULL; | ||
149 | while (true) { | ||
150 | int option_index = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option); | ||
154 | 151 | ||
155 | if (c == -1 || c == EOF || c == 1) | 152 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
156 | break; | 153 | break; |
154 | } | ||
157 | 155 | ||
158 | switch (c) { | 156 | switch (option_index) { |
159 | |||
160 | case 'h': /* host cluster */ | 157 | case 'h': /* host cluster */ |
161 | check_type = CHECK_HOSTS; | 158 | result.config.check_type = CHECK_HOSTS; |
162 | break; | 159 | break; |
163 | |||
164 | case 's': /* service cluster */ | 160 | case 's': /* service cluster */ |
165 | check_type = CHECK_SERVICES; | 161 | result.config.check_type = CHECK_SERVICES; |
166 | break; | 162 | break; |
167 | |||
168 | case 'w': /* warning threshold */ | 163 | case 'w': /* warning threshold */ |
169 | warn_threshold = strdup(optarg); | 164 | warn_threshold = strdup(optarg); |
170 | break; | 165 | break; |
171 | |||
172 | case 'c': /* warning threshold */ | 166 | case 'c': /* warning threshold */ |
173 | crit_threshold = strdup(optarg); | 167 | crit_threshold = strdup(optarg); |
174 | break; | 168 | break; |
175 | |||
176 | case 'd': /* data values */ | 169 | case 'd': /* data values */ |
177 | data_vals = (char *)strdup(optarg); | 170 | result.config.data_vals = strdup(optarg); |
178 | /* validate data */ | 171 | /* validate data */ |
179 | for (ptr = data_vals; ptr != NULL; ptr += 2) { | 172 | for (char *ptr = result.config.data_vals; ptr != NULL; ptr += 2) { |
180 | if (ptr[0] < '0' || ptr[0] > '3') | 173 | if (ptr[0] < '0' || ptr[0] > '3') { |
181 | return ERROR; | 174 | result.errorcode = ERROR; |
182 | if (ptr[1] == '\0') | 175 | return result; |
176 | } | ||
177 | if (ptr[1] == '\0') { | ||
183 | break; | 178 | break; |
184 | if (ptr[1] != ',') | 179 | } |
185 | return ERROR; | 180 | if (ptr[1] != ',') { |
181 | result.errorcode = ERROR; | ||
182 | return result; | ||
183 | } | ||
186 | } | 184 | } |
187 | break; | 185 | break; |
188 | |||
189 | case 'l': /* text label */ | 186 | case 'l': /* text label */ |
190 | label = (char *)strdup(optarg); | 187 | result.config.label = strdup(optarg); |
191 | break; | 188 | break; |
192 | |||
193 | case 'v': /* verbose */ | 189 | case 'v': /* verbose */ |
194 | verbose++; | 190 | verbose++; |
195 | break; | 191 | break; |
196 | |||
197 | case 'V': /* version */ | 192 | case 'V': /* version */ |
198 | print_revision(progname, NP_VERSION); | 193 | print_revision(progname, NP_VERSION); |
199 | exit(STATE_UNKNOWN); | 194 | exit(STATE_UNKNOWN); |
200 | break; | 195 | break; |
201 | |||
202 | case 'H': /* help */ | 196 | case 'H': /* help */ |
203 | print_help(); | 197 | print_help(); |
204 | exit(STATE_UNKNOWN); | 198 | exit(STATE_UNKNOWN); |
205 | break; | 199 | break; |
206 | |||
207 | default: | 200 | default: |
208 | return ERROR; | 201 | result.errorcode = ERROR; |
202 | return result; | ||
209 | break; | 203 | break; |
210 | } | 204 | } |
211 | } | 205 | } |
212 | 206 | ||
213 | if (data_vals == NULL) | 207 | if (result.config.data_vals == NULL) { |
214 | return ERROR; | 208 | result.errorcode = ERROR; |
209 | return result; | ||
210 | } | ||
215 | 211 | ||
216 | return OK; | 212 | set_thresholds(&result.config.thresholds, warn_threshold, crit_threshold); |
213 | return result; | ||
217 | } | 214 | } |
218 | 215 | ||
219 | void print_help(void) { | 216 | void print_help(void) { |