Finishing up the alert consumer and producer

I think...
This commit is contained in:
Dan Buch 2012-11-20 07:57:06 -05:00
parent 4e980011d6
commit 61a748bd44
2 changed files with 54 additions and 3 deletions

View File

@ -12,8 +12,8 @@ AMQP_USER = 'alert_user'
AMQP_PASS = 'alertme' AMQP_PASS = 'alertme'
AMQP_VHOST = '/' AMQP_VHOST = '/'
AMQP_EXCHANGE = 'alerts' AMQP_EXCHANGE = 'alerts'
OPS_EMAILS = ['daniel.buch+sylvilagus-alerts-ops@gmail.com'] OPS_EMAILS = ['me@localhost']
ADMIN_EMAILS = ['daniel.buch+sylvilagus-alerts-admin@gmail.com'] ADMIN_EMAILS = ['me@localhost']
def main(): def main():
@ -61,7 +61,7 @@ def send_mail(recipients, subject, message):
'To: ', 'To: ',
'Date: ', 'Date: ',
'Subject: {}'.format(subject) 'Subject: {}'.format(subject)
]) + '\r\n' ]) + '\r\n\r\n'
smtp_server = smtplib.SMTP() smtp_server = smtplib.SMTP()
smtp_server.connect('localhost', 25) smtp_server.connect('localhost', 25)
smtp_server.sendmail('alerts@sylvilagus.local', smtp_server.sendmail('alerts@sylvilagus.local',

View File

@ -0,0 +1,51 @@
from __future__ import print_function
import json
import sys
import pika
from argparse import ArgumentParser
def main(sysargs=sys.argv[:]):
parser = ArgumentParser()
parser.add_argument('-r',
'--routing-key',
help='Routing key for message (e.g. myalert.im)')
parser.add_argument('-m',
'--message',
help='Message text for alert.')
args = parser.parse_args(sysargs[1:])
creds_broker = pika.PlainCredentials('alert_user', 'alertme')
conn_params = pika.ConnectionParameters('localhost',
virtual_host='/',
credentials=creds_broker)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
msg = json.dumps({'message': args.message})
msg_props = pika.BasicProperties()
msg_props.content_type = 'application/json'
msg_props.durable = False
channel.basic_publish(body=msg,
exchange='alerts',
properties=msg_props,
routing_key=args.routing_key)
print(
('Sent message {} tagged with routing key {!r} to ' +
'exchange "alerts" on vhost "/".').format(msg, args.routing_key)
)
conn_broker.close()
return 0
if __name__ == '__main__':
sys.exit(main())