1 /*****************************************************************************
3 * CHECK_CLUSTER2.C - Host and Service Cluster Plugin for Nagios 2.x
5 * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
7 * Last Modified: 03-11-2004
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *****************************************************************************/
40 #define CHECK_SERVICES 1
43 #define MAX_INPUT_BUFFER 1024
46 #define STATE_WARNING 1
47 #define STATE_CRITICAL 2
48 #define STATE_UNKNOWN 3
50 int total_services_ok
=0;
51 int total_services_warning
=0;
52 int total_services_unknown
=0;
53 int total_services_critical
=0;
56 int total_hosts_down
=0;
57 int total_hosts_unreachable
=0;
59 int warning_threshold
=1;
60 int critical_threshold
=1;
62 int check_type
=CHECK_SERVICES
;
68 int process_arguments(int,char **);
72 int main(int argc
, char **argv
){
73 char input_buffer
[MAX_INPUT_BUFFER
];
76 int return_code
=STATE_OK
;
79 if(process_arguments(argc
,argv
)==ERROR
){
81 printf("Invalid arguments supplied\n");
84 printf("Host/Service Cluster Plugin for Nagios 2\n");
85 printf("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
86 printf("Last Modified: 03-11-2004\n");
87 printf("License: GPL\n");
89 printf("Usage: %s (-s | -h) [-l label] [-w threshold] [-c threshold] [-d val1,val2,...,valn]\n",argv
[0]);
92 printf(" -s, --service = Check service cluster status\n");
93 printf(" -h, --host = Check host cluster status\n");
94 printf(" -l, --label = Optional prepended text output (i.e. \"Host cluster\")\n");
95 printf(" -w, --warning = Specifies the number of hosts or services in cluster that must be in\n");
96 printf(" a non-OK state in order to return a WARNING status level\n");
97 printf(" -c, --critical = Specifies the number of hosts or services in cluster that must be in\n");
98 printf(" a non-OK state in order to return a CRITICAL status level\n");
99 printf(" -d, --data = The status codes of the hosts or services in the cluster, separated\n");
100 printf(" by commas\n");
103 return STATE_UNKNOWN
;
106 /* check the data values */
107 for(ptr
=strtok(data_vals
,",");ptr
!=NULL
;ptr
=strtok(NULL
,",")){
111 if(check_type
==CHECK_SERVICES
){
117 total_services_warning
++;
120 total_services_critical
++;
123 total_services_unknown
++;
138 total_hosts_unreachable
++;
147 /* return the status of the cluster */
148 if(check_type
==CHECK_SERVICES
){
149 if((total_services_warning
+total_services_unknown
+total_services_critical
) >= critical_threshold
)
150 return_code
=STATE_CRITICAL
;
151 else if((total_services_warning
+total_services_unknown
+total_services_critical
) >= warning_threshold
)
152 return_code
=STATE_WARNING
;
154 return_code
=STATE_OK
;
155 printf("%s %s: %d ok, %d warning, %d unknown, %d critical\n",(label
==NULL
)?"Service cluster":label
,(return_code
==STATE_OK
)?"ok":"problem",total_services_ok
,total_services_warning
,total_services_unknown
,total_services_critical
);
158 if((total_hosts_down
+total_hosts_unreachable
) >= critical_threshold
)
159 return_code
=STATE_CRITICAL
;
160 else if((total_hosts_down
+total_hosts_unreachable
) >= warning_threshold
)
161 return_code
=STATE_WARNING
;
163 return_code
=STATE_OK
;
164 printf("%s %s: %d up, %d down, %d unreachable\n",(label
==NULL
)?"Host cluster":label
,(return_code
==STATE_OK
)?"ok":"problem",total_hosts_up
,total_hosts_down
,total_hosts_unreachable
);
172 int process_arguments(int argc
, char **argv
){
175 static struct option longopts
[]={
176 {"data", required_argument
,0,'d'},
177 {"warning", required_argument
,0,'w'},
178 {"critical", required_argument
,0,'c'},
179 {"label", required_argument
,0,'l'},
180 {"host", no_argument
, 0,'h'},
181 {"service", no_argument
, 0,'s'},
185 /* no options were supplied */
191 c
=getopt_long(argc
,argv
,"hsw:c:d:l:",longopts
,&option
);
193 if(c
==-1 || c
==EOF
|| c
==1)
198 case 'h': /* host cluster */
199 check_type
=CHECK_HOSTS
;
202 case 's': /* service cluster */
203 check_type
=CHECK_SERVICES
;
206 case 'w': /* warning threshold */
207 warning_threshold
=atoi(optarg
);
210 case 'c': /* warning threshold */
211 critical_threshold
=atoi(optarg
);
214 case 'd': /* data values */
215 data_vals
=(char *)strdup(optarg
);
218 case 'l': /* text label */
219 label
=(char *)strdup(optarg
);