Update main.py
Add uptime ping test (separate interval), unify speed tests under new measurement.
This commit is contained in:
parent
3831683c12
commit
2d27de83ae
1 changed files with 88 additions and 32 deletions
80
main.py
80
main.py
|
@ -1,7 +1,9 @@
|
||||||
|
import os
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
|
import datetime
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
from pythonping import ping
|
||||||
from influxdb import InfluxDBClient
|
from influxdb import InfluxDBClient
|
||||||
|
|
||||||
# InfluxDB Settings
|
# InfluxDB Settings
|
||||||
|
@ -12,6 +14,7 @@ DB_USER = os.getenv('INFLUX_DB_USER', '')
|
||||||
DB_PASSWORD = os.getenv('INFLUX_DB_PASSWORD', '')
|
DB_PASSWORD = os.getenv('INFLUX_DB_PASSWORD', '')
|
||||||
DB_DATABASE = os.getenv('INFLUX_DB_DATABASE', 'speedtests')
|
DB_DATABASE = os.getenv('INFLUX_DB_DATABASE', 'speedtests')
|
||||||
DB_TAGS = os.getenv('INFLUX_DB_TAGS', None)
|
DB_TAGS = os.getenv('INFLUX_DB_TAGS', None)
|
||||||
|
PING_TARGETS = os.getenv('PING_TARGETS', '1.1.1.1, 8.8.8.8')
|
||||||
|
|
||||||
# Speedtest Settings
|
# Speedtest Settings
|
||||||
# Time between tests (in minutes, converts to seconds).
|
# Time between tests (in minutes, converts to seconds).
|
||||||
|
@ -20,6 +23,8 @@ TEST_INTERVAL = int(os.getenv('SPEEDTEST_INTERVAL', '5')) * 60
|
||||||
TEST_FAIL_INTERVAL = int(os.getenv('SPEEDTEST_FAIL_INTERVAL', '5')) * 60
|
TEST_FAIL_INTERVAL = int(os.getenv('SPEEDTEST_FAIL_INTERVAL', '5')) * 60
|
||||||
# Specific server ID
|
# Specific server ID
|
||||||
SERVER_ID = os.getenv('SPEEDTEST_SERVER_ID', '')
|
SERVER_ID = os.getenv('SPEEDTEST_SERVER_ID', '')
|
||||||
|
# Time between ping tests (in seconds).
|
||||||
|
PING_INTERVAL = int(os.getenv('PING_INTERVAL', '5'))
|
||||||
|
|
||||||
influxdb_client = InfluxDBClient(
|
influxdb_client = InfluxDBClient(
|
||||||
DB_ADDRESS, DB_PORT, DB_USER, DB_PASSWORD, None)
|
DB_ADDRESS, DB_PORT, DB_USER, DB_PASSWORD, None)
|
||||||
|
@ -121,23 +126,35 @@ def format_for_influx(data):
|
||||||
'fields': {
|
'fields': {
|
||||||
'packetLoss': pkt_loss(data)
|
'packetLoss': pkt_loss(data)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'measurement': 'speeds',
|
||||||
|
'time': data['timestamp'],
|
||||||
|
'fields': {
|
||||||
|
'jitter': data['ping']['jitter'],
|
||||||
|
'latency': data['ping']['latency'],
|
||||||
|
'packetLoss': pkt_loss(data),
|
||||||
|
# Byte to Megabit
|
||||||
|
'bandwidth_down': data['download']['bandwidth'] / 125000,
|
||||||
|
'bytes_down': data['download']['bytes'],
|
||||||
|
'elapsed_down': data['download']['elapsed'],
|
||||||
|
# Byte to Megabit
|
||||||
|
'bandwidth_up': data['upload']['bandwidth'] / 125000,
|
||||||
|
'bytes_up': data['upload']['bytes'],
|
||||||
|
'elapsed_up': data['upload']['elapsed']
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
tags = tag_selection(data)
|
tags = tag_selection(data)
|
||||||
if tags is None:
|
if tags is not None:
|
||||||
return influx_data
|
|
||||||
else:
|
|
||||||
for measurement in influx_data:
|
for measurement in influx_data:
|
||||||
measurement['tags'] = tags
|
measurement['tags'] = tags
|
||||||
|
|
||||||
return influx_data
|
return influx_data
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def speedtest():
|
||||||
init_db() # Setup the database if it does not already exist.
|
if not SERVER_ID:
|
||||||
|
|
||||||
while (1): # Run a Speedtest and send the results to influxDB indefinitely.
|
|
||||||
server_id = SERVER_ID
|
|
||||||
if not server_id:
|
|
||||||
speedtest = subprocess.run(
|
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")
|
print("Automatic server choice")
|
||||||
|
@ -153,14 +170,53 @@ def main():
|
||||||
data = format_for_influx(data_json)
|
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)
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
|
def pingtest():
|
||||||
|
timestamp = datetime.datetime.utcnow()
|
||||||
|
for target in PING_TARGETS.split(','):
|
||||||
|
target = target.strip()
|
||||||
|
pingtest = ping(target, verbose=False, timeout=1, count=1, size=128)
|
||||||
|
data = [
|
||||||
|
{
|
||||||
|
'measurement': 'pings',
|
||||||
|
'time': timestamp,
|
||||||
|
'tags': {
|
||||||
|
'target' : target
|
||||||
|
},
|
||||||
|
'fields': {
|
||||||
|
'success' : pingtest.success(),
|
||||||
|
'rtt': pingtest.rtt_avg_ms
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
if influxdb_client.write_points(data) == True:
|
||||||
|
print("Ping data written to DB successfully")
|
||||||
|
else: # Speedtest failed.
|
||||||
|
print("Ping Failed.")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
init_db() # Setup the database if it does not already exist.
|
||||||
|
|
||||||
|
loopcount = 1
|
||||||
|
while (1): # Run a Speedtest and send the results to influxDB indefinitely.
|
||||||
|
if loopcount % PING_INTERVAL == 0:
|
||||||
|
pingtest()
|
||||||
|
|
||||||
|
if loopcount % TEST_INTERVAL == 0:
|
||||||
|
speedtest()
|
||||||
|
|
||||||
|
if loopcount % ( PING_INTERVAL * TEST_INTERVAL ) == 0:
|
||||||
|
loopcount = 0
|
||||||
|
|
||||||
|
time.sleep(1)
|
||||||
|
loopcount += 1
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print('Speedtest CLI data logger to InfluxDB started...')
|
print('Speedtest CLI data logger to InfluxDB started...')
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue