forked from uakfdotb/pybearmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.py
92 lines (69 loc) · 2.7 KB
/
alerts.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import util
def email(data, context):
'''
Sends an email to target with the given parameters.
From address and email method are specified in configuration.
data['email']: the email address to send to
context['title']: email subject
context['message']: email body (plaintext)
'''
from config import config
if 'email' not in data:
util.die('alerts.email: missing email')
import email.utils
from_address = email.utils.parseaddr(config['mail_from'])[1]
if not from_address:
util.die('alerts.email: invalid from address [%s] specified in configuration' % (config['mail_from'],))
to_address = email.utils.parseaddr(data['email'])[1]
if not to_address:
util.die('alerts.email: invalid to address [%s] contact' % (data['email'],))
if config['mail_ssl']:
from smtplib import SMTP_SSL as SMTP
else:
from smtplib import SMTP
from email.MIMEText import MIMEText
conn = SMTP(config['mail_host'], config['mail_port'])
conn.login(config['mail_username'], config['mail_password'])
try:
msg = MIMEText(context['message'], 'plain')
msg['From'] = from_address
msg['To'] = to_address
msg['Subject'] = context['title']
conn.sendmail(from_address, to_address, msg.as_string())
finally:
conn.close()
def sms_twilio(data, context):
'''
Sends an SMS message via Twilio to the given number.
The message is "[title] message"
config must include the strings twilio_accountsid, twilio_authtoken, and twilio_number
data['number']: phone number to send SMS message to.
data['twilio_accountsid'], data['twilio_authtoken'], data['twilio_number']: optional Twilio configuration
context['title']: used in creating SMS message
context['message']: used in creating SMS message
'''
from config import config
from twilio.rest import TwilioRestClient
if 'number' not in data:
util.die('alert_sms_twilio: missing number')
config_target = config
if 'twilio_accountsid' in data and 'twilio_authtoken' in data and 'twilio_number' in data:
config_target = data
sms_message = "[%s] %s" % (context['title'], context['message'])
client = TwilioRestClient(config_target['twilio_accountsid'], config_target['twilio_authtoken'])
message = client.messages.create(body = sms_message, to = data['number'], from_ = config_target['twilio_number'])
def http(data, context):
'''
Notifies a web hook over HTTP regarding alert.
All context parameters are sent, via GET.
data['url']: the target web hook
context: encoded as GET parameters
'''
if 'url' not in data:
util.die('alert_http: missing url')
import urllib
import checks
url = data['url'] + '?' + urllib.urlencode(context)
result = checks.http_helper({'url': url})
if result['status'] == 'fail' and 'message' in result:
print "alert_http: error: " + result['message']