Code

Fix compilation with GCC 2.96 (Konstantin Khomoutov - #2977105)
[nagiosplug.git] / plugins / check_cluster.c
index e60948d6d17ceb89a30c69c064e58b69c8b74704..89b1a0e1ca338305cdcaac5ec4d44ce6ca226bf6 100644 (file)
@@ -1,51 +1,40 @@
 /*****************************************************************************
- *
- * CHECK_CLUSTER2.C - Host and Service Cluster Plugin for Nagios 2.x
- *
- * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
- * License: GPL
- * Last Modified:   03-11-2004
- *
- * License:
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- *****************************************************************************/
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <getopt.h>
-
-#define OK             0
-#define ERROR          -1
-
-#define TRUE           1
-#define FALSE          0
+* 
+* check_cluster.c - Host and Service Cluster Plugin for Nagios 2.x
+* 
+* License: GPL
+* Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
+* Copyright (c) 2007 Nagios Plugins Development Team
+* 
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+* 
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+* 
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+* 
+* 
+*****************************************************************************/
+
+const char *progname = "check_cluster";
+const char *copyright = "2000-2007";
+const char *email = "nagiosplug-devel@lists.sourceforge.net";
+
+#include "common.h"
+#include "utils.h"
+#include "utils_base.h"
 
 #define CHECK_SERVICES 1
 #define CHECK_HOSTS    2
 
-#define MAX_INPUT_BUFFER       1024
-
-#define STATE_OK       0
-#define STATE_WARNING  1
-#define STATE_CRITICAL 2
-#define STATE_UNKNOWN  3
+void print_help (void);
+void print_usage (void);
 
 int total_services_ok=0;
 int total_services_warning=0;
@@ -56,52 +45,40 @@ int total_hosts_up=0;
 int total_hosts_down=0;
 int total_hosts_unreachable=0;
 
-int warning_threshold=1;
-int critical_threshold=1;
+char *warn_threshold;
+char *crit_threshold;
 
 int check_type=CHECK_SERVICES;
 
 char *data_vals=NULL;
 char *label=NULL;
 
+int verbose=0;
 
 int process_arguments(int,char **);
 
 
 
 int main(int argc, char **argv){
-       char input_buffer[MAX_INPUT_BUFFER];
        char *ptr;
        int data_val;
        int return_code=STATE_OK;
-       int error=FALSE;
-
-       if(process_arguments(argc,argv)==ERROR){
-
-               printf("Invalid arguments supplied\n");
-               printf("\n");
-
-               printf("Host/Service Cluster Plugin for Nagios 2\n");
-               printf("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
-               printf("Last Modified: 03-11-2004\n");
-               printf("License: GPL\n");
-               printf("\n");
-               printf("Usage: %s (-s | -h) [-l label] [-w threshold] [-c threshold] [-d val1,val2,...,valn]\n",argv[0]);
-               printf("\n");
-               printf("Options:\n");
-               printf("   -s, --service  = Check service cluster status\n");
-               printf("   -h, --host     = Check host cluster status\n");
-               printf("   -l, --label    = Optional prepended text output (i.e. \"Host cluster\")\n");
-               printf("   -w, --warning  = Specifies the number of hosts or services in cluster that must be in\n");
-               printf("                    a non-OK state in order to return a WARNING status level\n");
-               printf("   -c, --critical = Specifies the number of hosts or services in cluster that must be in\n");
-               printf("                    a non-OK state in order to return a CRITICAL status level\n");
-               printf("   -d, --data     = The status codes of the hosts or services in the cluster, separated\n");
-               printf("                    by commas\n");
-               printf("\n");
-
-               return STATE_UNKNOWN;
-               }
+       thresholds *thresholds = NULL;
+
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
+       /* Parse extra opts if any */
+       argv=np_extra_opts(&argc, argv, progname);
+
+       if(process_arguments(argc,argv)==ERROR)
+               usage(_("Could not parse arguments"));
+
+       /* Initialize the thresholds */
+       set_thresholds(&thresholds, warn_threshold, crit_threshold);
+       if(verbose)
+               print_thresholds("check_cluster", thresholds);
 
        /* check the data values */
        for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
@@ -124,8 +101,8 @@ int main(int argc, char **argv){
                                break;
                        default:
                                break;
-                               }
                        }
+               }
                else{
                        switch(data_val){
                        case 0:
@@ -139,48 +116,46 @@ int main(int argc, char **argv){
                                break;
                        default:
                                break;
-                               }
                        }
                }
-       
+        }
+
 
        /* return the status of the cluster */
        if(check_type==CHECK_SERVICES){
-               if((total_services_warning+total_services_unknown+total_services_critical) >= critical_threshold)
-                       return_code=STATE_CRITICAL;
-               else if((total_services_warning+total_services_unknown+total_services_critical) >= warning_threshold)
-                       return_code=STATE_WARNING;
-               else
-                       return_code=STATE_OK;
-               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);
-                }
+               return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
+               printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
+                       state_text(return_code), (label==NULL)?"Service cluster":label,
+                       total_services_ok,total_services_warning,
+                       total_services_unknown,total_services_critical);
+       }
        else{
-               if((total_hosts_down+total_hosts_unreachable) >= critical_threshold)
-                       return_code=STATE_CRITICAL;
-               else if((total_hosts_down+total_hosts_unreachable) >= warning_threshold)
-                       return_code=STATE_WARNING;
-               else
-                       return_code=STATE_OK;
-               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);
-                }
+               return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
+               printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
+                       state_text(return_code), (label==NULL)?"Host cluster":label,
+                       total_hosts_up,total_hosts_down,total_hosts_unreachable);
+       }
 
        return return_code;
