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 PROGNAME "check_disk"
40 #define REVISION "$Revision$"
41 #define COPYRIGHT "2000-2002"
43 int process_arguments (int, char **);
44 int validate_arguments (void);
45 int check_disk (int usp, int free_disk);
46 void print_help (void);
47 void print_usage (void);
49 int w_df = -1;
50 int c_df = -1;
51 float w_dfp = -1.0;
52 float c_dfp = -1.0;
53 char *path = "";
54 int verbose = FALSE;
55 int display_mntp = FALSE;
57 int
58 main (int argc, char **argv)
59 {
60 int len;
61 int usp = -1;
62 int total_disk = -1;
63 int used_disk = -1;
64 int free_disk = -1;
65 int result = STATE_UNKNOWN;
66 int temp_result = STATE_UNKNOWN;
67 char *command_line = NULL;
68 char input_buffer[MAX_INPUT_BUFFER] = "";
69 char file_system[MAX_INPUT_BUFFER] = "";
70 char mntp[MAX_INPUT_BUFFER] = "";
71 char outbuf[MAX_INPUT_BUFFER] = "";
72 char *output = NULL;
74 if (process_arguments (argc, argv) != OK)
75 usage ("Could not parse arguments\n");
77 asprintf (&command_line, "%s %s", DF_COMMAND, path);
79 if (verbose)
80 printf ("%s ==> ", command_line);
82 child_process = spopen (command_line);
83 if (child_process == NULL) {
84 printf ("Could not open pipe: %s\n", command_line);
85 return STATE_UNKNOWN;
86 }
88 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
89 if (child_stderr == NULL) {
90 printf ("Could not open stderr for %s\n", command_line);
91 }
93 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
95 if (!index (input_buffer, '/'))
96 continue;
98 if (sscanf
99 (input_buffer, "%s %d %d %d %d%% %s", file_system, &total_disk,
100 &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 /* cannot use max now that STATE_UNKNOWN is greater than STATE_CRITICAL
105 result = max (result, check_disk (usp, free_disk)); */
106 temp_result = check_disk (usp, free_disk) ;
109 if ( temp_result == STATE_CRITICAL ) {
110 result = STATE_CRITICAL;
111 }
112 else if (temp_result == STATE_WARNING) {
113 if ( !( result == STATE_CRITICAL) ) {
114 result = STATE_WARNING;
115 }
116 }
117 else if (temp_result == STATE_OK) {
118 if ( ! ( result == STATE_CRITICAL || result == STATE_WARNING) ){
119 result = STATE_OK;
120 }
121 }
122 else if (temp_result == STATE_UNKNOWN) {
123 if ( ! ( result == STATE_CRITICAL || result == STATE_WARNING || result == STATE_OK) ){
124 result = STATE_UNKNOWN;
125 }
126 }
127 else {
128 /* don't have a match with the return value from check_disk() */
129 result = STATE_UNKNOWN;
130 }
134 len =
135 snprintf (outbuf, MAX_INPUT_BUFFER - 1,
136 " [%d kB (%d%%) free on %s]", free_disk, 100 - usp,
137 display_mntp ? mntp : file_system);
138 outbuf[len] = 0;
139 output = strscat (output, outbuf);
140 }
141 else {
142 printf ("Unable to read output:\n%s\n%s\n", command_line, input_buffer);
143 return result;
144 }
145 }
147 /* If we get anything on stderr, at least set warning */
148 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
149 /*result = max (result, STATE_WARNING); */
150 if( !( result == STATE_CRITICAL) ) {
151 result = STATE_WARNING;
152 }
154 /* close stderr */
155 (void) fclose (child_stderr);
157 /* close the pipe */
158 if (spclose (child_process))
159 /*result = max (result, STATE_WARNING); */
160 if( !( result == STATE_CRITICAL) ) {
161 result = STATE_WARNING;
162 }
163 if (usp < 0)
164 printf ("Disk \"%s\" not mounted or nonexistant\n", path);
165 else if (result == STATE_UNKNOWN)
166 printf ("Unable to read output\n%s\n%s\n", command_line, input_buffer);
167 else
168 printf ("DISK %s -%s\n", state_text (result), output);
170 return result;
171 }
173 /* process command-line arguments */
174 int
175 process_arguments (int argc, char **argv)
176 {
177 int c;
179 #ifdef HAVE_GETOPT_H
180 int option_index = 0;
181 static struct option long_options[] = {
182 {"warning", required_argument, 0, 'w'},
183 {"critical", required_argument, 0, 'c'},
184 {"timeout", required_argument, 0, 't'},
185 {"path", required_argument, 0, 'p'},
186 {"partition", required_argument, 0, 'p'},
187 {"verbose", no_argument, 0, 'v'},
188 {"version", no_argument, 0, 'V'},
189 {"help", no_argument, 0, 'h'},
190 {"mountpoint", no_argument, 0, 'm'},
191 {0, 0, 0, 0}
192 };
193 #endif
195 if (argc < 2)
196 return ERROR;
198 for (c = 1; c < argc; c++)
199 if (strcmp ("-to", argv[c]) == 0)
200 strcpy (argv[c], "-t");
202 while (1) {
203 #ifdef HAVE_GETOPT_H
204 c =
205 getopt_long (argc, argv, "Vhvt:c:w:p:m", long_options, &option_index);
206 #else
207 c = getopt (argc, argv, "Vhvt:c:w:p:m");
208 #endif
210 if (c == -1 || c == EOF)
211 break;
213 switch (c) {
214 case 'w': /* warning time threshold */
215 if (is_intnonneg (optarg)) {
216 w_df = atoi (optarg);
217 break;
218 }
219 else if (strpbrk (optarg, ",:") &&
220 strstr (optarg, "%") &&
221 sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
222 break;
223 }
224 else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
225 break;
226 }
227 else {
228 usage ("Warning threshold must be integer or percentage!\n");
229 }
230 case 'c': /* critical time threshold */
231 if (is_intnonneg (optarg)) {
232 c_df = atoi (optarg);
233 break;
234 }
235 else if (strpbrk (optarg, ",:") &&
236 strstr (optarg, "%") &&
237 sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
238 break;
239 }
240 else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
241 break;
242 }
243 else {
244 usage ("Critical threshold must be integer or percentage!\n");
245 }
246 case 't': /* timeout period */
247 if (is_integer (optarg)) {
248 timeout_interval = atoi (optarg);
249 break;
250 }
251 else {
252 usage ("Timeout Interval must be an integer!\n");
253 }
254 case 'p': /* path or partition */
255 path = optarg;
256 break;
257 case 'v': /* verbose */
258 verbose = TRUE;
259 break;
260 case 'm': /* display mountpoint */
261 display_mntp = TRUE;
262 break;
263 case 'V': /* version */
264 print_revision (PROGNAME, REVISION);
265 exit (STATE_OK);
266 case 'h': /* help */
267 print_help ();
268 exit (STATE_OK);
269 case '?': /* help */
270 usage ("check_disk: unrecognized option\n");
271 break;
272 }
273 }
275 c = optind;
276 if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
277 w_dfp = (100.0 - atof (argv[c++]));
279 if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
280 c_dfp = (100.0 - atof (argv[c++]));
282 if (argc > c && strlen (path) == 0)
283 path = argv[c++];
285 return validate_arguments ();
286 }
288 int
289 validate_arguments ()
290 {
291 if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
292 printf ("INPUT ERROR: Unable to parse command line\n");
293 return ERROR;
294 }
295 else if ((w_dfp >= 0 || c_dfp >= 0)
296 && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
297 || c_dfp > w_dfp)) {
298 printf
299 ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
300 c_dfp, w_dfp);
301 return ERROR;
302 }
303 else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
304 printf
305 ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
306 c_df, w_df);
307 return ERROR;
308 }
309 else {
310 return OK;
311 }
312 }
314 int
315 check_disk (usp, free_disk)
316 {
317 int result = STATE_UNKNOWN;
318 /* check the percent used space against thresholds */
319 if (usp >= 0 && usp >= (100.0 - c_dfp))
320 result = STATE_CRITICAL;
321 else if (c_df >= 0 && free_disk <= c_df)
322 result = STATE_CRITICAL;
323 else if (usp >= 0 && usp >= (100.0 - w_dfp))
324 result = STATE_WARNING;
325 else if (w_df >= 0 && free_disk <= w_df)
326 result = STATE_WARNING;
327 else if (usp >= 0.0)
328 result = STATE_OK;
329 return result;
330 }
332 void
333 print_help (void)
334 {
335 print_revision (PROGNAME, REVISION);
336 printf
337 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
338 "This plugin will check the percent of used disk space on a mounted\n"
339 "file system and generate an alert if percentage is above one of the\n"
340 "threshold values.\n\n");
341 print_usage ();
342 printf
343 ("\nOptions:\n"
344 " -w, --warning=INTEGER\n"
345 " Exit with WARNING status if less than INTEGER kilobytes of disk are free\n"
346 " -w, --warning=PERCENT%%\n"
347 " Exit with WARNING status if more than PERCENT of disk space is free\n"
348 " -c, --critical=INTEGER\n"
349 " Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n"
350 " -c, --critical=PERCENT%%\n"
351 " Exit with CRITCAL status if more than PERCENT of disk space is free\n"
352 " -p, --path=PATH, --partition=PARTTION\n"
353 " Path or partition (checks all mounted partitions if unspecified)\n"
354 " -m, --mountpoint\n"
355 " Display the mountpoint instead of the partition\n"
356 " -v, --verbose\n"
357 " Show details for command-line debugging (do not use with nagios server)\n"
358 " -h, --help\n"
359 " Print detailed help screen\n"
360 " -V, --version\n" " Print version information\n\n");
361 support ();
362 }
364 void
365 print_usage (void)
366 {
367 printf
368 ("Usage: %s -w limit -c limit [-p path] [-t timeout] [-m] [--verbose]\n"
369 " %s (-h|--help)\n"
370 " %s (-V|--version)\n", PROGNAME, PROGNAME, PROGNAME);
371 }