Skip to content

Instantly share code, notes, and snippets.

@tatsuyaueda
Created November 5, 2018 01:48
Show Gist options
  • Save tatsuyaueda/7cc750856cc236fdfb09b6a4f3a2638f to your computer and use it in GitHub Desktop.
Save tatsuyaueda/7cc750856cc236fdfb09b6a4f3a2638f to your computer and use it in GitHub Desktop.
ZABBIX Trigger -> Mattermost
#!/bin/sh
# Mattermost incoming web-hook URL and user name
url='https://mattermost.example.com/hooks/xxxxxxxxxx'
username='ZABBIX'
icon='https://jujucharms.com/_icon/120/zabbix-frontend.png'
## Values received by this script:
# To = $1 (Mattermost channel or user to send the message to, specified in the Zabbix web interface; "@username" or "#channel")
# Subject = $2 (usually either PROBLEM or RECOVERY)
# 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 Mattermost channel or user ($1) and Zabbix subject ($2 - hopefully either PROBLEM or RECOVERY)
to="$1"
title="$2"
# Replace NewLine Code
message="$(echo $3 | tr \\r \\n)"
type="$(echo $title | cut -f1 -d:)"
# Change color emoji depending on the subject - Green (RECOVERY), Red (PROBLEM)
if [ "$type" == 'OK' ]; then
emoji=':laughing:'
color="#00ff33"
elif [ "$type" == 'PROBLEM' ]; then
emoji=':fearful:'
color="#ff2a00"
fi
# The message that we want to send to Mattermost is the "subject" value ($2 / $subject - that we got earlier)
# followed by the message that Zabbix actually sent us ($3)
text="$emoji\n$message"
# Build our JSON payload and send it as a POST request to the Mattermost incoming web-hook URL
payload="payload={\"channel\": \"$to\", \"icon_url\": \"$icon\", \"attachments\": [ {\"color\": \"${color}\", \"title\": \"${title}\", \"text\": \"${text}\"} ], \"username\": \"${username}\"}"
/usr/local/bin/curl -s -m 5 -k --data-urlencode "${payload}" $url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment