diff --git a/packages/ns-api/files/ns.talkers b/packages/ns-api/files/ns.talkers index 021b45e52..7d2f55a86 100755 --- a/packages/ns-api/files/ns.talkers +++ b/packages/ns-api/files/ns.talkers @@ -14,7 +14,8 @@ import socket def list_talkers(limit = 20, stat_file="/tmp/netify-plugin-stats.json"): data = None stats = dict() - time_diff = 1 + applications = dict() + protocols = dict() with open(stat_file, 'r') as fp: data = json.load(fp) @@ -39,19 +40,33 @@ def list_talkers(limit = 20, stat_file="/tmp/netify-plugin-stats.json"): for app in data['stats'][mac][ip]: if app not in stats[ip]['apps']: stats[ip]['apps'][app] = 0 + if app not in applications: + applications[app] = 0 for proto in data['stats'][mac][ip][app]: + if proto not in protocols: + protocols[proto] = 0 stats[ip]['totals']['download'] += data['stats'][mac][ip][app][proto]['download'] stats[ip]['totals']['upload'] += data['stats'][mac][ip][app][proto]['upload'] stats[ip]['totals']['bandwidth'] += data['stats'][mac][ip][app][proto]['download'] + data['stats'][mac][ip][app][proto]['upload'] stats[ip]['totals']['packets'] += data['stats'][mac][ip][app][proto]['packets'] stats[ip]['apps'][app] += data['stats'][mac][ip][app][proto]['download'] + data['stats'][mac][ip][app][proto]['upload'] + applications[app] += data['stats'][mac][ip][app][proto]['download'] + data['stats'][mac][ip][app][proto]['upload'] + + protocols[proto] += data['stats'][mac][ip][app][proto]['download'] + data['stats'][mac][ip][app][proto]['upload'] + # name resolution can highly slow down the API stats[ip]['host'] = socket.getnameinfo((ip, 0), 0)[0] stats[ip]['totals']['bandwidth'] = stats[ip]['totals']['bandwidth'] / time_diff ordered = sorted(stats.values(), key=lambda x: x['totals']['bandwidth'], reverse=True) - return ordered[:limit] + applications = {k: v for k, v in sorted(applications.items(), key=lambda x: x[1], reverse=True)} + protocols = {k: v for k, v in sorted(protocols.items(), key=lambda x: x[1], reverse=True)} + return { + 'top_hosts': ordered[:limit], + 'top_apps': dict(list(applications.items())[:limit]), + 'top_protocols': dict(list(protocols.items())[:limit]) + } cmd = sys.argv[1]