Last active
November 20, 2019 14:06
-
-
Save rukeba/9f0de5b6679dc2922c3fe2646bc4cb8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Repository: https://github.com/h-otter/zabbix-slackpy | |
from json import loads | |
from json import dumps | |
from sys import argv | |
import re | |
def send_trigger(message, slack_hook): | |
""" | |
On zabbix, set actions to architect message like this | |
{ | |
"date": "{DATE} / {TIME}", | |
"host": "{HOST.NAME}", | |
"name": "{TRIGGER.NAME}", | |
"url": "{TRIGGER.URL}", | |
"status": "{TRIGGER.STATUS}", | |
"triage": "{TRIGGER.SEVERITY}", | |
"item_name": "{ITEM.NAME}", | |
"item_value": "{ITEM.VALUE}" | |
} | |
""" | |
title = "%s: %s" % (message["status"], message["name"]) | |
if message["status"] == "PROBLEM": | |
if message["triage"] == "Disaster": | |
color = "#e45959" | |
elif message["triage"] == "High": | |
color = "#e97659" | |
elif message["triage"] == "Average": | |
color = "#ffa059" | |
elif message["triage"] == "Warning": | |
color = "#ffc859" | |
elif message["triage"] == "Information": | |
color = "#7499ff" | |
else: | |
color = "#97aab3" | |
elif message["status"] == 'OK': | |
if message["triage"] in {"Disaster", "High", "Average", "Warning"}: | |
color = "good" | |
else: | |
return | |
else: | |
raise Exception("Input other status") | |
payload = { | |
"attachments": [ | |
{ | |
"fallback": "%s - %s." % (message["date"], title), | |
"color": color, | |
"author_name": message["triage"], | |
"title": title, | |
"title_link": message["url"], | |
"fields": [ | |
{ | |
"title": "Data", | |
"value": message["date"], | |
"short": True | |
}, | |
{ | |
"title": "Host", | |
"value": message["host"], | |
"short": True | |
}, | |
{ | |
"title": "Detail", | |
"value": "%s: %s" % (message["item_name"], message["item_value"]), | |
"short": True | |
}, | |
], | |
} | |
] | |
} | |
value = dumps(payload).encode("utf-8") | |
headers = {'Content-type':'application/json'} | |
try: | |
import urllib2 as urllib_req | |
except ImportError: | |
import urllib.request as urllib_req | |
req = urllib_req.Request(slack_hook, | |
data=value, | |
headers=headers) | |
return urllib_req.urlopen(req).read() | |
def fix_json(s): | |
s = s.replace('\\', '\\\\') | |
lines = s.split('\n') | |
s = '' | |
for l in lines: | |
l = (l or '').strip('\n\r') | |
if not l: | |
s += '\\n' | |
else: | |
newline = '\n' | |
m1 = re.search(r'[^{},"]$', l) | |
if m1: | |
newline = '\\n' | |
s += (l or '').strip() + newline | |
lines = s.split('\n') | |
s = '' | |
for l in lines: | |
# check quotes | |
m2 = re.match(r'("[^"]+"):\s*"(.+".+)"(,?)', l) | |
if m2: | |
s += m2.group(1) + ': "' + m2.group(2).replace('"', '\\"') + '"' + m2.group(3) + '\n' | |
else: | |
s += (l or '').strip() + '\n' | |
return s | |
if __name__ == "__main__": | |
""" | |
On zabbix, set media type for slack like these | |
- parameters | |
- {ALERT.SENDTO} -> set Slack hook URL on user configuration | |
- {ALERT.MESSAGE} | |
""" | |
debug_s = r"""{ | |
"date": "2019.11.20 / 13:34:47", | |
"host": "DEV_Server", | |
"name": "AWS SES Log Error", | |
"url": "https://zabbix.4ib.co/zabbix/zabbix.php?action=dashboard.view", | |
"status": "PROBLEM | |
ksfjhkdfj "site" kdjfhvnkhf | |
dkfvkfvkwdfvb ", | |
"triage": "High", | |
"item_name": "AWS SES log", | |
"item_value": "2019-11-20 08:34:03,362 ERROR health-check - Exception while send email - check logs 'D:\\PRODUCTION\\Log\\amazonsesservice\\log4net.*.log'" | |
}""" | |
argvs = argv | |
s2 = argvs[2] | |
#s2 = debug_s | |
print(s2) | |
s2 = fix_json(s2) | |
print(s2) | |
loaded = loads(s2) | |
print(loaded) | |
slack_hook = argvs[1] | |
send_trigger(loaded, slack_hook) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment