From b19220c2b44a175634db9b9fe54793105508a870 Mon Sep 17 00:00:00 2001 From: cn Date: Fri, 15 Feb 2013 22:33:59 +0100 Subject: [PATCH] Fiat lux --- Gemfile | 5 +++ Gemfile.lock | 20 +++++++++++ main.html.erb | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ status.rb | 70 +++++++++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 main.html.erb create mode 100644 status.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2acf7cf --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ + +source 'http://rubygems.org' + +gem 'rack' +gem 'mongrel' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..e4940ed --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,20 @@ +GEM + remote: http://rubygems.org/ + specs: + cgi_multipart_eof_fix (2.5.0) + daemons (1.1.9) + fastthread (1.0.7) + gem_plugin (0.2.3) + mongrel (1.1.5) + cgi_multipart_eof_fix (>= 2.4) + daemons (>= 1.0.3) + fastthread (>= 1.0.1) + gem_plugin (>= 0.2.3) + rack (1.4.1) + +PLATFORMS + ruby + +DEPENDENCIES + mongrel + rack diff --git a/main.html.erb b/main.html.erb new file mode 100644 index 0000000..2f87ca4 --- /dev/null +++ b/main.html.erb @@ -0,0 +1,96 @@ + + + + + OpenVPN Status + + + + +

OpenVPN Status for <%= name %>

+ +

Client List

+ + + + + + + + + +<% client_list.each do |client| %> + + + + + + + +<% end %> + +
Common NameReal AddressData ReceivedData SentConnected Since
<%= client[0] %><%= client[1] %><%= client[2].to_i.as_bytes %><%= client[3].to_i.as_bytes %><%= client[4] %>
+ +

Routing Table

+ + + + + + + + +<% routing_table.each do |e| %> + + + + + + +<% end %> + +
Virtual AddressCommon NameReal AddressLast Ref
<%= e[0] %><%= e[1] %><%= e[2] %><%= e[3] %>
+ +

Global Stats

+ + +<% global_stats.each do |e| %> + + + + +<% end %> + +
<%= e[0] %>:<%= e[1] %>
+ + + diff --git a/status.rb b/status.rb new file mode 100644 index 0000000..9c5e8fb --- /dev/null +++ b/status.rb @@ -0,0 +1,70 @@ + +require 'erb' + +require 'rubygems' +require 'rack' + +class Integer + def as_bytes + return "1 Byte" if self == 1 + + label = ["Bytes", "KiB", "MiB", "GiB", "TiB"] + i = 0 + num = self.to_f + while num >= 1024 do + num = num / 1024 + i += 1 + end + + "#{format('%.2f', num)} #{label[i]}" + end +end + +class OpenVPNStatusWeb + def initialize(name, file) + @name = name + @file = file + end + + def call(env) + main_tmpl = read_template('main.html.erb') + # variables for template + name = @name + client_list, routing_table, global_stats = read_status_log(@file) + + html = main_tmpl.result(binding) + [200, {"Content-Type" => "text/html"}, html] + end + + def read_template(file) + text = File.open(file, 'rb') do |f| f.read end + + ERB.new(text) + end + + def read_status_log(file) + text = File.open(file, 'rb') do |f| f.read end + + 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 then client_list << line.strip.split(',') + when :rt then routing_table << line.strip.split(',') + when :gs then global_stats << line.strip.split(',') + end + end + + [client_list[2..-1], routing_table[1..-1], global_stats] + end +end + +Rack::Handler::Mongrel.run OpenVPNStatusWeb.new('', ''), :Port => 9292