lib/ServState/network_usage.rb in ServState-0.1.2 vs lib/ServState/network_usage.rb in ServState-0.1.3
- old
+ new
@@ -7,11 +7,11 @@
def update
time_diff = Time.new - @old_time
@old_time = Time.new
- data = net_dev_data
+ data = net_data
data.each do |interface, values|
down = values[0] - @old_traffic[interface][0]
up = values[1] - @old_traffic[interface][1]
@@ -27,20 +27,26 @@
private
def initialize
@speeds = Hash.new
@old_time = Time.new
- @old_traffic = net_dev_data
+ @old_traffic = net_data
+ update
end
# returns hash with hash[:interface] = [total download bytes, total upload bytes]
- def net_dev_data
- lines = IO.readlines('/proc/net/dev').grep(/:/)
+ def net_data
+ # now we have two problems
+ output = `ip -s link`.split("\n").grep(/([0-9]:\ )|(([0-9]+\ +){6})/)
data = Hash.new
- lines.each do |line|
- splited = line.split
- data[splited[0].chop.to_sym] = [splited[1].to_i, splited[9].to_i]
+ output.each_cons(3) do |lines|
+ if lines[0] =~ /^[0-9]:/
+ name = lines[0].split[1].chop
+ down = lines[1].split[0].to_i
+ up = lines[2].split[0].to_i
+ data[name] = [down, up]
+ end
end
data
end
end
\ No newline at end of file