DCImanager 6
en En
es Es

Creating a UPS handler

You can add a custom UPS handler to DCImanager 6. To do this, create the handler code and upload it to the platform.

Preparing the Environment

The UPS handler must be written in Python. We recommend using Python version 3.9 or higher.

You can create a handler based on an existing project. To copy the project, connect to the DCImanager 6 location server via SSH and run the command:

docker cp eservice_handler:/opt/ispsystem/equip_handler ./

The following project directories may be useful when creating a handler:

  • /common — common helper classes and functions;
  • /ups_common — helper classes and functions for working with UPS;
  • /ups_common/handlers — UPS handlers.

You can see the required Python libraries and their versions in the requirements.txt project file. To install the necessary libraries, run the command:

pip3 install -r requirements.txt

To check data types in the project, we recommend using the mypy analyzer.

Creating a handler

Class for working with UPS

The UPS class inherits from the class:

  • BaseSnmpUps — for devices operating via SNMP;
  • BaseUps — for other devices.

The BaseSnmpUps class contains methods for interacting with the UPS via SNMP:

  • snmp_get — perform a read request by a specific OID;
  • snmp_set — perform a modifying request by a specific OID;
  • snmp_walk — perform a request that returns a list.
Example of class description
class ExampleHandlerSnmp(BaseSnmpUps):
    """Example handler class."""

    def __init__(self, ups_data: UpsSnmpData):
        """__init__.

        Args:
            ups_data (UpsSnmpData): ups connection data
        """
        super().__init__(ups_data)

Methods for working with UPS

For DCImanager 6 to interact with the UPS, override the methods of the BaseUps class:

  • status — UPS polling;
  • port_up — enable port;
  • port_down — disable port;
  • statistic — collect statistics.

The equipment service uses not raw json data, but their object representation. Instances of the UpsView class are the UPS representation. It has the following parameters:

  • load_percentage — load, %;
  • charge_time — charge time, min;
  • input_power — output power, kW;
  • output_power — power consumption, kW.

The UpsData class inherits from the base class ConnectionParams and defines the data for accessing the UPS.

Each handler file must contain the function make_handler. This function creates the handler object:

Example function
def make_handler(ups_data: UpsData) -> BaseSnmpUps:
    """
    Returns APC UPS handler object
    :param ups_data: Snmp UPS data object
    :return:
    """
    return ApcSnmp(ups_data)

Example handler code

Example code

Uploading the handler to the platform

To upload the handler to the platform:

  1. Create a directory with the following structure:

    handler_dir/
    ├── __init__.py
    └── my_handler.py
    Explanations
  2. Create a tar.gz archive with this directory:

    tar -czvf custom_handler.tar.gz handler_dir
    Command explanations
  3. Authenticate in DCImanager 6 with administrator rights:

    curl -o- -k https://example.com/api/auth/v4/public/token \
        -H "isp-box-instance: true" \
        -d '{
            "email": "<admin_email>",
            "password": "<admin_pass>"
        }'
    
    Command explanations

    The response will be of the form:

    {"id":"24","token":"24-cee181d2-ccaa-4b64-a229-234aa7a25db6"}

    Save the value of the token parameter from the received response — the authorization token.

  4. Create a description for the handler:

    curl -o- -k https://example.com/api/eservice/v3/custom_equipment \
        -H "isp-box-instance: true" \
        -H "x-xsrf-token: <token>" \
        -d '{
            "device_type": "<device>",
            "handler": "<internal_handler_name>",
            "name": "<handler_name>",
            "protocol": ["<handler_protocol>"],
            "features": []
        }'
    
    Command explanations

    The response will contain the id of the created handler. Save this value.

    Example response
    {"id": 1}
  5. Upload the archive with the handler to the platform:

    curl -o- -k https://example.com/api/eservice/v3/custom_equipment/<handler_id>/content \
        -H "isp-box-instance: true" \
        -H "x-xsrf-token: <token>" \
        -F "data=@custom_handler.tar.gz" \
        -F "handler_import=<import_path>"
    Command explanations
    You can also use this command to upload new versions of the handler to the platform.