Code

Add thresholds support for check_cluster + lots of standardization.
[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 = "2000-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 int total_services_ok=0;
42 int total_services_warning=0;
43 int total_services_unknown=0;
44 int total_services_critical=0;
46 int total_hosts_up=0;
47 int total_hosts_down=0;
48 int total_hosts_unreachable=0;
50 char *warn_threshold;
51 char *crit_threshold;
53 int check_type=CHECK_SERVICES;
55 char *data_vals=NULL;
56 char *label=NULL;
58 int verbose=0;
60 int process_arguments(int,char **);
64 int main(int argc, char **argv){
65         char input_buffer[MAX_INPUT_BUFFER];
66         char *ptr;
67         int data_val;
68         int return_code=STATE_OK;
69         int error=FALSE;
70         thresholds *thresholds;
72         if(process_arguments(argc,argv)==ERROR)
73                 usage(_("Could not parse arguments"));
75         /* Initialize the thresholds */
76         set_thresholds(&thresholds, warn_threshold, crit_threshold);
77         if(verbose)
78                 print_thresholds("check_cluster", thresholds);
80         /* check the data values */
81         for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
83                 data_val=atoi(ptr);
85                 if(check_type==CHECK_SERVICES){
86                         switch(data_val){
87                         case 0:
88                                 total_services_ok++;
89                                 break;
90                         case 1:
91                                 total_services_warning++;
92                                 break;
93                         case 2:
94                                 total_services_critical++;
95                                 break;
96                         case 3:
97                                 total_services_unknown++;
98                                 break;
99                         default:
100                                 break;
101                                 }
102                         }
103                 else{
104                         switch(data_val){
105                         case 0:
106                                 total_hosts_up++;
107                                 break;
108                         case 1:
109                                 total_hosts_down++;
110                                 break;
111                         case 2:
112                                 total_hosts_unreachable++;
113                                 break;
114                         default:
115                                 break;
116                                 }
117                         }
118                 }
119         
121         /* return the status of the cluster */
122         if(check_type==CHECK_SERVICES){
123                 return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
124                 printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
125                         state_text(return_code), (label==NULL)?"Service cluster":label,
126                         total_services_ok,total_services_warning,
127                         total_services_unknown,total_services_critical);
128                 }
129         else{
130                 return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
131                 printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
132                         state_text(return_code), (label==NULL)?"Host cluster":label,
133                         total_hosts_up,total_hosts_down,total_hosts_unreachable);
134                 }
136         return return_code;
137         }
141 int process_arguments(int argc, char **argv){
142         int c;
143         int option=0;
144         static struct option longopts[]={ 
145                 {"data",     required_argument,0,'d'},
146                 {"warning",  required_argument,0,'w'},
147                 {"critical", required_argument,0,'c'},
148                 {"label",    required_argument,0,'l'},
149                 {"host",     no_argument,      0,'h'},
150                 {"service",  no_argument,      0,'s'},
151                 {"verbose",  no_argument,      0,'v'},
152                 {"help",     no_argument,      0,'H'},
153                 {0,0,0,0}
154                 };
156         /* no options were supplied */
157         if(argc<2)
158                 return ERROR;
160         while(1){
162                 c=getopt_long(argc,argv,"hHsvw:c:d:l:",longopts,&option);
164                 if(c==-1 || c==EOF || c==1)
165                         break;
167                 switch(c){
169                 case 'h': /* host cluster */
170                         check_type=CHECK_HOSTS;
171                         break;
173                 case 's': /* service cluster */
174                         check_type=CHECK_SERVICES;
175                         break;
177                 case 'w': /* warning threshold */
178                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
179                                 usage2 (_("Invalid warning threshold: %s\n"), optarg);
180                         warn_threshold = strdup(optarg);
181                         break;
183                 case 'c': /* warning threshold */
184                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
185                                 usage2 (_("Invalid critical threshold: %s\n"), optarg);
186                         crit_threshold = strdup(optarg);
187                         break;
189                 case 'd': /* data values */
190                         data_vals=(char *)strdup(optarg);
191                         break;
193                 case 'l': /* text label */
194                         label=(char *)strdup(optarg);
195                         break;
197                 case 'v': /* verbose */
198                         verbose++;
199                         break;
201                 case 'H': /* help */
202                         print_help();
203                         exit(STATE_UNKNOWN);
204                         break;
206                 default:
207                         return ERROR;
208                         break;
209                         }
210                 }
212         if(data_vals==NULL)
213                 return ERROR;
215         return OK;
218 void
219 print_help(void)
221         print_revision(progname, revision);
222         printf(COPYRIGHT, copyright, email);
224         printf("%s\n", _("Host/Service Cluster Plugin for Nagios 2"));
226         print_usage();
229         printf("%s\n", _("Options:"));
230         printf (" %s\n", "-s, --service");
231         printf ("    %s\n", _("Check service cluster status"));
232         printf (" %s\n", "-h, --host");
233         printf ("    %s\n", _("Check host cluster status"));
234         printf (" %s\n", "-l, --label=STRING");
235         printf ("    %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
236         printf (" %s\n", "-w, --warning=THRESHOLD");
237         printf ("    %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
238         printf ("    %s\n", _("non-OK state in order to return a WARNING status level"));
239         printf (" %s\n", "-c, --critical=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 CRITICAL status level"));
242         printf (" %s\n", "-d, --data=LIST");
243         printf ("    %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
244         printf ("    %s\n", _("commas"));
246         printf(_(UT_VERBOSE));
248         printf("\n");
249         printf("%s\n", _("Notes:"));
250         printf(" %s\n", _("See:"));
251         printf(" %s\n", _("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
252         printf(" %s\n", _("for THRESHOLD format and examples."));
254         printf(_(UT_SUPPORT));
255         printf("\n");
259 void
260 print_usage(void)
263         printf("\n");
264         printf(_("Usage:"));
265         printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
266         printf("[-w threshold] [-c threshold] [-v] [--help]\n");
267         printf("\n");
271 #if 0
272 #endif