04919e0de18cf2f505779a442d8de070ad10e71a
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 * Last Modified: $Date$
10 *
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * $Id$
26 *
27 *****************************************************************************/
29 const char *progname = "check_cluster";
30 const char *revision = "$Revision$";
31 const char *copyright = "2000-2007";
32 const char *email = "nagiosplug-devel@lists.sourceforge.net";
34 #include "common.h"
35 #include "utils.h"
36 #include "utils_base.h"
38 #define CHECK_SERVICES 1
39 #define CHECK_HOSTS 2
41 void print_help (void);
42 void print_usage (void);
44 int total_services_ok=0;
45 int total_services_warning=0;
46 int total_services_unknown=0;
47 int total_services_critical=0;
49 int total_hosts_up=0;
50 int total_hosts_down=0;
51 int total_hosts_unreachable=0;
53 char *warn_threshold;
54 char *crit_threshold;
56 int check_type=CHECK_SERVICES;
58 char *data_vals=NULL;
59 char *label=NULL;
61 int verbose=0;
63 int process_arguments(int,char **);
67 int main(int argc, char **argv){
68 char *ptr;
69 int data_val;
70 int return_code=STATE_OK;
71 thresholds *thresholds = NULL;
73 setlocale (LC_ALL, "");
74 bindtextdomain (PACKAGE, LOCALEDIR);
75 textdomain (PACKAGE);
77 /* Parse extra opts if any */
78 argv=np_extra_opts(&argc, argv, progname);
80 if(process_arguments(argc,argv)==ERROR)
81 usage(_("Could not parse arguments"));
83 /* Initialize the thresholds */
84 set_thresholds(&thresholds, warn_threshold, crit_threshold);
85 if(verbose)
86 print_thresholds("check_cluster", thresholds);
88 /* check the data values */
89 for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
91 data_val=atoi(ptr);
93 if(check_type==CHECK_SERVICES){
94 switch(data_val){
95 case 0:
96 total_services_ok++;
97 break;
98 case 1:
99 total_services_warning++;
100 break;
101 case 2:
102 total_services_critical++;
103 break;
104 case 3:
105 total_services_unknown++;
106 break;
107 default:
108 break;
109 }
110 }
111 else{
112 switch(data_val){
113 case 0:
114 total_hosts_up++;
115 break;
116 case 1:
117 total_hosts_down++;
118 break;
119 case 2:
120 total_hosts_unreachable++;
121 break;
122 default:
123 break;
124 }
125 }
126 }
129 /* return the status of the cluster */
130 if(check_type==CHECK_SERVICES){
131 return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
132 printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
133 state_text(return_code), (label==NULL)?"Service cluster":label,
134 total_services_ok,total_services_warning,
135 total_services_unknown,total_services_critical);
136 }
137 else{
138 return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
139 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
140 state_text(return_code), (label==NULL)?"Host cluster":label,
141 total_hosts_up,total_hosts_down,total_hosts_unreachable);
142 }
144 return return_code;
145 }
149 int process_arguments(int argc, char **argv){
150 int c;
151 int option=0;
152 static struct option longopts[]={
153 {"data", required_argument,0,'d'},
154 {"warning", required_argument,0,'w'},
155 {"critical", required_argument,0,'c'},
156 {"label", required_argument,0,'l'},
157 {"host", no_argument, 0,'h'},
158 {"service", no_argument, 0,'s'},
159 {"verbose", no_argument, 0,'v'},
160 {"version", no_argument, 0,'V'},
161 {"help", no_argument, 0,'H'},
162 {0,0,0,0}
163 };
165 /* no options were supplied */
166 if(argc<2)
167 return ERROR;
169 while(1){
171 c=getopt_long(argc,argv,"hHsvVw:c:d:l:",longopts,&option);
173 if(c==-1 || c==EOF || c==1)
174 break;
176 switch(c){
178 case 'h': /* host cluster */
179 check_type=CHECK_HOSTS;
180 break;
182 case 's': /* service cluster */
183 check_type=CHECK_SERVICES;
184 break;
186 case 'w': /* warning threshold */
187 warn_threshold = strdup(optarg);
188 break;
190 case 'c': /* warning threshold */
191 crit_threshold = strdup(optarg);
192 break;
194 case 'd': /* data values */
195 data_vals=(char *)strdup(optarg);
196 break;
198 case 'l': /* text label */
199 label=(char *)strdup(optarg);
200 break;
202 case 'v': /* verbose */
203 verbose++;
204 break;
206 case 'V': /* version */
207 print_revision (progname, revision);
208 exit (STATE_OK);
209 break;
211 case 'H': /* help */
212 print_help();
213 exit(STATE_UNKNOWN);
214 break;
216 default:
217 return ERROR;
218 break;
219 }
220 }
222 if(data_vals==NULL)
223 return ERROR;
225 return OK;
226 }
228 void
229 print_help(void)
230 {
231 print_revision(progname, revision);
232 printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
233 printf(COPYRIGHT, copyright, email);
235 printf(_("Host/Service Cluster Plugin for Nagios 2"));
236 printf("\n\n");
238 print_usage();
240 printf("\n");
241 printf("%s\n", _("Options:"));
242 printf(_(UT_EXTRA_OPTS));
243 printf (" %s\n", "-s, --service");
244 printf (" %s\n", _("Check service cluster status"));
245 printf (" %s\n", "-h, --host");
246 printf (" %s\n", _("Check host cluster status"));
247 printf (" %s\n", "-l, --label=STRING");
248 printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
249 printf (" %s\n", "-w, --warning=THRESHOLD");
250 printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
251 printf (" %s\n", _("non-OK state in order to return a WARNING status level"));
252 printf (" %s\n", "-c, --critical=THRESHOLD");
253 printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
254 printf (" %s\n", _("non-OK state in order to return a CRITICAL status level"));
255 printf (" %s\n", "-d, --data=LIST");
256 printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
257 printf (" %s\n", _("commas"));
259 printf(_(UT_VERBOSE));
261 printf("\n");
262 printf("%s\n", _("Notes:"));
263 printf(_(UT_THRESHOLDS_NOTES));
264 #ifdef NP_EXTRA_OPTS
265 printf ("\n");
266 printf (_(UT_EXTRA_OPTS_NOTES));
267 #endif
269 printf(_(UT_SUPPORT));
270 }
273 void
274 print_usage(void)
275 {
277 printf(_("Usage:"));
278 printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
279 printf("[-w threshold] [-c threshold] [-v] [--help]\n");
281 }