summaryrefslogtreecommitdiffstats
path: root/plugins/check_disk.d/utils_disk.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_disk.d/utils_disk.c')
-rw-r--r--plugins/check_disk.d/utils_disk.c62
1 files changed, 39 insertions, 23 deletions
diff --git a/plugins/check_disk.d/utils_disk.c b/plugins/check_disk.d/utils_disk.c
index a78b4eae..eec1282b 100644
--- a/plugins/check_disk.d/utils_disk.c
+++ b/plugins/check_disk.d/utils_disk.c
@@ -309,7 +309,7 @@ measurement_unit create_measurement_unit_from_filesystem(parameter_list_elem fil
309 309
310#define RANDOM_STRING_LENGTH 64 310#define RANDOM_STRING_LENGTH 64
311 311
312char *humanize_byte_value(uintmax_t value, bool use_si_units) { 312char *humanize_byte_value(unsigned long long value, bool use_si_units) {
313 // Idea: A reasonable output should have at most 3 orders of magnitude 313 // Idea: A reasonable output should have at most 3 orders of magnitude
314 // before the decimal separator 314 // before the decimal separator
315 // 353GiB is ok, 2444 GiB should be 2.386 TiB 315 // 353GiB is ok, 2444 GiB should be 2.386 TiB
@@ -317,36 +317,52 @@ char *humanize_byte_value(uintmax_t value, bool use_si_units) {
317 if (result == NULL) { 317 if (result == NULL) {
318 die(STATE_UNKNOWN, _("allocation failed")); 318 die(STATE_UNKNOWN, _("allocation failed"));
319 } 319 }
320 const byte_unit KibiBytes_factor = 1024;
321 const byte_unit MebiBytes_factor = 1048576;
322 const byte_unit GibiBytes_factor = 1073741824;
323 const byte_unit TebiBytes_factor = 1099511627776;
324 const byte_unit PebiBytes_factor = 1125899906842624;
325 const byte_unit ExbiBytes_factor = 1152921504606846976;
326 const byte_unit KiloBytes_factor = 1000;
327 const byte_unit MegaBytes_factor = 1000000;
328 const byte_unit GigaBytes_factor = 1000000000;
329 const byte_unit TeraBytes_factor = 1000000000000;
330 const byte_unit PetaBytes_factor = 1000000000000000;
331 const byte_unit ExaBytes_factor = 1000000000000000000;
320 332
321 if (use_si_units) { 333 if (use_si_units) {
322 // SI units, powers of 10 334 // SI units, powers of 10
323 if (value < KiloBytes) { 335 if (value < KiloBytes_factor) {
324 snprintf(result, RANDOM_STRING_LENGTH, "%ju B", value); 336 snprintf(result, RANDOM_STRING_LENGTH, "%llu B", value);
325 } else if (value < MegaBytes) { 337 } else if (value < MegaBytes_factor) {
326 snprintf(result, RANDOM_STRING_LENGTH, "%ju KB", value / KiloBytes); 338 snprintf(result, RANDOM_STRING_LENGTH, "%llu KB", value / KiloBytes_factor);
327 } else if (value < GigaBytes) { 339 } else if (value < GigaBytes_factor) {
328 snprintf(result, RANDOM_STRING_LENGTH, "%ju MB", value / MegaBytes); 340 snprintf(result, RANDOM_STRING_LENGTH, "%llu MB", value / MegaBytes_factor);
329 } else if (value < TeraBytes) { 341 } else if (value < TeraBytes_factor) {
330 snprintf(result, RANDOM_STRING_LENGTH, "%ju GB", value / GigaBytes); 342 snprintf(result, RANDOM_STRING_LENGTH, "%llu GB", value / GigaBytes_factor);
331 } else if (value < PetaBytes) { 343 } else if (value < PetaBytes_factor) {
332 snprintf(result, RANDOM_STRING_LENGTH, "%ju TB", value / TeraBytes); 344 snprintf(result, RANDOM_STRING_LENGTH, "%llu TB", value / TeraBytes_factor);
345 } else if (value < ExaBytes_factor) {
346 snprintf(result, RANDOM_STRING_LENGTH, "%llu PB", value / PetaBytes_factor);
333 } else { 347 } else {
334 snprintf(result, RANDOM_STRING_LENGTH, "%ju PB", value / PetaBytes); 348 snprintf(result, RANDOM_STRING_LENGTH, "%llu EB", value / ExaBytes_factor);
335 } 349 }
336 } else { 350 } else {
337 // IEC units, powers of 2 ^ 10 351 // IEC units, powers of 2 ^ 10
338 if (value < KibiBytes) { 352 if (value < KibiBytes_factor) {
339 snprintf(result, RANDOM_STRING_LENGTH, "%ju B", value); 353 snprintf(result, RANDOM_STRING_LENGTH, "%llu B", value);
340 } else if (value < MebiBytes) { 354 } else if (value < MebiBytes_factor) {
341 snprintf(result, RANDOM_STRING_LENGTH, "%ju KiB", value / KibiBytes); 355 snprintf(result, RANDOM_STRING_LENGTH, "%llu KiB", value / KibiBytes_factor);
342 } else if (value < GibiBytes) { 356 } else if (value < GibiBytes_factor) {
343 snprintf(result, RANDOM_STRING_LENGTH, "%ju MiB", value / MebiBytes); 357 snprintf(result, RANDOM_STRING_LENGTH, "%llu MiB", value / MebiBytes_factor);
344 } else if (value < TebiBytes) { 358 } else if (value < TebiBytes_factor) {
345 snprintf(result, RANDOM_STRING_LENGTH, "%ju GiB", value / GibiBytes); 359 snprintf(result, RANDOM_STRING_LENGTH, "%llu GiB", value / GibiBytes_factor);
346 } else if (value < PebiBytes) { 360 } else if (value < PebiBytes_factor) {
347 snprintf(result, RANDOM_STRING_LENGTH, "%ju TiB", value / TebiBytes); 361 snprintf(result, RANDOM_STRING_LENGTH, "%llu TiB", value / TebiBytes_factor);
362 } else if (value < ExbiBytes_factor) {
363 snprintf(result, RANDOM_STRING_LENGTH, "%llu PiB", value / PebiBytes_factor);
348 } else { 364 } else {
349 snprintf(result, RANDOM_STRING_LENGTH, "%ju PiB", value / PebiBytes); 365 snprintf(result, RANDOM_STRING_LENGTH, "%llu EiB", value / ExbiBytes_factor);
350 } 366 }
351 } 367 }
352 368