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