diff --git a/lib/openvpn-status-web.rb b/lib/openvpn-status-web.rb index 9e06ea9..9320115 100644 --- a/lib/openvpn-status-web.rb +++ b/lib/openvpn-status-web.rb @@ -1,5 +1,6 @@ #!/usr/bin/env ruby +require 'date' require 'etc' require 'logger' require 'ipaddr' diff --git a/lib/openvpn-status-web/parser/modern_stateless.rb b/lib/openvpn-status-web/parser/modern_stateless.rb index a6afc7f..ab78dc9 100644 --- a/lib/openvpn-status-web/parser/modern_stateless.rb +++ b/lib/openvpn-status-web/parser/modern_stateless.rb @@ -10,13 +10,32 @@ module OpenVPNStatusWeb text.lines.each do |line| parts = line.strip.split(sep) - status.client_list << parts[1..5] if parts[0] == "CLIENT_LIST" - status.routing_table << parts[1..4] if parts[0] == "ROUTING_TABLE" - status.global_stats << parts[1..2] if parts[0] == "GLOBAL_STATS" + status.client_list << parse_client(parts[1..5]) if parts[0] == "CLIENT_LIST" + status.routing_table << parse_route(parts[1..4]) if parts[0] == "ROUTING_TABLE" + status.global_stats << parse_global(parts[1..2]) if parts[0] == "GLOBAL_STATS" end status end + + private + + def self.parse_client(client) + client[2] = client[2].to_i + client[3] = client[3].to_i + client[4] = DateTime.strptime(client[4], '%a %b %d %k:%M:%S %Y') + client + end + + def self.parse_route(route) + route[3] = DateTime.strptime(route[3], '%a %b %d %k:%M:%S %Y') + route + end + + def self.parse_global(global) + global[1] = global[1].to_i + global + end end end end diff --git a/lib/openvpn-status-web/parser/v1.rb b/lib/openvpn-status-web/parser/v1.rb index 9f45edc..f3fb067 100644 --- a/lib/openvpn-status-web/parser/v1.rb +++ b/lib/openvpn-status-web/parser/v1.rb @@ -15,18 +15,40 @@ module OpenVPNStatusWeb (current_section = :end; next) if line == "END\n" case current_section - when :cl then client_list << line.strip.split(',') - when :rt then routing_table << line.strip.split(',') - when :gs then global_stats << line.strip.split(',') + when :cl + client_list << line.strip.split(',') + when :rt + routing_table << line.strip.split(',') + when :gs + global_stats << line.strip.split(',') end end status = Status.new - status.client_list = client_list[2..-1] - status.routing_table = routing_table[1..-1] - status.global_stats = global_stats + status.client_list = client_list[2..-1].map { |client| parse_client(client) } + status.routing_table = routing_table[1..-1].map { |route| parse_route(route) } + status.global_stats = global_stats.map { |global| parse_global(global) } status end + + private + + def parse_client(client) + client[2] = client[2].to_i + client[3] = client[3].to_i + client[4] = DateTime.strptime(client[4], '%a %b %d %k:%M:%S %Y') + client + end + + def parse_route(route) + route[3] = DateTime.strptime(route[3], '%a %b %d %k:%M:%S %Y') + route + end + + def parse_global(global) + global[1] = global[1].to_i + global + end end end end diff --git a/spec/parser/modern_stateless_spec.rb b/spec/parser/modern_stateless_spec.rb index de76b09..b1499a6 100644 --- a/spec/parser/modern_stateless_spec.rb +++ b/spec/parser/modern_stateless_spec.rb @@ -6,17 +6,49 @@ describe OpenVPNStatusWeb::Parser::ModernStateless do 3 => status_v3 }.each do |version, status| context "for status-version #{version}" do - it 'parses client list' do - status.client_list.map { |client| client[0] }.should be_eql ["foo", "bar"] + context 'for client list' do + it 'parses common names' do + status.client_list.map { |client| client[0] }.should be == ["foo", "bar"] + end + + it 'parses real addresses' do + status.client_list.map { |client| client[1] }.should be == ["1.2.3.4:1234", "1.2.3.5:1235"] + end + + it 'parses received bytes' do + status.client_list.map { |client| client[2] }.should be == [11811160064, 512] + end + + it 'parses sent bytes' do + status.client_list.map { |client| client[3] }.should be == [4194304, 2048] + end + + it 'parses connected since date' do + status.client_list.map { |client| client[4] }.should be == [DateTime.new(2012,1,1,23,42,0), DateTime.new(2012,1,1,23,42,0)] + end end - it 'parses routing table' do - status.routing_table.map { |route| route[1] }.should be_eql ["foo", "bar", "foo", "bar"] + context 'for routing table' do + it 'parses virtual addresses' do + status.routing_table.map { |route| route[0] }.should be == ["192.168.0.0/24", "192.168.66.2", "192.168.66.3", "2001:db8:0:0::1000"] + end + + it 'parses common names' do + status.routing_table.map { |route| route[1] }.should be == ["foo", "bar", "foo", "bar"] + end + + it 'parses real addresses' do + status.routing_table.map { |route| route[2] }.should be == ["1.2.3.4:1234", "1.2.3.5:1235", "1.2.3.4:1234", "1.2.3.5:1235"] + end + + it 'parses last ref date' do + status.routing_table.map { |route| route[3] }.should be == [DateTime.new(2012,1,1,23,42,0), DateTime.new(2012,1,1,23,42,0), DateTime.new(2012,1,1,23,42,0), DateTime.new(2012,1,1,23,42,0)] + end end it 'parses global stats' do - status.global_stats.size.should be_eql 1 - status.global_stats.first.should be_eql ["Max bcast/mcast queue length", "42"] + status.global_stats.size.should be == 1 + status.global_stats.first.should be == ["Max bcast/mcast queue length", 42] end end end diff --git a/spec/parser/v1_spec.rb b/spec/parser/v1_spec.rb index 6ca23a6..eab2c0d 100644 --- a/spec/parser/v1_spec.rb +++ b/spec/parser/v1_spec.rb @@ -3,16 +3,48 @@ require 'spec_helper' describe OpenVPNStatusWeb::Parser::V1 do def status; status_v1; end - it 'parses client list' do - status.client_list.map { |client| client[0] }.should be_eql ["foo", "bar"] + context 'for client list' do + it 'parses common names' do + status.client_list.map { |client| client[0] }.should be == ["foo", "bar"] + end + + it 'parses real addresses' do + status.client_list.map { |client| client[1] }.should be == ["1.2.3.4:1234", "1.2.3.5:1235"] + end + + it 'parses received bytes' do + status.client_list.map { |client| client[2] }.should be == [11811160064, 512] + end + + it 'parses sent bytes' do + status.client_list.map { |client| client[3] }.should be == [4194304, 2048] + end + + it 'parses connected since date' do + status.client_list.map { |client| client[4] }.should be == [DateTime.new(2012,1,1,23,42,0), DateTime.new(2012,1,1,23,42,0)] + end end - it 'parses routing table' do - status.routing_table.map { |route| route[1] }.should be_eql ["foo", "bar", "foo", "bar"] + context 'for routing table' do + it 'parses virtual addresses' do + status.routing_table.map { |route| route[0] }.should be == ["192.168.0.0/24", "192.168.66.2", "192.168.66.3", "2001:db8:0:0::1000"] + end + + it 'parses common names' do + status.routing_table.map { |route| route[1] }.should be == ["foo", "bar", "foo", "bar"] + end + + it 'parses real addresses' do + status.routing_table.map { |route| route[2] }.should be == ["1.2.3.4:1234", "1.2.3.5:1235", "1.2.3.4:1234", "1.2.3.5:1235"] + end + + it 'parses last ref date' do + status.routing_table.map { |route| route[3] }.should be == [DateTime.new(2012,1,1,23,42,0), DateTime.new(2012,1,1,23,42,0), DateTime.new(2012,1,1,23,42,0), DateTime.new(2012,1,1,23,42,0)] + end end it 'parses global stats' do - status.global_stats.size.should be_eql 1 - status.global_stats.first.should be_eql ["Max bcast/mcast queue length", "42"] + status.global_stats.size.should be == 1 + status.global_stats.first.should be == ["Max bcast/mcast queue length", 42] end end