Skip to content

Commit

Permalink
Add basic logging and connection logging
Browse files Browse the repository at this point in the history
Add new logging options:
  - tmo_logfile: specify logfile
  - tmo_log_all: log all connection statistics

Also move some error output to logfile
  • Loading branch information
AndrewPardoe committed Dec 24, 2021
1 parent fd85144 commit 3c1b750
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
3 changes: 2 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ tmo_print_config=True

# General settings
# tmo_print_config # {True | False } Output configuration to console

# tmo_logfile # Filename for logging output
# tmo_log_all # Log all connection statistics
30 changes: 17 additions & 13 deletions tmo-monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def ping_time(ping_index):
if is_win:
pattern = b'Minimum = \d+ms, Maximum = \d+ms, Average = (\d+)ms'
ping_ms = re.search(pattern, ping_exec.stdout)
return int(ping_ms.group(1))
return round(float(ping_ms.group(1)))

for i in range (ping_count):
result = ping_time(i)
Expand Down Expand Up @@ -186,7 +186,7 @@ def __init__(self):
self.skip_reboot = False
self.login = dict([('username', 'admin'), ('password', '')])
self.ping = dict([('interface', ''), ('ping_host', 'google.com'), ('ping_count', 1), ('ping_interval', 10)])
self.connection = dict([('primary_band', ''), ('secondary_band', 'n41'), ('enbid', '')])
self.connection = dict([('primary_band', ''), ('secondary_band', 'n41'), ('enbid', ''), ('uptime', '')])
self.reboot = dict([('uptime', 90), ('ping', False), ('4G_band', False), ('5G_band', False), ('enbid', False)])
self.general = dict([('print_config', False), ('logfile', '')])

Expand All @@ -195,11 +195,12 @@ def __init__(self):
args = self.parse_commandline()
self.parse_arguments(args)

# TODO: Move this to logging instead of sys.stderr?
# Also check for sys.stdin and sys.stdin.isatty() before printing help?
if self.skip_reboot and self.reboot_now:
print('Incompatible options: --reboot and --skip-reboot\n', file=sys.stderr)
self.parser.print_help(sys.stderr)
msg = 'Incompatible options: --reboot and --skip-reboot\n'
print(msg, file=sys.stderr)
logging.error(msg)
if sys.stdin and sys.stdin.isatty():
self.parser.print_help(sys.stderr)
sys.exit(2)
if self.skip_reboot:
for var in {'ping', '4G_band', '5G_band', 'enbid'}:
Expand Down Expand Up @@ -348,7 +349,7 @@ def print_config(self):
if config.general['logfile']:
# DEBUG logs go to console, all other logs to this file
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y/%m/%d %H:%M:%S',
filename=config.general['logfile'], encoding='utf-8', level=logging.INFO)
filename=config.general['logfile'], level=logging.INFO)
if config.reboot_now:
logging.info('Immediate reboot requested.')
reboot_requested = True
Expand Down Expand Up @@ -419,6 +420,9 @@ def print_config(self):
if config.reboot['ping']:
reboot_requested = True

# Reboot if needed
if (reboot_requested or log_all):
connection['uptime'] = tc_control.get_uptime()
if reboot_requested:
if config.skip_reboot:
print('Not rebooting.')
Expand All @@ -427,7 +431,7 @@ def print_config(self):
print(msg)
logging.info(msg)

if config.reboot_now or (tc_control.get_uptime() >= config.reboot['uptime']):
if config.reboot_now or (connection['uptime'] >= config.reboot['uptime']):
msg = 'Rebooting.'
print(msg)
logging.info(msg)
Expand All @@ -437,11 +441,11 @@ def print_config(self):
print(msg)
logging.info(msg)
else:
msg = 'No reboot necessary.'
print(msg)
logging.info(msg)
print('No reboot necessary.')

if log_all:
logging.info('4G=' + connection['4G'] + ' 5G=' + connection['5G'] + ' eNB ID=' +
connection['enbid'] + ' average ping=' + connection['ping'])
msg = "4G: {0} | 5G: {1} | eNB ID: {2} | Avg Ping: {3} ms | Uptime: {4} sec".format(
connection['4G'], connection['5G'], connection['enbid'], connection['ping'], connection['uptime'])
print(msg)
logging.info(msg)

0 comments on commit 3c1b750

Please sign in to comment.