1 /******************************************************************************
2 *
3 * Nagios check_load plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_load plugin
13 *
14 * This plugin tests the current system load average.
15 *
16 *
17 * License Information:
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 $Id$
35 ******************************************************************************/
37 const char *progname = "check_load";
38 const char *revision = "$Revision$";
39 const char *copyright = "1999-2006";
40 const char *email = "nagiosplug-devel@lists.sourceforge.net";
42 #include "common.h"
43 #include "utils.h"
44 #include "popen.h"
46 #ifdef HAVE_SYS_LOADAVG_H
47 #include <sys/loadavg.h>
48 #endif
50 /* needed for compilation under NetBSD, as suggested by Andy Doran */
51 #ifndef LOADAVG_1MIN
52 #define LOADAVG_1MIN 0
53 #define LOADAVG_5MIN 1
54 #define LOADAVG_15MIN 2
55 #endif /* !defined LOADAVG_1MIN */
58 static int process_arguments (int argc, char **argv);
59 static int validate_arguments (void);
60 void print_help (void);
61 void print_usage (void);
63 /* strictly for pretty-print usage in loops */
64 static const int nums[3] = { 1, 5, 15 };
66 /* provide some fairly sane defaults */
67 double wload[3] = { 0.0, 0.0, 0.0 };
68 double cload[3] = { 0.0, 0.0, 0.0 };
69 #define la1 la[0]
70 #define la5 la[1]
71 #define la15 la[2]
73 char *status_line;
74 int take_into_account_cpus = 0;
76 static void
77 get_threshold(char *arg, double *th)
78 {
79 size_t i, n;
80 int valid = 0;
81 char *str = arg, *p;
83 n = strlen(arg);
84 for(i = 0; i < 3; i++) {
85 th[i] = strtod(str, &p);
86 if(p == str) break;
88 valid = 1;
89 str = p + 1;
90 if(n <= (size_t)(str - arg)) break;
91 }
93 /* empty argument or non-floatish, so warn about it and die */
94 if(!i && !valid) usage (_("Warning threshold must be float or float triplet!\n"));
96 if(i != 2) {
97 /* one or more numbers were given, so fill array with last
98 * we got (most likely to NOT produce the least expected result) */
99 for(n = i; n < 3; n++) th[n] = th[i];
100 }
101 }
104 int
105 main (int argc, char **argv)
106 {
107 int result;
108 int i;
109 long numcpus;
111 double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
112 #ifndef HAVE_GETLOADAVG
113 char input_buffer[MAX_INPUT_BUFFER];
114 # ifdef HAVE_PROC_LOADAVG
115 FILE *fp;
116 char *str, *next;
117 # endif
118 #endif
120 setlocale (LC_ALL, "");
121 bindtextdomain (PACKAGE, LOCALEDIR);
122 textdomain (PACKAGE);
123 setlocale(LC_NUMERIC, "POSIX");
125 if (process_arguments (argc, argv) == ERROR)
126 usage4 (_("Could not parse arguments"));
128 #ifdef HAVE_GETLOADAVG
129 result = getloadavg (la, 3);
130 if (result != 3)
131 return STATE_UNKNOWN;
132 #else
133 # ifdef HAVE_PROC_LOADAVG
134 fp = fopen (PROC_LOADAVG, "r");
135 if (fp == NULL) {
136 printf (_("Error opening %s\n"), PROC_LOADAVG);
137 return STATE_UNKNOWN;
138 }
140 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
141 str = (char *)input_buffer;
142 for(i = 0; i < 3; i++) {
143 la[i] = strtod(str, &next);
144 str = next;
145 }
146 }
148 fclose (fp);
149 # else
150 child_process = spopen (PATH_TO_UPTIME);
151 if (child_process == NULL) {
152 printf (_("Error opening %s\n"), PATH_TO_UPTIME);
153 return STATE_UNKNOWN;
154 }
155 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
156 if (child_stderr == NULL) {
157 printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
158 }
159 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
160 sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
162 result = spclose (child_process);
163 if (result) {
164 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
165 return STATE_UNKNOWN;
166 }
167 # endif
168 #endif
170 if (take_into_account_cpus == 1) {
171 if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
172 la[0] = la[0] / numcpus;
173 la[1] = la[1] / numcpus;
174 la[2] = la[2] / numcpus;
175 }
176 }
177 if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
178 #ifdef HAVE_GETLOADAVG
179 printf (_("Error in getloadavg()\n"));
180 #else
181 # ifdef HAVE_PROC_LOADAVG
182 printf (_("Error processing %s\n"), PROC_LOADAVG);
183 # else
184 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
185 # endif
186 #endif
187 return STATE_UNKNOWN;
188 }
190 /* we got this far, so assume OK until we've measured */
191 result = STATE_OK;
193 asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
195 for(i = 0; i < 3; i++) {
196 if(la[i] > cload[i]) {
197 result = STATE_CRITICAL;
198 break;
199 }
200 else if(la[i] > wload[i]) result = STATE_WARNING;
201 }
203 printf("%s - %s|", state_text(result), status_line);
204 for(i = 0; i < 3; i++)
205 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
207 putchar('\n');
208 return result;
209 }
212 /* process command-line arguments */
213 static int
214 process_arguments (int argc, char **argv)
215 {
216 int c = 0;
218 int option = 0;
219 static struct option longopts[] = {
220 {"warning", required_argument, 0, 'w'},
221 {"critical", required_argument, 0, 'c'},
222 {"percpu", no_argument, 0, 'r'},
223 {"version", no_argument, 0, 'V'},
224 {"help", no_argument, 0, 'h'},
225 {0, 0, 0, 0}
226 };
228 if (argc < 2)
229 return ERROR;
231 while (1) {
232 c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option);
234 if (c == -1 || c == EOF)
235 break;
237 switch (c) {
238 case 'w': /* warning time threshold */
239 get_threshold(optarg, wload);
240 break;
241 case 'c': /* critical time threshold */
242 get_threshold(optarg, cload);
243 break;
244 case 'r': /* Divide load average by number of CPUs */
245 take_into_account_cpus = 1;
246 break;
247 case 'V': /* version */
248 print_revision (progname, revision);
249 exit (STATE_OK);
250 case 'h': /* help */
251 print_help ();
252 exit (STATE_OK);
253 case '?': /* help */
254 usage5 ();
255 }
256 }
258 c = optind;
259 if (c == argc)
260 return validate_arguments ();
262 /* handle the case if both arguments are missing,
263 * but not if only one is given without -c or -w flag */
264 if(c - argc == 2) {
265 get_threshold(argv[c++], wload);
266 get_threshold(argv[c++], cload);
267 }
268 else if(c - argc == 1) {
269 get_threshold(argv[c++], cload);
270 }
272 return validate_arguments ();
273 }
277 static int
278 validate_arguments (void)
279 {
280 int i = 0;
282 /* match cload first, as it will give the most friendly error message
283 * if user hasn't given the -c switch properly */
284 for(i = 0; i < 3; i++) {
285 if(cload[i] < 0)
286 die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
287 if(wload[i] < 0)
288 die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
289 if(wload[i] > cload[i])
290 die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
291 }
293 return OK;
294 }
298 void
299 print_help (void)
300 {
301 print_revision (progname, revision);
303 printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
304 printf (COPYRIGHT, copyright, email);
306 printf (_("This plugin tests the current system load average."));
308 printf ("\n\n");
310 print_usage ();
312 printf (_(UT_HELP_VRSN));
314 printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
315 printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
316 printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
317 printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
318 printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
319 printf (" %s\n", "-r, --percpu");
320 printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
322 printf (_(UT_SUPPORT));
323 }
325 void
326 print_usage (void)
327 {
328 printf (_("Usage:"));
329 printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);
330 }