-
Notifications
You must be signed in to change notification settings - Fork 1
/
receiver.py
44 lines (31 loc) · 1.3 KB
/
receiver.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
#!/usr/bin/env python
import pika
import smtplib
import config
def send_email(configuration, subject, message):
from_addr = configuration['from']
to_addr_list = configuration['to']
cc_addr_list = configuration.get('cc', list())
header = 'From: %s\n' % from_addr
header += 'To: %s\n' % ','.join(to_addr_list)
header += 'Cc: %s\n' % ','.join(cc_addr_list)
header += 'Subject: %s\n\n' % subject
message = header + message
server = smtplib.SMTP(configuration['smtp_server'])
server.starttls()
server.login(configuration['username'], configuration['password'])
errors = server.sendmail(from_addr, to_addr_list, message)
server.quit()
def callback(ch, method, properties, body):
print " [x] %r" % (body,)
send_email(config.report['email'], 'Error', body)
if __name__ == "__main__":
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', type='fanout')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='logs', queue=queue_name)
print ' [*] Waiting for logs. To exit press CTRL+C'
channel.basic_consume(callback, queue=queue_name, no_ack=True)
channel.start_consuming()