1 /*****************************************************************************
3 * check_cluster.c - Host and Service Cluster Plugin for Monitoring
6 * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2007-2024 Monitoring Plugins Development Team
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
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
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/>.
23 *****************************************************************************/
25 const char *progname
= "check_cluster";
26 const char *copyright
= "2000-2024";
27 const char *email
= "devel@monitoring-plugins.org";
31 #include "utils_base.h"
38 static void print_help(void);
39 void print_usage(void);
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;
46 static int total_hosts_up
= 0;
47 static int total_hosts_down
= 0;
48 static int total_hosts_unreachable
= 0;
50 static char *warn_threshold
;
51 static char *crit_threshold
;
53 static int check_type
= CHECK_SERVICES
;
55 static char *data_vals
= NULL
;
56 static char *label
= NULL
;
58 static int verbose
= 0;
60 static int process_arguments(int /*argc*/, char ** /*argv*/);
62 int main(int argc
, char **argv
) {
65 int return_code
= STATE_OK
;
66 thresholds
*thresholds
= NULL
;
68 setlocale(LC_ALL
, "");
69 bindtextdomain(PACKAGE
, LOCALEDIR
);
72 /* Parse extra opts if any */
73 argv
= np_extra_opts(&argc
, argv
, progname
);
75 if (process_arguments(argc
, argv
) == ERROR
)
76 usage(_("Could not parse arguments"));
78 /* Initialize the thresholds */
79 set_thresholds(&thresholds
, warn_threshold
, crit_threshold
);
81 print_thresholds("check_cluster", thresholds
);
83 /* check the data values */
84 for (ptr
= strtok(data_vals
, ","); ptr
!= NULL
; ptr
= strtok(NULL
, ",")) {
88 if (check_type
== CHECK_SERVICES
) {
94 total_services_warning
++;
97 total_services_critical
++;
100 total_services_unknown
++;
114 total_hosts_unreachable
++;
122 /* return the status of the cluster */
123 if (check_type
== CHECK_SERVICES
) {
124 return_code
= get_status(total_services_warning
+ total_services_unknown
+ total_services_critical
, thresholds
);
125 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
,
127 total_services_critical
);
129 return_code
= get_status(total_hosts_down
+ total_hosts_unreachable
, thresholds
);
130 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code
), (label
== NULL
) ? "Host cluster" : label
,
131 total_hosts_up
, total_hosts_down
, total_hosts_unreachable
);
137 int process_arguments(int argc
, char **argv
) {
141 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'},
143 {"host", no_argument
, 0, 'h'}, {"service", no_argument
, 0, 's'},
144 {"verbose", no_argument
, 0, 'v'}, {"version", no_argument
, 0, 'V'},
145 {"help", no_argument
, 0, 'H'}, {0, 0, 0, 0}};
147 /* no options were supplied */
153 c
= getopt_long(argc
, argv
, "hHsvVw:c:d:l:", longopts
, &option
);
155 if (c
== -1 || c
== EOF
|| c
== 1)
160 case 'h': /* host cluster */
161 check_type
= CHECK_HOSTS
;
164 case 's': /* service cluster */
165 check_type
= CHECK_SERVICES
;
168 case 'w': /* warning threshold */
169 warn_threshold
= strdup(optarg
);
172 case 'c': /* warning threshold */
173 crit_threshold
= strdup(optarg
);
176 case 'd': /* data values */
177 data_vals
= (char *)strdup(optarg
);
179 for (ptr
= data_vals
; ptr
!= NULL
; ptr
+= 2) {
180 if (ptr
[0] < '0' || ptr
[0] > '3')
189 case 'l': /* text label */
190 label
= (char *)strdup(optarg
);
193 case 'v': /* verbose */
197 case 'V': /* version */
198 print_revision(progname
, NP_VERSION
);
213 if (data_vals
== NULL
)
219 void print_help(void) {
220 print_revision(progname
, NP_VERSION
);
221 printf("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
222 printf(COPYRIGHT
, copyright
, email
);
224 printf(_("Host/Service Cluster Plugin for Monitoring"));
230 printf("%s\n", _("Options:"));
231 printf(UT_EXTRA_OPTS
);
232 printf(" %s\n", "-s, --service");
233 printf(" %s\n", _("Check service cluster status"));
234 printf(" %s\n", "-h, --host");
235 printf(" %s\n", _("Check host cluster status"));
236 printf(" %s\n", "-l, --label=STRING");
237 printf(" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
238 printf(" %s\n", "-w, --warning=THRESHOLD");
239 printf(" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
240 printf(" %s\n", _("non-OK state in order to return a WARNING status level"));
241 printf(" %s\n", "-c, --critical=THRESHOLD");
242 printf(" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
243 printf(" %s\n", _("non-OK state in order to return a CRITICAL status level"));
244 printf(" %s\n", "-d, --data=LIST");
245 printf(" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
246 printf(" %s\n", _("commas"));
251 printf("%s\n", _("Notes:"));
252 printf(UT_THRESHOLDS_NOTES
);
255 printf("%s\n", _("Examples:"));
256 printf(" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:");
257 printf(" %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK"));
258 printf(" %s\n", _("state."));
263 void print_usage(void) {
265 printf("%s\n", _("Usage:"));
266 printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname
);
267 printf("[-w threshold] [-c threshold] [-v] [--help]\n");