Merge pull request #1 from Sky007FR/master

PR Sky007FR changes
This commit is contained in:
Leigh Phillips 2021-01-16 12:47:24 -08:00 committed by GitHub
commit b97dbb2478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 14 deletions

View File

@ -1,6 +1,6 @@
FROM python:3.8-slim-buster FROM python:3.8-slim-buster
LABEL maintainer="Josh Smith" \ LABEL maintainer="Luc VOISE" \
description="Original by Aiden Gilmartin. Speedtest to InfluxDB data bridge" description="Original by Aiden Gilmartin. Speedtest to InfluxDB data bridge"
# Install dependencies # Install dependencies

View File

@ -23,6 +23,7 @@ The variables available are:
- INFLUX_DB_TAGS = *comma seperated list of tags. See below for options* - INFLUX_DB_TAGS = *comma seperated list of tags. See below for options*
- SPEEDTEST_INTERVAL = 60 - SPEEDTEST_INTERVAL = 60
- SPEEDTEST_FAIL_INTERVAL = 5 - SPEEDTEST_FAIL_INTERVAL = 5
- SPEEDTEST_SERVER_ID = *id from https://c.speedtest.net/speedtest-servers-static.php*
### Variable Notes ### Variable Notes
- Intervals are in minutes. *Script will convert it to seconds.* - Intervals are in minutes. *Script will convert it to seconds.*
@ -59,7 +60,7 @@ Be aware that this script will automatically accept the license and GDPR stateme
1. Build the container. 1. Build the container.
`docker build -t breadlysm/speedtest-to-influxdb ./` `docker build -t sky007fr/speedtest-to-influxdb ./`
2. Run the container. 2. Run the container.
``` ```
@ -71,13 +72,14 @@ Be aware that this script will automatically accept the license and GDPR stateme
-e 'INFLUX_DB_DATABASE'='speedtest' \ -e 'INFLUX_DB_DATABASE'='speedtest' \
-e 'SPEEDTEST_INTERVAL'='1800' \ -e 'SPEEDTEST_INTERVAL'='1800' \
-e 'SPEEDTEST_FAIL_INTERVAL'='60' \ -e 'SPEEDTEST_FAIL_INTERVAL'='60' \
breadlysm/speedtest-to-influxdb -e 'SPEEDTEST_SERVER_ID'='12746' \
sky007fr/speedtest-to-influxdb
``` ```
### No Container ### No Container
1. Clone the repo 1. Clone the repo
`git clone https://github.com/breadlysm/speedtest-to-influxdb.git` `git clone https://github.com/sky007fr/speedtest-to-influxdb.git`
2. Configure the .env file in the repo or set the environment variables on your device. 2. Configure the .env file in the repo or set the environment variables on your device.
@ -95,4 +97,4 @@ Be aware that this script will automatically accept the license and GDPR stateme
This script looks to have been originally written by https://github.com/aidengilmartin/speedtest-to-influxdb/blob/master/main.py and I forked it from https://github.com/martinfrancois/speedtest-to-influxdb. They did the hard work, I've continued to modify it though to fit my needs. This script looks to have been originally written by https://github.com/aidengilmartin/speedtest-to-influxdb/blob/master/main.py and I forked it from https://github.com/breadlysm/speedtest-to-influxdb. They did the hard work, I've continued to modify it though to fit my needs.

29
main.py
View File

@ -17,6 +17,8 @@ DB_TAGS = os.environ.get('INFLUX_DB_TAGS')
TEST_INTERVAL = int(os.environ.get('SPEEDTEST_INTERVAL')) * 60 TEST_INTERVAL = int(os.environ.get('SPEEDTEST_INTERVAL')) * 60
# Time before retrying a failed Speedtest (in minutes, converts to seconds). # Time before retrying a failed Speedtest (in minutes, converts to seconds).
TEST_FAIL_INTERVAL = int(os.environ.get('SPEEDTEST_FAIL_INTERVAL')) * 60 TEST_FAIL_INTERVAL = int(os.environ.get('SPEEDTEST_FAIL_INTERVAL')) * 60
# Specific server ID
SERVER_ID = os.environ.get('SPEEDTEST_SERVER_ID')
influxdb_client = InfluxDBClient( influxdb_client = InfluxDBClient(
DB_ADDRESS, DB_PORT, DB_USER, DB_PASSWORD, None) DB_ADDRESS, DB_PORT, DB_USER, DB_PASSWORD, None)
@ -72,8 +74,8 @@ def tag_selection(data):
return options return options
def format_for_influx(cliout): def format_for_influx(data):
data = json.loads(cliout)
# There is additional data in the speedtest-cli output but it is likely not necessary to store. # There is additional data in the speedtest-cli output but it is likely not necessary to store.
influx_data = [ influx_data = [
{ {
@ -125,22 +127,31 @@ def main():
init_db() # Setup the database if it does not already exist. init_db() # Setup the database if it does not already exist.
while (1): # Run a Speedtest and send the results to influxDB indefinitely. while (1): # Run a Speedtest and send the results to influxDB indefinitely.
speedtest = subprocess.run( server_id = SERVER_ID
if not server_id:
speedtest = subprocess.run(
["speedtest", "--accept-license", "--accept-gdpr", "-f", "json"], capture_output=True) ["speedtest", "--accept-license", "--accept-gdpr", "-f", "json"], capture_output=True)
print("Automatic server choice")
else:
speedtest = subprocess.run(
["speedtest", "--accept-license", "--accept-gdpr", "-f", "json", "--server-id=" + SERVER_ID], capture_output=True)
print("Manual server choice : ID = " + SERVER_ID)
if speedtest.returncode == 0: # Speedtest was successful. if speedtest.returncode == 0: # Speedtest was successful.
data = format_for_influx(speedtest.stdout) print("Speedtest Successful :")
print("Speedtest Successful:") data_json = json.loads(speedtest.stdout)
print("time: " + str(data_json['timestamp']) + " - ping: " + str(data_json['ping']['latency']) + " ms - download: " + str(data_json['download']['bandwidth']/125000) + " Mb/s - upload: " + str(data_json['upload']['bandwidth'] / 125000) + " Mb/s - isp: " + data_json['isp'] + " - ext. IP: " + data_json['interface']['externalIp'] + " - server id: " + str(data_json['server']['id']) + " (" + data_json['server']['name'] + " @ " + data_json['server']['location'] + ")")
data = format_for_influx(data_json)
if influxdb_client.write_points(data) == True: if influxdb_client.write_points(data) == True:
print("Data written to DB successfully") print("Data written to DB successfully")
time.sleep(TEST_INTERVAL) time.sleep(TEST_INTERVAL)
else: # Speedtest failed. else: # Speedtest failed.
print("Speedtest Failed:") print("Speedtest Failed :")
print(speedtest.stderr) print(speedtest.stderr)
print(speedtest.stdout) print(speedtest.stdout)
time.sleep(TEST_FAIL_INTERVAL) time.sleep(TEST_FAIL_INTERVAL)
if __name__ == '__main__': if __name__ == '__main__':
print('Speedtest CLI Data Logger to InfluxDB') print('Speedtest CLI data logger to InfluxDB started...')
main() main()