Code

Predeclare functions and remove unused variables.
[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"
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;
73         if(process_arguments(argc,argv)==ERROR)
74                 usage(_("Could not parse arguments"));
76         /* Initialize the thresholds */
77         set_thresholds(&thresholds, warn_threshold, crit_threshold);
78         if(verbose)
79                 print_thresholds("check_cluster", thresholds);
81         /* check the data values */
82         for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
84                 data_val=atoi(ptr);
86                 if(check_type==CHECK_SERVICES){
87                         switch(data_val){
88                         case 0:
89                                 total_services_ok++;
90                                 break;
91                         case 1:
92                                 total_services_warning++;
93                                 break;
94                         case 2:
95                                 total_services_critical++;
96                                 break;
97                         case 3:
98                                 total_services_unknown++;
99                                 break;
100                         default:
101                                 break;
102                         }
103                 }
104                 else{
105                         switch(data_val){
106                         case 0:
107                                 total_hosts_up++;
108                                 break;
109                         case 1:
110                                 total_hosts_down++;
111                                 break;
112                         case 2:
113                                 total_hosts_unreachable++;
114                                 break;
115                         default:
116                                 break;
117                         }
118                 }
119         }
120         
122         /* return the status of the cluster */
123         if(check_type==CHECK_SERVICES){
124                 return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
125                 printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
126                         state_text(return_code), (label==NULL)?"Service cluster":label,
127                         total_services_ok,total_services_warning,
128                         total_services_unknown,total_services_critical);
129         }
130         else{
131                 return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
132                 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
133                         state_text(return_code), (label==NULL)?"Host cluster":label,
134                         total_hosts_up,total_hosts_down,total_hosts_unreachable);
135         }
137         return return_code;
142 int process_arguments(int argc, char **argv){
143         int c;
144         int option=0;
145         static struct option longopts[]={ 
146                 {"data",     required_argument,0,'d'},
147                 {"warning",  required_argument,0,'w'},
148                 {"critical", required_argument,0,'c'},
149                 {"label",    required_argument,0,'l'},
150                 {"host",     no_argument,      0,'h'},
151                 {"service",  no_argument,      0,'s'},
152                 {"verbose",  no_argument,      0,'v'},
153                 {"help",     no_argument,      0,'H'},
154                 {0,0,0,0}
155         };
157         /* no options were supplied */
158         if(argc<2)
159                 return ERROR;
161         while(1){
163                 c=getopt_long(argc,argv,"hHsvw:c:d:l:",longopts,&option);
165                 if(c==-1 || c==EOF || c==1)
166                         break;
168                 switch(c){
170                 case 'h': /* host cluster */
171                         check_type=CHECK_HOSTS;
172                         break;
174                 case 's': /* service cluster */
175                         check_type=CHECK_SERVICES;
176                         break;
178                 case 'w': /* warning threshold */
179                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
180                                 usage2 (_("Invalid warning threshold: %s\n"), optarg);
181                         warn_threshold = strdup(optarg);
182                         break;
184                 case 'c': /* warning threshold */
185                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
186                                 usage2 (_("Invalid critical threshold: %s\n"), optarg);
187                         crit_threshold = strdup(optarg);
188                         break;
190                 case 'd': /* data values */
191                         data_vals=(char *)strdup(optarg);
192                         break;
194                 case 'l': /* text label */
195                         label=(char *)strdup(optarg);
196                         break;
198                 case 'v': /* verbose */
199                         verbose++;
200                         break;
202                 case 'H': /* help */
203                         print_help();
204                         exit(STATE_UNKNOWN);
205                         break;
207                 default:
208                         return ERROR;
209                         break;
210                 }
211         }
213         if(data_vals==NULL)
214                 return ERROR;
216         return OK;
219 void
220 print_help(void)
222         print_revision(progname, revision);
223         printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
224         printf(COPYRIGHT, copyright, email);
226         printf(_("Host/Service Cluster Plugin for Nagios 2"));
227         printf("\n\n");
229         print_usage();
231         printf("\n");
232         printf("%s\n", _("Options:"));
233         printf (" %s\n", "-s, --service");
234         printf ("    %s\n", _("Check service cluster status"));
235         printf (" %s\n", "-h, --host");
236         printf ("    %s\n", _("Check host cluster status"));
237         printf (" %s\n", "-l, --label=STRING");
238         printf ("    %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
239         printf (" %s\n", "-w, --warning=THRESHOLD");
240         printf ("    %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
241         printf ("    %s\n", _("non-OK state in order to return a WARNING status level"));
242         printf (" %s\n", "-c, --critical=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 CRITICAL status level"));
245         printf (" %s\n", "-d, --data=LIST");
246         printf ("    %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
247         printf ("    %s\n", _("commas"));
249         printf(_(UT_VERBOSE));
251         printf("\n");
252         printf("%s\n", _("Notes:"));
253         printf(" %s\n", _("See:"));
254         printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
255         printf(" %s\n", _("for THRESHOLD format and examples."));
257         printf(_(UT_SUPPORT));
258         printf("\n");
262 void
263 print_usage(void)
266         printf(_("Usage:"));
267         printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
268         printf("[-w threshold] [-c threshold] [-v] [--help]\n");