Code

Fix check_radius returning OK on unexpected results
[nagiosplug.git] / plugins / check_swap.c
index fa989746d025d09569e5cb0ff5e977114cbea8c8..be20ca32ff1f86d8b11aa66cd5947cc8dcd69cb9 100644 (file)
@@ -1,39 +1,56 @@
-/******************************************************************************
- *
- * Program: Swap space plugin for Nagios
- * License: GPL
- *
- * License Information:
- *
- * 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.
- *
- * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
- *
- * $Id$
- *
- *****************************************************************************/
+/*****************************************************************************
+* 
+* Nagios check_disk plugin
+* 
+* License: GPL
+* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
+* Copyright (c) 2000-2007 Nagios Plugins Development Team
+* 
+* Description:
+* 
+* This file contains the check_disk plugin
+* 
+* 
+* 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_swap";
-const char *revision = "$Revision$";
-const char *copyright = "2000-2004";
+const char *copyright = "2000-2007";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
 #include "popen.h"
 #include "utils.h"
 
+#ifdef HAVE_DECL_SWAPCTL
+# ifdef HAVE_SYS_PARAM_H
+#  include <sys/param.h>
+# endif
+# ifdef HAVE_SYS_SWAP_H
+#  include <sys/swap.h>
+# endif
+# ifdef HAVE_SYS_STAT_H
+#  include <sys/stat.h>
+# endif
+#endif
+
+#ifndef SWAP_CONVERSION
+# define SWAP_CONVERSION 1
+#endif
+
 int check_swap (int usp, float free_swap_mb);
 int process_arguments (int argc, char **argv);
 int validate_arguments (void);
@@ -86,6 +103,9 @@ main (int argc, char **argv)
 
        status = strdup ("");
 
+       /* Parse extra opts if any */
+       argv=np_extra_opts (&argc, argv, progname);
+
        if (process_arguments (argc, argv) == ERROR)
                usage4 (_("Could not parse arguments"));
 
@@ -229,22 +249,33 @@ main (int argc, char **argv)
 #  ifdef CHECK_SWAP_SWAPCTL_SVR4
 
        /* get the number of active swap devices */
-       nswaps=swapctl(SC_GETNSWP, NULL);
+       if((nswaps=swapctl(SC_GETNSWP, NULL))== -1)
+               die(STATE_UNKNOWN, _("Error getting swap devices\n") );
+
+       if(nswaps == 0)
+               die(STATE_OK, _("SWAP OK: No swap devices defined\n"));
+
+       if(verbose >= 3)
+               printf("Found %d swap device(s)\n", nswaps);
 
        /* initialize swap table + entries */
        tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
+
+       if(tbl==NULL)
+               die(STATE_UNKNOWN, _("malloc() failed!\n"));
+
        memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
        tbl->swt_n=nswaps;
        for(i=0;i<nswaps;i++){
-               ent=&tbl->swt_ent[i];
-               ent->ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN);
+               if((tbl->swt_ent[i].ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN)) == NULL)
+                       die(STATE_UNKNOWN, _("malloc() failed!\n"));
        }
 
        /* and now, tally 'em up */
        swapctl_res=swapctl(SC_LIST, tbl);
        if(swapctl_res < 0){
                perror(_("swapctl failed: "));
-               result = STATE_WARNING;
+               die(STATE_UNKNOWN, _("Error in swapctl call\n"));
        }
 
        for(i=0;i<nswaps;i++){
@@ -286,7 +317,7 @@ main (int argc, char **argv)
        swapctl_res=swapctl(SWAP_STATS, ent, nswaps);
        if(swapctl_res < 0){
                perror(_("swapctl failed: "));
-               result = STATE_WARNING;
+               die(STATE_UNKNOWN, _("Error in swapctl call\n"));
        }
 
        for(i=0;i<nswaps;i++){
@@ -428,13 +459,13 @@ process_arguments (int argc, char **argv)
                        verbose++;
                        break;
                case 'V':                                                                       /* version */
-                       print_revision (progname, revision);
+                       print_revision (progname, NP_VERSION);
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
                        print_help ();
                        exit (STATE_OK);
                case '?':                                                                       /* error */
-                       usage2 (_("Unknown argument"), optarg);
+                       usage5 ();
                }
        }
 
@@ -472,7 +503,7 @@ validate_arguments (void)
                return ERROR;
        }
        else if (warn_percent < crit_percent) {
-               usage4 
+               usage4
                        (_("Warning percentage should be more than critical percentage"));
        }
        else if (warn_size_bytes < crit_size_bytes) {
@@ -487,32 +518,39 @@ validate_arguments (void)
 void
 print_help (void)
 {
-       print_revision (progname, revision);
+       print_revision (progname, NP_VERSION);
 
        printf (_(COPYRIGHT), copyright, email);
 
-       printf (_("Check swap space on local machine.\n\n"));
+       printf ("%s\n", _("Check swap space on local machine."));
+
+  printf ("\n\n");
 
        print_usage ();
 
        printf (_(UT_HELP_VRSN));
+       printf (_(UT_EXTRA_OPTS));
+
+       printf (" %s\n", "-w, --warning=INTEGER");
+  printf ("    %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free"));
+  printf (" %s\n", "-w, --warning=PERCENT%%");
+  printf ("    %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free"));
+  printf (" %s\n", "-c, --critical=INTEGER");
+  printf ("    %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free"));
+  printf (" %s\n", "-c, --critical=PERCENT%%");
+  printf ("    %s\n", _("Exit with CRITCAL status if less than PERCENT of swap space is free"));
+  printf (" %s\n", "-a, --allswaps");
+  printf ("    %s\n", _("Conduct comparisons for all swap partitions, one by one"));
+       printf (_(UT_VERBOSE));
+
+       printf ("\n");
+  printf ("%s\n", _("Notes:"));
+  printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s."));
+#ifdef NP_EXTRA_OPTS
+  printf ("\n");
+  printf (_(UT_EXTRA_OPTS_NOTES));
+#endif
 
-       printf (_("\n\
- -w, --warning=INTEGER\n\
-   Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
- -w, --warning=PERCENT%%\n\
-   Exit with WARNING status if less than PERCENT of swap space is free\n\
- -c, --critical=INTEGER\n\
-   Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
- -c, --critical=PERCENT%%\n\
-   Exit with CRITCAL status if less than PERCENT of swap space is free\n\
- -a, --allswaps\n\
-    Conduct comparisons for all swap partitions, one by one\n\
- -v, --verbose\n\
-    Verbose output. Up to 3 levels\n"));
-
-       printf (_("\n\
-On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
 
        printf (_(UT_SUPPORT));
 }
@@ -522,7 +560,7 @@ On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
 void
 print_usage (void)
 {
-       printf ("\
-Usage: %s [-av] -w <percent_free>%% -c <percent_free>%%\n\
-       %s [-av] -w <bytes_free> -c <bytes_free>\n", progname, progname);
+       printf (_("Usage:"));
+  printf ("%s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname);
+  printf ("%s [-av] -w <bytes_free> -c <bytes_free>\n", progname);
 }