Update main.py
Add defaults for ENVs and NAMESPACE
This commit is contained in:
parent
61fb17ea67
commit
74ae03ba9f
1 changed files with 24 additions and 10 deletions
34
main.py
34
main.py
|
@ -4,21 +4,29 @@ import subprocess
|
||||||
import os
|
import os
|
||||||
from influxdb import InfluxDBClient
|
from influxdb import InfluxDBClient
|
||||||
|
|
||||||
|
func getEnv(key, fallback string) string {
|
||||||
|
if value, ok := os.LookupEnv(key); ok {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
# InfluxDB Settings
|
# InfluxDB Settings
|
||||||
DB_ADDRESS = os.environ.get('INFLUX_DB_ADDRESS')
|
NAMESPACE = getEnv('NAMESPACE', '')
|
||||||
DB_PORT = int(os.environ.get('INFLUX_DB_PORT'))
|
DB_ADDRESS = getEnv('INFLUX_DB_ADDRESS', 'influxdb')
|
||||||
DB_USER = os.environ.get('INFLUX_DB_USER')
|
DB_PORT = int(getEnv('INFLUX_DB_PORT', '8086'))
|
||||||
DB_PASSWORD = os.environ.get('INFLUX_DB_PASSWORD')
|
DB_USER = getEnv('INFLUX_DB_USER', '')
|
||||||
DB_DATABASE = os.environ.get('INFLUX_DB_DATABASE')
|
DB_PASSWORD = getEnv('INFLUX_DB_PASSWORD', '')
|
||||||
DB_TAGS = os.environ.get('INFLUX_DB_TAGS')
|
DB_DATABASE = getEnv('INFLUX_DB_DATABASE', 'speedtests')
|
||||||
|
DB_TAGS = getEnv('INFLUX_DB_TAGS', '')
|
||||||
|
|
||||||
# Speedtest Settings
|
# Speedtest Settings
|
||||||
# Time between tests (in minutes, converts to seconds).
|
# Time between tests (in minutes, converts to seconds).
|
||||||
TEST_INTERVAL = int(os.environ.get('SPEEDTEST_INTERVAL')) * 60
|
TEST_INTERVAL = int(getEnv('SPEEDTEST_INTERVAL', '5')) * 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(getEnv('SPEEDTEST_FAIL_INTERVAL', '5')) * 60
|
||||||
# Specific server ID
|
# Specific server ID
|
||||||
SERVER_ID = os.environ.get('SPEEDTEST_SERVER_ID')
|
SERVER_ID = getEnv('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)
|
||||||
|
@ -48,6 +56,7 @@ def tag_selection(data):
|
||||||
return None
|
return None
|
||||||
# tag_switch takes in _data and attaches CLIoutput to more readable ids
|
# tag_switch takes in _data and attaches CLIoutput to more readable ids
|
||||||
tag_switch = {
|
tag_switch = {
|
||||||
|
'namespace': NAMESPACE,
|
||||||
'isp': data['isp'],
|
'isp': data['isp'],
|
||||||
'interface': data['interface']['name'],
|
'interface': data['interface']['name'],
|
||||||
'internal_ip': data['interface']['internalIp'],
|
'internal_ip': data['interface']['internalIp'],
|
||||||
|
@ -66,6 +75,7 @@ def tag_selection(data):
|
||||||
}
|
}
|
||||||
|
|
||||||
options = {}
|
options = {}
|
||||||
|
tags = 'namespace, ' + tags
|
||||||
tags = tags.split(',')
|
tags = tags.split(',')
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
# split the tag string, strip and add selected tags to {options} with corresponding tag_switch data
|
# split the tag string, strip and add selected tags to {options} with corresponding tag_switch data
|
||||||
|
@ -82,6 +92,7 @@ def format_for_influx(data):
|
||||||
'measurement': 'ping',
|
'measurement': 'ping',
|
||||||
'time': data['timestamp'],
|
'time': data['timestamp'],
|
||||||
'fields': {
|
'fields': {
|
||||||
|
'namespace': NAMESPACE,
|
||||||
'jitter': data['ping']['jitter'],
|
'jitter': data['ping']['jitter'],
|
||||||
'latency': data['ping']['latency']
|
'latency': data['ping']['latency']
|
||||||
}
|
}
|
||||||
|
@ -90,6 +101,7 @@ def format_for_influx(data):
|
||||||
'measurement': 'download',
|
'measurement': 'download',
|
||||||
'time': data['timestamp'],
|
'time': data['timestamp'],
|
||||||
'fields': {
|
'fields': {
|
||||||
|
'namespace': NAMESPACE,
|
||||||
# Byte to Megabit
|
# Byte to Megabit
|
||||||
'bandwidth': data['download']['bandwidth'] / 125000,
|
'bandwidth': data['download']['bandwidth'] / 125000,
|
||||||
'bytes': data['download']['bytes'],
|
'bytes': data['download']['bytes'],
|
||||||
|
@ -100,6 +112,7 @@ def format_for_influx(data):
|
||||||
'measurement': 'upload',
|
'measurement': 'upload',
|
||||||
'time': data['timestamp'],
|
'time': data['timestamp'],
|
||||||
'fields': {
|
'fields': {
|
||||||
|
'namespace': NAMESPACE,
|
||||||
# Byte to Megabit
|
# Byte to Megabit
|
||||||
'bandwidth': data['upload']['bandwidth'] / 125000,
|
'bandwidth': data['upload']['bandwidth'] / 125000,
|
||||||
'bytes': data['upload']['bytes'],
|
'bytes': data['upload']['bytes'],
|
||||||
|
@ -110,6 +123,7 @@ def format_for_influx(data):
|
||||||
'measurement': 'packetLoss',
|
'measurement': 'packetLoss',
|
||||||
'time': data['timestamp'],
|
'time': data['timestamp'],
|
||||||
'fields': {
|
'fields': {
|
||||||
|
'namespace': NAMESPACE,
|
||||||
'packetLoss': pkt_loss(data)
|
'packetLoss': pkt_loss(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,4 +168,4 @@ def main():
|
||||||
|
|
||||||
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