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