diff --git a/server/requirements.txt b/server/requirements.txt index 75d7726247e87342ce334a0e012d0b859dbb53e2..c76fef11e3ab27a1821e25973382e5f995f61e68 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 076683d4ba99000c980c6115ae78999cabfeba5f..41b3973b89b6b8de220002a9bd53909c4fa1a166 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()