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 char *perf;
146 uintmax_t psize;
147 float free_space, free_space_pct, total_space;
149 struct mount_entry *me;
150 struct fs_usage fsp;
151 struct name_list *temp_list;
153 output = strdup (" - free space:");
154 details = strdup ("");
155 perf = strdup ("");
157 setlocale (LC_ALL, "");
158 bindtextdomain (PACKAGE, LOCALEDIR);
159 textdomain (PACKAGE);
161 mount_list = read_filesystem_list (0);
163 if (process_arguments (argc, argv) != OK)
164 usage (_("Could not parse arguments\n"));
166 for (me = mount_list; me; me = me->me_next) {
168 if (path_select_list &&
169 (walk_name_list (path_select_list, me->me_mountdir) ||
170 walk_name_list (path_select_list, me->me_devname) ) )
171 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
172 else if (dev_select_list || path_select_list)
173 continue;
174 else if (me->me_remote && show_local_fs)
175 continue;
176 else if (me->me_dummy && !show_all_fs)
177 continue;
178 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
179 continue;
180 else if (dp_exclude_list &&
181 (walk_name_list (dp_exclude_list, me->me_devname) ||
182 walk_name_list (dp_exclude_list, me->me_mountdir)))
183 continue;
184 else
185 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
187 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
188 usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
189 disk_result = check_disk (usp, fsp.fsu_bavail);
190 result = max_state (disk_result, result);
191 psize = fsp.fsu_blocks*fsp.fsu_blocksize/mult;
192 asprintf (&perf, "%s %s", perf,
193 perfdata ((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
194 fsp.fsu_bavail*fsp.fsu_blocksize/mult, units,
195 TRUE, min ((uintmax_t)psize-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*psize)),
196 TRUE, min ((uintmax_t)psize-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*psize)),
197 TRUE, 0,
198 TRUE, psize));
199 if (disk_result==STATE_OK && erronly && !verbose)
200 continue;
202 free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
203 free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
204 total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
205 if (disk_result!=STATE_OK || verbose>=0)
206 asprintf (&output, ("%s %s %.0f %s (%.0f%%);"),
207 output,
208 (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
209 free_space,
210 units,
211 free_space_pct);
212 asprintf (&details, _("%s\n\
213 %.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
214 details, free_space, total_space, units, free_space_pct,
215 me->me_devname, me->me_type, me->me_mountdir,
216 (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
217 }
219 }
221 asprintf (&output, "%s|%s", output, perf);
223 if (verbose > 2)
224 asprintf (&output, "%s%s", output, details);
226 /* Override result if paths specified and not found */
227 temp_list = path_select_list;
228 while (temp_list) {
229 if (temp_list->found != TRUE) {
230 asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
231 result = STATE_CRITICAL;
232 }
233 temp_list = temp_list->name_next;
234 }
236 printf ("DISK %s%s\n", state_text (result), output);
237 return result;
238 }
242 \f
243 /* process command-line arguments */
244 int
245 process_arguments (int argc, char **argv)
246 {
247 int c;
248 struct name_list *se;
249 struct name_list **pathtail = &path_select_list;
250 struct name_list **fstail = &fs_exclude_list;
251 struct name_list **dptail = &dp_exclude_list;
252 struct name_list *temp_list;
253 int result = OK;
255 unsigned long l;
257 int option = 0;
258 static struct option longopts[] = {
259 {"timeout", required_argument, 0, 't'},
260 {"warning", required_argument, 0, 'w'},
261 {"critical", required_argument, 0, 'c'},
262 {"local", required_argument, 0, 'l'},
263 {"kilobytes", required_argument, 0, 'k'},
264 {"megabytes", required_argument, 0, 'm'},
265 {"units", required_argument, 0, 'u'},
266 {"path", required_argument, 0, 'p'},
267 {"partition", required_argument, 0, 'p'},
268 {"exclude_device", required_argument, 0, 'x'},
269 {"exclude-type", required_argument, 0, 'X'},
270 {"mountpoint", no_argument, 0, 'M'},
271 {"errors-only", no_argument, 0, 'e'},
272 {"verbose", no_argument, 0, 'v'},
273 {"quiet", no_argument, 0, 'q'},
274 {"clear", no_argument, 0, 'C'},
275 {"version", no_argument, 0, 'V'},
276 {"help", no_argument, 0, 'h'},
277 {0, 0, 0, 0}
278 };
280 if (argc < 2)
281 return ERROR;
283 se = (struct name_list *) malloc (sizeof (struct name_list));
284 se->name = strdup ("iso9660");
285 se->name_next = NULL;
286 *fstail = se;
287 fstail = &se->name_next;
289 for (c = 1; c < argc; c++)
290 if (strcmp ("-to", argv[c]) == 0)
291 strcpy (argv[c], "-t");
293 while (1) {
294 c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", longopts, &option);
296 if (c == -1 || c == EOF)
297 break;
299 switch (c) {
300 case 't': /* timeout period */
301 if (is_integer (optarg)) {
302 timeout_interval = atoi (optarg);
303 break;
304 }
305 else {
306 usage (_("Timeout Interval must be an integer!\n"));
307 }
308 case 'w': /* warning threshold */
309 if (is_intnonneg (optarg)) {
310 w_df = atoi (optarg);
311 break;
312 }
313 else if (strpbrk (optarg, ",:") &&
314 strstr (optarg, "%") &&
315 sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
316 w_df = (uintmax_t)l;
317 break;
318 }
319 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
320 break;
321 }
322 else {
323 usage (_("Warning threshold must be integer or percentage!\n"));
324 }
325 case 'c': /* critical threshold */
326 if (is_intnonneg (optarg)) {
327 c_df = atoi (optarg);
328 break;
329 }
330 else if (strpbrk (optarg, ",:") &&
331 strstr (optarg, "%") &&
332 sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) {
333 c_df = (uintmax_t)l;
334 break;
335 }
336 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
337 break;
338 }
339 else {
340 usage (_("Critical threshold must be integer or percentage!\n"));
341 }
342 case 'u':
343 if (units)
344 free(units);
345 if (! strcmp (optarg, "bytes")) {
346 mult = (uintmax_t)1;
347 units = strdup ("B");
348 } else if (! strcmp (optarg, "kB")) {
349 mult = (uintmax_t)1024;
350 units = strdup ("kB");
351 } else if (! strcmp (optarg, "MB")) {
352 mult = (uintmax_t)1024 * 1024;
353 units = strdup ("MB");
354 } else if (! strcmp (optarg, "GB")) {
355 mult = (uintmax_t)1024 * 1024 * 1024;
356 units = strdup ("GB");
357 } else if (! strcmp (optarg, "TB")) {
358 mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
359 units = strdup ("TB");
360 } else {
361 die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
362 }
363 if (units == NULL)
364 die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
365 break;
366 case 'k': /* display mountpoint */
367 mult = 1024;
368 if (units)
369 free(units);
370 units = strdup ("kB");
371 break;
372 case 'm': /* display mountpoint */
373 mult = 1024 * 1024;
374 if (units)
375 free(units);
376 units = strdup ("kB");
377 break;
378 case 'l':
379 show_local_fs = 1;
380 break;
381 case 'p': /* select path */
382 se = (struct name_list *) malloc (sizeof (struct name_list));
383 se->name = optarg;
384 se->name_next = NULL;
385 se->w_df = w_df;
386 se->c_df = c_df;
387 se->w_dfp = w_dfp;
388 se->c_dfp = c_dfp;
389 *pathtail = se;
390 pathtail = &se->name_next;
391 break;
392 case 'x': /* exclude path or partition */
393 se = (struct name_list *) malloc (sizeof (struct name_list));
394 se->name = optarg;
395 se->name_next = NULL;
396 *dptail = se;
397 dptail = &se->name_next;
398 break;
399 case 'X': /* exclude file system type */
400 se = (struct name_list *) malloc (sizeof (struct name_list));
401 se->name = optarg;
402 se->name_next = NULL;
403 *fstail = se;
404 fstail = &se->name_next;
405 break;
406 case 'v': /* verbose */
407 verbose++;
408 break;
409 case 'q': /* verbose */
410 verbose--;
411 break;
412 case 'e':
413 erronly = TRUE;
414 break;
415 case 'M': /* display mountpoint */
416 display_mntp = TRUE;
417 break;
418 case 'C':
419 w_df = 0;
420 c_df = 0;
421 w_dfp = -1.0;
422 c_dfp = -1.0;
423 break;
424 case 'V': /* version */
425 print_revision (progname, revision);
426 exit (STATE_OK);
427 case 'h': /* help */
428 print_help ();
429 exit (STATE_OK);
430 case '?': /* help */
431 usage (_("check_disk: unrecognized option\n"));
432 break;
433 }
434 }
436 /* Support for "check_disk warn crit [fs]" with thresholds at used level */
437 c = optind;
438 if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
439 w_dfp = (100.0 - atof (argv[c++]));
441 if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
442 c_dfp = (100.0 - atof (argv[c++]));
444 if (argc > c && path == NULL) {
445 se = (struct name_list *) malloc (sizeof (struct name_list));
446 se->name = strdup (argv[c++]);
447 se->name_next = NULL;
448 se->w_df = w_df;
449 se->c_df = c_df;
450 se->w_dfp = w_dfp;
451 se->c_dfp = c_dfp;
452 *pathtail = se;
453 }
455 if (path_select_list) {
456 temp_list = path_select_list;
457 while (temp_list) {
458 if (validate_arguments (temp_list->w_df,
459 temp_list->c_df,
460 temp_list->w_dfp,
461 temp_list->c_dfp,
462 temp_list->name) == ERROR)
463 result = ERROR;
464 temp_list = temp_list->name_next;
465 }
466 return result;
467 } else {
468 return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
469 }
470 }
473 void
474 print_path (const char *mypath)
475 {
476 if (mypath == NULL)
477 printf ("\n");
478 else
479 printf (" for %s\n", mypath);
481 return;
482 }
484 int
485 validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
486 {
487 if (w == 0 && c == 0 && wp < 0.0 && cp < 0.0) {
488 printf (_("INPUT ERROR: No thresholds specified"));
489 print_path (mypath);
490 return ERROR;
491 }
492 else if ((wp >= 0.0 || cp >= 0.0) &&
493 (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
494 printf (_("\
495 INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
496 cp, wp);
497 print_path (mypath);
498 return ERROR;
499 }
500 else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
501 printf (_("\
502 INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
503 (unsigned long)c, (unsigned long)w);
504 print_path (mypath);
505 return ERROR;
506 }
508 if (units == NULL) {
509 units = strdup ("MB");
510 mult = (uintmax_t)1024 * 1024;
511 }
512 return OK;
513 }
517 \f
518 int
519 check_disk (double usp, uintmax_t free_disk)
520 {
521 int result = STATE_UNKNOWN;
522 /* check the percent used space against thresholds */
523 if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp))
524 result = STATE_CRITICAL;
525 else if (c_df > 0 && free_disk <= c_df)
526 result = STATE_CRITICAL;
527 else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp))
528 result = STATE_WARNING;
529 else if (w_df > 0 && free_disk <= w_df)
530 result = STATE_WARNING;
531 else if (usp >= 0.0)
532 result = STATE_OK;
533 return result;
534 }
538 int
539 walk_name_list (struct name_list *list, const char *name)
540 {
541 while (list) {
542 if (! strcmp(list->name, name)) {
543 list->found = 1;
544 /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
545 if (list->w_df) w_df = list->w_df;
546 if (list->c_df) c_df = list->c_df;
547 if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
548 if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
549 return TRUE;
550 }
551 list = list->name_next;
552 }
553 return FALSE;
554 }
560 \f
561 void
562 print_help (void)
563 {
564 print_revision (progname, revision);
566 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
567 printf (_(COPYRIGHT), copyright, email);
569 printf (_("\
570 This plugin checks the amount of used disk space on a mounted file system\n\
571 and generates an alert if free space is less than one of the threshold values."));
573 print_usage ();
575 printf (_(UT_HELP_VRSN));
577 printf (_("\
578 -w, --warning=INTEGER\n\
579 Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
580 -w, --warning=PERCENT%%\n\
581 Exit with WARNING status if less than PERCENT of disk space is free\n\
582 -c, --critical=INTEGER\n\
583 Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
584 -c, --critical=PERCENT%%\n\
585 Exit with CRITCAL status if less than PERCENT of disk space is free\n\
586 -C, --clear\n\
587 Clear thresholds\n"));
589 printf (_("\
590 -u, --units=STRING\n\
591 Choose bytes, kB, MB, GB, TB (default: MB)\n\
592 -k, --kilobytes\n\
593 Same as '--units kB'\n\
594 -m, --megabytes\n\
595 Same as '--units MB'\n"));
597 printf (_("\
598 -l, --local\n\
599 Only check local filesystems\n\
600 -p, --path=PATH, --partition=PARTITION\n\
601 Path or partition (may be repeated)\n\
602 -x, --exclude_device=PATH <STRING>\n\
603 Ignore device (only works if -p unspecified)\n\
604 -X, --exclude-type=TYPE <STRING>\n\
605 Ignore all filesystems of indicated type (may be repeated)\n\
606 -M, --mountpoint\n\
607 Display the mountpoint instead of the partition\n\
608 -e, --errors-only\n\
609 Display only devices/mountpoints with errors\n"));
611 printf (_(UT_WARN_CRIT));
613 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
615 printf (_(UT_VERBOSE));
617 printf ("%s", _("Examples:\n\
618 check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
619 Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
621 printf (_(UT_SUPPORT));
622 }
627 void
628 print_usage (void)
629 {
630 printf (_("\
631 Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
632 [-v] [-q]\n\
633 %s (-h|--help)\n\
634 %s (-V|--version)\n"),
635 progname, progname, progname);
636 }