1 /******************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 ******************************************************************************/
19 const char *progname = "check_load";
20 const char *revision = "$Revision$";
21 const char *copyright = "1999-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "utils.h"
26 #include "popen.h"
28 #ifdef HAVE_SYS_LOADAVG_H
29 #include <sys/loadavg.h>
30 #endif
32 /* needed for compilation under NetBSD, as suggested by Andy Doran */
33 #ifndef LOADAVG_1MIN
34 #define LOADAVG_1MIN 0
35 #define LOADAVG_5MIN 1
36 #define LOADAVG_15MIN 2
37 #endif /* !defined LOADAVG_1MIN */
40 int process_arguments (int argc, char **argv);
41 int validate_arguments (void);
42 void print_help (void);
43 void print_usage (void);
45 float wload1 = -1, wload5 = -1, wload15 = -1;
46 float cload1 = -1, cload5 = -1, cload15 = -1;
48 char *status_line;
54 \f
55 int
56 main (int argc, char **argv)
57 {
58 #if HAVE_GETLOADAVG==1
59 int result;
60 double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
61 #else
62 # if HAVE_PROC_LOADAVG==1
63 FILE *fp;
64 char input_buffer[MAX_INPUT_BUFFER];
65 char *tmp_ptr;
66 # else
67 int result;
68 char input_buffer[MAX_INPUT_BUFFER];
69 # endif
70 #endif
72 float la1, la5, la15;
74 setlocale (LC_ALL, "");
75 bindtextdomain (PACKAGE, LOCALEDIR);
76 textdomain (PACKAGE);
78 if (process_arguments (argc, argv) == ERROR)
79 usage ("failed processing arguments\n");
81 #if HAVE_GETLOADAVG==1
82 result = getloadavg (la, 3);
83 if (result == -1)
84 return STATE_UNKNOWN;
85 la1 = la[LOADAVG_1MIN];
86 la5 = la[LOADAVG_5MIN];
87 la15 = la[LOADAVG_15MIN];
88 #else
89 # if HAVE_PROC_LOADAVG==1
90 fp = fopen (PROC_LOADAVG, "r");
91 if (fp == NULL) {
92 printf (_("Error opening %s\n"), PROC_LOADAVG);
93 return STATE_UNKNOWN;
94 }
96 la1 = la5 = la15 = -1;
98 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
99 tmp_ptr = strtok (input_buffer, " ");
100 la1 = atof (tmp_ptr);
101 tmp_ptr = strtok (NULL, " ");
102 la5 = atof (tmp_ptr);
103 tmp_ptr = strtok (NULL, " ");
104 la15 = atof (tmp_ptr);
105 }
107 fclose (fp);
108 # else
109 child_process = spopen (PATH_TO_UPTIME);
110 if (child_process == NULL) {
111 printf (_("Error opening %s\n"), PATH_TO_UPTIME);
112 return STATE_UNKNOWN;
113 }
114 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
115 if (child_stderr == NULL) {
116 printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
117 }
118 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
119 sscanf (input_buffer, "%*[^l]load average: %f, %f, %f"), &la1, &la5, &la15);
121 result = spclose (child_process);
122 if (result) {
123 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
124 return STATE_UNKNOWN;
125 }
126 # endif
127 #endif
129 if ((la1 < 0.0) || (la5 < 0.0) || (la15 < 0.0)) {
130 #if HAVE_GETLOADAVG==1
131 printf (_("Error in getloadavg()\n"));
132 #else
133 # if HAVE_PROC_LOADAVG==1
134 printf (_("Error processing %s\n"), PROC_LOADAVG);
135 # else
136 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
137 # endif
138 #endif
139 return STATE_UNKNOWN;
140 }
141 asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
142 if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
143 printf(_("CRITICAL - %s\n"), status_line);
144 return STATE_CRITICAL;
145 }
146 if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
147 printf (_("WARNING - %s\n"), status_line);
148 return STATE_WARNING;
149 }
150 printf (_("OK - %s\n"), status_line);
151 return STATE_OK;
152 }
158 \f
159 /* process command-line arguments */
160 int
161 process_arguments (int argc, char **argv)
162 {
163 int c = 0;
165 int option = 0;
166 static struct option longopts[] = {
167 {"warning", required_argument, 0, 'w'},
168 {"critical", required_argument, 0, 'c'},
169 {"version", no_argument, 0, 'V'},
170 {"help", no_argument, 0, 'h'},
171 {0, 0, 0, 0}
172 };
174 if (argc < 2)
175 return ERROR;
177 while (1) {
178 c = getopt_long (argc, argv, "Vhc:w:", longopts, &option);
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 ();
259 }
265 int
266 validate_arguments (void)
267 {
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;
287 }
293 \f
294 void
295 print_help (void)
296 {
297 print_revision (progname, revision);
299 printf (_("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"));
300 printf (_(COPYRIGHT), copyright, email);
302 printf (_("This plugin tests the current system load average.\n\n"));
304 print_usage ();
306 printf (_(UT_HELP_VRSN));
308 printf (_("\
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\n\
313 the load average format is the same used by \"uptime\" and \"w\"\n\n"));
315 printf (_(UT_SUPPORT));
316 }
318 void
319 print_usage (void)
320 {
321 printf (_("Usage: %s -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"),
322 progname);
323 printf (_(UT_HLP_VRS), progname, progname);
324 }