-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
123 lines (99 loc) · 3.28 KB
/
app.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from fastapi import FastAPI, Depends, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
import os
import psutil
app = FastAPI()
origin = ['*']
app.add_middleware(
CORSMiddleware,
allow_origins=origin,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*']
)
@app.get('/firstAPI')
def fastapi_url():
return {'message': 'This is first API'}
current_dir = os.path.dirname(os.path.realpath(__file__))
templates = Jinja2Templates(directory=os.path.join(current_dir, "frontend", "templates"))
@app.get("/", response_class=HTMLResponse)
@app.get("/health_page", response_class=HTMLResponse)
def get_health(request: Request):
context = {"request": request}
return templates.TemplateResponse("health.html", context)
def getSystemHealth():
# CPU Usage
cpu_usage = psutil.cpu_percent(interval=1)
# Memory Usage
memory_usage = psutil.virtual_memory().percent
# Disk Usage
disk_usage = psutil.disk_usage('/').percent
# Network Usage
net_info = psutil.net_io_counters()
bytes_sent = net_info.bytes_sent
bytes_received = net_info.bytes_recv
# Battery Information (if applicable)
try:
battery_info = psutil.sensors_battery()
battery_percent = battery_info.percent
power_plugged = battery_info.power_plugged
# Sensor Information (Temperature, Voltage, Fans)
sensor_info = psutil.sensors_temperatures()
except AttributeError:
battery_percent = None
power_plugged = None
sensor_info = None
# System Uptime
uptime = psutil.boot_time()
# Disk Partition Information
partitions = psutil.disk_partitions()
disk_partitions = {}
for partition in partitions:
try:
usage = psutil.disk_usage(partition.mountpoint)
disk_partitions[partition.device] = {
"total": usage.total,
"used": usage.used,
"free": usage.free
}
except PermissionError:
# Handle the case where the disk is not ready or accessible
disk_partitions[partition.device] = {
"total": None,
"used": None,
"free": None
}
swap_info = psutil.swap_memory()
num_logical_cores = psutil.cpu_count()
num_physical_cores = psutil.cpu_count(logical=False)
system_health = {
"cpu_usage": cpu_usage,
"memory_usage": memory_usage,
"disk_usage": disk_usage,
"network": {
"bytes_sent": bytes_sent,
"bytes_received": bytes_received
},
"battery": {
"percent": battery_percent,
"power_plugged": power_plugged
},
"sensors": sensor_info,
"uptime": uptime,
"disk_partitions": disk_partitions,
"swap_memory": {
"total": swap_info.total,
"used": swap_info.used,
"free": swap_info.free
},
"cpu_cores": {
"logical": num_logical_cores,
"physical": num_physical_cores
}
}
return system_health
@app.get('/health', response_model=dict)
def health_check(system_health: dict = Depends(getSystemHealth)):
return system_health