0

we are running a bash script on a unix-server to check a value against a treshold and sent the result to a monitoring system via snmp. The goal is to sent a defined alarmtext to the monitoring infrastructure if the value is greater than the defined treshold.

When running a debug of this script everything works fine and the correct alarmtext is displayed via echo on the cli.

Scriptsnippet:

## LOGIC ##
if [[ ${ActiveRecipientsQueue} -gt ${Threshold} ]]; then
        echo "$AlarmText";
        exit 1;
else
        echo "$NoAlarmText";
        exit 0;
fi

Result of debug - scenario threshold reached:

+ [[ 15 -gt 5 ]]
+ echo 'Treshsold was reached!'
Treshsold was reached!
+ exit 1

Result of debug - scenario everything ok:

+ [[ 15 -gt 500 ]]
+ echo 'Everything is fine'
Everything is fine
+ exit 0

The issue is now that on the monitoring infrastructure everytime the "everything ok" message is displayed, no matter if the threshold was reached or not.

I am still confused about this and can't find any logical explanation for this.

May you can help me?

Thanks a lot!

  • Use only one bracket! `[ $a -gt $b ]` – F. Hauri - Give Up GitHub Jan 16 '20 at 10:19
  • Does this answer your question? https://stackoverflow.com/questions/18668556/comparing-numbers-in-bash – samthegolden Jan 16 '20 at 10:24
  • Are you sure the monitoring infrastructure runs the same version of the script? Can you add some debugging into it, at least show the values being compared? – choroba Jan 16 '20 at 10:26
  • 1
    Are you sure that it's being run by the same shell in both environments? Perhaps stick to standard (POSIX) shell features? i.e. `[ "$a" -gt "$b" ]`. BTW, you misspelt *threshold*. – Toby Speight Jan 16 '20 at 10:31
  • To add to Toby Speight's comment, if your monitoring infrastructure is using some other POSIX shell rather than `bash`, `[[` is not recognized as a valid command, leading to a failure that your script interprets as a comparison that evaluates to false. Using `[ "$a" -gt "$b" ]`, as suggested by F. Hauri, should work. – chepner Jan 16 '20 at 14:04
  • @smuellert : To catch what chepner suspected: Did you verify that nothing is written on STDERR? Also, to debug the problem, I would include the values of `ActiveRecipientsQueue`, `Threshold` and `BASH_VERSION` into the ok-message. The latter also helps to verify that you are indeed running bash. – user1934428 Jan 16 '20 at 14:12

0 Answers0