dragonfly.implementations.sensor_monitor module

Some template vars

Members: SensorMonitor Functions: Classes:

Utility for monitoring logged values

Summary

class dragonfly.implementations.sensor_monitor.SensorMonitor(sensors, routing_key_base='sensor_value.', **kwargs)

Bases: dragonfly.implementations.sensor_monitor.SensorMonitor

A generic service that will monitor logged values via the relevant alerts exchanges. Each monitored endpoint should be configured as an element of the sensors list, and arbitrarily many alarms may be configured for each endpoint; alarms should be listed in decreasing severity as only the first alarm will be enacted.

NOTE Any “keys” kwarg provided will be overwritten and bindings are to the alerts exchange as specified in Gogol and Service.

Keyword Arguments:
 
  • key_base (str) – alerts exchange base, used to form complete names of alerts to bind
  • sensors (list) – list of endpoint and alarms to configure
  • exchange (str) – (overrides Service default)
  • keys (list|str) – (overrides Service default)
  • broker (str) – The AMQP url to connect with
  • exchange – Name of the AMQP exchange to connect to
  • keys – binding key or list of binding keys to use listen against
  • name (str) – name for the amqp queue, automatically generated if None (this behavior supplements the Endpoint arg of the same name)
  • setup_calls (list of dicts) – each element is a dictionary describing a method to call. Valid keys are target, method, args, and kwargs, which will be called as service.endpoints[target].method(*args,**kwargs). Note that on_set can be used to assign values to attributes in this syntax.
  • name – unique identifier across all dripline services (used to determine routing key)
  • calibration (str||dict) – string use to process raw get result (with .format(raw)) or a dict to use for the same purpose where raw must be a key
  • get_on_set (bool) – flag to toggle running ‘on_get’ after each ‘on_set’

key_base (str): alerts exchange base, used to form complete names of alerts to bind sensors (list): list of endpoint and alarms to configure

EXCHANGE_TYPE = 'topic'
acknowledge_message(delivery_tag)

Acknowledge the message delivery from RabbitMQ by sending a Basic.Ack RPC method for the delivery tag.

Parameters:delivery_tag (int) – The delivery tag from the Basic.Deliver frame
add_endpoint(endpoint)
add_on_cancel_callback()

Add a callback that will be invoked if RabbitMQ cancels the consumer for some reason. If RabbitMQ does cancel the consumer, on_consumer_cancelled will be invoked by pika.

add_on_channel_close_callback()

This method tells pika to call the on_channel_closed method if RabbitMQ unexpectedly closes the channel.

add_on_connection_close_callback()

This method adds an on close callback that will be invoked by pika when RabbitMQ closes the connection to the publisher unexpectedly.

alarm_status
check_if_float(key, alarm, target)
check_monitor_fields(alarm, target)
close_channel()

Call to close the channel with RabbitMQ cleanly by issuing the Channel.Close RPC command.

close_connection()

This method closes the connection to RabbitMQ.

cmd(target, method_name, value=[], payload={}, lockout_key=False, timeout=None, ignore_retcode=False)
configure_monitors(sensors)
connect()

This method connects to RabbitMQ, returning the connection handle. When the connection is established, the on_connection_open method will be invoked by pika.

Return type:pika.SelectConnection
endpoint(endpoint)
endpoint_names
endpoints
get(target, timeout=None, ignore_retcode=False)
handle_request(channel, method, properties, request)
is_locked
keys
list_endpoints()
lock(*args, **kwargs)
lockout_key
logging_status
on_alert_message(channel, method, properties, message)
on_any_message(**kwargs)
on_bindok(unused_frame)

Invoked by pika when the Queue.Bind method has completed. At this point we will start consuming messages by calling start_consuming which will invoke the needed RPC commands to start the process.

Parameters:unused_frame (pika.frame.Method) – The Queue.BindOk response frame
on_cancelok(unused_frame)

This method is invoked by pika when RabbitMQ acknowledges the cancellation of a consumer. At this point we will close the channel. This will invoke the on_channel_closed method once the channel has been closed, which will in-turn close the connection.

Parameters:unused_frame (pika.frame.Method) – The Basic.CancelOk frame
on_channel_closed(channel, reply_code, reply_text)

Invoked by pika when RabbitMQ unexpectedly closes the channel. Channels are usually closed if you attempt to do something that violates the protocol, such as re-declare an exchange or queue with different parameters. In this case, we’ll close the connection to shutdown the object.

Parameters:
  • pika.channel.Channel – The closed channel
  • reply_code (int) – The numeric reason the channel was closed
  • reply_text (str) – The text reason the channel was closed
on_channel_open(channel)

This method is invoked by pika when the channel has been opened. The channel object is passed in so we can make use of it.

Since the channel is now open, we’ll declare the exchange to use.

Parameters:channel (pika.channel.Channel) – The channel object
on_cmd(*args, **kwargs)

WARNING! if you override this method, you must ensure you deal with lockout properly

on_config(attribute, value=None)

configure a property again

