module Vidibus::Sysinfo::Throughput

Returns currently used throughput in MBit/s.

Analyzes /proc/net/dev

Public Class Methods

call(seconds = 1) click to toggle source

Provide seconds to sleep between first and second call. The higher the seconds, the more accurate are the results.

# File lib/vidibus/sysinfo/throughput.rb, line 38
def call(seconds = 1)
  seconds = seconds.to_i
  values = []
  2.times do
    output, error = perform(command)
    values << respond(output, error)
    sleep(seconds) unless values.size > 1
  end
  result = {}
  [:input, :output].each do |type|
    megs = values[1][type] - values[0][type]
    result[type] = round((megs*8)/seconds)
  end
  Result.new(result)
end
command() click to toggle source
# File lib/vidibus/sysinfo/throughput.rb, line 32
def command
  "cat /proc/net/dev | grep eth0:"
end
explain(error) click to toggle source
# File lib/vidibus/sysinfo/throughput.rb, line 67
def explain(error)
  if error.match("No such file or directory")
    return "This system does not provide /proc/net/dev"
  end
end
parse(output) click to toggle source

Returns received and sent megabytes.

# File lib/vidibus/sysinfo/throughput.rb, line 55
def parse(output)
  if output.match(/eth0\:\s*([\d\s]+)/)
    numbers = $1.split(/\s+/)
    input = numbers[0].to_i
    output = numbers[8].to_i
    {
      input: megabytes(input),
      output: megabytes(output)
    }
  end
end