Fix for SSL Versioning when multiple options are used.
[monitoring-plugins.git] / plugins / check_cluster.c
blobb046c14e96dacf15bbc3e3d954aa353d6614cf3a
1 /*****************************************************************************
2 *
3 * check_cluster.c - Host and Service Cluster Plugin for Nagios 2.x
4 *
5 * License: GPL
6 * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2007 Nagios Plugins Development Team
8 *
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-2007";
27 const char *email = "devel@nagios-plugins.org";
29 #include "common.h"
30 #include "utils.h"
31 #include "utils_base.h"
33 #define CHECK_SERVICES 1
34 #define CHECK_HOSTS 2
36 void print_help (void);
37 void print_usage (void);
39 int total_services_ok=0;
40 int total_services_warning=0;
41 int total_services_unknown=0;
42 int total_services_critical=0;
44 int total_hosts_up=0;
45 int total_hosts_down=0;
46 int total_hosts_unreachable=0;
48 char *warn_threshold;
49 char *crit_threshold;
51 int check_type=CHECK_SERVICES;
53 char *data_vals=NULL;
54 char *label=NULL;
56 int verbose=0;
58 int process_arguments(int,char **);
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;
106 else{
107 switch(data_val){
108 case 0:
109 total_hosts_up++;
110 break;
111 case 1:
112 total_hosts_down++;
113 break;
114 case 2:
115 total_hosts_unreachable++;
116 break;
117 default:
118 break;
124 /* return the status of the cluster */
125 if(check_type==CHECK_SERVICES){
126 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",
128 state_text(return_code), (label==NULL)?"Service cluster":label,
129 total_services_ok,total_services_warning,
130 total_services_unknown,total_services_critical);
132 else{
133 return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
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);
139 return return_code;
144 int process_arguments(int argc, char **argv){
145 int c;
146 int option=0;
147 static struct option longopts[]={
148 {"data", required_argument,0,'d'},
149 {"warning", required_argument,0,'w'},
150 {"critical", required_argument,0,'c'},
151 {"label", required_argument,0,'l'},
152 {"host", no_argument, 0,'h'},
153 {"service", no_argument, 0,'s'},
154 {"verbose", no_argument, 0,'v'},
155 {"version", no_argument, 0,'V'},
156 {"help", no_argument, 0,'H'},
157 {0,0,0,0}
160 /* no options were supplied */
161 if(argc<2)
162 return ERROR;
164 while(1){
166 c=getopt_long(argc,argv,"hHsvVw:c:d:l:",longopts,&option);
168 if(c==-1 || c==EOF || c==1)
169 break;
171 switch(c){
173 case 'h': /* host cluster */
174 check_type=CHECK_HOSTS;
175 break;
177 case 's': /* service cluster */
178 check_type=CHECK_SERVICES;
179 break;
181 case 'w': /* warning threshold */
182 warn_threshold = strdup(optarg);
183 break;
185 case 'c': /* warning threshold */
186 crit_threshold = strdup(optarg);
187 break;
189 case 'd': /* data values */
190 data_vals=(char *)strdup(optarg);
191 break;
193 case 'l': /* text label */
194 label=(char *)strdup(optarg);
195 break;
197 case 'v': /* verbose */
198 verbose++;
199 break;
201 case 'V': /* version */
202 print_revision (progname, NP_VERSION);
203 exit (STATE_OK);
204 break;
206 case 'H': /* help */
207 print_help();
208 exit(STATE_UNKNOWN);
209 break;
211 default:
212 return ERROR;
213 break;
217 if(data_vals==NULL)
218 return ERROR;
220 return OK;
223 void
224 print_help(void)
226 print_revision(progname, NP_VERSION);
227 printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
228 printf(COPYRIGHT, copyright, email);
230 printf(_("Host/Service Cluster Plugin for Nagios 2"));
231 printf("\n\n");
233 print_usage();
235 printf("\n");
236 printf("%s\n", _("Options:"));
237 printf(UT_EXTRA_OPTS);
238 printf (" %s\n", "-s, --service");
239 printf (" %s\n", _("Check service cluster status"));
240 printf (" %s\n", "-h, --host");
241 printf (" %s\n", _("Check host cluster status"));
242 printf (" %s\n", "-l, --label=STRING");
243 printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
244 printf (" %s\n", "-w, --warning=THRESHOLD");
245 printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
246 printf (" %s\n", _("non-OK state in order to return a WARNING status level"));
247 printf (" %s\n", "-c, --critical=THRESHOLD");
248 printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
249 printf (" %s\n", _("non-OK state in order to return a CRITICAL status level"));
250 printf (" %s\n", "-d, --data=LIST");
251 printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
252 printf (" %s\n", _("commas"));
254 printf(UT_VERBOSE);
256 printf("\n");
257 printf("%s\n", _("Notes:"));
258 printf(UT_THRESHOLDS_NOTES);
260 printf ("\n");
261 printf ("%s\n", _("Examples:"));
262 printf (" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:");
263 printf (" %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK") );
264 printf (" %s\n", _("state.") );
266 printf(UT_SUPPORT);
270 void
271 print_usage(void)
274 printf("%s\n", _("Usage:"));
275 printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
276 printf("[-w threshold] [-c threshold] [-v] [--help]\n");