Nagios bash syntax error

I want to refer with this question to the following post where I got the Skript:
Post with Script

I’m using Bash 4.1.2(1) and when I try to execute the script, I get following error messages:

 ./check_disk_by_size.sh: Zeile 9: [: ==: Einstelliger (unärer) Operator erwartet.
(standard_in) 1: syntax error
./check_disk_by_size.sh: Zeile 13: [: -eq: Einstelliger (unärer) Operator erwartet.
(standard_in) 1: syntax error
(standard_in) 1: syntax error
./check_disk_by_size.sh: Zeile 16: [: Zu viele Argumente.
(standard_in) 1: syntax error
./check_disk_by_size.sh: Zeile 19: [: -eq: Einstelliger (unärer) Operator erwartet.

I have basic Bash-scripting experiences but whenever I change something, I get even more errors. I’m not able to bring it to work.

EDIT 1: Here’s the script

#!/bin/bash

FREESPACE=`/usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s PASSWORD \
-v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'`

SIZE=`echo $FREESPACE | awk '{ print $2 }'`
UNIT=`echo $FREESPACE | awk '{ print $3 }'`

if [ $UNIT == "Gb" ]; then
    SIZE=`echo $SIZE \* 1024 | bc`
fi

if [ `echo "$SIZE >= $6" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space OK - $FREESPACE"
    exit 0
elif [ `echo "$SIZE < $6" | bc` -eq 1 -a `echo "$SIZE > $8" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space WARNING - $FREESPACE"
    exit 1
elif [ `echo "$SIZE <= $8" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space CRITICAL - $FREESPACE"
    exit 2
fi

I run the script like this:

./check_disk_by_size -H [IP_Adress] -l [DRIVE] -w [INTEGER] -c [INTEGER] -p [PORT] -s [PASSWORD]

Example:

./check_disk_by_size -H 192.168.1.110 -l c -w 10240 -c 8192 -p 12489 -s PASSWORD

UPDATE

Thanks for your help!
It works now 🙂 The script looks like this now:

#!/bin/bash
#

FREESPACE=`/usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s Nag4AlphA \
-v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'`

SIZE=`echo $FREESPACE | awk '{ print $2 }'`
UNIT=`echo $FREESPACE | awk '{ print $3 }'`

if [ "$UNIT" == "Gb" ]; then
    SIZE="$(echo "$(($SIZE * 1024))" | bc)"
fi


echo $SIZE
echo " " 
echo $6
echo " "
echo $8

if [ $SIZE -ge $6 ]; then
    echo "$4:\_Drive_Space OK - $FREESPACE"
    exit 0
elif [ $SIZE -lt $6 -a $SIZE -gt $8 ]; then
    echo "$4:\_Drive_Space WARNING - $FREESPACE"
    exit 1
elif [ $SIZE -lt $8 ]; then
    echo "$4:\_Drive_Space CRITICAL - $FREESPACE"
    exit 2
fi

Answer

To me it looks like the left operand in the “if test” is empty.

About why there might be an empty value, I suggest you to run the Nagios command from the shell and see what it does. What’s its output?
Run all the steps to the output:

  1. /usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s PASSWORD -v USEDDISKSPACE -l $4

  2. /usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s PASSWORD
    -v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }'

  3. /usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s PASSWORD
    -v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'

About the script itself I “kindly suggest” to:

  • ALWAYS enclose variables in double quotes, EVERYWHERE
  • ALWAYS prefer $() instead of the backticks for shell expansion, EVERYWHERE
  • ALWAYS enclose shell expansion in double quotes, EVERYWHERE

Of course seldom exceptions do apply, like when you test numeric values.

I don’t mean to be rude, just make sure you understand how important this is 🙂

Orignal:

if [ $UNIT == "Gb" ]; then
    SIZE=`echo $SIZE \* 1024 | bc`
fi

Hardened:

if [ "$UNIT" == "Gb" ]; then
    SIZE="$(echo "$SIZE * 1024" | bc)"
fi

I never ever had any problem after I started to consistently follow these rules.

Here is a nice article about Bash scripting best practices: http://www.davidpashley.com/articles/writing-robust-shell-scripts/ and another one about shell expansion http://mywiki.wooledge.org/BashFAQ/082

UDPATE: corrected few typos

Attribution
Source : Link , Question Author : Landapanda , Answer Author : ColOfAbRiX

Leave a Comment