Creating a New Media Type - Slack

Published: 05 July 2019
on channel: SBCODE
4,177
40

For more info on my Zabbix Course visit https://sbcode.net/zabbix/

There are two parts to this lecture,

1. Creating the Slack Hook that will accept messages

2. Creating the Script Media Type that sends messages

First part 1,

Create or log into your slack workspace, create a channel called Zabbix, or other name,

Go down to the 'Apps' section and press the + symbol.

Select 'Manage Apps'

Select 'Build'

Select 'Start Buiilding'

Name the new Slack App 'Zabbix' or other name,

then select to add it to your desired workspace.

Select 'Incoming Webhooks'

Activate it

Click the 'Add new webhook to the workspace' button,

select the channel to post to. I chose my workspace channel which is also named 'Zabbix'

A sample curl script is then created pre filled with your Slack Hook url and tokens.

Copy the example curl command.

Now part 2, create the Script Media Type

Find the zabbix alertscripts path.

By default it is /usr/lib/zabbix/alertscripts

In that folder, create a new file called slackalert.sh

Copy the curl command you copied from slack into the new slackalert.sh file, and replace

--data '{"text":"Hello World!"}'

with

--data '{"text":"'"$1"'"}'

Also add the line at the begining,

!#/bin/bash

Your complete file will be something like this

-----------------------------------------

#!/bin/bash

curl -X POST -H 'Content-type: application/json' --data '{"text":"'"$1"'"}' https://hooks.slack.com/services/[your slack hook tokens]

------------------------------------------

Save, then give the file execute permissions,

$ chmod a+x /usr/lib/zabbix/alertscripts/slackalert.sh

Go back into Zabbix and create a new media type,

Name = 'Slack Alert'

Type = 'Script'

Script Name = slackalert.sh

Script Parameters = {ALERT.SUBJECT}

You can now test that using the 'test' option for the new media type entry.

Open your slack channel on your browser and/or mobile phone and see the real time alerts.

Note that the slackalert.sh script is only passing the {ALERT.SUBJECT} value, so the 'Send To' and 'Message' test fields are ignored.