Skip to content
This repository was archived by the owner on Dec 18, 2019. It is now read-only.

Add ability to embed graphite graphs in emails #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/analyzer/alerters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import alerters
import settings

import urllib2


"""
Create any alerter you want here. The function will be invoked from trigger_alert.
Expand Down Expand Up @@ -34,14 +36,36 @@ def alert_smtp(alert, metric):
if type(recipients) is str:
recipients = [recipients]

link = '%s/render/?width=588&height=308&target=%s' % (settings.GRAPHITE_HOST, metric[1])
content_id = metric[1]
image_data = None
if settings.SMTP_OPTS.get('embed-images'):
try:
image_data = urllib2.urlopen(link).read()
except urllib2.URLError:
image_data = None

# If we failed to get the image or if it was explicitly disabled,
# use the image URL instead of the content.
if image_data is None:
img_tag = '<img src="%s"/>' % link
else:
img_tag = '<img src="cid:%s"/>' % content_id

body = 'Anomalous value: %s <br> Next alert in: %s seconds <br> <a href="%s">%s</a>' % (metric[0], alert[2], link, img_tag)

for recipient in recipients:
msg = MIMEMultipart('alternative')
msg['Subject'] = '[skyline alert] ' + metric[1]
msg['From'] = sender
msg['To'] = recipient
link = '%s/render/?width=588&height=308&target=%s' % (settings.GRAPHITE_HOST, metric[1])
body = 'Anomalous value: %s <br> Next alert in: %s seconds <a href="%s"><img src="%s"/></a>' % (metric[0], alert[2], link, link)

msg.attach(MIMEText(body, 'html'))
if image_data is not None:
msg_attachment = MIMEImage(image_data)
msg_attachment.add_header('Content-ID', '<%s>' % content_id)
msg.attach(msg_attachment)

s = SMTP('127.0.0.1')
s.sendmail(sender, recipient, msg.as_string())
s.quit()
Expand Down
1 change: 1 addition & 0 deletions src/settings.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ SMTP_OPTS = {
"recipients": {
"skyline": ["[email protected]", "[email protected]"],
},
"embed-images": False
}

# HipChat alerts require python-simple-hipchat
Expand Down