fix stddev calculaction logic
and other minor refactors
This commit is contained in:
parent
74f88e6601
commit
30d2adbb10
26
dnseval.py
26
dnseval.py
@ -37,7 +37,7 @@ import dns.resolver
|
|||||||
|
|
||||||
__VERSION__ = 1.0
|
__VERSION__ = 1.0
|
||||||
__PROGNAME__ = os.path.basename(sys.argv[0])
|
__PROGNAME__ = os.path.basename(sys.argv[0])
|
||||||
should_stop = False
|
shutdown = False
|
||||||
|
|
||||||
resolvers = dns.resolver.get_default_resolver().nameservers
|
resolvers = dns.resolver.get_default_resolver().nameservers
|
||||||
|
|
||||||
@ -56,10 +56,10 @@ usage: %s [-h] [-f server-list] [-c count] [-t type] [-w wait] hostname
|
|||||||
|
|
||||||
|
|
||||||
def signal_handler(sig, frame):
|
def signal_handler(sig, frame):
|
||||||
global should_stop
|
global shutdown
|
||||||
if should_stop: # pressed twice, so exit immediately
|
if shutdown: # pressed twice, so exit immediately
|
||||||
exit(0)
|
exit(0)
|
||||||
should_stop = True # pressed once, exit gracefully
|
shutdown = True # pressed once, exit gracefully
|
||||||
|
|
||||||
|
|
||||||
def maxlen(names):
|
def maxlen(names):
|
||||||
@ -77,10 +77,8 @@ def dnsping(host, server, dnsrecord, timeout, count):
|
|||||||
response_time = []
|
response_time = []
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
# print('DEBUG: host = %s , server = %s , count = %d' % (host, resolver.nameservers[0], count))
|
|
||||||
|
|
||||||
for i in range(count):
|
for i in range(count):
|
||||||
if should_stop:
|
if shutdown:
|
||||||
break
|
break
|
||||||
try:
|
try:
|
||||||
stime = time.time()
|
stime = time.time()
|
||||||
@ -102,7 +100,10 @@ def dnsping(host, server, dnsrecord, timeout, count):
|
|||||||
r_min = min(response_time)
|
r_min = min(response_time)
|
||||||
r_max = max(response_time)
|
r_max = max(response_time)
|
||||||
r_avg = sum(response_time) / r_received
|
r_avg = sum(response_time) / r_received
|
||||||
r_stddev = stdev(response_time)
|
if len(response_time) > 1:
|
||||||
|
r_stddev = stdev(response_time)
|
||||||
|
else:
|
||||||
|
r_stddev = 0
|
||||||
else:
|
else:
|
||||||
r_min = 0
|
r_min = 0
|
||||||
r_max = 0
|
r_max = 0
|
||||||
@ -116,7 +117,7 @@ def main():
|
|||||||
try:
|
try:
|
||||||
signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore CTRL+Z
|
signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore CTRL+Z
|
||||||
signal.signal(signal.SIGINT, signal_handler) # ignore CTRL+C
|
signal.signal(signal.SIGINT, signal_handler) # ignore CTRL+C
|
||||||
except AttributeError: # Some systems may not support all signals
|
except AttributeError: # Some systems may not support all signals
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
@ -166,7 +167,7 @@ def main():
|
|||||||
if len(f) == 0:
|
if len(f) == 0:
|
||||||
print("No nameserver specified")
|
print("No nameserver specified")
|
||||||
|
|
||||||
f = [ name.strip() for name in f]
|
f = [name.strip() for name in f]
|
||||||
width = maxlen(f)
|
width = maxlen(f)
|
||||||
blanks = (width - 5) * ' '
|
blanks = (width - 5) * ' '
|
||||||
print('server ', blanks, ' avg(ms) min(ms) max(ms) stddev(ms) lost(%)')
|
print('server ', blanks, ' avg(ms) min(ms) max(ms) stddev(ms) lost(%)')
|
||||||
@ -174,11 +175,12 @@ def main():
|
|||||||
for server in f:
|
for server in f:
|
||||||
if not server:
|
if not server:
|
||||||
continue
|
continue
|
||||||
(server, r_avg, r_min, r_max, r_stddev, r_lost_percent) = dnsping(hostname, server, dnsrecord, waittime, count)
|
(server, r_avg, r_min, r_max, r_stddev, r_lost_percent) = dnsping(hostname, server, dnsrecord, waittime,
|
||||||
|
count)
|
||||||
|
|
||||||
server = server.ljust(width + 1)
|
server = server.ljust(width + 1)
|
||||||
print("%s %-8.3f %-8.3f %-8.3f %-8.3f %%%d" % (
|
print("%s %-8.3f %-8.3f %-8.3f %-8.3f %%%d" % (
|
||||||
server, r_avg, r_min, r_max, r_stddev, r_lost_percent))
|
server, r_avg, r_min, r_max, r_stddev, r_lost_percent), flush=True)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('error: %s' % e)
|
print('error: %s' % e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user