- Test for internet
- Improve handling of no internet - Allow null values - Add netutilsmaster
parent
99b16b2ae3
commit
c33c637fd0
4
dbo.py
4
dbo.py
|
|
@ -43,8 +43,8 @@ class BroModel(Model):
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
class Entry(BroModel):
|
class Entry(BroModel):
|
||||||
upload = FloatField()
|
upload = FloatField(null=True)
|
||||||
download = FloatField()
|
download = FloatField(null=True)
|
||||||
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
from platform import system as system_name # Returns the system/OS name
|
||||||
|
from os import system as system_call # Execute a shell command
|
||||||
|
import time
|
||||||
|
|
||||||
|
test_ips = ['duckduckgo.com',
|
||||||
|
'cloudflare.com',
|
||||||
|
'google.com',
|
||||||
|
'ya.ru']
|
||||||
|
|
||||||
|
|
||||||
|
try_delay = 0.5
|
||||||
|
|
||||||
|
def ping(host):
|
||||||
|
"""
|
||||||
|
Returns True if host (str) responds to a ping request.
|
||||||
|
Remember that some hosts may not respond to a ping request even if the host name is valid.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Ping parameters as function of OS
|
||||||
|
parameters = "-n 1 -w 1000" if system_name().lower().strip() == "windows" else "-c 1 -W 1000"
|
||||||
|
|
||||||
|
# Pinging
|
||||||
|
return system_call("ping " + parameters + " " + host) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def wait_for_internet_connection():
|
||||||
|
while True:
|
||||||
|
for ip in test_ips:
|
||||||
|
if ping(ip):
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
print("Could not reach internet. Trying again...")
|
||||||
|
time.sleep(try_delay)
|
||||||
|
|
||||||
|
|
||||||
|
def test_intertnet_connection():
|
||||||
|
for ip in test_ips:
|
||||||
|
if ping(ip):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print("Could not reach internet. Trying another IP...")
|
||||||
|
time.sleep(try_delay)
|
||||||
|
return False
|
||||||
|
|
||||||
|
#########################################################
|
||||||
|
#########################################################
|
||||||
|
#########################################################
|
||||||
BIN
speedgraph.png
BIN
speedgraph.png
Binary file not shown.
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 13 KiB |
|
|
@ -121,6 +121,7 @@ wan_upload = None
|
||||||
wan_download = None
|
wan_download = None
|
||||||
results_dict = None
|
results_dict = None
|
||||||
test_started = False
|
test_started = False
|
||||||
|
speedtest_failed = False
|
||||||
downloading = True # True for download, False for upload
|
downloading = True # True for download, False for upload
|
||||||
threads = None
|
threads = None
|
||||||
servers = []
|
servers = []
|
||||||
|
|
@ -129,21 +130,26 @@ servers = []
|
||||||
def threaded_speedtest():
|
def threaded_speedtest():
|
||||||
global test_started
|
global test_started
|
||||||
global downloading
|
global downloading
|
||||||
|
|
||||||
s = speedtest.Speedtest()
|
|
||||||
s.get_servers(servers)
|
|
||||||
s.get_best_server()
|
|
||||||
print(f"Running test...")
|
|
||||||
test_started = True
|
|
||||||
downloading = True
|
|
||||||
s.download(threads=threads)
|
|
||||||
downloading = False
|
|
||||||
s.upload(threads=threads, pre_allocate=False)
|
|
||||||
|
|
||||||
global results_dict
|
global results_dict
|
||||||
results_dict = s.results.dict()
|
global speedtest_failed
|
||||||
log.info(
|
try:
|
||||||
f"Speedtest.net result: DOWN {mbits(results_dict['download'])} mbps; UP {mbits(results_dict['upload'])} mbps;")
|
s = speedtest.Speedtest()
|
||||||
|
s.get_servers(servers)
|
||||||
|
s.get_best_server()
|
||||||
|
print(f"Running test...")
|
||||||
|
test_started = True
|
||||||
|
downloading = True
|
||||||
|
s.download(threads=threads)
|
||||||
|
downloading = False
|
||||||
|
s.upload(threads=threads, pre_allocate=False)
|
||||||
|
|
||||||
|
|
||||||
|
results_dict = s.results.dict()
|
||||||
|
log.info(
|
||||||
|
f"Speedtest.net result: DOWN {mbits(results_dict['download'])} mbps; UP {mbits(results_dict['upload'])} mbps;")
|
||||||
|
except Exception as e:
|
||||||
|
speedtest_failed = True
|
||||||
|
log.error(f"Speedtest.net failed: {e}", exc_info=True)
|
||||||
return results_dict
|
return results_dict
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -153,13 +159,15 @@ def threaded_wan_speed():
|
||||||
global downloading
|
global downloading
|
||||||
print("Waiting for test to start...")
|
print("Waiting for test to start...")
|
||||||
while not test_started:
|
while not test_started:
|
||||||
|
if speedtest_failed:
|
||||||
|
return
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
print("Allow warm-up...")
|
print("Allow warm-up...")
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
uploads = []
|
uploads = []
|
||||||
downloads = []
|
downloads = []
|
||||||
print("Monitoring...")
|
print("Monitoring...")
|
||||||
while not results_dict:
|
while results_dict == None:
|
||||||
connection = routeros_api.RouterOsApiPool(config['ros_ip'], username=secrets["ros_login"],
|
connection = routeros_api.RouterOsApiPool(config['ros_ip'], username=secrets["ros_login"],
|
||||||
password=secrets["ros_password"], plaintext_login=True)
|
password=secrets["ros_password"], plaintext_login=True)
|
||||||
api = connection.get_api()
|
api = connection.get_api()
|
||||||
|
|
@ -194,6 +202,31 @@ def test_speed():
|
||||||
sws.join()
|
sws.join()
|
||||||
|
|
||||||
|
|
||||||
|
def generate_database_reports():
|
||||||
|
dates, downloads, uploads = gather_data()
|
||||||
|
|
||||||
|
generate_txt_output(dates, downloads, uploads)
|
||||||
|
|
||||||
|
generate_plot_image(dates, downloads, uploads)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from netutils import test_intertnet_connection
|
||||||
|
|
||||||
|
|
||||||
|
def on_fail_or_no_connection():
|
||||||
|
|
||||||
|
ros_fastrack_enable(False)
|
||||||
|
|
||||||
|
entry = Entry()
|
||||||
|
entry.upload = None
|
||||||
|
entry.download = None
|
||||||
|
entry.save()
|
||||||
|
|
||||||
|
generate_database_reports()
|
||||||
|
|
||||||
|
log.warning("No internet connection! Exiting.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
'''
|
'''
|
||||||
This script will run a few speed tests, calculate average upload and download speeds and record them into database.
|
This script will run a few speed tests, calculate average upload and download speeds and record them into database.
|
||||||
|
|
@ -206,12 +239,21 @@ if __name__ == "__main__":
|
||||||
from random import uniform
|
from random import uniform
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
log.debug("Test internet connection...")
|
||||||
|
# if not test_intertnet_connection():
|
||||||
|
# no_internet()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if config["ros_dynamic_speed"]:
|
if config["ros_dynamic_speed"]:
|
||||||
ros_fastrack_enable(True)
|
ros_fastrack_enable(True)
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
test_speed()
|
test_speed()
|
||||||
|
|
||||||
|
if speedtest_failed:
|
||||||
|
on_fail_or_no_connection()
|
||||||
|
|
||||||
entry = Entry()
|
entry = Entry()
|
||||||
entry.upload = mbits(wan_upload)
|
entry.upload = mbits(wan_upload)
|
||||||
entry.download = mbits(wan_download)
|
entry.download = mbits(wan_download)
|
||||||
|
|
@ -224,11 +266,7 @@ if __name__ == "__main__":
|
||||||
ros_dynamic_speed(wan_upload, wan_download)
|
ros_dynamic_speed(wan_upload, wan_download)
|
||||||
ros_fastrack_enable(False)
|
ros_fastrack_enable(False)
|
||||||
|
|
||||||
dates, downloads, uploads = gather_data()
|
generate_database_reports()
|
||||||
|
|
||||||
generate_txt_output(dates, downloads, uploads)
|
|
||||||
|
|
||||||
generate_plot_image(dates, downloads, uploads)
|
|
||||||
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue