SLAB=76876(cat /proc/meminfo | egrep "Slab:" | awk '{print 768762;}')
the shell parses that into these words
SLAB="76876(cat" /proc/meminfo | egrep "Slab:" | awk '{print 768762;}')
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ .......................................^
Where the "(cat" characters are part of the value for the SLAB temporary environment variable.
Then, the shell is attempting to call /proc/meminfo as a command.
That leaves an unpaired closing parenthesis causing the awk syntax error
I'm unclear what the purpose of SLAB=$$
and the (cat ...)
is. Are you trying to set the SLAB variable to hold a value that concatenates the pid and the result of parenthesized code?
My solution: I'd build the nagios -e
command in pieces:
nagios_cmd=""
for var in Slab MemTotal; do
nagios_cmd+=$(printf '%s="${$}$(awk '\''/%s:/ {print $2}'\'' /proc/meminfo)"; ' $var $var)
done
nagios_cmd+='awk -v s="$Slab" -v m="$MemTotal" '\''BEGIN {print 100*s/m}'\'
declare -p nagios_cmd
This outputs
declare -- nagios_cmd="Slab=\"\${\$}\$(awk '/Slab:/ {print \$2}' /proc/meminfo)\"; MemTotal=\"\${\$}\$(awk '/MemTotal:/ {print \$2}' /proc/meminfo)\"; awk -v s=\"\$Slab\" -v m=\"\$MemTotal\" 'BEGIN {print 100*s/m}'"
which shows you the shell will protect all the chacters that need protecting.
Then you invoke the nagios check like:
/usr/lib64/nagios/plugins/check_generic.pl -n "slab_mem" -e "$nagios_cmd" -w '>50' -c '>80' -p "slab_mem"
# ..........................................................^^^^^^^^^^^^^
That can be written as a single awk command without needing the temporary shell variables:
nagios_cmd='awk -F":" -v pid="$$" '\''
$1 == "Slab" {s = pid $2}
$1 == "MemTotal" {m = pid $2}
END {print 100*s/m}
'\'' /proc/meminfo'
Or, to help with the quoting hell, a here-document:
nagios_cmd=$(cat <<'END_CMD'
awk -F":" -v pid="$$" '
$1 == "Slab" {s = pid $2}
$1 == "MemTotal" {m = pid $2}
END {print 100*s/m}
' /proc/meminfo
END_CMD
)