Code

the last round of pedantic compiler warnings
[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.
17 ******************************************************************************/
19 const char *progname = "check_load";
20 const char *revision = "$Revision$";
21 const char *copyright = "1999-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "utils.h"
26 #include "popen.h"
28 #ifdef HAVE_SYS_LOADAVG_H
29 #include <sys/loadavg.h>
30 #endif
32 /* needed for compilation under NetBSD, as suggested by Andy Doran */
33 #ifndef LOADAVG_1MIN
34 #define LOADAVG_1MIN    0
35 #define LOADAVG_5MIN    1
36 #define LOADAVG_15MIN   2
37 #endif /* !defined LOADAVG_1MIN */
40 int process_arguments (int argc, char **argv);
41 int validate_arguments (void);
42 void print_help (void);
43 void print_usage (void);
45 float wload1 = -1, wload5 = -1, wload15 = -1;
46 float cload1 = -1, cload5 = -1, cload15 = -1;
48 char *status_line;
54 \f
55 int
56 main (int argc, char **argv)
57 {
58 #if HAVE_GETLOADAVG==1
59         int result;
60         double la[3] = { 0.0, 0.0, 0.0 };       /* NetBSD complains about unitialized arrays */
61 #else
62 # if HAVE_PROC_LOADAVG==1
63         FILE *fp;
64         char input_buffer[MAX_INPUT_BUFFER];
65         char *tmp_ptr;
66 # else
67         int result;
68         char input_buffer[MAX_INPUT_BUFFER];
69 # endif
70 #endif
72         float la1, la5, la15;
74         if (process_arguments (argc, argv) == ERROR)
75                 usage ("failed processing arguments\n");
77 #if HAVE_GETLOADAVG==1
78         result = getloadavg (la, 3);
79         if (result == -1)
80                 return STATE_UNKNOWN;
81         la1 = la[LOADAVG_1MIN];
82         la5 = la[LOADAVG_5MIN];
83         la15 = la[LOADAVG_15MIN];
84 #else
85 # if 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         }
92         la1 = la5 = la15 = -1;
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         }
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);
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 #endif
125         if ((la1 < 0.0) || (la5 < 0.0) || (la15 < 0.0)) {
126 #if HAVE_GETLOADAVG==1
127                 printf (_("Error in getloadavg()\n"));
128 #else
129 # if HAVE_PROC_LOADAVG==1
130                 printf (_("Error processing %s\n"), PROC_LOADAVG);
131 # else
132                 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
133 # endif
134 #endif
135                 return STATE_UNKNOWN;
136         }
137         asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
138         if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
139                 printf(_("CRITICAL - %s\n"), status_line);
140                 return STATE_CRITICAL;
141         }
142         if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
143                 printf (_("WARNING - %s\n"), status_line);
144                 return STATE_WARNING;
145         }
146         printf (_("OK - %s\n"), status_line);
147         return STATE_OK;
154 \f
155 /* process command-line arguments */
156 int
157 process_arguments (int argc, char **argv)
159         int c = 0;
161         int option = 0;
162         static struct option longopts[] = {
163                 {"warning", required_argument, 0, 'w'},
164                 {"critical", required_argument, 0, 'c'},
165                 {"version", no_argument, 0, 'V'},
166                 {"help", no_argument, 0, 'h'},
167                 {0, 0, 0, 0}
168         };
170         if (argc < 2)
171                 return ERROR;
173         while (1) {
174                 c = getopt_long (argc, argv, "Vhc:w:", longopts, &option);
176                 if (c == -1 || c == EOF)
177                         break;
179                 switch (c) {
180                 case 'w':                                                                       /* warning time threshold */
181                         if (is_intnonneg (optarg)) {
182                                 wload1 = atof (optarg);
183                                 wload5 = atof (optarg);
184                                 wload15 = atof (optarg);
185                                 break;
186                         }
187                         else if (strstr (optarg, ",") &&
188                                                                  sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
189                                 break;
190                         else if (strstr (optarg, ":") &&
191                                                          sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
192                                 break;
193                         else
194                                 usage (_("Warning threshold must be float or float triplet!\n"));
195                         break;
196                 case 'c':                                                                       /* critical time threshold */
197                         if (is_intnonneg (optarg)) {
198                                 cload1 = atof (optarg);
199                                 cload5 = atof (optarg);
200                                 cload15 = atof (optarg);
201                                 break;
202                         }
203                         else if (strstr (optarg, ",") &&
204                                                          sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
205                                 break;
206                         else if (strstr (optarg, ":") &&
207                                                          sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
208                                 break;
209                         else
210                                 usage (_("Critical threshold must be float or float triplet!\n"));
211                         break;
212                 case 'V':                                                                       /* version */
213                         print_revision (progname, "$Revision$");
214                         exit (STATE_OK);
215                 case 'h':                                                                       /* help */
216                         print_help ();
217                         exit (STATE_OK);
218                 case '?':                                                                       /* help */
219                         usage (_("Invalid argument\n"));
220                 }
221         }
223         c = optind;
224         if (c == argc)
225                 return validate_arguments ();
226         if (wload1 < 0 && is_nonnegative (argv[c]))
227                 wload1 = atof (argv[c++]);
229         if (c == argc)
230                 return validate_arguments ();
231         if (cload1 < 0 && is_nonnegative (argv[c]))
232                 cload1 = atof (argv[c++]);
234         if (c == argc)
235                 return validate_arguments ();
236         if (wload5 < 0 && is_nonnegative (argv[c]))
237                 wload5 = atof (argv[c++]);
239         if (c == argc)
240                 return validate_arguments ();
241         if (cload5 < 0 && is_nonnegative (argv[c]))
242                 cload5 = atof (argv[c++]);
244         if (c == argc)
245                 return validate_arguments ();
246         if (wload15 < 0 && is_nonnegative (argv[c]))
247                 wload15 = atof (argv[c++]);
249         if (c == argc)
250                 return validate_arguments ();
251         if (cload15 < 0 && is_nonnegative (argv[c]))
252                 cload15 = atof (argv[c++]);
254         return validate_arguments ();
261 int
262 validate_arguments (void)
264         if (wload1 < 0)
265                 usage (_("Warning threshold for 1-minute load average is not specified\n"));
266         if (wload5 < 0)
267                 usage (_("Warning threshold for 5-minute load average is not specified\n"));
268         if (wload15 < 0)
269                 usage (_("Warning threshold for 15-minute load average is not specified\n"));
270         if (cload1 < 0)
271                 usage (_("Critical threshold for 1-minute load average is not specified\n"));
272         if (cload5 < 0)
273                 usage (_("Critical threshold for 5-minute load average is not specified\n"));
274         if (cload15 < 0)
275                 usage (_("Critical threshold for 15-minute load average is not specified\n"));
276         if (wload1 > cload1)
277                 usage (_("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n"));
278         if (wload5 > cload5)
279                 usage (_("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n"));
280         if (wload15 > cload15)
281                 usage (_("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n"));
282         return OK;
289 \f
290 void
291 print_help (void)
293         print_revision (progname, revision);
295         printf (_("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"));
296         printf (_(COPYRIGHT), copyright, email);
298         printf (_("This plugin tests the current system load average.\n\n"));
300         print_usage ();
302         printf (_(UT_HELP_VRSN));
304         printf (_("\
305  -w, --warning=WLOAD1,WLOAD5,WLOAD15\n\
306    Exit with WARNING status if load average exceeds WLOADn\n\
307  -c, --critical=CLOAD1,CLOAD5,CLOAD15\n\
308    Exit with CRITICAL status if load average exceed CLOADn\n\n\
309 the load average format is the same used by \"uptime\" and \"w\"\n\n"));
311         printf (_(UT_SUPPORT));
314 void
315 print_usage (void)
317         printf (_("Usage: %s -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"),
318                 progname);
319         printf (_(UT_HLP_VRS), progname, progname);