Skip to content

Commit

Permalink
Add delta logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewPardoe committed Dec 26, 2021
1 parent 21e29e9 commit e31d079
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ tmo_print_config=True
# tmo_print_config # {True | False } Output configuration to console
# tmo_logfile # Filename for logging output
# tmo_log_all # Log all connection statistics
# tmo_log_delta # Log any change in connection statistics
62 changes: 44 additions & 18 deletions tmo-monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import os
from dotenv import load_dotenv, find_dotenv
import re
import tailer
from parse import *

class TrashCanController:
def __init__(self, username, password):
Expand Down Expand Up @@ -188,7 +190,7 @@ def __init__(self):
self.ping = dict([('interface', ''), ('ping_host', 'google.com'), ('ping_count', 1), ('ping_interval', 10)])
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', '')])
self.general = dict([('print_config', False), ('logfile', ''), ('log_all', False), ('log_delta', False)])

# Command line arguments override defaults & .env file
self.read_environment()
Expand Down Expand Up @@ -245,23 +247,16 @@ def read_environment(self):
self.reboot[var] = True
else:
self.reboot[var] = False
tmp = os.environ.get('tmo_print_config')
if tmp != None:
if tmp.lower() == 'true':
self.general['print_config'] = True
else:
self.general['print_config'] = False
tmp = os.environ.get('tmo_logfile')
if tmp != None:
self.general['logfile'] = tmp
tmp = os.environ.get('tmo_log_all')
if tmp != None:
if tmp.lower() == 'true':
self.general['log_all'] = True
else:
self.general['log_all'] = False


for var in {'print_config', 'log_all', 'log_delta'}:
tmp = os.environ.get('tmo_' + var)
if tmp != None:
if tmp.lower() == 'true':
self.general[var] = True
else:
self.general[var] = False

def parse_commandline(self):
self.parser = argparse.ArgumentParser(description='Check T-Mobile Home Internet cellular band(s) and connectivity and reboot if necessary')
Expand Down Expand Up @@ -357,7 +352,7 @@ def print_config(self):
reboot_requested = False

log_all = False
if config.general['log_all']:
if config.general['log_all'] or config.general['log_delta']:
log_all = True
connection = dict([('4G', ''), ('5G', ''), ('enbid', ''), ('ping', '')])

Expand Down Expand Up @@ -443,9 +438,40 @@ def print_config(self):
else:
print('No reboot necessary.')

if log_all:
if log_all and config.general['log_delta'] and config.general['logfile']:
logline = tailer.tail(open(config.general['logfile']), 1)
for line in logline:
if line.__contains__('|'):
data = parse("{0} [INFO] 4G: {1} | 5G: {2} | eNB ID: {3} | Avg Ping: {4} ms | Uptime: {5} sec", line)

if data[1] != connection['4G']:
msg = "4G connection is {0}, was {1}".format(connection['4G'], data[1])
print(msg)
logging.info(msg)
config.general['log_all'] = True
if data[2] != connection['5G']:
msg = "5G connection is {0}, was {1}".format(connection['5G'], data[2])
print(msg)
logging.info(msg)
config.general['log_all'] = True
if int(data[3]) != connection['enbid']:
msg = "eNB ID is {0}, was {1}".format(connection['enbid'], data[3])
print(msg)
logging.info(msg)
config.general['log_all'] = True
if int(data[4]) * 3 < connection['ping']:
msg = "Ping ms {0}, over 3x {1} ms".format(connection['ping'], data[4])
print(msg)
logging.info(msg)
config.general['log_all'] = True
if int(data[5]) > connection['uptime']:
msg = "Uptime {0} sec, less than {1} sec".format(connection['uptime'], data[5])
print(msg)
logging.info(msg)
config.general['log_all'] = True

if log_all and config.general['log_all']:
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 e31d079

Please sign in to comment.