Add logging
This commit is contained in:
27
src/app.py
27
src/app.py
@@ -1,7 +1,7 @@
|
||||
from flask import Flask, request
|
||||
from reconnect import reboot
|
||||
from os import environ
|
||||
from subprocess import call
|
||||
from subprocess import call, DEVNULL, STDOUT
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -15,47 +15,58 @@ path = environ.get('ROUTER_PATH') or '/ws'
|
||||
|
||||
@app.route('/', methods=['POST'])
|
||||
def result():
|
||||
print(request.json)
|
||||
return 'Received request, printing to console!'
|
||||
app.logger.debug(request.json)
|
||||
return 'Received request!'
|
||||
|
||||
@app.route('/safe', methods=['POST'])
|
||||
def safe():
|
||||
app.logger.info('Initializing safe reboot process')
|
||||
|
||||
# Try to ping the router 100 times
|
||||
for i in range(50):
|
||||
if call(['ping', '-c', '1', url]) == 0:
|
||||
if call(['ping', '-c', '1', url], stdout=DEVNULL, stderr=STDOUT) == 0:
|
||||
app.logger.info('Router is online')
|
||||
break
|
||||
else:
|
||||
app.logger.error('Router is offline')
|
||||
return 'Error: Unable to ping and reboot router automatically!'
|
||||
|
||||
# Try to nslookup 5 different websites
|
||||
# Try to nslookup 5 different websites to see if DNS is working
|
||||
websites = ['www.google.com', 'www.facebook.com', 'www.twitter.com', 'www.reddit.com', 'www.amazon.com']
|
||||
success = 0
|
||||
|
||||
for website in websites:
|
||||
if call(['nslookup', website]) == 0:
|
||||
if call(['nslookup', website], stdout=DEVNULL, stderr=STDOUT) == 0:
|
||||
app.logger.info(f'Successfully resolved {website}')
|
||||
success += 1
|
||||
|
||||
if success > 3:
|
||||
app.logger.error('Websites resolved successfully')
|
||||
return 'Error: DNS seems to be working, debug manually!'
|
||||
|
||||
# Reboot router
|
||||
app.logger.info('Rebooting router')
|
||||
status = reboot(url, path, username, password).text
|
||||
|
||||
if '{"status":true}' not in status:
|
||||
app.logger.error('Reboot failed')
|
||||
return 'Error: Something went wrong while rebooting router!'
|
||||
|
||||
app.logger.info('Reboot successful')
|
||||
return 'Success: Rebooted router!'
|
||||
|
||||
@app.route('/force', methods=['POST'])
|
||||
def force():
|
||||
app.logger.info('Initializing force reboot process')
|
||||
status = reboot(url, path, username, password).text
|
||||
|
||||
if '{"status":true}' not in status:
|
||||
app.logger.error('Reboot failed')
|
||||
return 'Error: Something went wrong while rebooting router!'
|
||||
|
||||
app.logger.info('Reboot successful')
|
||||
return 'Success: Rebooted router!'
|
||||
|
||||
@app.route('/grafana', methods=['POST'])
|
||||
def grafana():
|
||||
if request.json['state'] == 'alerting':
|
||||
safe()
|
||||
return safe()
|
||||
|
Reference in New Issue
Block a user