diff options
Diffstat (limited to 'plugins/check_cluster.c')
-rw-r--r-- | plugins/check_cluster.c | 255 |
1 files changed, 117 insertions, 138 deletions
diff --git a/plugins/check_cluster.c b/plugins/check_cluster.c index e1ede9f..b40c38c 100644 --- a/plugins/check_cluster.c +++ b/plugins/check_cluster.c | |||
@@ -1,92 +1,92 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * check_cluster.c - Host and Service Cluster Plugin for Monitoring | 3 | * check_cluster.c - Host and Service Cluster Plugin for Monitoring |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org) | 6 | * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org) |
7 | * Copyright (c) 2007 Monitoring Plugins Development Team | 7 | * Copyright (c) 2007-2024 Monitoring Plugins Development Team |
8 | * | 8 | * |
9 | * This program is free software: you can redistribute it and/or modify | 9 | * This program is free software: you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by | 10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation, either version 3 of the License, or | 11 | * the Free Software Foundation, either version 3 of the License, or |
12 | * (at your option) any later version. | 12 | * (at your option) any later version. |
13 | * | 13 | * |
14 | * This program is distributed in the hope that it will be useful, | 14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. | 17 | * GNU General Public License for more details. |
18 | * | 18 | * |
19 | * You should have received a copy of the GNU General Public License | 19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | * | 22 | * |
23 | *****************************************************************************/ | 23 | *****************************************************************************/ |
24 | 24 | ||
25 | const char *progname = "check_cluster"; | 25 | const char *progname = "check_cluster"; |
26 | const char *copyright = "2000-2007"; | 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 "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 | ||
33 | #define CHECK_SERVICES 1 | 33 | enum { |
34 | #define CHECK_HOSTS 2 | 34 | CHECK_SERVICES = 1, |
35 | CHECK_HOSTS = 2 | ||
36 | }; | ||
35 | 37 | ||
36 | void print_help (void); | 38 | static void print_help(void); |
37 | void print_usage (void); | 39 | void print_usage(void); |
38 | 40 | ||
39 | int total_services_ok=0; | 41 | static int total_services_ok = 0; |
40 | int total_services_warning=0; | 42 | static int total_services_warning = 0; |
41 | int total_services_unknown=0; | 43 | static int total_services_unknown = 0; |
42 | int total_services_critical=0; | 44 | static int total_services_critical = 0; |
43 | 45 | ||
44 | int total_hosts_up=0; | 46 | static int total_hosts_up = 0; |
45 | int total_hosts_down=0; | 47 | static int total_hosts_down = 0; |
46 | int total_hosts_unreachable=0; | 48 | static int total_hosts_unreachable = 0; |
47 | 49 | ||
48 | char *warn_threshold; | 50 | static char *warn_threshold; |
49 | char *crit_threshold; | 51 | static char *crit_threshold; |
50 | 52 | ||
51 | int check_type=CHECK_SERVICES; | 53 | static int check_type = CHECK_SERVICES; |
52 | 54 | ||
53 | char *data_vals=NULL; | 55 | static char *data_vals = NULL; |
54 | char *label=NULL; | 56 | static char *label = NULL; |
55 | 57 | ||
56 | int verbose=0; | 58 | static int verbose = 0; |
57 | 59 | ||
58 | int process_arguments(int,char **); | 60 | static int process_arguments(int /*argc*/, char ** /*argv*/); |
59 | 61 | ||
60 | 62 | int main(int argc, char **argv) { | |
61 | |||
62 | int main(int argc, char **argv){ | ||
63 | char *ptr; | 63 | char *ptr; |
64 | int data_val; | 64 | int data_val; |
65 | int return_code=STATE_OK; | 65 | int return_code = STATE_OK; |
66 | thresholds *thresholds = NULL; | 66 | thresholds *thresholds = NULL; |
67 | 67 | ||
68 | setlocale (LC_ALL, ""); | 68 | setlocale(LC_ALL, ""); |
69 | bindtextdomain (PACKAGE, LOCALEDIR); | 69 | bindtextdomain(PACKAGE, LOCALEDIR); |
70 | textdomain (PACKAGE); | 70 | textdomain(PACKAGE); |
71 | 71 | ||
72 | /* Parse extra opts if any */ | 72 | /* Parse extra opts if any */ |
73 | argv=np_extra_opts(&argc, argv, progname); | 73 | argv = np_extra_opts(&argc, argv, progname); |
74 | 74 | ||
75 | if(process_arguments(argc,argv)==ERROR) | 75 | if (process_arguments(argc, argv) == ERROR) |
76 | usage(_("Could not parse arguments")); | 76 | usage(_("Could not parse arguments")); |
77 | 77 | ||
78 | /* Initialize the thresholds */ | 78 | /* Initialize the thresholds */ |
79 | set_thresholds(&thresholds, warn_threshold, crit_threshold); | 79 | set_thresholds(&thresholds, warn_threshold, crit_threshold); |
80 | if(verbose) | 80 | if (verbose) |
81 | print_thresholds("check_cluster", thresholds); | 81 | print_thresholds("check_cluster", thresholds); |
82 | 82 | ||
83 | /* check the data values */ | 83 | /* check the data values */ |
84 | for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){ | 84 | for (ptr = strtok(data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) { |
85 | 85 | ||
86 | data_val=atoi(ptr); | 86 | data_val = atoi(ptr); |
87 | 87 | ||
88 | if(check_type==CHECK_SERVICES){ | 88 | if (check_type == CHECK_SERVICES) { |
89 | switch(data_val){ | 89 | switch (data_val) { |
90 | case 0: | 90 | case 0: |
91 | total_services_ok++; | 91 | total_services_ok++; |
92 | break; | 92 | break; |
@@ -101,10 +101,9 @@ int main(int argc, char **argv){ | |||
101 | break; | 101 | break; |
102 | default: | 102 | default: |
103 | break; | 103 | break; |
104 | } | 104 | } |
105 | } | 105 | } else { |
106 | else{ | 106 | switch (data_val) { |
107 | switch(data_val){ | ||
108 | case 0: | 107 | case 0: |
109 | total_hosts_up++; | 108 | total_hosts_up++; |
110 | break; | 109 | break; |
@@ -116,67 +115,54 @@ int main(int argc, char **argv){ | |||
116 | break; | 115 | break; |
117 | default: | 116 | default: |
118 | break; | 117 | break; |
119 | } | 118 | } |
120 | } | 119 | } |
121 | } | 120 | } |
122 | |||
123 | 121 | ||
124 | /* return the status of the cluster */ | 122 | /* return the status of the cluster */ |
125 | if(check_type==CHECK_SERVICES){ | 123 | if (check_type == CHECK_SERVICES) { |
126 | return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds); | 124 | return_code = get_status(total_services_warning + total_services_unknown + total_services_critical, thresholds); |
127 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", | 125 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", state_text(return_code), |
128 | state_text(return_code), (label==NULL)?"Service cluster":label, | 126 | (label == NULL) ? "Service cluster" : label, total_services_ok, total_services_warning, total_services_unknown, |
129 | total_services_ok,total_services_warning, | 127 | total_services_critical); |
130 | total_services_unknown,total_services_critical); | 128 | } else { |
131 | } | 129 | return_code = get_status(total_hosts_down + total_hosts_unreachable, thresholds); |
132 | else{ | 130 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), (label == NULL) ? "Host cluster" : label, |
133 | return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds); | 131 | total_hosts_up, total_hosts_down, total_hosts_unreachable); |
134 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", | ||
135 | state_text(return_code), (label==NULL)?"Host cluster":label, | ||
136 | total_hosts_up,total_hosts_down,total_hosts_unreachable); | ||
137 | } | 132 | } |
138 | 133 | ||
139 | return return_code; | 134 | return return_code; |
140 | } | 135 | } |
141 | 136 | ||
142 | 137 | int process_arguments(int argc, char **argv) { | |
143 | |||
144 | int process_arguments(int argc, char **argv){ | ||
145 | int c; | 138 | int c; |
146 | char *ptr; | 139 | char *ptr; |
147 | int option=0; | 140 | int option = 0; |
148 | static struct option longopts[]={ | 141 | static struct option longopts[] = {{"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, |
149 | {"data", required_argument,0,'d'}, | 142 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, |
150 | {"warning", required_argument,0,'w'}, | 143 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, |
151 | {"critical", required_argument,0,'c'}, | 144 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
152 | {"label", required_argument,0,'l'}, | 145 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; |
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 | {0,0,0,0} | ||
159 | }; | ||
160 | 146 | ||
161 | /* no options were supplied */ | 147 | /* no options were supplied */ |
162 | if(argc<2) | 148 | if (argc < 2) |
163 | return ERROR; | 149 | return ERROR; |
164 | 150 | ||
165 | while(1){ | 151 | while (1) { |
166 | 152 | ||
167 | c=getopt_long(argc,argv,"hHsvVw:c:d:l:",longopts,&option); | 153 | c = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option); |
168 | 154 | ||
169 | if(c==-1 || c==EOF || c==1) | 155 | if (c == -1 || c == EOF || c == 1) |
170 | break; | 156 | break; |
171 | 157 | ||
172 | switch(c){ | 158 | switch (c) { |
173 | 159 | ||
174 | case 'h': /* host cluster */ | 160 | case 'h': /* host cluster */ |
175 | check_type=CHECK_HOSTS; | 161 | check_type = CHECK_HOSTS; |
176 | break; | 162 | break; |
177 | 163 | ||
178 | case 's': /* service cluster */ | 164 | case 's': /* service cluster */ |
179 | check_type=CHECK_SERVICES; | 165 | check_type = CHECK_SERVICES; |
180 | break; | 166 | break; |
181 | 167 | ||
182 | case 'w': /* warning threshold */ | 168 | case 'w': /* warning threshold */ |
@@ -188,20 +174,20 @@ int process_arguments(int argc, char **argv){ | |||
188 | break; | 174 | break; |
189 | 175 | ||
190 | case 'd': /* data values */ | 176 | case 'd': /* data values */ |
191 | data_vals=(char *)strdup(optarg); | 177 | data_vals = (char *)strdup(optarg); |
192 | /* validate data */ | 178 | /* validate data */ |
193 | for (ptr=data_vals;ptr!=NULL;ptr+=2){ | 179 | for (ptr = data_vals; ptr != NULL; ptr += 2) { |
194 | if (ptr[0]<'0' || ptr[0]>'3') | 180 | if (ptr[0] < '0' || ptr[0] > '3') |
195 | return ERROR; | 181 | return ERROR; |
196 | if (ptr[1]=='\0') | 182 | if (ptr[1] == '\0') |
197 | break; | 183 | break; |
198 | if (ptr[1]!=',') | 184 | if (ptr[1] != ',') |
199 | return ERROR; | 185 | return ERROR; |
200 | } | 186 | } |
201 | break; | 187 | break; |
202 | 188 | ||
203 | case 'l': /* text label */ | 189 | case 'l': /* text label */ |
204 | label=(char *)strdup(optarg); | 190 | label = (char *)strdup(optarg); |
205 | break; | 191 | break; |
206 | 192 | ||
207 | case 'v': /* verbose */ | 193 | case 'v': /* verbose */ |
@@ -209,8 +195,8 @@ int process_arguments(int argc, char **argv){ | |||
209 | break; | 195 | break; |
210 | 196 | ||
211 | case 'V': /* version */ | 197 | case 'V': /* version */ |
212 | print_revision (progname, NP_VERSION); | 198 | print_revision(progname, NP_VERSION); |
213 | exit (STATE_UNKNOWN); | 199 | exit(STATE_UNKNOWN); |
214 | break; | 200 | break; |
215 | 201 | ||
216 | case 'H': /* help */ | 202 | case 'H': /* help */ |
@@ -221,20 +207,18 @@ int process_arguments(int argc, char **argv){ | |||
221 | default: | 207 | default: |
222 | return ERROR; | 208 | return ERROR; |
223 | break; | 209 | break; |
224 | } | 210 | } |
225 | } | 211 | } |
226 | 212 | ||
227 | if(data_vals==NULL) | 213 | if (data_vals == NULL) |
228 | return ERROR; | 214 | return ERROR; |
229 | 215 | ||
230 | return OK; | 216 | return OK; |
231 | } | 217 | } |
232 | 218 | ||
233 | void | 219 | void print_help(void) { |
234 | print_help(void) | ||
235 | { | ||
236 | print_revision(progname, NP_VERSION); | 220 | print_revision(progname, NP_VERSION); |
237 | printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n"); | 221 | printf("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n"); |
238 | printf(COPYRIGHT, copyright, email); | 222 | printf(COPYRIGHT, copyright, email); |
239 | 223 | ||
240 | printf(_("Host/Service Cluster Plugin for Monitoring")); | 224 | printf(_("Host/Service Cluster Plugin for Monitoring")); |
@@ -245,21 +229,21 @@ print_help(void) | |||
245 | printf("\n"); | 229 | printf("\n"); |
246 | printf("%s\n", _("Options:")); | 230 | printf("%s\n", _("Options:")); |
247 | printf(UT_EXTRA_OPTS); | 231 | printf(UT_EXTRA_OPTS); |
248 | printf (" %s\n", "-s, --service"); | 232 | printf(" %s\n", "-s, --service"); |
249 | printf (" %s\n", _("Check service cluster status")); | 233 | printf(" %s\n", _("Check service cluster status")); |
250 | printf (" %s\n", "-h, --host"); | 234 | printf(" %s\n", "-h, --host"); |
251 | printf (" %s\n", _("Check host cluster status")); | 235 | printf(" %s\n", _("Check host cluster status")); |
252 | printf (" %s\n", "-l, --label=STRING"); | 236 | printf(" %s\n", "-l, --label=STRING"); |
253 | printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")")); | 237 | printf(" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")")); |
254 | printf (" %s\n", "-w, --warning=THRESHOLD"); | 238 | printf(" %s\n", "-w, --warning=THRESHOLD"); |
255 | printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a")); | 239 | printf(" %s\n", _("Specifies the range of hosts or services in cluster that must be in a")); |
256 | printf (" %s\n", _("non-OK state in order to return a WARNING status level")); | 240 | printf(" %s\n", _("non-OK state in order to return a WARNING status level")); |
257 | printf (" %s\n", "-c, --critical=THRESHOLD"); | 241 | printf(" %s\n", "-c, --critical=THRESHOLD"); |
258 | printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a")); | 242 | printf(" %s\n", _("Specifies the range of hosts or services in cluster that must be in a")); |
259 | printf (" %s\n", _("non-OK state in order to return a CRITICAL status level")); | 243 | printf(" %s\n", _("non-OK state in order to return a CRITICAL status level")); |
260 | printf (" %s\n", "-d, --data=LIST"); | 244 | printf(" %s\n", "-d, --data=LIST"); |
261 | printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by")); | 245 | printf(" %s\n", _("The status codes of the hosts or services in the cluster, separated by")); |
262 | printf (" %s\n", _("commas")); | 246 | printf(" %s\n", _("commas")); |
263 | 247 | ||
264 | printf(UT_VERBOSE); | 248 | printf(UT_VERBOSE); |
265 | 249 | ||
@@ -267,23 +251,18 @@ print_help(void) | |||
267 | printf("%s\n", _("Notes:")); | 251 | printf("%s\n", _("Notes:")); |
268 | printf(UT_THRESHOLDS_NOTES); | 252 | printf(UT_THRESHOLDS_NOTES); |
269 | 253 | ||
270 | printf ("\n"); | 254 | printf("\n"); |
271 | printf ("%s\n", _("Examples:")); | 255 | printf("%s\n", _("Examples:")); |
272 | printf (" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:"); | 256 | printf(" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:"); |
273 | printf (" %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK") ); | 257 | printf(" %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK")); |
274 | printf (" %s\n", _("state.") ); | 258 | printf(" %s\n", _("state.")); |
275 | 259 | ||
276 | printf(UT_SUPPORT); | 260 | printf(UT_SUPPORT); |
277 | } | 261 | } |
278 | 262 | ||
279 | 263 | void print_usage(void) { | |
280 | void | ||
281 | print_usage(void) | ||
282 | { | ||
283 | 264 | ||
284 | printf("%s\n", _("Usage:")); | 265 | printf("%s\n", _("Usage:")); |
285 | printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname); | 266 | printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname); |
286 | printf("[-w threshold] [-c threshold] [-v] [--help]\n"); | 267 | printf("[-w threshold] [-c threshold] [-v] [--help]\n"); |
287 | |||
288 | } | 268 | } |
289 | |||