diff --git a/.travis.yml b/.travis.yml index cb081fa..dcaceb4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,4 @@ language: python python: - "3.4" install: "pip install pep8" -script: "pep8 *.py" +script: "pep8 *.py lib/*.py" diff --git a/lib/NodeRRD.py b/lib/NodeRRD.py index 37bc6f9..afabe6f 100644 --- a/lib/NodeRRD.py +++ b/lib/NodeRRD.py @@ -32,11 +32,13 @@ class NodeRRD(RRD): @property def imagename(self): - return "{basename}.png".format(basename=os.path.basename(self.filename).rsplit('.', 2)[0]) + return "{basename}.png".format( + basename=os.path.basename(self.filename).rsplit('.', 2)[0]) # TODO: fix this, python does not support function overloading def update(self): - super().update({'upstate': int(self.node['flags']['online']), 'clients': self.node['statistics']['clients']}) + super().update({'upstate': int(self.node['flags']['online']), + 'clients': self.node['statistics']['clients']}) def graph(self, directory, timeframe): """ diff --git a/lib/RRD.py b/lib/RRD.py index 799338c..3c406ac 100644 --- a/lib/RRD.py +++ b/lib/RRD.py @@ -83,9 +83,11 @@ class RRD(object): info = self.info() if set(ds_list) - set(info['ds'].values()) != set(): for ds in ds_list: - if ds.name in info['ds'] and ds.type != info['ds'][ds.name].type: - raise RRDIncompatibleException("%s is %s but should be %s" % - (ds.name, ds.type, info['ds'][ds.name].type)) + if ds.name in info['ds'] and\ + ds.type != info['ds'][ds.name].type: + raise RRDIncompatibleException( + "{} is {} but should be {}".format( + ds.name, ds.type, info['ds'][ds.name].type)) else: raise RRDOutdatedException() @@ -108,8 +110,10 @@ class RRD(object): if ds.name in info['ds']: old_ds = info['ds'][ds.name] if info['ds'][ds.name].type != ds.type: - raise RuntimeError('Cannot convert existing DS "%s" from type "%s" to "%s"' % - (ds.name, old_ds.type, ds.type)) + raise RuntimeError( + "Cannot convert existing DS '{}'" + "from type '{}' to '{}'".format( + ds.name, old_ds.type, ds.type)) ds.index = old_ds.index new_ds[ds.index] = ds else: @@ -237,7 +241,8 @@ class RRD(object): for line in out.splitlines(): base = info for match in self._info_regex.finditer(line): - section, key, name, value = match.group("section", "key", "name", "value") + section, key, name, value = match.group( + "section", "key", "name", "value") if section and key: try: key = int(key) @@ -258,7 +263,8 @@ class RRD(object): base[name] = value dss = {} for name, ds in info['ds'].items(): - ds_obj = DS(name, ds['type'], ds['minimal_heartbeat'], ds['min'], ds['max']) + ds_obj = DS(name, ds['type'], ds['minimal_heartbeat'], + ds['min'], ds['max']) ds_obj.index = ds['index'] ds_obj.last_ds = ds['last_ds'] ds_obj.value = ds['value'] @@ -267,7 +273,8 @@ class RRD(object): info['ds'] = dss rras = [] for rra in info['rra'].values(): - rras.append(RRA(rra['cf'], rra['xff'], rra['pdp_per_row'], rra['rows'])) + rras.append(RRA(rra['cf'], rra['xff'], + rra['pdp_per_row'], rra['rows'])) info['rra'] = rras self._cached_info = info return info diff --git a/lib/graph.py b/lib/graph.py index d5163ff..7b45828 100644 --- a/lib/graph.py +++ b/lib/graph.py @@ -8,12 +8,17 @@ from lib.nodes import build_mac_table def import_vis_data(graph, nodes, vis_data): macs = build_mac_table(nodes) - nodes_a = map(lambda d: 2*[d['primary']], filter(lambda d: 'primary' in d, vis_data)) - nodes_b = map(lambda d: [d['secondary'], d['of']], filter(lambda d: 'secondary' in d, vis_data)) - graph.add_nodes_from(map(lambda a, b: (a, dict(primary=b, node_id=macs.get(b))), *zip(*chain(nodes_a, nodes_b)))) + nodes_a = map(lambda d: 2*[d['primary']], + filter(lambda d: 'primary' in d, vis_data)) + nodes_b = map(lambda d: [d['secondary'], d['of']], + filter(lambda d: 'secondary' in d, vis_data)) + graph.add_nodes_from(map(lambda a, b: + (a, dict(primary=b, node_id=macs.get(b))), + *zip(*chain(nodes_a, nodes_b)))) edges = filter(lambda d: 'neighbor' in d, vis_data) - graph.add_edges_from(map(lambda d: (d['router'], d['neighbor'], dict(tq=float(d['label']))), edges)) + graph.add_edges_from(map(lambda d: (d['router'], d['neighbor'], + dict(tq=float(d['label']))), edges)) def mark_vpn(graph, vpn_macs): @@ -32,11 +37,13 @@ def to_multigraph(graph): return node['primary'] if node else a def map_node(node, data): - return (data['primary'], dict(node_id=data['node_id'])) if data else (node, dict()) + return (data['primary'], + dict(node_id=data['node_id'])) if data else (node, dict()) digraph = nx.MultiDiGraph() digraph.add_nodes_from(map(map_node, *zip(*graph.nodes_iter(data=True)))) - digraph.add_edges_from(map(lambda a, b, data: (f(a), f(b), data), *zip(*graph.edges_iter(data=True)))) + digraph.add_edges_from(map(lambda a, b, data: (f(a), f(b), data), + *zip(*graph.edges_iter(data=True)))) return digraph @@ -50,8 +57,9 @@ def merge_nodes(graph): multigraph = to_multigraph(graph) digraph = nx.DiGraph() digraph.add_nodes_from(multigraph.nodes_iter(data=True)) - edges = chain.from_iterable([[(e, d, merge_edges(multigraph[e][d].values())) - for d in multigraph[e]] for e in multigraph]) + edges = chain.from_iterable([[(e, d, merge_edges( + multigraph[e][d].values())) + for d in multigraph[e]] for e in multigraph]) digraph.add_edges_from(edges) return digraph @@ -69,8 +77,9 @@ def to_undirected(graph): graph = nx.Graph() graph.add_nodes_from(multigraph.nodes_iter(data=True)) - edges = chain.from_iterable([[(e, d, merge_edges(multigraph[e][d].values())) - for d in multigraph[e]] for e in multigraph]) + edges = chain.from_iterable([[(e, d, merge_edges( + multigraph[e][d].values())) + for d in multigraph[e]] for e in multigraph]) graph.add_edges_from(edges) return graph diff --git a/lib/nodes.py b/lib/nodes.py index 23a2b0e..bb1e129 100644 --- a/lib/nodes.py +++ b/lib/nodes.py @@ -53,23 +53,26 @@ def reset_statistics(nodes): node['statistics'] = {'clients': 0} -def import_statistics(nodes, statistics): +def import_statistics(nodes, stats): def add(node, statistics, target, source, f=lambda d: d): try: - node['statistics'][target] = f(reduce(dict.__getitem__, source, statistics)) + node['statistics'][target] = f(reduce(dict.__getitem__, + source, + statistics)) except (KeyError, TypeError): pass macs = build_mac_table(nodes) - statistics = filter(lambda d: 'node_id' in d, statistics) - statistics = filter(lambda d: d['node_id'] in nodes, statistics) - for node, statistics in map(lambda d: (nodes[d['node_id']], d), statistics): - add(node, statistics, 'clients', ['clients', 'total']) - add(node, statistics, 'gateway', ['gateway'], lambda d: macs.get(d, d)) - add(node, statistics, 'uptime', ['uptime']) - add(node, statistics, 'loadavg', ['loadavg']) - add(node, statistics, 'memory_usage', ['memory'], lambda d: 1 - d['free'] / d['total']) - add(node, statistics, 'rootfs_usage', ['rootfs_usage']) + stats = filter(lambda d: 'node_id' in d, stats) + stats = filter(lambda d: d['node_id'] in nodes, stats) + for node, stats in map(lambda d: (nodes[d['node_id']], d), stats): + add(node, stats, 'clients', ['clients', 'total']) + add(node, stats, 'gateway', ['gateway'], lambda d: macs.get(d, d)) + add(node, stats, 'uptime', ['uptime']) + add(node, stats, 'loadavg', ['loadavg']) + add(node, stats, 'memory_usage', ['memory'], + lambda d: 1 - d['free'] / d['total']) + add(node, stats, 'rootfs_usage', ['rootfs_usage']) def import_mesh_ifs_vis_data(nodes, vis_data): diff --git a/lib/rrddb.py b/lib/rrddb.py index bcd33b3..57437a7 100644 --- a/lib/rrddb.py +++ b/lib/rrddb.py @@ -28,8 +28,10 @@ class RRD(object): os.mkdir(self.imagePath) def update_database(self, nodes): - online_nodes = dict(filter(lambda d: d[1]['flags']['online'], nodes.items())) - client_count = sum(map(lambda d: d['statistics']['clients'], online_nodes.values())) + online_nodes = dict(filter( + lambda d: d[1]['flags']['online'], nodes.items())) + client_count = sum(map( + lambda d: d['statistics']['clients'], online_nodes.values())) self.globalDb.update(len(online_nodes), client_count) for node_id, node in online_nodes.items(): @@ -37,7 +39,8 @@ class RRD(object): rrd.update() def update_images(self): - self.globalDb.graph(os.path.join(self.imagePath, "globalGraph.png"), self.displayTimeGlobal) + self.globalDb.graph(os.path.join(self.imagePath, "globalGraph.png"), + self.displayTimeGlobal) nodedb_files = os.listdir(self.dbPath)