Skip to content

Commit

Permalink
ns-api: added applications and protocols to ns.talkers api
Browse files Browse the repository at this point in the history
Signed-off-by: Tommaso Bailetti <tommaso.bailetti@nethesis.it>
  • Loading branch information
Tbaile committed Sep 23, 2024
1 parent e2ea8a3 commit f79c7af
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/ns-api/files/ns.talkers
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down

0 comments on commit f79c7af

Please sign in to comment.