585c4a662b40e7c36fc8583bd626434074db25f8
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 *authors = "Nagios Plugin Development Team";
23 const char *email = "nagiosplug-devel@lists.sourceforge.net";
25 const char *summary = "\
26 This plugin checks the amount of used disk space on a mounted file system\n\
27 and generates an alert if free space is less than one of the threshold values.";
29 const char *option_summary = "\
30 -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
31 [-v] [-q]";
33 const char *options = "\
34 -w, --warning=INTEGER\n\
35 Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
36 -w, --warning=PERCENT%%\n\
37 Exit with WARNING status if less than PERCENT of disk space is free\n\
38 -c, --critical=INTEGER\n\
39 Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
40 -c, --critical=PERCENT%%\n\
41 Exit with CRITCAL status if less than PERCENT of disk space is free\n\
42 -u, --units=STRING\n\
43 Choose bytes, kB, MB, GB, TB (default: MB)\n\
44 -k, --kilobytes\n\
45 Same as '--units kB'\n\
46 -m, --megabytes\n\
47 Same as '--units MB'\n\
48 -l, --local\n\
49 Only check local filesystems\n\
50 -p, --path=PATH, --partition=PARTITION\n\
51 Path or partition (may be repeated)\n\
52 -x, --exclude_device=PATH <STRING>\n\
53 Ignore device (only works if -p unspecified)\n\
54 -X, --exclude-type=TYPE <STRING>\n\
55 Ignore all filesystems of indicated type (may be repeated)\n\
56 -M, --mountpoint\n\
57 Display the mountpoint instead of the partition\n\
58 -e, --errors-only\n\
59 Display only devices/mountpoints with errors\n\
60 -v, --verbose\n\
61 Show details for command-line debugging (do not use with nagios server)\n\
62 -h, --help\n\
63 Print detailed help screen\n\
64 -V, --version\n\
65 Print version information\n";
67 const char *notes = "\
68 \n";
70 #include "common.h"
71 #if HAVE_INTTYPES_H
72 # include <inttypes.h>
73 #endif
74 #include <assert.h>
75 #include "popen.h"
76 #include "utils.h"
77 #include <stdarg.h>
78 #include "../lib/fsusage.h"
79 #include "../lib/mountlist.h"
80 #if HAVE_LIMITS_H
81 # include <limits.h>
82 #endif
84 /* If nonzero, show inode information. */
85 static int inode_format;
87 /* If nonzero, show even filesystems with zero size or
88 uninteresting types. */
89 static int show_all_fs = 1;
91 /* If nonzero, show only local filesystems. */
92 static int show_local_fs = 0;
94 /* If positive, the units to use when printing sizes;
95 if negative, the human-readable base. */
96 static int output_block_size;
98 /* If nonzero, invoke the `sync' system call before getting any usage data.
99 Using this option can make df very slow, especially with many or very
100 busy disks. Note that this may make a difference on some systems --
101 SunOs4.1.3, for one. It is *not* necessary on Linux. */
102 static int require_sync = 0;
104 /* A filesystem type to display. */
106 struct name_list
107 {
108 char *name;
109 int found;
110 struct name_list *name_next;
111 };
113 /* Linked list of filesystem types to display.
114 If `fs_select_list' is NULL, list all types.
115 This table is generated dynamically from command-line options,
116 rather than hardcoding into the program what it thinks are the
117 valid filesystem types; let the user specify any filesystem type
118 they want to, and if there are any filesystems of that type, they
119 will be shown.
121 Some filesystem types:
122 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
124 static struct name_list *fs_select_list;
126 /* Linked list of filesystem types to omit.
127 If the list is empty, don't exclude any types. */
129 static struct name_list *fs_exclude_list;
131 static struct name_list *dp_exclude_list;
133 static struct name_list *path_select_list;
135 static struct name_list *dev_select_list;
137 /* Linked list of mounted filesystems. */
138 static struct mount_entry *mount_list;
140 /* For long options that have no equivalent short option, use a
141 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
142 enum
143 {
144 SYNC_OPTION = CHAR_MAX + 1,
145 NO_SYNC_OPTION,
146 BLOCK_SIZE_OPTION
147 };
149 #ifdef _AIX
150 #pragma alloca
151 #endif
153 int process_arguments (int, char **);
154 int validate_arguments (void);
155 int check_disk (int usp, int free_disk);
156 int walk_name_list (struct name_list *list, const char *name);
157 void print_help (void);
158 void print_usage (void);
160 int w_df = -1;
161 int c_df = -1;
162 float w_dfp = -1.0;
163 float c_dfp = -1.0;
164 char *path = "";
165 char *exclude_device = "";
166 char *units = "MB";
167 unsigned long mult = 1024 * 1024;
168 int verbose = 0;
169 int erronly = FALSE;
170 int display_mntp = FALSE;
172 /* Linked list of mounted filesystems. */
173 static struct mount_entry *mount_list;
175 int
176 main (int argc, char **argv)
177 {
178 int usp = -1;
179 int total_disk = -1;
180 int used_disk = -1;
181 int free_disk = -1;
182 int result = STATE_UNKNOWN;
183 int disk_result = STATE_UNKNOWN;
184 char *command_line = "";
185 char input_buffer[MAX_INPUT_BUFFER];
186 char file_system[MAX_INPUT_BUFFER];
187 char mntp[MAX_INPUT_BUFFER];
188 char *output = "";
189 char *details = "";
190 float free_space, free_space_pct, total_space;
192 struct mount_entry *me;
193 struct fs_usage fsp;
194 struct name_list *temp_list;
195 char *disk;
197 mount_list = read_filesystem_list (0);
199 if (process_arguments (argc, argv) != OK)
200 usage ("Could not parse arguments\n");
202 for (me = mount_list; me; me = me->me_next) {
204 if ((dev_select_list &&
205 walk_name_list (dev_select_list, me->me_devname)) ||
206 (path_select_list &&
207 walk_name_list (path_select_list, me->me_mountdir)))
208 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
209 else if (dev_select_list || path_select_list)
210 continue;
211 else if (me->me_remote && show_local_fs)
212 continue;
213 else if (me->me_dummy && !show_all_fs)
214 continue;
215 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
216 continue;
217 else if (dp_exclude_list &&
218 walk_name_list (dp_exclude_list, me->me_devname) ||
219 walk_name_list (dp_exclude_list, me->me_mountdir))
220 continue;
221 else
222 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
224 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
225 usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
226 disk_result = check_disk (usp, fsp.fsu_bavail);
227 result = max_state (disk_result, result);
228 if (disk_result==STATE_OK && erronly && !verbose)
229 continue;
231 free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
232 free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
233 total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
234 if (disk_result!=STATE_OK || verbose>=0)
235 asprintf (&output, "%s [%.0f %s (%2.0f%%) free on %s]",
236 output,
237 free_space,
238 units,
239 free_space_pct,
240 (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
241 asprintf (&details, "%s\n%.0f of %.0f %s (%2.0f%%) free on %s (type %s mounted on %s)",
242 details,
243 free_space,
244 total_space,
245 units,
246 free_space_pct,
247 me->me_devname,
248 me->me_type,
249 me->me_mountdir);
250 }
252 }
254 if (verbose > 2)
255 asprintf (&output, "%s%s", output, details);
257 /* Override result if paths specified and not found */
258 temp_list = path_select_list;
259 while (temp_list) {
260 if (temp_list->found != TRUE) {
261 asprintf (&output, "%s [%s not found]", output, temp_list->name);
262 result = STATE_CRITICAL;
263 }
264 temp_list = temp_list->name_next;
265 }
267 terminate (result, "DISK %s%s\n", state_text (result), output, details);
268 }
272 \f
273 /* process command-line arguments */
274 int
275 process_arguments (int argc, char **argv)
276 {
277 int c;
278 struct name_list *se;
279 struct name_list **pathtail = &path_select_list;
280 struct name_list **devtail = &dev_select_list;
281 struct name_list **fstail = &fs_exclude_list;
282 struct name_list **dptail = &dp_exclude_list;
284 int option_index = 0;
285 static struct option long_options[] = {
286 {"timeout", required_argument, 0, 't'},
287 {"warning", required_argument, 0, 'w'},
288 {"critical", required_argument, 0, 'c'},
289 {"local", required_argument, 0, 'l'},
290 {"kilobytes", required_argument, 0, 'k'},
291 {"megabytes", required_argument, 0, 'm'},
292 {"units", required_argument, 0, 'u'},
293 {"path", required_argument, 0, 'p'},
294 {"partition", required_argument, 0, 'p'},
295 {"device", required_argument, 0, 'd'},
296 {"exclude_device", required_argument, 0, 'x'},
297 {"exclude-type", required_argument, 0, 'X'},
298 {"mountpoint", no_argument, 0, 'M'},
299 {"errors-only", no_argument, 0, 'e'},
300 {"verbose", no_argument, 0, 'v'},
301 {"quiet", no_argument, 0, 'q'},
302 {"version", no_argument, 0, 'V'},
303 {"help", no_argument, 0, 'h'},
304 {0, 0, 0, 0}
305 };
307 if (argc < 2)
308 return ERROR;
310 se = (struct name_list *) malloc (sizeof (struct name_list));
311 se->name = strdup ("iso9660");
312 se->name_next = NULL;
313 *fstail = se;
314 fstail = &se->name_next;
316 for (c = 1; c < argc; c++)
317 if (strcmp ("-to", argv[c]) == 0)
318 strcpy (argv[c], "-t");
320 while (1) {
321 c = getopt_long (argc, argv, "+?Vqhvet:c:w:u:p:d:x:X:mklM", long_options, &option_index);
323 if (c == -1 || c == EOF)
324 break;
326 switch (c) {
327 case 't': /* timeout period */
328 if (is_integer (optarg)) {
329 timeout_interval = atoi (optarg);
330 break;
331 }
332 else {
333 usage ("Timeout Interval must be an integer!\n");
334 }
335 case 'w': /* warning time threshold */
336 if (is_intnonneg (optarg)) {
337 w_df = atoi (optarg);
338 break;
339 }
340 else if (strpbrk (optarg, ",:") &&
341 strstr (optarg, "%") &&
342 sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
343 break;
344 }
345 else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
346 break;
347 }
348 else {
349 usage ("Warning threshold must be integer or percentage!\n");
350 }
351 case 'c': /* critical time threshold */
352 if (is_intnonneg (optarg)) {
353 c_df = atoi (optarg);
354 break;
355 }
356 else if (strpbrk (optarg, ",:") &&
357 strstr (optarg, "%") &&
358 sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
359 break;
360 }
361 else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
362 break;
363 }
364 else {
365 usage ("Critical threshold must be integer or percentage!\n");
366 }
367 case 'u':
368 if (! strcmp (optarg, "bytes")) {
369 mult = 1;
370 units = "B";
371 } else if (! strcmp (optarg, "kB")) {
372 mult = 1024;
373 units = "kB";
374 } else if (! strcmp (optarg, "MB")) {
375 mult = 1024 * 1024;
376 units = "MB";
377 } else if (! strcmp (optarg, "GB")) {
378 mult = 1024 * 1024 * 1024;
379 units = "GB";
380 } else if (! strcmp (optarg, "TB")) {
381 mult = (unsigned long)1024 * 1024 * 1024 * 1024;
382 units = "TB";
383 } else {
384 terminate (STATE_UNKNOWN, "unit type %s not known\n", optarg);
385 }
386 break;
387 case 'k': /* display mountpoint */
388 mult = 1024;
389 units = "kB";
390 break;
391 case 'm': /* display mountpoint */
392 mult = 1024 * 1024;
393 units = "MB";
394 break;
395 case 'l':
396 show_local_fs = 1;
397 break;
398 case 'p': /* select path */
399 se = (struct name_list *) malloc (sizeof (struct name_list));
400 se->name = strdup (optarg);
401 se->name_next = NULL;
402 *pathtail = se;
403 pathtail = &se->name_next;
404 break;
405 case 'd': /* select partition/device */
406 se = (struct name_list *) malloc (sizeof (struct name_list));
407 se->name = strdup (optarg);
408 se->name_next = NULL;
409 *devtail = se;
410 devtail = &se->name_next;
411 break;
412 case 'x': /* exclude path or partition */
413 se = (struct name_list *) malloc (sizeof (struct name_list));
414 se->name = strdup (optarg);
415 se->name_next = NULL;
416 *dptail = se;
417 dptail = &se->name_next;
418 break;
419 break;
420 case 'X': /* exclude file system type */
421 se = (struct name_list *) malloc (sizeof (struct name_list));
422 se->name = strdup (optarg);
423 se->name_next = NULL;
424 *fstail = se;
425 fstail = &se->name_next;
426 break;
427 case 'v': /* verbose */
428 verbose++;
429 break;
430 case 'q': /* verbose */
431 verbose--;
432 break;
433 case 'e':
434 erronly = TRUE;
435 break;
436 case 'M': /* display mountpoint */
437 display_mntp = TRUE;
438 break;
439 case 'V': /* version */
440 print_revision (progname, revision);
441 exit (STATE_OK);
442 case 'h': /* help */
443 print_help ();
444 exit (STATE_OK);
445 case '?': /* help */
446 usage ("check_disk: unrecognized option\n");
447 break;
448 }
449 }
451 c = optind;
452 if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
453 w_dfp = (100.0 - atof (argv[c++]));
455 if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
456 c_dfp = (100.0 - atof (argv[c++]));
458 if (argc > c && strlen (path) == 0)
459 path = argv[c++];
461 return validate_arguments ();
462 }
466 int
467 validate_arguments ()
468 {
469 if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
470 printf ("INPUT ERROR: Unable to parse command line\n");
471 return ERROR;
472 }
473 else if ((w_dfp >= 0 || c_dfp >= 0)
474 && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
475 || c_dfp > w_dfp)) {
476 printf
477 ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
478 c_dfp, w_dfp);
479 return ERROR;
480 }
481 else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
482 printf
483 ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
484 c_df, w_df);
485 return ERROR;
486 }
487 else {
488 return OK;
489 }
490 }
494 \f
495 int
496 check_disk (int usp, int free_disk)
497 {
498 int result = STATE_UNKNOWN;
499 /* check the percent used space against thresholds */
500 if (usp >= 0 && usp >= (100.0 - c_dfp))
501 result = STATE_CRITICAL;
502 else if (c_df >= 0 && free_disk <= c_df)
503 result = STATE_CRITICAL;
504 else if (usp >= 0 && usp >= (100.0 - w_dfp))
505 result = STATE_WARNING;
506 else if (w_df >= 0 && free_disk <= w_df)
507 result = STATE_WARNING;
508 else if (usp >= 0.0)
509 result = STATE_OK;
510 return result;
511 }
515 int
516 walk_name_list (struct name_list *list, const char *name)
517 {
518 while (list) {
519 if (! strcmp(list->name, name)) {
520 list->found = 1;
521 return TRUE;
522 }
523 list = list->name_next;
524 }
525 return FALSE;
526 }
530 \f
531 void
532 print_help (void)
533 {
534 print_revision (progname, revision);
535 printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
536 copyright, authors, email, summary);
537 print_usage ();
538 printf ("\nOptions:\n");
539 printf (options);
540 printf (notes);
541 support ();
542 }
544 void
545 print_usage (void)
546 {
547 printf
548 ("Usage: %s %s\n"
549 " %s (-h|--help)\n"
550 " %s (-V|--version)\n", progname, option_summary, progname, progname);
551 }