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.
Configure the IP address of VLANIF 10 on the master switch in a stack to the management IP address of the stack.
Make a Python script slot_syslog.py to monitor whether member switches in the stack are restarted and record logs.
Log in to the switch remotely, upload the Python script to the switch, and install the script.
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.
Configure a Python script assistant.
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.
# 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
# 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
# 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
# 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 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")