Code

Fixed the output messages (Bug 688729 - Jayjay)
[nagiosplug.git] / plugins / check_load.c
1 /******************************************************************************
2  *
3  * CHECK_LOAD.C
4  *
5  * Written by Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>
6  * License: GPL
7  * Command line: CHECK_LOAD <wload1> <cload1> <wload5> <cload5> <wload15> <cload15>
8  * First Written: 04/17/99 
9  *
10  * Modifications:
11  * 
12  * 05/18/1999 - Modified to work getloadavg where available, and use uptime
13  *              where neither proc or getloadavg are found.  Also use autoconf.
14  *                 mods by Karl DeBisschop (kdebiss@alum.mit.edu)
15  * 07/01/1999 - Added some #DEFINEs to allow compilation under NetBSD, as
16  *              suggested by Andy Doran.
17  *                 mods by Ethan Galstad (nagios@nagios.org)
18  * 07/17/1999 - Initialized la[] array to prevent NetBSD from complaining
19  *                 mods by Ethan Galstad (nagios@nagios.org)
20  * 08/18/1999 - Integrated some code with common plugin utilities
21  *                 mods by Ethan Galstad (nagios@nagios.org)
22  * $Date$
23  * Note: The load format is the same used by "uptime" and "w"
24  *
25  *****************************************************************************/
27 #include "config.h"
28 #include "common.h"
29 #include "utils.h"
31 #ifdef HAVE_SYS_LOADAVG_H
32 #include <sys/loadavg.h>
33 #endif
35 /* needed for compilation under NetBSD, as suggested by Andy Doran */
36 #ifndef LOADAVG_1MIN
37 #define LOADAVG_1MIN    0
38 #define LOADAVG_5MIN    1
39 #define LOADAVG_15MIN   2
40 #endif /* !defined LOADAVG_1MIN */
42 #include "popen.h"
43 #ifdef HAVE_PROC_LOADAVG
45 #endif
47 const char *progname = "check_load";
49 int process_arguments (int argc, char **argv);
50 int validate_arguments (void);
51 void print_usage (void);
52 void print_help (void);
54 float wload1 = -1, wload5 = -1, wload15 = -1;
55 float cload1 = -1, cload5 = -1, cload15 = -1;
57 char *status_line = "";
59 int
60 main (int argc, char **argv)
61 {
62 #if HAVE_GETLOADAVG==1
63         int result;
64         double la[3] = { 0.0, 0.0, 0.0 };       /* NetBSD complains about unitialized arrays */
65 #elif HAVE_PROC_LOADAVG==1
66         FILE *fp;
67         char input_buffer[MAX_INPUT_BUFFER];
68         char *tmp_ptr;
69 #else
70         int result;
71         char input_buffer[MAX_INPUT_BUFFER];
72 #endif
74         float la1, la5, la15;
76         if (process_arguments (argc, argv) == ERROR)
77                 usage ("\n");
79 #if HAVE_GETLOADAVG==1
80         result = getloadavg (la, 3);
81         if (result == -1)
82                 return STATE_UNKNOWN;
83         la1 = la[LOADAVG_1MIN];
84         la5 = la[LOADAVG_5MIN];
85         la15 = la[LOADAVG_15MIN];
86 #elif HAVE_PROC_LOADAVG==1
87         fp = fopen (PROC_LOADAVG, "r");
88         if (fp == NULL) {
89                 printf ("Error opening %s\n", PROC_LOADAVG);
90                 return STATE_UNKNOWN;
91         }
93         la1 = la5 = la15 = -1;
95         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
96                 tmp_ptr = strtok (input_buffer, " ");
97                 la1 = atof (tmp_ptr);
98                 tmp_ptr = strtok (NULL, " ");
99                 la5 = atof (tmp_ptr);
100                 tmp_ptr = strtok (NULL, " ");
101                 la15 = atof (tmp_ptr);
102         }
104         fclose (fp);
105 #else
106         child_process = spopen (PATH_TO_UPTIME);
107         if (child_process == NULL) {
108                 printf ("Error opening %s\n", PATH_TO_UPTIME);
109                 return STATE_UNKNOWN;
110         }
111         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
112         if (child_stderr == NULL) {
113                 printf ("Could not open stderr for %s\n", PATH_TO_UPTIME);
114         }
115         fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
116         sscanf (input_buffer, "%*[^l]load average: %f, %f, %f", &la1, &la5, &la15);
118         result = spclose (child_process);
119         if (result) {
120                 printf ("Error code %d returned in %s\n", result, PATH_TO_UPTIME);
121                 return STATE_UNKNOWN;
122         }
123 #endif
125         if ((la1 == -1) || (la5 == -1) || (la15 == -1)) {
126 #if HAVE_GETLOADAVG==1
127                 printf ("Error in getloadavg()\n");
128 #elif HAVE_PROC_LOADAVG==1
129                 printf ("Error processing %s\n", PROC_LOADAVG);
130 #else
131                 printf ("Error processing %s\n", PATH_TO_UPTIME);
132 #endif
133                 return STATE_UNKNOWN;
134         }
135         asprintf(&status_line, "load average: %.2f, %.2f, %.2f", la1, la5, la15);
136         if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
137                 printf("CRITICAL - %s\n", status_line);
138                 return STATE_CRITICAL;
139         }
140         if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
141                 printf ("WARNING - %s\n", status_line);
142                 return STATE_WARNING;
143         }
144         printf ("OK - %s\n", status_line);
145         return STATE_OK;
152 /* process command-line arguments */
153 int
154 process_arguments (int argc, char **argv)
156         int c = 0;
158 #ifdef HAVE_GETOPT_H
159         int option_index = 0;
160         static struct option long_options[] = {
161                 {"warning", required_argument, 0, 'w'},
162                 {"critical", required_argument, 0, 'c'},
163                 {"version", no_argument, 0, 'V'},
164                 {"help", no_argument, 0, 'h'},
165                 {0, 0, 0, 0}
166         };
167 #endif
169 #define OPTCHARS "Vhc:w:"
171         if (argc < 2)
172                 return ERROR;
174         while (1) {
175 #ifdef HAVE_GETOPT_H
176                 c = getopt_long (argc, argv, OPTCHARS, long_options, &option_index);
177 #else
178                 c = getopt (argc, argv, OPTCHARS);
179 #endif
180                 if (c == -1 || c == EOF)
181                         break;
183                 switch (c) {
184                 case 'w':                                                                       /* warning time threshold */
185                         if (is_intnonneg (optarg)) {
186                                 wload1 = atof (optarg);
187                                 wload5 = atof (optarg);
188                                 wload15 = atof (optarg);
189                                 break;
190                         }
191                         else if (strstr (optarg, ",") &&
192                                                                  sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
193                                 break;
194                         else if (strstr (optarg, ":") &&
195                                                          sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
196                                 break;
197                         else
198                                 usage ("Warning threshold must be float or float triplet!\n");
199                         break;
200                 case 'c':                                                                       /* critical time threshold */
201                         if (is_intnonneg (optarg)) {
202                                 cload1 = atof (optarg);
203                                 cload5 = atof (optarg);
204                                 cload15 = atof (optarg);
205                                 break;
206                         }
207                         else if (strstr (optarg, ",") &&
208                                                          sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
209                                 break;
210                         else if (strstr (optarg, ":") &&
211                                                          sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
212                                 break;
213                         else
214                                 usage ("Critical threshold must be float or float triplet!\n");
215                         break;
216                 case 'V':                                                                       /* version */
217                         print_revision (progname, "$Revision$");
218                         exit (STATE_OK);
219                 case 'h':                                                                       /* help */
220                         print_help ();
221                         exit (STATE_OK);
222                 case '?':                                                                       /* help */
223                         usage ("Invalid argument\n");
224                 }
225         }
227         c = optind;
228         if (c == argc)
229                 return validate_arguments ();
230         if (wload1 < 0 && is_nonnegative (argv[c]))
231                 wload1 = atof (argv[c++]);
233         if (c == argc)
234                 return validate_arguments ();
235         if (cload1 < 0 && is_nonnegative (argv[c]))
236                 cload1 = atof (argv[c++]);
238         if (c == argc)
239                 return validate_arguments ();
240         if (wload5 < 0 && is_nonnegative (argv[c]))
241                 wload5 = atof (argv[c++]);
243         if (c == argc)
244                 return validate_arguments ();
245         if (cload5 < 0 && is_nonnegative (argv[c]))
246                 cload5 = atof (argv[c++]);
248         if (c == argc)
249                 return validate_arguments ();
250         if (wload15 < 0 && is_nonnegative (argv[c]))
251                 wload15 = atof (argv[c++]);
253         if (c == argc)
254                 return validate_arguments ();
255         if (cload15 < 0 && is_nonnegative (argv[c]))
256                 cload15 = atof (argv[c++]);
258         return validate_arguments ();
265 int
266 validate_arguments (void)
268         if (wload1 < 0)
269                 usage ("Warning threshold for 1-minute load average is not specified\n");
270         if (wload5 < 0)
271                 usage ("Warning threshold for 5-minute load average is not specified\n");
272         if (wload15 < 0)
273                 usage ("Warning threshold for 15-minute load average is not specified\n");
274         if (cload1 < 0)
275                 usage ("Critical threshold for 1-minute load average is not specified\n");
276         if (cload5 < 0)
277                 usage ("Critical threshold for 5-minute load average is not specified\n");
278         if (cload15 < 0)
279                 usage ("Critical threshold for 15-minute load average is not specified\n");
280         if (wload1 > cload1)
281                 usage ("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n");
282         if (wload5 > cload5)
283                 usage ("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n");
284         if (wload15 > cload15)
285                 usage ("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n");
286         return OK;
293 void
294 print_usage (void)
296         printf
297                 ("Usage: check_load -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"
298                  "       check_load --version\n" "       check_load --help\n");
305 void
306 print_help (void)
308         print_revision (progname, "$Revision$");
309         printf
310                 ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"
311                  "Copyright (c) 2000 Karl DeBisschop\n\n"
312                  "This plugin tests the current system load average.\n\n");
313         print_usage ();
314         printf
315                 ("\nOptions:\n"
316                  " -w, --warning=WLOAD1,WLOAD5,WLOAD15\n"
317                  "   Exit with WARNING status if load average exceeds WLOADn\n"
318                  " -c, --critical=CLOAD1,CLOAD5,CLOAD15\n"
319                  "   Exit with CRITICAL status if load average exceed CLOADn\n"
320                  " -h, --help\n"
321                  "    Print detailed help screen\n"
322                  " -V, --version\n"
323                  "    Print version information\n\n"
324                  "the load average format is the same used by \"uptime\" and \"w\"\n\n");
325         support ();