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 uintmax_t w_df;
68 uintmax_t c_df;
69 double w_dfp;
70 double c_dfp;
71 struct name_list *name_next;
72 };
74 /* Linked list of filesystem types to display.
75 If `fs_select_list' is NULL, list all types.
76 This table is generated dynamically from command-line options,
77 rather than hardcoding into the program what it thinks are the
78 valid filesystem types; let the user specify any filesystem type
79 they want to, and if there are any filesystems of that type, they
80 will be shown.
82 Some filesystem types:
83 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
85 /* static struct name_list *fs_select_list; */
87 /* Linked list of filesystem types to omit.
88 If the list is empty, don't exclude any types. */
90 static struct name_list *fs_exclude_list;
92 static struct name_list *dp_exclude_list;
94 static struct name_list *path_select_list;
96 static struct name_list *dev_select_list;
98 /* Linked list of mounted filesystems. */
99 static struct mount_entry *mount_list;
101 /* For long options that have no equivalent short option, use a
102 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
103 enum
104 {
105 SYNC_OPTION = CHAR_MAX + 1,
106 NO_SYNC_OPTION,
107 BLOCK_SIZE_OPTION
108 };
110 #ifdef _AIX
111 #pragma alloca
112 #endif
114 int process_arguments (int, char **);
115 void print_path (const char *mypath);
116 int validate_arguments (uintmax_t, uintmax_t, double, double, char *);
117 int check_disk (double usp, double free_disk);
118 int walk_name_list (struct name_list *list, const char *name);
119 void print_help (void);
120 void print_usage (void);
122 uintmax_t w_df = 0;
123 uintmax_t c_df = 0;
124 double w_dfp = -1.0;
125 double c_dfp = -1.0;
126 char *path;
127 char *exclude_device;
128 char *units;
129 uintmax_t mult = 1024 * 1024;
130 int verbose = 0;
131 int erronly = FALSE;
132 int display_mntp = FALSE;
134 /* Linked list of mounted filesystems. */
135 static struct mount_entry *mount_list;
139 int
140 main (int argc, char **argv)
141 {
142 double usp = -1.0;
143 int result = STATE_UNKNOWN;
144 int disk_result = STATE_UNKNOWN;
145 char file_system[MAX_INPUT_BUFFER];
146 char *output;
147 char *details;
148 char *perf;
149 uintmax_t psize;
150 float free_space, free_space_pct, total_space;
152 struct mount_entry *me;
153 struct fs_usage fsp;
154 struct name_list *temp_list;
156 output = strdup (" - free space:");
157 details = strdup ("");
158 perf = strdup ("");
160 setlocale (LC_ALL, "");
161 bindtextdomain (PACKAGE, LOCALEDIR);
162 textdomain (PACKAGE);
164 mount_list = read_filesystem_list (0);
166 if (process_arguments (argc, argv) == ERROR)
167 usage4 (_("Could not parse arguments"));
169 for (me = mount_list; me; me = me->me_next) {
171 if (path_select_list &&
172 (walk_name_list (path_select_list, me->me_mountdir) ||
173 walk_name_list (path_select_list, me->me_devname) ) )
174 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
175 else if (dev_select_list || path_select_list)
176 continue;
177 else if (me->me_remote && show_local_fs)
178 continue;
179 else if (me->me_dummy && !show_all_fs)
180 continue;
181 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
182 continue;
183 else if (dp_exclude_list &&
184 (walk_name_list (dp_exclude_list, me->me_devname) ||
185 walk_name_list (dp_exclude_list, me->me_mountdir)))
186 continue;
187 else
188 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
190 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
191 usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
192 disk_result = check_disk (usp, (double)(fsp.fsu_bavail * fsp.fsu_blocksize / mult));
193 result = max_state (disk_result, result);
194 psize = fsp.fsu_blocks*fsp.fsu_blocksize/mult;
195 asprintf (&perf, "%s %s", perf,
196 perfdata ((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
197 psize-(fsp.fsu_bavail*fsp.fsu_blocksize/mult), units,
198 TRUE, min ((uintmax_t)psize-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*psize)),
199 TRUE, min ((uintmax_t)psize-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*psize)),
200 TRUE, 0,
201 TRUE, psize));
202 if (disk_result==STATE_OK && erronly && !verbose)
203 continue;
205 free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
206 free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
207 total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
208 if (disk_result!=STATE_OK || verbose>=0)
209 asprintf (&output, ("%s %s %.0f %s (%.0f%%);"),
210 output,
211 (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
212 free_space,
213 units,
214 free_space_pct);
215 asprintf (&details, _("%s\n\
216 %.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
217 details, free_space, total_space, units, free_space_pct,
218 me->me_devname, me->me_type, me->me_mountdir,
219 (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
220 }
222 }
224 asprintf (&output, "%s|%s", output, perf);
226 if (verbose > 2)
227 asprintf (&output, "%s%s", output, details);
229 /* Override result if paths specified and not found */
230 temp_list = path_select_list;
231 while (temp_list) {
232 if (temp_list->found != TRUE) {
233 asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
234 result = STATE_CRITICAL;
235 }
236 temp_list = temp_list->name_next;
237 }
239 printf ("DISK %s%s\n", state_text (result), output);
240 return result;
241 }
245 /* process command-line arguments */
246 int
247 process_arguments (int argc, char **argv)
248 {
249 int c;
250 struct name_list *se;
251 struct name_list **pathtail = &path_select_list;
252 struct name_list **fstail = &fs_exclude_list;
253 struct name_list **dptail = &dp_exclude_list;
254 struct name_list *temp_list;
255 int result = OK;
257 unsigned long l;
259 int option = 0;
260 static struct option longopts[] = {
261 {"timeout", required_argument, 0, 't'},
262 {"warning", required_argument, 0, 'w'},
263 {"critical", required_argument, 0, 'c'},
264 {"local", required_argument, 0, 'l'},
265 {"kilobytes", required_argument, 0, 'k'},
266 {"megabytes", required_argument, 0, 'm'},
267 {"units", required_argument, 0, 'u'},
268 {"path", required_argument, 0, 'p'},
269 {"partition", required_argument, 0, 'p'},
270 {"exclude_device", required_argument, 0, 'x'},
271 {"exclude-type", required_argument, 0, 'X'},
272 {"mountpoint", no_argument, 0, 'M'},
273 {"errors-only", no_argument, 0, 'e'},
274 {"verbose", no_argument, 0, 'v'},
275 {"quiet", no_argument, 0, 'q'},
276 {"clear", no_argument, 0, 'C'},
277 {"version", no_argument, 0, 'V'},
278 {"help", no_argument, 0, 'h'},
279 {0, 0, 0, 0}
280 };
282 if (argc < 2)
283 return ERROR;
285 se = (struct name_list *) malloc (sizeof (struct name_list));
286 se->name = strdup ("iso9660");
287 se->name_next = NULL;
288 *fstail = se;
289 fstail = &se->name_next;
291 for (c = 1; c < argc; c++)
292 if (strcmp ("-to", argv[c]) == 0)
293 strcpy (argv[c], "-t");
295 while (1) {
296 c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", longopts, &option);
298 if (c == -1 || c == EOF)
299 break;
301 switch (c) {
302 case 't': /* timeout period */
303 if (is_integer (optarg)) {
304 timeout_interval = atoi (optarg);
305 break;
306 }
307 else {
308 usage2 (_("Timeout interval must be a positive integer"), optarg);
309 }
310 case 'w': /* warning threshold */
311 if (is_intnonneg (optarg)) {
312 w_df = atoi (optarg);
313 break;
314 }
315 else if (strpbrk (optarg, ",:") &&
316 strstr (optarg, "%") &&
317 sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
318 w_df = (uintmax_t)l;
319 break;
320 }
321 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
322 break;
323 }
324 else {
325 usage4 (_("Warning threshold must be integer or percentage!"));
326 }
327 case 'c': /* critical threshold */
328 if (is_intnonneg (optarg)) {
329 c_df = atoi (optarg);
330 break;
331 }
332 else if (strpbrk (optarg, ",:") &&
333 strstr (optarg, "%") &&
334 sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) {
335 c_df = (uintmax_t)l;
336 break;
337 }
338 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
339 break;
340 }
341 else {
342 usage4 (_("Critical threshold must be integer or percentage!"));
343 }
344 case 'u':
345 if (units)
346 free(units);
347 if (! strcmp (optarg, "bytes")) {
348 mult = (uintmax_t)1;
349 units = strdup ("B");
350 } else if (! strcmp (optarg, "kB")) {
351 mult = (uintmax_t)1024;
352 units = strdup ("kB");
353 } else if (! strcmp (optarg, "MB")) {
354 mult = (uintmax_t)1024 * 1024;
355 units = strdup ("MB");
356 } else if (! strcmp (optarg, "GB")) {
357 mult = (uintmax_t)1024 * 1024 * 1024;
358 units = strdup ("GB");
359 } else if (! strcmp (optarg, "TB")) {
360 mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
361 units = strdup ("TB");
362 } else {
363 die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
364 }
365 if (units == NULL)
366 die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
367 break;
368 case 'k': /* display mountpoint */
369 mult = 1024;
370 if (units)
371 free(units);
372 units = strdup ("kB");
373 break;
374 case 'm': /* display mountpoint */
375 mult = 1024 * 1024;
376 if (units)
377 free(units);
378 units = strdup ("MB");
379 break;
380 case 'l':
381 show_local_fs = 1;
382 break;
383 case 'p': /* select path */
384 se = (struct name_list *) malloc (sizeof (struct name_list));
385 se->name = optarg;
386 se->name_next = NULL;
387 se->w_df = w_df;
388 se->c_df = c_df;
389 se->w_dfp = w_dfp;
390 se->c_dfp = c_dfp;
391 *pathtail = se;
392 pathtail = &se->name_next;
393 break;
394 case 'x': /* exclude path or partition */
395 se = (struct name_list *) malloc (sizeof (struct name_list));
396 se->name = optarg;
397 se->name_next = NULL;
398 se->w_df = 0;
399 se->c_df = 0;
400 se->w_dfp = -1.0;
401 se->c_dfp = -1.0;
402 *dptail = se;
403 dptail = &se->name_next;
404 break;
405 case 'X': /* exclude file system type */
406 se = (struct name_list *) malloc (sizeof (struct name_list));
407 se->name = optarg;
408 se->name_next = NULL;
409 se->w_df = 0;
410 se->c_df = 0;
411 se->w_dfp = -1.0;
412 se->c_dfp = -1.0;
413 *fstail = se;
414 fstail = &se->name_next;
415 break;
416 case 'v': /* verbose */
417 verbose++;
418 break;
419 case 'q': /* verbose */
420 verbose--;
421 break;
422 case 'e':
423 erronly = TRUE;
424 break;
425 case 'M': /* display mountpoint */
426 display_mntp = TRUE;
427 break;
428 case 'C':
429 w_df = 0;
430 c_df = 0;
431 w_dfp = -1.0;
432 c_dfp = -1.0;
433 break;
434 case 'V': /* version */
435 print_revision (progname, revision);
436 exit (STATE_OK);
437 case 'h': /* help */
438 print_help ();
439 exit (STATE_OK);
440 case '?': /* help */
441 usage2 (_("Unknown argument"), optarg);
442 }
443 }
445 /* Support for "check_disk warn crit [fs]" with thresholds at used level */
446 c = optind;
447 if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
448 w_dfp = (100.0 - atof (argv[c++]));
450 if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
451 c_dfp = (100.0 - atof (argv[c++]));
453 if (argc > c && path == NULL) {
454 se = (struct name_list *) malloc (sizeof (struct name_list));
455 se->name = strdup (argv[c++]);
456 se->name_next = NULL;
457 se->w_df = w_df;
458 se->c_df = c_df;
459 se->w_dfp = w_dfp;
460 se->c_dfp = c_dfp;
461 *pathtail = se;
462 }
464 if (path_select_list) {
465 temp_list = path_select_list;
466 while (temp_list) {
467 if (validate_arguments (temp_list->w_df,
468 temp_list->c_df,
469 temp_list->w_dfp,
470 temp_list->c_dfp,
471 temp_list->name) == ERROR)
472 result = ERROR;
473 temp_list = temp_list->name_next;
474 }
475 return result;
476 } else {
477 return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
478 }
479 }
483 void
484 print_path (const char *mypath)
485 {
486 if (mypath == NULL)
487 printf ("\n");
488 else
489 printf (_(" for %s\n"), mypath);
491 return;
492 }
496 int
497 validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
498 {
499 if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) {
500 printf (_("INPUT ERROR: No thresholds specified"));
501 print_path (mypath);
502 return ERROR;
503 }
504 else if ((wp >= 0.0 || cp >= 0.0) &&
505 (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
506 printf (_("\
507 INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
508 cp, wp);
509 print_path (mypath);
510 return ERROR;
511 }
512 else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
513 printf (_("\
514 INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
515 (unsigned long)c, (unsigned long)w);
516 print_path (mypath);
517 return ERROR;
518 }
520 if (units == NULL) {
521 units = strdup ("MB");
522 mult = (uintmax_t)1024 * 1024;
523 }
524 return OK;
525 }
529 int
530 check_disk (double usp, double free_disk)
531 {
532 int result = STATE_UNKNOWN;
533 /* check the percent used space against thresholds */
534 if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp))
535 result = STATE_CRITICAL;
536 else if (c_df > 0 && free_disk <= c_df)
537 result = STATE_CRITICAL;
538 else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp))
539 result = STATE_WARNING;
540 else if (w_df > 0 && free_disk <= w_df)
541 result = STATE_WARNING;
542 else if (usp >= 0.0)
543 result = STATE_OK;
544 return result;
545 }
549 int
550 walk_name_list (struct name_list *list, const char *name)
551 {
552 while (list) {
553 if (! strcmp(list->name, name)) {
554 list->found = 1;
555 /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
556 if (list->w_df) w_df = list->w_df;
557 if (list->c_df) c_df = list->c_df;
558 if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
559 if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
560 return TRUE;
561 }
562 list = list->name_next;
563 }
564 return FALSE;
565 }
569 void
570 print_help (void)
571 {
572 print_revision (progname, revision);
574 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
575 printf (COPYRIGHT, copyright, email);
577 printf (_("\
578 This plugin checks the amount of used disk space on a mounted file system\n\
579 and generates an alert if free space is less than one of the threshold values.\n\n"));
581 print_usage ();
583 printf (_(UT_HELP_VRSN));
585 printf (_("\
586 -w, --warning=INTEGER\n\
587 Exit with WARNING status if less than INTEGER --units of disk are free\n\
588 -w, --warning=PERCENT%%\n\
589 Exit with WARNING status if less than PERCENT of disk space is free\n\
590 -c, --critical=INTEGER\n\
591 Exit with CRITICAL status if less than INTEGER --units of disk are free\n\
592 -c, --critical=PERCENT%%\n\
593 Exit with CRITCAL status if less than PERCENT of disk space is free\n\
594 -C, --clear\n\
595 Clear thresholds\n"));
597 printf (_("\
598 -u, --units=STRING\n\
599 Choose bytes, kB, MB, GB, TB (default: MB)\n\
600 -k, --kilobytes\n\
601 Same as '--units kB'\n\
602 -m, --megabytes\n\
603 Same as '--units MB'\n"));
605 printf (_("\
606 -l, --local\n\
607 Only check local filesystems\n\
608 -p, --path=PATH, --partition=PARTITION\n\
609 Path or partition (may be repeated)\n\
610 -x, --exclude_device=PATH <STRING>\n\
611 Ignore device (only works if -p unspecified)\n\
612 -X, --exclude-type=TYPE <STRING>\n\
613 Ignore all filesystems of indicated type (may be repeated)\n\
614 -M, --mountpoint\n\
615 Display the mountpoint instead of the partition\n\
616 -e, --errors-only\n\
617 Display only devices/mountpoints with errors\n"));
619 printf (_(UT_WARN_CRIT));
621 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
623 printf (_(UT_VERBOSE));
625 printf ("%s", _("Examples:\n\
626 check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
627 Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
629 printf (_(UT_SUPPORT));
630 }
634 void
635 print_usage (void)
636 {
637 printf ("\
638 Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
639 [-v] [-q]\n", progname);
640 }