#!/bin/bash #Enable DEBUG to '1' Debug='0' # Mattermost incoming web-hook URL and user name url='' # example: httpsL//mattermost.example.com/hooks/ere5h9gfbbbk8gdxsei1tt8ewewechjsd username='zabbix' icon='' ## 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 / subject of the message sent by Zabbix; by default, it is usually something like "(Problem|Resolved|Update): Lack of free swap space on Zabbix server" # Message = $3 / message body sent by Zabbix; by default, it is usually approximately 4 lines detailing the specific trigger involved # Alternate URL = $4 (optional) / alternative Slack.com web-hook URL to replace the above hard-coded one; useful when multiple groups have seperate Slack teams # Proxy = $5 (optional) / proxy host including port (such as "example.com:8080") to="${1}" subject="${2}" # 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) message="${subject}\n\n${3}" # Change color emoji depending on the subject - Green (Resolved), Red (Problem) if [[ "$subject" == *"Resolved"* ]]; then emoji=':smile:' color="#1BFF00" elif [[ "$subject" == *"Problem"* ]]; then emoji=':frowning:' color="#ff2a00" elif [[ "$subject" == *"Updated"* ]]; then color="#ccff00" else # Default color emoji=':question:' color="#1258b5" fi # Replace the above hard-coded Mattermost web-hook URL entirely, if one was passed via the optional 4th parameter url=${4-$url} # Use optional 5th parameter as proxy server for curl proxy=${5-""} if [[ "$proxy" != '' ]]; then proxy="-x $proxy" fi ## Debug if [[ "$Debug" == "1" ]]; then echo "to - $to" >> /tmp/SortieMattermost.txt echo "sujet - $subject" >> /tmp/SortieMattermost.txt echo "message - $message" >> /tmp/SortieMattermost.txt echo "........." >> /tmp/SortieMattermost.txt fi # Build our JSON payload and send it as a POST request to the Mattermost incoming web-hook URL payload="payload={\"icon_url\": \"$icon\", \"attachments\": [ {\"color\": \"${color}\", \"text\": \"${message}\"} ], \"channel\": \"${to}\", \"username\": \"${username}\", \"icon_emoji\": \"${emoji}\"}" # Execute the HTTP POST request of the payload to Slack via curl, storing stdout (the response body) return=$(curl $proxy -sm 5 --data-urlencode "${payload}" $url -A 'zabbix-mattermost-alertscript / https://github.com/vincent1890/zabbix-mattermost-alertscript/') # If the response body was not what was expected from Slack ("ok"), something went wrong so print the Slack error to stderr and exit with non-zero if [[ "$return" != 'ok' ]]; then >&2 echo "$return" exit 1 fi