diff --git a/backend.py b/backend.py index 4bf9058..392d931 100755 --- a/backend.py +++ b/backend.py @@ -16,6 +16,7 @@ from lib import graph, nodes from lib.alfred import Alfred from lib.batman import Batman from lib.rrddb import RRD +from lib.nodelist import export_nodelist NODES_VERSION = 1 GRAPH_VERSION = 1 @@ -26,6 +27,7 @@ def main(params): nodes_fn = os.path.join(params['dest_dir'], 'nodes.json') graph_fn = os.path.join(params['dest_dir'], 'graph.json') + nodelist_fn = os.path.join(params['dest_dir'], 'nodelist.json') now = datetime.utcnow().replace(microsecond=0) @@ -128,6 +130,9 @@ def main(params): with open(graph_fn, 'w') as f: json.dump(graph_out, f) + with open(nodelist_fn, 'w') as f: + json.dump(export_nodelist(now, nodedb), f) + # optional rrd graphs (trigger with --rrd) if params['rrd']: script_directory = os.path.dirname(os.path.realpath(__file__)) diff --git a/lib/nodelist.py b/lib/nodelist.py new file mode 100644 index 0000000..15aea63 --- /dev/null +++ b/lib/nodelist.py @@ -0,0 +1,24 @@ +def export_nodelist(now, nodedb): + nodelist = list() + + for node_id, node in nodedb["nodes"].items(): + node_out = dict() + node_out["id"] = node_id + node_out["name"] = node["nodeinfo"]["hostname"] + + if "location" in node["nodeinfo"]: + node_out["position"] = {"lat": node["nodeinfo"]["location"]["latitude"], + "long": node["nodeinfo"]["location"]["longitude"]} + + node_out["status"] = dict() + node_out["status"]["online"] = node["flags"]["online"] + + if "lastseen" in node: + node_out["status"]["lastcontact"] = node["lastseen"] + + if "clients" in node["statistics"]: + node_out["status"]["clients"] = node["statistics"]["clients"] + + nodelist.append(node_out) + + return {"version": "1.0.1", "nodes": nodelist, "updated_at": now.isoformat()}