summaryrefslogtreecommitdiffstats
path: root/plugins/check_load.c
diff options
context:
space:
mode:
authorEthan Galstad <egalstad@users.sourceforge.net>2002-02-28 06:42:51 (GMT)
committerEthan Galstad <egalstad@users.sourceforge.net>2002-02-28 06:42:51 (GMT)
commit44a321cb8a42d6c0ea2d96a1086a17f2134c89cc (patch)
treea1a4d9f7b92412a17ab08f34f04eec45433048b7 /plugins/check_load.c
parent54fd5d7022ff2d6a59bc52b8869182f3fc77a058 (diff)
downloadmonitoring-plugins-44a321cb8a42d6c0ea2d96a1086a17f2134c89cc.tar.gz
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_load.c')
-rw-r--r--plugins/check_load.c321
1 files changed, 321 insertions, 0 deletions
diff --git a/plugins/check_load.c b/plugins/check_load.c
new file mode 100644
index 0000000..6673b1d
--- /dev/null
+++ b/plugins/check_load.c
@@ -0,0 +1,321 @@
1/******************************************************************************
2 *
3 * CHECK_LOAD.C
4 *
5 * Written by Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>
6 * License: GPL
7 * Command line: CHECK_LOAD <wload1> <cload1> <wload5> <cload5> <wload15> <cload15>
8 * First Written: 04/17/99
9 *
10 * Modifications:
11 *
12 * 05/18/1999 - Modified to work getloadavg where available, and use uptime
13 * where neither proc or getloadavg are found. Also use autoconf.
14 * mods by Karl DeBisschop (kdebiss@alum.mit.edu)
15 * 07/01/1999 - Added some #DEFINEs to allow compilation under NetBSD, as
16 * suggested by Andy Doran.
17 * mods by Ethan Galstad (nagios@nagios.org)
18 * 07/17/1999 - Initialized la[] array to prevent NetBSD from complaining
19 * mods by Ethan Galstad (nagios@nagios.org)
20 * 08/18/1999 - Integrated some code with common plugin utilities
21 * mods by Ethan Galstad (nagios@nagios.org)
22 * $Date$
23 * Note: The load format is the same used by "uptime" and "w"
24 *
25 *****************************************************************************/
26
27#include "config.h"
28#include "common.h"
29#include "utils.h"
30
31#ifdef HAVE_SYS_LOADAVG_H
32#include <sys/loadavg.h>
33#endif
34
35/* needed for compilation under NetBSD, as suggested by Andy Doran */
36#ifndef LOADAVG_1MIN
37#define LOADAVG_1MIN 0
38#define LOADAVG_5MIN 1
39#define LOADAVG_15MIN 2
40#endif /* !defined LOADAVG_1MIN */
41
42#include "popen.h"
43#ifdef HAVE_PROC_LOADAVG
44
45#endif
46
47#define PROGNAME "check_load"
48
49int process_arguments (int argc, char **argv);
50int call_getopt (int argc, char **argv);
51int validate_arguments (void);
52void print_usage (void);
53void print_help (void);
54
55float wload1 = -1, wload5 = -1, wload15 = -1;
56float cload1 = -1, cload5 = -1, cload15 = -1;
57
58int
59main (int argc, char **argv)
60{
61#if HAVE_GETLOADAVG==1
62 int result;
63 double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
64#elif HAVE_PROC_LOADAVG==1
65 FILE *fp;
66 char input_buffer[MAX_INPUT_BUFFER];
67 char *tmp_ptr;
68#else
69 int result;
70 char input_buffer[MAX_INPUT_BUFFER];
71#endif
72
73 float la1, la5, la15;
74
75 if (process_arguments (argc, argv) == ERROR)
76 usage ("\n");
77
78#if HAVE_GETLOADAVG==1
79 result = getloadavg (la, 3);
80 if (result == -1)
81 return STATE_UNKNOWN;
82 la1 = la[LOADAVG_1MIN];
83 la5 = la[LOADAVG_5MIN];
84 la15 = la[LOADAVG_15MIN];
85#elif HAVE_PROC_LOADAVG==1
86 fp = fopen (PROC_LOADAVG, "r");
87 if (fp == NULL) {
88 printf ("Error opening %s\n", PROC_LOADAVG);
89 return STATE_UNKNOWN;
90 }
91
92 la1 = la5 = la15 = -1;
93
94 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
95 tmp_ptr = strtok (input_buffer, " ");
96 la1 = atof (tmp_ptr);
97 tmp_ptr = strtok (NULL, " ");
98 la5 = atof (tmp_ptr);
99 tmp_ptr = strtok (NULL, " ");
100 la15 = atof (tmp_ptr);
101 }
102
103 fclose (fp);
104#else
105 child_process = spopen (PATH_TO_UPTIME);
106 if (child_process == NULL) {
107 printf ("Error opening %s\n", PATH_TO_UPTIME);
108 return STATE_UNKNOWN;
109 }
110 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
111 if (child_stderr == NULL) {
112 printf ("Could not open stderr for %s\n", PATH_TO_UPTIME);
113 }
114 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
115 sscanf (input_buffer, "%*[^l]load average: %f, %f, %f", &la1, &la5, &la15);
116
117 result = spclose (child_process);
118 if (result) {
119 printf ("Error code %d returned in %s\n", result, PATH_TO_UPTIME);
120 return STATE_UNKNOWN;
121 }
122#endif
123
124 if ((la1 == -1) || (la5 == -1) || (la15 == -1)) {
125#if HAVE_GETLOADAVG==1
126 printf ("Error in getloadavg()\n");
127#elif HAVE_PROC_LOADAVG==1
128 printf ("Error processing %s\n", PROC_LOADAVG);
129#else
130 printf ("Error processing %s\n", PATH_TO_UPTIME);
131#endif
132 return STATE_UNKNOWN;
133 }
134 printf ("load average: %.2f, %.2f, %.2f", la1, la5, la15);
135 if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
136 printf (" CRITICAL\n");
137 return STATE_CRITICAL;
138 }
139 if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
140 printf (" WARNING\n");
141 return STATE_WARNING;
142 }
143 printf ("\n");
144 return STATE_OK;
145}
146
147
148
149
150
151/* process command-line arguments */
152int
153process_arguments (int argc, char **argv)
154{
155 int c;
156
157 if (argc < 2)
158 return ERROR;
159
160 c = 0;
161 while (c += (call_getopt (argc - c, &argv[c]))) {
162 if (argc <= c)
163 break;
164
165 if (wload1 < 0 && is_nonnegative (argv[c]))
166 wload1 = atof (argv[c]);
167 else if (cload1 < 0 && is_nonnegative (argv[c]))
168 cload1 = atof (argv[c]);
169 else if (wload5 < 0 && is_nonnegative (argv[c]))
170 wload5 = atof (argv[c]);
171 else if (cload5 < 0 && is_nonnegative (argv[c]))
172 cload5 = atof (argv[c]);
173 else if (wload15 < 0 && is_nonnegative (argv[c]))
174 wload15 = atof (argv[c]);
175 else if (cload15 < 0 && is_nonnegative (argv[c]))
176 cload15 = atof (argv[c]);
177 }
178
179 return validate_arguments ();
180}
181
182
183
184
185
186int
187call_getopt (int argc, char **argv)
188{
189 int c, i = 0;
190
191#ifdef HAVE_GETOPT_H
192 int option_index = 0;
193 static struct option long_options[] = {
194 {"warning", required_argument, 0, 'w'},
195 {"critical", required_argument, 0, 'c'},
196 {"version", no_argument, 0, 'V'},
197 {"help", no_argument, 0, 'h'},
198 {0, 0, 0, 0}
199 };
200#endif
201
202 while (1) {
203#ifdef HAVE_GETOPT_H
204 c = getopt_long (argc, argv, "+?Vhc:w:", long_options, &option_index);
205#else
206 c = getopt (argc, argv, "+?Vhc:w:");
207#endif
208
209 i++;
210
211 if (c == -1 || c == EOF)
212 break;
213
214 switch (c) {
215 case 'c':
216 case 'w':
217 i++;
218 }
219
220 switch (c) {
221 case 'w': /* warning time threshold */
222 if (is_intnonneg (optarg)) {
223 if (wload1 < 0 && is_nonnegative (argv[c]))
224 wload1 = atof (argv[c]);
225 else if (wload5 < 0 && is_nonnegative (argv[c]))
226 wload5 = atof (argv[c]);
227 else if (wload15 < 0 && is_nonnegative (argv[c]))
228 wload15 = atof (argv[c]);
229 break;
230 }
231 else if (strstr (optarg, ",") &&
232 sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3) {
233 break;
234 }
235 else {
236 usage ("Warning threshold must be float or float triplet!\n");
237 }
238 case 'c': /* critical time threshold */
239 if (is_intnonneg (optarg)) {
240 if (cload1 < 0 && is_nonnegative (argv[c]))
241 cload1 = atof (argv[c]);
242 else if (cload5 < 0 && is_nonnegative (argv[c]))
243 cload5 = atof (argv[c]);
244 else if (cload15 < 0 && is_nonnegative (argv[c]))
245 cload15 = atof (argv[c]);
246 break;
247 }
248 else if (strstr (optarg, ",") &&
249 sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3) {
250 break;
251 }
252 else {
253 usage ("Critical threshold must be float or float triplet!\n");
254 }
255 case 'V': /* version */
256 print_revision (my_basename (argv[0]), "$Revision$");
257 exit (STATE_OK);
258 case 'h': /* help */
259 print_help ();
260 exit (STATE_OK);
261 case '?': /* help */
262 usage ("Invalid argument\n");
263 }
264 }
265 return i;
266}
267
268
269
270
271
272int
273validate_arguments (void)
274{
275 if ((wload1 > cload1) || (wload5 > cload5) || (wload15 > cload15)) {
276 printf
277 ("Inconsistence in parameters: \"warning load\" greater than \"critical load\".\n");
278 return STATE_UNKNOWN;
279 }
280
281 return OK;
282}
283
284
285
286
287
288void
289print_usage (void)
290{
291 printf
292 ("Usage: check_load -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"
293 " check_load --version\n" " check_load --help\n");
294}
295
296
297
298
299
300void
301print_help (void)
302{
303 print_revision (PROGNAME, "$Revision$");
304 printf
305 ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"
306 "Copyright (c) 2000 Karl DeBisschop\n\n"
307 "This plugin tests the current system load average.\n\n");
308 print_usage ();
309 printf
310 ("\nOptions:\n"
311 " -w, --warning=WLOAD1,WLOAD5,WLOAD15\n"
312 " Exit with WARNING status if load average exceeds WLOADn\n"
313 " -c, --critical=CLOAD1,CLOAD5,CLOAD15\n"
314 " Exit with CRITICAL status if load average exceed CLOADn\n"
315 " -h, --help\n"
316 " Print detailed help screen\n"
317 " -V, --version\n"
318 " Print version information\n\n"
319 "the load average format is the same used by \"uptime\" and \"w\"\n\n");
320 support ();
321}