LMIWBEM Usage Examples

This section describes common LMIWBEM use-cases.

Connecting to CIMOM

This section describes and demonstrates how to create a connection object, through which all the CIM operations are performed.

There are 3 means, how to create and use the connection object:

  • Providing username and password
  • Providing X509 certificate
  • Using Unix socket

Connection using username/password

Major difference when compared to PyWBEM, is that it is possible to create and maintain a connection with the CIMOM. See the next example:

import lmiwbem

conn = lmiwbem.WBEMConnection()
conn.connect("hostname", "username", "password")

# CIM operations

conn.disconnect()

It is possible to create a connection object in PyWBEM’s way. The connection to CIMOM is opened for each CIM operation and when done, the connection is closed. See the next example:

import lmiwbem

conn = lmiwbem.WBEMConnection("hostname", ("username", "password"))

# CIM operations

NOTE: When WBEMConnection.connect() is not called, temporary connection for each CIM operations will be performed.

Connection using X509 certificate

Apart from username/password authentication, LMIWBEM is capable of using X509 certificate and private key for authentication purposes. See the next example:

import lmiwbem

conn = lmiwbem.WBEMConnection()
conn.connect(
    "hostname",
    cert_file="path/to/certificate.pem",
    key_file="path/to/private_key.pem")

# CIM operations

conn.disconnect()

Or PyWBEM’s way:

import lmiwbem

conn = lmiwbem.WBEMConnection(
    "hostname",
    x509={
        "cert_file" : "path/to/certificate.pem",
        "key_file"  : "path/to/private_key.pem"})

# CIM operations

NOTE: When WBEMConnection.connect() is not called, temporary connection for each CIM operations will be performed.

Connection using Unix Socket

LMIWBEM is capable of creating a connection via Unix socket. See next 3 following examples:

import lmiwbem

conn = lmiwbem.WBEMConnection()
conn.connectLocally()

# CIM operations

conn.disconnect()

Or:

import lmiwbem

conn = lmiwbem.WBEMConnection(connect_locally=True)
conn.connect() # No need to call connectLocally()

# CIM operations

conn.disconnect()

Even:

import lmiwbem

conn = lmiwbem.WBEMConnection(connect_locally=True)

# CIM operations

NOTE: When WBEMConnection.connect() is not called, temporary connection for each CIM operations will be performed.

Indication Listener

This section briefly describes how to create CIMIndicationListener and start listening for incoming indications.

import lmiwbem

def handler(indication, *args, **kwargs):
    """
    :param CIMInstance indication: exported instance
    :param *args: positional arguments
    :param **kwargs: keyword arguments
    """
    # Do something with exported indication
    pass

 listener = lmiwbem.CIMIndicationListener()
 listener.add_handler("indication_name", handler, *args, **kwargs)
 listener.start()

 #
 # Do something useful here
 #

 listener.stop()

Break-down:

  1. Define an indication handler:

    def handler(indication, *args, **kwargs):
        ...
    

    The handler needs to take at least 1 argument (indication in the example) and it can also accept any other positional and keyword arguments. Values of these optional arguments will be defined when registering the indication handler in an indication listener.

  2. Create an indication listener and register the indication handler:

    listener = lmiwbem.CIMIndicationListener()
    listener.add_handler("indication_name", handler, *args, **kwargs)
    

    CIMIndicationListener.add_handler() takes at least 2 arguments, where the first one defines the indication name and the second is callable handler. It is possible to pass any optional (user defined) positional and keywords arguments here – they will passed to indication handler when the indication arrives.

    NOTE: When creating indication subscription, it is substantial to prefix indication name with “CIMListener”. If the prefix is missing, indication will not be processed by CIMIndicationListener. The next listing demonstrates CIMIndicationHandler instance with proper Destination property:

    instance of CIM_IndicationHandlerCIMXML {
        ...
    
        Destination = 'https://destination:port/CIMListener/indication_name';
                                                ^^^^^^^^^^^
                                             THIS IS IMPORTANT
    
        ...
    };
    
  3. Start the indication listener:

    listener.start()
    
  4. Some user-defined code

    #
    # Do something useful here
    #
    
  5. Stop the indication listener:

    listener.stop()