6. WBEM indication API

The WBEM indication API supports subscription for and receiving of CIM indications.

This chapter has the following sections:

Note

The WBEM indication API has been introduced in v0.9.0 as experimental and has been declared final in 0.10.0.

6.1. WBEMListener

The WBEMListener class provides a thread-based WBEM listener service that can receive CIM indications from multiple WBEM servers and that calls registered callback functions to deliver the received indications.

6.1.1. Examples

The following example creates and runs a listener:

import sys
import logging
from socket import getfqdn
from pywbem import WBEMListener

def process_indication(indication, host):
    '''This function gets called when an indication is received.'''

    print("Received CIM indication from {host}: {ind!r}". \
        format(host=host, ind=indication))

def main():

    logging.basicConfig(stream=sys.stderr, level=logging.WARNING,
        format='%(asctime)s - %(levelname)s - %(message)s)

    certkeyfile = 'listener.pem'

    url1 = 'http://server1'
    conn1 = WBEMConnection(url1)
    server1 = WBEMServer(conn1)
    server1.determine_interop_ns()

    url2 = 'http://server2'
    conn2 = WBEMConnection(url2)
    server2 = WBEMServer(conn2)
    server2.validate_interop_ns('root/PG_InterOp')

    my_listener = WBEMListener(host=getfqdn()
                            http_port=5988,
                            https_port=5989,
                            certfile=certkeyfile,
                            keyfile=certkeyfile)
    my_listener.add_callback(process_indication)
    listener.start()

        # listener runs until executable terminated
        # or listener.stop()

See the example in section WBEMSubscriptionManager for an example of using a listener in combination with a subscription manager.

Another listener example is in the script examples/listen.py (when you clone the GitHub pywbem/pywbem project). It is an interactive Python shell that creates a listener and displays any indications it receives, in MOF format.

class pywbem.WBEMListener(host, http_port=None, https_port=None, certfile=None, keyfile=None)[source]

A WBEM listener.

The listener supports starting and stopping threads that listen for CIM-XML ExportIndication messages using HTTP and/or HTTPS, and that pass any received indications on to registered callback functions.

Parameters:
  • host (string) – IP address or host name this listener can be reached at.
  • http_port (string or integer) –

    HTTP port this listener can be reached at. Note that at least one port (HTTP or HTTPS) must be set

    None means not to set up a port for HTTP.

  • https_port (string or integer) –

    HTTPS port this listener can be reached at.

    None means not to set up a port for HTTPS.

  • certfile (string) –

    File path of certificate file to be used as server certificate during SSL/TLS handshake when creating the secure HTTPS connection.

    It is valid for the certificate file to contain a private key; the server certificate sent during SSL/TLS handshake is sent without the private key.

    None means not to use a server certificate file. Setting up a port for HTTPS requires specifying a certificate file.

  • keyfile (string) –

    File path of private key file to be used by the server during SSL/TLS handshake when creating the secure HTTPS connection.

    It is valid to specify a certificate file that contains a private key.

    None means not to use a private key file. Setting up a port for HTTPS requires specifying a private key file.

__repr__()[source]

Return a representation of the WBEMListener object with all attributes, that is suitable for debugging.

host

The IP address or host name this listener can be reached at, as a string.

http_port

The HTTP port this listener can be reached at, as an integer.

None means there is no port set up for HTTP.

https_port

The HTTPS port this listener can be reached at, as an integer.

None means there is no port set up for HTTPS.

certfile

The file path of the certificate file used as server certificate during SSL/TLS handshake when creating the secure HTTPS connection.

None means there is no certificate file being used (that is, no port is set up for HTTPS).

keyfile

The file path of the private key file used by the server during SSL/TLS handshake when creating the secure HTTPS connection.

None means there is no certificate file being used (that is, no port is set up for HTTPS).

logger

The logger object for this listener.

Each listener object has its own separate logger object that is created via logging.getLogger().

The name of this logger object is:

pywbem.listener.{id}

where {id} is the id() value of the listener object. Users of the listener should not look up the logger object by name, but should use this property to get to it.

By default, this logger uses the NullHandler log handler, and its log level is logging.NOTSET. This causes this logger not to emit any log messages and to propagate them to the Python root logger.

The behavior of this logger can be changed by invoking its methods (see logging.Logger). The behavior of the root logger can for example be configured using logging.basicConfig():

import sys
import logging

logging.basicConfig(stream=sys.stderr, level=logging.WARNING,
    format='%(asctime)s - %(levelname)s - %(message)s')
start()[source]

Start the WBEM listener threads, if they are not yet running.

A thread serving CIM-XML over HTTP is started if an HTTP port was specified for the listener. A thread serving CIM-XML over HTTPS is started if an HTTPS port was specified for the listener.

These server threads will handle the ExportIndication export message described in DSP0200 and they will invoke the registered callback functions for any received CIM indications.

These server threads can be stopped using the stop() method. They will be automatically stopped when the main thread terminates.

stop()[source]

Stop the WBEM listener threads, if they are running.

deliver_indication(indication, host)[source]

This function is called by the listener threads for each received indication. It is not supposed to be called by the user.

It delivers the indication to all callback functions that have been added to the listener.

If a callback function raises any exception this is logged as an error using the listener logger and the next registered callback function is called.

Parameters:
  • indication (CIMInstance) – Representation of the CIM indication to be delivered.
  • host (string) – Host name or IP address of WBEM server sending the indication.
add_callback(callback)[source]

Add a callback function to the listener.

The callback function will be called for each indication this listener receives from any WBEM server.

If the callback function is already known to the listener, it will not be added.

Parameters:callback (callback_interface()) – Callable that is being called for each CIM indication that is received while the listener threads are running.
pywbem.callback_interface(indication, host)[source]

Interface of a callback function that is provided by the user of the API and that will be called by the listener for each received CIM indication.

Parameters:
  • indication (CIMInstance) – Representation of the CIM indication that has been received. Its path attribute is None.
  • host (string) – Host name or IP address of WBEM server sending the indication.
Raises:

Exception – If a callback function raises any exception this is logged as an error using the listener logger and the next registered callback function is called.

6.2. WBEMSubscriptionManager

The WBEMSubscriptionManager class is a subscription manager that provides for creating and removing indication subscriptions (including indication filters and listener destinations) for multiple WBEM servers and multiple WBEM listeners and for getting information about existing indication subscriptions.

The WBEM listener is identified through its URL, so it may be a WBEMListener object or any external WBEM listener.

This subscription manager supports three types of ownership for subscription, filter and listener destination CIM instances in WBEM servers:

  • Owned

    Owned CIM instances are created via the subscription manager and their life cycle is bound to the life cycle of the registration of that WBEM server with the subscription manager via add_server().

    Owned CIM instances are deleted automatically when their WBEM server is deregistered from the subscription manager via remove_server() or remove_all_servers(). They can still explicitly be deleted by the user via the removal methods of the WBEMSubscriptionManager class.

  • Permanent

    Permanent CIM instances are created via the subscription manager and their life cycle is independent of the life cycle of the registration of that WBEM server with the subscription manager.

    Permanent CIM instances are not deleted automatically when their WBEM server is deregistered from the subscription manager. The user is responsible for their lifetime management: They can be deleted via the removal methods of the WBEMSubscriptionManager class.

  • Static

    Static CIM instances pre-exist in the WBEM server and cannot be deleted (or created) by a WBEM client.

If a client creates a subscription between a filter and a listener destination, the types of ownership of these three CIM instances may be arbitrarily mixed, with one exception:

  • A permanent subscription cannot be created on an owned filter or an owned listener destination. Allowing that would prevent the automatic life cycle management of the owned filter or listener destination by the subscription manager. This restriction is enforced by the WBEMSubscriptionManager class.

The WBEMSubscriptionManager object remembers owned subscriptions, filters, and listener destinations. If for some reason that object gets deleted before all servers could be removed (e.g. because the Python program aborts), the corresponding CIM instances in the WBEM server still exist, but the knowledge is lost that these instances were owned by that subscription manager. Therefore, the subscription manager discovers owned subscriptions, filters, and listener destinations when a server is added. For filters, this discovery is based upon the Name property. Therefore, if the Name property is set by the user (e.g. because a management profile requires a particular name), the filter must be permanent and cannot be owned.

6.2.1. Examples

The following example code demonstrates the use of a subscription manager to subscribe for a CIM alert indication on a WBEM server. The WBEM listener is assumed to exist somewhere and is identified by its URL:

import sys
from socket import getfqdn
from pywbem import WBEMConnection, WBEMServer, WBEMSubscriptionManager

server_url = 'http://myserver'
server_credentials = (user, password)

listener_url = 'http://mylistener'

conn = WBEMConnection(server_url, server_credentials)
server = WBEMServer(conn)
sub_mgr = WBEMSubscriptionManager(subscription_manager_id='fred')

# Register the server in the subscription manager:
server_id = sub_mgr.add_server(server)

# Add a listener destination in the server:
dest_inst = sub_mgr.add_listener_destinations(server_id, listener_url)

# Subscribe to a static filter of a given name:
filter1_name = "DMTF:Indications:GlobalAlertIndicationFilter"
filter1_insts = sub_mgr.get_all_filters(server_id)
for inst in filter1_insts:
    if inst['Name'] == filter1_name:
        sub_mgr.add_subscriptions(server_id, inst.path, dest_inst.path)
        break

# Create a dynamic alert filter and subscribe to it:
filter2_source_ns = "root/cimv2"
filter2_query = "SELECT * FROM CIM_AlertIndication " \
                "WHERE OwningEntity = 'DMTF' " \
                "AND MessageID LIKE 'SVPC0123|SVPC0124|SVPC0125'"
filter2_language = "DMTF:CQL"
filter2_inst = subscription_manager.add_filter(server_id,
    filter2_source_ns, filter2_query, filter2_language, owned=True,
    filter_id="myalert")
sub_mgr.add_subscriptions(server_id, filter2_inst.path, dest_inst.path)

The following example code briefly shows how to use a subscription manager as a context manager, in order to get automatic cleanup of owned instances:

with WBEMSubscriptionManager('fred') as sub_mgr:
    server_id = sub_mgr.add_server(server)
    . . .
# The exit method automatically calls remove_all_servers(), which deletes
# all owned subscription, filter and destination instances in the servers
# that were registered.

The Tutorial section contains a tutorial about the subscription manager.

class pywbem.WBEMSubscriptionManager(subscription_manager_id)[source]

A class for managing subscriptions for CIM indications in a WBEM server.

The class may be used as a Python context manager, in order to get automatic clean up (see __exit__()).

Parameters:subscription_manager_id (string) –

A subscription manager ID string that is used as a component in the value of the Name property of indication filter and listener destination instances to help the user identify these instances in a WBEM server.

Must not be None.

The string must consist of printable characters, and must not contain the character ‘:’ because that is the separator between components within the value of the Name property.

The subscription manager ID must be unique on the current host.

For example, the form of the Name property of a filter instance is (for details, see add_filter()):

"pywbemfilter:" {ownership} ":" {subscription_manager_id} ":" {filter_id} ":" {guid}
Raises:ValueError, TypeError for incorrect input parameters.
__repr__()[source]

Return a representation of the WBEMSubscriptionManager object with all attributes, that is suitable for debugging.

__enter__()[source]

Enter method when the class is used as a context manager. Returns the subscription manager object

__exit__(exc_type, exc_value, traceback)[source]

Exit method when the class is used as a context manager.

It cleans up by calling remove_all_servers().

add_server(server)[source]

Register a WBEM server with the subscription manager. This is a prerequisite for adding listener destinations, indication filters and indication subscriptions to the server.

This method discovers listener destination, indication filter, and subscription instances in the WBEM server that are owned by this subscription manager, and registers them in the subscription manager as if they had been created through it.

In this discovery process, listener destination and indication filter instances are matched based upon the value of their Name property. Subscription instances are matched based upon the ownership of the referenced destination and filter instances: If a subscription references a filter or a destination that is owned by this subscription manager, it is considered owned by this subscription manager as well.

Parameters:

server (WBEMServer) – The WBEM server.

Returns:

An ID for the WBEM server, for use by other methods of this class.

Return type:

string

Raises:
  • Exceptions raised by WBEMConnection.
  • ValueError, TypeError for incorrect input parameters.
remove_server(server_id)[source]

Remove a registered WBEM server from the subscription manager. This also unregisters listeners from that server and removes all owned indication subscriptions, owned indication filters and owned listener destinations that were created by this subscription manager for that server.

Parameters:server_id (string) – The server ID of the WBEM server, returned by add_server().
Raises:Exceptions raised by WBEMConnection.
remove_all_servers()[source]

Remove all registered WBEM servers from the subscription manager. This also unregisters listeners from these servers and removes all owned indication subscriptions, owned indication filters, and owned listener destinations that were created by this subscription manager.

This is, in effect, a complete shutdown of the subscription manager.

Raises:Exceptions raised by WBEMConnection.
add_listener_destinations(server_id, listener_urls, owned=True)[source]

Register WBEM listeners to be the target of indications sent by a WBEM server.

This function automatically creates a listener destination instance (of CIM class “CIM_ListenerDestinationCIMXML”) for each specified listener URL in the Interop namespace of the specified WBEM server.

The form of the Name property of the created destination instance is:

"pywbemdestination:" {ownership} ":" {subscription_manager_id} ":" {guid}

where {ownership} is "owned" or "permanent" dependent on the owned argument; {subscription_manager_id} is the subscription manager ID; and {guid} is a globally unique identifier.

Owned listener destinations are added or updated conditionally: If the listener destination instance to be added is already registered with this subscription manager and has the same property values, it is not created or modified. It it has the same path but different property values, it is modified to get the desired property values. If an instance with this path does not exist yet (the normal case), it is created.

Permanent listener destinations are created unconditionally, and it is up to the user to ensure that such an instance does not exist yet.

Parameters:
  • server_id (string) – The server ID of the WBEM server, returned by add_server().
  • listener_urls (string or list of string) –

    The URL or URLs of the WBEM listeners to be registered.

    The WBEM listener may be a WBEMListener object or any external WBEM listener.

    Each listener URL string must have the format:

    [{scheme}://]{host}:{port}

    The following URL schemes are supported:

    • https: Causes HTTPS to be used.
    • http: Causes HTTP to be used. This is the default

    The host can be specified in any of the usual formats:

    • a short or fully qualified DNS hostname
    • a literal (= dotted) IPv4 address
    • a literal IPv6 address, formatted as defined in RFC3986 with the extensions for zone identifiers as defined in RFC6874, supporting - (minus) for the delimiter before the zone ID string, as an additional choice to %25.

    Note that the port is required in listener URLs.

    See WBEMConnection for examples of valid URLs, with the caveat that the port in server URLs is optional.

  • owned (bool) – Defines the ownership type of the created listener destination instances: If True, they will be owned. Otherwise, they will be permanent. See WBEMSubscriptionManager for details about these ownership types.
Returns:

The created listener destination instances for the defined listener URLs.

Return type:

list of CIMInstance

Raises:

Exceptions raised by WBEMConnection.

get_owned_destinations(server_id)[source]

Return the owned listener destinations in a WBEM server that have been created by this subscription manager.

This function accesses only the local list of owned destinations; it does not contact the WBEM server.

Parameters:server_id (string) – The server ID of the WBEM server, returned by add_server().
Returns:The listener destination instances.
Return type:list of CIMInstance
get_all_destinations(server_id)[source]

Return all listener destinations in a WBEM server.

This function contacts the WBEM server and retrieves the listener destinations by enumerating the instances of CIM class “CIM_ListenerDestinationCIMXML” in the Interop namespace of the WBEM server.

Parameters:server_id (string) – The server ID of the WBEM server, returned by add_server().
Returns:The listener destination instances.
Return type:list of CIMInstance
Raises:Exceptions raised by WBEMConnection.
remove_destinations(server_id, destination_paths)[source]

Remove listener destinations from a WBEM server, by deleting the listener destination instances in the server.

The listener destinations must be owned or permanent (i.e. not static).

This method verifies that there are not currently any subscriptions on the listener destinations to be removed, in order to handle server implementations that do not ensure that on the server side as required by DSP1054.

Parameters:
Raises:
  • Exceptions raised by WBEMConnection.
  • CIMError(CIM_ERR_FAILED) if there are referencing subscriptions.
add_filter(server_id, source_namespace, query, query_language='WQL', owned=True, filter_id=None, name=None)[source]

Add a dynamic indication filter to a WBEM server, by creating an indication filter instance (of CIM class “CIM_IndicationFilter”) in the Interop namespace of the server.

The Name property of the created filter instance can be set in one of two ways:

  • directly by specifying the name parameter.

    In this case, the Name property is set directly to the name parameter.

    This should be used in cases where the user needs to have control over the filter name (e.g. because a DMTF management profile requires a particular name), but it cannot be used for owned filters.

  • indirectly by specifying the filter_id parameter.

    In this case, the value of the Name property will be:

    "pywbemfilter:" {ownership} ":" {subscription_manager_id} ":" {filter_id} ":" {guid}

    where {ownership} is "owned" or "permanent" dependent on whether the filter is owned or permmanent; {subscription_manager_id} is the subscription manager ID; {filter_id} is the filter ID; and {guid} is a globally unique identifier.

    This can be used for both owned and permanent filters.

Owned indication filters are added or updated conditionally: If the indication filter instance to be added is already registered with this subscription manager and has the same property values, it is not created or modified. It it has the same path but different property values, it is modified to get the desired property values. If an instance with this path does not exist yet (the normal case), it is created.

Permanent indication filters are created unconditionally, and it is up to the user to ensure that such an instance does not exist yet.

Parameters:
  • server_id (string) – The server ID of the WBEM server, returned by add_server().
  • source_namespace (string) – Source namespace of the indication filter.
  • query (string) – Filter query in the specified query language.
  • query_language (string) –

    Query language for the specified filter query.

    Examples: ‘WQL’, ‘DMTF:CQL’.

  • owned (bool) – Defines the ownership type of the created indication filter instance: If True, it will be owned. Otherwise, it will be permanent. See WBEMSubscriptionManager for details about these ownership types.
  • filter_id (string) –

    A filter ID string that is used as a component in the value of the Name property of filter instances to help the user identify these instances in a WBEM server, or None if the name parameter is specified.

    The string must consist of printable characters, and must not contain the character ‘:’ because that is the separator between components within the value of the Name property.

    There is no requirement that the filter ID be unique. This can be used to identify groups of filters by using the same value for multiple filters.

  • name (string) – The filter name to be used directly for the Name property of the filter instance, or None if the filter_id parameter is specified.
Returns:

The created indication filter instance.

Return type:

CIMInstance

Raises:
  • Exceptions raised by WBEMConnection.
  • ValueError, TypeError for incorrect input parameters.
get_owned_filters(server_id)[source]

Return the owned indication filters in a WBEM server that have been created by this subscription manager.

This function accesses only the local list of owned filters; it does not contact the WBEM server.

Parameters:server_id (string) – The server ID of the WBEM server, returned by add_server().
Returns:The indication filter instances.
Return type:list of CIMInstance
get_all_filters(server_id)[source]

Return all indication filters in a WBEM server.

This function contacts the WBEM server and retrieves the indication filters by enumerating the instances of CIM class “CIM_IndicationFilter” in the Interop namespace of the WBEM server.

Parameters:server_id (string) – The server ID of the WBEM server, returned by add_server().
Returns:The indication filter instances.
Return type:list of CIMInstance
Raises:Exceptions raised by WBEMConnection.
remove_filter(server_id, filter_path)[source]

Remove an indication filter from a WBEM server, by deleting the indication filter instance in the WBEM server.

The indication filter must be owned or permanent (i.e. not static).

This method verifies that there are not currently any subscriptions on the specified indication filter, in order to handle server implementations that do not ensure that on the server side as required by DSP1054.

Parameters:
  • server_id (string) – The server ID of the WBEM server, returned by add_server().
  • filter_path (CIMInstanceName) – Instance path of the indication filter instance in the WBEM server.
Raises:
  • Exceptions raised by WBEMConnection.
  • CIMError(CIM_ERR_FAILED) if there are referencing subscriptions.
add_subscriptions(server_id, filter_path, destination_paths=None, owned=True)[source]

Add subscriptions to a WBEM server for a particular set of indications defined by an indication filter and for a particular set of WBEM listeners defined by the instance paths of their listener destinations, by creating indication subscription instances (of CIM class “CIM_IndicationSubscription”) in the Interop namespace of that server.

The specified indication filter may be owned, permanent or static.

The specified listener destinations may be owned, permanent or static.

When creating permanent subscriptions, the indication filter and the listener destinations must not be owned.

Owned subscriptions are added or updated conditionally: If the subscription instance to be added is already registered with this subscription manager and has the same property values, it is not created or modified. It it has the same path but different property values, it is modified to get the desired property values. If an instance with this path does not exist yet (the normal case), it is created.

Permanent subscriptions are created unconditionally, and it is up to the user to ensure that such an instance does not exist yet.

Upon successful return of this method, the added subscriptions are active, so that the specified WBEM listeners may immediately receive indications.

Parameters:
  • server_id (string) – The server ID of the WBEM server, returned by add_server().
  • filter_path (CIMInstanceName) – Instance path of the indication filter instance in the WBEM server that specifies the indications to be sent.
  • destination_paths (CIMInstanceName or list of CIMInstanceName) –

    Instance paths of the listener destination instances in the WBEM server that specify the target WBEM listener.

    If None, subscriptions will be created for all owned listener destinations registered to this subscription manager.

  • owned (bool) – Defines the ownership type of the created subscription instances: If True, they will be owned. Otherwise, they will be permanent. See WBEMSubscriptionManager for details about these ownership types.
Returns:

The indication subscription instances created in the WBEM server.

Return type:

list of CIMInstance

Raises:
  • Exceptions raised by WBEMConnection.
  • ValueError for incorrect input parameters.
get_owned_subscriptions(server_id)[source]

Return the owned indication subscriptions for a WBEM server that have been created by this subscription manager.

This function accesses only the local list of owned subscriptions; it does not contact the WBEM server.

Parameters:server_id (string) – The server ID of the WBEM server, returned by add_server().
Returns:The indication subscription instances.
Return type:list of CIMInstance
get_all_subscriptions(server_id)[source]

Return all indication subscriptions in a WBEM server.

This function contacts the WBEM server and retrieves the indication subscriptions by enumerating the instances of CIM class “CIM_IndicationSubscription” in the Interop namespace of the WBEM server.

Parameters:server_id (string) – The server ID of the WBEM server, returned by add_server().
Returns:The indication subscription instances.
Return type:list of CIMInstance
Raises:Exceptions raised by WBEMConnection.
remove_subscriptions(server_id, sub_paths)[source]

Remove indication subscription(s) from a WBEM server, by deleting the indication subscription instances in the server.

The indication subscriptions must be owned or permanent (i.e. not static).

Parameters:
Raises:

Exceptions raised by WBEMConnection.