Example #1
0
    def setup_speedtest(self, server=None):
        """
        Initializes the Speed Test client with the provided server
        :param server: Int
        :return: None
        """
        speedtest.build_user_agent()

        log.debug('Setting up SpeedTest.net client')

        if server is None:
            server = []
        else:
            server = server.split()  # Single server to list

        try:
            self.speedtest = speedtest.Speedtest()
        except speedtest.ConfigRetrievalError:
            log.critical(
                'Failed to get speedtest.net configuration.  Aborting')
            sys.exit(1)

        self.speedtest.get_servers(server)

        log.debug('Picking the closest server')

        self.speedtest.get_best_server()

        log.info('Selected Server %s in %s', self.speedtest.best['id'],
                 self.speedtest.best['name'])

        self.results = self.speedtest.results
Example #2
0
    def _get_influx_connection(self):
        """
        Create an InfluxDB connection and test to make sure it works.
        We test with the get all users command.  If the address is bad it fails
        with a 404.  If the user doesn't have permission it fails with 401
        :return:
        """

        influx = InfluxDBClient(config.influx_address,
                                config.influx_port,
                                database=config.influx_database,
                                ssl=config.influx_ssl,
                                verify_ssl=config.influx_verify_ssl,
                                username=config.influx_user,
                                password=config.influx_password,
                                timeout=5)
        try:
            log.debug(
                'Testing connection to InfluxDb using provided credentials')
            influx.get_list_users(
            )  # TODO - Find better way to test connection and permissions
            log.debug('Successful connection to InfluxDb')
        except (ConnectTimeout, InfluxDBClientError) as e:
            if isinstance(e, ConnectTimeout):
                log.critical(
                    'Unable to connect to InfluxDB at the provided address (%s)',
                    config.influx_address)
            elif e.code == 401:
                log.critical(
                    'Unable to connect to InfluxDB with provided credentials')

            sys.exit(1)

        return influx
Example #3
0
    def run_speed_test(self, server=None):
        """
        Performs the speed test with the provided server
        :param server: Server to test against
        """
        log.info('Starting Speed Test For Server %s', server)

        try:
            self.setup_speedtest(server)
        except speedtest.NoMatchedServers:
            log.error('No matched servers: %s', server)
            return
        except speedtest.ServersRetrievalError:
            log.critical('Cannot retrieve speedtest.net server list. Aborting')
            return
        except speedtest.InvalidServerIDType:
            log.error('%s is an invalid server type, must be int', server)
            return

        log.info('Starting download test')
        self.speedtest.download()
        log.info('Starting upload test')
        self.speedtest.upload()
        self.send_results()

        results = self.results.dict()
        log.info('Download: %sMbps - Upload: %sMbps - Latency: %sms',
                 round(results['download'] / 1000000, 2),
                 round(results['upload'] / 1000000, 2),
                 results['server']['latency'])
    def _get_influx_connection(self):
        """
        Create an InfluxDB connection and test to make sure it works.
        We test with the get all users command.  If the address is bad it fails
        with a 404.  If the user doesn't have permission it fails with 401
        :return:
        """
        if config.influx_version == 1:
            influx = InfluxDBClient(config.influx_address,
                                    config.influx_port,
                                    database=config.influx_database,
                                    ssl=config.influx_ssl,
                                    verify_ssl=config.influx_verify_ssl,
                                    username=config.influx_user,
                                    password=config.influx_password,
                                    timeout=5)
        elif config.influx_version == 2:
            log.debug('InfluxDB V2.0 is selected')
            protocol = "http://"
            if config.influx_ssl:
                protocol = "https://"
            else:
                protocol = "http://"
            influx = InfluxDBClient2(url=protocol + config.influx_address +
                                     ":" + str(config.influx_port),
                                     org=config.influx_org,
                                     token=config.influx_token,
                                     bucket=config.influx_bucket)
        try:
            log.debug(
                'Testing connection to InfluxDb using provided credentials')
            if config.influx_version == 1:
                influx.get_list_users(
                )  # TODO - Find better way to test connection and permissions
            else:
                influx.health()
            log.debug('Successful connection to InfluxDb')
        except (ConnectTimeout, InfluxDBClientError, ConnectionError) as e:
            if isinstance(e, ConnectTimeout):
                log.critical(
                    'Unable to connect to InfluxDB at the provided address (%s)',
                    config.influx_address)
            elif e.code == 401:
                log.critical(e)
                log.critical(
                    'Unable to connect to InfluxDB with provided credentials')
            else:
                log.critical(
                    'Failed to connect to InfluxDB for unknown reason')

            sys.exit(1)

        return influx
Example #5
0
    def setup_speedtest(self, servers=None, mode='select'):
        """
        Initializes the Speed Test client with the provided server
        :param server: Int
        :return: None
        """
        speedtest.build_user_agent()

        log.debug('Setting up Speedtest.net client')

        if servers:
            log.info(
                f"Selecting server {('excluding','from')[mode!='exclude']}: {servers}"
            )

        try:
            self.speedtest = speedtest.Speedtest(secure=config.secure)
        except speedtest.ConfigRetrievalError:
            log.critical(
                'Failed to get speedtest.net configuration.  Aborting')
            sys.exit(1)

        servers_in = None
        servers_ex = None

        if mode == 'select':
            servers_in = servers
        else:
            servers_ex = servers

        self.speedtest.get_servers(servers_in, servers_ex)

        # log.debug(self.speedtest.servers)

        if len(self.speedtest.servers) != 1:
            log.debug('Selecting the closest server')
            self.speedtest.get_best_server()

            log.info(
                f"Selected server {self.speedtest.best['name']} (id:{self.speedtest.best['id']})"
            )
Example #6
0
    def run_speed_test(self, servers=None, share=False, mode='select'):
        """
        Performs the speed test with the provided server
        :param server: Server to test against
        """
        log.info('Starting Speedtest')
        #ensure previous results are removed
        self.speedtest = None

        try:
            self.setup_speedtest(servers, mode)
        except speedtest.NoMatchedServers:
            log.error(f'No servers matched: {servers}')
            return
        except speedtest.ServersRetrievalError:
            log.critical('Cannot retrieve speedtest.net server list. Aborting')
            return
        except speedtest.InvalidServerIDType:
            log.error(f'{servers} is an invalid server type, must be int')
            return

        log.info('Starting download test')
        self.speedtest.download()
        log.info('Starting upload test')
        self.speedtest.upload()

        if (share):
            self.speedtest.results.share()

        self.send_results()

        results = self.speedtest.results.dict()
        log.info(
            'Download: %sMbps - Upload: %sMbps - Latency: %sms - Share: %s',
            round(results['download'] / 1000000, 2),
            round(results['upload'] / 1000000, 2), results['ping'],
            results['share'])