This is a script to pull switch port stats from our access switches in our network. The data pulled in this script is used for ‘right sizing’ and network planning.
We can then export the results, and pivot table them to see the % of switch ports in use/not-in use. I’ll add this attachment later.
/**
* @intent Show int counters on Arista & IOS-XE Switches
* @description
* 1. Check to see what switch ports have had no traffic accross the wire for sizing and planning.
* 2. Match on platform.os
* 3. Use the information from the custom command 'show int counters'.
* 4. Get counts of switch ports on that device so we can use to calculate % in use
**/
// Pattern of output
// Arista & Cisco Same Columns
Traffic =
```
{Port:string} {InOctets:number} {InUcastPkts:number} {InMcastPkts:number} {InBcastPkts:number}
```;
// Arista Function
EOS_Stats =
foreach device in network.devices
where device.platform.os == OS.ARISTA_EOS
foreach command in device.outputs.commands
where command.commandText == "sh int counters"
let parsed = parseConfigBlocks(OS.ARISTA_EOS, command.response)
let matchData = blockMatches(parsed, Traffic)
foreach iface in device.interfaces
// get 1 match per interface
let x = max(foreach y in matchData
where iface.name == toLowerCase(y.data.Port)
select y)
where isPresent(x)
select {
name: device.name,
Vendor: device.platform.vendor,
Model: device.platform.model,
OS: device.platform.os,
Tag: device.tagNames,
Port: iface.name,
OperStatus: iface.operStatus,
Description: iface.description,
InOctets: x.data.InOctets,
InUcastPkts: x.data.InUcastPkts,
InMcastPkts: x.data.InMcastPkts,
InBcastPkts: x.data.InBcastPkts,
InterfaceCount: length(device.interfaces),
};
// Cisco Function
Cisco_Stats =
foreach device in network.devices
where device.platform.vendor == Vendor.CISCO
foreach command in device.outputs.commands
where command.commandText == "sh int counters"
let parsed = parseConfigBlocks(OS.IOS_XE, command.response)
let matchData = blockMatches(parsed, Traffic)
foreach iface in device.interfaces
// get 1 match per interface
let x = max(foreach y in matchData
where iface.name == toLowerCase(y.data.Port)
select y)
where isPresent(x)
select {
name: device.name,
Vendor: device.platform.vendor,
Model: device.platform.model,
OS: device.platform.os,
Tag: device.tagNames,
Port: iface.name,
OperStatus: iface.operStatus,
Description: iface.description,
InOctets: x.data.InOctets,
InUcastPkts: x.data.InUcastPkts,
InMcastPkts: x.data.InMcastPkts,
InBcastPkts: x.data.InBcastPkts,
InterfaceCount: length(device.interfaces),
};
// Combine Functions
ifaceStats = EOS_Stats + Cisco_Stats;
foreach result in ifaceStats
select {
Hostname: result.name,
Vendor: result.Vendor,
Model: result.Model,
OS: result.OS,
Tag: result.Tag,
OperStatus: result.OperStatus,
Desc: result.Description,
Port: result.Port,
InUcastPkts: result.InUcastPkts,
InMcastPkts: result.InMcastPkts,
InBcastPkts: result.InBcastPkts,
InterfaceCount: result.InterfaceCount
}