Merge pull request #2044 from RincewindsHat/fix/fedora-rpm-build
[monitoring-plugins.git] / plugins / check_cluster.c
blobb40c38c7ebbe8e5a421e61321ef295c462884481
1 /*****************************************************************************
3 * check_cluster.c - Host and Service Cluster Plugin for Monitoring
5 * License: GPL
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";
29 #include "common.h"
30 #include "utils.h"
31 #include "utils_base.h"
33 enum {
34 CHECK_SERVICES = 1,
35 CHECK_HOSTS = 2
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) {
63 char *ptr;
64 int data_val;
65 int return_code = STATE_OK;
66 thresholds *thresholds = NULL;
68 setlocale(LC_ALL, "");
69 bindtextdomain(PACKAGE, LOCALEDIR);
70 textdomain(PACKAGE);
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);
80 if (verbose)
81 print_thresholds("check_cluster", thresholds);
83 /* check the data values */
84 for (ptr = strtok(data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
86 data_val = atoi(ptr);
88 if (check_type == CHECK_SERVICES) {
89 switch (data_val) {
90 case 0:
91 total_services_ok++;
92 break;
93 case 1:
94 total_services_warning++;
95 break;
96 case 2:
97 total_services_critical++;
98 break;
99 case 3:
100 total_services_unknown++;
101 break;
102 default:
103 break;
105 } else {
106 switch (data_val) {
107 case 0:
108 total_hosts_up++;
109 break;
110 case 1:
111 total_hosts_down++;
112 break;
113 case 2:
114 total_hosts_unreachable++;
115 break;
116 default:
117 break;
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);
128 } else {
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);
134 return return_code;
137 int 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'},
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 */
148 if (argc < 2)
149 return ERROR;
151 while (1) {
153 c = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option);
155 if (c == -1 || c == EOF || c == 1)
156 break;
158 switch (c) {
160 case 'h': /* host cluster */
161 check_type = CHECK_HOSTS;
162 break;
164 case 's': /* service cluster */
165 check_type = CHECK_SERVICES;
166 break;
168 case 'w': /* warning threshold */
169 warn_threshold = strdup(optarg);
170 break;
172 case 'c': /* warning threshold */
173 crit_threshold = strdup(optarg);
174 break;
176 case 'd': /* data values */
177 data_vals = (char *)strdup(optarg);
178 /* validate data */
179 for (ptr = data_vals; ptr != NULL; ptr += 2) {
180 if (ptr[0] < '0' || ptr[0] > '3')
181 return ERROR;
182 if (ptr[1] == '\0')
183 break;
184 if (ptr[1] != ',')
185 return ERROR;
187 break;
189 case 'l': /* text label */
190 label = (char *)strdup(optarg);
191 break;
193 case 'v': /* verbose */
194 verbose++;
195 break;
197 case 'V': /* version */
198 print_revision(progname, NP_VERSION);
199 exit(STATE_UNKNOWN);
200 break;
202 case 'H': /* help */
203 print_help();
204 exit(STATE_UNKNOWN);
205 break;
207 default:
208 return ERROR;
209 break;
213 if (data_vals == NULL)
214 return ERROR;
216 return OK;
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"));
225 printf("\n\n");
227 print_usage();
229 printf("\n");
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"));
248 printf(UT_VERBOSE);
250 printf("\n");
251 printf("%s\n", _("Notes:"));
252 printf(UT_THRESHOLDS_NOTES);
254 printf("\n");
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."));
260 printf(UT_SUPPORT);
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");