From 016596ccfb78be872eea4fe35a82a2d68ba4a21f Mon Sep 17 00:00:00 2001 From: Rens Pastoor <555408@student.fontys.nl> Date: Wed, 29 Oct 2025 14:31:41 +0100 Subject: [PATCH] monitoring posts to db --- Case study 1/monitoring/collect-to-db.py | 80 ++++++++++++++++++ .../{collect.py => collect-to-log.py} | 0 Case study 1/monitoring/metrics.db | Bin 0 -> 12288 bytes Case study 1/monitoring/send_metrics.py | 2 +- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 Case study 1/monitoring/collect-to-db.py rename Case study 1/monitoring/{collect.py => collect-to-log.py} (100%) create mode 100644 Case study 1/monitoring/metrics.db diff --git a/Case study 1/monitoring/collect-to-db.py b/Case study 1/monitoring/collect-to-db.py new file mode 100644 index 0000000..189f94d --- /dev/null +++ b/Case study 1/monitoring/collect-to-db.py @@ -0,0 +1,80 @@ +from flask import Flask, request, jsonify +import sqlite3 +from datetime import datetime +import os + +app = Flask(__name__) +DB_PATH = "metrics.db" + +# Creates the database table if it doesn't exist +def init_db(): + with sqlite3.connect(DB_PATH) as conn: + conn.execute(""" + CREATE TABLE IF NOT EXISTS metrics ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + hostname TEXT, + os TEXT, + cpu_percent REAL, + memory_percent REAL, + disk_usage REAL, + timestamp TEXT + ) + """) + print("Database initialized") + +# route for sending metrics +@app.route('/collect', methods=['POST']) +def collect(): + data = request.get_json() + + if not data or "hostname" not in data: + return jsonify({"error": "Invalid data"}), 400 + + timestamp = datetime.now().isoformat() + + with sqlite3.connect(DB_PATH) as conn: + conn.execute(""" + INSERT INTO metrics (hostname, os, cpu_percent, memory_percent, disk_usage, timestamp) + VALUES (?, ?, ?, ?, ?, ?) + """, ( + data.get("hostname"), + data.get("os"), + data.get("cpu_percent"), + data.get("memory_percent"), + data.get("disk_usage"), + timestamp + )) + conn.commit() + + print(f"[{timestamp}] Data received from {data.get('hostname')}") + return jsonify({"status": "stored"}), 200 + +# for posting metrics which you can view in the browser +@app.route('/metrics', methods=['GET']) +def list_metrics(): + with sqlite3.connect(DB_PATH) as conn: + cursor = conn.execute(""" + SELECT hostname, os, cpu_percent, memory_percent, disk_usage, timestamp + FROM metrics + ORDER BY id DESC + LIMIT 10 + """) + rows = cursor.fetchall() + + results = [ + { + "hostname": row[0], + "os": row[1], + "cpu_percent": row[2], + "memory_percent": row[3], + "disk_usage": row[4], + "timestamp": row[5], + } + for row in rows + ] + return jsonify(results) + +if __name__ == '__main__': + if not os.path.exists(DB_PATH): + init_db() + app.run(host='0.0.0.0', port=5000) diff --git a/Case study 1/monitoring/collect.py b/Case study 1/monitoring/collect-to-log.py similarity index 100% rename from Case study 1/monitoring/collect.py rename to Case study 1/monitoring/collect-to-log.py diff --git a/Case study 1/monitoring/metrics.db b/Case study 1/monitoring/metrics.db new file mode 100644 index 0000000000000000000000000000000000000000..77cff107da8226ea7e4a931e092dcbdb0af4938a GIT binary patch literal 12288 zcmeI&&rZTH90%}r7*P_%gN6f_9x;evLy2;cVTK_hD$YbsWX>XsjKMmhUX3T8$XD=v zcmO;896*f+FC>0{S=V)KfBEfmYqG1$suOV5@_g3{SdMHEN=cD1LWm+;M7C*A`LHw{ z$UYqZ77E#Y+gy{Yl=Vll{vmG&2tWV=5P$##AOHafKmY;|_)h{C%5ox|rj5Y5?QqfW z$TMhD@cw{z?fGHkxNhjC!A!kWHQ3zRRM&F(j@-HZvPYwdWRkufjdHp?@Eu!BI%Ouu zXkFS-e9LjgX!1id8HR=;L;x$F7Qzbv|Q#6xo+tb6`LmcVhj2rRcZs$-`7?eJ_)