Ask Your Question
0

Tracking a particular keyword from response in rabbitmq

asked 2015-09-07 02:23:26 -0500

itsme gravatar image

updated 2015-09-10 02:27:48 -0500

I have done the codes for fetching the response from queue.

My aim is to log all these in a file.

Whenever I am getting a particular response in a queue.

I need to log those things.

For example:

[x] 'notifications.info':'{"_context_roles": ["admin", "_member_", "heat_stack_owner"], "_context_request_id": "req-99bc87eb-3a97-4c05-9f67-1e2d683b8bf8", "_context_quota_class": null, "event_type": "compute.instance.create.end"

In this response We can see that there are some keywords were there.

Now what I need to do is logging a particular event.

In more clear way:

I need to track the response only for "event_type": "compute.instance.create.end" .

So that whenever I am getting a particular keyword in response I need to log the details in a file.

I have attached a code which I have done.

#!/usr/bin/env python
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='nova',
                         type='topic')

result = channel.queue_declare(exclusive=True)
queue_name = 'notifications.info'

binding_keys = 'notifications.info'
if not binding_keys:
    print >> sys.stderr, "Usage: %s [binding_key]..." % (sys.argv[0],)
    sys.exit(1)

for binding_key in binding_keys:
    channel.queue_bind(exchange='nova',
                       queue=queue_name,
                       routing_key=binding_key)

print ' [*] Waiting for logs. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] %r:%r" % (method.routing_key, body,)

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()

I will be executing a file using the following method:

python filename.py > filename.log &

Now I am getting all reponse from particular queue using above code, Instead that I need to log only when I am getting particular keyword in reponse.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-09-28 05:06:32 -0500

Tosugueur gravatar image

Hi. Each time the callback is executed (a message is received) you need to verify weather the body contains the targeted sting - "event_type": "compute.instance.create.end" -. With a simple if statement you can exit the loop and display the result.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2015-09-07 02:23:26 -0500

Seen: 106 times

Last updated: Sep 28 '15