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_disk";
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 #if HAVE_INTTYPES_H
26 # include <inttypes.h>
27 #endif
28 #include <assert.h>
29 #include "popen.h"
30 #include "utils.h"
31 #include <stdarg.h>
32 #include "../lib/fsusage.h"
33 #include "../lib/mountlist.h"
34 #if HAVE_LIMITS_H
35 # include <limits.h>
36 #endif
38 /* If nonzero, show inode information. */
39 /* static int inode_format; */
41 /* If nonzero, show even filesystems with zero size or
42 uninteresting types. */
43 static int show_all_fs = 1;
45 /* If nonzero, show only local filesystems. */
46 static int show_local_fs = 0;
48 /* If positive, the units to use when printing sizes;
49 if negative, the human-readable base. */
50 /* static int output_block_size; */
52 /* If nonzero, invoke the `sync' system call before getting any usage data.
53 Using this option can make df very slow, especially with many or very
54 busy disks. Note that this may make a difference on some systems --
55 SunOs4.1.3, for one. It is *not* necessary on Linux. */
56 /* static int require_sync = 0; */
58 /* A filesystem type to display. */
60 struct name_list
61 {
62 char *name;
63 int found;
64 uintmax_t w_df;
65 uintmax_t c_df;
66 double w_dfp;
67 double c_dfp;
68 struct name_list *name_next;
69 };
71 /* Linked list of filesystem types to display.
72 If `fs_select_list' is NULL, list all types.
73 This table is generated dynamically from command-line options,
74 rather than hardcoding into the program what it thinks are the
75 valid filesystem types; let the user specify any filesystem type
76 they want to, and if there are any filesystems of that type, they
77 will be shown.
79 Some filesystem types:
80 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
82 /* static struct name_list *fs_select_list; */
84 /* Linked list of filesystem types to omit.
85 If the list is empty, don't exclude any types. */
87 static struct name_list *fs_exclude_list;
89 static struct name_list *dp_exclude_list;
91 static struct name_list *path_select_list;
93 static struct name_list *dev_select_list;
95 /* Linked list of mounted filesystems. */
96 static struct mount_entry *mount_list;
98 /* For long options that have no equivalent short option, use a
99 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
100 enum
101 {
102 SYNC_OPTION = CHAR_MAX + 1,
103 NO_SYNC_OPTION,
104 BLOCK_SIZE_OPTION
105 };
107 #ifdef _AIX
108 #pragma alloca
109 #endif
111 int process_arguments (int, char **);
112 void print_path (const char *mypath);
113 int validate_arguments (uintmax_t, uintmax_t, double, double, char *);
114 int check_disk (double usp, uintmax_t free_disk);
115 int walk_name_list (struct name_list *list, const char *name);
116 void print_help (void);
117 void print_usage (void);
119 uintmax_t w_df = 0;
120 uintmax_t c_df = 0;
121 double w_dfp = -1.0;
122 double c_dfp = -1.0;
123 char *path;
124 char *exclude_device;
125 char *units;
126 uintmax_t mult = 1024 * 1024;
127 int verbose = 0;
128 int erronly = FALSE;
129 int display_mntp = FALSE;
131 /* Linked list of mounted filesystems. */
132 static struct mount_entry *mount_list;
135 \f
136 int
137 main (int argc, char **argv)
138 {
139 double usp = -1.0;
140 int result = STATE_UNKNOWN;
141 int disk_result = STATE_UNKNOWN;
142 char file_system[MAX_INPUT_BUFFER];
143 char *output;
144 char *details;
145 float free_space, free_space_pct, total_space;
147 struct mount_entry *me;
148 struct fs_usage fsp;
149 struct name_list *temp_list;
151 output = strdup ("");
152 details = strdup ("");
154 setlocale (LC_ALL, "");
155 bindtextdomain (PACKAGE, LOCALEDIR);
156 textdomain (PACKAGE);
158 mount_list = read_filesystem_list (0);
160 if (process_arguments (argc, argv) != OK)
161 usage (_("Could not parse arguments\n"));
163 for (me = mount_list; me; me = me->me_next) {
165 if (path_select_list &&
166 (walk_name_list (path_select_list, me->me_mountdir) ||
167 walk_name_list (path_select_list, me->me_devname) ) )
168 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
169 else if (dev_select_list || path_select_list)
170 continue;
171 else if (me->me_remote && show_local_fs)
172 continue;
173 else if (me->me_dummy && !show_all_fs)
174 continue;
175 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
176 continue;
177 else if (dp_exclude_list &&
178 (walk_name_list (dp_exclude_list, me->me_devname) ||
179 walk_name_list (dp_exclude_list, me->me_mountdir)))
180 continue;
181 else
182 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
184 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
185 usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
186 disk_result = check_disk (usp, fsp.fsu_bavail);
187 result = max_state (disk_result, result);
188 if (disk_result==STATE_OK && erronly && !verbose)
189 continue;
191 free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
192 free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
193 total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
194 if (disk_result!=STATE_OK || verbose>=0)
195 asprintf (&output, ("%s [%.0f %s (%.0f%%) free on %s]"),
196 output,
197 free_space,
198 units,
199 free_space_pct,
200 (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
201 asprintf (&details, _("%s\n\
202 %.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
203 details, free_space, total_space, units, free_space_pct,
204 me->me_devname, me->me_type, me->me_mountdir,
205 (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
206 }
208 }
210 if (verbose > 2)
211 asprintf (&output, "%s%s", output, details);
213 /* Override result if paths specified and not found */
214 temp_list = path_select_list;
215 while (temp_list) {
216 if (temp_list->found != TRUE) {
217 asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
218 result = STATE_CRITICAL;
219 }
220 temp_list = temp_list->name_next;
221 }
223 die (result, "DISK %s%s%s\n", state_text (result), output, details);
224 return STATE_UNKNOWN;
225 }
229 \f
230 /* process command-line arguments */
231 int
232 process_arguments (int argc, char **argv)
233 {
234 int c;
235 struct name_list *se;
236 struct name_list **pathtail = &path_select_list;
237 struct name_list **fstail = &fs_exclude_list;
238 struct name_list **dptail = &dp_exclude_list;
239 struct name_list *temp_list;
240 int result = OK;
242 unsigned long l;
244 int option = 0;
245 static struct option longopts[] = {
246 {"timeout", required_argument, 0, 't'},
247 {"warning", required_argument, 0, 'w'},
248 {"critical", required_argument, 0, 'c'},
249 {"local", required_argument, 0, 'l'},
250 {"kilobytes", required_argument, 0, 'k'},
251 {"megabytes", required_argument, 0, 'm'},
252 {"units", required_argument, 0, 'u'},
253 {"path", required_argument, 0, 'p'},
254 {"partition", required_argument, 0, 'p'},
255 {"exclude_device", required_argument, 0, 'x'},
256 {"exclude-type", required_argument, 0, 'X'},
257 {"mountpoint", no_argument, 0, 'M'},
258 {"errors-only", no_argument, 0, 'e'},
259 {"verbose", no_argument, 0, 'v'},
260 {"quiet", no_argument, 0, 'q'},
261 {"clear", no_argument, 0, 'C'},
262 {"version", no_argument, 0, 'V'},
263 {"help", no_argument, 0, 'h'},
264 {0, 0, 0, 0}
265 };
267 if (argc < 2)
268 return ERROR;
270 se = (struct name_list *) malloc (sizeof (struct name_list));
271 se->name = strdup ("iso9660");
272 se->name_next = NULL;
273 *fstail = se;
274 fstail = &se->name_next;
276 for (c = 1; c < argc; c++)
277 if (strcmp ("-to", argv[c]) == 0)
278 strcpy (argv[c], "-t");
280 while (1) {
281 c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", longopts, &option);
283 if (c == -1 || c == EOF)
284 break;
286 switch (c) {
287 case 't': /* timeout period */
288 if (is_integer (optarg)) {
289 timeout_interval = atoi (optarg);
290 break;
291 }
292 else {
293 usage (_("Timeout Interval must be an integer!\n"));
294 }
295 case 'w': /* warning threshold */
296 if (is_intnonneg (optarg)) {
297 w_df = atoi (optarg);
298 break;
299 }
300 else if (strpbrk (optarg, ",:") &&
301 strstr (optarg, "%") &&
302 sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
303 w_df = (uintmax_t)l;
304 break;
305 }
306 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
307 break;
308 }
309 else {
310 usage (_("Warning threshold must be integer or percentage!\n"));
311 }
312 case 'c': /* critical threshold */
313 if (is_intnonneg (optarg)) {
314 c_df = atoi (optarg);
315 break;
316 }
317 else if (strpbrk (optarg, ",:") &&
318 strstr (optarg, "%") &&
319 sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) {
320 c_df = (uintmax_t)l;
321 break;
322 }
323 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
324 break;
325 }
326 else {
327 usage (_("Critical threshold must be integer or percentage!\n"));
328 }
329 case 'u':
330 if (units)
331 free(units);
332 if (! strcmp (optarg, "bytes")) {
333 mult = (uintmax_t)1;
334 units = strdup ("B");
335 } else if (! strcmp (optarg, "kB")) {
336 mult = (uintmax_t)1024;
337 units = strdup ("kB");
338 } else if (! strcmp (optarg, "MB")) {
339 mult = (uintmax_t)1024 * 1024;
340 units = strdup ("MB");
341 } else if (! strcmp (optarg, "GB")) {
342 mult = (uintmax_t)1024 * 1024 * 1024;
343 units = strdup ("GB");
344 } else if (! strcmp (optarg, "TB")) {
345 mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
346 units = strdup ("TB");
347 } else {
348 die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
349 }
350 if (units == NULL)
351 die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
352 break;
353 case 'k': /* display mountpoint */
354 mult = 1024;
355 if (units)
356 free(units);
357 units = strdup ("kB");
358 break;
359 case 'm': /* display mountpoint */
360 mult = 1024 * 1024;
361 if (units)
362 free(units);
363 units = strdup ("kB");
364 break;
365 case 'l':
366 show_local_fs = 1;
367 break;
368 case 'p': /* select path */
369 se = (struct name_list *) malloc (sizeof (struct name_list));
370 se->name = optarg;
371 se->name_next = NULL;
372 se->w_df = w_df;
373 se->c_df = c_df;
374 se->w_dfp = w_dfp;
375 se->c_dfp = c_dfp;
376 *pathtail = se;
377 pathtail = &se->name_next;
378 break;
379 case 'x': /* exclude path or partition */
380 se = (struct name_list *) malloc (sizeof (struct name_list));
381 se->name = optarg;
382 se->name_next = NULL;
383 *dptail = se;
384 dptail = &se->name_next;
385 break;
386 case 'X': /* exclude file system type */
387 se = (struct name_list *) malloc (sizeof (struct name_list));
388 se->name = optarg;
389 se->name_next = NULL;
390 *fstail = se;
391 fstail = &se->name_next;
392 break;
393 case 'v': /* verbose */
394 verbose++;
395 break;
396 case 'q': /* verbose */
397 verbose--;
398 break;
399 case 'e':
400 erronly = TRUE;
401 break;
402 case 'M': /* display mountpoint */
403 display_mntp = TRUE;
404 break;
405 case 'C':
406 w_df = 0;
407 c_df = 0;
408 w_dfp = -1.0;
409 c_dfp = -1.0;
410 break;
411 case 'V': /* version */
412 print_revision (progname, revision);
413 exit (STATE_OK);
414 case 'h': /* help */
415 print_help ();
416 exit (STATE_OK);
417 case '?': /* help */
418 usage (_("check_disk: unrecognized option\n"));
419 break;
420 }
421 }
423 /* Support for "check_disk warn crit [fs]" with thresholds at used level */
424 c = optind;
425 if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
426 w_dfp = (100.0 - atof (argv[c++]));
428 if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
429 c_dfp = (100.0 - atof (argv[c++]));
431 if (argc > c && path == NULL) {
432 se = (struct name_list *) malloc (sizeof (struct name_list));
433 se->name = strdup (argv[c++]);
434 se->name_next = NULL;
435 se->w_df = w_df;
436 se->c_df = c_df;
437 se->w_dfp = w_dfp;
438 se->c_dfp = c_dfp;
439 *pathtail = se;
440 }
442 if (path_select_list) {
443 temp_list = path_select_list;
444 while (temp_list) {
445 if (validate_arguments (temp_list->w_df,
446 temp_list->c_df,
447 temp_list->w_dfp,
448 temp_list->c_dfp,
449 temp_list->name) == ERROR)
450 result = ERROR;
451 temp_list = temp_list->name_next;
452 }
453 return result;
454 } else {
455 return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
456 }
457 }
460 void
461 print_path (const char *mypath)
462 {
463 if (mypath == NULL)
464 printf ("\n");
465 else
466 printf (" for %s\n", mypath);
468 return;
469 }
471 int
472 validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
473 {
474 if (w == 0 && c == 0 && wp < 0.0 && cp < 0.0) {
475 printf (_("INPUT ERROR: No thresholds specified"));
476 print_path (mypath);
477 return ERROR;
478 }
479 else if ((wp >= 0.0 || cp >= 0.0) &&
480 (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
481 printf (_("\
482 INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
483 cp, wp);
484 print_path (mypath);
485 return ERROR;
486 }
487 else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
488 printf (_("\
489 INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
490 (unsigned long)c, (unsigned long)w);
491 print_path (mypath);
492 return ERROR;
493 }
495 if (units == NULL) {
496 units = strdup ("MB");
497 mult = (uintmax_t)1024 * 1024;
498 }
499 return OK;
500 }
504 \f
505 int
506 check_disk (double usp, uintmax_t free_disk)
507 {
508 int result = STATE_UNKNOWN;
509 /* check the percent used space against thresholds */
510 if (usp >= 0.0 && usp >= (100.0 - c_dfp))
511 result = STATE_CRITICAL;
512 else if (c_df > 0 && free_disk <= c_df)
513 result = STATE_CRITICAL;
514 else if (usp >= 0.0 && usp >= (100.0 - w_dfp))
515 result = STATE_WARNING;
516 else if (w_df > 0 && free_disk <= w_df)
517 result = STATE_WARNING;
518 else if (usp >= 0.0)
519 result = STATE_OK;
520 return result;
521 }
525 int
526 walk_name_list (struct name_list *list, const char *name)
527 {
528 while (list) {
529 if (! strcmp(list->name, name)) {
530 list->found = 1;
531 /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
532 if (list->w_df) w_df = list->w_df;
533 if (list->c_df) c_df = list->c_df;
534 if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
535 if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
536 return TRUE;
537 }
538 list = list->name_next;
539 }
540 return FALSE;
541 }
547 \f
548 void
549 print_help (void)
550 {
551 print_revision (progname, revision);
553 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
554 printf (_(COPYRIGHT), copyright, email);
556 printf (_("\
557 This plugin checks the amount of used disk space on a mounted file system\n\
558 and generates an alert if free space is less than one of the threshold values."));
560 print_usage ();
562 printf (_(UT_HELP_VRSN));
564 printf (_("\
565 -w, --warning=INTEGER\n\
566 Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
567 -w, --warning=PERCENT%%\n\
568 Exit with WARNING status if less than PERCENT of disk space is free\n\
569 -c, --critical=INTEGER\n\
570 Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
571 -c, --critical=PERCENT%%\n\
572 Exit with CRITCAL status if less than PERCENT of disk space is free\n\
573 -C, --clear\n\
574 Clear thresholds\n"));
576 printf (_("\
577 -u, --units=STRING\n\
578 Choose bytes, kB, MB, GB, TB (default: MB)\n\
579 -k, --kilobytes\n\
580 Same as '--units kB'\n\
581 -m, --megabytes\n\
582 Same as '--units MB'\n"));
584 printf (_("\
585 -l, --local\n\
586 Only check local filesystems\n\
587 -p, --path=PATH, --partition=PARTITION\n\
588 Path or partition (may be repeated)\n\
589 -x, --exclude_device=PATH <STRING>\n\
590 Ignore device (only works if -p unspecified)\n\
591 -X, --exclude-type=TYPE <STRING>\n\
592 Ignore all filesystems of indicated type (may be repeated)\n\
593 -M, --mountpoint\n\
594 Display the mountpoint instead of the partition\n\
595 -e, --errors-only\n\
596 Display only devices/mountpoints with errors\n"));
598 printf (_(UT_WARN_CRIT));
600 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
602 printf (_(UT_VERBOSE));
604 printf ("%s", _("Examples:\n\
605 check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
606 Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
608 printf (_(UT_SUPPORT));
609 }
614 void
615 print_usage (void)
616 {
617 printf (_("\
618 Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
619 [-v] [-q]\n\
620 %s (-h|--help)\n\
621 %s (-V|--version)\n"),
622 progname, progname, progname);
623 }