forked from 45Drives/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_server.py
More file actions
96 lines (80 loc) · 3.46 KB
/
ui_server.py
File metadata and controls
96 lines (80 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import subprocess
import json
import plotly.graph_objs as go
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
START_TIME_FILE = "/tmp/health_check_start_time"
with open(START_TIME_FILE, "w") as f:
f.write(str(subprocess.check_output("date +%s.%N", shell=True).decode().strip()))
# Function to execute Bash script and capture JSON output
def get_system_report():
try:
result = subprocess.run(
["./health_check_script.sh"],
capture_output=True, text=True, check=True
)
data = json.loads(result.stdout.strip())
# Check if 'system' exists and is a dictionary
if not isinstance(data.get("system"), dict):
data["system"] = {}
return data
except json.JSONDecodeError as e:
print(f"JSON parsing error: {e}")
return {"system": {}}
except Exception as e:
print(f"Error executing script: {e}")
return {"system": {}}
app = dash.Dash(__name__)
# Layout
app.layout = html.Div(children=[
html.H1("System Health Dashboard", style={"textAlign": "center"}),
html.Div([
# System Info Container
html.Div(id="system-info", style={
"background-color": "#333", "color": "white", "padding": "15px", "border-radius": "5px", "width": "28%", "font-size": "18px", "marginTop": "100px", "marginRight": "20px", "display": "flex", "flexDirection": "column", "alignItems": "flex-start"
}),
# Pie Charts Container
html.Div([
dcc.Graph(id="disk-usage-pie", style={"width": "48%", "display": "inline-block"}),
dcc.Graph(id="ram-usage-pie", style={"width": "48%", "display": "inline-block"})
], style={"display": "flex", "justifyContent": "space-between", "width": "75%"})
], style={"display": "flex", "alignItems": "flex-start"}),
dcc.Interval(id="interval-component", n_intervals=0)
])
@app.callback([Output("system-info", "children"), Output("ram-usage-pie", "figure"), Output("disk-usage-pie", "figure")], Input("interval-component", "n_intervals"))
def update_info(_):
data = get_system_report()
system_data = data.get("system", {})
ram_usage = float(system_data.get("ram_usage_percent", 0))
ram_free = 100 - ram_usage
disk_usage = float(system_data.get("disk_usage_percent", 0))
disk_free = 100 - disk_usage
system_info = html.Div([
html.P(f"Filename: {data.get('filename', 'N/A')}"),
html.P(f"Tool Version: {data.get('tool_version', 'N/A')}"),
html.P(f"Platform: {data.get('platform', 'N/A')}"),
html.P(f"Duration: {data.get('duration', 'N/A')} seconds"),
html.P(f"Start Time: {data.get('start_time', 'N/A')}")
])
disk_fig = go.Figure(data=[go.Pie(
labels=["In Use", "Free"],
values=[disk_usage, disk_free],
marker=dict(colors=["#FF0000", "#00FF00"]), # Red for used, Green for free
textinfo="percent",
hoverinfo="label+percent"
)])
disk_fig.update_layout(title="Storage Usage")
# RAM Usage Pie Chart
ram_fig = go.Figure(data=[go.Pie(
labels=["In Use", "Free"],
values=[ram_usage, ram_free],
marker=dict(colors=["#FF0000", "#00FF00"]), # Red for used, Green for free
textinfo="percent",
hoverinfo="label+percent"
)])
ram_fig.update_layout(title="RAM Usage")
return system_info, ram_fig, disk_fig
# Run the Server
if __name__ == '__main__':
app.run_server(host="0.0.0.0", port=8080, debug=False)