I've been attempting to implement an alert script for Zabbix. Zabbix attempts to run the script in Shell for some reason, whilst the script is written in Bash.
#!/bin/bash
# Slack incoming web-hook URL and user name
url='https://hooks.slack.com/services/this/is/my/webhook/' # example: https://hooks.slack.com/services/QW3R7Y/D34DC0D3/BCADFGabcDEF123
username='Zabbix Notification System'
## Values received by this script:
# To = $1 (Slack channel or user to send the message to, specified in the Zabbix web interface; "@username" or "#channel")
# Subject = $2 (usually either PROBLEM or RECOVERY/OK)
# Message = $3 (whatever message the Zabbix action sends, preferably something like "Zabbix server is unreachable for 5 minutes - Zabbix server (127.0.0.1)")
# Get the Slack channel or user ($1) and Zabbix subject ($2 - hopefully either PROBLEM or RECOVERY/OK)
to="$1"
subject="$2"
# Change message emoji depending on the subject - smile (RECOVERY/OK), frowning (PROBLEM), or ghost (for everything else)
recoversub='^RECOVER(Y|ED)?$'
if [[ "$subject" =~ ${recoversub} ]]; then
emoji=':smile:'
elif [ "$subject" == 'OK' ]; then
emoji=':smile:'
elif [ "$subject" == 'PROBLEM' ]; then
emoji=':frowning:'
else
emoji=':ghost:'
fi
# The message that we want to send to Slack is the "subject" value ($2 / $subject - that we got earlier)
# followed by the message that Zabbix actually sent us ($3)
message="${subject}: $3"
# Build our JSON payload and send it as a POST request to the Slack incoming web-hook URL
payload="payload={\"channel\": \"${to//\"/\\\"}\", \"username\": \"${username//\"/\\\"}\", \"text\": \"${message//\"/\\\"}\", \"icon_emoji\": \"${emoji}\"}"
curl -m 5 --data-urlencode "${payload}" $url -A "https://hooks.slack.com/services/this/is/my/web/hook"
~
When I run the script locally using 'bash slack.sh' it sends an empty notification, which I recieve in Slack. When I run the script locally using 'sh slack.sh' I get the following error.
slack.sh: 19: slack.sh: [[: not found
slack.sh: 21: [: unexpected operator
slack.sh: 23: [: unexpected operator
slack.sh: 34: slack.sh: Bad substitution
Thanks for the assistance.
You can force your script to run with bash by adding
#! /bin/bash
if [ -z "$BASH" ]
then
exec /bin/bash "$0" "$@"
fi
...