< Home

Example for Using the User-defined Environment Variable to Monitor Stack Status Change

Networking Requirements

As shown in Figure 1, the user remotely manages the stack through a network. The user wants to detect the change in time and record logs when the specified member switch in the stack restarts.

Figure 1 Networking for monitoring stack status change

Configuration Roadmap

  1. Configure the IP address of VLANIF 10 on the master switch in a stack to the management IP address of the stack.

  2. Make a Python script slot_syslog.py to monitor whether member switches in the stack are restarted and record logs.

  3. Log in to the switch remotely, upload the Python script to the switch, and install the script.

  4. Configure a user-defined environment variable and specify the stack ID of the member switch to be monitored. Using the user-defined environment variable, the user can switch the member switch to be monitored without rewriting the Python script.

  5. Configure a Python script assistant.

Procedure

  1. Make a Python script.

    # Make a Python script slot_syslog.py to implement the following functions:
    • Subscribe to the event of stack status change, monitor whether member switches in the stack are restarted, and record logs when a member switch or card is restarted.

    • Specify the stack ID of the member switch.

  2. Upload and install the Python script.

    # Log in to the switch remotely and upload the Python script from the PC to the switch. For details about how to upload a file, see File Management in S2720, S5700, and S6700 V200R019C10 Configuration Guide - Basic Configuration.

    # Install the Python script on the switch.

    <Switch> ops install file slot_syslog.py

  3. Configure a user-defined environment variable.

    # Configure the user-defined environment variable slotid and specify the stack ID of the member switch to be monitored. You can switch the member switch without rewriting the Python script. For example, to monitor SwitchB, set slotid to 2. When the monitored member switch is changed to SwitchC, set slotid to 3. The following uses the slotid set to 2.

    <Switch> system-view
    [Switch] ops
    [Switch-ops] environment slotid 2

  4. Configure a Python script assistant.

    # Configure a Python script assistant and register the subscription event in the script slot_syslog.py. Wait for the event to be triggered.

    [Switch-ops] script-assistant python slot_syslog.py
    [Switch-ops] quit

  5. Verify the configuration.

    # Enable the log function. When the user-defined environment variable slotid is set to 2 and SwitchB is restarted, the switch generates logs.

    [Switch] info-center enable
    [Switch] quit
    <Switch> terminal monitor
    <Switch> terminal logging
    <Switch>
    Apr 08 2018 14:29:17+08:00 Switch %%01OPSA/6/SCRIPT_LOG: OPS: Slot 2 has been reset. (user="slot_syslog.py", session=864036019).

Configuration Files and Scripts

  • Configuration file of the switch

    #
    ops
     script-assistant python slot_syslog.py
     environment slotid 2
    #
    return
  • Example of the script slot_syslog.py

    # -*- coding: utf-8 -*-
    import ops         # Import the OPS module.
    import sys         # Import the sys module.
    import os          # Import the OS module.
    # Subscription processing function
    def ops_condition (ops):
    	# Obtain the value of the user-defined environment variable slotid.
    	slotid, errstr = ops.environment.get("slotid")
    	# Monitor the event of restarting the member switch in the specified slot.
    	slot = "slot " + slotid
    	value1, err_str1 = ops.device.subscribe("a12", slot, "RESET")
    	return 0
    # Work processing function
    def ops_execute (ops):
    	# Obtain the value of the user-defined environment variable slotid.
    	slotid, errstr = ops.environment.get("slotid")
    	slot = "slot " + slotid
    	logstring = "Slot %s has been reset." %slotid
    	# Generate the user log Slot x has been reset.
    	status, err_log = ops.syslog(logstring, "informational", "syslog")
Copyright © Huawei Technologies Co., Ltd.
Copyright © Huawei Technologies Co., Ltd.
< Previous topic Next topic >