aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorGravatar RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> 2021-10-28 16:13:28 +0200
committerGravatar GitHub <noreply@github.com> 2021-10-28 16:13:28 +0200
commit7415eb2f065911b138e167e35d0f67cddb5718ef (patch)
tree27c82696cf49a00a379f030bb09adc3f1e37102e /plugins
parente79ada81a696e3c826b383e3ff7cbc8f91bf46a4 (diff)
parent884327ee21b25be3ce1b5627bb40339e14545256 (diff)
downloadmonitoring-plugins-7415eb2f065911b138e167e35d0f67cddb5718ef.tar.gz
Merge pull request #1714 from RincewindsHat/fix_perfdata_for_big_values_for_check_disk
Fix perfdata for big values for check disk. First merge \o/
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_disk.c184
-rw-r--r--plugins/t/check_disk.t7
-rw-r--r--plugins/utils.c32
3 files changed, 123 insertions, 100 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index a2735195..c526d056 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -1,29 +1,29 @@
/*****************************************************************************
-*
+*
* Monitoring check_disk plugin
-*
+*
* License: GPL
* Copyright (c) 1999-2008 Monitoring Plugins Development Team
-*
+*
* Description:
-*
+*
* This file contains the check_disk plugin
-*
-*
+*
+*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
-*
+*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
-*
+*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
-*
-*
+*
+*
*****************************************************************************/
const char *progname = "check_disk";
@@ -46,7 +46,7 @@ const char *email = "devel@monitoring-plugins.org";
#include <stdarg.h>
#include "fsusage.h"
#include "mountlist.h"
-#include "intprops.h" /* necessary for TYPE_MAXIMUM */
+#include "intprops.h" /* necessary for TYPE_MAXIMUM */
#if HAVE_LIMITS_H
# include <limits.h>
#endif
@@ -172,8 +172,6 @@ main (int argc, char **argv)
char *preamble;
char *flag_header;
double inode_space_pct;
- double warning_high_tide;
- double critical_high_tide;
int temp_result;
struct mount_entry *me;
@@ -245,17 +243,17 @@ main (int argc, char **argv)
#ifdef __CYGWIN__
if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11)
- continue;
+ continue;
snprintf(mountdir, sizeof(mountdir), "%s:\\", me->me_mountdir + 10);
if (GetDriveType(mountdir) != DRIVE_FIXED)
- me->me_remote = 1;
+ me->me_remote = 1;
#endif
/* Filters */
/* Remove filesystems already seen */
if (np_seen_name(seen, me->me_mountdir)) {
continue;
- }
+ }
np_add_name(&seen, me->me_mountdir);
if (path->group == NULL) {
@@ -288,8 +286,17 @@ main (int argc, char **argv)
get_stats (path, &fsp);
if (verbose >= 3) {
- printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g free_inodes_pct=%g fsp.fsu_blocksize=%llu mult=%llu\n",
- me->me_mountdir, path->dused_pct, path->dfree_pct, path->dused_units, path->dfree_units, path->dtotal_units, path->dused_inodes_percent, path->dfree_inodes_percent, fsp.fsu_blocksize, mult);
+ printf ("For %s, used_pct=%g free_pct=%g used_units=%llu free_units=%llu total_units=%llu used_inodes_pct=%g free_inodes_pct=%g fsp.fsu_blocksize=%llu mult=%llu\n",
+ me->me_mountdir,
+ path->dused_pct,
+ path->dfree_pct,
+ path->dused_units,
+ path->dfree_units,
+ path->dtotal_units,
+ path->dused_inodes_percent,
+ path->dfree_inodes_percent,
+ fsp.fsu_blocksize,
+ mult);
}
/* Threshold comparisons */
@@ -326,77 +333,79 @@ main (int argc, char **argv)
*/
/* *_high_tide must be reinitialized at each run */
- warning_high_tide = UINT_MAX;
- critical_high_tide = UINT_MAX;
+ uint64_t warning_high_tide = UINT64_MAX;
if (path->freespace_units->warning != NULL) {
- warning_high_tide = path->dtotal_units - path->freespace_units->warning->end;
+ warning_high_tide = (path->dtotal_units - path->freespace_units->warning->end) * mult;
}
if (path->freespace_percent->warning != NULL) {
- warning_high_tide = abs( min( (double) warning_high_tide, (double) (1.0 - path->freespace_percent->warning->end/100)*path->dtotal_units ));
+ warning_high_tide = min( warning_high_tide, (uint64_t)((1.0 - path->freespace_percent->warning->end/100) * (path->dtotal_units * mult)) );
}
+
+ uint64_t critical_high_tide = UINT64_MAX;
+
if (path->freespace_units->critical != NULL) {
- critical_high_tide = path->dtotal_units - path->freespace_units->critical->end;
+ critical_high_tide = (path->dtotal_units - path->freespace_units->critical->end) * mult;
}
if (path->freespace_percent->critical != NULL) {
- critical_high_tide = abs( min( (double) critical_high_tide, (double) (1.0 - path->freespace_percent->critical->end/100)*path->dtotal_units ));
+ critical_high_tide = min( critical_high_tide, (uint64_t)((1.0 - path->freespace_percent->critical->end/100) * (path->dtotal_units * mult)) );
}
- /* Nb: *_high_tide are unset when == UINT_MAX */
+ /* Nb: *_high_tide are unset when == UINT64_MAX */
xasprintf (&perf, "%s %s", perf,
- perfdata ((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
- path->dused_units, units,
- (warning_high_tide != UINT_MAX ? TRUE : FALSE), warning_high_tide,
- (critical_high_tide != UINT_MAX ? TRUE : FALSE), critical_high_tide,
- TRUE, 0,
- TRUE, path->dtotal_units));
+ perfdata_uint64 (
+ (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
+ path->dused_units * mult, "B",
+ (warning_high_tide == UINT64_MAX ? FALSE : TRUE), warning_high_tide,
+ (critical_high_tide == UINT64_MAX ? FALSE : TRUE), critical_high_tide,
+ TRUE, 0,
+ TRUE, path->dtotal_units * mult));
if (display_inodes_perfdata) {
/* *_high_tide must be reinitialized at each run */
- warning_high_tide = UINT_MAX;
- critical_high_tide = UINT_MAX;
+ warning_high_tide = UINT64_MAX;
+ critical_high_tide = UINT64_MAX;
if (path->freeinodes_percent->warning != NULL) {
- warning_high_tide = abs( min( (double) warning_high_tide, (double) (1.0 - path->freeinodes_percent->warning->end/100)*path->inodes_total ));
+ warning_high_tide = llabs( min( (double) warning_high_tide, (double) (1.0 - path->freeinodes_percent->warning->end/100)*path->inodes_total ));
}
if (path->freeinodes_percent->critical != NULL) {
- critical_high_tide = abs( min( (double) critical_high_tide, (double) (1.0 - path->freeinodes_percent->critical->end/100)*path->inodes_total ));
+ critical_high_tide = llabs( min( (double) critical_high_tide, (double) (1.0 - path->freeinodes_percent->critical->end/100)*path->inodes_total ));
}
xasprintf (&perf_ilabel, "%s (inodes)", (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
- /* Nb: *_high_tide are unset when == UINT_MAX */
+ /* Nb: *_high_tide are unset when == UINT64_MAX */
xasprintf (&perf, "%s %s", perf,
- perfdata (perf_ilabel,
- path->inodes_used, "",
- (warning_high_tide != UINT_MAX ? TRUE : FALSE), warning_high_tide,
- (critical_high_tide != UINT_MAX ? TRUE : FALSE), critical_high_tide,
- TRUE, 0,
- TRUE, path->inodes_total));
+ perfdata_uint64 (perf_ilabel,
+ path->inodes_used, "",
+ (warning_high_tide != UINT64_MAX ? TRUE : FALSE), warning_high_tide,
+ (critical_high_tide != UINT64_MAX ? TRUE : FALSE), critical_high_tide,
+ TRUE, 0,
+ TRUE, path->inodes_total));
}
if (disk_result==STATE_OK && erronly && !verbose)
continue;
- if(disk_result && verbose >= 1) {
- xasprintf(&flag_header, " %s [", state_text (disk_result));
- } else {
- xasprintf(&flag_header, "");
- }
- xasprintf (&output, "%s%s %s %.0f %s (%.0f%%",
- output, flag_header,
- (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
- path->dfree_units,
- units,
- path->dfree_pct);
- if (path->dused_inodes_percent < 0) {
- xasprintf(&output, "%s inode=-)%s;", output, (disk_result ? "]" : ""));
- } else {
- xasprintf(&output, "%s inode=%.0f%%)%s;", output, path->dfree_inodes_percent, ((disk_result && verbose >= 1) ? "]" : ""));
- }
+ if(disk_result && verbose >= 1) {
+ xasprintf(&flag_header, " %s [", state_text (disk_result));
+ } else {
+ xasprintf(&flag_header, "");
+ }
+ xasprintf (&output, "%s%s %s %llu%s (%.0f%%",
+ output, flag_header,
+ (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
+ path->dfree_units,
+ units,
+ path->dfree_pct);
+ if (path->dused_inodes_percent < 0) {
+ xasprintf(&output, "%s inode=-)%s;", output, (disk_result ? "]" : ""));
+ } else {
+ xasprintf(&output, "%s inode=%.0f%%)%s;", output, path->dfree_inodes_percent, ((disk_result && verbose >= 1) ? "]" : ""));
+ }
free(flag_header);
/* TODO: Need to do a similar debug line
- xasprintf (&details, _("%s\n\
-%.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
+ xasprintf (&details, _("%s\n\%.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct,
me->me_devname, me->me_type, me->me_mountdir,
(unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
@@ -557,14 +566,14 @@ process_arguments (int argc, char **argv)
}
break;
- case 'W': /* warning inode threshold */
+ case 'W': /* warning inode threshold */
if (*optarg == '@') {
warn_freeinodes_percent = optarg;
} else {
xasprintf(&warn_freeinodes_percent, "@%s", optarg);
}
break;
- case 'K': /* critical inode threshold */
+ case 'K': /* critical inode threshold */
if (*optarg == '@') {
crit_freeinodes_percent = optarg;
} else {
@@ -574,21 +583,24 @@ process_arguments (int argc, char **argv)
case 'u':
if (units)
free(units);
- if (! strcmp (optarg, "bytes")) {
+ if (! strcasecmp (optarg, "bytes")) {
mult = (uintmax_t)1;
units = strdup ("B");
- } else if (! strcmp (optarg, "kB")) {
+ } else if ( (! strcmp (optarg, "kB")) || (!strcmp(optarg, "KiB")) ) {
mult = (uintmax_t)1024;
- units = strdup ("kB");
- } else if (! strcmp (optarg, "MB")) {
+ units = strdup ("kiB");
+ } else if ( (! strcmp (optarg, "MB")) || (!strcmp(optarg, "MiB")) ) {
mult = (uintmax_t)1024 * 1024;
- units = strdup ("MB");
- } else if (! strcmp (optarg, "GB")) {
+ units = strdup ("MiB");
+ } else if ( (! strcmp (optarg, "GB")) || (!strcmp(optarg, "GiB")) ) {
mult = (uintmax_t)1024 * 1024 * 1024;
- units = strdup ("GB");
- } else if (! strcmp (optarg, "TB")) {
+ units = strdup ("GiB");
+ } else if ( (! strcmp (optarg, "TB")) || (!strcmp(optarg, "TiB")) ) {
mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
- units = strdup ("TB");
+ units = strdup ("TiB");
+ } else if ( (! strcmp (optarg, "PB")) || (!strcmp(optarg, "PiB")) ) {
+ mult = (uintmax_t)1024 * 1024 * 1024 * 1024 * 1024;
+ units = strdup ("PiB");
} else {
die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
}
@@ -599,13 +611,13 @@ process_arguments (int argc, char **argv)
mult = 1024;
if (units)
free(units);
- units = strdup ("kB");
+ units = strdup ("kiB");
break;
case 'm': /* display mountpoint */
mult = 1024 * 1024;
if (units)
free(units);
- units = strdup ("MB");
+ units = strdup ("MiB");
break;
case 'L':
stat_remote_fs = 1;
@@ -812,7 +824,7 @@ process_arguments (int argc, char **argv)
}
if (units == NULL) {
- units = strdup ("MB");
+ units = strdup ("MiB");
mult = (uintmax_t)1024 * 1024;
}
@@ -1026,20 +1038,20 @@ get_stats (struct parameter_list *p, struct fs_usage *fsp) {
if (p_list->group && ! (strcmp(p_list->group, p->group))) {
stat_path(p_list);
get_fs_usage (p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp);
- get_path_stats(p_list, &tmpfsp);
+ get_path_stats(p_list, &tmpfsp);
if (verbose >= 3)
printf("Group %s: adding %llu blocks sized %llu, (%s) used_units=%g free_units=%g total_units=%g fsu_blocksize=%llu mult=%llu\n",
p_list->group, tmpfsp.fsu_bavail, tmpfsp.fsu_blocksize, p_list->best_match->me_mountdir, p_list->dused_units, p_list->dfree_units,
p_list->dtotal_units, mult);
- /* prevent counting the first FS of a group twice since its parameter_list entry
+ /* prevent counting the first FS of a group twice since its parameter_list entry
* is used to carry the information of all file systems of the entire group */
if (! first) {
p->total += p_list->total;
p->available += p_list->available;
p->available_to_root += p_list->available_to_root;
p->used += p_list->used;
-
+
p->dused_units += p_list->dused_units;
p->dfree_units += p_list->dfree_units;
p->dtotal_units += p_list->dtotal_units;
@@ -1050,20 +1062,26 @@ get_stats (struct parameter_list *p, struct fs_usage *fsp) {
}
first = 0;
}
- if (verbose >= 3)
+ if (verbose >= 3)
printf("Group %s now has: used_units=%g free_units=%g total_units=%g fsu_blocksize=%llu mult=%llu\n",
- p->group, tmpfsp.fsu_bavail, tmpfsp.fsu_blocksize, p->best_match->me_mountdir, p->dused_units,
- p->dfree_units, p->dtotal_units, mult);
+ p->group,
+ tmpfsp.fsu_bavail,
+ tmpfsp.fsu_blocksize,
+ p->best_match->me_mountdir,
+ p->dused_units,
+ p->dfree_units,
+ p->dtotal_units,
+ mult);
}
/* modify devname and mountdir for output */
p->best_match->me_mountdir = p->best_match->me_devname = p->group;
}
/* finally calculate percentages for either plain FS or summed up group */
- p->dused_pct = calculate_percent( p->used, p->used + p->available ); /* used + available can never be > uintmax */
+ p->dused_pct = calculate_percent( p->used, p->used + p->available ); /* used + available can never be > uintmax */
p->dfree_pct = 100 - p->dused_pct;
p->dused_inodes_percent = calculate_percent(p->inodes_total - p->inodes_free, p->inodes_total);
p->dfree_inodes_percent = 100 - p->dused_inodes_percent;
-
+
}
void
@@ -1078,7 +1096,7 @@ get_path_stats (struct parameter_list *p, struct fs_usage *fsp) {
/* default behaviour : take all the blocks into account */
p->total = fsp->fsu_blocks;
}
-
+
p->dused_units = p->used*fsp->fsu_blocksize/mult;
p->dfree_units = p->available*fsp->fsu_blocksize/mult;
p->dtotal_units = p->total*fsp->fsu_blocksize/mult;
diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t
index fdd8769b..ec527e7f 100644
--- a/plugins/t/check_disk.t
+++ b/plugins/t/check_disk.t
@@ -88,8 +88,9 @@ $result = NPTest->testCmd(
);
$_ = $result->perf_output;
my ($warn_absth_data, $crit_absth_data, $total_absth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
-is ($warn_absth_data, $total_absth_data - 20, "Wrong warning in perf data using absolute thresholds");
-is ($crit_absth_data, $total_absth_data - 10, "Wrong critical in perf data using absolute thresholds");
+# default unit is MiB, but perfdata is always bytes
+is ($warn_absth_data, $total_absth_data - (20 * (2 ** 20)), "Wrong warning in perf data using absolute thresholds");
+is ($crit_absth_data, $total_absth_data - (10 * (2 ** 20)), "Wrong critical in perf data using absolute thresholds");
# Then check percent thresholds.
$result = NPTest->testCmd(
@@ -119,7 +120,7 @@ like ( $result->only_output, qr/$more_free/, "Have disk name in text");
$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" );
cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free");
$_ = $result->output;
-my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+) MB .* (\d+) MB /g);
+my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+)MiB .* (\d+)MiB /g);
my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2;
diff --git a/plugins/utils.c b/plugins/utils.c
index f7f8952f..ebdae2e1 100644
--- a/plugins/utils.c
+++ b/plugins/utils.c
@@ -601,13 +601,13 @@ char *perfdata (const char *label,
char *perfdata_uint64 (const char *label,
uint64_t val,
const char *uom,
- int warnp,
+ int warnp, /* Warning present */
uint64_t warn,
- int critp,
+ int critp, /* Critical present */
uint64_t crit,
- int minp,
+ int minp, /* Minimum present */
uint64_t minv,
- int maxp,
+ int maxp, /* Maximum present */
uint64_t maxv)
{
char *data = NULL;
@@ -618,20 +618,22 @@ char *perfdata_uint64 (const char *label,
xasprintf (&data, "%s=%ld%s;", label, val, uom);
if (warnp)
- xasprintf (&data, "%s%ld;", data, warn);
+ xasprintf (&data, "%s%lu;", data, warn);
else
xasprintf (&data, "%s;", data);
if (critp)
- xasprintf (&data, "%s%ld;", data, crit);
+ xasprintf (&data, "%s%lu;", data, crit);
else
xasprintf (&data, "%s;", data);
if (minp)
- xasprintf (&data, "%s%ld", data, minv);
+ xasprintf (&data, "%s%lu;", data, minv);
+ else
+ xasprintf (&data, "%s;", data);
if (maxp)
- xasprintf (&data, "%s;%ld", data, maxv);
+ xasprintf (&data, "%s%lu", data, maxv);
return data;
}
@@ -640,13 +642,13 @@ char *perfdata_uint64 (const char *label,
char *perfdata_int64 (const char *label,
int64_t val,
const char *uom,
- int warnp,
+ int warnp, /* Warning present */
int64_t warn,
- int critp,
+ int critp, /* Critical present */
int64_t crit,
- int minp,
+ int minp, /* Minimum present */
int64_t minv,
- int maxp,
+ int maxp, /* Maximum present */
int64_t maxv)
{
char *data = NULL;
@@ -667,10 +669,12 @@ char *perfdata_int64 (const char *label,
xasprintf (&data, "%s;", data);
if (minp)
- xasprintf (&data, "%s%ld", data, minv);
+ xasprintf (&data, "%s%ld;", data, minv);
+ else
+ xasprintf (&data, "%s;", data);
if (maxp)
- xasprintf (&data, "%s;%ld", data, maxv);
+ xasprintf (&data, "%s%ld", data, maxv);
return data;
}