Code

Added getaddrinfo.[ch] & gethostbyname.[ch] to provide RFC2553 functions
[nagiosplug.git] / plugins / check_load.c
index 6673b1dcaf55ae0ec6923e507a33e45be417e1a8..7d60d2343a85d2ee1054c5f2f61ca09474b58b0b 100644 (file)
 
 #endif
 
-#define PROGNAME "check_load"
+const char *progname = "check_load";
 
 int process_arguments (int argc, char **argv);
-int call_getopt (int argc, char **argv);
 int validate_arguments (void);
 void print_usage (void);
 void print_help (void);
@@ -55,6 +54,8 @@ void print_help (void);
 float wload1 = -1, wload5 = -1, wload15 = -1;
 float cload1 = -1, cload5 = -1, cload15 = -1;
 
+char *status_line = "";
+
 int
 main (int argc, char **argv)
 {
@@ -131,16 +132,16 @@ main (int argc, char **argv)
 #endif
                return STATE_UNKNOWN;
        }
-       printf ("load average: %.2f, %.2f, %.2f", la1, la5, la15);
+       asprintf(&status_line, "load average: %.2f, %.2f, %.2f", la1, la5, la15);
        if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
-               printf (" CRITICAL\n");
+               printf("CRITICAL - %s\n", status_line);
                return STATE_CRITICAL;
        }
        if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
-               printf (" WARNING\n");
+               printf ("WARNING - %s\n", status_line);
                return STATE_WARNING;
        }
-       printf ("\n");
+       printf ("OK - %s\n", status_line);
        return STATE_OK;
 }
 
@@ -152,41 +153,7 @@ main (int argc, char **argv)
 int
 process_arguments (int argc, char **argv)
 {
-       int c;
-
-       if (argc < 2)
-               return ERROR;
-
-       c = 0;
-       while (c += (call_getopt (argc - c, &argv[c]))) {
-               if (argc <= c)
-                       break;
-
-               if (wload1 < 0 && is_nonnegative (argv[c]))
-                       wload1 = atof (argv[c]);
-               else if (cload1 < 0 && is_nonnegative (argv[c]))
-                       cload1 = atof (argv[c]);
-               else if (wload5 < 0 && is_nonnegative (argv[c]))
-                       wload5 = atof (argv[c]);
-               else if (cload5 < 0 && is_nonnegative (argv[c]))
-                       cload5 = atof (argv[c]);
-               else if (wload15 < 0 && is_nonnegative (argv[c]))
-                       wload15 = atof (argv[c]);
-               else if (cload15 < 0 && is_nonnegative (argv[c]))
-                       cload15 = atof (argv[c]);
-       }
-
-       return validate_arguments ();
-}
-
-
-
-
-
-int
-call_getopt (int argc, char **argv)
-{
-       int c, i = 0;
+       int c = 0;
 
 #ifdef HAVE_GETOPT_H
        int option_index = 0;
@@ -199,61 +166,55 @@ call_getopt (int argc, char **argv)
        };
 #endif
 
