summaryrefslogtreecommitdiffstats
path: root/plugins/check_disk.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_disk.c')
-rw-r--r--plugins/check_disk.c226
1 files changed, 119 insertions, 107 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index b0b0d2a5..7feb5a57 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -35,80 +35,6 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
35# include <limits.h> 35# include <limits.h>
36#endif 36#endif
37 37
38void
39print_usage (void)
40{
41 printf (_("\
42Usage: %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}
48
49void
50print_help (void)
51{
52 print_revision (progname, revision);
53
54 printf (_(COPYRIGHT), copyright, email);
55
56 printf (_("\
57This plugin checks the amount of used disk space on a mounted file system\n\
58and generates an alert if free space is less than one of the threshold values."));
59
60 print_usage ();
61
62 printf (_(UT_HELP_VRSN));
63
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"));
75
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"));
83
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"));
97
98 printf (_(UT_WARN_CRIT));
99
100 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
101
102 printf (_(UT_VERBOSE));
103
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"));
107
108 support ();
109}
110
111
112/* If nonzero, show inode information. */ 38/* If nonzero, show inode information. */
113/* static int inode_format; */ 39/* static int inode_format; */
114 40
@@ -135,8 +61,8 @@ struct name_list
135{ 61{
136 char *name; 62 char *name;
137 int found; 63 int found;
138 int w_df; 64 uintmax_t w_df;
139 int c_df; 65 uintmax_t c_df;
140 double w_dfp; 66 double w_dfp;
141 double c_dfp; 67 double c_dfp;
142 struct name_list *name_next; 68 struct name_list *name_next;
@@ -183,18 +109,21 @@ enum
183#endif 109#endif
184 110
185int process_arguments (int, char **); 111int process_arguments (int, char **);
186int validate_arguments (int, int, double, double, char *); 112void print_path (char *mypath);
187int check_disk (int usp, int free_disk); 113int validate_arguments (uintmax_t, uintmax_t, double, double, char *);
114int check_disk (int usp, uintmax_t free_disk);
188int walk_name_list (struct name_list *list, const char *name); 115int walk_name_list (struct name_list *list, const char *name);
116void print_help (void);
117void print_usage (void);
189 118
190int w_df = -1; 119uintmax_t w_df = 0;
191int c_df = -1; 120uintmax_t c_df = 0;
192double w_dfp = -1.0; 121double w_dfp = -1.0;
193double c_dfp = -1.0; 122double c_dfp = -1.0;
194char *path = ""; 123char *path = "";
195char *exclude_device = ""; 124char *exclude_device = "";
196char *units = "MB"; 125char *units = NULL;
197unsigned long mult = 1024 * 1024; 126uintmax_t mult = 1024 * 1024;
198int verbose = 0; 127int verbose = 0;
199int erronly = FALSE; 128int erronly = FALSE;
200int display_mntp = FALSE; 129int display_mntp = FALSE;
@@ -202,10 +131,12 @@ int display_mntp = FALSE;
202/* Linked list of mounted filesystems. */ 131/* Linked list of mounted filesystems. */
203static struct mount_entry *mount_list; 132static struct mount_entry *mount_list;
204 133
134
135
205int 136int
206main (int argc, char **argv) 137main (int argc, char **argv)
207{ 138{
208 int usp = -1; 139 double usp = -1.0;
209 int result = STATE_UNKNOWN; 140 int result = STATE_UNKNOWN;
210 int disk_result = STATE_UNKNOWN; 141 int disk_result = STATE_UNKNOWN;
211 char file_system[MAX_INPUT_BUFFER]; 142 char file_system[MAX_INPUT_BUFFER];
@@ -244,7 +175,7 @@ main (int argc, char **argv)
244 get_fs_usage (me->me_mountdir, me->me_devname, &fsp); 175 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
245 176
246 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { 177 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
247 usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks; 178 usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
248 disk_result = check_disk (usp, fsp.fsu_bavail); 179 disk_result = check_disk (usp, fsp.fsu_bavail);
249 result = max_state (disk_result, result); 180 result = max_state (disk_result, result);
250 if (disk_result==STATE_OK && erronly && !verbose) 181 if (disk_result==STATE_OK && erronly && !verbose)
@@ -364,7 +295,7 @@ process_arguments (int argc, char **argv)
364 } 295 }
365 else if (strpbrk (optarg, ",:") && 296 else if (strpbrk (optarg, ",:") &&
366 strstr (optarg, "%") && 297 strstr (optarg, "%") &&
367 sscanf (optarg, "%d%*[:,]%lf%%", &w_df, &w_dfp) == 2) { 298 sscanf (optarg, "%ul%*[:,]%lf%%", &w_df, &w_dfp) == 2) {
368 break; 299 break;
369 } 300 }
370 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) { 301 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
@@ -380,7 +311,7 @@ process_arguments (int argc, char **argv)
380 } 311 }
381 else if (strpbrk (optarg, ",:") && 312 else if (strpbrk (optarg, ",:") &&
382 strstr (optarg, "%") && 313 strstr (optarg, "%") &&
383 sscanf (optarg, "%d%*[,:]%lf%%", &c_df, &c_dfp) == 2) { 314 sscanf (optarg, "%ul%*[,:]%lf%%", &c_df, &c_dfp) == 2) {
384 break; 315 break;
385 } 316 }
386 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) { 317 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
@@ -391,19 +322,19 @@ process_arguments (int argc, char **argv)
391 } 322 }
392 case 'u': 323 case 'u':
393 if (! strcmp (optarg, "bytes")) { 324 if (! strcmp (optarg, "bytes")) {
394 mult = 1; 325 mult = (uintmax_t)1;
395 units = "B"; 326 units = "B";
396 } else if (! strcmp (optarg, "kB")) { 327 } else if (! strcmp (optarg, "kB")) {
397 mult = 1024; 328 mult = (uintmax_t)1024;
398 units = "kB"; 329 units = "kB";
399 } else if (! strcmp (optarg, "MB")) { 330 } else if (! strcmp (optarg, "MB")) {
400 mult = 1024 * 1024; 331 mult = (uintmax_t)1024 * 1024;
401 units = "MB"; 332 units = "MB";
402 } else if (! strcmp (optarg, "GB")) { 333 } else if (! strcmp (optarg, "GB")) {
403 mult = 1024 * 1024 * 1024; 334 mult = (uintmax_t)1024 * 1024 * 1024;
404 units = "GB"; 335 units = "GB";
405 } else if (! strcmp (optarg, "TB")) { 336 } else if (! strcmp (optarg, "TB")) {
406 mult = (unsigned long)1024 * 1024 * 1024 * 1024; 337 mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
407 units = "TB"; 338 units = "TB";
408 } else { 339 } else {
409 die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg); 340 die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
@@ -458,8 +389,8 @@ process_arguments (int argc, char **argv)
458 display_mntp = TRUE; 389 display_mntp = TRUE;
459 break; 390 break;
460 case 'C': 391 case 'C':
461 w_df = -1; 392 w_df = 0;
462 c_df = -1; 393 c_df = 0;
463 w_dfp = -1.0; 394 w_dfp = -1.0;
464 c_dfp = -1.0; 395 c_dfp = -1.0;
465 break; 396 break;
@@ -477,10 +408,10 @@ process_arguments (int argc, char **argv)
477 408
478 /* Support for "check_disk warn crit [fs]" with thresholds at used level */ 409 /* Support for "check_disk warn crit [fs]" with thresholds at used level */
479 c = optind; 410 c = optind;
480 if (w_dfp == -1 && argc > c && is_intnonneg (argv[c])) 411 if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
481 w_dfp = (100.0 - atof (argv[c++])); 412 w_dfp = (100.0 - atof (argv[c++]));
482 413
483 if (c_dfp == -1 && argc > c && is_intnonneg (argv[c])) 414 if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
484 c_dfp = (100.0 - atof (argv[c++])); 415 c_dfp = (100.0 - atof (argv[c++]));
485 416
486 if (argc > c && strlen (path) == 0) { 417 if (argc > c && strlen (path) == 0) {
@@ -520,9 +451,9 @@ void print_path (char *mypath)
520} 451}
521 452
522int 453int
523validate_arguments (int w, int c, double wp, double cp, char *mypath) 454validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
524{ 455{
525 if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) { 456 if (w == 0 && c == 0 && wp < 0.0 && cp < 0.0) {
526 printf (_("INPUT ERROR: No thresholds specified")); 457 printf (_("INPUT ERROR: No thresholds specified"));
527 print_path (mypath); 458 print_path (mypath);
528 return ERROR; 459 return ERROR;
@@ -535,10 +466,10 @@ INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be betw
535 print_path (path); 466 print_path (path);
536 return ERROR; 467 return ERROR;
537 } 468 }
538 else if ((w > 0 || c > 0) && (w < 0 || c < 0 || c > w)) { 469 else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
539 printf (_("\ 470 printf (_("\
540INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero"), 471INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
541 c, w); 472 (unsigned long)c, (unsigned long)w);
542 print_path (path); 473 print_path (path);
543 return ERROR; 474 return ERROR;
544 } 475 }
@@ -551,17 +482,17 @@ INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater
551 482
552 483
553int 484int
554check_disk (int usp, int free_disk) 485check_disk (int usp, uintmax_t free_disk)
555{ 486{
556 int result = STATE_UNKNOWN; 487 int result = STATE_UNKNOWN;
557 /* check the percent used space against thresholds */ 488 /* check the percent used space against thresholds */
558 if (usp >= 0 && usp >= (100.0 - c_dfp)) 489 if (usp >= 0.0 && usp >= (100.0 - c_dfp))
559 result = STATE_CRITICAL; 490 result = STATE_CRITICAL;
560 else if (c_df >= 0 && free_disk <= c_df) 491 else if (c_df > 0 && free_disk <= c_df)
561 result = STATE_CRITICAL; 492 result = STATE_CRITICAL;
562 else if (usp >= 0 && usp >= (100.0 - w_dfp)) 493 else if (usp >= 0.0 && usp >= (100.0 - w_dfp))
563 result = STATE_WARNING; 494 result = STATE_WARNING;
564 else if (w_df >= 0 && free_disk <= w_df) 495 else if (w_df > 0 && free_disk <= w_df)
565 result = STATE_WARNING; 496 result = STATE_WARNING;
566 else if (usp >= 0.0) 497 else if (usp >= 0.0)
567 result = STATE_OK; 498 result = STATE_OK;
@@ -579,11 +510,92 @@ walk_name_list (struct name_list *list, const char *name)
579 /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */ 510 /* 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; 511 if (list->w_df) w_df = list->w_df;
581 if (list->c_df) c_df = list->c_df; 512 if (list->c_df) c_df = list->c_df;
582 if (list->w_dfp) w_dfp = list->w_dfp; 513 if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
583 if (list->c_dfp) c_dfp = list->c_dfp; 514 if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
584 return TRUE; 515 return TRUE;
585 } 516 }
586 list = list->name_next; 517 list = list->name_next;
587 } 518 }
588 return FALSE; 519 return FALSE;
589} 520}
521
522
523
524
525
526
527void
528print_help (void)
529{
530 print_revision (progname, revision);
531
532 printf (_(COPYRIGHT), copyright, email);
533
534 printf (_("\
535This plugin checks the amount of used disk space on a mounted file system\n\
536and generates an alert if free space is less than one of the threshold values."));
537
538 print_usage ();
539
540 printf (_(UT_HELP_VRSN));
541
542 printf (_("\
543 -w, --warning=INTEGER\n\
544 Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
545 -w, --warning=PERCENT%%\n\
546 Exit with WARNING status if less than PERCENT of disk space is free\n\
547 -c, --critical=INTEGER\n\
548 Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
549 -c, --critical=PERCENT%%\n\
550 Exit with CRITCAL status if less than PERCENT of disk space is free\n\
551 -C, --clear\n\
552 Clear thresholds\n"));
553
554 printf (_("\
555 -u, --units=STRING\n\
556 Choose bytes, kB, MB, GB, TB (default: MB)\n\
557 -k, --kilobytes\n\
558 Same as '--units kB'\n\
559 -m, --megabytes\n\
560 Same as '--units MB'\n"));
561
562 printf (_("\
563 -l, --local\n\
564 Only check local filesystems\n\
565 -p, --path=PATH, --partition=PARTITION\n\
566 Path or partition (may be repeated)\n\
567 -x, --exclude_device=PATH <STRING>\n\
568 Ignore device (only works if -p unspecified)\n\
569 -X, --exclude-type=TYPE <STRING>\n\
570 Ignore all filesystems of indicated type (may be repeated)\n\
571 -M, --mountpoint\n\
572 Display the mountpoint instead of the partition\n\
573 -e, --errors-only\n\
574 Display only devices/mountpoints with errors\n"));
575
576 printf (_(UT_WARN_CRIT));
577
578 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
579
580 printf (_(UT_VERBOSE));
581
582 printf ("%s", _("Examples:\n\
583 check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
584 Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
585
586 support ();
587}
588
589
590
591
592void
593print_usage (void)
594{
595 printf (_("\
596Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
597 [-v] [-q]\n\
598 %s (-h|--help)\n\
599 %s (-V|--version)\n"),
600 progname, progname, progname);
601}