1
0
mirror of https://github.com/cmur2/openvpn-status-web.git synced 2024-11-05 04:56:17 +01:00
This commit is contained in:
cn 2013-02-15 22:33:59 +01:00
commit b19220c2b4
4 changed files with 191 additions and 0 deletions

5
Gemfile Normal file
View File

@ -0,0 +1,5 @@
source 'http://rubygems.org'
gem 'rack'
gem 'mongrel'

20
Gemfile.lock Normal file
View File

@ -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

96
main.html.erb Normal file
View File

@ -0,0 +1,96 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>OpenVPN Status</title>
<style type="text/css">
table {
border-collapse: collapse;
}
h1 {
font-size: 24px;
}
h2 {
font-size: 16px;
font-weight: normal;
}
td {
margin: 0px;
}
td.first {
padding: 0px 6px 0px 0px;
}
td.middle {
padding: 0px 6px 0px 6px;
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
}
td.last {
padding: 0px 0px 0px 6px;
}
thead {
font-weight: bold;
border-bottom: 1px solid #ccc;
}
</style>
</head>
<body>
<h1>OpenVPN Status for <%= name %></h1>
<h2>Client List</h2>
<table>
<thead>
<td class="first">Common Name</td>
<td class="middle">Real Address</td>
<td class="middle">Data Received</td>
<td class="middle">Data Sent</td>
<td class="last">Connected Since</td>
</thead>
<tbody>
<% client_list.each do |client| %>
<tr>
<td class="first"><%= client[0] %></td>
<td class="middle"><%= client[1] %></td>
<td class="middle"><%= client[2].to_i.as_bytes %></td>
<td class="middle"><%= client[3].to_i.as_bytes %></td>
<td class="last"><%= client[4] %></td>
</tr>
<% end %>
</tbody>
</table>
<h2>Routing Table</h2>
<table>
<thead>
<td class="first">Virtual Address</td>
<td class="middle">Common Name</td>
<td class="middle">Real Address</td>
<td class="last">Last Ref</td>
</thead>
<tbody>
<% routing_table.each do |e| %>
<tr>
<td class="first"><%= e[0] %></td>
<td class="middle"><%= e[1] %></td>
<td class="middle"><%= e[2] %></td>
<td class="last"><%= e[3] %></td>
</tr>
<% end %>
</tbody>
</table>
<h2>Global Stats</h2>
<table>
<tbody>
<% global_stats.each do |e| %>
<tr>
<td><%= e[0] %>:</td>
<td><%= e[1] %></td>
</tr>
<% end %>
</tbody>
</table>
</body>
</html>

70
status.rb Normal file
View File

@ -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