blob: 164db5f735ce4a69f83fcf33c001e957df241439 [file] [log] [blame]
#! /usr/bin/python
# Copyright 2015 Linaro Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Visualisation module for VLANd. Fork a trivial webserver
# implementation on an extra, and generate a simple set of pages and
# graphics on demand.
#
import os, sys, logging, time, datetime, re
from multiprocessing import Process
from BaseHTTPServer import BaseHTTPRequestHandler
from BaseHTTPServer import HTTPServer
import urlparse
if __name__ == '__main__':
vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
sys.path.insert(0, vlandpath)
sys.path.insert(0, "%s/.." % vlandpath)
from errors import InputError
from db.db import VlanDB
from config.config import VlanConfig
from graphics import Graphics,Switch
from util import VlanUtil
class VlandHTTPServer(HTTPServer):
""" Trivial wrapper for HTTPServer so we can include our own state. """
def __init__(self, server_address, handler, state):
HTTPServer.__init__(self, server_address, handler)
self.state = state
class Visualisation(object):
""" Code and config for the visualisation graphics module. """
state = None
p = None
# Fork a new process for the visualisation webserver
def __init__(self, state):
self.state = state
self.p = Process(target=self.visloop, args=())
self.p.start()
# The main loop for the visualisation webserver
def visloop(self):
self.state.db = VlanDB(db_name=self.state.config.database.dbname,
username=self.state.config.database.username)
loglevel = VlanUtil().set_logging_level(self.state.config.logging.level)
# Should we log to stderr?
if self.state.config.logging.filename is None:
logging.basicConfig(level = loglevel,
format = '%(asctime)s %(levelname)-8s %(message)s')
else:
logging.basicConfig(level = loglevel,
format = '%(asctime)s %(levelname)-8s VIS %(message)s',
datefmt = '%Y-%m-%d %H:%M:%S %Z',
filename = self.state.config.logging.filename,
filemode = 'a')
logging.info('%s visualisation starting up', self.state.banner)
server = VlandHTTPServer(('', self.state.config.visualisation.port),
GetHandler, self.state)
server.serve_forever()
# Kill the webserver
def shutdown(self):
self.p.terminate()
class GetHandler(BaseHTTPRequestHandler):
""" Methods to generate and serve the pages """
parsed_path = None
# Trivial top-level page. Link to images for each of the VLANs we
# know about.
def send_index(self):
self.send_response(200)
self.wfile.write('Content-type: text/html\r\n')
self.end_headers()
config = self.server.state.config.visualisation
page = []
page.append('<html>')
page.append('<head>')
page.append('<TITLE>VLANd visualisation</TITLE>')
page.append('<link rel="stylesheet" type="text/css" href="style.css">')
if config.refresh and config.refresh > 0:
page.append('<meta http-equiv="refresh" content="%d">' % config.refresh)
page.append('</HEAD>')
page.append('<body>')
switches = self.server.state.db.all_switches()
vlans = self.server.state.db.all_vlans()
page.append('<div class="menu">')
if len(switches) > 0:
page.append('<h2>Menu</h2>')
page.append('<p>VLANs: %d</p>' % len(vlans))
page.append('<ul>')
for vlan in vlans:
page.append('<li><a href="./#vlan%d">VLAN id %d, tag %d<br>(%s)</a>' % (vlan.vlan_id, vlan.vlan_id, vlan.tag, vlan.name))
page.append('</ul>')
page.append('<div class="date"><p>Current time: %s</p>' % datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC"))
page.append('<p>version %s</p>' % self.server.state.version)
page.append('</div>')
page.append('</div>')
page.append('<div class="content">')
page.append('<h1>VLANd visualisation</h1>')
if len(switches) == 0:
page.append('<p>No switches found in the database, nothing to show...</p>')
else:
for vlan in vlans:
page.append('<a name="vlan%d"></a>' % vlan.vlan_id)
page.append('<h3>VLAN id %d, tag %d, name %s</h3>' % (vlan.vlan_id, vlan.tag, vlan.name))
page.append('<p><a href="/images/vlan/%d.png"><img src="/images/vlan/%d.png" alt="VLAN %d diagram"></a></p>' % (vlan.vlan_id,vlan.vlan_id,vlan.vlan_id))
page.append('<hr>')
page.append('</div>')
page.append('</body>')
self.wfile.write('\r\n'.join(page))
# Trivial style sheet, TODO!
def send_style(self):
self.send_response(200)
self.wfile.write('Content-type: text/css\r\n')
self.end_headers()
page = []
page.append("body {")
page.append(" background: white;")
page.append(" color: black;")
page.append(" font-size: 12pt;")
page.append("}")
page.append("")
page.append(".menu {")
page.append(" position:fixed;")
page.append(" float:left;")
page.append(" font-family: arial, Helvetica, sans-serif;")
page.append(" width:20%;")
page.append(" height:100%;")
page.append(" font-size: 10pt;")
page.append(" padding-top: 10px;")
page.append("}")
page.append("")
page.append(".content {")
page.append(" padding-top: 10px;")
page.append(" width:80%;")
page.append(" max-width:80%;")
page.append(" margin-left: 21%;")
page.append(" margin-top: 50px;")
page.append(" height:100%;")
page.append("}")
page.append("")
page.append(".footer {")
page.append(" vertical-align: bottom;")
page.append(" text-align: left;")
page.append("}")
page.append("")
page.append(".caption {")
page.append(" padding-top: 1px;")
page.append(" padding-left: 10%;")
page.append(" padding-right: 10%;")
page.append(" font-size: 8pt;")
page.append(" font-style: italic;")
page.append(" text-align: center;")
page.append("}")
page.append("")
page.append("td.headline {")
page.append(" font-family: arial, Helvetica, sans-serif;")
page.append(" font-size: 20pt;")
page.append("}")
page.append("h1,h2,h3,h4,h5 {")
page.append(" font-family: arial, Helvetica, sans-serif;")
page.append(" padding-right:3pt;")
page.append(" padding-top:2pt;")
page.append(" padding-bottom:2pt;")
page.append(" margin-top:8pt;")
page.append(" margin-bottom:8pt;")
page.append(" border-style:none;")
page.append(" border-width:thin;")
page.append("}")
page.append("")
page.append("A:link { text-decoration: none; }")
page.append("A:visited { text-decoration: none}")
page.append("")
page.append("h1 { font-size: 18pt; }")
page.append("h2 { font-size: 14pt; }")
page.append("h3 { font-size: 12pt; }")
page.append("h4 { font-size: 10pt; }")
page.append("h5 { font-size: 8pt; }")
page.append("dl,ul { margin-top: 1pt; text-indent: 0 }")
page.append("ol { margin-top: 1pt; text-indent: 0 }")
page.append("")
page.append("tt,pre {")
page.append(" font-family: Lucida Console,Courier New,Courier,monotype;")
page.append(" font-size: 10pt;")
page.append("}")
page.append("")
page.append("pre.code {")
page.append(" font-family: Lucida Console,Courier New,Courier,monotype;")
page.append(" margin-top: 8pt;")
page.append(" margin-bottom: 8pt;")
page.append(" background-color: #FFFFEE;")
page.append(" white-space:pre;")
page.append(" border-style:solid;")
page.append(" border-width:1pt;")
page.append(" border-color:#999999;")
page.append(" color:#111111;")
page.append(" padding:5px;")
page.append("}")
page.append("")
page.append("div.date {")
page.append(" font-size: 8pt;")
page.append("}")
page.append("")
page.append("div.sig {")
page.append(" font-size: 8pt;")
page.append("}")
page.append("")
self.wfile.write('\r\n'.join(page))
# Generate a PNG showing the layout of switches/port/trunks for a
# specific VLAN
def send_graphic(self):
vlan_id = 0
vlan_re = re.compile(r'^/images/vlan/(\d+).png$')
match = vlan_re.match(self.parsed_path.path)
if match:
vlan_id = match.group(1)
db = self.server.state.db
vlan = db.get_vlan_by_id(vlan_id)
# We've been asked for a VLAN that doesn't exist
if vlan is None:
self.send_response(404)
self.wfile.write('Content-type: text/plain\r\n')
self.end_headers()
self.wfile.write('404 Not Found\r\n')
logging.error('VLAN graphic not found - asked for %s', self.parsed_path.path)
return
gim = Graphics()
# Pick fonts. TODO: Make these configurable?
gim.set_font(['/usr/share/fonts/truetype/inconsolata/Inconsolata.otf',
'/usr/share/fonts/truetype/freefont/FreeMono.ttf'])
try:
gim.font
# If we can't get the font we need, fail
except NameError:
self.send_response(500)
self.wfile.write('Content-type: text/plain\r\n')
self.end_headers()
self.wfile.write('500 Internal Server Error\r\n')
logging.error('Unable to generate graphic, no fonts found - asked for %s',
self.parsed_path.path)
return
switch = {}
size_x = {}
size_y = {}
switches = db.all_switches()
# Need to set gaps big enough for the number of trunks, at least.
trunks = db.all_trunks()
y_gap = max(20, 15 * len(trunks))
x_gap = max(20, 15 * len(trunks))
x = 0
y = y_gap
# Work out how much space we need for the switches
for i in range(0, len(switches)):
ports = db.get_ports_by_switch(switches[i].switch_id)
switch[i] = Switch(gim, len(ports), switches[i].name)
(size_x[i], size_y[i]) = switch[i].get_dimensions()
x = max(x, size_x[i])
y += size_y[i] + y_gap
# Add space for the legend and the label
label = "VLAN %d - %s" % (vlan.tag, vlan.name)
(legend_width, legend_height) = gim.get_legend_dimensions()
(label_width, label_height) = gim.get_label_size(label, gim.label_font_size)
x = max(x, legend_width + 2*x_gap + label_width)
x = x_gap + x + x_gap
y = y + max(legend_height + y_gap, label_height)
# Create a canvas of the right size
gim.create_canvas(x, y)
# Draw the switches and ports in it
curr_y = y_gap
for i in range(0, len(switches)):
switch[i].draw_switch(gim, x_gap, curr_y)
ports = db.get_ports_by_switch(switches[i].switch_id)
for port_id in ports:
port = db.get_port_by_id(port_id)
if port.is_locked:
switch[i].draw_port(gim, port.number, 'locked')
elif port.is_trunk:
switch[i].draw_port(gim, port.number, 'trunk')
elif port.current_vlan_id == int(vlan_id):
switch[i].draw_port(gim, port.number, 'VLAN')
else:
switch[i].draw_port(gim, port.number, 'normal')
curr_y += size_y[i] + y_gap
# Now add the trunks
for i in range(0, len(trunks)):
ports = db.get_ports_by_trunk(trunks[i].trunk_id)
port1 = db.get_port_by_id(ports[0])
port2 = db.get_port_by_id(ports[1])
for s in range(0, len(switches)):
if switches[s].switch_id == port1.switch_id:
switch1 = s
if switches[s].switch_id == port2.switch_id:
switch2 = s
gim.draw_trunk(i,
switch[switch1].get_port_location(port1.number),
switch[switch2].get_port_location(port2.number),
gim.port_pallette['trunk']['trace'])
# And the legend and label
gim.draw_legend(x_gap, curr_y)
gim.draw_label(x - label_width - 2*x_gap, curr_y, label, int(x_gap / 2))
# All done - send it down the http socket
self.send_response(200)
self.wfile.write('Content-type: image/png\r\n')
self.end_headers()
gim.im.writePng(self.wfile)
# Implement an HTTP GET handler for the HTTPServer instance
def do_GET(self):
# Compare the URL path to any of the names we recognise and
# call the right generator function if we get a match
self.parsed_path = urlparse.urlparse(self.path)
for url in self.functionMap:
match = re.match(url['re'], self.parsed_path.path)
if match:
return url['fn'](self)
# Fall-through for any files we don't recognise
self.send_response(404)
self.wfile.write('Content-type: text/plain\r\n')
self.end_headers()
self.wfile.write('404 Not Found')
logging.error('File not supported - asked for %s', self.parsed_path.path)
return
# Override the BaseHTTPRequestHandler log_message() method so we
# can log requests properly
def log_message(self, fmt, *args):
"""Log an arbitrary message. """
logging.info('%s %s', self.client_address[0], fmt%args)
functionMap = (
{'re': r'^/$', 'fn': send_index},
{'re': r'^/style.css$', 'fn': send_style},
{'re': r'^/images/vlan/(\d+).png$', 'fn': send_graphic}
)