No relevant resource is found in the selected language.

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Read our privacy policy>Search

Reminder

To have a better experience, please upgrade your IE browser.

upgrade

Configuration Guide - Device Management

CloudEngine 8800, 7800, 6800, and 5800 V200R005C10

This document describes the configurations of Device Management, including device status query, hardware management, Information Center Configuration, NTP, Synchronous Ethernet Configuration, Fault Management Configuration, Energy-Saving Management Configuration, Performance Management Configuration, Maintenance Assistant Configuration, and OPS Configuration.
Rate and give feedback:
Huawei uses machine translation combined with human proofreading to translate this document to different languages in order to help you better understand the content of this document. Note: Even the most advanced machine translation cannot match the quality of professional translators. Huawei shall not bear any responsibility for translation accuracy and it is recommended that you refer to the English document (a link for which has been provided).
OPS Scripting and Sample Template

OPS Scripting and Sample Template

Currently, the OPS integrates the Python running environment and can run Python scripts. In a Python script, methods of sending HTTP requests to the system need to be defined based on open RESTful APIs. When the Python script runs, HTTP requests are sent to the system to implement system management.

The following example describes how to make Python scripts to obtain device startup information.
NOTE:
  • When using the OPS function, you need to be familiar with the Python language and write Python scripts correctly.

  • The following Python script is for your reference. You can modify the script according to service requirements.

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import traceback
import httplib
import string

# Define the class that invokes RESTful APIs. This class defines some methods to perform the operations of setting up an HTTP connection. You can invoke this part of script without modifying it.
# You can invoke this part of script without modifying it.
class OPSConnection(object):
    """Make an OPS connection instance."""

    # Initialization class that creates an HTTP connection
    def __init__(self, host, port = 80):
        self.host = host
        self.port = port
        self.headers = {
            "Content-type": "text/xml",
            "Accept":       "text/xml"
            }
        self.conn = None

    # Close an HTTP connection.
    def close(self):
        """Close the connection"""
        self.conn.close()

    # Create device resources.
    def create(self, uri, req_data):
        """Create operation"""
        ret = self.rest_call("POST", uri, req_data)
        return ret

    # Delete device resources.
    def delete(self, uri, req_data):
        """Delete operation"""
        ret = self.rest_call("DELETE", uri, req_data)
        return ret

    # Query device resources.
    def get(self, uri, req_data = None):
        """Get operation"""
        ret = self.rest_call("GET", uri, req_data)
        return ret

    # Modify device resources.
    def set(self, uri, req_data):
        """Set operation"""
        ret = self.rest_call("PUT", uri, req_data)
        return ret

    # Invoke classes internally.
    def rest_call(self, method, uri, req_data):
        """REST call"""
        print('|---------------------------------- request: ----------------------------------|')
        print('%s %s HTTP/1.1\n' % (method, uri))
        if req_data == None:
            body = ""
        else:
            body = req_data
            print(body)
        if self.conn:
            self.conn.close()
        self.conn = httplib.HTTPConnection(self.host, self.port)

        self.conn.request(method, uri, body, self.headers)
        response = self.conn.getresponse()
        response.status = httplib.OK    # stub code
        ret = (response.status, response.reason, response.read())
        print('|---------------------------------- response: ---------------------------------|')
        print('HTTP/1.1 %s %s\n\n%s' % ret)
        print('|------------------------------------------------------------------------------|')
        return ret

# Define the function for querying device startup information.
def get_startup_info(ops_conn):

    # Specify the uniform resource identifier (URI) of system startup information. The URI is the managed object defined in Resetful APIs. Different managed objects have different URIs.
    # You can modify the URI according to service requirements. For the supported URIs, see RESTful API.
    uri = "/cfg/startupInfos/startupInfo"

    # Specify the request to be sent. Different URIs correspond to different requests. 
    # You can modify the request based on the actual URI. For details about the request format, see RESTful API.
    req_data = \
'''<?xml version="1.0" encoding="UTF-8"?>
<startupInfo>
</startupInfo>
'''
    
    # Perform a get operation request. uri and req_data indicate the request URI and request data. ret indicates whether a request is successful, and rsp_data indicates the data sent in response to the request. For details about the response data, see RESTful API.
    # The following provides the response data of device startup information. You can parse the response data to obtain device startup information.
    '''
    <?xml version="1.0" encoding="UTF-8"?>
    <rpc-reply>
      <data>
        <cfg xmlns="http://www.huawei.com/netconf/vrp" format-version="1.0" content-version="1.0">
          <startupInfos>
            <startupInfo>
              <position>6</position>
              <nextStartupFile>flash:/vrpcfg.cfg</nextStartupFile>
              <configedSysSoft>flash:/system-software.cc</configedSysSoft>
              <curSysSoft>flash:/system-software.cc</curSysSoft>
              <nextSysSoft>flash:/system-software.cc</nextSysSoft>
              <curStartupFile>flash:/vrpcfg.cfg</curStartupFile>
              <curPatchFile>NULL</curPatchFile>
              <nextPatchFile>NULL</nextPatchFile>
            </startupInfo>
          </startupInfos>
        </cfg>
      </data>
    </rpc-reply>
    '''
    # You can change the request type get() according to service requirements. For example, change get() to set() or create().
    ret, _, rsp_data = ops_conn.get(uri, req_data)
    if ret != httplib.OK:
        return None

    return rsp_data

    # main() function defines the operations to be performed by this script. You can modify the function according to service requirements.
def main():
    """The main function."""

    # host indicates a loop address. Currently, RESTful API can only be invoked within the device. That is, the value is localhost.
    host = "localhost"
    try:
        # Set up an HTTP connection.
        ops_conn = OPSConnection(host)
        # Invoke the function for obtaining device startup information.
        rsp_data = get_startup_info(ops_conn)
        # Close an HTTP connection.
        ops_conn.close()
        return

    except:
        errinfo = traceback.format_exc()
        print(errinfo)
        return

if __name__ == "__main__":
    main()

Translation
Download
Updated: 2019-04-20

Document ID: EDOC1100075362

Views: 25377

Downloads: 102

Average rating:
This Document Applies to these Products
Related Version
Related Documents
Share
Previous Next