def scan_network(retry=3): GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(INTERNET_INDICATOR_GPIO, GPIO.OUT) redis_cache.set('network_hosts', '...') redis_cache.set('gateway_latency', '...') redis_cache.set('google_latency', '...') redis_cache.set('ip_address', 'IP Unavailable') if GPIO.input(INTERNET_INDICATOR_GPIO) == 0: redis_cache.set( 'ip_address', os.popen( "ifconfig wlan0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'" ).read().strip()) gateway_ip = os.popen( "route -n | grep 'UG[ \t]' | awk '{print $2}'").read().strip() nm = nmap.PortScanner() redis_cache.set( 'network_hosts', nm.scan(hosts=gateway_ip[:-1] + '0/24', arguments='-sP')['nmap']['scanstats']['uphosts']) redis_cache.set( 'gateway_latency', '{:.0f}ms'.format( sum(measure_latency(host=gateway_ip, runs=10)) / 10)) redis_cache.set( 'google_latency', '{:.0f}ms'.format( sum(measure_latency('www.google.com', runs=10)) / 10)) elif retry > 0: time.sleep(10) scan_network(retry - 1)
def ping(self): lat = measure_latency(host=self.__server) try: self.__json['latency'] = str(int(lat[0])) except: self.__json['latency'] = str(500) return self.__json['latency']
def checkHost(checkType, host=None, port=80, attempts=10, timeout=4): checkType = str(checkType).lower() if (checkType == "icmp"): raise NotImplementedError elif (checkType == "tcp_up"): raise NotImplementedError elif (checkType == "latency"): pass if (host is not None): latency_pre = measure_latency(host=host, port=port, runs=attempts, timeout=timeout) else: raise ValueError sum = 0 valid = 0 dropped = 0 for time in latency_pre: try: sum += time valid += 1 except Exception as exp: global debug if debug: print("Exception: "+ str(exp)) dropped += 1 #return False average = 0 if sum is not 0: average = sum/valid return {"avg": float(str("%.2f" % average)), "dropped": dropped}
def send_memory_warning(mem, swap, status): global memory_good_counter global memory_warning_counter global memory_critical_counter global swap_good_counter global swap_warning_counter global swap_critical_counter if status == "good": color_bla = 8311585 elif status == "warning": color_bla = 16312092 elif status == "critical": color_bla = 13632027 else: color_bla = 13632027 embed = DiscordEmbed(title=f'Memory {status} [{server_name}]', description=f'{server_name} {status}', color=color_bla) embed.set_footer(text=f"Server Watcher | {server_name}", icon_url=icon_url_footer) embed.add_embed_field(name="Memory", value=f'{str(get_gb(mem.total))}GB', inline=True) embed.add_embed_field(name="Memory Available", value=f'{str(get_gb(mem.available))}GB', inline=True) embed.add_embed_field(name="Status", value=status, inline=True) embed.add_embed_field(name="Swap", value=f'{str(get_gb(swap.total))}GB', inline=True) embed.add_embed_field(name="Swap Free", value=f'{str(get_gb(swap.free))}GB', inline=True) #ping (just to add another field to make it look better) ping_here_b = measure_latency(host='8.8.8.8') ping_here = round(float(ping_here_b[0]), 3) embed.add_embed_field(name="Ping", value=f'{str(ping_here)}ms', inline=True) embed.add_embed_field( name="Memory Counter", value= f'**Good:** {memory_good_counter}\n**Warning:** {memory_warning_counter}\n**Critical:** {memory_critical_counter}\n', inline=True) embed.add_embed_field( name="SWAP Counter", value= f'**Good:** {swap_good_counter}\n**Warning:** {swap_warning_counter}\n**Critical:** {swap_critical_counter}\n', inline=True) webhook = DiscordWebhook(url=webhook_url) webhook.add_embed(embed) response = webhook.execute()
def latency(hosts, runs): hosts_list = [] for host, port in hosts.items(): keys = ['host', 'port', 'latencies'] values = [host, port, measure_latency(host=host, port=port, runs=runs)] host_dict = {key: value for (key, value) in zip(keys, values)} hosts_list.append(host_dict) return hosts_list
def calcLatency(self, data): result = measure_latency(host=data, port=443, timeout=1) latency = result[0] try: self.telegraf_client.metric('tcp_latency', {"latency": latency}, tags={'exchange': 'bitstamp'}) except Exception as e: print(e)
def output(): for url, tag in data['websites_and_content_requirements'].items(): if (get_status(url)): latency = measure_latency(host=url, port=80) MonitoredUrl(url, latency, True, True) print(f"Server hosting {url} is up, latency to it is {latency}") else: MonitoredUrl(url, 0, False, True) print(f"Server hosting {url} is down")
def min_rtt(self): rtts = [] try: rtts = measure_latency(host=self._host, port=80, runs=5, timeout=5) except Exception: return rtts = list(filter(None, rtts)) if len(rtts) > 0: self._result.put(Mirror(self._url, self._host, min(rtts)))
def GetLatency(self): """determines latency from speedbox to speed server retunrs the average """ temp = measure_latency(host=self.latency_server , port=80, runs=10, timeout=2.5) #calculate average sum = 0. for k in range(0,len(temp)-1): sum = sum + temp[k] return sum/len(temp)
def ping(address, port): ping = measure_latency(host=address, port=port, runs=1, timeout=2.5)[0] is_alive = True if ping == None: ping = -1 is_alive = False return { "ping": utils.format.format_output(round(ping)), "is_alive": is_alive }
def LatencyDataCollection(t_host_list, t_port_list, t_timeout, t_wait, t_file_name_list): # Create a loop for data collection. for i in range(number_of_data_points): for ii in range(len(t_host_list)): latency_ms = measure_latency(host=t_host_list[ii], port=t_port_list[ii], \ timeout=t_timeout, runs=1) # latency_ms is a list with one element. if latency_ms[0] is None: latency_ms = [0 ] # If timeout, use 0 to replace the None value. time_at_measurement = datetime.datetime.now() latency_value = latency_ms[0] with open(t_file_name_list[ii], 'a') as data_file: data_file.write('%s' % time_at_measurement + ',' + \ '%s' % latency_value + '\n') time.sleep(t_wait) # Wait for next TCP connection. return
def test_tcp(who, stype, host, port): delay = 1000 my_post_body = {'who': who, 'type': stype, 'latency': delay} while True: # ping server try: delay_ar = measure_latency(host=host, port=port, wait=0) delay = round(delay_ar[0], 3) except: delay = 1000 # write latency to post data my_post_body['latency'] = delay # post to rest API ret = mypost(my_post_body) if (ret) != 200: print('rest service error [%d]' % ret, file=sys.stderr) if stype == 'ping' and delay == 1000: continue time.sleep(0.9)
def pingtesttcp(): timestamp = datetime.datetime.utcnow() for target in PING_TARGETS.split(','): target = target.strip() pingtest = measure_latency(host=target, runs=1, timeout=1) data = [{ 'measurement': 'pings', 'time': timestamp, 'tags': { 'namespace': NAMESPACE, 'target': target }, 'fields': { 'success': int(pingtest[0] is None), 'rtt': float(0 if pingtest[0] is None else pingtest[0]) } }] if influxdb_client.write_points(data) == True: print("Ping data written to DB successfully") else: # Speedtest failed. print("Ping Failed.")
def main(): row_dict_list = [] with open('pools.csv') as f: reader = csv.DictReader(f, delimiter=',') for row in reader: row_dict = {} row_dict['coin'] = row['coin'] row_dict['region'] = row['region'] row_dict['host'] = row['host'] row_dict['port'] = row['port'] row_dict_list.append(row_dict) logging.info(row_dict_list) with open('latency.csv', 'w') as f: fields = [ 'coin', 'region', 'host', 'port', 'latency_mean', 'latency_std' ] f_csv = csv.DictWriter(f, fieldnames=fields) f_csv.writeheader() for row_dict in row_dict_list: # print(row_dict) res = measure_latency(host=row_dict['host'], port=row_dict['port'], runs=10, timeout=2.5) # basic data processing res = [i for i in res if i] # remove None elements if len(res) == 0: continue # mean and std row_dict['latency_mean'] = mean(res) row_dict['latency_std'] = stdev(res) logging.info(row_dict) # write f_csv.writerow(row_dict)
def test_tcpLatency_multiple_runs(): number_of_iterations = 10 multiple_runs = measure_latency( host='google.com', runs=number_of_iterations, ) assert len(multiple_runs) is number_of_iterations
result = client.run() show(client, result) f.write(f'{test["section"]},') dump(client, result, f) if client.protocol == 'tcp' and tcp_mss_default is None and result.error is None: tcp_mss_default = result.tcp_mss_default del client except Exception as msg: print(f'Failed running iperf test: {msg}') elif test['section'].startswith('latency'): print(f'Measure latency..') try: from tcp_latency import measure_latency data = measure_latency(host=server['host'], port=server['latency_port'], runs=test.get('runs', 10), wait=test.get('wait', 1), timeout=test.get('timeout', 5)) data = [d for d in data if not d] if data: print(f'{test["section"]} min/avg/max: {min(data)}/{sum(data)/len(data)}/{max(data)} ms') f.write(f'{test["section"]},{min(data)},{sum(data)/len(data)},{max(data)}\n') f.flush() else: print('Skipping latency test: no data.') except Exception as msg: print(f'Failed running latency test: {msg}') elif test['section'].startswith('ping'): print(f'Ping..') tfn = os.path.join(td, 'ping.txt') cmd = f'ping {server["host"]} -n -c {test.get("count", 10)} -q > {tfn}'
def ping_datanodes(datanodes): latency = [] for datanode in datanodes: latency.append(measure_latency(host=datanode, port=21)[0]) return latency
def main(): parser = argparse.ArgumentParser() parser.add_argument("--model", type=str) parser.add_argument("--ip", type=str) parser.add_argument("--verbose", "-v", action='store_true') parser.add_argument('--report_interval', '-r', help="Duration of reporting interval, in seconds", default=10, type=int) args = parser.parse_args() relay = Relay(args.ip) # Initialize TRT environment input_shape = (300, 300) trt_logger = trt.Logger(trt.Logger.INFO) trt.init_libnvinfer_plugins(trt_logger, '') with open(args.model, 'rb') as f, trt.Runtime(trt_logger) as runtime: engine = runtime.deserialize_cuda_engine(f.read()) host_inputs = [] cuda_inputs = [] host_outputs = [] cuda_outputs = [] bindings = [] stream = cuda.Stream() for binding in engine: size = trt.volume( engine.get_binding_shape(binding)) * engine.max_batch_size host_mem = cuda.pagelocked_empty(size, np.float32) cuda_mem = cuda.mem_alloc(host_mem.nbytes) bindings.append(int(cuda_mem)) if engine.binding_is_input(binding): host_inputs.append(host_mem) cuda_inputs.append(cuda_mem) else: host_outputs.append(host_mem) cuda_outputs.append(cuda_mem) context = engine.create_execution_context() watch = Stopwatch() while True: img = relay.get_image() print("Received image", watch.numread) if img is None: break # Preprocessing: watch.start() ih, iw = img.shape[:-1] if (iw, ih) != input_shape: img = cv2.resize(img, input_shape) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img.transpose((2, 0, 1)).astype(np.float32) img *= (2.0 / 255.0) img -= 1.0 np.copyto(host_inputs[0], img.ravel()) watch.stop(Stopwatch.MODE_PREPROCESS) watch.start() cuda.memcpy_htod_async(cuda_inputs[0], host_inputs[0], stream) context.execute_async(batch_size=1, bindings=bindings, stream_handle=stream.handle) cuda.memcpy_dtoh_async(host_outputs[1], cuda_outputs[1], stream) cuda.memcpy_dtoh_async(host_outputs[0], cuda_outputs[0], stream) stream.synchronize() watch.stop(Stopwatch.MODE_INFER) # Postprocessing watch.start() output = host_outputs[0] results = [] for prefix in range(0, len(output), 7): conf = float(output[prefix + 2]) if conf < 0.5: continue x1 = int(output[prefix + 3] * iw) y1 = int(output[prefix + 4] * ih) x2 = int(output[prefix + 5] * iw) y2 = int(output[prefix + 6] * ih) cls = int(output[prefix + 1]) results.append(((x1, y1, x2, y2), cls, conf)) if args.verbose: print(results) relay.send_results(results) watch.stop(Stopwatch.MODE_POSTPROCESS) if watch.report(): print("TCP Latency to source: ", round(measure_latency(host=args.ip, port=relay.port)[0], 3), "ms") relay.close()
def test_latency(address): return measure_latency(host=address, port=8080, runs=3, timeout=2.5)
ctr = 0 # iterations in X seconds while time.time() < end: ctr += 1 print(ctr) domainNum = 0 pings = "" while True: domain = os.getenv('DOMAIN' + str(domainNum)) if not domain: break print(domain) values = measure_latency(host=domain, port=80, runs=9) sortedValues = sorted(values) print(sortedValues) pings = pings + str(int(sortedValues[4])) + " " domainNum += 1 requestParams = { "id": AGENTID, "agent": AGENTID, "ts": TS, "cpu": ctr, "pings": pings, } requests.get(ENDPOINT, params=requestParams)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', help='Path of the detection model.', required=True, type=str) parser.add_argument('--ip', "-i", help='File path of the input image.', required=True, type=str) parser.add_argument('--report_interval', '-r', help="Duration of reporting interval, in seconds", default=10, type=int) parser.add_argument('-v', "--verbose", help="Print information about detected objects", action='store_true') args = parser.parse_args() relay = Relay(args.ip, args.verbose) engine = DetectionEngine(args.model) watch = Stopwatch() while True: if args.verbose: print("ready for next inference") img = relay.get_image() if img is None: break if args.verbose: print("Received image ", watch.numread) watch.start() initial_h, initial_w, _ = img.shape if (initial_h, initial_w) != (300, 300): frame = cv2.resize(img, (300, 300)) else: frame = img frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) watch.stop(Stopwatch.MODE_PREPROCESS) watch.start() ans = engine.detect_with_input_tensor(frame.flatten(), threshold=0.5, top_k=10) watch.stop(Stopwatch.MODE_INFER) if args.verbose: print("Got inference results for frame ", watch.numread, ": ", ans) watch.start() # Display result results = [] for obj in ans: box = obj.bounding_box.flatten().tolist() bbox = [0] * 4 bbox[0] = int(box[0] * initial_w) bbox[1] = int(box[1] * initial_h) bbox[2] = int(box[2] * initial_w) bbox[3] = int(box[3] * initial_h) result = (bbox, obj.label_id + 1, obj.score) results.append(result) relay.send_results(results) watch.stop(Stopwatch.MODE_POSTPROCESS) if watch.report(): print("TCP Latency to source: ", round(measure_latency(host=args.ip, port=relay.port)[0], 3), "ms") relay.close()
def get_latency(url: str, runs: int = 3): domain_name = url.replace("https://", "").replace("/api/v3", "") print(domain_name) latency = measure_latency(host=domain_name, runs=runs) return latency
def tcpLatency(): _ls = tcp_latency.measure_latency(host='google.com', runs=5, timeout=2.5) _latency = statistics.median(_ls) _latency = int(_latency * 100) / 100 return _latency
"youtube.com", "amazon.com", "facebook.com", "twitter.com", "pinterest.com", "imdb.com", "reddit.com", "juliopdx.com", "404", ] device_table = Table(title="TCP Latency Test") device_table.add_column("Site Name", justify="center") device_table.add_column("Results", justify="center") with Progress() as progress: task1 = progress.add_task("[green]Testing Sites...", total=len(sites)) for site in sites: test_results = measure_latency(host=site, runs=3) if None in test_results: device_table.add_row(f"[red underline]{site}[/]", f"[red]Not Found[/]") else: device_table.add_row(f"[dodger_blue3 underline]{site}[/]", f"[green]{test_results}[/]") progress.update(task1, advance=1.0) console = Console() console.print(device_table)
def ping(host, port): delay = measure_latency(host, port, 1)[0] return delay
def test_tcpLatency_negative_runs(): negative_runs = measure_latency(host='google.com', runs=-1) assert not negative_runs
import requests import time from threading import Thread import socket from tcp_latency import measure_latency import json from datetime import datetime out_url = "http://127.0.0.1:5000/pinger" test_url = "www.calorizator.ru" my_delay = 50 while True: try: out = { 'hostname': 'DESKTOP-NIFA-TEST', #socket.gethostname(), 'ping': measure_latency(host=test_url, timeout=2.5)[0], 'test_url': test_url, 'delay': my_delay, 'time': datetime.now().strftime('%d/%m/%y %H:%M:%S') } request = requests.post(out_url, json=out) result = json.loads(request.text) my_delay = result['delay'] # test_url = result['test_url'] print('sent') except Exception as e: print('failed') pass time.sleep(my_delay)
def test_tcpLatency_valid_list_return(): latency_run = measure_latency(host='google.com') assert isinstance(latency_run, list) for element in latency_run: assert isinstance(element, float) or None
def checkLatency(self): """ Check latency every periodic time. """ while not self.terminate: time.sleep(LAT_PERIOD) self.latency = mean(measure_latency(host='google.com')) print(self.latency)
def tryConnected(host, port): latencyList = measure_latency(host=host, port=port, runs=3, timeout=2.5) for latency in latencyList: if latency != None: return True return False