RC: (update) script to save load history

This commit is contained in:
2026-03-31 15:06:30 +00:00
parent f5dea6770e
commit bb3ce618ca

View File

@@ -9,7 +9,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
# --- CONFIGURATION --- # --- CONFIGURATION ---
PORT = 8080 PORT = 8080
IFACE = "vmbr0" IFACE = "bond0"
ENABLE_PVE_STATS = True ENABLE_PVE_STATS = True
# --------------------- # ---------------------
@@ -18,6 +18,7 @@ hist_cpu = deque([0]*60, maxlen=60)
hist_ram = deque([0]*60, maxlen=60) hist_ram = deque([0]*60, maxlen=60)
hist_rx = deque([0.0]*60, maxlen=60) hist_rx = deque([0.0]*60, maxlen=60)
hist_tx = deque([0.0]*60, maxlen=60) hist_tx = deque([0.0]*60, maxlen=60)
hist_load = deque([0.0]*60, maxlen=60)
# Dictionary to hold the most recent slow-changing data # Dictionary to hold the most recent slow-changing data
latest_state = {} latest_state = {}
@@ -42,7 +43,7 @@ def get_cpu_times():
def background_poller(): def background_poller():
"""Runs in the background, gathering data every 1 second.""" """Runs in the background, gathering data every 1 second."""
global latest_state global latest_state
# Baseline for 1-second delta calculations # Baseline for 1-second delta calculations
prev_idle, prev_total = get_cpu_times() prev_idle, prev_total = get_cpu_times()
rx_prev = int(run_cmd(f"cat /sys/class/net/{IFACE}/statistics/rx_bytes") or 0) rx_prev = int(run_cmd(f"cat /sys/class/net/{IFACE}/statistics/rx_bytes") or 0)
@@ -60,10 +61,9 @@ def background_poller():
hist_cpu.append(round(cpu_pct, 1)) hist_cpu.append(round(cpu_pct, 1))
prev_idle, prev_total = idle, total prev_idle, prev_total = idle, total
# 2. Network Delta Calculation (Changed to Mbps) # 2. Network Delta Calculation
rx_now = int(run_cmd(f"cat /sys/class/net/{IFACE}/statistics/rx_bytes") or 0) rx_now = int(run_cmd(f"cat /sys/class/net/{IFACE}/statistics/rx_bytes") or 0)
tx_now = int(run_cmd(f"cat /sys/class/net/{IFACE}/statistics/tx_bytes") or 0) tx_now = int(run_cmd(f"cat /sys/class/net/{IFACE}/statistics/tx_bytes") or 0)
# Multiply by 8 for bits, divide by 1,000,000 for Megabits # Multiply by 8 for bits, divide by 1,000,000 for Megabits
hist_rx.append(round(((rx_now - rx_prev) * 8) / 1000000, 2)) hist_rx.append(round(((rx_now - rx_prev) * 8) / 1000000, 2))
hist_tx.append(round(((tx_now - tx_prev) * 8) / 1000000, 2)) hist_tx.append(round(((tx_now - tx_prev) * 8) / 1000000, 2))
@@ -81,7 +81,12 @@ def background_poller():
"total_gb": round(ram_total / (1024**3), 1) "total_gb": round(ram_total / (1024**3), 1)
} }
# 4. Storage & System Health # 4. System Load (ADD THIS BLOCK)
load1, load5, load15 = os.getloadavg()
hist_load.append(round(load1, 2))
latest_state["load_avg"] = [round(load1, 2), round(load5, 2), round(load15, 2)]
# 5. Storage & System Health
st = os.statvfs('/') st = os.statvfs('/')
latest_state["root_disk_pct"] = round((1.0 - (st.f_bavail / st.f_blocks)) * 100.0, 1) latest_state["root_disk_pct"] = round((1.0 - (st.f_bavail / st.f_blocks)) * 100.0, 1)
@@ -105,9 +110,11 @@ class MetricsHandler(BaseHTTPRequestHandler):
data["cpu"] = hist_cpu[-1] if hist_cpu else 0 data["cpu"] = hist_cpu[-1] if hist_cpu else 0
data["rx_mb"] = hist_rx[-1] if hist_rx else 0 data["rx_mb"] = hist_rx[-1] if hist_rx else 0
data["tx_mb"] = hist_tx[-1] if hist_tx else 0 data["tx_mb"] = hist_tx[-1] if hist_tx else 0
data["cpu_hist"] = list(hist_cpu) data["cpu_hist"] = list(hist_cpu)
data["ram_hist"] = list(hist_ram) data["ram_hist"] = list(hist_ram)
data["load_hist"] = list(hist_load)
data["rx_hist"] = list(hist_rx) data["rx_hist"] = list(hist_rx)
data["tx_hist"] = list(hist_tx) data["tx_hist"] = list(hist_tx)
@@ -119,7 +126,7 @@ class MetricsHandler(BaseHTTPRequestHandler):
if __name__ == '__main__': if __name__ == '__main__':
# Start the data collector in the background # Start the data collector in the background
threading.Thread(target=background_poller, daemon=True).start() threading.Thread(target=background_poller, daemon=True).start()
server = HTTPServer(('0.0.0.0', PORT), MetricsHandler) server = HTTPServer(('0.0.0.0', PORT), MetricsHandler)
print(f"Starting API on port {PORT}...") print(f"Starting API on port {PORT}...")
server.serve_forever() server.serve_forever()