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 char *str = arg, *p;
82 n = strlen(arg);
83 for(i = 0; i < 3; i++) {
84 th[i] = strtod(str, &p);
85 if(p == str) break;
87 str = p + 1;
88 if(n <= (size_t)(str - arg)) break;
89 }
91 /* empty argument or non-floatish, so warn about it and die */
92 if(!i) usage (_("Warning threshold must be float or float triplet!\n"));
94 if(i != 2) {
95 /* one or more numbers were given, so fill array with last
96 * we got (most likely to NOT produce the least expected result) */
97 for(n = i; n < 3; n++) th[n] = th[i];
98 }
99 }
102 int
103 main (int argc, char **argv)
104 {
105 int result;
106 int i;
107 long numcpus;
109 double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
110 #ifndef HAVE_GETLOADAVG
111 char input_buffer[MAX_INPUT_BUFFER];
112 # ifdef HAVE_PROC_LOADAVG
113 FILE *fp;
114 char *str, *next;
115 # endif
116 #endif
118 setlocale (LC_ALL, "");
119 bindtextdomain (PACKAGE, LOCALEDIR);
120 textdomain (PACKAGE);
121 setlocale(LC_NUMERIC, "POSIX");
123 if (process_arguments (argc, argv) == ERROR)
124 usage4 (_("Could not parse arguments"));
126 #ifdef HAVE_GETLOADAVG
127 result = getloadavg (la, 3);
128 if (result != 3)
129 return STATE_UNKNOWN;
130 #else
131 # ifdef HAVE_PROC_LOADAVG
132 fp = fopen (PROC_LOADAVG, "r");
133 if (fp == NULL) {
134 printf (_("Error opening %s\n"), PROC_LOADAVG);
135 return STATE_UNKNOWN;
136 }
138 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
139 str = (char *)input_buffer;
140 for(i = 0; i < 3; i++) {
141 la[i] = strtod(str, &next);
142 str = next;
143 }
144 }
146 fclose (fp);
147 # else
148 child_process = spopen (PATH_TO_UPTIME);
149 if (child_process == NULL) {
150 printf (_("Error opening %s\n"), PATH_TO_UPTIME);
151 return STATE_UNKNOWN;
152 }
153 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
154 if (child_stderr == NULL) {
155 printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
156 }
157 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
158 sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
160 result = spclose (child_process);
161 if (result) {
162 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
163 return STATE_UNKNOWN;
164 }
165 # endif
166 #endif
168 if (take_into_account_cpus == 1) {
169 if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
170 la[0] = la[0] / numcpus;
171 la[1] = la[1] / numcpus;
172 la[2] = la[2] / numcpus;
173 }
174 }
175 if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
176 #ifdef HAVE_GETLOADAVG
177 printf (_("Error in getloadavg()\n"));
178 #else
179 # ifdef HAVE_PROC_LOADAVG
180 printf (_("Error processing %s\n"), PROC_LOADAVG);
181 # else
182 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
183 # endif
184 #endif
185 return STATE_UNKNOWN;
186 }
188 /* we got this far, so assume OK until we've measured */
189 result = STATE_OK;
191 asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
193 for(i = 0; i < 3; i++) {
194 if(la[i] > cload[i]) {
195 result = STATE_CRITICAL;
196 break;
197 }
198 else if(la[i] > wload[i]) result = STATE_WARNING;
199 }
201 printf("%s - %s|", state_text(result), status_line);
202 for(i = 0; i < 3; i++)
203 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
205 putchar('\n');
206 return result;
207 }
210 /* process command-line arguments */
211 static int
212 process_arguments (int argc, char **argv)
213 {
214 int c = 0;
216 int option = 0;
217 static struct option longopts[] = {
218 {"warning", required_argument, 0, 'w'},
219 {"critical", required_argument, 0, 'c'},
220 {"percpu", no_argument, 0, 'r'},
221 {"version", no_argument, 0, 'V'},
222 {"help", no_argument, 0, 'h'},
223 {0, 0, 0, 0}
224 };
226 if (argc < 2)
227 return ERROR;
229 while (1) {
230 c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option);
232 if (c == -1 || c == EOF)
233 break;
235 switch (c) {
236 case 'w': /* warning time threshold */
237 get_threshold(optarg, wload);
238 break;
239 case 'c': /* critical time threshold */
240 get_threshold(optarg, cload);
241 break;
242 case 'r': /* Divide load average by number of CPUs */
243 take_into_account_cpus = 1;
244 break;
245 case 'V': /* version */
246 print_revision (progname, revision);
247 exit (STATE_OK);
248 case 'h': /* help */
249 print_help ();
250 exit (STATE_OK);
251 case '?': /* help */
252 usage5 ();
253 }
254 }
256 c = optind;
257 if (c == argc)
258 return validate_arguments ();
260 /* handle the case if both arguments are missing,
261 * but not if only one is given without -c or -w flag */
262 if(c - argc == 2) {
263 get_threshold(argv[c++], wload);
264 get_threshold(argv[c++], cload);
265 }
266 else if(c - argc == 1) {
267 get_threshold(argv[c++], cload);
268 }
270 return validate_arguments ();
271 }
275 static int
276 validate_arguments (void)
277 {
278 int i = 0;
280 /* match cload first, as it will give the most friendly error message
281 * if user hasn't given the -c switch properly */
282 for(i = 0; i < 3; i++) {
283 if(cload[i] < 0)
284 die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
285 if(wload[i] < 0)
286 die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
287 if(wload[i] > cload[i])
288 die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
289 }
291 return OK;
292 }
296 void
297 print_help (void)
298 {
299 print_revision (progname, revision);
301 printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
302 printf (COPYRIGHT, copyright, email);
304 printf (_("This plugin tests the current system load average."));
306 printf ("\n\n");
308 print_usage ();
310 printf (_(UT_HELP_VRSN));
312 printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
313 printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
314 printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
315 printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
316 printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
317 printf (" %s\n", "-r, --percpu");
318 printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
320 printf (_(UT_SUPPORT));
321 }
323 void
324 print_usage (void)
325 {
326 printf (_("Usage:"));
327 printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);
328 }