Code

Fix translations when extra-opts aren't enabled
[nagiosplug.git] / plugins / check_load.c
index 21627feecf6a9e520098efa5bd8ecdb3bd59edc4..450a4fc3bc3afa99c9017b12bb447629c0c6e2e5 100644 (file)
@@ -1,26 +1,35 @@
-/******************************************************************************
-
- 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.
- $Id$
-
-******************************************************************************/
+/*****************************************************************************
+* 
+* Nagios check_load plugin
+* 
+* License: GPL
+* Copyright (c) 1999-2007 Nagios Plugins Development Team
+* 
+* Description:
+* 
+* This file contains the check_load plugin
+* 
+* This plugin tests the current system load average.
+* 
+* 
+* 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_load";
-const char *revision = "$Revision$";
-const char *copyright = "1999-2004";
+const char *copyright = "1999-2007";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
@@ -55,11 +64,13 @@ double cload[3] = { 0.0, 0.0, 0.0 };
 #define la15 la[2]
 
 char *status_line;
+int take_into_account_cpus = 0;
 
 static void
 get_threshold(char *arg, double *th)
 {
        size_t i, n;
+       int valid = 0;
        char *str = arg, *p;
 
        n = strlen(arg);
@@ -67,12 +78,13 @@ get_threshold(char *arg, double *th)
                th[i] = strtod(str, &p);
                if(p == str) break;
 
+               valid = 1;
                str = p + 1;
                if(n <= (size_t)(str - arg)) break;
        }
 
        /* empty argument or non-floatish, so warn about it and die */
-       if(!i) usage (_("Warning threshold must be float or float triplet!\n"));
+       if(!i && !valid) usage (_("Warning threshold must be float or float triplet!\n"));
 
        if(i != 2) {
                /* one or more numbers were given, so fill array with last
@@ -87,6 +99,7 @@ main (int argc, char **argv)
 {
        int result;
        int i;
+       long numcpus;
 
        double la[3] = { 0.0, 0.0, 0.0 };       /* NetBSD complains about unitialized arrays */
 #ifndef HAVE_GETLOADAVG
@@ -102,6 +115,9 @@ main (int argc, char **argv)
        textdomain (PACKAGE);
        setlocale(LC_NUMERIC, "POSIX");
 
+       /* Parse extra opts if any */
+       argv = np_extra_opts (&argc, argv, progname);
+
        if (process_arguments (argc, argv) == ERROR)
                usage4 (_("Could not parse arguments"));
 
@@ -137,7 +153,7 @@ main (int argc, char **argv)
                printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
        }
        fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
-       sscanf (input_buffer, "%*[^l]load average: %f, %f, %f", &la1, &la5, &la15);
+       sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
 
        result = spclose (child_process);
        if (result) {
@@ -147,6 +163,13 @@ main (int argc, char **argv)
 # endif
 #endif
 
+       if (take_into_account_cpus == 1) {
+               if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
+                       la[0] = la[0] / numcpus;
+                       la[1] = la[1] / numcpus;
+                       la[2] = la[2] / numcpus;
+               }
+       }
        if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
 #ifdef HAVE_GETLOADAVG
                printf (_("Error in getloadavg()\n"));
@@ -192,6 +215,7 @@ process_arguments (int argc, char **argv)
        static struct option longopts[] = {
                {"warning", required_argument, 0, 'w'},
                {"critical", required_argument, 0, 'c'},
+               {"percpu", no_argument, 0, 'r'},
                {"version", no_argument, 0, 'V'},
                {"help", no_argument, 0, 'h'},
                {0, 0, 0, 0}
@@ -201,7 +225,7 @@ process_arguments (int argc, char **argv)
                return ERROR;
 
        while (1) {
-               c = getopt_long (argc, argv, "Vhc:w:", longopts, &option);
+               c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option);
 
                if (c == -1 || c == EOF)
                        break;
@@ -213,14 +237,17 @@ process_arguments (int argc, char **argv)
                case 'c': /* critical time threshold */
                        get_threshold(optarg, cload);
                        break;
+               case 'r': /* Divide load average by number of CPUs */
+                       take_into_account_cpus = 1;
+                       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 '?':                                                                       /* help */
-                       usage2 (_("Unknown argument"), optarg);
+                       usage5 ();
                }
        }
 
@@ -267,29 +294,40 @@ validate_arguments (void)
 void
 print_help (void)
 {
-       print_revision (progname, revision);
+       print_revision (progname, NP_VERSION);
 
        printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
        printf (COPYRIGHT, copyright, email);
 
-       printf (_("This plugin tests the current system load average.\n\n"));
+       printf (_("This plugin tests the current system load average."));
 
-       print_usage ();
+  printf ("\n\n");
 
-       printf (_(UT_HELP_VRSN));
+       print_usage ();
 
-       printf (_("\
- -w, --warning=WLOAD1,WLOAD5,WLOAD15\n\
-   Exit with WARNING status if load average exceeds WLOADn\n\
- -c, --critical=CLOAD1,CLOAD5,CLOAD15\n\
-   Exit with CRITICAL status if load average exceed CLOADn\n\n\
-the load average format is the same used by \"uptime\" and \"w\"\n\n"));
+       printf (UT_HELP_VRSN);
+       printf (UT_EXTRA_OPTS);
+
+       printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
+  printf ("    %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
+  printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
+  printf ("    %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
+  printf ("    %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
+  printf (" %s\n", "-r, --percpu");
+  printf ("    %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
+
+#ifdef NP_EXTRA_OPTS
+       printf ("\n");
+       printf ("%s\n", _("Notes:"));
+       printf (UT_EXTRA_OPTS_NOTES);
+#endif
 
-       printf (_(UT_SUPPORT));
+       printf (UT_SUPPORT);
 }
 
 void
 print_usage (void)
 {
-       printf ("Usage: %s -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);
+  printf (_("Usage:"));
+       printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);
 }