Module: Trafficmonitor::Iftop

Defined in:
lib/trafficmonitor/iftop.rb

Instance Method Summary collapse

Instance Method Details

#connections(interface, s = 2) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/trafficmonitor/iftop.rb', line 5

def connections(interface, s = 2)
  command = 'sudo iftop -B -Nn -t -L 100  -s ' + s.to_s + ' -i ' + interface

  stdin, stdout, stderr = Open3.popen3(command)
  result = stdout.readlines
  lines = result[3..-10]

  conns = {"connection" => []}

  lines.each_slice(2) do |i|
    download = i[0].split(' ')
    upload = i[1].split(' ')
    remote_ip, download_speed = download[1], remove_speed_unit(download[3])
    local_ip, upload_speed = upload[0], remove_speed_unit(upload[2])

    conns['connection'] << {"local_ip" => local_ip,
                            "download_speed" => download_speed,
                            "remote_ip" => remote_ip,
                            "upload_speed" => upload_speed}
  end

  total_send_rate_2s = result[-8].split(' ')[3]
  total_receive_rate_2s = result[-7].split(' ')[3]

  conns['total_send_rate'] = remove_speed_unit(total_send_rate_2s)
  conns['total_receive_rate'] = remove_speed_unit(total_receive_rate_2s)

  return conns
end