From 33013c56f3483b372ee7f6880beab99fe8698ebc Mon Sep 17 00:00:00 2001 From: cn Date: Fri, 3 May 2013 20:09:46 +0200 Subject: [PATCH] Add support for multiple status-versions --- lib/openvpn-status-web.rb | 17 ++++++++++++----- lib/openvpn-status-web/parser/v2.rb | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 lib/openvpn-status-web/parser/v2.rb diff --git a/lib/openvpn-status-web.rb b/lib/openvpn-status-web.rb index cfb9b1b..2803723 100644 --- a/lib/openvpn-status-web.rb +++ b/lib/openvpn-status-web.rb @@ -11,6 +11,7 @@ require 'better_errors' require 'openvpn-status-web/status' require 'openvpn-status-web/parser/v1' +require 'openvpn-status-web/parser/v2' require 'openvpn-status-web/int_patch' require 'openvpn-status-web/version' @@ -42,10 +43,9 @@ module OpenVPNStatusWeb # variables for template name = @vpns.keys.first - status = read_status_log(@vpns[name]['status_file']) + status = parse_status_log(@vpns[name]) # eval html = @main_tmpl.result(binding) - #html = "" [200, {"Content-Type" => "text/html"}, [html]] end @@ -56,10 +56,17 @@ module OpenVPNStatusWeb ERB.new(text) end - def read_status_log(file) - text = File.open(file, 'rb') do |f| f.read end + def parse_status_log(vpn) + text = File.open(vpn['status_file'], 'rb') do |f| f.read end - OpenVPNStatusWeb::Parser::V1.new.parse_status_log(text) + case vpn['version'] + when 1 + OpenVPNStatusWeb::Parser::V1.new.parse_status_log(text) + when 2 + OpenVPNStatusWeb::Parser::V2.new.parse_status_log(text) + else + raise "No suitable parser for status-version #{vpn['version']}" + end end def self.run! diff --git a/lib/openvpn-status-web/parser/v2.rb b/lib/openvpn-status-web/parser/v2.rb new file mode 100644 index 0000000..03bc1b5 --- /dev/null +++ b/lib/openvpn-status-web/parser/v2.rb @@ -0,0 +1,25 @@ + +module OpenVPNStatusWeb + module Parser + class V2 + def parse_status_log(text) + client_list = [] + routing_table = [] + global_stats = [] + + text.lines.each do |line| + parts = line.strip.split(',') + client_list << parts[1..5] if parts[0] == "CLIENT_LIST" + routing_table << parts[1..4] if parts[0] == "ROUTING_TABLE" + global_stats << parts[1..2] if parts[0] == "GLOBAL_STATS" + end + + status = Status.new + status.client_list = client_list + status.routing_table = routing_table + status.global_stats = global_stats + status + end + end + end +end