Merge pull request #1563 from jacobbaungard/ipv6_check_icmp
[monitoring-plugins.git] / plugins / check_load.c
blobb1cc498fd95405705d64abc31a722cf575b5cf82
1 /*****************************************************************************
2 *
3 * Monitoring check_load plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2007 Monitoring Plugins Development Team
7 *
8 * Description:
9 *
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 = "devel@monitoring-plugins.org";
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)
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;
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];
97 int
98 main (int argc, char **argv)
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;
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;
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;
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 if(strstr(input_buffer, "load average:")) {
157 sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
159 else if(strstr(input_buffer, "load averages:")) {
160 sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15);
162 else {
163 printf (_("could not parse load from uptime %s: %s\n"), PATH_TO_UPTIME, result);
164 return STATE_UNKNOWN;
167 result = spclose (child_process);
168 if (result) {
169 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
170 return STATE_UNKNOWN;
172 # endif
173 #endif
175 if (take_into_account_cpus == 1) {
176 if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
177 la[0] = la[0] / numcpus;
178 la[1] = la[1] / numcpus;
179 la[2] = la[2] / numcpus;
182 if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
183 #ifdef HAVE_GETLOADAVG
184 printf (_("Error in getloadavg()\n"));
185 #else
186 # ifdef HAVE_PROC_LOADAVG
187 printf (_("Error processing %s\n"), PROC_LOADAVG);
188 # else
189 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
190 # endif
191 #endif
192 return STATE_UNKNOWN;
195 /* we got this far, so assume OK until we've measured */
196 result = STATE_OK;
198 xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
200 for(i = 0; i < 3; i++) {
201 if(la[i] > cload[i]) {
202 result = STATE_CRITICAL;
203 break;
205 else if(la[i] > wload[i]) result = STATE_WARNING;
208 printf("%s - %s|", state_text(result), status_line);
209 for(i = 0; i < 3; i++)
210 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
212 putchar('\n');
213 return result;
217 /* process command-line arguments */
218 static int
219 process_arguments (int argc, char **argv)
221 int c = 0;
223 int option = 0;
224 static struct option longopts[] = {
225 {"warning", required_argument, 0, 'w'},
226 {"critical", required_argument, 0, 'c'},
227 {"percpu", no_argument, 0, 'r'},
228 {"version", no_argument, 0, 'V'},
229 {"help", no_argument, 0, 'h'},
230 {0, 0, 0, 0}
233 if (argc < 2)
234 return ERROR;
236 while (1) {
237 c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option);
239 if (c == -1 || c == EOF)
240 break;
242 switch (c) {
243 case 'w': /* warning time threshold */
244 get_threshold(optarg, wload);
245 break;
246 case 'c': /* critical time threshold */
247 get_threshold(optarg, cload);
248 break;
249 case 'r': /* Divide load average by number of CPUs */
250 take_into_account_cpus = 1;
251 break;
252 case 'V': /* version */
253 print_revision (progname, NP_VERSION);
254 exit (STATE_UNKNOWN);
255 case 'h': /* help */
256 print_help ();
257 exit (STATE_UNKNOWN);
258 case '?': /* help */
259 usage5 ();
263 c = optind;
264 if (c == argc)
265 return validate_arguments ();
267 /* handle the case if both arguments are missing,
268 * but not if only one is given without -c or -w flag */
269 if(c - argc == 2) {
270 get_threshold(argv[c++], wload);
271 get_threshold(argv[c++], cload);
273 else if(c - argc == 1) {
274 get_threshold(argv[c++], cload);
277 return validate_arguments ();
282 static int
283 validate_arguments (void)
285 int i = 0;
287 /* match cload first, as it will give the most friendly error message
288 * if user hasn't given the -c switch properly */
289 for(i = 0; i < 3; i++) {
290 if(cload[i] < 0)
291 die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
292 if(wload[i] < 0)
293 die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
294 if(wload[i] > cload[i])
295 die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
298 return OK;
303 void
304 print_help (void)
306 print_revision (progname, NP_VERSION);
308 printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
309 printf (COPYRIGHT, copyright, email);
311 printf (_("This plugin tests the current system load average."));
313 printf ("\n\n");
315 print_usage ();
317 printf (UT_HELP_VRSN);
318 printf (UT_EXTRA_OPTS);
320 printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
321 printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
322 printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
323 printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
324 printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
325 printf (" %s\n", "-r, --percpu");
326 printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
328 printf (UT_SUPPORT);
331 void
332 print_usage (void)
334 printf ("%s\n", _("Usage:"));
335 printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);