1 /*****************************************************************************
2 *
3 * CHECK_CLUSTER2.C - Host and Service Cluster Plugin for Nagios 2.x
4 *
5 * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
6 * Copyright (c) 2007 nagios-plugins team
7 * License: GPL
8 * Last Modified: $Date$
9 *
10 * License Information:
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 2 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, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * $Id$
27 *
28 ******************************************************************************/
30 const char *progname = "check_cluster";
31 const char *revision = "$Revision$";
32 const char *copyright = "2007";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 #include "common.h"
36 #include "utils.h"
37 #include "utils_base.h"
39 #define CHECK_SERVICES 1
40 #define CHECK_HOSTS 2
42 void print_help (void);
43 void print_usage (void);
45 int total_services_ok=0;
46 int total_services_warning=0;
47 int total_services_unknown=0;
48 int total_services_critical=0;
50 int total_hosts_up=0;
51 int total_hosts_down=0;
52 int total_hosts_unreachable=0;
54 char *warn_threshold;
55 char *crit_threshold;
57 int check_type=CHECK_SERVICES;
59 char *data_vals=NULL;
60 char *label=NULL;
62 int verbose=0;
64 int process_arguments(int,char **);
68 int main(int argc, char **argv){
69 char *ptr;
70 int data_val;
71 int return_code=STATE_OK;
72 thresholds *thresholds = NULL;
74 if(process_arguments(argc,argv)==ERROR)
75 usage(_("Could not parse arguments"));
77 /* Initialize the thresholds */
78 set_thresholds(&thresholds, warn_threshold, crit_threshold);
79 if(verbose)
80 print_thresholds("check_cluster", thresholds);
82 /* check the data values */
83 for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
85 data_val=atoi(ptr);
87 if(check_type==CHECK_SERVICES){
88 switch(data_val){
89 case 0:
90 total_services_ok++;
91 break;
92 case 1:
93 total_services_warning++;
94 break;
95 case 2:
96 total_services_critical++;
97 break;
98 case 3:
99 total_services_unknown++;
100 break;
101 default:
102 break;
103 }
104 }
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;
118 }
119 }
120 }
123 /* return the status of the cluster */
124 if(check_type==CHECK_SERVICES){
125 return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
126 printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
127 state_text(return_code), (label==NULL)?"Service cluster":label,
128 total_services_ok,total_services_warning,
129 total_services_unknown,total_services_critical);
130 }
131 else{
132 return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
133 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
134 state_text(return_code), (label==NULL)?"Host cluster":label,
135 total_hosts_up,total_hosts_down,total_hosts_unreachable);
136 }
138 return return_code;
139 }
143 int process_arguments(int argc, char **argv){
144 int c;
145 int option=0;
146 static struct option longopts[]={
147 {"data", required_argument,0,'d'},
148 {"warning", required_argument,0,'w'},
149 {"critical", required_argument,0,'c'},
150 {"label", required_argument,0,'l'},
151 {"host", no_argument, 0,'h'},
152 {"service", no_argument, 0,'s'},
153 {"verbose", no_argument, 0,'v'},
154 {"version", no_argument, 0,'V'},
155 {"help", no_argument, 0,'H'},
156 {0,0,0,0}
157 };
159 /* no options were supplied */
160 if(argc<2)
161 return ERROR;
163 while(1){
165 c=getopt_long(argc,argv,"hHsvVw:c:d:l:",longopts,&option);
167 if(c==-1 || c==EOF || c==1)
168 break;
170 switch(c){
172 case 'h': /* host cluster */
173 check_type=CHECK_HOSTS;
174 break;
176 case 's': /* service cluster */
177 check_type=CHECK_SERVICES;
178 break;
180 case 'w': /* warning threshold */
181 warn_threshold = strdup(optarg);
182 break;
184 case 'c': /* warning threshold */
185 crit_threshold = strdup(optarg);
186 break;
188 case 'd': /* data values */
189 data_vals=(char *)strdup(optarg);
190 break;
192 case 'l': /* text label */
193 label=(char *)strdup(optarg);
194 break;
196 case 'v': /* verbose */
197 verbose++;
198 break;
200 case 'V': /* version */
201 print_revision (progname, revision);
202 exit (STATE_OK);
203 break;
205 case 'H': /* help */
206 print_help();
207 exit(STATE_UNKNOWN);
208 break;
210 default:
211 return ERROR;
212 break;
213 }
214 }
216 if(data_vals==NULL)
217 return ERROR;
219 return OK;
220 }
222 void
223 print_help(void)
224 {
225 print_revision(progname, revision);
226 printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
227 printf(COPYRIGHT, copyright, email);
229 printf(_("Host/Service Cluster Plugin for Nagios 2"));
230 printf("\n\n");
232 print_usage();
234 printf("\n");
235 printf("%s\n", _("Options:"));
236 printf (" %s\n", "-s, --service");
237 printf (" %s\n", _("Check service cluster status"));
238 printf (" %s\n", "-h, --host");
239 printf (" %s\n", _("Check host cluster status"));
240 printf (" %s\n", "-l, --label=STRING");
241 printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
242 printf (" %s\n", "-w, --warning=THRESHOLD");
243 printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
244 printf (" %s\n", _("non-OK state in order to return a WARNING status level"));
245 printf (" %s\n", "-c, --critical=THRESHOLD");
246 printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
247 printf (" %s\n", _("non-OK state in order to return a CRITICAL status level"));
248 printf (" %s\n", "-d, --data=LIST");
249 printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
250 printf (" %s\n", _("commas"));
252 printf(_(UT_VERBOSE));
254 printf("\n");
255 printf("%s\n", _("Notes:"));
256 printf(" %s\n", _("See:"));
257 printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
258 printf(" %s\n", _("for THRESHOLD format and examples."));
260 printf(_(UT_SUPPORT));
261 printf("\n");
262 }
265 void
266 print_usage(void)
267 {
269 printf(_("Usage:"));
270 printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
271 printf("[-w threshold] [-c threshold] [-v] [--help]\n");
273 }