srv/poe_manager/app.py aktualisiert
This commit is contained in:
@@ -81,6 +81,38 @@ def logout():
|
|||||||
logout_user()
|
logout_user()
|
||||||
return redirect(url_for('login'))
|
return redirect(url_for('login'))
|
||||||
|
|
||||||
|
import glob
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
def get_last_seen(dev_name: str):
|
||||||
|
"""Letztes Mal, dass ein Gerät erreichbar war."""
|
||||||
|
log_files = glob.glob("/var/log/rpi-*.log")
|
||||||
|
if not log_files:
|
||||||
|
return None
|
||||||
|
|
||||||
|
latest_time = None
|
||||||
|
|
||||||
|
# alle Logs durchgehen
|
||||||
|
for logfile in sorted(log_files):
|
||||||
|
with open(logfile, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if f"{dev_name} ist erreichbar!" in line:
|
||||||
|
try:
|
||||||
|
ts_str = line.split(" ")[0] + " " + line.split(" ")[1] # "YYYY-MM-DD HH:MM:SS"
|
||||||
|
ts = datetime.strptime(ts_str, "%Y-%m-%d %H:%M:%S")
|
||||||
|
if latest_time is None or ts > latest_time:
|
||||||
|
latest_time = ts
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if latest_time:
|
||||||
|
date_str = latest_time.strftime("Zuletzt Online am %d.%m.%Y")
|
||||||
|
time_str = latest_time.strftime("um %H:%M Uhr")
|
||||||
|
return date_str, time_str
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
@login_required
|
@login_required
|
||||||
def index():
|
def index():
|
||||||
@@ -112,8 +144,21 @@ def index():
|
|||||||
elif f"{dev[1]} ist nicht erreichbar!" in line:
|
elif f"{dev[1]} ist nicht erreichbar!" in line:
|
||||||
status_dict[dev[0]] = "offline"
|
status_dict[dev[0]] = "offline"
|
||||||
|
|
||||||
|
last_seen_dict = {}
|
||||||
|
|
||||||
|
for dev in devices:
|
||||||
|
# Status aus Log ermitteln
|
||||||
|
status_dict[dev[0]] = "offline" if f"{dev[1]} ist nicht erreichbar!" in open(max(glob.glob('/var/log/rpi-*.log'), key=os.path.getctime)).read() else "online"
|
||||||
|
|
||||||
|
# Last Seen nur, wenn offline
|
||||||
|
if status_dict[dev[0]] == "offline":
|
||||||
|
last_seen = get_last_seen(dev[1]) # dev[1] = Device Name
|
||||||
|
if last_seen:
|
||||||
|
last_seen_dict[dev[0]] = last_seen
|
||||||
|
|
||||||
|
|
||||||
# Template rendern mit Devices, Status und Intervall
|
# Template rendern mit Devices, Status und Intervall
|
||||||
return render_template("index.html", devices=devices, status=status_dict, interval=interval)
|
return render_template("index.html", devices=devices, status=status_dict, last_seen=last_seen_dict, interval=interval)
|
||||||
|
|
||||||
@app.route("/settings", methods=["GET", "POST"])
|
@app.route("/settings", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
|
|||||||
Reference in New Issue
Block a user