Repository navigation
Expand file tree
/
Copy pathsystem_diagnostic_report.py
More file actions
217 lines (170 loc) · 8.78 KB
/
Copy pathsystem_diagnostic_report.py
File metadata and controls
217 lines (170 loc) · 8.78 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import psutil
def main():
cpu_usage, ram_usage, disk_usage = get_system_stats()
#cpu_usage, ram_usage, disk_usage = 91, 92, 93
cpu_performance, ram_performance, disk_performance = analyze_performance(
cpu_usage, ram_usage, disk_usage
)
assessment = overall_assessment(cpu_performance, ram_performance, disk_performance)
# use for debugging: "✓ NORMAL" "⚠ WARNING" "✗ CRITICAL"
cpu_recommendation, ram_recommendation, disk_recommendation = (
generate_recommendations(cpu_performance, ram_performance, disk_performance)
)
display_report(
cpu_usage,
ram_usage,
disk_usage,
cpu_performance,
ram_performance,
disk_performance,
cpu_recommendation,
ram_recommendation,
disk_recommendation,
assessment,
)
def get_system_stats():
cpu_usage = psutil.cpu_percent(interval=1)
ram_usage = psutil.virtual_memory().percent
disk_usage = psutil.disk_usage("/").percent
return cpu_usage, ram_usage, disk_usage
def analyze_performance(cpu_usage, ram_usage, disk_usage):
cpu_performance = analyzer(cpu_usage)
ram_performance = analyzer(ram_usage)
disk_performance = analyzer(disk_usage)
return cpu_performance, ram_performance, disk_performance
def analyzer(usage):
if usage < 70:
return "✓ NORMAL"
elif usage < 90:
return "⚠ WARNING"
else:
return "✗ CRITICAL"
def overall_assessment(cpu_performance, ram_performance, disk_performance):
problems = []
# CPU problems
if cpu_performance == "⚠ WARNING":
problems.append("high CPU usage")
elif cpu_performance == "✗ CRITICAL":
problems.append("critically high CPU usage")
# RAM problems
if ram_performance == "⚠ WARNING":
problems.append("high memory usage")
elif ram_performance == "✗ CRITICAL":
problems.append("critically high memory usage")
# DISK problems
if disk_performance == "⚠ WARNING":
problems.append("low available disk space")
elif disk_performance == "✗ CRITICAL":
problems.append("critically low disk space")
# No problems
if not problems:
return "No major resource bottlenecks detected. Your system resources are currently within normal ranges."
# One problem
if len(problems) == 1:
return f"Your system may be experiencing performance issues primarily due to {problems[0]}."
# Multiple problems
elif len(problems) == 2:
return f"Your system may be experiencing performance issues due to {problems[0]} and {problems[1]}."
else:
return f"Your system may be experiencing performance issues due to {problems[0]}, {problems[1]} and {problems[2]}."
def generate_recommendations(cpu_performance, ram_performance, disk_performance):
cpu_recommendation = generate_cpu_recommendations(cpu_performance)
ram_recommendation = generate_ram_recommendations(ram_performance)
disk_recommendation = generate_disk_recommendations(disk_performance)
return cpu_recommendation, ram_recommendation, disk_recommendation
def generate_cpu_recommendations(cpu_performance):
if cpu_performance == "⚠ WARNING":
return (
"CPU usage is high. Close unnecessary applications or background processes."
)
elif cpu_performance == "✗ CRITICAL":
return "CPU usage is critically high. Close resource-intensive applications and check for processes consuming excessive CPU."
def generate_ram_recommendations(ram_performance):
if ram_performance == "⚠ WARNING":
return "Memory usage is high. Close unnecessary applications and browser tabs to free up RAM."
elif ram_performance == "✗ CRITICAL":
return "Memory usage is critically high. Close memory-intensive applications and unnecessary browser tabs to free up RAM."
def generate_disk_recommendations(disk_performance):
if disk_performance == "⚠ WARNING":
return "Disk space is getting low. Consider removing unnecessary files or uninstalling unused applications."
elif disk_performance == "✗ CRITICAL":
return "Disk space is critically low. Free up storage by removing unnecessary files and applications."
def display_report(
cpu_usage,
ram_usage,
disk_usage,
cpu_performance,
ram_performance,
disk_performance,
cpu_recommendation,
ram_recommendation,
disk_recommendation,
assessment,
):
print("""
╔═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ SYSTEM DIAGNOSTIC REPORT ║
╚═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
""")
print("""
RESOURCE STATUS
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
""")
print(
f"CPU {cpu_usage}% {cpu_performance}\n"
f"RAM {ram_usage}% {ram_performance}\n"
f"Disk {disk_usage}% {disk_performance}"
)
print("""
LIKELY PERFORMANCE ISSUES
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
""")
if cpu_performance == "⚠ WARNING":
print("• CPU usage is high.")
elif cpu_performance == "✗ CRITICAL":
print("• CPU usage is critically high.")
if ram_performance == "⚠ WARNING":
print("• Memory usage is high.")
elif ram_performance == "✗ CRITICAL":
print("• Memory usage is critically high.")
if disk_performance == "⚠ WARNING":
print("• Disk space is getting low.")
elif disk_performance == "✗ CRITICAL":
print("• Disk space is critically low.")
if (
cpu_performance == "✓ NORMAL"
and ram_performance == "✓ NORMAL"
and disk_performance == "✓ NORMAL"
):
print("• No major performance issues detected.")
print("""
RECOMMENDATIONS
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
""")
if cpu_recommendation:
print(f"→ {cpu_recommendation}")
if ram_recommendation:
print(f"→ {ram_recommendation}")
if disk_recommendation:
print(f"→ {disk_recommendation}")
if not any([
cpu_recommendation,
ram_recommendation,
disk_recommendation
]):
print("→ No recommendations. Your system resources are within normal ranges.")
print("""
OVERALL ASSESSMENT
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
""")
if "No major" in assessment:
print(f"✓ {assessment}")
else:
print(f"⚠ {assessment}")
print("""
=======================================================================================================================
END OF REPORT
=======================================================================================================================
""")
if __name__ == "__main__":
main()