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;
146 }
152 /* process command-line arguments */
153 int
154 process_arguments (int argc, char **argv)
155 {
156 int c = 0;
158 int option_index = 0;
159 static struct option long_options[] = {
160 {"warning", required_argument, 0, 'w'},
161 {"critical", required_argument, 0, 'c'},
162 {"version", no_argument, 0, 'V'},
163 {"help", no_argument, 0, 'h'},
164 {0, 0, 0, 0}
165 };
167 if (argc < 2)
168 return ERROR;
170 while (1) {
171 c = getopt_long (argc, argv, "Vhc:w:", long_options, &option_index);
173 if (c == -1 || c == EOF)
174 break;
176 switch (c) {
177 case 'w': /* warning time threshold */
178 if (is_intnonneg (optarg)) {
179 wload1 = atof (optarg);
180 wload5 = atof (optarg);
181 wload15 = atof (optarg);
182 break;
183 }
184 else if (strstr (optarg, ",") &&
185 sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
186 break;
187 else if (strstr (optarg, ":") &&
188 sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
189 break;
190 else
191 usage ("Warning threshold must be float or float triplet!\n");
192 break;
193 case 'c': /* critical time threshold */
194 if (is_intnonneg (optarg)) {
195 cload1 = atof (optarg);
196 cload5 = atof (optarg);
197 cload15 = atof (optarg);
198 break;
199 }
200 else if (strstr (optarg, ",") &&
201 sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
202 break;
203 else if (strstr (optarg, ":") &&
204 sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
205 break;
206 else
207 usage ("Critical threshold must be float or float triplet!\n");
208 break;
209 case 'V': /* version */
210 print_revision (progname, "$Revision$");
211 exit (STATE_OK);
212 case 'h': /* help */
213 print_help ();
214 exit (STATE_OK);
215 case '?': /* help */
216 usage ("Invalid argument\n");
217 }
218 }
220 c = optind;
221 if (c == argc)
222 return validate_arguments ();
223 if (wload1 < 0 && is_nonnegative (argv[c]))
224 wload1 = atof (argv[c++]);
226 if (c == argc)
227 return validate_arguments ();
228 if (cload1 < 0 && is_nonnegative (argv[c]))
229 cload1 = atof (argv[c++]);
231 if (c == argc)
232 return validate_arguments ();
233 if (wload5 < 0 && is_nonnegative (argv[c]))
234 wload5 = atof (argv[c++]);
236 if (c == argc)
237 return validate_arguments ();
238 if (cload5 < 0 && is_nonnegative (argv[c]))
239 cload5 = atof (argv[c++]);
241 if (c == argc)
242 return validate_arguments ();
243 if (wload15 < 0 && is_nonnegative (argv[c]))
244 wload15 = atof (argv[c++]);
246 if (c == argc)
247 return validate_arguments ();
248 if (cload15 < 0 && is_nonnegative (argv[c]))
249 cload15 = atof (argv[c++]);
251 return validate_arguments ();
252 }
258 int
259 validate_arguments (void)
260 {
261 if (wload1 < 0)
262 usage ("Warning threshold for 1-minute load average is not specified\n");
263 if (wload5 < 0)
264 usage ("Warning threshold for 5-minute load average is not specified\n");
265 if (wload15 < 0)
266 usage ("Warning threshold for 15-minute load average is not specified\n");
267 if (cload1 < 0)
268 usage ("Critical threshold for 1-minute load average is not specified\n");
269 if (cload5 < 0)
270 usage ("Critical threshold for 5-minute load average is not specified\n");
271 if (cload15 < 0)
272 usage ("Critical threshold for 15-minute load average is not specified\n");
273 if (wload1 > cload1)
274 usage ("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n");
275 if (wload5 > cload5)
276 usage ("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n");
277 if (wload15 > cload15)
278 usage ("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n");
279 return OK;
280 }
286 void
287 print_usage (void)
288 {
289 printf
290 ("Usage: check_load -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"
291 " check_load --version\n" " check_load --help\n");
292 }
298 void
299 print_help (void)
300 {
301 print_revision (progname, "$Revision$");
302 printf
303 ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"
304 "Copyright (c) 2000 Karl DeBisschop\n\n"
305 "This plugin tests the current system load average.\n\n");
306 print_usage ();
307 printf
308 ("\nOptions:\n"
309 " -w, --warning=WLOAD1,WLOAD5,WLOAD15\n"
310 " Exit with WARNING status if load average exceeds WLOADn\n"
311 " -c, --critical=CLOAD1,CLOAD5,CLOAD15\n"
312 " Exit with CRITICAL status if load average exceed CLOADn\n"
313 " -h, --help\n"
314 " Print detailed help screen\n"
315 " -V, --version\n"
316 " Print version information\n\n"
317 "the load average format is the same used by \"uptime\" and \"w\"\n\n");
318 support ();
319 }