openvpn-status-web/lib/openvpn-status-web/parser/v1.rb

58 lines
1.7 KiB
Ruby
Raw Normal View History

2020-03-07 01:30:57 +01:00
# frozen_string_literal: true
2013-05-03 18:20:07 +02:00
module OpenVPNStatusWeb
module Parser
class V1
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"
2020-03-02 01:57:58 +01:00
2013-05-03 18:20:07 +02:00
case current_section
2013-05-03 22:23:45 +02:00
when :cl
client_list << line.strip.split(',')
when :rt
routing_table << line.strip.split(',')
when :gs
global_stats << line.strip.split(',')
2013-05-03 18:20:07 +02:00
end
end
2013-05-03 18:30:34 +02:00
status = Status.new
status.client_list_headers = ['Common Name', 'Real Address', 'Data Received', 'Data Sent', 'Connected Since']
2022-05-08 16:30:19 +02:00
status.client_list = client_list[2..].map { |client| parse_client(client) }
status.routing_table_headers = ['Virtual Address', 'Common Name', 'Real Address', 'Last Ref']
2022-05-08 16:30:19 +02:00
status.routing_table = routing_table[1..].map { |route| parse_route(route) }
2013-05-03 22:23:45 +02:00
status.global_stats = global_stats.map { |global| parse_global(global) }
2013-05-03 18:30:34 +02:00
status
2013-05-03 18:20:07 +02:00
end
2013-05-03 22:23:45 +02:00
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
2013-05-03 18:20:07 +02:00
end
end
end