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