Added consecutive downtime threshold for rebooting

This commit is contained in:
Ashish D'Souza 2023-06-28 12:54:30 +10:00
parent 32f5d9147a
commit 8df316f5fa
1 changed files with 17 additions and 5 deletions

View File

@ -47,18 +47,30 @@ class Router:
if __name__ == '__main__': if __name__ == '__main__':
router = Router(os.environ['ROUTER_IP'], os.environ['ROUTER_PASSWORD']) router = Router(os.environ['ROUTER_IP'], os.environ['ROUTER_PASSWORD'])
consecutive_internet_downtime = 0
while True: while True:
sleep(60)
if not router.check_internet_connectivity(): if not router.check_internet_connectivity():
# Internet is down # Internet is down
if router.check_lan_connectivity(): if router.check_lan_connectivity():
# LAN is up # LAN is up
with open('/logs/connectivity.log', 'a') as log: if consecutive_internet_downtime >= 2:
log.write(f'{datetime.now()} - Internet down') with open('/logs/connectivity.log', 'a') as log:
log.write(f'{datetime.now()} - Internet down, rebooting router\n')
router.reboot()
consecutive_downtime = 0
else:
consecutive_internet_downtime += 1
with open('/logs/connectivity.log', 'a') as log:
log.write(f'{datetime.now()} - Internet down\n')
router.reboot()
else: else:
# LAN is down # LAN is down
with open('/logs/connectivity.log', 'a') as log: with open('/logs/connectivity.log', 'a') as log:
log.write(f'{datetime.now()} - Internet and LAN down') log.write(f'{datetime.now()} - Internet and LAN down\n')
sleep(60) consecutive_downtime = 0
else:
consecutive_downtime = 0