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

56 lines
1.8 KiB
Ruby
Raw Normal View History

2020-03-07 01:30:57 +01:00
# frozen_string_literal: true
2013-05-03 20:16:17 +02:00
module OpenVPNStatusWeb
module Parser
class ModernStateless
def self.parse_status_log(text, sep)
status = Status.new
status.client_list_headers = []
2013-05-03 20:16:17 +02:00
status.client_list = []
status.routing_table_headers = []
2013-05-03 20:16:17 +02:00
status.routing_table = []
status.global_stats = []
text.lines.each do |line|
parts = line.strip.split(sep)
2022-05-08 16:30:19 +02:00
status.client_list_headers = parts[2..] if parts[0] == 'HEADER' && parts[1] == 'CLIENT_LIST'
status.client_list << parse_client(parts[1..], status.client_list_headers) if parts[0] == 'CLIENT_LIST'
status.routing_table_headers = parts[2..] if parts[0] == 'HEADER' && parts[1] == 'ROUTING_TABLE'
status.routing_table << parse_route(parts[1..], status.routing_table_headers) if parts[0] == 'ROUTING_TABLE'
2020-03-02 01:57:58 +01:00
status.global_stats << parse_global(parts[1..2]) if parts[0] == 'GLOBAL_STATS'
2013-05-03 20:16:17 +02:00
end
status
end
2013-05-03 22:23:45 +02:00
private_class_method def self.parse_client(client, headers)
headers.each_with_index do |header, i|
client[i] = parse_date(client[i]) if header.end_with?('Since')
client[i] = client[i].to_i if header.start_with?('Bytes')
end
2013-05-03 22:23:45 +02:00
client
end
private_class_method def self.parse_route(route, headers)
headers.each_with_index do |header, i|
route[i] = parse_date(route[i]) if header.end_with?('Last Ref')
end
2013-05-03 22:23:45 +02:00
route
end
2020-03-02 01:57:58 +01:00
private_class_method def self.parse_global(global)
2013-05-03 22:23:45 +02:00
global[1] = global[1].to_i
global
end
private_class_method def self.parse_date(date_string)
DateTime.strptime(date_string, '%a %b %d %k:%M:%S %Y')
rescue ArgumentError
DateTime.strptime(date_string, '%Y-%m-%d %k:%M:%S')
end
2013-05-03 20:16:17 +02:00
end
end
end