mej / nhc

LBNL Node Health Check
Other
213 stars 78 forks source link

`nhc_common_parse_size` doesn't support decimal values #120

Open kcgthb opened 1 year ago

kcgthb commented 1 year ago

Using decimal values in checks that use nhc_common_parse_size fail with an arithmetic operator error.

For instance:

$ nhc -d -e 'check_hw_physmem 1.5TB 1.5TB 10%'
DEBUG:  Debugging activated via -d option.
DEBUG:  Evaluating single check line:  check_hw_physmem 1.5TB 1.5TB 10%
[...]
/etc/nhc/scripts/common.nhc: line 508: 1.5*1024*1024*1024: syntax error: invalid arithmetic operator (error token is ".5*1024*1024*1024")
mej commented 1 year ago

This is a known limitation of Bash; it supports integer arithmetic only.

The "converse" function, nhc_common_unparse_size(), has a block of code to work around the lack of support for fractions and the various challenges that inevitably ensue. 😜 But in truth there are numerous potential "solutions" (mostly workarounds).

One possible workaround that's still readable involves some creative variable naming:

# In /etc/sysconfig/nhc
KB=1024 ; MB=$((1024*KB)) ; GB=$((1024*MB)) ; TB=$((1024*GB)) ; PB=$((1024*TB)) ; EB=$((1024*PB))

This allows for the command-line args or config files to contain something like this:

check_hw_physmem $((15*TB/10)) $((15*TB/10)) 10%

Alternatively, you can employ a similar technique to the code I referenced above, reverting to the next lower size unit:

check_hw_physmem 1536GB 1536GB 10%

That said, if this is actually intended to be a request for adding some level of decimal-parsing support to nhc_common_parse_size()... It would take some creativity, but I think it would be doable, probably using a similar technique to the one shown in the 2nd code block above (i.e., shift decimal right by n, multiply, divide by 10^n). Was that your intent?