-        }
+}
 
 
 
 int process_arguments(int argc, char **argv){
        int c;
        int option=0;
-       static struct option longopts[]={ 
+       static struct option longopts[]={
                {"data",     required_argument,0,'d'},
                {"warning",  required_argument,0,'w'},
                {"critical", required_argument,0,'c'},
                {"label",    required_argument,0,'l'},
                {"host",     no_argument,      0,'h'},
                {"service",  no_argument,      0,'s'},
+               {"verbose",  no_argument,      0,'v'},
+               {"version",  no_argument,      0,'V'},
+               {"help",     no_argument,      0,'H'},
                {0,0,0,0}
-               };
+       };
 
        /* no options were supplied */
        if(argc<2)
@@ -188,7 +163,7 @@ int process_arguments(int argc, char **argv){
 
        while(1){
 
-               c=getopt_long(argc,argv,"hsw:c:d:l:",longopts,&option);
+               c=getopt_long(argc,argv,"hHsvVw:c:d:l:",longopts,&option);
 
                if(c==-1 || c==EOF || c==1)
                        break;
@@ -204,11 +179,11 @@ int process_arguments(int argc, char **argv){
                        break;
 
                case 'w': /* warning threshold */
-                       warning_threshold=atoi(optarg);
+                       warn_threshold = strdup(optarg);
                        break;
 
                case 'c': /* warning threshold */
-                       critical_threshold=atoi(optarg);
+                       crit_threshold = strdup(optarg);
                        break;
 
                case 'd': /* data values */
@@ -219,14 +194,89 @@ int process_arguments(int argc, char **argv){
                        label=(char *)strdup(optarg);
                        break;
 
+               case 'v': /* verbose */
+                       verbose++;
+                       break;
+
+               case 'V': /* version */
+                       print_revision (progname, NP_VERSION);
+                       exit (STATE_OK);
+                       break;
+
+               case 'H': /* help */
+                       print_help();
+                       exit(STATE_UNKNOWN);
+                       break;
+
                default:
                        return ERROR;
                        break;
-                       }
-               }
+               }
+       }
 
        if(data_vals==NULL)
                return ERROR;
 
        return OK;
-        }
+}
+
+void
+print_help(void)
+{
+       print_revision(progname, NP_VERSION);
+       printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
+       printf(COPYRIGHT, copyright, email);
+
+       printf(_("Host/Service Cluster Plugin for Nagios 2"));
+       printf("\n\n");
+
+       print_usage();
+
+       printf("\n");
+       printf("%s\n", _("Options:"));
+       printf(_(UT_EXTRA_OPTS));
+       printf (" %s\n", "-s, --service");
+       printf ("    %s\n", _("Check service cluster status"));
+       printf (" %s\n", "-h, --host");
+       printf ("    %s\n", _("Check host cluster status"));
+       printf (" %s\n", "-l, --label=STRING");
+       printf ("    %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
+       printf (" %s\n", "-w, --warning=THRESHOLD");
+       printf ("    %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
+       printf ("    %s\n", _("non-OK state in order to return a WARNING status level"));
+       printf (" %s\n", "-c, --critical=THRESHOLD");
+       printf ("    %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
+       printf ("    %s\n", _("non-OK state in order to return a CRITICAL status level"));
+       printf (" %s\n", "-d, --data=LIST");
+       printf ("    %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
+       printf ("    %s\n", _("commas"));
+
+       printf(_(UT_VERBOSE));
+
+       printf("\n");
+       printf("%s\n", _("Notes:"));
+       printf(_(UT_THRESHOLDS_NOTES));
+#ifdef NP_EXTRA_OPTS
+       printf ("\n");
+       printf (_(UT_EXTRA_OPTS_NOTES));
+#endif
+
+       printf ("\n");
+       printf ("%s\n", _("Examples:"));
+       printf (" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:");
+       printf ("    %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK state") );
+
+       printf(_(UT_SUPPORT));
+}
+
+
+void
+print_usage(void)
+{
+
+       printf(_("Usage:"));
+       printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
+       printf("[-w threshold] [-c threshold] [-v] [--help]\n");
+
+}
+