Back to index

nagios-plugins  1.4.16
check_cluster.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * check_cluster.c - Host and Service Cluster Plugin for Nagios 2.x
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
00007 * Copyright (c) 2007 Nagios Plugins Development Team
00008 * 
00009 * This program is free software: you can redistribute it and/or modify
00010 * it under the terms of the GNU General Public License as published by
00011 * the Free Software Foundation, either version 3 of the License, or
00012 * (at your option) any later version.
00013 * 
00014 * This program is distributed in the hope that it will be useful,
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 * GNU General Public License for more details.
00018 * 
00019 * You should have received a copy of the GNU General Public License
00020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021 * 
00022 * 
00023 *****************************************************************************/
00024 
00025 const char *progname = "check_cluster";
00026 const char *copyright = "2000-2007";
00027 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00028 
00029 #include "common.h"
00030 #include "utils.h"
00031 #include "utils_base.h"
00032 
00033 #define CHECK_SERVICES      1
00034 #define CHECK_HOSTS  2
00035 
00036 void print_help (void);
00037 void print_usage (void);
00038 
00039 int total_services_ok=0;
00040 int total_services_warning=0;
00041 int total_services_unknown=0;
00042 int total_services_critical=0;
00043 
00044 int total_hosts_up=0;
00045 int total_hosts_down=0;
00046 int total_hosts_unreachable=0;
00047 
00048 char *warn_threshold;
00049 char *crit_threshold;
00050 
00051 int check_type=CHECK_SERVICES;
00052 
00053 char *data_vals=NULL;
00054 char *label=NULL;
00055 
00056 int verbose=0;
00057 
00058 int process_arguments(int,char **);
00059 
00060 
00061 
00062 int main(int argc, char **argv){
00063        char *ptr;
00064        int data_val;
00065        int return_code=STATE_OK;
00066        thresholds *thresholds = NULL;
00067 
00068        setlocale (LC_ALL, "");
00069        bindtextdomain (PACKAGE, LOCALEDIR);
00070        textdomain (PACKAGE);
00071 
00072        /* Parse extra opts if any */
00073        argv=np_extra_opts(&argc, argv, progname);
00074 
00075        if(process_arguments(argc,argv)==ERROR)
00076               usage(_("Could not parse arguments"));
00077 
00078        /* Initialize the thresholds */
00079        set_thresholds(&thresholds, warn_threshold, crit_threshold);
00080        if(verbose)
00081               print_thresholds("check_cluster", thresholds);
00082 
00083        /* check the data values */
00084        for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
00085 
00086               data_val=atoi(ptr);
00087 
00088               if(check_type==CHECK_SERVICES){
00089                      switch(data_val){
00090                      case 0:
00091                             total_services_ok++;
00092                             break;
00093                      case 1:
00094                             total_services_warning++;
00095                             break;
00096                      case 2:
00097                             total_services_critical++;
00098                             break;
00099                      case 3:
00100                             total_services_unknown++;
00101                             break;
00102                      default:
00103                             break;
00104                       }
00105                }
00106               else{
00107                      switch(data_val){
00108                      case 0:
00109                             total_hosts_up++;
00110                             break;
00111                      case 1:
00112                             total_hosts_down++;
00113                             break;
00114                      case 2:
00115                             total_hosts_unreachable++;
00116                             break;
00117                      default:
00118                             break;
00119                       }
00120                }
00121         }
00122 
00123 
00124        /* return the status of the cluster */
00125        if(check_type==CHECK_SERVICES){
00126               return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
00127               printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
00128                      state_text(return_code), (label==NULL)?"Service cluster":label,
00129                      total_services_ok,total_services_warning,
00130                      total_services_unknown,total_services_critical);
00131        }
00132        else{
00133               return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
00134               printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
00135                      state_text(return_code), (label==NULL)?"Host cluster":label,
00136                      total_hosts_up,total_hosts_down,total_hosts_unreachable);
00137        }
00138 
00139        return return_code;
00140 }
00141 
00142 
00143 
00144 int process_arguments(int argc, char **argv){
00145        int c;
00146        int option=0;
00147        static struct option longopts[]={
00148               {"data",     required_argument,0,'d'},
00149               {"warning",  required_argument,0,'w'},
00150               {"critical", required_argument,0,'c'},
00151               {"label",    required_argument,0,'l'},
00152               {"host",     no_argument,      0,'h'},
00153               {"service",  no_argument,      0,'s'},
00154               {"verbose",  no_argument,      0,'v'},
00155               {"version",  no_argument,      0,'V'},
00156               {"help",     no_argument,      0,'H'},
00157               {0,0,0,0}
00158        };
00159 
00160        /* no options were supplied */
00161        if(argc<2)
00162               return ERROR;
00163 
00164        while(1){
00165 
00166               c=getopt_long(argc,argv,"hHsvVw:c:d:l:",longopts,&option);
00167 
00168               if(c==-1 || c==EOF || c==1)
00169                      break;
00170 
00171               switch(c){
00172 
00173               case 'h': /* host cluster */
00174                      check_type=CHECK_HOSTS;
00175                      break;
00176 
00177               case 's': /* service cluster */
00178                      check_type=CHECK_SERVICES;
00179                      break;
00180 
00181               case 'w': /* warning threshold */
00182                      warn_threshold = strdup(optarg);
00183                      break;
00184 
00185               case 'c': /* warning threshold */
00186                      crit_threshold = strdup(optarg);
00187                      break;
00188 
00189               case 'd': /* data values */
00190                      data_vals=(char *)strdup(optarg);
00191                      break;
00192 
00193               case 'l': /* text label */
00194                      label=(char *)strdup(optarg);
00195                      break;
00196 
00197               case 'v': /* verbose */
00198                      verbose++;
00199                      break;
00200 
00201               case 'V': /* version */
00202                      print_revision (progname, NP_VERSION);
00203                      exit (STATE_OK);
00204                      break;
00205 
00206               case 'H': /* help */
00207                      print_help();
00208                      exit(STATE_UNKNOWN);
00209                      break;
00210 
00211               default:
00212                      return ERROR;
00213                      break;
00214                }
00215        }
00216 
00217        if(data_vals==NULL)
00218               return ERROR;
00219 
00220        return OK;
00221 }
00222 
00223 void
00224 print_help(void)
00225 {
00226        print_revision(progname, NP_VERSION);
00227        printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
00228        printf(COPYRIGHT, copyright, email);
00229 
00230        printf(_("Host/Service Cluster Plugin for Nagios 2"));
00231        printf("\n\n");
00232 
00233        print_usage();
00234 
00235        printf("\n");
00236        printf("%s\n", _("Options:"));
00237        printf(UT_EXTRA_OPTS);
00238        printf (" %s\n", "-s, --service");
00239        printf ("    %s\n", _("Check service cluster status"));
00240        printf (" %s\n", "-h, --host");
00241        printf ("    %s\n", _("Check host cluster status"));
00242        printf (" %s\n", "-l, --label=STRING");
00243        printf ("    %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
00244        printf (" %s\n", "-w, --warning=THRESHOLD");
00245        printf ("    %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
00246        printf ("    %s\n", _("non-OK state in order to return a WARNING status level"));
00247        printf (" %s\n", "-c, --critical=THRESHOLD");
00248        printf ("    %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
00249        printf ("    %s\n", _("non-OK state in order to return a CRITICAL status level"));
00250        printf (" %s\n", "-d, --data=LIST");
00251        printf ("    %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
00252        printf ("    %s\n", _("commas"));
00253 
00254        printf(UT_VERBOSE);
00255 
00256        printf("\n");
00257        printf("%s\n", _("Notes:"));
00258        printf(UT_THRESHOLDS_NOTES);
00259 
00260        printf ("\n");
00261        printf ("%s\n", _("Examples:"));
00262        printf (" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:");
00263        printf ("    %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK") );
00264        printf ("    %s\n", _("state.") );
00265 
00266        printf(UT_SUPPORT);
00267 }
00268 
00269 
00270 void
00271 print_usage(void)
00272 {
00273 
00274        printf("%s\n", _("Usage:"));
00275        printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
00276        printf("[-w threshold] [-c threshold] [-v] [--help]\n");
00277 
00278 }
00279