-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththroughput.rb
executable file
·50 lines (44 loc) · 1.21 KB
/
throughput.rb
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/ruby
require 'rubygems'
require 'snmp'
require 'lib/router'
port = 2000
require 'socket'
@routers = []
Thread.new do
loop do
@routers.each { | router | router.get_counters }
sleep(10)
end
end
TCPServer.open(port) do | s |
loop do
Thread.start(s.accept) do | client |
begin
while t = client.gets
begin
if t.strip =~ /^([^\ ]+)\ ([0-9]+)\ (in|out)$/
ip_address, interface, direction = $1, $2.to_i, $3
router = @routers.find { | router | router.ip_address == ip_address }
if router.nil?
@routers << Router.new(:ip_address => ip_address, :interfaces => [interface])
else
if @routers.interfaces.index(interface)
client.puts(router.throughput(:direction => direction, :interface => interface))
else
@router.interfaces << interface
end
end
else
raise "INVALID FORMAT: [ip_address] [interface] [direction]"
end
rescue Exception => e
client.puts e.to_s
end
end
ensure
client.close
end
end
end
end