Skip to content

lorenzo-biava/zabbix-slack-alertscript

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Zabbix Slack AlertScript

About

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.

This definitely works with Zabbix 2.0 or greater (including 2.2 and 2.4) and thanks to Leslie at AspirationHosting for confirming that it works on 1.8.2 as well!

Thanks to Paul Reeves for the hint that Slack changed their API/URLs recently as well as to Igor Shishkin for the ability to message users as well as channels!

Installation

The script itself

The "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 (usually "zabbix") running the zabbix_server binary on the Zabbix server before restarting the Zabbix server software:

[root@zabbix ~]# grep AlertScriptsPath /etc/zabbix/zabbix_server.conf
### Option: AlertScriptsPath
AlertScriptsPath=/usr/local/share/zabbix/alertscripts

[root@zabbix ~]# ls -lh /usr/local/share/zabbix/alertscripts/slack.sh
-rwxr-xr-x 1 root root 1.4K Dec 27 13:48 /usr/local/share/zabbix/alertscripts/slack.sh

Feel free to edit the user name at the top of the script while making sure that you specify your correct Slack.com incoming web-hook URL:

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

The message is sent using the form of an attachment and a link to the Zabbix Console can be specified:

# We might also want to put a link to the Zabbix Console
zabbix_url="https://ZABBIX_HOST:PORT/zabbix"
link="$zabbix_url/tr_status.php"	# For example, to the Triggers page

The result is the following:

Zabbix example message attachment

Please, note that the icon (aka the emoji) can be set in Slack's Webhook configuration page.

At Slack.com

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/QW3R7Y/D34DC0D3/BCADFGabcDEF123".

Within the Zabbix web interface

When logged in to the Zabbix servers web interface as a user with super-administrator privileges, click the "Create media type" button on the "Media types" sub-tab of the "Administration" tab.

You need to create a media type with the name "Slack", type of "Script", script name of "slack.sh" that is enabled like so:

Zabbix 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 User

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". Additionally, you can have multiple different Zabbix users with "Slack" media types that each notify unique Slack users or channels upon different Zabbix actions.

Keeping the messages short is probably a good idea - use something such as "{TRIGGER.NAME} - {HOSTNAME} ({IPADDRESS})" for the contents of each message.

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":

Slack Testing

More Information

About

Zabbix AlertScript for Slack.com chat

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%