created tag_selection function

This commit is contained in:
Josh Smith 2020-07-30 23:45:23 -04:00
parent 770a4c8b75
commit 3a392f2084

43
main.py
View file

@ -10,10 +10,13 @@ DB_PORT = int(os.environ.get('INFLUX_DB_PORT'))
DB_USER = os.environ.get('INFLUX_DB_USER') DB_USER = os.environ.get('INFLUX_DB_USER')
DB_PASSWORD = os.environ.get('INFLUX_DB_PASSWORD') DB_PASSWORD = os.environ.get('INFLUX_DB_PASSWORD')
DB_DATABASE = os.environ.get('INFLUX_DB_DATABASE') DB_DATABASE = os.environ.get('INFLUX_DB_DATABASE')
DB_TAGS = os.environ.get('INFLUX_DB_TAGS')
# Speedtest Settings # Speedtest Settings
TEST_INTERVAL = int(os.environ.get('SPEEDTEST_INTERVAL')) # Time between tests (in seconds). # Time between tests (in seconds).
TEST_FAIL_INTERVAL = int(os.environ.get('SPEEDTEST_FAIL_INTERVAL')) # Time before retrying a failed Speedtest (in seconds). TEST_INTERVAL = int(os.environ.get('SPEEDTEST_INTERVAL'))
# Time before retrying a failed Speedtest (in seconds).
TEST_FAIL_INTERVAL = int(os.environ.get('SPEEDTEST_FAIL_INTERVAL'))
influxdb_client = InfluxDBClient( influxdb_client = InfluxDBClient(
DB_ADDRESS, DB_PORT, DB_USER, DB_PASSWORD, None) DB_ADDRESS, DB_PORT, DB_USER, DB_PASSWORD, None)
@ -26,13 +29,47 @@ def init_db():
influxdb_client.create_database( influxdb_client.create_database(
DB_DATABASE) # Create if does not exist. DB_DATABASE) # Create if does not exist.
else: else:
influxdb_client.switch_database(DB_DATABASE) # Switch to if does exist. # Switch to if does exist.
influxdb_client.switch_database(DB_DATABASE)
def pkt_loss(data): def pkt_loss(data):
if 'packetLoss' in data.keys(): if 'packetLoss' in data.keys():
return data['packetLoss'] return data['packetLoss']
else: else:
return 0 return 0
def tag_selection(data):
tags = DB_TAGS
tag_switch = {
'isp': data['isp'],
'interface': data['interface']['name'],
'internal_ip': data['interface']['internalIp'],
'interface_mac': data['interface']['macAddr'],
'vpn_enabled': (False if data['interface']['isVpn'] == 'false' else True),
'external_ip': data['interface']['externalIp'],
'server_id': data['server']['id'],
'server_name': data['server']['name'],
'server_location': data['server']['location'],
'server_country': data['server']['country'],
'server_host': data['server']['host'],
'server_port': data['server']['port'],
'server_ip': data['server']['ip'],
'speedtest_id': data['result']['id'],
'speedtest_url': data['result']['url']
}
options = {}
if tags == '':
return None
else:
tags = tags.split(',')
for tag in tags:
tag = tag.strip()
options[tag] = tag_switch[tag]
return options
def format_for_influx(cliout): def format_for_influx(cliout):
data = json.loads(cliout) 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.