diff --git a/__pycache__/main.cpython-313.pyc b/__pycache__/main.cpython-313.pyc index 55cae70..987dcb2 100644 Binary files a/__pycache__/main.cpython-313.pyc and b/__pycache__/main.cpython-313.pyc differ diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..b597471 --- /dev/null +++ b/compose.yml @@ -0,0 +1,25 @@ +version: '3.8' + +services: + db: + image: postgres:16 + environment: + POSTGRES_USER: sysmon + POSTGRES_PASSWORD: sysmonpass + POSTGRES_DB: sysmondb + volumes: + - db_data:/var/lib/postgresql/data + ports: + - "5432:5432" + + nuiks-sysmon: + image: nuiks-sysmon:latest + environment: + DATABASE_URL: postgres://sysmon:sysmonpass@db:5432/sysmondb + depends_on: + - db + ports: + - "8080:8080" + +volumes: + db_data: \ No newline at end of file diff --git a/main.py b/main.py index 51da36d..a0367c1 100644 --- a/main.py +++ b/main.py @@ -13,18 +13,19 @@ import threading if os.environ.get("DATABASE_URL") == None: os.environ["DATABASE_URL"] = "sqlite:///./wall_messages.db" - +else + DATABASE_URL = os.environ["DATABASE_URL"] + DATABASE_PASSWORD = os.environ["DATABASE_PASSWORD"] + + + app = FastAPI() -cpu_threshold = 80 -ram_threshold = 80 -disk_threshold = 80 - cpu_normal = True ram_normal = True disk_normal = True -DATABASE_URL = "sqlite:///./wall_messages.db" + engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) SessionLocal = sessionmaker(bind=engine) @@ -79,7 +80,7 @@ def check_and_log_overages(): cpu_limit = limits.cpu_threshold ram_limit = limits.ram_threshold disk_limit = limits.disk_threshold - + print("LIMITS: ",cpu_limit, ram_limit, disk_limit) cpu = psutil.cpu_percent(interval=0.5) ram = psutil.virtual_memory().percent disk = psutil.disk_usage('/').percent @@ -115,9 +116,8 @@ def check_and_log_overages(): threading.Thread(target=check_and_log_overages, daemon=True).start() - - start_time = time.time() + class Info(BaseModel): uptime: datetime.datetime cpu: float @@ -136,46 +136,56 @@ class Thresholds(BaseModel): disk_threshold: int -@app.get("/info", response_class=Info) +@app.get("/info", response_model=Info) def get_info(): - cpu = psutil.cpu_percent(interval=0.5) - ram = psutil.virtual_memory().percent - disk = psutil.disk_usage('/').percent - uptime = time.time() - start_time - info = Info( - uptime=uptime, - cpu=cpu, - ram=ram, - disk=disk, - cpu_threshold=cpu_threshold, - ram_threshold=ram_threshold, - disk_threshold=disk_threshold, - cpu_normal=cpu <= cpu_threshold, - ram_normal=ram <= ram_threshold, - disk_normal=disk <= disk_threshold + db = SessionLocal() + limits = db.query(LimitsModel).first() + if not limits: + db.close() + raise HTTPException(status_code=404, detail="Limits not found") + delta_seconds = int(time.time() - start_time) + system_information = Info( + uptime=datetime.datetime.now() - datetime.timedelta(seconds=delta_seconds), + cpu=psutil.cpu_percent(interval=0.5), + ram=psutil.virtual_memory().percent, + disk=psutil.disk_usage('/').percent, + cpu_threshold=limits.cpu_threshold, + ram_threshold=limits.ram_threshold, + disk_threshold=limits.disk_threshold, + cpu_normal=cpu_normal, + ram_normal=ram_normal, + disk_normal=disk_normal ) - return info + print(system_information) + return system_information @app.get("/limits", response_model=Thresholds) def get_limits(): + db = SessionLocal() + limits = db.query(LimitsModel).first() + if not limits: + db.close() + raise HTTPException(status_code=404, detail="Limits not found") return { - "cpu_threshold": cpu_threshold, - "ram_threshold": ram_threshold, - "disk_threshold": disk_threshold + "cpu_threshold": limits.cpu_threshold, + "ram_threshold": limits.ram_threshold, + "disk_threshold": limits.disk_threshold } @app.post("/limits") def set_limits(thresholds: Thresholds): + print (thresholds) db = SessionLocal() limits = db.query(LimitsModel).first() + cpu_threshold = thresholds.cpu_threshold + ram_threshold = thresholds.ram_threshold + disk_threshold = thresholds.disk_threshold if limits: - limits.cpu_threshold = thresholds.cpu_threshold - limits.ram_threshold = thresholds.ram_threshold - limits.disk_threshold = thresholds.disk_threshold + limits.cpu_threshold = cpu_threshold + limits.ram_threshold = ram_threshold + limits.disk_threshold = disk_threshold db.commit() - db.refresh(limits) db.close() - return { "cpu": cpu_threshold, "ram": ram_threshold, diff --git a/wall_messages.db b/wall_messages.db index a86a9ae..6b74bc4 100644 Binary files a/wall_messages.db and b/wall_messages.db differ diff --git a/webroot/index.html b/webroot/index.html index cdbe140..5eda061 100644 --- a/webroot/index.html +++ b/webroot/index.html @@ -21,20 +21,19 @@