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