check_dmi_data_match fails because of whitespaces
The autogenerated nhc.conf on some of our nodes contains spaces in check_dmi_data_match tests like this:
check_dmi_data_match -h 0x0400 -t 4 "Processor Information: Version: Intel(R) Xeon(R) CPU X5550 @ 2.67GHz"
The string is later read in and passed around and when it is fed to eval, the whitespaces are collapsed:
1510827263] - DEBUG: Glob match check: Processor Information: Version: Intel(R) Xeon(R) CPU X5550 @ 2.67GHz does not match Processor Information: Version: Intel(R) Xeon(R) CPU X5550 @ 2.67GHz
The easiest workaround I found is escaping all whitespaces twice:
check_dmi_data_match -h 0x0401 -t 4 "Processor\\ Information:\\ Version:\\ Intel(R)\\ Xeon(R)\\ CPU\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ X5550\\ \\ @\\ 2.67GHz"
Now the tests run just fine:
Running check: "check_dmi_data_match -h 0x0400 -t 4 "Processor\ Information:\ Version:\ Intel(R)\ Xeon(R)\ CPU\ \ \ \ \ \ \ \ \ \ \ X5550\ \ @\ 2.67GHz"
I've adapted nhc-genconf to change the output if two consecutive whitespaces are found:
diff -ruN nhc.orig/nhc-genconf nhc/nhc-genconf
--- nhc.orig/nhc-genconf 2017-11-16 11:38:10.872479034 +0100
+++ nhc/nhc-genconf 2017-11-16 11:37:43.041458469 +0100
@@ -248,7 +248,12 @@
IFS=$' \t\n'
for ((i=0; i<${#LINES[*]}; i++)); do
if mcheck "${LINES[$i]}" "$DMI_MATCH"; then
- echo "# $HOSTNAME || check_dmi_data_match -h $HANDLE ${DMI_TYPE_IDS[$HANDLE]:+-t ${DMI_TYPE_IDS[$HANDLE]}} \"${LINES[i]}\""
+ if [[ "${LINES[$i]}" == *" "* ]]; then
+ ESCAPED=$(echo "${LINES[i]}" | sed 's/\([ ]\)/\\\\\1/g')
+ echo "# $HOSTNAME || check_dmi_data_match -h $HANDLE ${DMI_TYPE_IDS[$HANDLE]:+-t ${DMI_TYPE_IDS[$HANDLE]}} \"${ESCAPED}\""
+ else
+ echo "# $HOSTNAME || check_dmi_data_match -h $HANDLE ${DMI_TYPE_IDS[$HANDLE]:+-t ${DMI_TYPE_IDS[$HANDLE]}} \"${LINES[i]}\""
+ fi
fi
done
done
But maybe this should be fixed at the point where the whitespaces are collapsed due to missing quotation marks?
Roland
I'm trying to evaluate if this is the best solution or if there's some quoting missing somewhere that needs to be fixed.
I've finally come back to look into this: The problem seems to be in the main nhc script, lines 629 and 633:
629: eval $CHECK
633: eval $CHECK &
If CHECK contains multiple consecutive whitespaces, they are removed. Inserting quotes preserves them:
eval "$CHECK"
The effect can be seen quickly like this:
[root@cheops10101 nhc]# A='echo "A B C"'
[root@cheops10101 nhc]# eval $A
A B C
[root@cheops10101 nhc]# eval "$A"
A B C
[root@cheops10101 nhc]#