sensu-plugins / sensu-plugins-disk-checks

This plugin provides native disk instrumentation for monitoring and metrics collection, including: health, usage, and various metrics.
http://sensu-plugins.io
MIT License
27 stars 63 forks source link

Fix wrong TiB formating in to_human function #29

Closed Salamek closed 8 years ago

Salamek commented 8 years ago

Fixes bug mentioned in https://github.com/sensu-plugins/sensu-plugins-disk-checks/issues/25

When:

fs_info.bytes_used was 635216715776b
fs_info.bytes_total was 975788797952b

old to_human failed to format it resulting in:

CheckDisk WARNING: / 65.1% bytes usage (0 TiB/0 TiB) 

Simple test script:

# Old version
def to_human(s)
    prefix = %w(TiB GiB MiB KiB B)
    s.to_f
    i = prefix.length - 1
    while s > 512 && i > 0
      s /= 1024
      i -= 1
    end
    ((s > 9 || s.modulo(1) < 0.1 ? '%d' : '%.1f') % s) + ' ' + prefix[i]
  end

# New version
def to_human2(s)
  unit = [[1099511627776, "TiB"],[1073741824, "GiB"], [1048576, "MiB"], [1024, "KiB"], [0,"B"]].detect{ |u| s >= u[0] }
  "#{s>0 ? s/unit[0] : s} #{unit[1]}"
end

print to_human(635216715776)
print "\n"
print to_human2(635216715776)
print "\n"

Resulting in:

0 TiB
591 GiB
eheydrick commented 8 years ago

Thanks @Salamek!