Bash – nagios bash script/command returning null

bashnagios

I wrote a simple nagios command to check the change of a value over 1 second

#!/bin/bash
HOSTNAME=$1
COMMUNITY=$2
OID=$3
WAITTIME=1
echo "/usr/lib/nagios/plugins/check_snmp -H $HOSTNAME -C $COMMUNITY -o $OID" > /tmp/csnmp_comand
VAR1=`/usr/lib/nagios/plugins/check_snmp -H $HOSTNAME -C $COMMUNITY -o $OID|cut -d"=" -f2|sed 's/[a-Z]//'`
/bin/sleep $WAITTIME
VAR2=`/usr/lib/nagios/plugins/check_snmp -H $HOSTNAME -C $COMMUNITY -o $OID|cut -d"=" -f2|sed 's/[a-Z]//'`
CHANGED=`/usr/bin/expr $VAR2 - $VAR1`
BPS=`/usr/bin/expr $CHANGED / $WAITTIME`
echo "OK $BPS|bps=$BPS" > /tmp/check_snmptest
echo "OK $BPS|bps=$BPS"
exit 0

And I wrote a service calling this script

define command{
    command_name    snmp_cps
    command_line     /usr/lib/nagios/plugins/check_cps '$HOSTADDRESS$' '$_HOSTSNMPCOMMUNITY$' '$ARG1'
}

When I call The script manually ( Even as the nagios user ) I get

OK 233|bps=233 

Or something like that, but when I schedule this command it returns null and sets it to critical

Also My host.cfg is

define host{
    use     generic-host
    host_name   asa5505.customer.local
    alias       Options ASA 5505
    address     asa5505.customer.local
    _SNMPCOMMUNITY  SetSecurly
}
define service {
        use                             generic-service
        host_name                       asa5505.customer.local
        service_description             Outside Interface PBS
        check_command                   snmp_cps!1.3.6.1.2.1.2.2.1.10.16
}

With a few changes ( hostname and snmpcommunity )

Best Answer

You lost the trailing $ on $ARG1$, so the OID is being completely ignored (Nagios will pass $ARG1 on to the shell, which will dereference it to "").

You should add some sanity checking to $1, $2, and $3 in the script to prevent this in the future.