functions
This commit is contained in:
89
app.py
89
app.py
@@ -81,6 +81,21 @@ class Log(db.Model):
|
||||
device_id = db.Column(db.Integer, db.ForeignKey('device.id'))
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
device = db.relationship('Device', backref='logs')
|
||||
class UpdateHistory(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
device_id = db.Column(db.Integer, db.ForeignKey('device.id'))
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
update_type = db.Column(db.String(50))
|
||||
details = db.Column(db.Text)
|
||||
device = db.relationship('Device', backref='update_histories')
|
||||
|
||||
class Anomaly(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
device_id = db.Column(db.Integer, db.ForeignKey('device.id'), nullable=True)
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
description = db.Column(db.Text)
|
||||
resolved = db.Column(db.Boolean, default=False)
|
||||
device = db.relationship('Device', backref='anomalies')
|
||||
|
||||
# Inicjalizacja bazy (utworzyć bazę przy pierwszym uruchomieniu)
|
||||
with app.app_context():
|
||||
@@ -268,10 +283,9 @@ def check_all_devices():
|
||||
device.current_version = current_version
|
||||
device.current_firmware = current_firmware
|
||||
db.session.commit()
|
||||
# Zapis do tabeli logów
|
||||
#log_entry = Log(message=result, device_id=device.id, user_id=device.user_id)
|
||||
log_message = f"Urządzenie {device.name or device.ip} - {result}"
|
||||
log_entry = Log(message=log_message, device_id=device.id, user_id=device.user_id)
|
||||
log_entry = Log(message=result, device_id=device.id, user_id=device.user_id)
|
||||
#log_message = f"Urządzenie {device.name or device.ip} - {result}"
|
||||
#log_entry = Log(message=log_message, device_id=device.id, user_id=device.user_id)
|
||||
db.session.add(log_entry)
|
||||
db.session.commit()
|
||||
# Powiadomienia, jeśli dostępna aktualizacja
|
||||
@@ -281,7 +295,6 @@ def check_all_devices():
|
||||
send_pushover_notification(user, message)
|
||||
send_email_notification(user, "Aktualizacja dostępna", message)
|
||||
|
||||
|
||||
def bytes_to_human(n):
|
||||
try:
|
||||
n = int(n)
|
||||
@@ -304,10 +317,34 @@ def clean_old_logs():
|
||||
Log.query.filter(Log.user_id == setting.user_id, Log.timestamp < cutoff).delete()
|
||||
db.session.commit()
|
||||
|
||||
def detect_anomalies():
|
||||
with app.app_context():
|
||||
# Ustal okres analizy, np. ostatnie 24 godziny
|
||||
cutoff = datetime.utcnow() - timedelta(hours=24)
|
||||
# Pobierz logi użytkowników (lub logi globalne) z tego okresu
|
||||
logs = Log.query.filter(Log.timestamp >= cutoff).all()
|
||||
|
||||
# Przykładowa analiza: wykryj logi zawierające określone słowa kluczowe
|
||||
error_keywords = ["błąd", "error", "niepowodzenie", "exception"]
|
||||
detected = {}
|
||||
for log in logs:
|
||||
lower_msg = log.message.lower()
|
||||
if any(keyword in lower_msg for keyword in error_keywords):
|
||||
detected.setdefault(log.device_id, []).append(log.message)
|
||||
|
||||
# Dla każdego urządzenia, jeżeli wykryto więcej niż określony próg błędów, zapisz anomalię
|
||||
for device_id, messages in detected.items():
|
||||
if len(messages) >= 3: # przykładowy próg
|
||||
description = f"Wykryto {len(messages)} błędne logi w ciągu ostatnich 24 godzin. Przykłady: " + "; ".join(messages[:3])
|
||||
anomaly = Anomaly(device_id=device_id, description=description)
|
||||
db.session.add(anomaly)
|
||||
# Możesz również wysłać powiadomienie, np. e-mail lub Pushover
|
||||
db.session.commit()
|
||||
|
||||
# Harmonogram sprawdzania aktualizacji – wykorzystujemy APScheduler
|
||||
scheduler = BackgroundScheduler()
|
||||
|
||||
# Inicjalizacja bazy i scheduler’a
|
||||
# Inicjalizacja bazy i schedulera
|
||||
with app.app_context():
|
||||
db.create_all() # lub już wcześniej utworzona baza
|
||||
# Pobranie globalnych ustawień – zakładamy, że Settings.query.first() zwróci ustawienia globalne
|
||||
@@ -324,6 +361,14 @@ with app.app_context():
|
||||
id="check_all_devices",
|
||||
max_instances=1
|
||||
)
|
||||
scheduler.add_job(
|
||||
func=detect_anomalies,
|
||||
trigger="interval",
|
||||
minutes=60,
|
||||
id="detect_anomalies",
|
||||
max_instances=1
|
||||
)
|
||||
|
||||
app.logger.debug(f"Scheduler initialized with interval: {interval} seconds")
|
||||
|
||||
scheduler.start()
|
||||
@@ -462,7 +507,6 @@ def device_detail(device_id):
|
||||
resource_data = {'error': str(e)}
|
||||
return render_template('device_detail.html', device=device, resource=resource_data)
|
||||
|
||||
|
||||
# Strona z logami
|
||||
@app.route('/logs')
|
||||
@login_required
|
||||
@@ -509,7 +553,6 @@ def settings():
|
||||
return render_template('settings.html', settings=user_settings)
|
||||
|
||||
|
||||
|
||||
@app.route('/device/<int:device_id>/edit', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_device(device_id):
|
||||
@@ -575,6 +618,15 @@ def update_device(device_id):
|
||||
list(api('/system/package/update/install', branch='beta'))
|
||||
else:
|
||||
list(api('/system/package/update/install'))
|
||||
|
||||
history = UpdateHistory(
|
||||
device_id=device.id,
|
||||
update_type="system",
|
||||
details=f"Aktualizacja systemu rozpoczęta na urządzeniu {device.name or device.ip}."
|
||||
)
|
||||
db.session.add(history)
|
||||
db.session.commit()
|
||||
|
||||
flash("Aktualizacja systemu została rozpoczęta.")
|
||||
app.logger.debug("System update command executed successfully")
|
||||
except Exception as e:
|
||||
@@ -599,6 +651,15 @@ def update_firmware(device_id):
|
||||
)
|
||||
# Przykładowa komenda aktualizacji firmware
|
||||
list(api('/system/routerboard/upgrade'))
|
||||
|
||||
history = UpdateHistory(
|
||||
device_id=device.id,
|
||||
update_type="firmware",
|
||||
details=f"Aktualizacja firmware rozpoczęta na urządzeniu {device.name or device.ip}."
|
||||
)
|
||||
db.session.add(history)
|
||||
db.session.commit()
|
||||
|
||||
flash("Aktualizacja firmware została rozpoczęta.")
|
||||
except Exception as e:
|
||||
flash(f"Błąd podczas aktualizacji firmware: {e}")
|
||||
@@ -658,6 +719,18 @@ def clean_logs():
|
||||
flash(f"Usunięto {num_deleted} logów starszych niż {days} dni.")
|
||||
return redirect(url_for('logs'))
|
||||
|
||||
@app.route('/update_history')
|
||||
@login_required
|
||||
def update_history():
|
||||
histories = UpdateHistory.query.join(Device).filter(Device.user_id == current_user.id).order_by(UpdateHistory.timestamp.desc()).all()
|
||||
return render_template('update_history.html', histories=histories)
|
||||
|
||||
@app.route('/anomalies')
|
||||
@login_required
|
||||
def anomalies():
|
||||
anomalies = Anomaly.query.join(Device).filter(Device.user_id == current_user.id).order_by(Anomaly.timestamp.desc()).all()
|
||||
return render_template('anomalies.html', anomalies=anomalies)
|
||||
|
||||
# Zamknięcie harmonogramu przy zatrzymaniu aplikacji
|
||||
|
||||
atexit.register(lambda: scheduler.shutdown())
|
||||
|
Reference in New Issue
Block a user