From f1e069f1b81ac66f6776bfb95d902c641635174c Mon Sep 17 00:00:00 2001 From: hans362 Date: Mon, 6 Feb 2023 11:01:53 +0800 Subject: [PATCH] :sparkles: Add packet loss statistics --- agent.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/agent.py b/agent.py index 82c46b1..71634f3 100644 --- a/agent.py +++ b/agent.py @@ -38,6 +38,21 @@ def RegisterRecord(apiEndpoint, secret, agentId, nodeId, latency): print(e) +def PingNode(hostname, count=4, unit='ms'): + sum = 0 + times = count + for i in range(count): + latency = ping(hostname, unit=unit) + if latency != None and latency != False: + sum += latency + else: + times -= 1 + if times == 0: + return None, 1.00 + else: + return round(sum/times, 2), round((count-times)/count, 2) + + # Accept args from command line parser = argparse.ArgumentParser() parser.add_argument('-a', '--apiEndpoint', @@ -62,10 +77,10 @@ if nodeList == None: # Ping nodes for node in nodeList: print('Pinging ' + node['hostname']) - latency = ping(node['hostname'], unit='ms') - if latency == None or latency == False: + latency, loss = PingNode(node['hostname']) + if latency == None: latency = -1 - latency = int(latency) print('latency: ' + str(latency)) + print('loss: ' + str(loss)) RegisterRecord(args.apiEndpoint, args.secret, - str(agentId), str(node['id']), str(latency)) + str(agentId), str(node['id']), str(latency), str(loss))