This repository was archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtornado_slack_plugin.py
More file actions
65 lines (61 loc) · 2.57 KB
/
tornado_slack_plugin.py
File metadata and controls
65 lines (61 loc) · 2.57 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
import json
import tornado.web
#Slack settings for the user to change
#Slack Webhook key from https://YOURSITE.slack.com/services/new/incoming-webhook
auth_token = ""
#Slack room ID
room_id = "#general"
#Base Slack URL for your team. Must include a trailing slash
slack_base_url = "https://YOURSITE.slack.com/"
#Username that will show in the Slack room
slack_user = "CirconusBot"
#Circonus API Information
circonus_api_name = ""
circonus_api_key = ""
class SlackHandler(tornado.web.RequestHandler):
def post(self):
#Check to make sure settings are in place
if not auth_token:
print "ERROR: auth_token not set in tornado_slack_plugin"
elif not room_id:
print "ERROR: room_id not set in tornado_slack_plugin"
elif "YOURSITE" in slack_base_url:
print "ERROR: URL not properly setup in tornado_slack_plugin"
else:
data = json.loads(self.request.body)
print data
payload = {'room_id':room_id, 'username':slack_user}
url = slack_base_url+"services/hooks/incoming-webhook?token="+auth_token
#Handle the message if someone clicked 'Send Message' from Circonus UI
if data['alerts'][0]['metric_name'] == 'dummy_metric_name':
payload['text'] = data['alerts'][0]['alert_value']
r = requests.post(url, data=json.dumps(payload))
r.raise_for_status()
else:
#Handle alerts that occur from ruleset thresholds
for alert in data['alerts']:
#If Circonus API details were provided, get the check information from the alert
check_bundle = None
if circonus_api_name and circonus_api_key:
check_bundle = requests.get(
"https://api.circonus.com/check_bundle?f__checks_has=/check/"+str(alert['check_id']),
auth=(circonus_api_name,circonus_api_key),
headers={"Accept": "application/json"}
).json()
if 'clear_value' in alert:
message = 'CLEARED - '+alert['check_name']+' - '+alert['host']+' - '+alert['metric_name']+' - '+'Clear Value = '+alert['clear_value']+' - '+alert['alert_url']
else:
message = 'SEVERITY '+alert['severity']+' - '+alert['check_name']+' - '+alert['host']+' - '+alert['metric_name']+' - '+'Value = '+alert['alert_value']+' - '+alert['alert_url']
#Append tag information to message if we looked up the check_bundle
if check_bundle:
if check_bundle[0]['tags']:
message += ' - TAGS = '
for tag in check_bundle[0]['tags']:
message += tag+', '
#Remove trailing comma
if message[-2:] == ', ':
message = message[:-2]
payload['text'] = message
r = requests.post(url, data=json.dumps(payload))
r.raise_for_status()