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