Code

9625036d879d93d387907d455484da9ff7977458
[nagiosplug.git] / plugins / check_load.c
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.
16  
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);
104         if (process_arguments (argc, argv) == ERROR)
105                 usage4 (_("Could not parse arguments"));
107 #ifdef HAVE_GETLOADAVG
108         result = getloadavg (la, 3);
109         if (result != 3)
110                 return STATE_UNKNOWN;
111 #else
112 # ifdef HAVE_PROC_LOADAVG
113         fp = fopen (PROC_LOADAVG, "r");
114         if (fp == NULL) {
115                 printf (_("Error opening %s\n"), PROC_LOADAVG);
116                 return STATE_UNKNOWN;
117         }
119         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
120                 str = (char *)input_buffer;
121                 for(i = 0; i < 3; i++) {
122                         la[i] = strtod(str, &next);
123                         str = next;
124                 }
125         }
127         fclose (fp);
128 # else
129         child_process = spopen (PATH_TO_UPTIME);
130         if (child_process == NULL) {
131                 printf (_("Error opening %s\n"), PATH_TO_UPTIME);
132                 return STATE_UNKNOWN;
133         }
134         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
135         if (child_stderr == NULL) {
136                 printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
137         }
138         fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
139         sscanf (input_buffer, "%*[^l]load average: %f, %f, %f", &la1, &la5, &la15);
141         result = spclose (child_process);
142         if (result) {
143                 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
144                 return STATE_UNKNOWN;
145         }
146 # endif
147 #endif
149         if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
150 #ifdef HAVE_GETLOADAVG
151                 printf (_("Error in getloadavg()\n"));
152 #else
153 # ifdef HAVE_PROC_LOADAVG
154                 printf (_("Error processing %s\n"), PROC_LOADAVG);
155 # else
156                 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
157 # endif
158 #endif
159                 return STATE_UNKNOWN;
160         }
162         /* we got this far, so assume OK until we've measured */
163         result = STATE_OK;
165         asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
167         for(i = 0; i < 3; i++) {
168                 if(la[i] > cload[i]) {
169                         result = STATE_CRITICAL;
170                         break;
171                 }
172                 else if(la[i] > wload[i]) result = STATE_WARNING;
173         }
175         printf("%s - %s|", state_text(result), status_line);
176         for(i = 0; i < 3; i++)
177                 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
179         putchar('\n');
180         return result;
184 /* process command-line arguments */
185 static int
186 process_arguments (int argc, char **argv)
188         int c = 0;
190         int option = 0;
191         static struct option longopts[] = {
192                 {"warning", required_argument, 0, 'w'},
193                 {"critical", required_argument, 0, 'c'},
194                 {"version", no_argument, 0, 'V'},
195                 {"help", no_argument, 0, 'h'},
196                 {0, 0, 0, 0}
197         };
199         if (argc < 2)
200                 return ERROR;
202         while (1) {
203                 c = getopt_long (argc, argv, "Vhc:w:", longopts, &option);
205                 if (c == -1 || c == EOF)
206                         break;
208                 switch (c) {
209                 case 'w': /* warning time threshold */
210                         get_threshold(optarg, wload);
211                         break;
212                 case 'c': /* critical time threshold */
213                         get_threshold(optarg, cload);
214                         break;
215                 case 'V':                                                                       /* version */
216                         print_revision (progname, revision);
217                         exit (STATE_OK);
218                 case 'h':                                                                       /* help */
219                         print_help ();
220                         exit (STATE_OK);
221                 case '?':                                                                       /* help */
222                         usage2 (_("Unknown argument"), optarg);
223                 }
224         }
226         c = optind;
227         if (c == argc)
228                 return validate_arguments ();
230         /* handle the case if both arguments are missing,
231          * but not if only one is given without -c or -w flag */
232         if(c - argc == 2) {
233                 get_threshold(argv[c++], wload);
234                 get_threshold(argv[c++], cload);
235         }
236         else if(c - argc == 1) {
237                 get_threshold(argv[c++], cload);
238         }
240         return validate_arguments ();
245 static int
246 validate_arguments (void)
248         int i = 0;
250         /* match cload first, as it will give the most friendly error message
251          * if user hasn't given the -c switch properly */
252         for(i = 0; i < 3; i++) {
253                 if(cload[i] < 0)
254                         die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
255                 if(wload[i] < 0)
256                         die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
257                 if(wload[i] > cload[i])
258                         die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
259         }
261         return OK;
266 void
267 print_help (void)
269         print_revision (progname, revision);
271         printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
272         printf (COPYRIGHT, copyright, email);
274         printf (_("This plugin tests the current system load average.\n\n"));
276         print_usage ();
278         printf (_(UT_HELP_VRSN));
280         printf (_("\
281  -w, --warning=WLOAD1,WLOAD5,WLOAD15\n\
282    Exit with WARNING status if load average exceeds WLOADn\n\
283  -c, --critical=CLOAD1,CLOAD5,CLOAD15\n\
284    Exit with CRITICAL status if load average exceed CLOADn\n\n\
285 the load average format is the same used by \"uptime\" and \"w\"\n\n"));
287         printf (_(UT_SUPPORT));
290 void
291 print_usage (void)
293         printf ("Usage: %s -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);