Code

check_http min size option (680467 - Dave Viner)
[nagiosplug.git] / plugins / check_disk.c
1 /******************************************************************************
2  *
3  * CHECK_DISK.C
4  *
5  * Program: Disk space plugin for Nagios
6  * License: GPL
7  * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8  * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
9  *
10  * $Id$
11  *
12  * Description:
13  *
14  * This plugin will use the /bin/df command to check the free space on
15  * currently mounted filesystems.  If the percent used disk space is
16  * above <c_dfp>, a STATE_CRITICAL is returned.  If the percent used
17  * disk space is above <w_dfp>, a STATE_WARNING is returned.  If the
18  * speicified filesystem cannot be read, a STATE_CRITICAL is returned,
19  * other errors with reading the output result in a STATE_UNKNOWN
20  * error.
21  *
22  * Notes:
23  *  - IRIX support added by Charlie Cook 4-16-1999
24  *  - Modifications by Karl DeBisschop 1999-11-24
25  *     reformat code to 80 char screen width
26  *     set STATE_WARNING if stderr is written or spclose status set
27  *     set default result to STAT_UNKNOWN
28  *     initailize usp to -1, eliminate 'found' variable
29  *     accept any filename/filesystem
30  *     use sscanf, drop while loop
31  *
32  *****************************************************************************/
34 #include "common.h"
35 #include "popen.h"
36 #include "utils.h"
37 #include <stdarg.h>
39 #define REVISION "$Revision$"
40 #define COPYRIGHT "2000-2002"
42 int process_arguments (int, char **);
43 int validate_arguments (void);
44 int check_disk (int usp, int free_disk);
45 void print_help (void);
46 void print_usage (void);
48 const char *progname = "check_disk";
50 int w_df = -1;
51 int c_df = -1;
52 float w_dfp = -1.0;
53 float c_dfp = -1.0;
54 char *path = "";
55 char *exclude_device = "";
56 int verbose = 0;
57 int display_mntp = FALSE;
60 int
61 main (int argc, char **argv)
62 {
63         int usp = -1;
64         int total_disk = -1;
65         int used_disk = -1;
66         int free_disk = -1;
67         int result = STATE_UNKNOWN;
68         int disk_result = STATE_UNKNOWN;
69         char *command_line = "";
70         char input_buffer[MAX_INPUT_BUFFER];
71         char file_system[MAX_INPUT_BUFFER];
72         char mntp[MAX_INPUT_BUFFER];
73         char *output = "";
75         if (process_arguments (argc, argv) != OK)
76                 usage ("Could not parse arguments\n");
78         asprintf (&command_line, "%s %s", DF_COMMAND, path);
80         if (verbose>0)
81                 printf ("%s ==> ", command_line);
83         child_process = spopen (command_line);
84         if (child_process == NULL) {
85                 printf ("Could not open pipe: %s\n", command_line);
86                 return STATE_UNKNOWN;
87         }
89         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
90         if (child_stderr == NULL) {
91                 printf ("Could not open stderr for %s\n", command_line);
92         }
94         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
96                 if (!index (input_buffer, '/'))
97                         continue;
99                 if (sscanf (input_buffer, "%s %d %d %d %d%% %s", file_system,
100                      &total_disk, &used_disk, &free_disk, &usp, mntp) == 6 ||
101                     sscanf (input_buffer, "%s %*s %d %d %d %d%% %s", file_system,
102                                  &total_disk, &used_disk, &free_disk, &usp, mntp) == 6) {
104                         if (strcmp(exclude_device,file_system) == 0 ||
105                             strcmp(exclude_device,mntp) == 0) {
106                                 if (verbose>0)
107                                         printf ("ignoring %s.", file_system);
108                                 continue;
109                         }
111                         disk_result = check_disk (usp, free_disk);
113                         if (strcmp (file_system, "none") == 0)
114                                 strncpy (file_system, mntp, MAX_INPUT_BUFFER-1);
116                         if (disk_result!=STATE_OK || verbose>=0) 
117                                 asprintf (&output, "%s [%d kB (%d%%) free on %s]", output,
118                                           free_disk, 100 - usp, display_mntp ? mntp : file_system);
120                         result = max_state (result, disk_result);
121                 }
123                 else {
124                         printf ("Unable to read output:\n%s\n%s\n", command_line, input_buffer);
125                         return result;
126                 }
128         }
130         /* If we get anything on stderr, at least set warning */
131         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
132                 if (result != STATE_CRITICAL) {
133                         result = STATE_WARNING;
134                 }
135         }
137         /* close stderr */
138         if (child_stderr) 
139                 (void) fclose (child_stderr);
141         /* close the pipe */
142         if (spclose(child_process)!=0 && result!=STATE_CRITICAL)
143                         result = STATE_WARNING;
145         if (usp < 0)
146                 printf ("Disk \"%s\" not mounted or nonexistant\n", path);
147         else if (result == STATE_UNKNOWN)
148                 printf ("Unable to read output\n%s\n%s\n", command_line, input_buffer);
149         else
150                 printf ("DISK %s%s\n", state_text (result), output);
152         return result;
155 /* process command-line arguments */
156 int
157 process_arguments (int argc, char **argv)
159         int c;
161 #ifdef HAVE_GETOPT_H
162         int option_index = 0;
163         static struct option long_options[] = {
164                 {"warning", required_argument, 0, 'w'},
165                 {"critical", required_argument, 0, 'c'},
166                 {"timeout", required_argument, 0, 't'},
167                 {"path", required_argument, 0, 'p'},
168                 {"partition", required_argument, 0, 'p'},
169                 {"verbose", no_argument, 0, 'v'},
170                 {"version", no_argument, 0, 'V'},
171                 {"help", no_argument, 0, 'h'},
172                 {"mountpoint", no_argument, 0, 'm'},
173                 {"exclude_device", required_argument, 0, 'x'},
174                 {"quiet", no_argument, 0, 'q'},
176                 {0, 0, 0, 0}
177         };
178 #endif
180         if (argc < 2)
181                 return ERROR;
183         for (c = 1; c < argc; c++)
184                 if (strcmp ("-to", argv[c]) == 0)
185                         strcpy (argv[c], "-t");
187         while (1) {
188 #ifdef HAVE_GETOPT_H
189                 c =
190                         getopt_long (argc, argv, "+?Vqhvt:c:w:p:x:m", long_options, &option_index);
191 #else
192                 c = getopt (argc, argv, "+?Vqhvt:c:w:p:x:m");
193 #endif
195                 if (c == -1 || c == EOF)
196                         break;
198                 switch (c) {
199                 case 'w':                                                                       /* warning time threshold */
200                         if (is_intnonneg (optarg)) {
201                                 w_df = atoi (optarg);
202                                 break;
203                         }
204                         else if (strpbrk (optarg, ",:") &&
205                                                          strstr (optarg, "%") &&
206                                                          sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
207                                 break;
208                         }
209                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
210                                 break;
211                         }
212                         else {
213                                 usage ("Warning threshold must be integer or percentage!\n");
214                         }
215                 case 'c':                                                                       /* critical time threshold */
216                         if (is_intnonneg (optarg)) {
217                                 c_df = atoi (optarg);
218                                 break;
219                         }
220                         else if (strpbrk (optarg, ",:") &&
221                                                          strstr (optarg, "%") &&
222                                                          sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
223                                 break;
224                         }
225                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
226                                 break;
227                         }
228                         else {
229                                 usage ("Critical threshold must be integer or percentage!\n");
230                         }
231                 case 't':                                                                       /* timeout period */
232                         if (is_integer (optarg)) {
233                                 timeout_interval = atoi (optarg);
234                                 break;
235                         }
236                         else {
237                                 usage ("Timeout Interval must be an integer!\n");
238                         }
239                 case 'p':                                                                       /* path or partition */
240                         path = optarg;
241                         break;
242                 case 'v':                                                                       /* verbose */
243                         verbose++;
244                         break;
245                 case 'q':                                                                       /* verbose */
246                         verbose--;
247                         break;
248                 case 'm': /* display mountpoint */
249                         display_mntp = TRUE;
250                         break;
251                 case 'x':                                                                       /* exclude path or partition */
252                         exclude_device = optarg;
253                         break;
254                 case 'V':                                                                       /* version */
255                         print_revision (progname, REVISION);
256                         exit (STATE_OK);
257                 case 'h':                                                                       /* help */
258                         print_help ();
259                         exit (STATE_OK);
260                 case '?':                                                                       /* help */
261                         usage ("check_disk: unrecognized option\n");
262                         break;
263                 }
264         }
266         c = optind;
267         if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
268                 w_dfp = (100.0 - atof (argv[c++]));
270         if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
271                 c_dfp = (100.0 - atof (argv[c++]));
273         if (argc > c && strlen (path) == 0)
274                 path = argv[c++];
276         return validate_arguments ();
279 int
280 validate_arguments ()
282         if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
283                 printf ("INPUT ERROR: Unable to parse command line\n");
284                 return ERROR;
285         }
286         else if ((w_dfp >= 0 || c_dfp >= 0)
287                                          && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
288                                                          || c_dfp > w_dfp)) {
289                 printf
290                         ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
291                          c_dfp, w_dfp);
292                 return ERROR;
293         }
294         else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
295                 printf
296                         ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
297                          c_df, w_df);
298                 return ERROR;
299         }
300         else {
301                 return OK;
302         }
305 int
306 check_disk (usp, free_disk)
308         int result = STATE_UNKNOWN;
309         /* check the percent used space against thresholds */
310         if (usp >= 0 && usp >= (100.0 - c_dfp))
311                 result = STATE_CRITICAL;
312         else if (c_df >= 0 && free_disk <= c_df)
313                 result = STATE_CRITICAL;
314         else if (usp >= 0 && usp >= (100.0 - w_dfp))
315                 result = STATE_WARNING;
316         else if (w_df >= 0 && free_disk <= w_df)
317                 result = STATE_WARNING;
318         else if (usp >= 0.0)
319                 result = STATE_OK;
320         return result;
323 void
324 print_help (void)
326         print_revision (progname, REVISION);
327         printf
328                 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
329                  "This plugin will check the percent of used disk space on a mounted\n"
330                  "file system and generate an alert if percentage is above one of the\n"
331                  "threshold values.\n\n");
332         print_usage ();
333         printf
334                 ("\nOptions:\n"
335                  " -w, --warning=INTEGER\n"
336                  "   Exit with WARNING status if less than INTEGER kilobytes of disk are free\n"
337                  " -w, --warning=PERCENT%%\n"
338                  "   Exit with WARNING status if less than PERCENT of disk space is free\n"
339                  " -c, --critical=INTEGER\n"
340                  "   Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n"
341                  " -c, --critical=PERCENT%%\n"
342                  "   Exit with CRITCAL status if less than PERCENT of disk space is free\n"
343                  " -p, --path=PATH, --partition=PARTTION\n"
344                  "    Path or partition (checks all mounted partitions if unspecified)\n"
345                  " -m, --mountpoint\n"
346                  "    Display the mountpoint instead of the partition\n"
347                  " -x, --exclude_device=PATH\n"
348                  "    Ignore device (only works if -p unspecified)\n"
349                  " -e, --errors-only\n"
350                  "    Display only devices/mountpoints with errors\n"
351                  " -v, --verbose\n"
352                  "    Show details for command-line debugging (do not use with nagios server)\n"
353                  " -h, --help\n"
354                  "    Print detailed help screen\n"
355                  " -V, --version\n" "    Print version information\n\n");
356         support ();
359 void
360 print_usage (void)
362         printf
363                 ("Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e] [--verbose]\n"
364                  "       %s (-h|--help)\n"
365                  "       %s (-V|--version)\n", progname, progname, progname);