Code

Fix the "--help" output.
[nagiosplug.git] / plugins / check_cluster.c
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         setlocale (LC_ALL, "");
75         bindtextdomain (PACKAGE, LOCALEDIR);
76         textdomain (PACKAGE);
78         if(process_arguments(argc,argv)==ERROR)
79                 usage(_("Could not parse arguments"));
81         /* Initialize the thresholds */
82         set_thresholds(&thresholds, warn_threshold, crit_threshold);
83         if(verbose)
84                 print_thresholds("check_cluster", thresholds);
86         /* check the data values */
87         for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
89                 data_val=atoi(ptr);
91                 if(check_type==CHECK_SERVICES){
92                         switch(data_val){
93                         case 0:
94                                 total_services_ok++;
95                                 break;
96                         case 1:
97                                 total_services_warning++;
98                                 break;
99                         case 2:
100                                 total_services_critical++;
101                                 break;
102                         case 3:
103                                 total_services_unknown++;
104                                 break;
105                         default:
106                                 break;
107                         }
108                 }
109                 else{
110                         switch(data_val){
111                         case 0:
112                                 total_hosts_up++;
113                                 break;
114                         case 1:
115                                 total_hosts_down++;
116                                 break;
117                         case 2:
118                                 total_hosts_unreachable++;
119                                 break;
120                         default:
121                                 break;
122                         }
123                 }
124         }
125         
127         /* return the status of the cluster */
128         if(check_type==CHECK_SERVICES){
129                 return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
130                 printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
131                         state_text(return_code), (label==NULL)?"Service cluster":label,
132                         total_services_ok,total_services_warning,
133                         total_services_unknown,total_services_critical);
134         }
135         else{
136                 return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
137                 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
138                         state_text(return_code), (label==NULL)?"Host cluster":label,
139                         total_hosts_up,total_hosts_down,total_hosts_unreachable);
140         }
142         return return_code;
147 int process_arguments(int argc, char **argv){
148         int c;
149         int option=0;
150         static struct option longopts[]={ 
151                 {"data",     required_argument,0,'d'},
152                 {"warning",  required_argument,0,'w'},
153                 {"critical", required_argument,0,'c'},
154                 {"label",    required_argument,0,'l'},
155                 {"host",     no_argument,      0,'h'},
156                 {"service",  no_argument,      0,'s'},
157                 {"verbose",  no_argument,      0,'v'},
158                 {"version",  no_argument,      0,'V'},
159                 {"help",     no_argument,      0,'H'},
160                 {0,0,0,0}
161         };
163         /* no options were supplied */
164         if(argc<2)
165                 return ERROR;
167         while(1){
169                 c=getopt_long(argc,argv,"hHsvVw:c:d:l:",longopts,&option);
171                 if(c==-1 || c==EOF || c==1)
172                         break;
174                 switch(c){
176                 case 'h': /* host cluster */
177                         check_type=CHECK_HOSTS;
178                         break;
180                 case 's': /* service cluster */
181                         check_type=CHECK_SERVICES;
182                         break;
184                 case 'w': /* warning threshold */
185                         warn_threshold = strdup(optarg);
186                         break;
188                 case 'c': /* warning threshold */
189                         crit_threshold = strdup(optarg);
190                         break;
192                 case 'd': /* data values */
193                         data_vals=(char *)strdup(optarg);
194                         break;
196                 case 'l': /* text label */
197                         label=(char *)strdup(optarg);
198                         break;
200                 case 'v': /* verbose */
201                         verbose++;
202                         break;
204                 case 'V': /* version */
205                         print_revision (progname, revision);
206                         exit (STATE_OK);
207                         break;
209                 case 'H': /* help */
210                         print_help();
211                         exit(STATE_UNKNOWN);
212                         break;
214                 default:
215                         return ERROR;
216                         break;
217                 }
218         }
220         if(data_vals==NULL)
221                 return ERROR;
223         return OK;
226 void
227 print_help(void)
229         print_revision(progname, revision);
230         printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
231         printf(COPYRIGHT, copyright, email);
233         printf(_("Host/Service Cluster Plugin for Nagios 2"));
234         printf("\n\n");
236         print_usage();
238         printf("\n");
239         printf("%s\n", _("Options:"));
240         printf (" %s\n", "-s, --service");
241         printf ("    %s\n", _("Check service cluster status"));
242         printf (" %s\n", "-h, --host");
243         printf ("    %s\n", _("Check host cluster status"));
244         printf (" %s\n", "-l, --label=STRING");
245         printf ("    %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
246         printf (" %s\n", "-w, --warning=THRESHOLD");
247         printf ("    %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
248         printf ("    %s\n", _("non-OK state in order to return a WARNING status level"));
249         printf (" %s\n", "-c, --critical=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 CRITICAL status level"));
252         printf (" %s\n", "-d, --data=LIST");
253         printf ("    %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
254         printf ("    %s\n", _("commas"));
256         printf(_(UT_VERBOSE));
258         printf("\n");
259         printf("%s\n", _("Notes:"));
260         printf(" %s\n", _("See:"));
261         printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
262         printf(" %s\n", _("for THRESHOLD format and examples."));
264         printf(_(UT_SUPPORT));
265         printf("\n");
269 void
270 print_usage(void)
273         printf(_("Usage:"));
274         printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
275         printf("[-w threshold] [-c threshold] [-v] [--help]\n");