diff options
-rw-r--r-- | plugins/check_disk.c | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c index b966fabe..e7a3b216 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c | |||
@@ -121,6 +121,7 @@ void print_path (const char *mypath); | |||
121 | int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *); | 121 | int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *); |
122 | void print_help (void); | 122 | void print_help (void); |
123 | void print_usage (void); | 123 | void print_usage (void); |
124 | double calculate_percent(uintmax_t, uintmax_t); | ||
124 | 125 | ||
125 | double w_dfp = -1.0; | 126 | double w_dfp = -1.0; |
126 | double c_dfp = -1.0; | 127 | double c_dfp = -1.0; |
@@ -152,7 +153,7 @@ main (int argc, char **argv) | |||
152 | char *output; | 153 | char *output; |
153 | char *details; | 154 | char *details; |
154 | char *perf; | 155 | char *perf; |
155 | float inode_space_pct; | 156 | double inode_space_pct; |
156 | uintmax_t total, available, available_to_root, used; | 157 | uintmax_t total, available, available_to_root, used; |
157 | double dfree_pct = -1, dused_pct = -1; | 158 | double dfree_pct = -1, dused_pct = -1; |
158 | double dused_units, dfree_units, dtotal_units; | 159 | double dused_units, dfree_units, dtotal_units; |
@@ -239,32 +240,13 @@ main (int argc, char **argv) | |||
239 | available_to_root = fsp.fsu_bfree; | 240 | available_to_root = fsp.fsu_bfree; |
240 | used = total - available_to_root; | 241 | used = total - available_to_root; |
241 | 242 | ||
242 | /* I don't understand the below, but it is taken from coreutils' df */ | 243 | dused_pct = calculate_percent( used, used + available ); /* used + available can never be > uintmax */ |
243 | /* Is setting dused_pct, in the best possible way */ | 244 | |
244 | if (used <= TYPE_MAXIMUM(uintmax_t) / 100) { | ||
245 | uintmax_t u100 = used * 100; | ||
246 | uintmax_t nonroot_total = used + available; | ||
247 | dused_pct = u100 / nonroot_total + (u100 % nonroot_total != 0); | ||
248 | } else { | ||
249 | /* Possible rounding errors - see coreutils' df for more explanation */ | ||
250 | double u = used; | ||
251 | double a = available; | ||
252 | double nonroot_total = u + a; | ||
253 | if (nonroot_total) { | ||
254 | long int lipct = dused_pct = u * 100 / nonroot_total; | ||
255 | double ipct = lipct; | ||
256 | |||
257 | /* Like 'pct = ceil (dpct);', but without ceil - from coreutils again */ | ||
258 | if (ipct - 1 < dused_pct && dused_pct <= ipct + 1) | ||
259 | dused_pct = ipct + (ipct < dused_pct); | ||
260 | } | ||
261 | } | ||
262 | |||
263 | dfree_pct = 100 - dused_pct; | 245 | dfree_pct = 100 - dused_pct; |
264 | dused_units = used*fsp.fsu_blocksize/mult; | 246 | dused_units = used*fsp.fsu_blocksize/mult; |
265 | dfree_units = available*fsp.fsu_blocksize/mult; | 247 | dfree_units = available*fsp.fsu_blocksize/mult; |
266 | dtotal_units = total*fsp.fsu_blocksize/mult; | 248 | dtotal_units = total*fsp.fsu_blocksize/mult; |
267 | dused_inodes_percent = (fsp.fsu_files - fsp.fsu_ffree) * 100 / fsp.fsu_files; | 249 | dused_inodes_percent = calculate_percent(fsp.fsu_files - fsp.fsu_ffree, fsp.fsu_files); |
268 | 250 | ||
269 | if (verbose >= 3) { | 251 | if (verbose >= 3) { |
270 | printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g\n", | 252 | printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g\n", |
@@ -297,9 +279,9 @@ main (int argc, char **argv) | |||
297 | 279 | ||
298 | 280 | ||
299 | 281 | ||
300 | /* Moved this computation up here so we can add it | 282 | /* Moved this computation up here so we can add it |
301 | * to perf */ | 283 | * to perf */ |
302 | inode_space_pct = (float)fsp.fsu_ffree*100/fsp.fsu_files; | 284 | inode_space_pct = (1 - dused_inodes_percent) * 100; |
303 | 285 | ||
304 | 286 | ||
305 | asprintf (&perf, "%s %s", perf, | 287 | asprintf (&perf, "%s %s", perf, |
@@ -307,23 +289,27 @@ main (int argc, char **argv) | |||
307 | dused_units, units, | 289 | dused_units, units, |
308 | FALSE, 0, /* min ((uintmax_t)dtotal_units-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*dtotal_units)), */ | 290 | FALSE, 0, /* min ((uintmax_t)dtotal_units-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*dtotal_units)), */ |
309 | FALSE, 0, /* min ((uintmax_t)dtotal_units-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*dtotal_units)), */ | 291 | FALSE, 0, /* min ((uintmax_t)dtotal_units-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*dtotal_units)), */ |
310 | FALSE, 0, /* inode_space_pct, */ | 292 | FALSE, 0, /* inode_space_pct - this is not meant to be here???, */ |
311 | FALSE, 0));; /* dtotal_units)); */ | 293 | FALSE, 0));; /* dtotal_units)); */ |
312 | 294 | ||
313 | if (disk_result==STATE_OK && erronly && !verbose) | 295 | if (disk_result==STATE_OK && erronly && !verbose) |
314 | continue; | 296 | continue; |
315 | 297 | ||
316 | if (disk_result!=STATE_OK || verbose>=0) { | 298 | if (disk_result!=STATE_OK || verbose>=0) { |
317 | asprintf (&output, ("%s %s %.0f %s (%.0f%% inode=%.0f%%);"), | 299 | asprintf (&output, "%s %s %.0f %s (%.0f%%", |
318 | output, | 300 | output, |
319 | (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | 301 | (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, |
320 | dfree_units, | 302 | dfree_units, |
321 | units, | 303 | units, |
322 | dfree_pct, | 304 | dfree_pct); |
323 | inode_space_pct); | 305 | if (dused_inodes_percent < 0) { |
306 | asprintf(&output, "%s inode=-);", output); | ||
307 | } else { | ||
308 | asprintf(&output, "%s inode=%.0f%%);", output, (1 - dused_inodes_percent) * 100); | ||
309 | } | ||
324 | } | 310 | } |
325 | 311 | ||
326 | /* Need to do a similar one | 312 | /* TODO: Need to do a similar debug line |
327 | asprintf (&details, _("%s\n\ | 313 | asprintf (&details, _("%s\n\ |
328 | %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"), | 314 | %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"), |
329 | details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct, | 315 | details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct, |
@@ -344,6 +330,29 @@ main (int argc, char **argv) | |||
344 | } | 330 | } |
345 | 331 | ||
346 | 332 | ||
333 | double calculate_percent(uintmax_t value, uintmax_t total) { | ||
334 | double pct = -1; | ||
335 | /* I don't understand the below, but it is taken from coreutils' df */ | ||
336 | /* Seems to be calculating pct, in the best possible way */ | ||
337 | if (value <= TYPE_MAXIMUM(uintmax_t) / 100 | ||
338 | && total != 0) { | ||
339 | uintmax_t u100 = value * 100; | ||
340 | pct = u100 / total + (u100 % total != 0); | ||
341 | } else { | ||
342 | /* Possible rounding errors - see coreutils' df for more explanation */ | ||
343 | double u = value; | ||
344 | double t = total; | ||
345 | if (t) { | ||
346 | long int lipct = pct = u * 100 / t; | ||
347 | double ipct = lipct; | ||
348 | |||
349 | /* Like 'pct = ceil (dpct);', but without ceil - from coreutils again */ | ||
350 | if (ipct - 1 < pct && pct <= ipct + 1) | ||
351 | pct = ipct + (ipct < pct); | ||
352 | } | ||
353 | } | ||
354 | return pct; | ||
355 | } | ||
347 | 356 | ||
348 | /* process command-line arguments */ | 357 | /* process command-line arguments */ |
349 | int | 358 | int |