From bbbaf328efa7b0e28870f1855c2571fcce328b79 Mon Sep 17 00:00:00 2001 From: TheJoeCoder <joe@radialbog9.uk> Date: Thu, 11 Jul 2024 19:03:49 +0100 Subject: [PATCH] Finish server sorta --- server/requirements.txt | 3 ++- server/sysmonserver.py | 55 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/server/requirements.txt b/server/requirements.txt index 75d7726..c76fef1 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,2 +1,3 @@ psutil~=6.0.0 -flask~=3.0.3 \ No newline at end of file +flask~=3.0.3 +humanize~=4.10.0 \ No newline at end of file diff --git a/server/sysmonserver.py b/server/sysmonserver.py index 076683d..41b3973 100644 --- a/server/sysmonserver.py +++ b/server/sysmonserver.py @@ -1,4 +1,5 @@ import psutil +import humanize from flask import Flask app = Flask(__name__) @@ -8,6 +9,7 @@ app = Flask(__name__) def cpu_usage(): return {"usage": psutil.cpu_percent()} + @app.route("/cpu/freq") def cpu_frequency(): cpuf = psutil.cpu_freq() @@ -18,5 +20,56 @@ def cpu_frequency(): } +@app.route("/ram/physical") +def pmem_usage(): + ram = psutil.virtual_memory() + return { + "percent": ram.percent, + "total": ram.total, + "free": ram.free, + "used": ram.used, + "available": ram.available, + "total_fancy": humanize.naturalsize(ram.total, binary=True), + "free_fancy": humanize.naturalsize(ram.free, binary=True), + "used_fancy": humanize.naturalsize(ram.used, binary=True), + "available_fancy": humanize.naturalsize(ram.available, binary=True) + } + + +@app.route("/ram/swap") +def vmem_usage(): + swap = psutil.swap_memory() + return { + "percent": swap.percent, + "total": swap.total, + "free": swap.free, + "used": swap.used, + "total_fancy": humanize.naturalsize(swap.total, binary=True), + "free_fancy": humanize.naturalsize(swap.free, binary=True), + "used_fancy": humanize.naturalsize(swap.used, binary=True) + } + + +if hasattr(psutil, "sensors_temperatures"): + @app.route("/sensors/coretemp") + def temperature_sensor(): + sensors = psutil.sensors_temperatures() + cpusense = sensors.get("coretemp", {}) + # Work out average + temp = 0 + for t in cpusense: + temp += t.current + temp /= len(cpusense) + return { + "temperature": temp + } + +else: + @app.route("/sensors/coretemp") + def temperature_sensor(): + return { + "error": "Not supported on this OS" + }, 401 + if __name__ == "__main__": - app.run() \ No newline at end of file + app.run() -- GitLab