写一个小脚本,分析linux网卡流量
用法:./traff.rb
or ./traff.rb eth0
or ./traff.rb eth1
#!/usr/bin/ruby
#
# usage: ./traff.rb
# author: zuerrong
#
dev = ARGV || 'eth0'
def getTraff(device)
throuIn,throuOut = 0,0
File.open("/proc/net/dev").each do |s|
if s =~ /#{device}:\d+/
arr = s.split
throuOut = arr.to_i
throuIn= arr.split(/:/).pop.to_i
end
end
return throuIn+throuOut
end
v1 = getTraff(dev)
sleep 1
v2 = getTraff(dev)
printf "Traffic on #{dev} is: %10d KB/s\n",(v2-v1)/1024
页:
[1]