Class: OpenVPNStatusWeb::Parser::V1

Inherits:
Object
  • Object
show all
Defined in:
lib/openvpn-status-web/parser/v1.rb

Instance Method Summary collapse

Instance Method Details

#parse_status_log(text) ⇒ Object



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
# File 'lib/openvpn-status-web/parser/v1.rb', line 6

def parse_status_log(text)
  current_section = :none
  client_list = []
  routing_table = []
  global_stats = []

  text.lines.each do |line|
    (current_section = :cl; next) if line == "OpenVPN CLIENT LIST\n"
    (current_section = :rt; next) if line == "ROUTING TABLE\n"
    (current_section = :gs; next) if line == "GLOBAL STATS\n"
    (current_section = :end; next) if line == "END\n"

    case current_section
    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_headers = ['Common Name', 'Real Address', 'Data Received', 'Data Sent', 'Connected Since']
  status.client_list = client_list[2..].map { |client| parse_client(client) }
  status.routing_table_headers = ['Virtual Address', 'Common Name', 'Real Address', 'Last Ref']
  status.routing_table = routing_table[1..].map { |route| parse_route(route) }
  status.global_stats = global_stats.map { |global| parse_global(global) }
  status
end