Skip to content

Instantly share code, notes, and snippets.

@ytti
Last active January 18, 2021 01:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ytti/436fe3b602a963acf21e to your computer and use it in GitHub Desktop.
Save ytti/436fe3b602a963acf21e to your computer and use it in GitHub Desktop.
XR NPU counter to pcap
#!/usr/bin/env ruby
## 1) capture data => monitor np counter MDF_PUNT_POLICE_DROP np0 count 25 location 0/2/CPU0
##
## 2) You'll need 'text2pcap' => sudo apt-get install wireshark-common
##
## 3) Run with file as argument:
## [ytti@lintukoto ~/tmp/ios-xr-npu-monitor-pcap]% ./xr2pcap.rb r04.miamfl02.us.bb.MDF_PUNT_POLICE_DROP.ioscap
## Input from: r04.miamfl02.us.bb.MDF_PUNT_POLICE_DROP.txt
## Output to: r04.miamfl02.us.bb.MDF_PUNT_POLICE_DROP.pcap
## Output format: PCAP
## ...
## Read 20 potential packets, wrote 20 packets (2791 bytes).
##
## 4) Run with data from stdin:
## [ytti@lintukoto ~/tmp/ios-xr-npu-monitor-pcap]% cat r04.miamfl02.us.bb.MDF_PUNT_POLICE_DROP.ioscap|./xr2pcap.rb
## Input from: packets.txt
## Output to: packets.pcap
## Output format: PCAP
## ...
## Read 20 potential packets, wrote 20 packets (2791 bytes).
class XRNPUCounter
PACKET_START = /byte packet/
LINE_RE = /^....: (.{48}).*/
def self.parse lines
c = new
packets = c.parse_lines lines
c.parse_packets packets
end
def parse_lines lines
packet = false
count = -1
packets = []
lines.each do |line|
next if not packet and not line.match PACKET_START
if line.match PACKET_START
packet = true
count += 1
packets[count] = ""
next
end
packets[count] += line
packet = false if not line.match(/^\d/)
end
packets
end
def parse_packets packets
parsed = []
packets.each do |packet|
hex = []
packet.lines.each do |line|
line = line.sub LINE_RE, '\1'
hex << line.split
end
parsed.push "0000000 " + hex.flatten.join(" ")
end
parsed
end
end
if $0 == __FILE__
begin
packets = XRNPUCounter.parse ARGF
file = ARGF.filename.split "."
file = if file.size > 1
file[0..-2].join "."
else
"packets"
end
File.write "#{file}.txt", packets.join("\n")
# we could write the packets to STDIN via popen, but meh, nice to have interim
%x(text2pcap #{file}.txt #{file}.pcap)
rescue => error
warn error.class.to_s + ": " + error.message
raise if $DEBUG
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment