Code

markup for translation
[nagiosplug.git] / plugins / check_load.c
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  *****************************************************************************/
27 const char *progname = "check_load";
28 const char *revision = "$Revision$";
29 const char *copyright = "1999-2003";
30 const char *email = "nagiosplug-devel@lists.sourceforge.net";
32 #include "common.h"
33 #include "utils.h"
34 #include "popen.h"
36 void
37 print_usage (void)
38 {
39         printf (_("Usage: %s -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"),
40                 progname);
41         printf (_(UT_HLP_VRS), progname, progname);
42 }
44 void
45 print_help (void)
46 {
47         print_revision (progname, revision);
49         printf (_("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"));
50         printf (_(COPYRIGHT), copyright, email);
52         printf (_("This plugin tests the current system load average.\n\n"));
54         print_usage ();
56         printf (_(UT_HELP_VRSN));
58         printf (_("\
59  -w, --warning=WLOAD1,WLOAD5,WLOAD15\n\
60    Exit with WARNING status if load average exceeds WLOADn\n\
61  -c, --critical=CLOAD1,CLOAD5,CLOAD15\n\
62    Exit with CRITICAL status if load average exceed CLOADn\n\n\
63 the load average format is the same used by \"uptime\" and \"w\"\n\n"));
65         printf (_(UT_SUPPORT));
66 }
68 #ifdef HAVE_SYS_LOADAVG_H
69 #include <sys/loadavg.h>
70 #endif
72 /* needed for compilation under NetBSD, as suggested by Andy Doran */
73 #ifndef LOADAVG_1MIN
74 #define LOADAVG_1MIN    0
75 #define LOADAVG_5MIN    1
76 #define LOADAVG_15MIN   2
77 #endif /* !defined LOADAVG_1MIN */
80 int process_arguments (int argc, char **argv);
81 int validate_arguments (void);
83 float wload1 = -1, wload5 = -1, wload15 = -1;
84 float cload1 = -1, cload5 = -1, cload15 = -1;
86 char *status_line = "";
88 int
89 main (int argc, char **argv)
90 {
91 #if HAVE_GETLOADAVG==1
92         int result;
93         double la[3] = { 0.0, 0.0, 0.0 };       /* NetBSD complains about unitialized arrays */
94 #elif HAVE_PROC_LOADAVG==1
95         FILE *fp;
96         char input_buffer[MAX_INPUT_BUFFER];
97         char *tmp_ptr;
98 #else
99         int result;
100         char input_buffer[MAX_INPUT_BUFFER];
101 #endif
103         float la1, la5, la15;
105         if (process_arguments (argc, argv) == ERROR)
106                 usage ("\n");
108 #if HAVE_GETLOADAVG==1
109         result = getloadavg (la, 3);
110         if (result == -1)
111                 return STATE_UNKNOWN;
112         la1 = la[LOADAVG_1MIN];
113         la5 = la[LOADAVG_5MIN];
114         la15 = la[LOADAVG_15MIN];
115 #elif HAVE_PROC_LOADAVG==1
116         fp = fopen (PROC_LOADAVG, "r");
117         if (fp == NULL) {
118                 printf (_("Error opening %s\n"), PROC_LOADAVG);
119                 return STATE_UNKNOWN;
120         }
122         la1 = la5 = la15 = -1;
124         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
125                 tmp_ptr = strtok (input_buffer, " ");
126                 la1 = atof (tmp_ptr);
127                 tmp_ptr = strtok (NULL, " ");
128                 la5 = atof (tmp_ptr);
129                 tmp_ptr = strtok (NULL, " ");
130                 la15 = atof (tmp_ptr);
131         }
133         fclose (fp);
134 #else
135         child_process = spopen (PATH_TO_UPTIME);
136         if (child_process == NULL) {
137                 printf (_("Error opening %s\n"), PATH_TO_UPTIME);
138                 return STATE_UNKNOWN;
139         }
140         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
141         if (child_stderr == NULL) {
142                 printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
143         }
144         fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
145         sscanf (input_buffer, "%*[^l]load average: %f, %f, %f"), &la1, &la5, &la15);
147         result = spclose (child_process);
148         if (result) {
149                 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
150                 return STATE_UNKNOWN;
151         }
152 #endif
154         if ((la1 == -1) || (la5 == -1) || (la15 == -1)) {
155 #if HAVE_GETLOADAVG==1
156                 printf (_("Error in getloadavg()\n"));
157 #elif HAVE_PROC_LOADAVG==1
158                 printf (_("Error processing %s\n"), PROC_LOADAVG);
159 #else
160                 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
161 #endif
162                 return STATE_UNKNOWN;
163         }
164         asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
165         if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
166                 printf(_("CRITICAL - %s\n"), status_line);
167                 return STATE_CRITICAL;
168         }
169         if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
170                 printf (_("WARNING - %s\n"), status_line);
171                 return STATE_WARNING;
172         }
173         printf (_("OK - %s\n"), status_line);
174         return STATE_OK;
181 /* process command-line arguments */
182 int
183 process_arguments (int argc, char **argv)
185         int c = 0;
187         int option_index = 0;
188         static struct option long_options[] = {
189                 {"warning", required_argument, 0, 'w'},
190                 {"critical", required_argument, 0, 'c'},
191                 {"version", no_argument, 0, 'V'},
192                 {"help", no_argument, 0, 'h'},
193                 {0, 0, 0, 0}
194         };
196         if (argc < 2)
197                 return ERROR;
199         while (1) {
200                 c = getopt_long (argc, argv, "Vhc:w:", long_options, &option_index);
202                 if (c == -1 || c == EOF)
203                         break;
205                 switch (c) {
206                 case 'w':                                                                       /* warning time threshold */
207                         if (is_intnonneg (optarg)) {
208                                 wload1 = atof (optarg);
209                                 wload5 = atof (optarg);
210                                 wload15 = atof (optarg);
211                                 break;
212                         }
213                         else if (strstr (optarg, ",") &&
214                                                                  sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
215                                 break;
216                         else if (strstr (optarg, ":") &&
217                                                          sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
218                                 break;
219                         else
220                                 usage (_("Warning threshold must be float or float triplet!\n"));
221                         break;
222                 case 'c':                                                                       /* critical time threshold */
223                         if (is_intnonneg (optarg)) {
224                                 cload1 = atof (optarg);
225                                 cload5 = atof (optarg);
226                                 cload15 = atof (optarg);
227                                 break;
228                         }
229                         else if (strstr (optarg, ",") &&
230                                                          sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
231                                 break;
232                         else if (strstr (optarg, ":") &&
233                                                          sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
234                                 break;
235                         else
236                                 usage (_("Critical threshold must be float or float triplet!\n"));
237                         break;
238                 case 'V':                                                                       /* version */
239                         print_revision (progname, "$Revision$");
240                         exit (STATE_OK);
241                 case 'h':                                                                       /* help */
242                         print_help ();
243                         exit (STATE_OK);
244                 case '?':                                                                       /* help */
245                         usage (_("Invalid argument\n"));
246                 }
247         }
249         c = optind;
250         if (c == argc)
251                 return validate_arguments ();
252         if (wload1 < 0 && is_nonnegative (argv[c]))
253                 wload1 = atof (argv[c++]);
255         if (c == argc)
256                 return validate_arguments ();
257         if (cload1 < 0 && is_nonnegative (argv[c]))
258                 cload1 = atof (argv[c++]);
260         if (c == argc)
261                 return validate_arguments ();
262         if (wload5 < 0 && is_nonnegative (argv[c]))
263                 wload5 = atof (argv[c++]);
265         if (c == argc)
266                 return validate_arguments ();
267         if (cload5 < 0 && is_nonnegative (argv[c]))
268                 cload5 = atof (argv[c++]);
270         if (c == argc)
271                 return validate_arguments ();
272         if (wload15 < 0 && is_nonnegative (argv[c]))
273                 wload15 = atof (argv[c++]);
275         if (c == argc)
276                 return validate_arguments ();
277         if (cload15 < 0 && is_nonnegative (argv[c]))
278                 cload15 = atof (argv[c++]);
280         return validate_arguments ();
287 int
288 validate_arguments (void)
290         if (wload1 < 0)
291                 usage (_("Warning threshold for 1-minute load average is not specified\n"));
292         if (wload5 < 0)
293                 usage (_("Warning threshold for 5-minute load average is not specified\n"));
294         if (wload15 < 0)
295                 usage (_("Warning threshold for 15-minute load average is not specified\n"));
296         if (cload1 < 0)
297                 usage (_("Critical threshold for 1-minute load average is not specified\n"));
298         if (cload5 < 0)
299                 usage (_("Critical threshold for 5-minute load average is not specified\n"));
300         if (cload15 < 0)
301                 usage (_("Critical threshold for 15-minute load average is not specified\n"));
302         if (wload1 > cload1)
303                 usage (_("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n"));
304         if (wload5 > cload5)
305                 usage (_("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n"));
306         if (wload15 > cload15)
307                 usage (_("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n"));
308         return OK;