-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_monitor.py
More file actions
169 lines (133 loc) · 4.82 KB
/
system_monitor.py
File metadata and controls
169 lines (133 loc) · 4.82 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Company: SpudWorks.
Program Name: Live Translate.
Description: A helpful AI Assistent that act as the host device.
File: system_monitor.py
Date: 2026/01/28
Version: 0.1-2026.02.02
===============================================================================
Copyright (C) 2026 SpudWorks Labs.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
import psutil
import json
import subprocess
from datetime import datetime
def get_cpu_info():
"""
~ Get the percentage, core count and load average. ~
Return:
- dict : The CPU information.
"""
return {
"usage_pct": psutil.cpu_percent(interval=0.1),
"core_count": psutil.cpu_count(),
"load_avg": psutil.getloadavg()
}
def get_memory_info():
"""
~ Get the total, available and used percentage. ~
Return:
- dict : The memory information.
"""
return {
"total_gb": round(psutil.virtual_memory().total / (1024**3), 2),
"avail_gb": round(psutil.virtual_memory().available / (1024**3), 2),
"used_pct": psutil.virtual_memory().percent
}
def get_storage_info():
"""
~ Get the path, total GB, free GB and filesystem type. ~
Return:
- dict : The storage information.
"""
# Get root disk usage info
root_disk_usage = psutil.disk_usage('/')
fs_type = None
path = None
for part in psutil.disk_partitions(all=False):
if part.mountpoint == '/':
fs_type = part.fstype
path = part.mountpoint
break
if path is None:
path = '/'
if fs_type is None:
fs_type = "unknown"
return {
"path": path,
"total_gb": round(root_disk_usage.total / (1024**3), 2),
"free_gb": round(root_disk_usage.free / (1024**3), 2),
"fs_type": fs_type
}
def get_status(cpu_info, memory_info, storage_info):
"""
~ Get the status of the system. ~
Arguments:
- cpu_info (dict) : The CPU information.
- memory_info (dict) : The memory information.
- storage_info (dict) : The storage information.
Return:
- str : The status of the system.
"""
current_status = "online"
# ~ Check the CPU usage. ~ #
if cpu_info["usage_pct"] > 80:
current_status = "critical"
elif cpu_info["usage_pct"] > 60:
if current_status != "critical":
current_status = "warning"
# ~ Check the memory usage. ~ #
if memory_info["used_pct"] > 80:
current_status = "critical"
elif memory_info["used_pct"] > 60:
if current_status != "critical":
current_status = "warning"
# ~ Check the storage usage. ~ #
# ~ Values are in GB ~ #
if storage_info["free_gb"] < 10:
current_status = "critical"
elif storage_info["free_gb"] < 20:
if current_status != "critical":
current_status = "warning"
return current_status
def get_system_info():
"""
~ Get the information for the system. ~
Return:
- dict : The information or an error dict.
"""
try:
cpu_info = get_cpu_info()
memory_info = get_memory_info()
storage_info = get_storage_info()
status = get_status(cpu_info, memory_info, storage_info)
return {
"timestamp": datetime.now().isoformat(),
"cpu": cpu_info,
"memory": memory_info,
"storage": storage_info,
"status": status
}
except Exception as e:
return {
"timestamp": datetime.now().isoformat(),
"cpu": None,
"memory": None,
"storage": None,
"status": "error",
"error": str(e)
}
if __name__ == "__main__":
print(get_storage_info())