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 $Id$
19 ******************************************************************************/
21 const char *progname = "check_load";
22 const char *revision = "$Revision$";
23 const char *copyright = "1999-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "utils.h"
28 #include "popen.h"
30 #ifdef HAVE_SYS_LOADAVG_H
31 #include <sys/loadavg.h>
32 #endif
34 /* needed for compilation under NetBSD, as suggested by Andy Doran */
35 #ifndef LOADAVG_1MIN
36 #define LOADAVG_1MIN 0
37 #define LOADAVG_5MIN 1
38 #define LOADAVG_15MIN 2
39 #endif /* !defined LOADAVG_1MIN */
42 static int process_arguments (int argc, char **argv);
43 static int validate_arguments (void);
44 void print_help (void);
45 void print_usage (void);
47 /* strictly for pretty-print usage in loops */
48 static const int nums[3] = { 1, 5, 15 };
50 /* provide some fairly sane defaults */
51 double wload[3] = { 0.0, 0.0, 0.0 };
52 double cload[3] = { 0.0, 0.0, 0.0 };
53 #define la1 la[0]
54 #define la5 la[1]
55 #define la15 la[2]
57 char *status_line;
59 static void
60 get_threshold(char *arg, double *th)
61 {
62 size_t i, n;
63 char *str = arg, *p;
65 n = strlen(arg);
66 for(i = 0; i < 3; i++) {
67 th[i] = strtod(str, &p);
68 if(p == str) break;
70 str = p + 1;
71 if(n <= (size_t)(str - arg)) break;
72 }
74 /* empty argument or non-floatish, so warn about it and die */
75 if(!i) usage (_("Warning threshold must be float or float triplet!\n"));
77 if(i != 2) {
78 /* one or more numbers were given, so fill array with last
79 * we got (most likely to NOT produce the least expected result) */
80 for(n = i; n < 3; n++) th[n] = th[i];
81 }
82 }
85 int
86 main (int argc, char **argv)
87 {
88 int result;
89 int i;
91 double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
92 #ifndef HAVE_GETLOADAVG
93 char input_buffer[MAX_INPUT_BUFFER];
94 # ifdef HAVE_PROC_LOADAVG
95 FILE *fp;
96 char *str, *next;
97 # endif
98 #endif
100 setlocale (LC_ALL, "");
101 bindtextdomain (PACKAGE, LOCALEDIR);
102 textdomain (PACKAGE);
103 setlocale(LC_NUMERIC, "POSIX");
105 if (process_arguments (argc, argv) == ERROR)
106 usage4 (_("Could not parse arguments"));
108 #ifdef HAVE_GETLOADAVG
109 result = getloadavg (la, 3);
110 if (result != 3)
111 return STATE_UNKNOWN;
112 #else
113 # ifdef HAVE_PROC_LOADAVG
114 fp = fopen (PROC_LOADAVG, "r");
115 if (fp == NULL) {
116 printf (_("Error opening %s\n"), PROC_LOADAVG);
117 return STATE_UNKNOWN;
118 }
120 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
121 str = (char *)input_buffer;
122 for(i = 0; i < 3; i++) {
123 la[i] = strtod(str, &next);
124 str = next;
125 }
126 }
128 fclose (fp);
129 # else
130 child_process = spopen (PATH_TO_UPTIME);
131 if (child_process == NULL) {
132 printf (_("Error opening %s\n"), PATH_TO_UPTIME);
133 return STATE_UNKNOWN;
134 }
135 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
136 if (child_stderr == NULL) {
137 printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
138 }
139 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
140 sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
142 result = spclose (child_process);
143 if (result) {
144 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
145 return STATE_UNKNOWN;
146 }
147 # endif
148 #endif
150 if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
151 #ifdef HAVE_GETLOADAVG
152 printf (_("Error in getloadavg()\n"));
153 #else
154 # ifdef HAVE_PROC_LOADAVG
155 printf (_("Error processing %s\n"), PROC_LOADAVG);
156 # else
157 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
158 # endif
159 #endif
160 return STATE_UNKNOWN;
161 }
163 /* we got this far, so assume OK until we've measured */
164 result = STATE_OK;
166 asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
168 for(i = 0; i < 3; i++) {
169 if(la[i] > cload[i]) {
170 result = STATE_CRITICAL;
171 break;
172 }
173 else if(la[i] > wload[i]) result = STATE_WARNING;
174 }
176 printf("%s - %s|", state_text(result), status_line);
177 for(i = 0; i < 3; i++)
178 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
180 putchar('\n');
181 return result;
182 }
185 /* process command-line arguments */
186 static int
187 process_arguments (int argc, char **argv)
188 {
189 int c = 0;
191 int option = 0;
192 static struct option longopts[] = {
193 {"warning", required_argument, 0, 'w'},
194 {"critical", required_argument, 0, 'c'},
195 {"version", no_argument, 0, 'V'},
196 {"help", no_argument, 0, 'h'},
197 {0, 0, 0, 0}
198 };
200 if (argc < 2)
201 return ERROR;
203 while (1) {
204 c = getopt_long (argc, argv, "Vhc:w:", longopts, &option);
206 if (c == -1 || c == EOF)
207 break;
209 switch (c) {
210 case 'w': /* warning time threshold */
211 get_threshold(optarg, wload);
212 break;
213 case 'c': /* critical time threshold */
214 get_threshold(optarg, cload);
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 usage2 (_("Unknown argument"), optarg);
224 }
225 }
227 c = optind;
228 if (c == argc)
229 return validate_arguments ();
231 /* handle the case if both arguments are missing,
232 * but not if only one is given without -c or -w flag */
233 if(c - argc == 2) {
234 get_threshold(argv[c++], wload);
235 get_threshold(argv[c++], cload);
236 }
237 else if(c - argc == 1) {
238 get_threshold(argv[c++], cload);
239 }
241 return validate_arguments ();
242 }
246 static int
247 validate_arguments (void)
248 {
249 int i = 0;
251 /* match cload first, as it will give the most friendly error message
252 * if user hasn't given the -c switch properly */
253 for(i = 0; i < 3; i++) {
254 if(cload[i] < 0)
255 die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
256 if(wload[i] < 0)
257 die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
258 if(wload[i] > cload[i])
259 die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
260 }
262 return OK;
263 }
267 void
268 print_help (void)
269 {
270 print_revision (progname, revision);
272 printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
273 printf (COPYRIGHT, copyright, email);
275 printf (_("This plugin tests the current system load average.\n\n"));
277 print_usage ();
279 printf (_(UT_HELP_VRSN));
281 printf (_("\
282 -w, --warning=WLOAD1,WLOAD5,WLOAD15\n\
283 Exit with WARNING status if load average exceeds WLOADn\n\
284 -c, --critical=CLOAD1,CLOAD5,CLOAD15\n\
285 Exit with CRITICAL status if load average exceed CLOADn\n\n\
286 the load average format is the same used by \"uptime\" and \"w\"\n\n"));
288 printf (_(UT_SUPPORT));
289 }
291 void
292 print_usage (void)
293 {
294 printf ("Usage: %s -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);
295 }