Last active
January 13, 2018 19:08
-
-
Save n0rc/67630dad27455510688e2e3bfb68a179 to your computer and use it in GitHub Desktop.
wsgi script to get the current tinc graph as png image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import re | |
from subprocess import Popen, PIPE, check_output | |
from urlparse import parse_qs | |
re_filter = re.compile(r' \w+ \[label = "[^"]+", color = "red"];\n') | |
def application(environ, start_response): | |
d = parse_qs(environ['QUERY_STRING']) | |
mode = d.get('mode', [''])[0] | |
# this command requires sudo permissions for the user running the wsgi/web service | |
graph = check_output(["/usr/bin/sudo", "/path/to/tinc", "-n", "yournetwork", "dump", "graph"]) | |
if mode != "all": | |
# remove unreachable nodes | |
graph = re_filter.sub('', graph) | |
# you can also use dot, circo, whatever with different options here | |
p = Popen(["/usr/bin/neato", "-Tpng", "-Nfontname=Sans", "-Goverlap=scale", "-Gsplines=true"], stdin=PIPE, stdout=PIPE) | |
data, _ = p.communicate(input=graph) | |
status = '200 OK' | |
if p.returncode == 0: | |
headers = [('Content-Type', 'image/png')] | |
ret = data | |
else: | |
headers = [('Content-Type', 'text/plain')] | |
ret = "some bad shit went down" | |
start_response(status, headers) | |
return [ret] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 3 writes image files to disk for caching.
Revision 1 keeps everything in memory but calls tinc every single time a request is made.