Code

remove unused variables
[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 #define 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 int
58 main (int argc, char **argv)
59 {
60 #if HAVE_GETLOADAVG==1
61         int result;
62         double la[3] = { 0.0, 0.0, 0.0 };       /* NetBSD complains about unitialized arrays */
63 #elif HAVE_PROC_LOADAVG==1
64         FILE *fp;
65         char input_buffer[MAX_INPUT_BUFFER];
66         char *tmp_ptr;
67 #else
68         int result;
69         char input_buffer[MAX_INPUT_BUFFER];
70 #endif
72         float la1, la5, la15;
74         if (process_arguments (argc, argv) == ERROR)
75                 usage ("\n");
77 #if HAVE_GETLOADAVG==1
78         result = getloadavg (la, 3);
79         if (result == -1)
80                 return STATE_UNKNOWN;
81         la1 = la[LOADAVG_1MIN];
82         la5 = la[LOADAVG_5MIN];
83         la15 = la[LOADAVG_15MIN];
84 #elif HAVE_PROC_LOADAVG==1
85         fp = fopen (PROC_LOADAVG, "r");
86         if (fp == NULL) {
87                 printf ("Error opening %s\n", PROC_LOADAVG);
88                 return STATE_UNKNOWN;
89         }
91         la1 = la5 = la15 = -1;
93         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
94                 tmp_ptr = strtok (input_buffer, " ");
95                 la1 = atof (tmp_ptr);
96                 tmp_ptr = strtok (NULL, " ");
97                 la5 = atof (tmp_ptr);
98                 tmp_ptr = strtok (NULL, " ");
99                 la15 = atof (tmp_ptr);
100         }
102         fclose (fp);
103 #else
104         child_process = spopen (PATH_TO_UPTIME);
105         if (child_process == NULL) {
106                 printf ("Error opening %s\n", PATH_TO_UPTIME);
107                 return STATE_UNKNOWN;
108         }
109         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
110         if (child_stderr == NULL) {
111                 printf ("Could not open stderr for %s\n", PATH_TO_UPTIME);
112         }
113         fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
114         sscanf (input_buffer, "%*[^l]load average: %f, %f, %f", &la1, &la5, &la15);
116         result = spclose (child_process);
117         if (result) {
118                 printf ("Error code %d returned in %s\n", result, PATH_TO_UPTIME);
119                 return STATE_UNKNOWN;
120         }
121 #endif
123         if ((la1 == -1) || (la5 == -1) || (la15 == -1)) {
124 #if HAVE_GETLOADAVG==1
125                 printf ("Error in getloadavg()\n");
126 #elif HAVE_PROC_LOADAVG==1
127                 printf ("Error processing %s\n", PROC_LOADAVG);
128 #else
129                 printf ("Error processing %s\n", PATH_TO_UPTIME);
130 #endif
131                 return STATE_UNKNOWN;
132         }
133         printf ("load average: %.2f, %.2f, %.2f", la1, la5, la15);
134         if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
135                 printf (" CRITICAL\n");
136                 return STATE_CRITICAL;
137         }
138         if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
139                 printf (" WARNING\n");
140                 return STATE_WARNING;
141         }
142         printf ("\n");
143         return STATE_OK;
150 /* process command-line arguments */
151 int
152 process_arguments (int argc, char **argv)
154         int c = 0;
156 #ifdef HAVE_GETOPT_H
157         int option_index = 0;
158         static struct option long_options[] = {
159                 {"warning", required_argument, 0, 'w'},
160                 {"critical", required_argument, 0, 'c'},
161                 {"version", no_argument, 0, 'V'},
162                 {"help", no_argument, 0, 'h'},
163                 {0, 0, 0, 0}
164         };
165 #endif
167 #define OPTCHARS "Vhc:w:"
169         if (argc < 2)
170                 return ERROR;
172         while (1) {
173 #ifdef HAVE_GETOPT_H
174                 c = getopt_long (argc, argv, OPTCHARS, long_options, &option_index);
175 #else
176                 c = getopt (argc, argv, OPTCHARS);
177 #endif
178                 if (c == -1 || c == EOF)
179                         break;
181                 switch (c) {
182                 case 'w':                                                                       /* warning time threshold */
183                         if (is_intnonneg (optarg)) {
184                                 wload1 = atof (optarg);
185                                 wload5 = atof (optarg);
186                                 wload15 = atof (optarg);
187                                 break;
188                         }
189                         else if (strstr (optarg, ",") &&
190                                                                  sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
191                                 break;
192                         else if (strstr (optarg, ":") &&
193                                                          sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
194                                 break;
195                         else
196                                 usage ("Warning threshold must be float or float triplet!\n");
197                         break;
198                 case 'c':                                                                       /* critical time threshold */
199                         if (is_intnonneg (optarg)) {
200                                 cload1 = atof (optarg);
201                                 cload5 = atof (optarg);
202                                 cload15 = atof (optarg);
203                                 break;
204                         }
205                         else if (strstr (optarg, ",") &&
206                                                          sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
207                                 break;
208                         else if (strstr (optarg, ":") &&
209                                                          sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
210                                 break;
211                         else
212                                 usage ("Critical threshold must be float or float triplet!\n");
213                         break;
214                 case 'V':                                                                       /* version */
215                         print_revision (my_basename (argv[0]), "$Revision$");
216                         exit (STATE_OK);
217                 case 'h':                                                                       /* help */
218                         print_help ();
219                         exit (STATE_OK);
220                 case '?':                                                                       /* help */
221                         usage ("Invalid argument\n");
222                 }
223         }
225         c = optind;
226         if (c == argc)
227                 return validate_arguments ();
228         if (wload1 < 0 && is_nonnegative (argv[c]))
229                 wload1 = atof (argv[c++]);
231         if (c == argc)
232                 return validate_arguments ();
233         if (cload1 < 0 && is_nonnegative (argv[c]))
234                 cload1 = atof (argv[c++]);
236         if (c == argc)
237                 return validate_arguments ();
238         if (wload5 < 0 && is_nonnegative (argv[c]))
239                 wload5 = atof (argv[c++]);
241         if (c == argc)
242                 return validate_arguments ();
243         if (cload5 < 0 && is_nonnegative (argv[c]))
244                 cload5 = atof (argv[c++]);
246         if (c == argc)
247                 return validate_arguments ();
248         if (wload15 < 0 && is_nonnegative (argv[c]))
249                 wload15 = atof (argv[c++]);
251         if (c == argc)
252                 return validate_arguments ();
253         if (cload15 < 0 && is_nonnegative (argv[c]))
254                 cload15 = atof (argv[c++]);
256         return validate_arguments ();
263 int
264 validate_arguments (void)
266         if (wload1 < 0)
267                 usage ("Warning threshold for 1-minute load average is not specified\n");
268         if (wload5 < 0)
269                 usage ("Warning threshold for 5-minute load average is not specified\n");
270         if (wload15 < 0)
271                 usage ("Warning threshold for 15-minute load average is not specified\n");
272         if (cload1 < 0)
273                 usage ("Critical threshold for 1-minute load average is not specified\n");
274         if (cload5 < 0)
275                 usage ("Critical threshold for 5-minute load average is not specified\n");
276         if (cload15 < 0)
277                 usage ("Critical threshold for 15-minute load average is not specified\n");
278         if (wload1 > cload1)
279                 usage ("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n");
280         if (wload5 > cload5)
281                 usage ("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n");
282         if (wload15 > cload15)
283                 usage ("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n");
284         return OK;
291 void
292 print_usage (void)
294         printf
295                 ("Usage: check_load -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"
296                  "       check_load --version\n" "       check_load --help\n");
303 void
304 print_help (void)
306         print_revision (PROGNAME, "$Revision$");
307         printf
308                 ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"
309                  "Copyright (c) 2000 Karl DeBisschop\n\n"
310                  "This plugin tests the current system load average.\n\n");
311         print_usage ();
312         printf
313                 ("\nOptions:\n"
314                  " -w, --warning=WLOAD1,WLOAD5,WLOAD15\n"
315                  "   Exit with WARNING status if load average exceeds WLOADn\n"
316                  " -c, --critical=CLOAD1,CLOAD5,CLOAD15\n"
317                  "   Exit with CRITICAL status if load average exceed CLOADn\n"
318                  " -h, --help\n"
319                  "    Print detailed help screen\n"
320                  " -V, --version\n"
321                  "    Print version information\n\n"
322                  "the load average format is the same used by \"uptime\" and \"w\"\n\n");
323         support ();