Skip to content

Instantly share code, notes, and snippets.

@johnl
Created May 13, 2015 15:30
Show Gist options
  • Save johnl/e7f586489c4a6e0a8fd0 to your computer and use it in GitHub Desktop.
Save johnl/e7f586489c4a6e0a8fd0 to your computer and use it in GitHub Desktop.
script to parse Linux scsi command logs
#!/usr/bin/ruby
# Parses linux scsi command logs
# http://www.seagate.com/staticfiles/support/disc/manuals/scsi/100293068a.pdf
# give this program itself as input to test
# DE AD BE EF == 3735928559
# CA FE == 51966
# CDB: Write(10): 2a 08 DE AD BE EF 00 CA FE 00
block_size = 512
STDIN.readlines.each do |line|
m = line.scan(/CDB: (.*)\(([0-9]+)\): (.*)/)
next if m.empty?
m.flatten!
opname = m[0]
opnum = m[1].to_i
bytes = m[2].split.collect { |b| b.to_i(16) }.pack('C*')
if (opname == 'Read' || opname == 'Write') && opnum == 10
fields = bytes.unpack('CCNCnC')
flags = fields[1]
dpo = flags[4]
fua = flags[3]
fua_nv = flags[1]
offset = fields[2] * block_size
length = fields[4] * block_size
group = [fields[5][4], fields[5][3],fields[5][2],fields[5][1],fields[5][0]].join
puts "#{opname} (#{opnum}) offset:#{offset} length:#{length} dpo:#{dpo} fua:#{fua} fua_nv:#{fua_nv} group:#{group}"
elsif (opname == 'Synchronize Cache' && opnum == 10)
fields = bytes.unpack('CCNCnC')
flags = fields[1]
sync_nv = flags[2]
immed = flags[1]
offset = fields[2] * block_size
length = fields[4] * block_size
group = [fields[5][4], fields[5][3],fields[5][2],fields[5][1],fields[5][0]].join
# sync_nv: 0:sync to medium. 1: sync to non-volatile cache, or medium if no cache
# length: if 0, sync whole device
puts "#{opname} (#{opnum}) offset:#{offset} length:#{length} sync_nv:#{sync_nv} immed:#{immed} group:#{group}"
else
puts "Unknown op: #{line}"
end
end
@ymdysk
Copy link

ymdysk commented Apr 14, 2018

Thank you for the great parser! It helped me to analyze SCSI commands.
And I added some lines to see Write(16) and Read(16).
By the way, what kind of license is applied for the code? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment