-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from breadlysm/breadlysm/issue12
Breadlysm/issue12
- Loading branch information
Showing
8 changed files
with
167 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,40 @@ | ||
import time | ||
|
||
from speedflux.config import get_config | ||
from speedflux.influx import Influx | ||
import speedflux | ||
from multiprocessing import Process | ||
from speedflux.data import speedtest, pingtest | ||
from speedflux.logs import log | ||
from speedflux import data | ||
|
||
|
||
def main(): | ||
config = get_config() | ||
influx = Influx(config) | ||
config['influx'] = influx | ||
pPing = Process(target=pingtest, args=(config,)) | ||
pSpeed = Process(target=speedtest, args=(config,)) | ||
|
||
speedflux.initialize() | ||
speedflux.LOG.info('Speedtest CLI data logger to InfluxDB started...') | ||
pPing = Process(target=data.pingtest, args=()) | ||
pSpeed = Process(target=data.speedtest, args=()) | ||
speedtest_interval = speedflux.CONFIG.SPEEDTEST_INTERVAL * 60 | ||
ping_interval = speedflux.CONFIG.PING_INTERVAL | ||
loopcount = 0 | ||
while (1): # Run a Speedtest and send the results to influxDB | ||
if loopcount == 0 or loopcount % config['ping_interval'] == 0: | ||
if pPing.is_alive(): | ||
pPing.terminate() | ||
pPing = Process(target=pingtest, args=(config,)) | ||
pPing.start() | ||
|
||
if loopcount == 0 or loopcount % config['test_interval'] == 0: | ||
if ping_interval != 0: | ||
if loopcount == 0 or loopcount % ping_interval == 0: | ||
if pPing.is_alive(): | ||
pPing.terminate() | ||
pPing = Process(target=data.pingtest, args=()) | ||
pPing.start() | ||
|
||
if loopcount == 0 or loopcount % speedtest_interval == 0: | ||
if pSpeed.is_alive(): | ||
pSpeed.terminate() | ||
pSpeed = Process(target=speedtest, args=(config,)) | ||
pSpeed = Process(target=data.speedtest, args=()) | ||
pSpeed.start() | ||
|
||
if loopcount % ( | ||
config['ping_interval'] * config['test_interval']) == 0: | ||
loopcount = 0 | ||
|
||
if ping_interval != 0: | ||
if loopcount % (ping_interval * speedtest_interval) == 0: | ||
loopcount = 0 | ||
else: | ||
if loopcount == speedtest_interval: | ||
loopcount = 0 | ||
time.sleep(1) | ||
loopcount += 1 | ||
|
||
|
||
if __name__ == '__main__': | ||
log.info('Speedtest CLI data logger to InfluxDB started...') | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from speedflux import config, logs, influx | ||
|
||
# Speedflux | ||
CONFIG = None | ||
DB_TYPE = None | ||
LOG = None | ||
INFLUXDB = None | ||
|
||
|
||
def initialize(): | ||
global CONFIG | ||
global LOG | ||
global INFLUXDB | ||
|
||
try: | ||
CONFIG = config.Config() | ||
except Exception as err: | ||
raise SystemExit("Unable to initialize SpeedFlux", err) | ||
try: | ||
LOG = logs.Log(CONFIG) | ||
except Exception as err: | ||
raise SystemExit("Couldn't initiate logging", err) | ||
try: | ||
INFLUXDB = influx.Influx(CONFIG) | ||
except Exception as err: | ||
raise SystemExit("Couldn't initiate InfluxDB <2", err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,48 @@ | ||
import os | ||
import re | ||
|
||
# import speedflux | ||
|
||
def get_config(): | ||
NAMESPACE = os.getenv('NAMESPACE', 'None') | ||
DB_ADDRESS = os.getenv('INFLUX_DB_ADDRESS', 'influxdb') | ||
DB_PORT = int(os.getenv('INFLUX_DB_PORT', '8086')) | ||
DB_USER = os.getenv('INFLUX_DB_USER', '') | ||
DB_PASSWORD = os.getenv('INFLUX_DB_PASSWORD', '') | ||
DB_DATABASE = os.getenv('INFLUX_DB_DATABASE', 'speedtests') | ||
DB_TAGS = os.getenv('INFLUX_DB_TAGS', None) | ||
PING_TARGETS = os.getenv('PING_TARGETS', '1.1.1.1, 8.8.8.8') | ||
# Speedtest Settings | ||
# Time between tests (in minutes, converts to seconds). | ||
TEST_INTERVAL = int(os.getenv('SPEEDTEST_INTERVAL', '180')) * 60 | ||
# Specific server ID | ||
SERVER_ID = os.getenv('SPEEDTEST_SERVER_ID', '') | ||
# Time between ping tests (in seconds). | ||
PING_INTERVAL = int(os.getenv('PING_INTERVAL', '120')) | ||
LOG_TYPE = os.getenv('LOG_TYPE', 'info') | ||
config = { | ||
'namespace': NAMESPACE, | ||
'db_host': DB_ADDRESS, | ||
'db_port': DB_PORT, | ||
'db_user': DB_USER, | ||
'db_pass': DB_PASSWORD, | ||
'db_tags': DB_TAGS, | ||
'db_name': DB_DATABASE, | ||
'ping_targets': PING_TARGETS, | ||
'test_interval': TEST_INTERVAL, | ||
'ping_interval': PING_INTERVAL, | ||
'server_id': SERVER_ID, | ||
'log_level': LOG_TYPE | ||
} | ||
return config | ||
|
||
_CONFIG_DEFAULTS = { | ||
'NAMESPACE': (str, 'Database', None), | ||
'INFLUX_DB_ADDRESS': (str, 'Database', 'influxdb'), | ||
'INFLUX_DB_PORT': (int, 'Database', 8086), | ||
'INFLUX_DB_USER': (str, 'Database', None), | ||
'INFLUX_DB_PASSWORD': (str, 'Database', None), | ||
'INFLUX_DB_DATABASE': (str, 'Database', 'speedtests'), | ||
'INFLUX_DB_TAGS': (str, 'Database', None), | ||
'SPEEDTEST_INTERVAL': (int, 'SpeedTest', 180), | ||
'SPEEDTEST_SERVER_ID': (str, 'SpeedTest', None), | ||
'PING_TARGETS': (str, 'PingTest', '1.1.1.1, 8.8.8.8'), | ||
'PING_INTERVAL': (int, 'PingTest', 120), | ||
'LOG_TYPE': (str, 'Logs', 'info'), | ||
} | ||
|
||
|
||
class Config: | ||
|
||
def get_setting(self, key): | ||
""" Cast any value in the config to the right type or use the default | ||
""" | ||
key, definition_type, section, default = self._define(key) | ||
my_val = definition_type(os.getenv(key, default)) | ||
return my_val | ||
|
||
def _define(self, name): | ||
key = name.upper() | ||
definition = _CONFIG_DEFAULTS[key] | ||
if len(definition) == 3: | ||
definition_type, section, default = definition | ||
else: | ||
definition_type, section, _, default = definition | ||
return key, definition_type, section, default | ||
|
||
def __getattr__(self, name): | ||
""" | ||
Retrieves config value for the setting | ||
""" | ||
if not re.match(r'[A-Z_]+$', name): | ||
return super(Config, self).__getattr__(name) | ||
else: | ||
return self.get_setting(name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.