Add suggested multi-oid test (John A. Barbuto)
[monitoring-plugins.git] / contrib / check_cluster2.c
blobe60948d6d17ceb89a30c69c064e58b69c8b74704
1 /*****************************************************************************
3 * CHECK_CLUSTER2.C - Host and Service Cluster Plugin for Nagios 2.x
5 * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
6 * License: GPL
7 * Last Modified: 03-11-2004
9 * License:
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 *****************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <getopt.h>
34 #define OK 0
35 #define ERROR -1
37 #define TRUE 1
38 #define FALSE 0
40 #define CHECK_SERVICES 1
41 #define CHECK_HOSTS 2
43 #define MAX_INPUT_BUFFER 1024
45 #define STATE_OK 0
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;
55 int total_hosts_up=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;
64 char *data_vals=NULL;
65 char *label=NULL;
68 int process_arguments(int,char **);
72 int main(int argc, char **argv){
73 char input_buffer[MAX_INPUT_BUFFER];
74 char *ptr;
75 int data_val;
76 int return_code=STATE_OK;
77 int error=FALSE;
79 if(process_arguments(argc,argv)==ERROR){
81 printf("Invalid arguments supplied\n");
82 printf("\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");
88 printf("\n");
89 printf("Usage: %s (-s | -h) [-l label] [-w threshold] [-c threshold] [-d val1,val2,...,valn]\n",argv[0]);
90 printf("\n");
91 printf("Options:\n");
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");
101 printf("\n");
103 return STATE_UNKNOWN;
106 /* check the data values */
107 for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
109 data_val=atoi(ptr);
111 if(check_type==CHECK_SERVICES){
112 switch(data_val){
113 case 0:
114 total_services_ok++;
115 break;
116 case 1:
117 total_services_warning++;
118 break;
119 case 2:
120 total_services_critical++;
121 break;
122 case 3:
123 total_services_unknown++;
124 break;
125 default:
126 break;
129 else{
130 switch(data_val){
131 case 0:
132 total_hosts_up++;
133 break;
134 case 1:
135 total_hosts_down++;
136 break;
137 case 2:
138 total_hosts_unreachable++;
139 break;
140 default:
141 break;
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;
153 else
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);
157 else{
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;
162 else
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);
167 return return_code;
172 int process_arguments(int argc, char **argv){
173 int c;
174 int option=0;
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'},
182 {0,0,0,0}
185 /* no options were supplied */
186 if(argc<2)
187 return ERROR;
189 while(1){
191 c=getopt_long(argc,argv,"hsw:c:d:l:",longopts,&option);
193 if(c==-1 || c==EOF || c==1)
194 break;
196 switch(c){
198 case 'h': /* host cluster */
199 check_type=CHECK_HOSTS;
200 break;
202 case 's': /* service cluster */
203 check_type=CHECK_SERVICES;
204 break;
206 case 'w': /* warning threshold */
207 warning_threshold=atoi(optarg);
208 break;
210 case 'c': /* warning threshold */
211 critical_threshold=atoi(optarg);
212 break;
214 case 'd': /* data values */
215 data_vals=(char *)strdup(optarg);
216 break;
218 case 'l': /* text label */
219 label=(char *)strdup(optarg);
220 break;
222 default:
223 return ERROR;
224 break;
228 if(data_vals==NULL)
229 return ERROR;
231 return OK;