Below is the file 'drawnetwork.py' from this revision. You can also download the file.
#!/usr/bin/python from pyPgSQL import libpq import sys import sets def build_tuple_dictionary(res): rv = [] for tuple_index in range(res.ntuples): rvn = {} for field_index in range(res.nfields): rvn[res.fname(field_index)] = res.getvalue(tuple_index, field_index) rv.append(rvn) return rv def draw_graph(): def q(s): if s == None: return "NULL" else: return libpq.PgQuoteString(s) def get_hostname(ip_address): if not ip_address: return query = "SELECT hostname FROM addresses WHERE host(ip_address)=%s LIMIT 1" % (q(ip_address)) res = cnx.query(query) routers = build_tuple_dictionary(res) if len(routers): return routers[0]['hostname'] else: return None cnx = libpq.PQconnectdb("dbname=netinfo") res = cnx.query("SELECT hostname, adjacent_hostname, ip_address FROM adjacency") res = build_tuple_dictionary(res) print """\ graph smut { graph [rankdir=LR, ranksep="2.00", ratio="0.40"]; """ hosts = sets.Set() drawn = sets.Set() internal_lines = [] external_lines = [] for row in res: hostname = row.get('hostname') adjacent_hostname = row.get('adjacent_hostname') ip_address = row.get('ip_address') # try and resolve this adjacent machine to one of our configured devices adjacent = get_hostname(ip_address) # if we match, then it is a configured device (and internal) if adjacent: in_network = True else: adjacent = adjacent_hostname in_network = False if hostname: hosts.add(hostname) if adjacent: hosts.add(adjacent) if not hostname or not adjacent: continue if (hostname, adjacent) in drawn or (adjacent, hostname) in drawn: continue linkstr = '"%s"--"%s"' % (hostname, adjacent) if in_network: internal_lines.append(linkstr) else: external_lines.append(linkstr) drawn.add((hostname, adjacent)) print "subgraph cluster_CORE {" print " bgcolor=grey" print '\n'.join(internal_lines) print "}" print '\n'.join(external_lines) for host in hosts: print '"%s" [href="%s"]' % (host, host) print "}" if __name__ == '__main__': draw_graph()