WARNING! if you override this method, you must ensure you deal with lockout properly

on_connection_closed(connection, reply_code, reply_text)

This method is invoked by pika when the connection to RabbitMQ is closed unexpectedly. Since it is unexpected, we will reconnect to RabbitMQ if it disconnects.

Parameters:
  • connection (pika.connection.Connection) – The closed connection obj
  • reply_code (int) – The server provided reply_code if given
  • reply_text (str) – The server provided reply_text if given
on_connection_open(unused_connection)

This method is called by pika once the connection to RabbitMQ has been established. It passes the handle to the connection object in case we need it, but in this case, we’ll just mark it unused.

on_consumer_cancelled(method_frame)

Invoked by pika when RabbitMQ sends a Basic.Cancel for a consumer receiving messages.

Parameters:method_frame (pika.frame.Method) – The Basic.Cancel frame
on_exchange_declareok(unused_frame)

Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC command.

Parameters:unused_frame (pika.Frame.Method) – Exchange.DeclareOk response frame
on_message(unused_channel, basic_deliver, properties, body)

Invoked by pika when a message is delivered from RabbitMQ. The channel is passed for your convenience. The basic_deliver object that is passed in carries the exchange, routing key, delivery tag and a redelivered flag for the message. The properties passed in is an instance of BasicProperties with the message properties and the body is the message that was sent.

Parameters:
  • unused_channel (pika.channel.Channel) – The channel object
  • pika.Spec.Basic.Deliver – basic_deliver method
  • pika.Spec.BasicProperties – properties
  • body (str|unicode) – The message body
on_queue_declareok(method_frame)

Method invoked by pika when the Queue.Declare RPC call made in setup_queue has completed. In this method we will bind the queue and exchange together with the routing key by issuing the Queue.Bind RPC command. When this command is complete, the on_bindok method will be invoked by pika.

Parameters:method_frame (pika.frame.Method) – The Queue.DeclareOk frame
on_reply_message(**kwargs)
on_request_message(channel, method, header, body)
on_send(*commands)
on_set(*args, **kwargs)
open_channel()

Open a new channel with RabbitMQ by issuing the Channel.Open RPC command. When RabbitMQ responds that the channel is open, the on_channel_open callback will be invoked by pika.

ping(*args, **kwargs)

ignore all details and respond with an empty message

process_alarms(alarm, result)
process_new_value(sensor)
reconnect()

Will be invoked by the IOLoop timer if the connection is closed. See the on_connection_closed method.

run()

Run the example consumer by connecting to RabbitMQ and then starting the IOLoop to block and allow the SelectConnection to operate.

schedule_status
send_alert(alert, severity)
send_message(target, message, return_queue=None, properties=None, exchange=None, return_connection=False, ensure_delivery=True)
send_reply(properties, reply)
send_request(target, request, timeout=10, multi_reply=False)

It seems like there should be a way to do this with the existing SelectConnection. The problem is that the message handler needs to send a request and then be called not block the reply from being processed, and needs to get the reply from that processed response. The non-blocking part seems tricky. I’m sure there exists a good solution for this, maybe within asyncio and/or asyncore, but I don’t know where it is. This seems to work.

send_status_message(alert, severity)
set(target, value, lockout_key=False, timeout=None, ignore_retcode=False)
set_condition(*args, **kwargs)

To actually do something inside a class, the set_condition should be implemented in the class itself.

setup_exchange(exchange_name)

Setup the exchange on RabbitMQ by invoking the Exchange.Declare RPC command. When it is complete, the on_exchange_declareok method will be invoked by pika.

Parameters:exchange_name (str|unicode) – The name of the exchange to declare
setup_queue(queue_name)

Setup the queue on RabbitMQ by invoking the Queue.Declare RPC command. When it is complete, the on_queue_declareok method will be invoked by pika.

Parameters:queue_name (str|unicode) – The name of the queue to declare.
silence_alarm(target, alarm_ct, endtime)
start()

Begin consuming by calling self.run (from Spimescape)

start_consuming()

This method sets up the consumer by first calling add_on_cancel_callback so that the object is notified if RabbitMQ cancels the consumer. It then issues the Basic.Consume RPC command which returns the consumer tag that is used to uniquely identify the consumer with RabbitMQ. We keep the value to use it when we want to cancel consuming. The on_message method is passed in as a callback pika will invoke when a message is fully received.

start_event_loop()

Call self.run with controlled stop

stop()

Cleanly shutdown the connection to RabbitMQ by stopping the consumer with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok will be invoked by pika, which will then closing the channel and connection. The IOLoop is started again because this method is invoked when CTRL-C is pressed raising a KeyboardInterrupt exception. This exception stops the IOLoop which needs to be running for pika to communicate with RabbitMQ. All of the commands issued prior to starting the IOLoop will be buffered but not processed.

stop_consuming()

Tell RabbitMQ that you would like to stop consuming by sending the Basic.Cancel RPC command.

this_consume(message, method)
unlock(*args, **kwargs)

End of modules condition

Data:

End of data condition