script német verzio
This commit is contained in:
92
Scripts/python/check_file_age_de.py
Normal file
92
Scripts/python/check_file_age_de.py
Normal file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Skript zur Überprüfung des Dateialters für n8n-Automatisierung.
|
||||
Verwendung: python3 check_file_age_de.py <verzeichnispfad> <max_minuten>
|
||||
|
||||
Ausgabe: JSON-Format (n8n-freundlich)
|
||||
Exit-Code: 0 (wenn alles ok), 1 (wenn eine alte Datei gefunden wurde oder ein Fehler auftrat)
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import sys
|
||||
import json
|
||||
|
||||
def check_files(directory, max_age_minutes=40):
|
||||
now = time.time()
|
||||
max_age_seconds = max_age_minutes * 60
|
||||
too_old_files = []
|
||||
|
||||
# Überprüfung, ob das Verzeichnis existiert
|
||||
if not os.path.exists(directory):
|
||||
return {
|
||||
"status": "error",
|
||||
"alert": True,
|
||||
"message": f"Das angegebene Verzeichnis existiert nicht: {directory}",
|
||||
"files": []
|
||||
}
|
||||
|
||||
try:
|
||||
# Dateien auflisten
|
||||
files_in_dir = os.listdir(directory)
|
||||
|
||||
for filename in files_in_dir:
|
||||
filepath = os.path.join(directory, filename)
|
||||
|
||||
# Nur Dateien prüfen (keine Verzeichnisse)
|
||||
if os.path.isfile(filepath):
|
||||
file_age_seconds = now - os.path.getmtime(filepath)
|
||||
|
||||
if file_age_seconds > max_age_seconds:
|
||||
age_min = round(file_age_seconds / 60, 1)
|
||||
too_old_files.append({
|
||||
"file": filename,
|
||||
"age_minutes": age_min,
|
||||
"last_modified": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(filepath)))
|
||||
})
|
||||
|
||||
if too_old_files:
|
||||
return {
|
||||
"status": "alert",
|
||||
"alert": True,
|
||||
"message": f"{len(too_old_files)} Datei(en) gefunden, die älter als {max_age_minutes} Minuten sind!",
|
||||
"directory": directory,
|
||||
"limit_minutes": max_age_minutes,
|
||||
"files": too_old_files
|
||||
}
|
||||
|
||||
return {
|
||||
"status": "ok",
|
||||
"alert": False,
|
||||
"message": "Alle Dateien sind aktuell.",
|
||||
"directory": directory,
|
||||
"files": []
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
"status": "error",
|
||||
"alert": True,
|
||||
"message": f"Fehler während der Überprüfung aufgetreten: {str(e)}",
|
||||
"files": []
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Parameter aus der Befehlszeile übernehmen
|
||||
if len(sys.argv) < 2:
|
||||
print(json.dumps({"status": "error", "message": "Fehlender Parameter! Verwendung: python3 check_file_age_de.py <verzeichnis> <minuten>"}))
|
||||
sys.exit(1)
|
||||
|
||||
dir_to_check = sys.argv[1]
|
||||
age_limit = int(sys.argv[2]) if len(sys.argv) > 2 else 40
|
||||
|
||||
result = check_files(dir_to_check, age_limit)
|
||||
|
||||
# JSON-Ausgabe für n8n
|
||||
print(json.dumps(result, indent=2))
|
||||
|
||||
# Exit-Code für den n8n SSH-Node festlegen
|
||||
if result.get("alert"):
|
||||
sys.exit(1)
|
||||
else:
|
||||
sys.exit(0)
|
||||
Reference in New Issue
Block a user