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 $Id$
19 *****************************************************************************/
21 const char *progname = "check_disk";
22 const char *program_name = "check_disk"; /* Required for coreutils libs */
23 const char *revision = "$Revision$";
24 const char *copyright = "1999-2004";
25 const char *email = "nagiosplug-devel@lists.sourceforge.net";
27 #include "common.h"
28 #if HAVE_INTTYPES_H
29 # include <inttypes.h>
30 #endif
31 #include <assert.h>
32 #include "popen.h"
33 #include "utils.h"
34 #include <stdarg.h>
35 #include "../lib/fsusage.h"
36 #include "../lib/mountlist.h"
37 #if HAVE_LIMITS_H
38 # include <limits.h>
39 #endif
41 /* If nonzero, show inode information. */
42 /* static int inode_format; */
44 /* If nonzero, show even filesystems with zero size or
45 uninteresting types. */
46 static int show_all_fs = 1;
48 /* If nonzero, show only local filesystems. */
49 static int show_local_fs = 0;
51 /* If positive, the units to use when printing sizes;
52 if negative, the human-readable base. */
53 /* static int output_block_size; */
55 /* If nonzero, invoke the `sync' system call before getting any usage data.
56 Using this option can make df very slow, especially with many or very
57 busy disks. Note that this may make a difference on some systems --
58 SunOs4.1.3, for one. It is *not* necessary on Linux. */
59 /* static int require_sync = 0; */
61 /* A filesystem type to display. */
63 struct name_list
64 {
65 char *name;
66 int found;
67 int found_len;
68 uintmax_t w_df;
69 uintmax_t c_df;
70 double w_dfp;
71 double c_dfp;
72 struct name_list *name_next;
73 };
75 /* Linked list of filesystem types to display.
76 If `fs_select_list' is NULL, list all types.
77 This table is generated dynamically from command-line options,
78 rather than hardcoding into the program what it thinks are the
79 valid filesystem types; let the user specify any filesystem type
80 they want to, and if there are any filesystems of that type, they
81 will be shown.
83 Some filesystem types:
84 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
86 /* static struct name_list *fs_select_list; */
88 /* Linked list of filesystem types to omit.
89 If the list is empty, don't exclude any types. */
91 static struct name_list *fs_exclude_list;
93 static struct name_list *dp_exclude_list;
95 static struct name_list *path_select_list;
97 static struct name_list *dev_select_list;
99 /* Linked list of mounted filesystems. */
100 static struct mount_entry *mount_list;
102 /* For long options that have no equivalent short option, use a
103 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
104 enum
105 {
106 SYNC_OPTION = CHAR_MAX + 1,
107 NO_SYNC_OPTION,
108 BLOCK_SIZE_OPTION
109 };
111 #ifdef _AIX
112 #pragma alloca
113 #endif
115 int process_arguments (int, char **);
116 void print_path (const char *mypath);
117 int validate_arguments (uintmax_t, uintmax_t, double, double, char *);
118 int check_disk (double usp, double free_disk);
119 int walk_name_list (struct name_list *list, const char *name);
120 void print_help (void);
121 void print_usage (void);
123 uintmax_t w_df = 0;
124 uintmax_t c_df = 0;
125 double w_dfp = -1.0;
126 double c_dfp = -1.0;
127 char *path;
128 char *exclude_device;
129 char *units;
130 uintmax_t mult = 1024 * 1024;
131 int verbose = 0;
132 int erronly = FALSE;
133 int display_mntp = FALSE;
135 /* Linked list of mounted filesystems. */
136 static struct mount_entry *mount_list;
140 int
141 main (int argc, char **argv)
142 {
143 double usp = -1.0;
144 int result = STATE_UNKNOWN;
145 int disk_result = STATE_UNKNOWN;
146 char file_system[MAX_INPUT_BUFFER];
147 char *output;
148 char *details;
149 char *perf;
150 uintmax_t psize;
151 float free_space, free_space_pct, total_space;
153 struct mount_entry *me;
154 struct fs_usage fsp;
155 struct name_list *temp_list;
157 output = strdup (" - free space:");
158 details = strdup ("");
159 perf = strdup ("");
161 setlocale (LC_ALL, "");
162 bindtextdomain (PACKAGE, LOCALEDIR);
163 textdomain (PACKAGE);
165 mount_list = read_filesystem_list (0);
167 if (process_arguments (argc, argv) == ERROR)
168 usage4 (_("Could not parse arguments"));
170 /* if a list of paths has been selected, preseed the list with
171 * the longest matching filesystem name by iterating across
172 * the mountlist once ahead of time. this will allow a query on
173 * "/var/log" to return information about "/var" if no "/var/log"
174 * filesystem exists, etc. this is the default behavior already
175 * with df-based checks, but for systems with their own space
176 * checking routines, this should make them more consistent.
177 */
178 if(path_select_list){
179 for (me = mount_list; me; me = me->me_next) {
180 walk_name_list(path_select_list, me->me_mountdir);
181 walk_name_list(path_select_list, me->me_devname);
182 }
183 /* now pretend we never saw anything, but keep found_len.
184 * thus future searches will only match the best match */
185 for (temp_list = path_select_list; temp_list; temp_list=temp_list->name_next){
186 temp_list->found=0;
187 }
188 }
190 /* for every mount entry */
191 for (me = mount_list; me; me = me->me_next) {
192 /* if there's a list of paths to select, the current mount
193 * entry matches in path or device name, get fs usage */
194 if (path_select_list &&
195 (walk_name_list (path_select_list, me->me_mountdir) ||
196 walk_name_list (path_select_list, me->me_devname) ) ) {
197 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
198 /* else if there's a list of paths/devices to select (but
199 * we didn't match above) skip to the next mount entry */
200 } else if (dev_select_list || path_select_list) {
201 continue;
202 /* skip remote filesystems if we're not interested in them */
203 } else if (me->me_remote && show_local_fs) {
204 continue;
205 /* skip pseudo fs's if we haven't asked for all fs's */
206 } else if (me->me_dummy && !show_all_fs) {
207 continue;
208 /* skip excluded fstypes */
209 } else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type)) {
210 continue;
211 /* skip excluded fs's */
212 } else if (dp_exclude_list &&
213 (walk_name_list (dp_exclude_list, me->me_devname) ||
214 walk_name_list (dp_exclude_list, me->me_mountdir))) {
215 continue;
216 /* otherwise, get fs usage */
217 } else {
218 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
219 }
221 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
222 usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
223 disk_result = check_disk (usp, (double)(fsp.fsu_bavail * fsp.fsu_blocksize / mult));
224 result = max_state (disk_result, result);
225 psize = fsp.fsu_blocks*fsp.fsu_blocksize/mult;
226 asprintf (&perf, "%s %s", perf,
227 perfdata ((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
228 psize-(fsp.fsu_bavail*fsp.fsu_blocksize/mult), units,
229 TRUE, min ((uintmax_t)psize-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*psize)),
230 TRUE, min ((uintmax_t)psize-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*psize)),
231 TRUE, 0,
232 TRUE, psize));
233 if (disk_result==STATE_OK && erronly && !verbose)
234 continue;
236 free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
237 free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
238 total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
239 if (disk_result!=STATE_OK || verbose>=0)
240 asprintf (&output, ("%s %s %.0f %s (%.0f%%);"),
241 output,
242 (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
243 free_space,
244 units,
245 free_space_pct);
246 asprintf (&details, _("%s\n\
247 %.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
248 details, free_space, total_space, units, free_space_pct,
249 me->me_devname, me->me_type, me->me_mountdir,
250 (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
251 }
253 }
255 asprintf (&output, "%s|%s", output, perf);
257 if (verbose > 2)
258 asprintf (&output, "%s%s", output, details);
260 /* Override result if paths specified and not found */
261 temp_list = path_select_list;
262 while (temp_list) {
263 if (!temp_list->found) {
264 asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
265 result = STATE_CRITICAL;
266 }
267 temp_list = temp_list->name_next;
268 }
270 printf ("DISK %s%s\n", state_text (result), output);
271 return result;
272 }
276 /* process command-line arguments */
277 int
278 process_arguments (int argc, char **argv)
279 {
280 int c;
281 struct name_list *se;
282 struct name_list **pathtail = &path_select_list;
283 struct name_list **fstail = &fs_exclude_list;
284 struct name_list **dptail = &dp_exclude_list;
285 struct name_list *temp_list;
286 int result = OK;
288 unsigned long l;
290 int option = 0;
291 static struct option longopts[] = {
292 {"timeout", required_argument, 0, 't'},
293 {"warning", required_argument, 0, 'w'},
294 {"critical", required_argument, 0, 'c'},
295 {"local", required_argument, 0, 'l'},
296 {"kilobytes", required_argument, 0, 'k'},
297 {"megabytes", required_argument, 0, 'm'},
298 {"units", required_argument, 0, 'u'},
299 {"path", required_argument, 0, 'p'},
300 {"partition", required_argument, 0, 'p'},
301 {"exclude_device", required_argument, 0, 'x'},
302 {"exclude-type", required_argument, 0, 'X'},
303 {"mountpoint", no_argument, 0, 'M'},
304 {"errors-only", no_argument, 0, 'e'},
305 {"verbose", no_argument, 0, 'v'},
306 {"quiet", no_argument, 0, 'q'},
307 {"clear", no_argument, 0, 'C'},
308 {"version", no_argument, 0, 'V'},
309 {"help", no_argument, 0, 'h'},
310 {0, 0, 0, 0}
311 };
313 if (argc < 2)
314 return ERROR;
316 se = (struct name_list *) malloc (sizeof (struct name_list));
317 se->name = strdup ("iso9660");
318 se->name_next = NULL;
319 se->found = 0;
320 se->found_len = 0;
321 *fstail = se;
322 fstail = &se->name_next;
324 for (c = 1; c < argc; c++)
325 if (strcmp ("-to", argv[c]) == 0)
326 strcpy (argv[c], "-t");
328 while (1) {
329 c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", longopts, &option);
331 if (c == -1 || c == EOF)
332 break;
334 switch (c) {
335 case 't': /* timeout period */
336 if (is_integer (optarg)) {
337 timeout_interval = atoi (optarg);
338 break;
339 }
340 else {
341 usage2 (_("Timeout interval must be a positive integer"), optarg);
342 }
343 case 'w': /* warning threshold */
344 if (is_intnonneg (optarg)) {
345 w_df = atoi (optarg);
346 break;
347 }
348 else if (strpbrk (optarg, ",:") &&
349 strstr (optarg, "%") &&
350 sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
351 w_df = (uintmax_t)l;
352 break;
353 }
354 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
355 break;
356 }
357 else {
358 usage4 (_("Warning threshold must be integer or percentage!"));
359 }
360 case 'c': /* critical threshold */
361 if (is_intnonneg (optarg)) {
362 c_df = atoi (optarg);
363 break;
364 }
365 else if (strpbrk (optarg, ",:") &&
366 strstr (optarg, "%") &&
367 sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) {
368 c_df = (uintmax_t)l;
369 break;
370 }
371 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
372 break;
373 }
374 else {
375 usage4 (_("Critical threshold must be integer or percentage!"));
376 }
377 case 'u':
378 if (units)
379 free(units);
380 if (! strcmp (optarg, "bytes")) {
381 mult = (uintmax_t)1;
382 units = strdup ("B");
383 } else if (! strcmp (optarg, "kB")) {
384 mult = (uintmax_t)1024;
385 units = strdup ("kB");
386 } else if (! strcmp (optarg, "MB")) {
387 mult = (uintmax_t)1024 * 1024;
388 units = strdup ("MB");
389 } else if (! strcmp (optarg, "GB")) {
390 mult = (uintmax_t)1024 * 1024 * 1024;
391 units = strdup ("GB");
392 } else if (! strcmp (optarg, "TB")) {
393 mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
394 units = strdup ("TB");
395 } else {
396 die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
397 }
398 if (units == NULL)
399 die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
400 break;
401 case 'k': /* display mountpoint */
402 mult = 1024;
403 if (units)
404 free(units);
405 units = strdup ("kB");
406 break;
407 case 'm': /* display mountpoint */
408 mult = 1024 * 1024;
409 if (units)
410 free(units);
411 units = strdup ("MB");
412 break;
413 case 'l':
414 show_local_fs = 1;
415 break;
416 case 'p': /* select path */
417 se = (struct name_list *) malloc (sizeof (struct name_list));
418 se->name = optarg;
419 se->name_next = NULL;
420 se->w_df = w_df;
421 se->c_df = c_df;
422 se->w_dfp = w_dfp;
423 se->c_dfp = c_dfp;
424 se->found = 0;
425 se->found_len = 0;
426 *pathtail = se;
427 pathtail = &se->name_next;
428 break;
429 case 'x': /* exclude path or partition */
430 se = (struct name_list *) malloc (sizeof (struct name_list));
431 se->name = optarg;
432 se->name_next = NULL;
433 se->w_df = 0;
434 se->c_df = 0;
435 se->w_dfp = -1.0;
436 se->c_dfp = -1.0;
437 se->found = 0;
438 se->found_len = 0;
439 *dptail = se;
440 dptail = &se->name_next;
441 break;
442 case 'X': /* exclude file system type */
443 se = (struct name_list *) malloc (sizeof (struct name_list));
444 se->name = optarg;
445 se->name_next = NULL;
446 se->w_df = 0;
447 se->c_df = 0;
448 se->w_dfp = -1.0;
449 se->c_dfp = -1.0;
450 se->found = 0;
451 se->found_len = 0;
452 *fstail = se;
453 fstail = &se->name_next;
454 break;
455 case 'v': /* verbose */
456 verbose++;
457 break;
458 case 'q': /* verbose */
459 verbose--;
460 break;
461 case 'e':
462 erronly = TRUE;
463 break;
464 case 'M': /* display mountpoint */
465 display_mntp = TRUE;
466 break;
467 case 'C':
468 w_df = 0;
469 c_df = 0;
470 w_dfp = -1.0;
471 c_dfp = -1.0;
472 break;
473 case 'V': /* version */
474 print_revision (progname, revision);
475 exit (STATE_OK);
476 case 'h': /* help */
477 print_help ();
478 exit (STATE_OK);
479 case '?': /* help */
480 usage2 (_("Unknown argument"), optarg);
481 }
482 }
484 /* Support for "check_disk warn crit [fs]" with thresholds at used level */
485 c = optind;
486 if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
487 w_dfp = (100.0 - atof (argv[c++]));
489 if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
490 c_dfp = (100.0 - atof (argv[c++]));
492 if (argc > c && path == NULL) {
493 se = (struct name_list *) malloc (sizeof (struct name_list));
494 se->name = strdup (argv[c++]);
495 se->name_next = NULL;
496 se->w_df = w_df;
497 se->c_df = c_df;
498 se->w_dfp = w_dfp;
499 se->c_dfp = c_dfp;
500 se->found =0;
501 se->found_len = 0;
502 *pathtail = se;
503 }
505 if (path_select_list) {
506 temp_list = path_select_list;
507 while (temp_list) {
508 if (validate_arguments (temp_list->w_df,
509 temp_list->c_df,
510 temp_list->w_dfp,
511 temp_list->c_dfp,
512 temp_list->name) == ERROR)
513 result = ERROR;
514 temp_list = temp_list->name_next;
515 }
516 return result;
517 } else {
518 return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
519 }
520 }
524 void
525 print_path (const char *mypath)
526 {
527 if (mypath == NULL)
528 printf ("\n");
529 else
530 printf (_(" for %s\n"), mypath);
532 return;
533 }
537 int
538 validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
539 {
540 if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) {
541 printf (_("INPUT ERROR: No thresholds specified"));
542 print_path (mypath);
543 return ERROR;
544 }
545 else if ((wp >= 0.0 || cp >= 0.0) &&
546 (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
547 printf (_("\
548 INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
549 cp, wp);
550 print_path (mypath);
551 return ERROR;
552 }
553 else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
554 printf (_("\
555 INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
556 (unsigned long)c, (unsigned long)w);
557 print_path (mypath);
558 return ERROR;
559 }
561 if (units == NULL) {
562 units = strdup ("MB");
563 mult = (uintmax_t)1024 * 1024;
564 }
565 return OK;
566 }
570 int
571 check_disk (double usp, double free_disk)
572 {
573 int result = STATE_UNKNOWN;
574 /* check the percent used space against thresholds */
575 if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp))
576 result = STATE_CRITICAL;
577 else if (c_df > 0 && free_disk <= c_df)
578 result = STATE_CRITICAL;
579 else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp))
580 result = STATE_WARNING;
581 else if (w_df > 0 && free_disk <= w_df)
582 result = STATE_WARNING;
583 else if (usp >= 0.0)
584 result = STATE_OK;
585 return result;
586 }
590 int
591 walk_name_list (struct name_list *list, const char *name)
592 {
593 int name_len;
594 name_len = strlen(name);
595 while (list) {
596 /* if the paths match up to the length of the mount path,
597 * AND if the mount path name is longer than the longest
598 * found match, we have a new winner */
599 if (name_len >= list->found_len &&
600 ! strncmp(list->name, name, name_len)) {
601 list->found = 1;
602 list->found_len = name_len;
603 /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
604 if (list->w_df) w_df = list->w_df;
605 if (list->c_df) c_df = list->c_df;
606 if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
607 if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
608 return TRUE;
609 }
610 list = list->name_next;
611 }
612 return FALSE;
613 }
617 void
618 print_help (void)
619 {
620 print_revision (progname, revision);
622 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
623 printf (COPYRIGHT, copyright, email);
625 printf (_("\
626 This plugin checks the amount of used disk space on a mounted file system\n\
627 and generates an alert if free space is less than one of the threshold values.\n\n"));
629 print_usage ();
631 printf (_(UT_HELP_VRSN));
633 printf (_("\
634 -w, --warning=INTEGER\n\
635 Exit with WARNING status if less than INTEGER --units of disk are free\n\
636 -w, --warning=PERCENT%%\n\
637 Exit with WARNING status if less than PERCENT of disk space is free\n\
638 -c, --critical=INTEGER\n\
639 Exit with CRITICAL status if less than INTEGER --units of disk are free\n\
640 -c, --critical=PERCENT%%\n\
641 Exit with CRITCAL status if less than PERCENT of disk space is free\n\
642 -C, --clear\n\
643 Clear thresholds\n"));
645 printf (_("\
646 -u, --units=STRING\n\
647 Choose bytes, kB, MB, GB, TB (default: MB)\n\
648 -k, --kilobytes\n\
649 Same as '--units kB'\n\
650 -m, --megabytes\n\
651 Same as '--units MB'\n"));
653 printf (_("\
654 -l, --local\n\
655 Only check local filesystems\n\
656 -p, --path=PATH, --partition=PARTITION\n\
657 Path or partition (may be repeated)\n\
658 -x, --exclude_device=PATH <STRING>\n\
659 Ignore device (only works if -p unspecified)\n\
660 -X, --exclude-type=TYPE <STRING>\n\
661 Ignore all filesystems of indicated type (may be repeated)\n\
662 -M, --mountpoint\n\
663 Display the mountpoint instead of the partition\n\
664 -e, --errors-only\n\
665 Display only devices/mountpoints with errors\n"));
667 printf (_(UT_WARN_CRIT));
669 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
671 printf (_(UT_VERBOSE));
673 printf ("%s", _("Examples:\n\
674 check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
675 Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
677 printf (_(UT_SUPPORT));
678 }
682 void
683 print_usage (void)
684 {
685 printf ("\
686 Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
687 [-v] [-q]\n", progname);
688 }