I had a client that wished to know whether ports with SFPs had fibre attached to them on NXOS devices. SFPs with Digital Optical Monitoring can report the received levels of light on the SFP, no or very low light (sub -30db) means that there is no fibre attached.
So the challenge was to create a command line interface command to display whether fibre was attached or not. The objective was to demonstrate the programmability of NXOS and how to leverage NXSDK to create command line arguments, and run them as a service.
This project is using the inbuilt Python 2.7 interpreter that comes shipped with NXOS as well as leveraging NXSDK
Cisco Products:
This simple application will display the SFP connected status of all ports on the switch. Where an
SFP supports Digital Optical Monitoring, the application will display the received power levels, and
based upon the power level received will report whether fibre is connected to an active device at the
opposite end of the link.
n9kv1# show FibrePorts ports
INTERFACE STATE SFP FIBRE POWER ----------------------------------------------------------------- Ethernet1/1 up not applicable No Fibre N/A Ethernet1/10 down not applicable No Fibre N/A
The application can either be run directly from the NXOS Bash Shell or installed as a Service.
The advantage of running as a service is that the application is persistent between reboots as the service is installed as part of the configuration.
Copy the file fibreports.py to bootflash of the NXOS switch.
scp ~/FibreAttachedPorts/FibrePorts.py <username>@<9k_address_or_fqdn>:
Login to your NXOS switch
ssh <username>@<9k_address_or_fqdn>
Run bash and start up the python program. Use nohup so that the application continues to run when the bash session closes and use & for the application to run in the background
n9k# run bash bash-4.2$ cd /bootflash bash-4.2$ nohup /isan/bin/python FibrePorts.py &
STOPPING THE APPLICATION
If you need to stop the application then do not kill the process in Bash. The recommended method of stopping the application is from the NXOS switch command line
n9k#(config)FibrePorts.py stop-event-loop
The advantages of running as a Service is that the application can be controlled by the operator from the NXOS command line. Additionally the application will be persistent and will continue to run after device reload.
The rpm in order to run the application as a service has been included in this repo. The rpm will need to be copied to the bootflash of the NXOS switch.
scp ~/FibreAttachedPorts/RPMs/FibrePorts.py-1.0-1.5.0.x86_64.rpm \<username>@<9k_address_or_fqdn>:
Login to your NXOS switch
ssh <username>@<9k_address_or_fqdn>
Check if NXSDK feature is running
n9k#(config)show feature
If NXSDK feature is disabled then enable by issuing the following command
n9k#(config)feature nxsdk
Now install the rpm within Bash and exit back NXOS
n9k# run bash sudo su bash-4.3# yum install /bootflash/FibrePorts.py-1.0-1.5.0.x86_64.rpm bash-4.3# exit exit n9k#
Initiate the service from the command line
n9k(config)# nxsdk service-name FibrePorts.py % This could take some time. "show nxsdk internal service" to check if your App is Started & Runnning
The service is now running, to check the state of the service "show nxsdk internal service"
Stopping the service
To stop the service issue the following command
n9k(config)# no nxsdk service-name FibrePorts.py
For further details on creating and deploying services with NXSDK please go to NXSDK Guide
Timer - Syslog
The application in it's default state will create a syslog message every 2 minutes to log the that the service is still alive. The form of the syslog message will be in the format below
2019 May 31 14:35:31 n9k nxsdk: Fibreports Timer ticked - 1
To stop the messages then comment out the following code (line 255)
timer_thread.start()
Power Levels
The power level defaults to -30db. To adjust this value then amend the variable
POWERLEVEL = -30
Smart people responsible for the creation and maintenance of this project:
This project is licensed to you under the terms of the Cisco Sample
Code License.
A large Cisco Enterprise customer had a number of NXOS-capable switches in their data center. The switches were fully populated with SFPs, but administrators were unable to easily determine whether fiber was attached to the devices remotely. If fiber is attached between two switches and the SFPs support DOM, then regardless of the interface state (up/down) the SFP will report on light received. If there is no fiber attached, then the SFP may report ambient light, but this would be in excess of -30db, and thus can be assumed to have no fiber attached.
NXOS includes Bash capability and a built-in Python interpreter. It also includes the nx_sdk_py
library, which provides the capability to interact with NXOS and enables the creation of custom CLI commands through the invocation of callback classes. These features enable administrators to use a script (FibrePorts.py
) to report on the light levels reported by DOM.
Initially, the script was run directly from Bash:
bash-4.2$ nohup /isan/bin/python FibrePorts.py &
It worked, but there was no persistence and there were only limited means of verifying that the code was still running. To overcome this issue, the NXSDK toolkit was used to create an RPM that can be distributed to the switches. The customer also required that informational syslog messages be sent every two minutes to confirm that the service was running. The nx_sdk_py
library runs in a thread separate from the main execution code; therefore, care had to be taken when creating an additional thread for syslog messaging.
The RPM is installed on NXOS via Bash using YUM. Once YUM has installed the RPM package, the code can be treated as a service and invoked from the command line:
n9k(config)# nxsdk service-name FibrePorts.py
The advantages are:
The disadvantages are:
The following additional features may be added later:
Code Exchange Community
Get help, share code, and collaborate with other developers in the Code Exchange community.View Community