Reverted check_procs for solaris back to using pst3 due to truncation
[monitoring-plugins.git] / plugins / check_cluster.c
blobc67573a36cc4bd83e805e67d4c4bb53f686d5611
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$
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.
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.
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/>.
25 * $Id$
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 if(process_arguments(argc,argv)==ERROR)
78 usage(_("Could not parse arguments"));
80 /* Initialize the thresholds */
81 set_thresholds(&thresholds, warn_threshold, crit_threshold);
82 if(verbose)
83 print_thresholds("check_cluster", thresholds);
85 /* check the data values */
86 for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
88 data_val=atoi(ptr);
90 if(check_type==CHECK_SERVICES){
91 switch(data_val){
92 case 0:
93 total_services_ok++;
94 break;
95 case 1:
96 total_services_warning++;
97 break;
98 case 2:
99 total_services_critical++;
100 break;
101 case 3:
102 total_services_unknown++;
103 break;
104 default:
105 break;
108 else{
109 switch(data_val){
110 case 0:
111 total_hosts_up++;
112 break;
113 case 1:
114 total_hosts_down++;
115 break;
116 case 2:
117 total_hosts_unreachable++;
118 break;
119 default:
120 break;
126 /* return the status of the cluster */
127 if(check_type==CHECK_SERVICES){
128 return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
129 printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
130 state_text(return_code), (label==NULL)?"Service cluster":label,
131 total_services_ok,total_services_warning,
132 total_services_unknown,total_services_critical);
134 else{
135 return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
136 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
137 state_text(return_code), (label==NULL)?"Host cluster":label,
138 total_hosts_up,total_hosts_down,total_hosts_unreachable);
141 return return_code;
146 int process_arguments(int argc, char **argv){
147 int c;
148 int option=0;
149 static struct option longopts[]={
150 {"data", required_argument,0,'d'},
151 {"warning", required_argument,0,'w'},
152 {"critical", required_argument,0,'c'},
153 {"label", required_argument,0,'l'},
154 {"host", no_argument, 0,'h'},
155 {"service", no_argument, 0,'s'},
156 {"verbose", no_argument, 0,'v'},
157 {"version", no_argument, 0,'V'},
158 {"help", no_argument, 0,'H'},
159 {0,0,0,0}
162 /* no options were supplied */
163 if(argc<2)
164 return ERROR;
166 while(1){
168 c=getopt_long(argc,argv,"hHsvVw:c:d:l:",longopts,&option);
170 if(c==-1 || c==EOF || c==1)
171 break;
173 switch(c){
175 case 'h': /* host cluster */
176 check_type=CHECK_HOSTS;
177 break;
179 case 's': /* service cluster */
180 check_type=CHECK_SERVICES;
181 break;
183 case 'w': /* warning threshold */
184 warn_threshold = strdup(optarg);
185 break;
187 case 'c': /* warning threshold */
188 crit_threshold = strdup(optarg);
189 break;
191 case 'd': /* data values */
192 data_vals=(char *)strdup(optarg);
193 break;
195 case 'l': /* text label */
196 label=(char *)strdup(optarg);
197 break;
199 case 'v': /* verbose */
200 verbose++;
201 break;
203 case 'V': /* version */
204 print_revision (progname, revision);
205 exit (STATE_OK);
206 break;
208 case 'H': /* help */
209 print_help();
210 exit(STATE_UNKNOWN);
211 break;
213 default:
214 return ERROR;
215 break;
219 if(data_vals==NULL)
220 return ERROR;
222 return OK;
225 void
226 print_help(void)
228 print_revision(progname, revision);
229 printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
230 printf(COPYRIGHT, copyright, email);
232 printf(_("Host/Service Cluster Plugin for Nagios 2"));
233 printf("\n\n");
235 print_usage();
237 printf("\n");
238 printf("%s\n", _("Options:"));
239 printf (" %s\n", "-s, --service");
240 printf (" %s\n", _("Check service cluster status"));
241 printf (" %s\n", "-h, --host");
242 printf (" %s\n", _("Check host cluster status"));
243 printf (" %s\n", "-l, --label=STRING");
244 printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
245 printf (" %s\n", "-w, --warning=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 WARNING status level"));
248 printf (" %s\n", "-c, --critical=THRESHOLD");
249 printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
250 printf (" %s\n", _("non-OK state in order to return a CRITICAL status level"));
251 printf (" %s\n", "-d, --data=LIST");
252 printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
253 printf (" %s\n", _("commas"));
255 printf(_(UT_VERBOSE));
257 printf("\n");
258 printf("%s\n", _("Notes:"));
259 printf(" %s\n", _("See:"));
260 printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
261 printf(" %s\n", _("for THRESHOLD format and examples."));
263 printf(_(UT_SUPPORT));
264 printf("\n");
268 void
269 print_usage(void)
272 printf(_("Usage:"));
273 printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
274 printf("[-w threshold] [-c threshold] [-v] [--help]\n");