-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
305 lines (239 loc) · 9.93 KB
/
main.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import psutil
import platform
import argparse
import os
import time
from rich.console import Console
from rich.table import Table
try:
import GPUtil
except ImportError:
GPUtil = None
console = Console()
# Function to get OS information
def get_os_info():
table = Table(title="Operating System Information")
table.add_column("System", justify="center", style="cyan", no_wrap=True)
table.add_column("OS Release", justify="center", style="cyan")
table.add_column("OS Version", justify="center", style="magenta")
table.add_row(
platform.system(),
str(platform.release()),
str(platform.version()),
)
return table
# Function to get CPU information
def get_cpu_info():
table = Table(title="CPU Information")
table.add_column("Processor", justify="start", style="cyan", no_wrap=True)
table.add_column("Core count", justify="start", style="cyan")
table.add_column("Physical cores", justify="start", style="magenta")
table.add_column("CPU Usage (%)", justify="start", style="green")
usage_percent = psutil.cpu_percent(interval=1, percpu=False)
if usage_percent <= 25:
usage_str = f"[bold green]{usage_percent}%[/bold green]"
elif usage_percent <= 50:
usage_str = f"[bold blue]{usage_percent}%[/bold blue]"
elif usage_percent <= 75:
usage_str = f"[bold yellow]{usage_percent}%[/bold yellow]"
else:
usage_str = f"[bold red]{usage_percent}%[/bold red]"
table.add_row(
platform.processor(),
str(psutil.cpu_count(logical=True)),
str(psutil.cpu_count(logical=False)),
str(usage_str),
)
return table
# Function to get RAM information
def get_ram_info():
svmem = psutil.virtual_memory()
table = Table(title="RAM Information")
table.add_column("Total memory (GB)", justify="center", style="cyan", no_wrap=True)
table.add_column("Available memory (GB)", justify="center", style="cyan")
table.add_column("Used memory (%)", justify="center", style="magenta")
table.add_row(
str(round(svmem.total / (1024**3), 2)),
str(round(svmem.available / (1024**3), 2)),
str(svmem.percent),
)
return table
# Function to get Disk information
def get_disk_info():
partitions = psutil.disk_partitions()
table = Table(title="Disk Information")
table.add_column("Device", justify="start", style="cyan", no_wrap=True)
table.add_column("Total size (GB)", justify="start", style="cyan")
table.add_column("Used size (GB)", justify="start", style="magenta")
table.add_column("Free size (GB)", justify="start", style="green")
table.add_column("Usage (%)", justify="start")
for partition in partitions:
try:
partition_usage = psutil.disk_usage(partition.mountpoint)
device = partition.device
total = round(partition_usage.total / (1024**3), 2)
used = round(partition_usage.used / (1024**3), 2)
free = round(partition_usage.free / (1024**3), 2)
usage_percent = partition_usage.percent
if usage_percent <= 25:
usage_str = f"[bold green]{usage_percent}%[/bold green]"
elif usage_percent <= 50:
usage_str = f"[bold blue]{usage_percent}%[/bold blue]"
elif usage_percent <= 75:
usage_str = f"[bold yellow]{usage_percent}%[/bold yellow]"
else:
usage_str = f"[bold red]{usage_percent}%[/bold red]"
table.add_row(device, str(total), str(used), str(free), usage_str)
except PermissionError:
continue
return table
# Function to get GPU information
def get_gpu_info():
if GPUtil is None:
return Table(title="GPU Information - GPUtil not available")
gpu_info = GPUtil.getGPUs()
table = Table(title="GPU Information")
table.add_column("GPU", justify="start", style="cyan", no_wrap=True)
table.add_column("Memory Total (GB)", justify="start", style="cyan")
table.add_column("Memory Used (GB)", justify="start", style="magenta")
for gpu in gpu_info:
usage_percent = gpu.memoryUsed
if usage_percent <= 25:
usage_str = f"[bold green]{usage_percent}%[/bold green]"
elif usage_percent <= 50:
usage_str = f"[bold blue]{usage_percent}%[/bold blue]"
elif usage_percent <= 75:
usage_str = f"[bold yellow]{usage_percent}%[/bold yellow]"
else:
usage_str = f"[bold red]{usage_percent}%[/bold red]"
table.add_row(gpu.name, str(gpu.memoryTotal), str(usage_str))
return table
# Function to get Network interface information
def get_network_info():
network_info = {}
table = Table(title="Network Interface Information")
table.add_column("Interface", justify="start", style="cyan", no_wrap=True)
table.add_column("IP Address", justify="start", style="cyan")
for interface, addresses in psutil.net_if_addrs().items():
ip_address = ", ".join([address.address for address in addresses])
network_info[interface] = ip_address
table.add_row(interface, ip_address)
return table
# Function to get Process information
def get_process_info():
processes = []
for process in psutil.process_iter():
processes.append(
{
"PID": process.pid,
"Name": process.name(),
"CPU Usage (%)": process.cpu_percent(),
"Memory Usage (MB)": round(
process.memory_info().rss / (1024 * 1024), 2
),
}
)
table = Table(title="Process Information")
for key in processes[0].keys():
table.add_column(key, justify="start", style="cyan")
for process in processes:
table.add_row(*[str(value) for value in process.values()])
return table
# Function to get System Uptime
def get_system_uptime():
table = Table(title="System Uptime")
table.add_column("Uptime", justify="start", style="cyan")
uptime = round(time.time() - psutil.boot_time())
days, remainder = divmod(uptime, 86400)
hours, remainder = divmod(remainder, 3600)
minutes, seconds = divmod(remainder, 60)
uptime_str = f"{days} days, {hours} hours, {minutes} minutes, {seconds} seconds"
table.add_row(uptime_str)
return table
# Function to get Network Usage
def get_network_usage():
net_io = psutil.net_io_counters()
table = Table(title="Network Usage")
table.add_column("Total Bytes Sent", justify="start", style="cyan")
table.add_column("Total Bytes Received", justify="start", style="magenta")
table.add_row(str(net_io.bytes_sent), str(net_io.bytes_recv))
return table
# Function to get User Information
def get_user_info():
table = Table(title="User Information")
table.add_column("Username", justify="start", style="cyan")
table.add_column("Home Directory", justify="start", style="magenta")
username = os.getlogin()
home_directory = os.path.expanduser("~")
table.add_row(username, home_directory)
return table
# Function to get System Temperature
def get_system_temperature():
sensors = psutil.sensors_temperatures()
table = Table(title="System Temperature")
for sensor, data in sensors.items():
table.add_column(sensor, justify="start", style="cyan")
sensor_data = " | ".join([f"{item.label}: {item.current}°C" for item in data])
table.add_row(sensor_data)
return table
# Add command line argument parser
parser = argparse.ArgumentParser(description="Display system information")
parser.add_argument("--all", action="store_true", help="Display All information")
parser.add_argument("--os", action="store_true", help="Display OS information")
parser.add_argument("--cpu", action="store_true", help="Display CPU information")
parser.add_argument("--ram", action="store_true", help="Display RAM information")
parser.add_argument("--disk", action="store_true", help="Display Disk information")
parser.add_argument("--gpu", action="store_true", help="Display GPU information")
parser.add_argument(
"--network", action="store_true", help="Display Network interface information"
)
parser.add_argument(
"--process", action="store_true", help="Display Process information"
)
parser.add_argument("--uptime", action="store_true", help="Display System Uptime")
parser.add_argument(
"--network_usage", action="store_true", help="Display Network Usage"
)
parser.add_argument("--user", action="store_true", help="Display User Information")
parser.add_argument(
"--temperature", action="store_true", help="Display System Temperature"
)
args = parser.parse_args()
# Function to display system information based on user input
def display_system_info(args):
if args.all:
console.print("\n", get_os_info())
console.print("\n", get_cpu_info())
console.print("\n", get_ram_info())
console.print("\n", get_disk_info())
console.print("\n", get_gpu_info())
console.print("\n", get_network_info())
console.print("\n", get_process_info())
console.print("\n", get_system_uptime())
console.print("\n", get_network_usage())
console.print("\n", get_user_info())
console.print("\n", get_system_temperature())
if args.os:
console.print("\n", get_os_info())
if args.cpu:
console.print("\n", get_cpu_info())
if args.ram:
console.print("\n", get_ram_info())
if args.disk:
console.print("\n", get_disk_info())
if args.gpu:
console.print("\n", get_gpu_info())
if args.network:
console.print("\n", get_network_info())
if args.process:
console.print("\n", get_process_info())
if args.uptime:
console.print("\n", get_system_uptime())
if args.network_usage:
console.print("\n", get_network_usage())
if args.user:
console.print("\n", get_user_info())
if args.temperature:
console.print("\n", get_system_temperature())
if __name__ == "__main__":
display_system_info(args)