Example for Configuring an Assistant for Automatic Health Check (Using a Python Script)

This section provides an example for configuring an assistant for automatic health check using a Python script.

Networking Requirements

As shown in Figure 1, the remote server is a TFTP server. Device A and the TFTP server have reachable routes to each other. To reduce maintenance workload, configure DeviceA to automatically collect health information and send it to the TFTP server for analysis.

Figure 1 Automatic health check using a Python script

Configuration Roadmap

The configuration roadmap is as follows:
  1. Compile a Python script, upload, and install it on DeviceA.

  2. Create a script assistant.


  1. Compile a Python script.

    # Compile a Python script named health.py.

    In the Python script, set the trigger condition as the timer and the task as executing commands to collect device information, including hardware status, route status, and interface link status, and to send the information to the TFTP server. The Python script is as follows:
    # Define the function of the trigger condition.
    def ops_condition(_ops):
    	_ops.timer.cron("con1","0 1 * * * *")      # Set the timer event.
    # Define the functions of tasks.
    def ops_execute(_ops):
    	handle, err_desp  = _ops.cli.open()      # Open the CLI channel.
    	_ops.cli.execute(handle,"display device > health.txt")      # Execute CLIs.
    	_ops.cli.execute(handle,"display health >> health.txt")
    	_ops.cli.execute(handle,"display ip routing-table >> health.txt")
    	_ops.cli.execute(handle,"display lldp neighbor brief >> health.txt")
    	_ops.cli.execute(handle,"tftp put health.txt")
    	ret = _ops.cli.close(handle)      # Close the CLI channel.
    	return 0

  2. Upload and install the Python script.

    # Configure DeviceA functioning as a TFTP client to download the Python script file health.py from the TFTP server. The Python script is stored on the TFTP server.

    <HUAWEI> system-view
    [~HUAWEI] sysname DeviceA
    [*HUAWEI] commit
    [~DeviceA] quit
    <DeviceA> tftp get health.py
    Info: Transfer file in binary mode.
    Please wait for a while...
    /    635 bytes transferred
    Info: Downloaded the file successfully.

    # Install the Python script on DeviceA.

    <DeviceA> ops install file health.py

  3. Configure a script assistant.

    <DeviceA> system-view
    [~DeviceA] ops
    [~DeviceA-ops] script-assistant python health.py
    [*DeviceA-ops] commit
    [~DeviceA-ops] return

  4. Verify the configuration.

    # Check the configuration of the assistant.

    <DeviceA> display ops assistant verbose name health.py
    Assistant information
      Name                 : health.py
      State                : ready
      Type                 : script
      Default assistant    : no
     Running statistics
      Running times        : 0
      Queue size/(free)    : 10/(10)
      Skip for queue full  : 0
      Skip for delay       : 0
      Skip for suppression : 0
      Skip for error       : 0
     Execute information
      Task abstract        : health.py : ops_execute()
     Trigger control
      Occurs threshold     : 1
      Period (s)           : 0
      Delay (s)            : 0
      Suppress max         : 0
      Hits in period       : 0
     Condition information
      Correlate expression : con1
      Condition tag        : con1
        Condition type     : timer
        Subscribe result   : success
        Occurs threshold   : 0
        Period (s)         : 0
        Hits in period     : 0

Configuration Files

  • Device A configuration file

    sysname DeviceA
     script-assistant python health.py
Copyright © Huawei Technologies Co., Ltd.
Copyright © Huawei Technologies Co., Ltd.
< Previous topic