コード例 #1
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 write_influx_data(self, json_data):
        """
        Writes the provided JSON to the database
        :param json_data:
        :return: None
        """
        log.debug(json_data)

        try:
            if config.influx_version == 1:
                self.influx_client.write_points(json_data)
            else:
                write_api = self.influx_client.write_api(
                    write_options=SYNCHRONOUS)
                write_api.write(config.influx_bucket, config.influx_org,
                                json_data)
        except (InfluxDBClientError, ConnectionError,
                InfluxDBServerError) as e:
            if hasattr(e, 'code') and e.code == 404:
                log.error('Database %s Does Not Exist.  Attempting To Create',
                          config.influx_database)
                self.influx_client.create_database(config.influx_database)
                self.influx_client.write_points(json_data)
                return

            log.error('Failed To Write To InfluxDB')
            print(e)

        log.debug('Data written to InfluxDB')
コード例 #3
0
    def send_to_ifttt(self, json_data):
        """
        Writes the provided JSON to ifttt
        :param json_data:
        :return: Non
        """
        try:
            post(
                f'https://maker.ifttt.com/trigger/speedtest/with/key/{config.ifttt_key}',
                json=json_data,
                timeout=5)
        except RequestException as e:
            log.error('Failed To Write To IFTTT')
            print(e)

        log.debug('Data written to IFTTT')
コード例 #4
0
    def write_influx_data(self, json_data):
        """
        Writes the provided JSON to the database
        :param json_data:
        :return: None
        """
        log.debug(json_data)

        try:
            self.influx_client.write_points(json_data)
        except (InfluxDBClientError, ConnectionError,
                InfluxDBServerError) as e:
            if hasattr(e, 'code') and e.code == 404:
                log.error('Database %s Does Not Exist.  Attempting To Create',
                          self.config_influx_database)
                self.influx_client.create_database(self.config_influx_database)
                self.influx_client.write_points(json_data)
                return

            log.error('Failed To Write To InfluxDB')
            print(e)

        log.debug('Data written to InfluxDB')
コード例 #5
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, 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(
                    'Unable to connect to InfluxDB with provided credentials')
            else:
                log.critical(
                    'Failed to connect to InfluxDB for unknown reason')
                log.error(e)

            sys.exit(1)

        return influx
コード例 #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'])