Create ui.py
Browse files
ui.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import psutil
|
| 3 |
+
import speedtest
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Function to get CPU and memory usage
|
| 8 |
+
def get_system_metrics():
|
| 9 |
+
cpu_usage = psutil.cpu_percent(interval=1)
|
| 10 |
+
memory_info = psutil.virtual_memory()
|
| 11 |
+
memory_usage = memory_info.percent
|
| 12 |
+
return cpu_usage, memory_usage
|
| 13 |
+
|
| 14 |
+
# Function to get internet speed
|
| 15 |
+
def get_internet_speed():
|
| 16 |
+
st = speedtest.Speedtest()
|
| 17 |
+
download_speed = st.download() / 1_000_000 # Convert to Mbps
|
| 18 |
+
upload_speed = st.upload() / 1_000_000 # Convert to Mbps
|
| 19 |
+
return download_speed, upload_speed
|
| 20 |
+
|
| 21 |
+
# Function to create a gauge chart
|
| 22 |
+
def create_gauge_chart(value, title, min_value=0, max_value=100):
|
| 23 |
+
fig = go.Figure(go.Indicator(
|
| 24 |
+
mode="gauge+number",
|
| 25 |
+
value=value,
|
| 26 |
+
title={'text': title},
|
| 27 |
+
gauge={'axis': {'range': [min_value, max_value]},
|
| 28 |
+
'bar': {'color': "lightblue"},
|
| 29 |
+
'bgcolor': "white",
|
| 30 |
+
'steps': [{'range': [min_value, max_value], 'color': "lightgrey"}],
|
| 31 |
+
'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': max_value}}))
|
| 32 |
+
return fig
|
| 33 |
+
|
| 34 |
+
# Streamlit layout
|
| 35 |
+
st.title("System Monitoring Dashboard")
|
| 36 |
+
|
| 37 |
+
# Create columns for each gauge
|
| 38 |
+
col1, col2 = st.columns(2)
|
| 39 |
+
col3, col4 = st.columns(2)
|
| 40 |
+
|
| 41 |
+
# Update the metrics in real-time
|
| 42 |
+
while True:
|
| 43 |
+
cpu_usage, memory_usage = get_system_metrics()
|
| 44 |
+
download_speed, upload_speed = get_internet_speed()
|
| 45 |
+
|
| 46 |
+
# Create gauge charts
|
| 47 |
+
with col1:
|
| 48 |
+
st.plotly_chart(create_gauge_chart(cpu_usage, "CPU Usage"), use_container_width=True)
|
| 49 |
+
with col2:
|
| 50 |
+
st.plotly_chart(create_gauge_chart(memory_usage, "Memory Usage"), use_container_width=True)
|
| 51 |
+
with col3:
|
| 52 |
+
st.plotly_chart(create_gauge_chart(download_speed, "Download Speed (Mbps)", 0, 1000), use_container_width=True)
|
| 53 |
+
with col4:
|
| 54 |
+
st.plotly_chart(create_gauge_chart(upload_speed, "Upload Speed (Mbps)", 0, 1000), use_container_width=True)
|
| 55 |
+
|
| 56 |
+
# Refresh the metrics every 5 seconds
|
| 57 |
+
time.sleep(5)
|