From b0b6f8e0cd47e57bf220dca09991b049bfb3ff6e Mon Sep 17 00:00:00 2001 From: kantorkel Date: Mon, 30 Nov 2015 19:45:14 +0100 Subject: [PATCH] status srv01 --- alfred_merge.py | 42 ++++++++++++++++++++++++++++++++++++++++++ backend.py | 7 +++---- lib/nodelist.py | 3 +++ lib/nodes.py | 21 ++++++++++++++++----- 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100755 alfred_merge.py diff --git a/alfred_merge.py b/alfred_merge.py new file mode 100755 index 0000000..aa623b2 --- /dev/null +++ b/alfred_merge.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +import subprocess +import json + +from collections import MutableMapping + +def rec_merge(d1, d2): + ''' + Update two dicts of dicts recursively, + if either mapping has leaves that are non-dicts, + the second's leaf overwrites the first's. + ''' + for k, v in d1.items(): # in Python 2, use .iteritems()! + if k in d2: + # this next check is the only difference! + if all(isinstance(e, MutableMapping) for e in (v, d2[k])): + d2[k] = rec_merge(v, d2[k]) + # we could further check types and merge as appropriate here. + d3 = d1.copy() + d3.update(d2) + return d3 + + +class alfred_merge: + def __init__(self,request_data_type_1 = 158, request_data_type_2 = 159): + self.request_data_type_1 = request_data_type_1 + self.request_data_type_2 = request_data_type_2 + + def aliases(self): + output = subprocess.check_output(["/usr/local/sbin/alfred-json","-z", "-r",str(self.request_data_type_1),"-f","json"]) + alfred_data_1 = json.loads(output.decode("utf-8")) + output = subprocess.check_output(["/usr/local/sbin/alfred-json","-z", "-r",str(self.request_data_type_2),"-f","json"]) + alfred_data_2 = json.loads(output.decode("utf-8")) + + return json.dumps(rec_merge(alfred_data_1, alfred_data_2)) + + +if __name__ == "__main__": + ad = alfred_merge() + al = ad.aliases() + print(al) + diff --git a/backend.py b/backend.py index 3ea2f5d..b2f38b7 100755 --- a/backend.py +++ b/backend.py @@ -84,9 +84,9 @@ def main(params): # integrate static aliases data for aliases in params['aliases']: with open(aliases, 'r') as f: - nodeinfo = validate_nodeinfos(json.load(f)) - nodes.import_nodeinfo(nodedb['nodes'], nodeinfo, - now, assume_online=False) +# nodeinfo = validate_nodeinfos(json.load(f)) + nodes.import_nodeinfo(nodedb['nodes'], json.load(f), + now, assume_online=False, statics=True) nodes.reset_statistics(nodedb['nodes']) for alfred in alfred_instances: @@ -157,7 +157,6 @@ def main(params): rrd.update_database(nodedb['nodes']) rrd.update_images() - if __name__ == '__main__': parser = argparse.ArgumentParser() diff --git a/lib/nodelist.py b/lib/nodelist.py index 15aea63..a931dcf 100644 --- a/lib/nodelist.py +++ b/lib/nodelist.py @@ -13,6 +13,9 @@ def export_nodelist(now, nodedb): node_out["status"] = dict() node_out["status"]["online"] = node["flags"]["online"] + if "firstseen" in node: + node_out["status"]["firstcontact"] = node["firstseen"] + if "lastseen" in node: node_out["status"]["lastcontact"] = node["lastseen"] diff --git a/lib/nodes.py b/lib/nodes.py index 3c0fa2f..f517438 100644 --- a/lib/nodes.py +++ b/lib/nodes.py @@ -56,12 +56,23 @@ def mark_online(node, now): node['flags']['online'] = True -def import_nodeinfo(nodes, nodeinfos, now, assume_online=False): +def overrideFields(dest, src, fields): + for field in fields: + if field in src: + dest[field] = src[field] + else: + dest.pop(field, None) + + +def import_nodeinfo(nodes, nodeinfos, now, assume_online=False, statics=False): for nodeinfo in filter(lambda d: 'node_id' in d, nodeinfos): - node = nodes.setdefault(nodeinfo['node_id'], {'flags': dict()}) - node['nodeinfo'] = nodeinfo - node['flags']['online'] = False - node['flags']['gateway'] = False + node = nodes.setdefault(nodeinfo['node_id'], {'flags': {'online': False, 'gateway': False}}) + + if statics: + node['nodeinfo'] = node.setdefault('nodeinfo', {}) + overrideFields(node['nodeinfo'], nodeinfo, ['hostname', 'location', 'node_id']) + else: + node['nodeinfo'] = nodeinfo if assume_online: mark_online(node, now)