I’ve been using Slack and Zabbix now for a few years, and figured since there’s not too many guides out there on how to integrate the two easily. I know there are a few examples so far, but this is what’s worked best for me.
First I use this script, and put it in your alertscripts directory, ex. /usr/lib/zabbix/alertscripts .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#!/usr/bin/python import sys import yaml from slackclient import SlackClient slack_config_file = '/etc/zabbix/slack.yml' with open(slack_config_file, 'r') as ymlfile: slack_config = yaml.load(ymlfile) slack = SlackClient(slack_config['slack_api_token']) channel = sys.argv[1] message = "```Status: %s\n%s```" %(sys.argv[2], sys.argv[3]) response = send_slack_message(channel, message) if response["ok"]: print("Message posted successfully: " + response["message"]["ts"]) # If the message failed, check for rate limit headers in the response elif response["ok"] is False and response["headers"]["Retry-After"]: # The `Retry-After` header will tell you how long to wait before retrying delay = int(response["headers"]["Retry-After"]) print("Rate limited. Retrying in " + str(delay) + " seconds") time.sleep(delay) slack.api_call("chat.postMessage", channel=channel, text=message) |
Notice there’s no field for credentials. That’s because over time I found it easier to deploy with a configuration file that was in yaml format.
Make sure you create an API token for slack by creating a Slack App, then using it’s Verification Token. Hopefully in the future I revisit this and make use of the Client ID, Client Secret, Signing Secret configuration.
So to provide that file we need to create the following in your zabbix configuration directory ex. /ext/zabbix/slack.yml .
1 2 |
# Zabbix - Slack Configuration slack_api_token: {{ zabbix_slack_api_token }} |
This makes it easier at least for me to deploy and separate configuration from the script itself.
Next steps include creating the Slack Media Type.
Navigate to Administration > Media types
Select “Create media type”, and fill out the following:
1 2 3 4 5 6 7 8 |
Name: Slack Type: Script Script name: zabbix_slack.py Script parameters: {ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE} Enabled: Checked |
Don’t forget to save!
Your first value is going to be “Channel” the rest fill in the message details.