Skip to content

Commit

Permalink
fix multiple bugs discovered in #31
Browse files Browse the repository at this point in the history
  • Loading branch information
highvolt-dev committed Jan 1, 2022
1 parent 8cb8dff commit 9e2df70
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ optional arguments:
how long in seconds to wait between ping health checks
-R, --reboot skip health checks and immediately reboot gateway
-r, --skip-reboot skip rebooting gateway
--skip-bands skip check for connected band
--skip-bands skip check for connected 4g band
--skip-5g-bands skip check for connected 5g band
--skip-ping skip check for successful ping
--skip-enbid skip check for connected eNB ID
Expand Down Expand Up @@ -87,7 +87,7 @@ optional arguments:
Skip rebooting gateway.

**Skip Bands:** `--skip-bands`
Skip check for connected band.
Skip check for connected 4g band.

**Skip 5g Bands:** `--skip-5g-bands`
Skip check for connected 5g band.
Expand Down
10 changes: 5 additions & 5 deletions example.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rename file to `.env` to use.
# Rename file to `.env` to use.
# The .env file will be loaded from the current or (recursive) parent directories.
# All settings are prefixed with `tmo_` to allow for common `.env` files at the root.
# All settings are prefixed with `tmo_` to allow for common `.env` files at the root.

# Example:
tmo_username=admin
Expand Down Expand Up @@ -31,7 +31,7 @@ tmo_print_config=True
# tmo_secondary_band # 5G band: comma-separated list from {n41,n71}
# tmo_enbid # eNB ID

# Reboot settings: minimum uptime (seconds) & reboot on failed check {True, False}
# Reboot settings: minimum uptime (seconds) & reboot on failed check {True, False}
# Note that these semantics differ from command line arguments!
# tmo_skip_reboot # overrides all other reboot options
# tmo_min_uptime # Minimum uptime to reboot, defaults to 90 seconds
Expand All @@ -43,5 +43,5 @@ tmo_print_config=True
# General settings
# tmo_print_config # {True | False } Output configuration to console
# tmo_logfile # Filename for logging output (default: 'tmo-monitor.log')
# tmo_log_all # Log all connection statistics
# tmo_log_delta # Log any change in connection statistics
# tmo_log_all # {True | False } Log all connection statistics
# tmo_log_delta # {True | False } Log any change in connection statistics
48 changes: 28 additions & 20 deletions tmo-monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,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', ''), ('uptime', '')])
self.connection = dict([('primary_band', None), ('secondary_band', ['n41']), ('enbid', None), ('uptime', '')])
self.reboot = dict([('uptime', 90), ('ping', False), ('4G_band', False), ('5G_band', False), ('enbid', False)])
self.general = dict([('print_config', False), ('logfile', ''), ('log_all', False), ('log_delta', False)])

Expand All @@ -207,7 +207,7 @@ def __init__(self):
for var in {'ping', '4G_band', '5G_band', 'enbid'}:
self.reboot[var] = False
if not self.login['password']:
self.password = getpass.getpass('Password: ')
self.login['password'] = getpass.getpass('Password: ')


def read_environment(self):
Expand All @@ -233,7 +233,11 @@ def read_environment(self):
tmp = os.environ.get('tmo_enbid')
if tmp != None:
self.connection['enbid'] = tmp
for var in {'uptime', 'ping', '4G_band', '5G_band', 'enbid'}:
tmp = os.environ.get('tmo_min_uptime')
if tmp != None:
self.reboot['uptime'] = tmp

