diff --git a/README.md b/README.md index c07a895..e46b282 100644 --- a/README.md +++ b/README.md @@ -1 +1,47 @@ -# zabbix-slack-notifier \ No newline at end of file +# Zabbix Slack Notifier + +Simple python script to send notifications to Slack using `attachments: []` so that they look pretty in the chat. + +Only tested in Zabbix 3.4 on Ubuntu 14.04 + +## Setup +Put your Slack webhook and url to your Zabbix install in the top of the python script. Lines 9 and 10. You can change the username the messages will seem to come from here too. + +Place the script in the custom alert scripts location. It's defined by the `AlertScriptsPath=` setting in your __zabbix_server.conf__ file. + +Make it executable with `chmod +x`. + +Create a media type in Zabbix that uses the python script and the following three parameters: + +* `{ALERT.SENDTO}` +* `{ALERT.SUBJECT}` +* `{ALERT.MESSAGE}` + +![media_type](img/media-type.png) + + +Add this media to a zabbix user, set the channel you want the messages sent to and at which times and severities. +![user_media](img/user-media.png) + + +Create an action that uses exactly the following as operation and recovery operation default messages. + +Operation + +`{HOST.NAME},,{STATUS},,{TRIGGER.ID},,{TRIGGER.NAME},,{TRIGGER.DESCRIPTION},,{TRIGGER.SEVERITY},,{EVENT.ID},,{EVENT.DATE},,{EVENT.TIME},,` + +Recovery operation + +`{HOST.NAME},,{STATUS},,{TRIGGER.ID},,{TRIGGER.NAME},,{TRIGGER.DESCRIPTION},,{TRIGGER.SEVERITY},,{EVENT.ID},,{EVENT.RECOVERY.DATE},,{EVENT.RECOVERY.TIME},,` + +Set the operations to send to your user from earlier also. You can set the repeat interval and conditions as well. In this example messages will be sent every 15 minutes until the trigger has been acknowledged. + +![action](img/action.png) + +And finally... Wait for the alerts to start coming it. The emoji and colour of the attachment depends on which severity the trigger has or if it's a recovery-event. + +Slack won't show emoji on repeated messages from the same user unless enough time has passed so don't worry if some emoji seem to be missing. + +Clicking on the name of the trigger will take you to the event in the Zabbix web UI. This is why it's defined up top along with the webhook. + +![demo](img/demo.png) diff --git a/img/action.png b/img/action.png new file mode 100644 index 0000000..a02a813 Binary files /dev/null and b/img/action.png differ diff --git a/img/demo.png b/img/demo.png new file mode 100644 index 0000000..935596b Binary files /dev/null and b/img/demo.png differ diff --git a/img/media-type.png b/img/media-type.png new file mode 100644 index 0000000..581aadd Binary files /dev/null and b/img/media-type.png differ diff --git a/img/user-media.png b/img/user-media.png new file mode 100644 index 0000000..b18ca38 Binary files /dev/null and b/img/user-media.png differ diff --git a/slack-notifier.py b/slack-notifier.py new file mode 100644 index 0000000..61a2e07 --- /dev/null +++ b/slack-notifier.py @@ -0,0 +1,94 @@ +#! /usr/bin/env python + +import sys +import requests +import json + + +# basic settings +webhook_url = 'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' +zabbix_url = 'https:///zabbix/' +username = 'zabbix' + + +# data from zabbix +zabbix_to = sys.argv[1] +zabbix_subject = sys.argv[2] +zabbix_body = sys.argv[3] + + +# splitting the message body +splitstring = zabbix_body.split(",,") + + +# selecting the pieces +host_name = splitstring[0] +status = splitstring[1] +trigger_id = splitstring[2] +trigger_name = splitstring[3] +trigger_description = splitstring[4] +trigger_severity = splitstring[5] +event_id = splitstring[6] +event_date = splitstring[7] +event_time = splitstring[8] + + +# make a link +zabbix_link = zabbix_url + "tr_events.php?triggerid=" + trigger_id + "&eventid=" + event_id + + +# join event date and time +event_combined = event_date + " " + event_time + + +# make fallback string +fallback = host_name + " - " + trigger_name + " - " + status + + +# select color and emoji +if status == "OK": + color = "#7bd795" + icon_emoji = ":ok_hand:" +elif trigger_severity == "Information": + color = "#799af8" + icon_emoji = ":information_source:" +elif trigger_severity == "Warning": + color = "#f8c96c" + icon_emoji = ":warning:" +elif trigger_severity == "Average": + color = "#f4a366" + icon_emoji = ":exclamation:" +elif trigger_severity == "High": + color = "#dc7c61" + icon_emoji = ":bangbang:" +elif trigger_severity == "Disaster": + color = "#d6625e" + icon_emoji = ":fire:" +else: + color = "#9aa9b2" + icon_emoji = ":grey_question:" + + +# make json +message_to_slack = json.dumps({"text": zabbix_subject,"channel": zabbix_to,"username": username,"icon_emoji": icon_emoji,"attachments": [{"color": color,"fallback": fallback,"pretext": fallback,"author_name": host_name,"title": trigger_name,"title_link": zabbix_link,"text": trigger_description,"fields": [{"title": "Status","value": status,"short": True},{"title": "Severity","value": trigger_severity,"short": True},{"title": "Time","value": event_combined,"short": True},{"title": "EventID","value": event_id,"short": True}]}]}) + + +# some tests +#f = open('/tmp/myfile.txt', 'w') +#f.write(message_to_slack) +#f.close() + +#print(message_to_slack) + + +# send to slack +response = requests.post( + webhook_url, data=message_to_slack, + headers={'Content-Type': 'application/json'} +) +if response.status_code != 200: + raise ValueError( + 'Request to slack returned an error %s, the response is:\n%s' + % (response.status_code, response.text) + ) +