+#define OPTCHARS "Vhc:w:"
+
+       if (argc < 2)
+               return ERROR;
+
        while (1) {
 #ifdef HAVE_GETOPT_H
-               c = getopt_long (argc, argv, "+?Vhc:w:", long_options, &option_index);
+               c = getopt_long (argc, argv, OPTCHARS, long_options, &option_index);
 #else
-               c = getopt (argc, argv, "+?Vhc:w:");
+               c = getopt (argc, argv, OPTCHARS);
 #endif
-
-               i++;
-
                if (c == -1 || c == EOF)
                        break;
 
-               switch (c) {
-               case 'c':
-               case 'w':
-                       i++;
-               }
-
                switch (c) {
                case 'w':                                                                       /* warning time threshold */
                        if (is_intnonneg (optarg)) {
-                               if (wload1 < 0 && is_nonnegative (argv[c]))
-                                       wload1 = atof (argv[c]);
-                               else if (wload5 < 0 && is_nonnegative (argv[c]))
-                                       wload5 = atof (argv[c]);
-                               else if (wload15 < 0 && is_nonnegative (argv[c]))
-                                       wload15 = atof (argv[c]);
+                               wload1 = atof (optarg);
+                               wload5 = atof (optarg);
+                               wload15 = atof (optarg);
                                break;
                        }
                        else if (strstr (optarg, ",") &&
-                                                        sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3) {
+                                                                sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
                                break;
-                       }
-                       else {
+                       else if (strstr (optarg, ":") &&
+                                                        sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
+                               break;
+                       else
                                usage ("Warning threshold must be float or float triplet!\n");
-                       }
+                       break;
                case 'c':                                                                       /* critical time threshold */
                        if (is_intnonneg (optarg)) {
-                               if (cload1 < 0 && is_nonnegative (argv[c]))
-                                       cload1 = atof (argv[c]);
-                               else if (cload5 < 0 && is_nonnegative (argv[c]))
-                                       cload5 = atof (argv[c]);
-                               else if (cload15 < 0 && is_nonnegative (argv[c]))
-                                       cload15 = atof (argv[c]);
+                               cload1 = atof (optarg);
+                               cload5 = atof (optarg);
+                               cload15 = atof (optarg);
                                break;
                        }
                        else if (strstr (optarg, ",") &&
-                                                        sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3) {
+                                                        sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
                                break;
-                       }
-                       else {
+                       else if (strstr (optarg, ":") &&
+                                                        sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
+                               break;
+                       else
                                usage ("Critical threshold must be float or float triplet!\n");
-                       }
+                       break;
                case 'V':                                                                       /* version */
-                       print_revision (my_basename (argv[0]), "$Revision$");
+                       print_revision (progname, "$Revision$");
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
                        print_help ();
@@ -262,7 +223,39 @@ call_getopt (int argc, char **argv)
                        usage ("Invalid argument\n");
                }
        }
-       return i;
+
+       c = optind;
+       if (c == argc)
+               return validate_arguments ();
+       if (wload1 < 0 && is_nonnegative (argv[c]))
+               wload1 = atof (argv[c++]);
+
+       if (c == argc)
+               return validate_arguments ();
+       if (cload1 < 0 && is_nonnegative (argv[c]))
+               cload1 = atof (argv[c++]);
+
+       if (c == argc)
+               return validate_arguments ();
+       if (wload5 < 0 && is_nonnegative (argv[c]))
+               wload5 = atof (argv[c++]);
+
+       if (c == argc)
+               return validate_arguments ();
+       if (cload5 < 0 && is_nonnegative (argv[c]))
+               cload5 = atof (argv[c++]);
+
+       if (c == argc)
+               return validate_arguments ();
+       if (wload15 < 0 && is_nonnegative (argv[c]))
+               wload15 = atof (argv[c++]);
+
+       if (c == argc)
+               return validate_arguments ();
+       if (cload15 < 0 && is_nonnegative (argv[c]))
+               cload15 = atof (argv[c++]);
+
+       return validate_arguments ();
 }
 
 
@@ -272,12 +265,24 @@ call_getopt (int argc, char **argv)
 int
 validate_arguments (void)
 {
-       if ((wload1 > cload1) || (wload5 > cload5) || (wload15 > cload15)) {
-               printf
-                       ("Inconsistence in parameters: \"warning load\" greater than \"critical load\".\n");
-               return STATE_UNKNOWN;
-       }
-
+       if (wload1 < 0)
+               usage ("Warning threshold for 1-minute load average is not specified\n");
+       if (wload5 < 0)
+               usage ("Warning threshold for 5-minute load average is not specified\n");
+       if (wload15 < 0)
+               usage ("Warning threshold for 15-minute load average is not specified\n");
+       if (cload1 < 0)
+               usage ("Critical threshold for 1-minute load average is not specified\n");
+       if (cload5 < 0)
+               usage ("Critical threshold for 5-minute load average is not specified\n");
+       if (cload15 < 0)
+               usage ("Critical threshold for 15-minute load average is not specified\n");
+       if (wload1 > cload1)
+               usage ("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n");
+       if (wload5 > cload5)
+               usage ("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n");
+       if (wload15 > cload15)
+               usage ("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n");
        return OK;
 }
 
@@ -300,7 +305,7 @@ print_usage (void)
 void
 print_help (void)
 {
-       print_revision (PROGNAME, "$Revision$");
+       print_revision (progname, "$Revision$");
        printf
                ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"
                 "Copyright (c) 2000 Karl DeBisschop\n\n"