for var in {'ping', '4G_band', '5G_band', 'enbid'}:
tmp = os.environ.get('tmo_' + var + '_reboot')
if tmp != None:
if tmp.lower() == 'true':
Expand All @@ -243,9 +247,9 @@ def read_environment(self):
tmp = os.environ.get('tmo_skip_reboot')
if tmp != None:
if tmp.lower() == 'true':
self.reboot[var] = True
self.skip_reboot = True
else:
self.reboot[var] = False
self.skip_reboot = False
tmp = os.environ.get('tmo_logfile')
if tmp != None:
self.general['logfile'] = tmp
Expand All @@ -264,26 +268,26 @@ def parse_commandline(self):
self.parser.add_argument('password', type=str, help='the administrative password (will be requested at runtime if not passed as argument)', nargs='?')
# ping configuration
self.parser.add_argument('-I', '--interface', type=str, help='the network interface to use for ping. pass the source IP on Windows')
self.parser.add_argument('-H', '--ping-host', type=str, default='google.com', help='the host to ping (defaults to google.com)')
self.parser.add_argument('--ping-count', type=int, default=1, help='how many ping health checks to perform (defaults to 1)')
self.parser.add_argument('--ping-interval', type=int, default=10, help='how long in seconds to wait between ping health checks (defaults to 10)')
self.parser.add_argument('-H', '--ping-host', type=str, default=self.ping['ping_host'], help='the host to ping (defaults to google.com)')
self.parser.add_argument('--ping-count', type=int, default=self.ping['ping_count'], help='how many ping health checks to perform (defaults to 1)')
self.parser.add_argument('--ping-interval', type=int, default=self.ping['ping_interval'], help='how long in seconds to wait between ping health checks (defaults to 10)')
# reboot settings
self.parser.add_argument('-R', '--reboot', action='store_true', help='skip health checks and immediately reboot gateway')
self.parser.add_argument('-r', '--skip-reboot', action='store_true', help='skip rebooting gateway')
self.parser.add_argument('--skip-bands', action='store_true', help='skip check for connected band')
self.parser.add_argument('--skip-bands', action='store_true', help='skip check for connected 4g band')
self.parser.add_argument('--skip-5g-bands', action='store_true', help='skip check for connected 5g band')
self.parser.add_argument('--skip-ping', action='store_true', help='skip check for successful ping')
self.parser.add_argument('--skip-enbid', action='store_true', help='skip check for connected eNB ID')
self.parser.add_argument('--uptime', type=int, default=90, help='how long the gateway must be up before considering a reboot (defaults to 90 seconds)')
self.parser.add_argument('--uptime', type=int, default=self.reboot['uptime'], help='how long the gateway must be up before considering a reboot (defaults to 90 seconds)')
# connection configuration
self.parser.add_argument('-4', '--4g-band', type=str, action='append', dest='primary_band', default=None, choices=['B2', 'B4', 'B5', 'B12', 'B13', 'B25', 'B26', 'B41', 'B46', 'B48', 'B66', 'B71'], help='the 4g band(s) to check')
self.parser.add_argument('-5', '--5g-band', type=str, action='append', dest='secondary_band', default=None, choices=['n41', 'n71'], help='the 5g band(s) to check (defaults to n41)')
self.parser.add_argument('--enbid', type=int, default=None, help='check for a connection to a given eNB ID')
self.parser.add_argument('--enbid', type=int, default=self.connection['enbid'], help='check for a connection to a given eNB ID')
# general configuration
self.parser.add_argument('--print-config', action='store_true', help='output configuration settings')
self.parser.add_argument('--logfile', type=str, default='tmo-monitor.log', help='output file for logging')
self.parser.add_argument('--log-all', action='store_true', help='always write connection details to logfile')
self.parser.add_argument('--log-delta', action='store_true', help='write connection details to logfile on change')
self.parser.add_argument('--print-config', action='store_true', default=self.general['print_config'], help='output configuration settings')
self.parser.add_argument('--logfile', type=str, default=self.general['logfile'], help='output file for logging')
self.parser.add_argument('--log-all', action='store_true', default=self.general['log_all'], help='always write connection details to logfile')
self.parser.add_argument('--log-delta', action='store_true', default=self.general['log_delta'], help='write connection details to logfile on change')
return self.parser.parse_args()

def parse_arguments(self, args):
Expand Down Expand Up @@ -364,9 +368,9 @@ def print_config(self):
reboot_requested = False

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

tc_control = TrashCanController(config.login['username'], config.login['password'])

Expand All @@ -390,7 +394,7 @@ def print_config(self):
primary_band = config.connection['primary_band']
band_4g = signal_info['cell_LTE_stats_cfg'][0]['stat']['Band']
connection['4G'] = band_4g
if (band_4g not in primary_band) and config.reboot['4G_band']:
if (primary_band and band_4g not in primary_band) and config.reboot['4G_band']:
print_and_log('Not on ' + ('one of ' if len(primary_band) > 1 else '') + ', '.join(primary_band) + '.')
if config.reboot['4G_band']:
reboot_requested = True
Expand Down Expand Up @@ -437,10 +441,13 @@ def print_config(self):
print('No reboot necessary.')

if log_all and config.general['log_delta'] and config.general['logfile']:
logline = tailer.tail(open(config.general['logfile']), 1)
# Tail the last 10 lines of the file (to account for logged errors) and reverse to detect the newest logline
logline = tailer.tail(open(config.general['logfile']), 10)
logline.reverse()
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)
print(line)
data = parse("{0} [INFO] 4G: {1} | 5G: {2} | eNB ID: {3} | Avg Ping: {4} ms | Uptime: {5} sec", line)
if data[1] != connection['4G']:
print_and_log("4G connection is {0}, was {1}".format(connection['4G'], data[1]))
config.general['log_all'] = True
Expand All @@ -456,11 +463,12 @@ def print_config(self):
if int(data[5]) > connection['uptime']:
print_and_log("Uptime {0} sec, less than {1} sec".format(connection['uptime'], data[5]))
config.general['log_all'] = True
break

if log_all and config.general['log_all']:
if config.general['logfile'] == '':
logging.error("Logging requested but file not specified")
else:
msg = "4G: {0} | 5G: {1} | eNB ID: {2} | Avg Ping: {3} ms | Uptime: {4} sec".format(
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_and_log(msg)

0 comments on commit 9e2df70

Please sign in to comment.