This section provides an example for configuring an assistant for automatic health check using a Python script.
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.
# Compile a Python script named health.py.
# Define the function of the trigger condition. def ops_condition(_ops): _ops.timer.cron("con1","0 1 * * * *") # Set the timer event. _ops.correlate("con1") # 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 10.2.1.1 put health.txt") ret = _ops.cli.close(handle) # Close the CLI channel. return 0
# 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 10.2.1.1 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
<DeviceA> system-view [~DeviceA] ops [~DeviceA-ops] script-assistant python health.py [*DeviceA-ops] commit [~DeviceA-ops] return
# 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