Zabbix integration with Slack Debian

Zabbix integration with Slack with AlertScript

This is simply a Bash script that uses the custom alert script functionality within Zabbix along with the incoming web-hook feature of Slack that I got a chance to write since I could not find any already existing/similar scripts.

Versions

This process is working with Zabbix 1.8.x or greater – even 2.2, 2.4 and 3.x.x.

Installation

The script itself

This slack.sh script needs to be placed in the AlertScriptsPath directory that is specified within the Zabbix servers’ configuration file (zabbix_server.conf) and must be executable by the user running the zabbix_server binary (usually “zabbix”) on the Zabbix server:

[root@zabbix-server ~]# grep AlertScriptsPath /etc/zabbix/zabbix_server.conf
### Option: AlertScriptsPath
AlertScriptsPath=/usr/lib/zabbix/alertscripts/

[root@zabbix ~]# ls -lh /usr/lib/zabbix/alertscripts/slack.sh
-rwxr-xr-x 1 root root 1.4K Jan 20 01:48 /usr/lib/zabbix/alertscripts/slack.sh

If you do change AlertScriptsPath (or any other values) within zabbix_server.conf, a restart of the Zabbix server software is required.

 

Script:

#!/bin/bash

# Slack incoming web-hook URL and user name
url=’https://hooks.slack.com/services/7dwdwxwY/D34DC0D3/BCADFGabcDvdvd7′
username=’Zabbix’

## 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)
# 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)
to=”$1″
subject=”$2″

# Change message emoji depending on the subject – smile (RECOVERY), frowning (PROBLEM), or ghost (for everything else)
recoversub=’^RECOVER(Y|ED)?$’
if [[ “$subject” =~ ${recoversub} ]]; 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 ‘zabbix-slack-alertscript / https://github.com/ericoc/zabbix-slack-alertscript’

 

Configuration

Slack.com web-hook

An incoming web-hook integration must be created within your Slack.com account which can be done at https://my.slack.com/services/new/incoming-webhook as shown below:

Slack.com Incoming Web-hook Integration

Given the above screenshot, the incoming web-hook URL would be:

https://hooks.slack.com/services/7dwdwxwY/D34DC0D3/BCADFGabcDvdvd7

Make sure that you specify your correct Slack.com incoming web-hook URL and feel free to edit the sender user name at the top of the script:

# Slack incoming web-hook URL and user name
url='https://hooks.slack.com/services/QW3R7Y/D34DC0D3/BCADFGabcDEF123'
username='Zabbix'

Within the Zabbix web interface

When logged in to the Zabbix servers web interface with super-administrator privileges, navigate to the “Administration” tab, access the “Media Types” sub-tab, and click the “Create media type” button.

You need to create a media type as follows:

  • Name: Slack
  • Type: Script
  • Script name: slack.sh

…and ensure that it is enabled before clicking “Save”, like so:

Zabbix Media Type

However, on Zabbix 3.x and greater, media types are configured slightly differently and you must explicity define the parameters sent to the slack.sh script. On Zabbix 3.x, three script parameters should be added as follows:

  • {ALERT.SENDTO}
  • {ALERT.SUBJECT}
  • {ALERT.MESSAGE}

…as shown here:

Zabbix 3.x Media Type

Then, create a “Slack” user on the “Users” sub-tab of the “Administration” tab within the Zabbix servers web interface and specify this users “Media” as the “Slack” media type that was just created with the Slack.com channel (“#alerts” in the example) or user name (such as “@ericoc”) that you want messages to go to in the “Send to” field as seen below:

 

Zabbix > Administration > Users > Create user > User

 

Zabbix > Administration > Create User > Users > Media >

Zabbix > Administration > Create User > Users > Permissions >

Finally, an action can then be created on the “Actions” sub-tab of the “Configuration” tab within the Zabbix servers web interface to notify the Zabbix “Slack” user ensuring that the “Subject” is “PROBLEM” for “Default message” and “RECOVERY” should you choose to send a “Recovery message”.

Zabbix > Configuration Action > Create Action

 

Zabbix > Configuration Action > Create Action > Operations

 

In operations tab there is an option click add and select user slack from the list.

You can customize Default Subject and Default Message according to your preferences. You can use mine:

TRIGGER-STATUS: {TRIGGER.STATUS} TRIGGER NAME: {TRIGGER.NAME}

TRIGGER SEVERITY: {TRIGGER.SEVERITY}
ISSUE: {ITEM.NAME1} HOSTNAME: ({HOST.NAME1})
————————————————————————————————————-

Similar Template can be used in recovery tab.

Additionally, you can have multiple different Zabbix users each with “Slack” media types that notify unique Slack users or channels upon different triggered Zabbix actions.

Testing

Assuming that you have set a valid Slack web-hook URL within your “slack.sh” file, you can execute the script manually (as opposed to via Zabbix) from Bash on a terminal:

$ bash slack.sh '@ericoc' PROBLEM 'Oh no! Something is wrong!'

Alerting a specific user name results in the message actually coming from the “slackbot” user using a sort-of “spoofed” user name within the message. A channel alert is sent as you would normally expect from whatever user name you specify in “slack.sh”:

 

Source: https://github.com/ericoc/zabbix-slack-alertscript

Leave a Reply

Your email address will not be published.


*