summaryrefslogtreecommitdiffstats
path: root/plugins/check_cluster.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_cluster.c')
-rw-r--r--plugins/check_cluster.c312
1 files changed, 144 insertions, 168 deletions
diff --git a/plugins/check_cluster.c b/plugins/check_cluster.c
index e1ede9f7..9b695499 100644
--- a/plugins/check_cluster.c
+++ b/plugins/check_cluster.c
@@ -1,92 +1,82 @@
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
25const char *progname = "check_cluster"; 25const char *progname = "check_cluster";
26const char *copyright = "2000-2007"; 26const char *copyright = "2000-2024";
27const char *email = "devel@monitoring-plugins.org"; 27const 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#include "check_cluster.d/config.h"
32 33
33#define CHECK_SERVICES 1 34static void print_help(void);
34#define CHECK_HOSTS 2 35void print_usage(void);
35 36
36void print_help (void); 37static int verbose = 0;
37void print_usage (void);
38 38
39int total_services_ok=0; 39typedef struct {
40int total_services_warning=0; 40 int errorcode;
41int total_services_unknown=0; 41 check_cluster_config config;
42int total_services_critical=0; 42} check_cluster_config_wrapper;
43static check_cluster_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
43 44
44int total_hosts_up=0; 45int main(int argc, char **argv) {
45int total_hosts_down=0; 46 setlocale(LC_ALL, "");
46int total_hosts_unreachable=0; 47 bindtextdomain(PACKAGE, LOCALEDIR);
47 48 textdomain(PACKAGE);
48char *warn_threshold;
49char *crit_threshold;
50
51int check_type=CHECK_SERVICES;
52
53char *data_vals=NULL;
54char *label=NULL;
55
56int verbose=0;
57
58int process_arguments(int,char **);
59
60
61
62int 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, "");
69 bindtextdomain (PACKAGE, LOCALEDIR);
70 textdomain (PACKAGE);
71 49
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++;
92 break; 82 break;
@@ -101,10 +91,9 @@ int main(int argc, char **argv){
101 break; 91 break;
102 default: 92 default:
103 break; 93 break;
104 } 94 }
105 } 95 } else {
106 else{ 96 switch (data_val) {
107 switch(data_val){
108 case 0: 97 case 0:
109 total_hosts_up++; 98 total_hosts_up++;
110 break; 99 break;
@@ -116,125 +105,117 @@ int main(int argc, char **argv){
116 break; 105 break;
117 default: 106 default:
118 break; 107 break;
119 } 108 }
120 } 109 }
121 } 110 }
122
123 111
112 int return_code = STATE_OK;
124 /* return the status of the cluster */ 113 /* return the status of the cluster */
125 if(check_type==CHECK_SERVICES){ 114 if (config.check_type == CHECK_SERVICES) {
126 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);
127 printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", 116 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, 117 (config.label == NULL) ? "Service cluster" : config.label, total_services_ok, total_services_warning, total_services_unknown,
129 total_services_ok,total_services_warning, 118 total_services_critical);
130 total_services_unknown,total_services_critical); 119 } else {
131 } 120 return_code = get_status(total_hosts_down + total_hosts_unreachable, config.thresholds);
132 else{ 121 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code),
133 return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds); 122 (config.label == NULL) ? "Host cluster" : config.label, 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 } 123 }
138 124
139 return return_code; 125 exit(return_code);
140} 126}
141 127
128check_cluster_config_wrapper process_arguments(int argc, char **argv) {
129 static struct option longopts[] = {{"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'},
130 {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'},
131 {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'},
132 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'},
133 {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}};
142 134
143 135 check_cluster_config_wrapper result = {
144int process_arguments(int argc, char **argv){ 136 .errorcode = OK,
145 int c; 137 .config = check_cluster_config_init(),
146 char *ptr;
147 int option=0;
148 static struct option longopts[]={
149 {"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 {0,0,0,0}
159 }; 138 };
160 139
161 /* no options were supplied */ 140 /* no options were supplied */
162 if(argc<2) 141 if (argc < 2) {
163 return ERROR; 142 result.errorcode = ERROR;
164 143 return result;
165 while(1){ 144 }
166 145
167 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);
168 151
169 if(c==-1 || c==EOF || c==1) 152 if (option_index == -1 || option_index == EOF || option_index == 1) {
170 break; 153 break;
154 }
171 155
172 switch(c){ 156 switch (option_index) {
173
174 case 'h': /* host cluster */ 157 case 'h': /* host cluster */
175 check_type=CHECK_HOSTS; 158 result.config.check_type = CHECK_HOSTS;
176 break; 159 break;
177
178 case 's': /* service cluster */ 160 case 's': /* service cluster */
179 check_type=CHECK_SERVICES; 161 result.config.check_type = CHECK_SERVICES;
180 break; 162 break;
181
182 case 'w': /* warning threshold */ 163 case 'w': /* warning threshold */
183 warn_threshold = strdup(optarg); 164 warn_threshold = strdup(optarg);
184 break; 165 break;
185
186 case 'c': /* warning threshold */ 166 case 'c': /* warning threshold */
187 crit_threshold = strdup(optarg); 167 crit_threshold = strdup(optarg);
188 break; 168 break;
189
190 case 'd': /* data values */ 169 case 'd': /* data values */
191 data_vals=(char *)strdup(optarg); 170 result.config.data_vals = strdup(optarg);
192 /* validate data */ 171 /* validate data */
193 for (ptr=data_vals;ptr!=NULL;ptr+=2){ 172 for (char *ptr = result.config.data_vals; ptr != NULL; ptr += 2) {
194 if (ptr[0]<'0' || ptr[0]>'3') 173 if (ptr[0] < '0' || ptr[0] > '3') {
195 return ERROR; 174 result.errorcode = ERROR;
196 if (ptr[1]=='\0') 175 return result;
176 }
177 if (ptr[1] == '\0') {
197 break; 178 break;
198 if (ptr[1]!=',') 179 }
199 return ERROR; 180 if (ptr[1] != ',') {
181 result.errorcode = ERROR;
182 return result;
183 }
200 } 184 }
201 break; 185 break;
202
203 case 'l': /* text label */ 186 case 'l': /* text label */
204 label=(char *)strdup(optarg); 187 result.config.label = strdup(optarg);
205 break; 188 break;
206
207 case 'v': /* verbose */ 189 case 'v': /* verbose */
208 verbose++; 190 verbose++;
209 break; 191 break;
210
211 case 'V': /* version */ 192 case 'V': /* version */
212 print_revision (progname, NP_VERSION); 193 print_revision(progname, NP_VERSION);
213 exit (STATE_UNKNOWN); 194 exit(STATE_UNKNOWN);
214 break; 195 break;
215
216 case 'H': /* help */ 196 case 'H': /* help */
217 print_help(); 197 print_help();
218 exit(STATE_UNKNOWN); 198 exit(STATE_UNKNOWN);
219 break; 199 break;
220
221 default: 200 default:
222 return ERROR; 201 result.errorcode = ERROR;
202 return result;
223 break; 203 break;
224 } 204 }
225 } 205 }
226 206
227 if(data_vals==NULL) 207 if (result.config.data_vals == NULL) {
228 return ERROR; 208 result.errorcode = ERROR;
209 return result;
210 }
229 211
230 return OK; 212 set_thresholds(&result.config.thresholds, warn_threshold, crit_threshold);
213 return result;
231} 214}
232 215
233void 216void print_help(void) {
234print_help(void)
235{
236 print_revision(progname, NP_VERSION); 217 print_revision(progname, NP_VERSION);
237 printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n"); 218 printf("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
238 printf(COPYRIGHT, copyright, email); 219 printf(COPYRIGHT, copyright, email);
239 220
240 printf(_("Host/Service Cluster Plugin for Monitoring")); 221 printf(_("Host/Service Cluster Plugin for Monitoring"));
@@ -245,21 +226,21 @@ print_help(void)
245 printf("\n"); 226 printf("\n");
246 printf("%s\n", _("Options:")); 227 printf("%s\n", _("Options:"));
247 printf(UT_EXTRA_OPTS); 228 printf(UT_EXTRA_OPTS);
248 printf (" %s\n", "-s, --service"); 229 printf(" %s\n", "-s, --service");
249 printf (" %s\n", _("Check service cluster status")); 230 printf(" %s\n", _("Check service cluster status"));
250 printf (" %s\n", "-h, --host"); 231 printf(" %s\n", "-h, --host");
251 printf (" %s\n", _("Check host cluster status")); 232 printf(" %s\n", _("Check host cluster status"));
252 printf (" %s\n", "-l, --label=STRING"); 233 printf(" %s\n", "-l, --label=STRING");
253 printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")")); 234 printf(" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
254 printf (" %s\n", "-w, --warning=THRESHOLD"); 235 printf(" %s\n", "-w, --warning=THRESHOLD");
255 printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a")); 236 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")); 237 printf(" %s\n", _("non-OK state in order to return a WARNING status level"));
257 printf (" %s\n", "-c, --critical=THRESHOLD"); 238 printf(" %s\n", "-c, --critical=THRESHOLD");
258 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"));
259 printf (" %s\n", _("non-OK state in order to return a CRITICAL status level")); 240 printf(" %s\n", _("non-OK state in order to return a CRITICAL status level"));
260 printf (" %s\n", "-d, --data=LIST"); 241 printf(" %s\n", "-d, --data=LIST");
261 printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by")); 242 printf(" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
262 printf (" %s\n", _("commas")); 243 printf(" %s\n", _("commas"));
263 244
264 printf(UT_VERBOSE); 245 printf(UT_VERBOSE);
265 246
@@ -267,23 +248,18 @@ print_help(void)
267 printf("%s\n", _("Notes:")); 248 printf("%s\n", _("Notes:"));
268 printf(UT_THRESHOLDS_NOTES); 249 printf(UT_THRESHOLDS_NOTES);
269 250
270 printf ("\n"); 251 printf("\n");
271 printf ("%s\n", _("Examples:")); 252 printf("%s\n", _("Examples:"));
272 printf (" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:"); 253 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") ); 254 printf(" %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK"));
274 printf (" %s\n", _("state.") ); 255 printf(" %s\n", _("state."));
275 256
276 printf(UT_SUPPORT); 257 printf(UT_SUPPORT);
277} 258}
278 259
279 260void print_usage(void) {
280void
281print_usage(void)
282{
283 261
284 printf("%s\n", _("Usage:")); 262 printf("%s\n", _("Usage:"));
285 printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname); 263 printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
286 printf("[-w threshold] [-c threshold] [-v] [--help]\n"); 264 printf("[-w threshold] [-c threshold] [-v] [--help]\n");
287
288} 265}
289