Back to index

nagios-plugins  1.4.16
check_cluster2.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * CHECK_CLUSTER2.C - Host and Service Cluster Plugin for Nagios 2.x
00004  *
00005  * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
00006  * License: GPL
00007  * Last Modified:   03-11-2004
00008  *
00009  * License:
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024  *
00025  *****************************************************************************/
00026 
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <unistd.h>
00032 #include <getopt.h>
00033 
00034 #define OK           0
00035 #define ERROR        -1
00036 
00037 #define TRUE         1
00038 #define FALSE        0
00039 
00040 #define CHECK_SERVICES      1
00041 #define CHECK_HOSTS  2
00042 
00043 #define MAX_INPUT_BUFFER    1024
00044 
00045 #define STATE_OK     0
00046 #define STATE_WARNING       1
00047 #define STATE_CRITICAL      2
00048 #define STATE_UNKNOWN       3
00049 
00050 int total_services_ok=0;
00051 int total_services_warning=0;
00052 int total_services_unknown=0;
00053 int total_services_critical=0;
00054 
00055 int total_hosts_up=0;
00056 int total_hosts_down=0;
00057 int total_hosts_unreachable=0;
00058 
00059 int warning_threshold=1;
00060 int critical_threshold=1;
00061 
00062 int check_type=CHECK_SERVICES;
00063 
00064 char *data_vals=NULL;
00065 char *label=NULL;
00066 
00067 
00068 int process_arguments(int,char **);
00069 
00070 
00071 
00072 int main(int argc, char **argv){
00073        char input_buffer[MAX_INPUT_BUFFER];
00074        char *ptr;
00075        int data_val;
00076        int return_code=STATE_OK;
00077        int error=FALSE;
00078 
00079        if(process_arguments(argc,argv)==ERROR){
00080 
00081               printf("Invalid arguments supplied\n");
00082               printf("\n");
00083 
00084               printf("Host/Service Cluster Plugin for Nagios 2\n");
00085               printf("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
00086               printf("Last Modified: 03-11-2004\n");
00087               printf("License: GPL\n");
00088               printf("\n");
00089               printf("Usage: %s (-s | -h) [-l label] [-w threshold] [-c threshold] [-d val1,val2,...,valn]\n",argv[0]);
00090               printf("\n");
00091               printf("Options:\n");
00092               printf("   -s, --service  = Check service cluster status\n");
00093               printf("   -h, --host     = Check host cluster status\n");
00094               printf("   -l, --label    = Optional prepended text output (i.e. \"Host cluster\")\n");
00095               printf("   -w, --warning  = Specifies the number of hosts or services in cluster that must be in\n");
00096               printf("                    a non-OK state in order to return a WARNING status level\n");
00097               printf("   -c, --critical = Specifies the number of hosts or services in cluster that must be in\n");
00098               printf("                    a non-OK state in order to return a CRITICAL status level\n");
00099               printf("   -d, --data     = The status codes of the hosts or services in the cluster, separated\n");
00100               printf("                    by commas\n");
00101               printf("\n");
00102 
00103               return STATE_UNKNOWN;
00104                }
00105 
00106        /* check the data values */
00107        for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
00108 
00109               data_val=atoi(ptr);
00110 
00111               if(check_type==CHECK_SERVICES){
00112                      switch(data_val){
00113                      case 0:
00114                             total_services_ok++;
00115                             break;
00116                      case 1:
00117                             total_services_warning++;
00118                             break;
00119                      case 2:
00120                             total_services_critical++;
00121                             break;
00122                      case 3:
00123                             total_services_unknown++;
00124                             break;
00125                      default:
00126                             break;
00127                              }
00128                       }
00129               else{
00130                      switch(data_val){
00131                      case 0:
00132                             total_hosts_up++;
00133                             break;
00134                      case 1:
00135                             total_hosts_down++;
00136                             break;
00137                      case 2:
00138                             total_hosts_unreachable++;
00139                             break;
00140                      default:
00141                             break;
00142                              }
00143                       }
00144                }
00145        
00146 
00147        /* return the status of the cluster */
00148        if(check_type==CHECK_SERVICES){
00149               if((total_services_warning+total_services_unknown+total_services_critical) >= critical_threshold)
00150                      return_code=STATE_CRITICAL;
00151               else if((total_services_warning+total_services_unknown+total_services_critical) >= warning_threshold)
00152                      return_code=STATE_WARNING;
00153               else
00154                      return_code=STATE_OK;
00155               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);
00156                 }
00157        else{
00158               if((total_hosts_down+total_hosts_unreachable) >= critical_threshold)
00159                      return_code=STATE_CRITICAL;
00160               else if((total_hosts_down+total_hosts_unreachable) >= warning_threshold)
00161                      return_code=STATE_WARNING;
00162               else
00163                      return_code=STATE_OK;
00164               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);
00165                 }
00166 
00167        return return_code;
00168         }
00169 
00170 
00171 
00172 int process_arguments(int argc, char **argv){
00173        int c;
00174        int option=0;
00175        static struct option longopts[]={ 
00176               {"data",     required_argument,0,'d'},
00177               {"warning",  required_argument,0,'w'},
00178               {"critical", required_argument,0,'c'},
00179               {"label",    required_argument,0,'l'},
00180               {"host",     no_argument,      0,'h'},
00181               {"service",  no_argument,      0,'s'},
00182               {0,0,0,0}
00183               };
00184 
00185        /* no options were supplied */
00186        if(argc<2)
00187               return ERROR;
00188 
00189        while(1){
00190 
00191               c=getopt_long(argc,argv,"hsw:c:d:l:",longopts,&option);
00192 
00193               if(c==-1 || c==EOF || c==1)
00194                      break;
00195 
00196               switch(c){
00197 
00198               case 'h': /* host cluster */
00199                      check_type=CHECK_HOSTS;
00200                      break;
00201 
00202               case 's': /* service cluster */
00203                      check_type=CHECK_SERVICES;
00204                      break;
00205 
00206               case 'w': /* warning threshold */
00207                      warning_threshold=atoi(optarg);
00208                      break;
00209 
00210               case 'c': /* warning threshold */
00211                      critical_threshold=atoi(optarg);
00212                      break;
00213 
00214               case 'd': /* data values */
00215                      data_vals=(char *)strdup(optarg);
00216                      break;
00217 
00218               case 'l': /* text label */
00219                      label=(char *)strdup(optarg);
00220                      break;
00221 
00222               default:
00223                      return ERROR;
00224                      break;
00225                       }
00226               }
00227 
00228        if(data_vals==NULL)
00229               return ERROR;
00230 
00231        return OK;
00232         }