Code

Don't try to print `optarg' (which will be a NULL pointer) if an unknown
[nagiosplug.git] / plugins / check_load.c
1 /******************************************************************************
2 *
3 * Nagios check_load plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_load plugin
13 *
14 *  This plugin tests the current system load average.
15 *
16 *
17 * License Information:
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32  
33  $Id$
35 ******************************************************************************/
37 const char *progname = "check_load";
38 const char *revision = "$Revision$";
39 const char *copyright = "1999-2006";
40 const char *email = "nagiosplug-devel@lists.sourceforge.net";
42 #include "common.h"
43 #include "utils.h"
44 #include "popen.h"
46 #ifdef HAVE_SYS_LOADAVG_H
47 #include <sys/loadavg.h>
48 #endif
50 /* needed for compilation under NetBSD, as suggested by Andy Doran */
51 #ifndef LOADAVG_1MIN
52 #define LOADAVG_1MIN    0
53 #define LOADAVG_5MIN    1
54 #define LOADAVG_15MIN   2
55 #endif /* !defined LOADAVG_1MIN */
58 static int process_arguments (int argc, char **argv);
59 static int validate_arguments (void);
60 void print_help (void);
61 void print_usage (void);
63 /* strictly for pretty-print usage in loops */
64 static const int nums[3] = { 1, 5, 15 };
66 /* provide some fairly sane defaults */
67 double wload[3] = { 0.0, 0.0, 0.0 };
68 double cload[3] = { 0.0, 0.0, 0.0 };
69 #define la1 la[0]
70 #define la5 la[1]
71 #define la15 la[2]
73 char *status_line;
75 static void
76 get_threshold(char *arg, double *th)
77 {
78         size_t i, n;
79         char *str = arg, *p;
81         n = strlen(arg);
82         for(i = 0; i < 3; i++) {
83                 th[i] = strtod(str, &p);
84                 if(p == str) break;
86                 str = p + 1;
87                 if(n <= (size_t)(str - arg)) break;
88         }
90         /* empty argument or non-floatish, so warn about it and die */
91         if(!i) usage (_("Warning threshold must be float or float triplet!\n"));
93         if(i != 2) {
94                 /* one or more numbers were given, so fill array with last
95                  * we got (most likely to NOT produce the least expected result) */
96                 for(n = i; n < 3; n++) th[n] = th[i];
97         }
98 }
101 int
102 main (int argc, char **argv)
104         int result;
105         int i;
107         double la[3] = { 0.0, 0.0, 0.0 };       /* NetBSD complains about unitialized arrays */
108 #ifndef HAVE_GETLOADAVG
109         char input_buffer[MAX_INPUT_BUFFER];
110 # ifdef HAVE_PROC_LOADAVG
111         FILE *fp;
112         char *str, *next;
113 # endif
114 #endif
116         setlocale (LC_ALL, "");
117         bindtextdomain (PACKAGE, LOCALEDIR);
118         textdomain (PACKAGE);
119         setlocale(LC_NUMERIC, "POSIX");
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;
134         }
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;
141                 }
142         }
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;
150         }
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);
154         }
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);
159         if (result) {
160                 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
161                 return STATE_UNKNOWN;
162         }
163 # endif
164 #endif
166         if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
167 #ifdef HAVE_GETLOADAVG
168                 printf (_("Error in getloadavg()\n"));
169 #else
170 # ifdef HAVE_PROC_LOADAVG
171                 printf (_("Error processing %s\n"), PROC_LOADAVG);
172 # else
173                 printf (_("Error processing %s\n"), PATH_TO_UPTIME);
174 # endif
175 #endif
176                 return STATE_UNKNOWN;
177         }
179         /* we got this far, so assume OK until we've measured */
180         result = STATE_OK;
182         asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
184         for(i = 0; i < 3; i++) {
185                 if(la[i] > cload[i]) {
186                         result = STATE_CRITICAL;
187                         break;
188                 }
189                 else if(la[i] > wload[i]) result = STATE_WARNING;
190         }
192         printf("%s - %s|", state_text(result), status_line);
193         for(i = 0; i < 3; i++)
194                 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
196         putchar('\n');
197         return result;
201 /* process command-line arguments */
202 static int
203 process_arguments (int argc, char **argv)
205         int c = 0;
207         int option = 0;
208         static struct option longopts[] = {
209                 {"warning", required_argument, 0, 'w'},
210                 {"critical", required_argument, 0, 'c'},
211                 {"version", no_argument, 0, 'V'},
212                 {"help", no_argument, 0, 'h'},
213                 {0, 0, 0, 0}
214         };
216         if (argc < 2)
217                 return ERROR;
219         while (1) {
220                 c = getopt_long (argc, argv, "Vhc:w:", longopts, &option);
222                 if (c == -1 || c == EOF)
223                         break;
225                 switch (c) {
226                 case 'w': /* warning time threshold */
227                         get_threshold(optarg, wload);
228                         break;
229                 case 'c': /* critical time threshold */
230                         get_threshold(optarg, cload);
231                         break;
232                 case 'V':                                                                       /* version */
233                         print_revision (progname, revision);
234                         exit (STATE_OK);
235                 case 'h':                                                                       /* help */
236                         print_help ();
237                         exit (STATE_OK);
238                 case '?':                                                                       /* help */
239                         usage5 ();
240                 }
241         }
243         c = optind;
244         if (c == argc)
245                 return validate_arguments ();
247         /* handle the case if both arguments are missing,
248          * but not if only one is given without -c or -w flag */
249         if(c - argc == 2) {
250                 get_threshold(argv[c++], wload);
251                 get_threshold(argv[c++], cload);
252         }
253         else if(c - argc == 1) {
254                 get_threshold(argv[c++], cload);
255         }
257         return validate_arguments ();
262 static int
263 validate_arguments (void)
265         int i = 0;
267         /* match cload first, as it will give the most friendly error message
268          * if user hasn't given the -c switch properly */
269         for(i = 0; i < 3; i++) {
270                 if(cload[i] < 0)
271                         die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
272                 if(wload[i] < 0)
273                         die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
274                 if(wload[i] > cload[i])
275                         die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
276         }
278         return OK;
283 void
284 print_help (void)
286         print_revision (progname, revision);
288         printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
289         printf (COPYRIGHT, copyright, email);
291         printf (_("This plugin tests the current system load average."));
293   printf ("\n\n");
294   
295         print_usage ();
297         printf (_(UT_HELP_VRSN));
299         printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
300   printf ("    %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
301   printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
302   printf ("    %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
303   printf ("    %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
305         printf (_(UT_SUPPORT));
308 void
309 print_usage (void)
311   printf (_("Usage:"));
312         printf ("%s -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);