Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2015 Linaro Limited |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation; either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program; if not, write to the Free Software |
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | # MA 02110-1301, USA. |
| 19 | # |
| 20 | # Visualisation module for VLANd. Fork a trivial webserver |
| 21 | # implementation on an extra, and generate a simple set of pages and |
| 22 | # graphics on demand. |
| 23 | # |
| 24 | |
Steve McIntyre | bc06c93 | 2018-01-31 17:04:29 +0000 | [diff] [blame] | 25 | import os, sys, logging, time, datetime, re, signal |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 26 | from multiprocessing import Process |
| 27 | from BaseHTTPServer import BaseHTTPRequestHandler |
| 28 | from BaseHTTPServer import HTTPServer |
| 29 | import urlparse |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 30 | import cStringIO |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 31 | |
| 32 | if __name__ == '__main__': |
| 33 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 34 | sys.path.insert(0, vlandpath) |
| 35 | sys.path.insert(0, "%s/.." % vlandpath) |
| 36 | |
| 37 | from errors import InputError |
| 38 | from db.db import VlanDB |
| 39 | from config.config import VlanConfig |
| 40 | from graphics import Graphics,Switch |
| 41 | from util import VlanUtil |
| 42 | class VlandHTTPServer(HTTPServer): |
| 43 | """ Trivial wrapper for HTTPServer so we can include our own state. """ |
| 44 | def __init__(self, server_address, handler, state): |
| 45 | HTTPServer.__init__(self, server_address, handler) |
| 46 | self.state = state |
| 47 | |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 48 | class GraphicsCache(object): |
| 49 | """ Cache for graphics state, to avoid having to recalculate every |
| 50 | query too many times. """ |
| 51 | last_update = None |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 52 | graphics = {} |
| 53 | |
| 54 | def __init__(self): |
| 55 | # Pick an epoch older than any sensible use |
| 56 | self.last_update = datetime.datetime(2000, 01, 01) |
| 57 | |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 58 | class Visualisation(object): |
| 59 | """ Code and config for the visualisation graphics module. """ |
| 60 | |
| 61 | state = None |
| 62 | p = None |
| 63 | |
| 64 | # Fork a new process for the visualisation webserver |
| 65 | def __init__(self, state): |
| 66 | self.state = state |
| 67 | self.p = Process(target=self.visloop, args=()) |
| 68 | self.p.start() |
| 69 | |
Steve McIntyre | bc06c93 | 2018-01-31 17:04:29 +0000 | [diff] [blame] | 70 | def _receive_signal(self, signum, stack): |
| 71 | if signum == signal.SIGUSR1: |
| 72 | self.state.db_ok = True |
| 73 | |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 74 | # The main loop for the visualisation webserver |
| 75 | def visloop(self): |
Steve McIntyre | bc06c93 | 2018-01-31 17:04:29 +0000 | [diff] [blame] | 76 | self.state.db_ok = False |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 77 | self.state.cache = GraphicsCache() |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 78 | |
| 79 | loglevel = VlanUtil().set_logging_level(self.state.config.logging.level) |
| 80 | |
| 81 | # Should we log to stderr? |
| 82 | if self.state.config.logging.filename is None: |
| 83 | logging.basicConfig(level = loglevel, |
| 84 | format = '%(asctime)s %(levelname)-8s %(message)s') |
| 85 | else: |
| 86 | logging.basicConfig(level = loglevel, |
| 87 | format = '%(asctime)s %(levelname)-8s VIS %(message)s', |
| 88 | datefmt = '%Y-%m-%d %H:%M:%S %Z', |
| 89 | filename = self.state.config.logging.filename, |
| 90 | filemode = 'a') |
| 91 | logging.info('%s visualisation starting up', self.state.banner) |
| 92 | |
Steve McIntyre | bc06c93 | 2018-01-31 17:04:29 +0000 | [diff] [blame] | 93 | # Wait for main process to signal to us that it's finished with any |
| 94 | # database upgrades and we can open it without any problems. |
| 95 | signal.signal(signal.SIGUSR1, self._receive_signal) |
| 96 | while not self.state.db_ok: |
| 97 | logging.info('%s visualisation waiting for db_ok signal', self.state.banner) |
| 98 | time.sleep(1) |
| 99 | logging.info('%s visualisation received db_ok signal', self.state.banner) |
| 100 | |
| 101 | self.state.db = VlanDB(db_name=self.state.config.database.dbname, |
| 102 | username=self.state.config.database.username, |
| 103 | readonly=True) |
| 104 | |
Steve McIntyre | 86916e4 | 2015-09-28 02:39:32 +0100 | [diff] [blame] | 105 | server = VlandHTTPServer(('', self.state.config.visualisation.port), |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 106 | GetHandler, self.state) |
| 107 | server.serve_forever() |
| 108 | |
| 109 | # Kill the webserver |
| 110 | def shutdown(self): |
| 111 | self.p.terminate() |
| 112 | |
Steve McIntyre | bc06c93 | 2018-01-31 17:04:29 +0000 | [diff] [blame] | 113 | # Kill the webserver |
| 114 | def signal_db_ok(self): |
| 115 | os.kill(self.p.pid, signal.SIGUSR1) |
| 116 | |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 117 | class GetHandler(BaseHTTPRequestHandler): |
| 118 | """ Methods to generate and serve the pages """ |
| 119 | |
| 120 | parsed_path = None |
| 121 | |
| 122 | # Trivial top-level page. Link to images for each of the VLANs we |
| 123 | # know about. |
| 124 | def send_index(self): |
| 125 | self.send_response(200) |
| 126 | self.wfile.write('Content-type: text/html\r\n') |
| 127 | self.end_headers() |
Steve McIntyre | b0aa460 | 2015-10-08 15:33:28 +0100 | [diff] [blame] | 128 | config = self.server.state.config.visualisation |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 129 | cache = self.server.state.cache |
| 130 | db = self.server.state.db |
| 131 | switches = db.all_switches() |
| 132 | vlans = db.all_vlans() |
| 133 | vlan_tags = {} |
| 134 | |
| 135 | for vlan in vlans: |
| 136 | vlan_tags[vlan.vlan_id] = vlan.tag |
| 137 | |
| 138 | if cache.last_update < self.server.state.db.get_last_modified_time(): |
| 139 | logging.debug('Cache is out of date') |
| 140 | # Fill the cache with all the information we need: |
| 141 | # * the graphics themselves |
| 142 | # * the data to match each graphic, so we can generate imagemap/tooltips |
| 143 | cache.graphics = {} |
| 144 | if len(switches) > 0: |
| 145 | for vlan in vlans: |
| 146 | cache.graphics[vlan.vlan_id] = self.generate_graphic(vlan.vlan_id) |
| 147 | cache.last_update = datetime.datetime.utcnow() |
| 148 | |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 149 | page = [] |
Steve McIntyre | de1ee97 | 2015-10-28 18:23:05 +0000 | [diff] [blame] | 150 | page.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">') |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 151 | page.append('<html>') |
| 152 | page.append('<head>') |
Steve McIntyre | de1ee97 | 2015-10-28 18:23:05 +0000 | [diff] [blame] | 153 | page.append('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">') |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 154 | page.append('<TITLE>VLANd visualisation</TITLE>') |
Steve McIntyre | b74ea6b | 2015-09-24 20:45:31 +0100 | [diff] [blame] | 155 | page.append('<link rel="stylesheet" type="text/css" href="style.css">') |
Steve McIntyre | b0aa460 | 2015-10-08 15:33:28 +0100 | [diff] [blame] | 156 | if config.refresh and config.refresh > 0: |
| 157 | page.append('<meta http-equiv="refresh" content="%d">' % config.refresh) |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 158 | page.append('</HEAD>') |
| 159 | page.append('<body>') |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 160 | |
| 161 | # Generate left-hand menu with links to each VLAN diagram |
Steve McIntyre | b74ea6b | 2015-09-24 20:45:31 +0100 | [diff] [blame] | 162 | page.append('<div class="menu">') |
| 163 | if len(switches) > 0: |
| 164 | page.append('<h2>Menu</h2>') |
| 165 | page.append('<p>VLANs: %d</p>' % len(vlans)) |
| 166 | page.append('<ul>') |
| 167 | for vlan in vlans: |
Steve McIntyre | def2686 | 2015-10-29 17:35:10 +0000 | [diff] [blame] | 168 | 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)) |
Steve McIntyre | b74ea6b | 2015-09-24 20:45:31 +0100 | [diff] [blame] | 169 | page.append('</ul>') |
| 170 | page.append('<div class="date"><p>Current time: %s</p>' % datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")) |
| 171 | page.append('<p>version %s</p>' % self.server.state.version) |
| 172 | page.append('</div>') |
| 173 | page.append('</div>') |
| 174 | |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 175 | # Now the main content area with the graphics |
Steve McIntyre | b74ea6b | 2015-09-24 20:45:31 +0100 | [diff] [blame] | 176 | page.append('<div class="content">') |
| 177 | page.append('<h1>VLANd visualisation</h1>') |
| 178 | |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 179 | # Bail early if we have nothing to show! |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 180 | if len(switches) == 0: |
| 181 | page.append('<p>No switches found in the database, nothing to show...</p>') |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 182 | page.append('</div>') |
| 183 | page.append('</body>') |
| 184 | self.wfile.write('\r\n'.join(page)) |
| 185 | return |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 186 | |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 187 | # Trivial javascript helpers for tooltip control |
| 188 | page.append('<SCRIPT LANGUAGE="javascript">') |
| 189 | page.append('function popup(v,p) {') |
| 190 | page.append('a=v.toString();') |
| 191 | page.append('b=p.toString();') |
| 192 | page.append('id="port".concat("",a).concat(".",b);') |
| 193 | page.append('document.getElementById(id).style.visibility="visible";') |
| 194 | page.append('}') |
| 195 | page.append('function popdown(v,p) {') |
| 196 | page.append('a=v.toString();') |
| 197 | page.append('b=p.toString();') |
| 198 | page.append('id="port".concat("",a).concat(".",b);') |
| 199 | page.append('document.getElementById(id).style.visibility="hidden";') |
| 200 | page.append('}') |
| 201 | page.append('</SCRIPT>') |
| 202 | |
| 203 | # For each VLAN, add a graphic |
| 204 | for vlan in vlans: |
| 205 | this_image = cache.graphics[vlan.vlan_id] |
| 206 | page.append('<a name="vlan%d"></a>' % vlan.vlan_id) |
| 207 | page.append('<h3>VLAN ID %d, Tag %d, name %s</h3>' % (vlan.vlan_id, vlan.tag, vlan.name)) |
| 208 | |
| 209 | # Link to an image we generate from our data |
| 210 | page.append('<p><img src="images/vlan/%d.png" ' % vlan.vlan_id) |
| 211 | page.append('width="%d" height="%d" ' % ( this_image['image']['width'], this_image['image']['height'])) |
| 212 | page.append('alt="VLAN %d diagram" usemap="#MAPVLAN%d">' % (vlan.vlan_id,vlan.vlan_id)) |
| 213 | |
| 214 | # Generate an imagemap describing all the ports, with |
| 215 | # javascript hooks to pop up/down a tooltip box based on |
| 216 | # later data. |
| 217 | page.append('<map name="MAPVLAN%d">' % vlan.vlan_id) |
| 218 | for switch in this_image['ports'].keys(): |
| 219 | for portnum in this_image['ports'][switch].keys(): |
| 220 | this_port = this_image['ports'][switch][portnum] |
| 221 | port = this_port['db'] |
| 222 | ((ulx,uly),(lrx,lry),upper) = this_port['location'] |
| 223 | page.append('<area shape="rect" ') |
| 224 | page.append('coords="%d,%d,%d,%d" ' % (ulx,uly,lrx,lry)) |
| 225 | page.append('onMouseOver="popup(%d,%d)" onMouseOut="popdown(%d,%d)">' % (vlan.vlan_id, port.port_id, vlan.vlan_id, port.port_id)) |
| 226 | page.append('</map></p>') |
| 227 | page.append('<hr>') |
| 228 | page.append('</div>') # End of normal content, all the VLAN graphics shown |
| 229 | |
| 230 | # Now generate the tooltip boxes for the ports. Each is |
| 231 | # fully-formed but invisible, ready for our javascript helper |
| 232 | # to pop visible on demand. |
| 233 | for vlan in vlans: |
| 234 | this_image = cache.graphics[vlan.vlan_id] |
| 235 | for switch in this_image['ports'].keys(): |
| 236 | for portnum in this_image['ports'][switch].keys(): |
| 237 | this_port = this_image['ports'][switch][portnum] |
| 238 | port = this_port['db'] |
| 239 | page.append('<div class="port" id="port%d.%d">' % (vlan.vlan_id, port.port_id)) |
| 240 | page.append('Port ID: %d Port number: %d<br>' % (port.port_id, port.number)) |
| 241 | if port.is_locked: |
| 242 | page.append('Locked<br>') |
| 243 | if port.is_trunk: |
| 244 | page.append('Trunk') |
| 245 | if port.trunk_id != -1: |
| 246 | page.append(' (Trunk ID %d)' % port.trunk_id) |
| 247 | page.append('<br>') |
| 248 | else: |
| 249 | page.append('Current VLAN ID: %d (Tag %d)<br>' % (port.current_vlan_id, vlan_tags[port.current_vlan_id])) |
| 250 | page.append('Base VLAN ID: %d (Tag %d)<br>' % (port.base_vlan_id, vlan_tags[port.base_vlan_id])) |
| 251 | page.append('</div>') |
| 252 | |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 253 | page.append('</body>') |
| 254 | self.wfile.write('\r\n'.join(page)) |
| 255 | |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 256 | # Simple-ish style sheet |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 257 | def send_style(self): |
| 258 | self.send_response(200) |
| 259 | self.wfile.write('Content-type: text/css\r\n') |
| 260 | self.end_headers() |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 261 | cache = self.server.state.cache |
Steve McIntyre | b74ea6b | 2015-09-24 20:45:31 +0100 | [diff] [blame] | 262 | page = [] |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 263 | page.append('body {') |
| 264 | page.append(' background: white;') |
| 265 | page.append(' color: black;') |
| 266 | page.append(' font-size: 12pt;') |
| 267 | page.append('}') |
| 268 | page.append('') |
| 269 | page.append('.menu {') |
| 270 | page.append(' position:fixed;') |
| 271 | page.append(' float:left;') |
| 272 | page.append(' font-family: arial, Helvetica, sans-serif;') |
| 273 | page.append(' width:20%;') |
| 274 | page.append(' height:100%;') |
| 275 | page.append(' font-size: 10pt;') |
| 276 | page.append(' padding-top: 10px;') |
| 277 | page.append('}') |
| 278 | page.append('') |
| 279 | page.append('.content {') |
| 280 | page.append(' position:relative;') |
| 281 | page.append(' padding-top: 10px;') |
Steve McIntyre | e8f39df | 2015-10-28 18:36:39 +0000 | [diff] [blame] | 282 | page.append(' width: 80%;') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 283 | page.append(' max-width:80%;') |
| 284 | page.append(' margin-left: 21%;') |
| 285 | page.append(' margin-top: 50px;') |
| 286 | page.append(' height:100%;') |
| 287 | page.append('}') |
| 288 | page.append('') |
Steve McIntyre | 8179f61 | 2015-10-30 18:24:20 +0000 | [diff] [blame] | 289 | page.append('h1,h2,h3 {') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 290 | page.append(' font-family: arial, Helvetica, sans-serif;') |
| 291 | page.append(' padding-right:3pt;') |
| 292 | page.append(' padding-top:2pt;') |
| 293 | page.append(' padding-bottom:2pt;') |
| 294 | page.append(' margin-top:8pt;') |
| 295 | page.append(' margin-bottom:8pt;') |
| 296 | page.append(' border-style:none;') |
| 297 | page.append(' border-width:thin;') |
| 298 | page.append('}') |
| 299 | page.append('A:link { text-decoration: none; }') |
| 300 | page.append('A:visited { text-decoration: none}') |
| 301 | page.append('h1 { font-size: 18pt; }') |
| 302 | page.append('h2 { font-size: 14pt; }') |
| 303 | page.append('h3 { font-size: 12pt; }') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 304 | page.append('dl,ul { margin-top: 1pt; text-indent: 0 }') |
| 305 | page.append('ol { margin-top: 1pt; text-indent: 0 }') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 306 | page.append('div.date { font-size: 8pt; }') |
| 307 | page.append('div.sig { font-size: 8pt; }') |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 308 | page.append('div.port {') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 309 | page.append(' display: block;') |
| 310 | page.append(' position: fixed;') |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 311 | page.append(' left: 0px;') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 312 | page.append(' bottom: 0px;') |
| 313 | page.append(' z-index: 99;') |
| 314 | page.append(' background: #FFFF00;') |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 315 | page.append(' border-style: solid;') |
| 316 | page.append(' border-width: 3pt;') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 317 | page.append(' border-color: #3B3B3B;') |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 318 | page.append(' margin: 1;') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 319 | page.append(' padding: 5px;') |
| 320 | page.append(' font-size: 10pt;') |
| 321 | page.append(' font-family: Courier,monotype;') |
Steve McIntyre | bfef506 | 2015-10-30 18:28:32 +0000 | [diff] [blame] | 322 | page.append(' visibility: hidden;') |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 323 | page.append('}') |
Steve McIntyre | b74ea6b | 2015-09-24 20:45:31 +0100 | [diff] [blame] | 324 | self.wfile.write('\r\n'.join(page)) |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 325 | |
Steve McIntyre | e8f39df | 2015-10-28 18:36:39 +0000 | [diff] [blame] | 326 | # Generate a PNG showing the layout of switches/port/trunks for a |
| 327 | # specific VLAN |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 328 | def send_graphic(self): |
| 329 | vlan_id = 0 |
| 330 | vlan_re = re.compile(r'^/images/vlan/(\d+).png$') |
| 331 | match = vlan_re.match(self.parsed_path.path) |
| 332 | if match: |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 333 | vlan_id = int(match.group(1)) |
| 334 | cache = self.server.state.cache |
| 335 | |
Steve McIntyre | def2686 | 2015-10-29 17:35:10 +0000 | [diff] [blame] | 336 | # Do we have a graphic for this VLAN ID? |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 337 | if not vlan_id in cache.graphics.keys(): |
| 338 | logging.debug('asked for vlan_id %s', vlan_id) |
| 339 | logging.debug(cache.graphics.keys()) |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 340 | self.send_response(404) |
Steve McIntyre | 4f584a7 | 2015-09-28 02:28:56 +0100 | [diff] [blame] | 341 | self.wfile.write('Content-type: text/plain\r\n') |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 342 | self.end_headers() |
Steve McIntyre | 4f584a7 | 2015-09-28 02:28:56 +0100 | [diff] [blame] | 343 | self.wfile.write('404 Not Found\r\n') |
Steve McIntyre | 0f561cd | 2015-10-28 18:05:20 +0000 | [diff] [blame] | 344 | self.wfile.write('%s' % self.parsed_path.path) |
Steve McIntyre | 4f584a7 | 2015-09-28 02:28:56 +0100 | [diff] [blame] | 345 | logging.error('VLAN graphic not found - asked for %s', self.parsed_path.path) |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 346 | return |
| 347 | |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 348 | # Yes - just send it from the cache |
| 349 | self.send_response(200) |
| 350 | self.wfile.write('Content-type: image/png\r\n') |
| 351 | self.end_headers() |
| 352 | self.wfile.write(cache.graphics[vlan_id]['image']['png'].getvalue()) |
| 353 | return |
| 354 | |
| 355 | # Generate a PNG showing the layout of switches/port/trunks for a |
| 356 | # specific VLAN, and return that PNG along with geometry details |
| 357 | def generate_graphic(self, vlan_id): |
| 358 | db = self.server.state.db |
| 359 | vlan = db.get_vlan_by_id(vlan_id) |
| 360 | # We've been asked for a VLAN that doesn't exist |
| 361 | if vlan is None: |
| 362 | return None |
| 363 | |
| 364 | data = {} |
| 365 | data['image'] = {} |
| 366 | data['ports'] = {} |
| 367 | |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 368 | gim = Graphics() |
| 369 | |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 370 | # Pick fonts. TODO: Make these configurable? |
| 371 | gim.set_font(['/usr/share/fonts/truetype/inconsolata/Inconsolata.otf', |
| 372 | '/usr/share/fonts/truetype/freefont/FreeMono.ttf']) |
| 373 | try: |
| 374 | gim.font |
| 375 | # If we can't get the font we need, fail |
| 376 | except NameError: |
| 377 | self.send_response(500) |
Steve McIntyre | 4f584a7 | 2015-09-28 02:28:56 +0100 | [diff] [blame] | 378 | self.wfile.write('Content-type: text/plain\r\n') |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 379 | self.end_headers() |
Steve McIntyre | 4f584a7 | 2015-09-28 02:28:56 +0100 | [diff] [blame] | 380 | self.wfile.write('500 Internal Server Error\r\n') |
| 381 | logging.error('Unable to generate graphic, no fonts found - asked for %s', |
| 382 | self.parsed_path.path) |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 383 | return |
| 384 | |
| 385 | switch = {} |
| 386 | size_x = {} |
| 387 | size_y = {} |
| 388 | |
| 389 | switches = db.all_switches() |
| 390 | |
| 391 | # Need to set gaps big enough for the number of trunks, at least. |
| 392 | trunks = db.all_trunks() |
| 393 | y_gap = max(20, 15 * len(trunks)) |
| 394 | x_gap = max(20, 15 * len(trunks)) |
| 395 | |
| 396 | x = 0 |
| 397 | y = y_gap |
| 398 | |
| 399 | # Work out how much space we need for the switches |
| 400 | for i in range(0, len(switches)): |
| 401 | ports = db.get_ports_by_switch(switches[i].switch_id) |
| 402 | switch[i] = Switch(gim, len(ports), switches[i].name) |
| 403 | (size_x[i], size_y[i]) = switch[i].get_dimensions() |
| 404 | x = max(x, size_x[i]) |
| 405 | y += size_y[i] + y_gap |
| 406 | |
| 407 | # Add space for the legend and the label |
| 408 | label = "VLAN %d - %s" % (vlan.tag, vlan.name) |
| 409 | (legend_width, legend_height) = gim.get_legend_dimensions() |
| 410 | (label_width, label_height) = gim.get_label_size(label, gim.label_font_size) |
| 411 | x = max(x, legend_width + 2*x_gap + label_width) |
| 412 | x = x_gap + x + x_gap |
| 413 | y = y + max(legend_height + y_gap, label_height) |
| 414 | |
| 415 | # Create a canvas of the right size |
| 416 | gim.create_canvas(x, y) |
| 417 | |
| 418 | # Draw the switches and ports in it |
| 419 | curr_y = y_gap |
| 420 | for i in range(0, len(switches)): |
| 421 | switch[i].draw_switch(gim, x_gap, curr_y) |
| 422 | ports = db.get_ports_by_switch(switches[i].switch_id) |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 423 | data['ports'][i] = {} |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 424 | for port_id in ports: |
| 425 | port = db.get_port_by_id(port_id) |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 426 | port_location = switch[i].get_port_location(port.number) |
| 427 | data['ports'][i][port.number] = {} |
| 428 | data['ports'][i][port.number]['db'] = port |
| 429 | data['ports'][i][port.number]['location'] = port_location |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 430 | if port.is_locked: |
| 431 | switch[i].draw_port(gim, port.number, 'locked') |
| 432 | elif port.is_trunk: |
| 433 | switch[i].draw_port(gim, port.number, 'trunk') |
| 434 | elif port.current_vlan_id == int(vlan_id): |
| 435 | switch[i].draw_port(gim, port.number, 'VLAN') |
| 436 | else: |
| 437 | switch[i].draw_port(gim, port.number, 'normal') |
| 438 | curr_y += size_y[i] + y_gap |
| 439 | |
| 440 | # Now add the trunks |
| 441 | for i in range(0, len(trunks)): |
| 442 | ports = db.get_ports_by_trunk(trunks[i].trunk_id) |
| 443 | port1 = db.get_port_by_id(ports[0]) |
| 444 | port2 = db.get_port_by_id(ports[1]) |
| 445 | for s in range(0, len(switches)): |
| 446 | if switches[s].switch_id == port1.switch_id: |
| 447 | switch1 = s |
| 448 | if switches[s].switch_id == port2.switch_id: |
| 449 | switch2 = s |
| 450 | gim.draw_trunk(i, |
| 451 | switch[switch1].get_port_location(port1.number), |
| 452 | switch[switch2].get_port_location(port2.number), |
| 453 | gim.port_pallette['trunk']['trace']) |
| 454 | |
| 455 | # And the legend and label |
| 456 | gim.draw_legend(x_gap, curr_y) |
| 457 | gim.draw_label(x - label_width - 2*x_gap, curr_y, label, int(x_gap / 2)) |
| 458 | |
Steve McIntyre | 57a9d0a | 2015-10-28 18:22:31 +0000 | [diff] [blame] | 459 | # All done - push the image file into the cache for this vlan |
| 460 | data['image']['png'] = cStringIO.StringIO() |
| 461 | gim.im.writePng(data['image']['png']) |
| 462 | data['image']['width'] = x |
| 463 | data['image']['height'] = y |
| 464 | return data |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 465 | |
| 466 | # Implement an HTTP GET handler for the HTTPServer instance |
| 467 | def do_GET(self): |
| 468 | # Compare the URL path to any of the names we recognise and |
| 469 | # call the right generator function if we get a match |
| 470 | self.parsed_path = urlparse.urlparse(self.path) |
| 471 | for url in self.functionMap: |
| 472 | match = re.match(url['re'], self.parsed_path.path) |
| 473 | if match: |
| 474 | return url['fn'](self) |
| 475 | |
| 476 | # Fall-through for any files we don't recognise |
| 477 | self.send_response(404) |
Steve McIntyre | 4f584a7 | 2015-09-28 02:28:56 +0100 | [diff] [blame] | 478 | self.wfile.write('Content-type: text/plain\r\n') |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 479 | self.end_headers() |
| 480 | self.wfile.write('404 Not Found') |
Steve McIntyre | 0f561cd | 2015-10-28 18:05:20 +0000 | [diff] [blame] | 481 | self.wfile.write('%s' % self.parsed_path.path) |
Steve McIntyre | 4f584a7 | 2015-09-28 02:28:56 +0100 | [diff] [blame] | 482 | logging.error('File not supported - asked for %s', self.parsed_path.path) |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 483 | return |
| 484 | |
| 485 | # Override the BaseHTTPRequestHandler log_message() method so we |
| 486 | # can log requests properly |
Steve McIntyre | 9ff96bf | 2015-09-23 18:54:53 +0100 | [diff] [blame] | 487 | def log_message(self, fmt, *args): |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 488 | """Log an arbitrary message. """ |
Steve McIntyre | 9ff96bf | 2015-09-23 18:54:53 +0100 | [diff] [blame] | 489 | logging.info('%s %s', self.client_address[0], fmt%args) |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 490 | |
| 491 | functionMap = ( |
Steve McIntyre | 9ff96bf | 2015-09-23 18:54:53 +0100 | [diff] [blame] | 492 | {'re': r'^/$', 'fn': send_index}, |
| 493 | {'re': r'^/style.css$', 'fn': send_style}, |
| 494 | {'re': r'^/images/vlan/(\d+).png$', 'fn': send_graphic} |
Steve McIntyre | 2454bf0 | 2015-09-23 18:33:02 +0100 | [diff] [blame] | 495 | ) |