93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Fájl kor ellenőrző script n8n automatizáláshoz.
|
|
Használat: python3 check_file_age.py <mappa_utvonal> <max_perc>
|
|
|
|
Kimenet: JSON formátum (n8n barát)
|
|
Exit kód: 0 (ha minden rendben), 1 (ha régi fájlt talált vagy hiba történt)
|
|
"""
|
|
|
|
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 = []
|
|
|
|
# Mappa létezésének ellenőrzése
|
|
if not os.path.exists(directory):
|
|
return {
|
|
"status": "error",
|
|
"alert": True,
|
|
"message": f"A megadott könyvtár nem létezik: {directory}",
|
|
"files": []
|
|
}
|
|
|
|
try:
|
|
# Fájlok listázása
|
|
files_in_dir = os.listdir(directory)
|
|
|
|
for filename in files_in_dir:
|
|
filepath = os.path.join(directory, filename)
|
|
|
|
# Csak a fájlokat ellenőrizzük (mappákat nem)
|
|
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"Találtam {len(too_old_files)} db fájlt, ami régebbi mint {max_age_minutes} perc!",
|
|
"directory": directory,
|
|
"limit_minutes": max_age_minutes,
|
|
"files": too_old_files
|
|
}
|
|
|
|
return {
|
|
"status": "ok",
|
|
"alert": False,
|
|
"message": "Minden fájl friss.",
|
|
"directory": directory,
|
|
"files": []
|
|
}
|
|
|
|
except Exception as e:
|
|
return {
|
|
"status": "error",
|
|
"alert": True,
|
|
"message": f"Hiba történt az ellenőrzés közben: {str(e)}",
|
|
"files": []
|
|
}
|
|
|
|
if __name__ == "__main__":
|
|
# Paraméterek átvétele a parancssorból
|
|
if len(sys.argv) < 2:
|
|
print(json.dumps({"status": "error", "message": "Hiányzó paraméter! Használat: python3 check_file_age.py <mappa> <perc>"}))
|
|
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 kimenet az n8n számára
|
|
print(json.dumps(result, indent=2))
|
|
|
|
# Exit code beállítása az n8n SSH Node számára
|
|
if result.get("alert"):
|
|
sys.exit(1)
|
|
else:
|
|
sys.exit(0)
|