- 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()
 | 
			
		||||
 | 
			
		||||
class Entry(BroModel):
 | 
			
		||||
    upload = FloatField()
 | 
			
		||||
    download = FloatField()
 | 
			
		||||
    upload = FloatField(null=True)
 | 
			
		||||
    download = FloatField(null=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
results_dict = None
 | 
			
		||||
test_started = False
 | 
			
		||||
speedtest_failed = False
 | 
			
		||||
downloading = True  # True for download, False for upload
 | 
			
		||||
threads = None
 | 
			
		||||
servers = []
 | 
			
		||||
| 
						 | 
				
			
			@ -129,21 +130,26 @@ servers = []
 | 
			
		|||
def threaded_speedtest():
 | 
			
		||||
    global test_started
 | 
			
		||||
    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
 | 
			
		||||
    results_dict = s.results.dict()
 | 
			
		||||
    log.info(
 | 
			
		||||
        f"Speedtest.net result: DOWN {mbits(results_dict['download'])} mbps; UP {mbits(results_dict['upload'])} mbps;")
 | 
			
		||||
    global speedtest_failed
 | 
			
		||||
    try:
 | 
			
		||||
        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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -153,13 +159,15 @@ def threaded_wan_speed():
 | 
			
		|||
    global downloading
 | 
			
		||||
    print("Waiting for test to start...")
 | 
			
		||||
    while not test_started:
 | 
			
		||||
        if speedtest_failed:
 | 
			
		||||
            return
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
    print("Allow warm-up...")
 | 
			
		||||
    time.sleep(2)
 | 
			
		||||
    uploads = []
 | 
			
		||||
    downloads = []
 | 
			
		||||
    print("Monitoring...")
 | 
			
		||||
    while not results_dict:
 | 
			
		||||
    while results_dict == None:
 | 
			
		||||
        connection = routeros_api.RouterOsApiPool(config['ros_ip'], username=secrets["ros_login"],
 | 
			
		||||
                                                  password=secrets["ros_password"], plaintext_login=True)
 | 
			
		||||
        api = connection.get_api()
 | 
			
		||||
| 
						 | 
				
			
			@ -194,6 +202,31 @@ def test_speed():
 | 
			
		|||
    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__":
 | 
			
		||||
    '''
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        log.debug("Test internet connection...")
 | 
			
		||||
        # if not test_intertnet_connection():
 | 
			
		||||
        #     no_internet()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        if config["ros_dynamic_speed"]:
 | 
			
		||||
            ros_fastrack_enable(True)
 | 
			
		||||
            time.sleep(5)
 | 
			
		||||
 | 
			
		||||
        test_speed()
 | 
			
		||||
 | 
			
		||||
        if speedtest_failed:
 | 
			
		||||
            on_fail_or_no_connection()
 | 
			
		||||
 | 
			
		||||
        entry = Entry()
 | 
			
		||||
        entry.upload = mbits(wan_upload)
 | 
			
		||||
        entry.download = mbits(wan_download)
 | 
			
		||||
| 
						 | 
				
			
			@ -224,11 +266,7 @@ if __name__ == "__main__":
 | 
			
		|||
            ros_dynamic_speed(wan_upload, wan_download)
 | 
			
		||||
            ros_fastrack_enable(False)
 | 
			
		||||
 | 
			
		||||
        dates, downloads, uploads = gather_data()
 | 
			
		||||
 | 
			
		||||
        generate_txt_output(dates, downloads, uploads)
 | 
			
		||||
 | 
			
		||||
        generate_plot_image(dates, downloads, uploads)
 | 
			
		||||
        generate_database_reports()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    except:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue