Format
This commit is contained in:
@@ -5,63 +5,99 @@ import tomllib
|
|||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
with open('config.toml', 'rb') as f:
|
with open("config.toml", "rb") as f:
|
||||||
config = tomllib.load(f)
|
config = tomllib.load(f)
|
||||||
|
|
||||||
app = Flask('velping')
|
app = Flask("velping")
|
||||||
logging.disable(logging.CRITICAL)
|
logging.disable(logging.CRITICAL)
|
||||||
|
|
||||||
uptime = {}
|
uptime = {}
|
||||||
|
|
||||||
|
|
||||||
def ping(service):
|
def ping(service):
|
||||||
while True:
|
while True:
|
||||||
print(f'[I] Pinging {service}')
|
print(f"[I] Pinging {service}")
|
||||||
try:
|
try:
|
||||||
resp = requests.get(config['services'][service])
|
resp = requests.get(config["services"][service])
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
|
|
||||||
if uptime[service][config['frontend']['pings'] - 1] != 'I' and config['ntfy']['enabled']:
|
if (
|
||||||
requests.post(f'https://{config['ntfy']['server']}/{config['ntfy']['topic']}', data=f'Service {service} is online')
|
uptime[service][config["frontend"]["pings"] - 1] != "I"
|
||||||
|
and config["ntfy"]["enabled"]
|
||||||
|
):
|
||||||
|
requests.post(
|
||||||
|
f"https://{config['ntfy']['server']}/{config['ntfy']['topic']}",
|
||||||
|
data=f"Service {service} is online",
|
||||||
|
)
|
||||||
|
|
||||||
uptime[service].pop(0)
|
uptime[service].pop(0)
|
||||||
uptime[service].append('I')
|
uptime[service].append("I")
|
||||||
print(f'[I] Pinging {service} worked!')
|
print(f"[I] Pinging {service} worked!")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if 'Remote end closed connection without response' in str(e) and config['pinging']['allow_empty_responses']:
|
if (
|
||||||
|
"Remote end closed connection without response" in str(e)
|
||||||
|
and config["pinging"]["allow_empty_responses"]
|
||||||
|
):
|
||||||
uptime[service].pop(0)
|
uptime[service].pop(0)
|
||||||
uptime[service].append('I')
|
uptime[service].append("I")
|
||||||
print(f'[I] Pinging {service} worked!')
|
print(f"[I] Pinging {service} worked!")
|
||||||
else:
|
else:
|
||||||
if uptime[service][config['frontend']['pings'] - 1] != 'O' and config['ntfy']['enabled']:
|
if (
|
||||||
requests.post(f'https://{config['ntfy']['server']}/{config['ntfy']['topic']}', data=f'Service {service} is offline')
|
uptime[service][config["frontend"]["pings"] - 1] != "O"
|
||||||
|
and config["ntfy"]["enabled"]
|
||||||
|
):
|
||||||
|
requests.post(
|
||||||
|
f"https://{config['ntfy']['server']}/{config['ntfy']['topic']}",
|
||||||
|
data=f"Service {service} is offline",
|
||||||
|
)
|
||||||
|
|
||||||
uptime[service].pop(0)
|
uptime[service].pop(0)
|
||||||
uptime[service].append('O')
|
uptime[service].append("O")
|
||||||
print(f'[E] An error happened while pinging for {service}: {e}')
|
print(f"[E] An error happened while pinging for {service}: {e}")
|
||||||
|
|
||||||
time.sleep(config['pinging']['seconds_between_ping'])
|
time.sleep(config["pinging"]["seconds_between_ping"])
|
||||||
|
|
||||||
for service in config['services']:
|
|
||||||
|
for service in config["services"]:
|
||||||
uptime[service] = []
|
uptime[service] = []
|
||||||
for ping_but_different in range(config['frontend']['pings']):
|
for _ in range(config["frontend"]["pings"]):
|
||||||
uptime[service].append('?')
|
uptime[service].append("?")
|
||||||
|
|
||||||
|
threading.Thread(
|
||||||
|
target=ping,
|
||||||
|
args=(service,),
|
||||||
|
).start()
|
||||||
|
|
||||||
threading.Thread(target=ping, args=(service,)).start()
|
|
||||||
|
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
def not_found(error):
|
def not_found(error):
|
||||||
return redirect('/', 303)
|
return redirect("/", 308)
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(500)
|
@app.errorhandler(500)
|
||||||
def internal_error(error):
|
def internal_error(error):
|
||||||
return render_template('internal_error.xht', config=config), 500
|
return render_template(
|
||||||
|
"internal_error.xht",
|
||||||
|
config=config,
|
||||||
|
), 500
|
||||||
|
|
||||||
@app.route('/style.css')
|
|
||||||
|
@app.route("/style.css")
|
||||||
def css():
|
def css():
|
||||||
return send_file('assets/style.css')
|
return send_file("assets/style.css")
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
|
@app.route("/")
|
||||||
def root():
|
def root():
|
||||||
return render_template('index.xht', config=config, services=config['services'], uptime=uptime), 200
|
return render_template(
|
||||||
|
"index.xht",
|
||||||
|
config=config,
|
||||||
|
services=config["services"],
|
||||||
|
uptime=uptime,
|
||||||
|
), 200
|
||||||
|
|
||||||
app.run(port=config['frontend']['port'], host='::')
|
|
||||||
|
app.run(
|
||||||
|
port=config["frontend"]["port"],
|
||||||
|
host="::",
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user