Add cli parameter lan_gateway_ip.
This commit is contained in:
parent
8422d6e36d
commit
8ceec7bb96
1 changed files with 30 additions and 20 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
|
import argparse
|
||||||
import socket
|
import socket
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from voussoirkit import betterhelp
|
||||||
from voussoirkit import hms
|
from voussoirkit import hms
|
||||||
from voussoirkit import threadpool
|
from voussoirkit import threadpool
|
||||||
from voussoirkit import vlogging
|
from voussoirkit import vlogging
|
||||||
|
|
@ -35,12 +37,11 @@ def percentage(items):
|
||||||
trues = sum(bool(i) for i in items)
|
trues = sum(bool(i) for i in items)
|
||||||
return trues / len(items)
|
return trues / len(items)
|
||||||
|
|
||||||
def ping_lan():
|
def ping_lan(gateway_ip):
|
||||||
ip = '192.168.1.1'
|
log.debug('Checking LAN at %s.', gateway_ip)
|
||||||
log.debug('Checking LAN at %s.', ip)
|
|
||||||
try:
|
try:
|
||||||
socket.setdefaulttimeout(1)
|
socket.setdefaulttimeout(1)
|
||||||
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((ip, 80))
|
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((gateway_ip, 80))
|
||||||
return True
|
return True
|
||||||
except socket.error:
|
except socket.error:
|
||||||
return False
|
return False
|
||||||
|
|
@ -76,21 +77,12 @@ def check_ip():
|
||||||
log.debug('Checking IP %s.', ip)
|
log.debug('Checking IP %s.', ip)
|
||||||
try:
|
try:
|
||||||
socket.setdefaulttimeout(2)
|
socket.setdefaulttimeout(2)
|
||||||
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(('104.43.253.214', 80))
|
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((ip, 80))
|
||||||
return True
|
return True
|
||||||
except socket.error:
|
except socket.error:
|
||||||
return False
|
return False
|
||||||
ips = [
|
ips = [
|
||||||
'104.43.253.214',
|
'198.100.155.125', # voussoir.net
|
||||||
'13.107.21.200',
|
|
||||||
'13.77.161.179',
|
|
||||||
'142.136.81.136',
|
|
||||||
'151.101.1.140',
|
|
||||||
'172.217.11.174',
|
|
||||||
'172.217.11.78',
|
|
||||||
'176.32.103.205',
|
|
||||||
'35.165.194.49',
|
|
||||||
'69.252.80.75',
|
|
||||||
]
|
]
|
||||||
thread_pool.pause()
|
thread_pool.pause()
|
||||||
jobs = [{'function': check, 'args': [ip]} for ip in ips]
|
jobs = [{'function': check, 'args': [ip]} for ip in ips]
|
||||||
|
|
@ -127,10 +119,10 @@ def end_down():
|
||||||
down = False
|
down = False
|
||||||
outage_started = None
|
outage_started = None
|
||||||
|
|
||||||
def check_forever():
|
def check_forever(lan_gateway_ip):
|
||||||
while True:
|
while True:
|
||||||
now = time.strftime('%Y-%m-%d %H:%M:%S')
|
now = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||||
lan_ok = int(ping_lan())
|
lan_ok = int(ping_lan(lan_gateway_ip))
|
||||||
dns_stat = check_dns()
|
dns_stat = check_dns()
|
||||||
ip_stat = check_ip()
|
ip_stat = check_ip()
|
||||||
message = f'{now}, LAN={lan_ok}, DNS={dns_stat:0.2f}, IP={ip_stat:0.2f}'
|
message = f'{now}, LAN={lan_ok}, DNS={dns_stat:0.2f}, IP={ip_stat:0.2f}'
|
||||||
|
|
@ -148,13 +140,31 @@ def check_forever():
|
||||||
else:
|
else:
|
||||||
time.sleep(20)
|
time.sleep(20)
|
||||||
|
|
||||||
@vlogging.main_decorator
|
def internetcheck_argparse(args):
|
||||||
def main(argv):
|
|
||||||
try:
|
try:
|
||||||
check_forever()
|
check_forever(lan_gateway_ip=args.lan_gateway_ip)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@vlogging.main_decorator
|
||||||
|
def main(argv):
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='''
|
||||||
|
This program will periodically ping both LAN and external IP addresses
|
||||||
|
to help you troubleshoot the time and duration of network outages.
|
||||||
|
''',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--lan_gateway_ip',
|
||||||
|
'--lan-gateway-ip',
|
||||||
|
default='192.168.1.1',
|
||||||
|
help='''
|
||||||
|
''',
|
||||||
|
)
|
||||||
|
parser.set_defaults(func=internetcheck_argparse)
|
||||||
|
|
||||||
|
return betterhelp.go(parser, argv)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
raise SystemExit(main(sys.argv[1:]))
|
raise SystemExit(main(sys.argv[1:]))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue