1 /*****************************************************************************
3 * Nagios check_load plugin
6 * Copyright (c) 1999-2007 Nagios Plugins Development Team
10 * This file contains the check_load plugin
12 * This plugin tests the current system load average.
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.
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.
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/>.
29 *****************************************************************************/
31 const char *progname
= "check_load";
32 const char *copyright
= "1999-2007";
33 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
39 #ifdef HAVE_SYS_LOADAVG_H
40 #include <sys/loadavg.h>
43 /* needed for compilation under NetBSD, as suggested by Andy Doran */
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 };
67 int take_into_account_cpus
= 0;
70 get_threshold(char *arg
, double *th
)
77 for(i
= 0; i
< 3; i
++) {
78 th
[i
] = strtod(str
, &p
);
83 if(n
<= (size_t)(str
- arg
)) break;
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"));
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
];
98 main (int argc
, char **argv
)
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
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);
127 return STATE_UNKNOWN
;
129 # ifdef HAVE_PROC_LOADAVG
130 fp
= fopen (PROC_LOADAVG
, "r");
132 printf (_("Error opening %s\n"), PROC_LOADAVG
);
133 return STATE_UNKNOWN
;
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
);
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
;
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
);
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
);
160 printf (_("Error code %d returned in %s\n"), result
, PATH_TO_UPTIME
);
161 return STATE_UNKNOWN
;
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
;
173 if ((la
[0] < 0.0) || (la
[1] < 0.0) || (la
[2] < 0.0)) {
174 #ifdef HAVE_GETLOADAVG
175 printf (_("Error in getloadavg()\n"));
177 # ifdef HAVE_PROC_LOADAVG
178 printf (_("Error processing %s\n"), PROC_LOADAVG
);
180 printf (_("Error processing %s\n"), PATH_TO_UPTIME
);
183 return STATE_UNKNOWN
;
186 /* we got this far, so assume OK until we've measured */
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
;
196 else if(la
[i
] > wload
[i
]) result
= STATE_WARNING
;
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
]);
208 /* process command-line arguments */
210 process_arguments (int argc
, char **argv
)
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'},
228 c
= getopt_long (argc
, argv
, "Vhrc:w:", longopts
, &option
);
230 if (c
== -1 || c
== EOF
)
234 case 'w': /* warning time threshold */
235 get_threshold(optarg
, wload
);
237 case 'c': /* critical time threshold */
238 get_threshold(optarg
, cload
);
240 case 'r': /* Divide load average by number of CPUs */
241 take_into_account_cpus
= 1;
243 case 'V': /* version */
244 print_revision (progname
, NP_VERSION
);
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 */
261 get_threshold(argv
[c
++], wload
);
262 get_threshold(argv
[c
++], cload
);
264 else if(c
- argc
== 1) {
265 get_threshold(argv
[c
++], cload
);
268 return validate_arguments ();
274 validate_arguments (void)
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
++) {
282 die (STATE_UNKNOWN
, _("Critical threshold for %d-minute load average is not specified\n"), nums
[i
]);
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
]);
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."));
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)"));
321 printf ("%s\n", _("Notes:"));
322 printf (_(UT_EXTRA_OPTS_NOTES
));
325 printf (_(UT_SUPPORT
));
331 printf (_("Usage:"));
332 printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname
);