1 /*****************************************************************************
2 *
3 * Nagios check_load plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2007 Nagios Plugins Development 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 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 *
30 * $Id$
31 *
32 *****************************************************************************/
34 const char *progname = "check_load";
35 const char *revision = "$Revision$";
36 const char *copyright = "1999-2007";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include "common.h"
40 #include "utils.h"
41 #include "popen.h"
43 #ifdef HAVE_SYS_LOADAVG_H
44 #include <sys/loadavg.h>
45 #endif
47 /* needed for compilation under NetBSD, as suggested by Andy Doran */
48 #ifndef LOADAVG_1MIN
49 #define LOADAVG_1MIN 0
50 #define LOADAVG_5MIN 1
51 #define LOADAVG_15MIN 2
52 #endif /* !defined LOADAVG_1MIN */
55 static int process_arguments (int argc, char **argv);
56 static int validate_arguments (void);
57 void print_help (void);
58 void print_usage (void);
60 /* strictly for pretty-print usage in loops */
61 static const int nums[3] = { 1, 5, 15 };
63 /* provide some fairly sane defaults */
64 double wload[3] = { 0.0, 0.0, 0.0 };
65 double cload[3] = { 0.0, 0.0, 0.0 };
66 #define la1 la[0]
67 #define la5 la[1]
68 #define la15 la[2]
70 char *status_line;
71 int take_into_account_cpus = 0;
73 static void
74 get_threshold(char *arg, double *th)
75 {
76 size_t i, n;
77 int valid = 0;
78 char *str = arg, *p;
80 n = strlen(arg);
81 for(i = 0; i < 3; i++) {
82 th[i] = strtod(str, &p);
83 if(p == str) break;
85 valid = 1;
86 str = p + 1;
87 if(n <= (size_t)(str - arg)) break;
88 }
90 /* empty argument or non-floatish, so warn about it and die */
91 if(!i && !valid) usage (_("Warning threshold must be float or float triplet!\n"));
93 if(i != 2) {
94 /* one or more numbers were given, so fill array with last
95 * we got (most likely to NOT produce the least expected result) */
96 for(n = i; n < 3; n++) th[n] = th[i];
97 }
98 }
101 int
102 main (int argc, char **argv)
103 {
104 int result;
105 int i;
106 long numcpus;
108 double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
109 #ifndef HAVE_GETLOADAVG
110 char input_buffer[MAX_INPUT_BUFFER];
111 # ifdef HAVE_PROC_LOADAVG
112 FILE *fp;
113 char *str, *next;
114 # endif
115 #endif
117 setlocale (LC_ALL, "");
118 bindtextdomain (PACKAGE, LOCALEDIR);
119 textdomain (PACKAGE);
120 setlocale(LC_NUMERIC, "POSIX");
122 if (process_arguments (argc, argv) == ERROR)
123 usage4 (_("Could not parse arguments"));
125 #ifdef HAVE_GETLOADAVG
126 result = getloadavg (la, 3);
127 if (result != 3)
128 return STATE_UNKNOWN;
129 #else
130 # ifdef HAVE_PROC_LOADAVG
131 fp = fopen (PROC_LOADAVG, "r");
132 if (fp == NULL) {
133 printf (_("Error opening %s\n"), PROC_LOADAVG);
134 return STATE_UNKNOWN;
135 }
137 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
138 str = (char *)input_buffer;
139 for(i = 0; i < 3; i++) {
140 la[i] = strtod(str, &next);
141 str = next;
142 }
143 }
145 fclose (fp);
146 # else
147 child_process = spopen (PATH_TO_UPTIME);
148 if (child_process == NULL) {
149 printf (_("Error opening %s\n"), PATH_TO_UPTIME);
150 return STATE_UNKNOWN;
151 }
152 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
153 if (child_stderr == NULL) {
154 printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
155 }
156 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
157 sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
159 result = spclose (child_process);
160 if (result) {
161 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
162 return STATE_UNKNOWN;
163 }
164 # endif
165 #endif
167 if (take_into_account_cpus == 1) {
168 if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
169 la[0] = la[0] / numcpus;
170 la[1] = la[1] / numcpus;
171 la[2] = la[2] / numcpus;
172 }
173 }
174 if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
175 #ifdef HAVE_GETLOADAVG
176 printf (_("Error in getloadavg()\n"));
177 #else
178 # ifdef HAVE_PROC_LOADAVG
179 printf (_("Error processing %s\n"), PROC_LOADAVG);
180 # else
181 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
182 # endif
183 #endif
184 return STATE_UNKNOWN;
185 }
187 /* we got this far, so assume OK until we've measured */
188 result = STATE_OK;
190 asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
192 for(i = 0; i < 3; i++) {
193 if(la[i] > cload[i]) {
194 result = STATE_CRITICAL;
195 break;
196 }
197 else if(la[i] > wload[i]) result = STATE_WARNING;
198 }
200 printf("%s - %s|", state_text(result), status_line);
201 for(i = 0; i < 3; i++)
202 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
204 putchar('\n');
205 return result;
206 }
209 /* process command-line arguments */
210 static int
211 process_arguments (int argc, char **argv)
212 {
213 int c = 0;
215 int option = 0;
216 static struct option longopts[] = {
217 {"warning", required_argument, 0, 'w'},
218 {"critical", required_argument, 0, 'c'},
219 {"percpu", no_argument, 0, 'r'},
220 {"version", no_argument, 0, 'V'},
221 {"help", no_argument, 0, 'h'},
222 {0, 0, 0, 0}
223 };
225 if (argc < 2)
226 return ERROR;
228 while (1) {
229 c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option);
231 if (c == -1 || c == EOF)
232 break;
234 switch (c) {
235 case 'w': /* warning time threshold */
236 get_threshold(optarg, wload);
237 break;
238 case 'c': /* critical time threshold */
239 get_threshold(optarg, cload);
240 break;
241 case 'r': /* Divide load average by number of CPUs */
242 take_into_account_cpus = 1;
243 break;
244 case 'V': /* version */
245 print_revision (progname, revision);
246 exit (STATE_OK);
247 case 'h': /* help */
248 print_help ();
249 exit (STATE_OK);
250 case '?': /* help */
251 usage5 ();
252 }
253 }
255 c = optind;
256 if (c == argc)
257 return validate_arguments ();
259 /* handle the case if both arguments are missing,
260 * but not if only one is given without -c or -w flag */
261 if(c - argc == 2) {
262 get_threshold(argv[c++], wload);
263 get_threshold(argv[c++], cload);
264 }
265 else if(c - argc == 1) {
266 get_threshold(argv[c++], cload);
267 }
269 return validate_arguments ();
270 }
274 static int
275 validate_arguments (void)
276 {
277 int i = 0;
279 /* match cload first, as it will give the most friendly error message
280 * if user hasn't given the -c switch properly */
281 for(i = 0; i < 3; i++) {
282 if(cload[i] < 0)
283 die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
284 if(wload[i] < 0)
285 die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
286 if(wload[i] > cload[i])
287 die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
288 }
290 return OK;
291 }
295 void
296 print_help (void)
297 {
298 print_revision (progname, revision);
300 printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
301 printf (COPYRIGHT, copyright, email);
303 printf (_("This plugin tests the current system load average."));
305 printf ("\n\n");
307 print_usage ();
309 printf (_(UT_HELP_VRSN));
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 printf (_(UT_SUPPORT));
320 }
322 void
323 print_usage (void)
324 {
325 printf (_("Usage:"));
326 printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);
327 }