Skip to content

Instantly share code, notes, and snippets.

@vagelim
Last active July 30, 2020 15:16
Show Gist options
  • Save vagelim/64b355b65378ecba15b0 to your computer and use it in GitHub Desktop.
Save vagelim/64b355b65378ecba15b0 to your computer and use it in GitHub Desktop.
Event notification listener for OpenStack
########################################
# Required changes to nova.conf #
########################################
# notification_driver=nova.openstack.common.notifier.rpc_notifier
# notification_topics = notifications
# notify_on_state_change = vm_and_task_state
# instance_usage_audit_period = hour
# instance_usage_audit = True
# default_notification_level = INFO
# notify_api_faults = true
###### Restart Nova services after making the above changes #######
# Environment Variables: $AMQP_USER, $AMQP_PASSWORD, $AMQP_HOST
import os
from kombu import Connection, Exchange, Queue
from pprint import pprint
nova_x = Exchange('nova', type='topic', durable=False)
info_q = Queue('notifications.info', exchange=nova_x, durable=False,
routing_key='notifications.info')
def process_msg(body, message):
print '='*80
pprint(body)
message.ack()
# Maybe wrap these in try/except: will raise KeyError if not found
AMQP_USER = os.environ['AMQP_USER']
AMQP_PASSWORD = os.environ['AMQP_PASSWORD']
AMQP_HOST = os.environ['AMQP_HOST']
# Maybe wrap this in a try/except: will raise socket.error if connection refused
with Connection('amqp://{0}:{1}@{2}//'.format(AMQP_USER, AMQP_PASSWORD, AMQP_HOST)) as conn:
with conn.Consumer(info_q, callbacks=[process_msg]):
try:
while True:
conn.drain_events()
except KeyboardInterrupt:
exit()
@gpion
Copy link

gpion commented Apr 30, 2018

Hello,
I had to replace routing_key='notifications.info' by routing_key='notifications.*' since I was not catching some nova messages like instance.create.{start,end} amongst others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment