Add logging

This commit is contained in:
2022-04-23 13:42:07 +03:00
parent cd283aee37
commit 9e26787e42

View File

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