forked from metafy-social/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (119 loc) · 4.1 KB
/
main.py
File metadata and controls
155 lines (119 loc) · 4.1 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
import psutil
import platform
import uuid
import re
import json
import socket
from datetime import datetime
### COMPUTER STATS CHECKER ###
uname = platform.uname()
boot_time_timestamp = psutil.boot_time() # Boot time timestamp
bt = datetime.fromtimestamp(boot_time_timestamp) # Boot time
cpufreq = psutil.cpu_freq() # Cpu
svmem = psutil.virtual_memory() # Memory
disk = psutil.disk_usage("/") # Disk
def get_size(bytes, suffix="B"):
"""
Scale bytes to its proper format
e.g:
1253656 => '1.20MB'
1253656678 => '1.17GB'
"""
factor = 1024
for unit in ["", "K", "M", "G", "T", "P"]:
if bytes < factor:
return f"{bytes:.2f}{unit}{suffix}"
bytes /= factor
data = { # Json object to store all of the data. Values can be added and removed.
"system": {
"system": uname.system,
"nodes": uname.node,
"release": uname.release,
"version": uname.version,
"machine": uname.machine,
"processor": uname.processor,
"ip adress": socket.gethostbyname(socket.gethostname()),
"mac adress": ':'.join(re.findall('..', '%012x' % uuid.getnode())),
"ram": str(round(psutil.virtual_memory().total / (1024.0 **3)))+" GB",
},
"boot_time": {
"time": f"{bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}"
},
"cpu_info": {
"physical cores": psutil.cpu_count(logical=False),
"total cores": psutil.cpu_count(logical=True),
"max frequency": f"{cpufreq.max:.2f}Mhz",
"min frequency": f"{cpufreq.min:.2f}Mhz",
"current frequency": f"{cpufreq.current:.2f}Mhz",
"total cpu usage": f"{psutil.cpu_percent()}%"
},
"memory_info": {
"total": f"{get_size(svmem.total)}",
"available": f"{get_size(svmem.available)}",
"used": f"{get_size(svmem.used)}",
"percentage": f"{svmem.percent}%"
},
"disk_info": {
"total": f"{int(disk.total / (1024.0 ** 3))} GB",
"used": f"{int(disk.used / (1024.0 ** 3))} GB",
"free": f"{int(disk.free / (1024.0 ** 3))} GB",
}
}
# All functions below do get every key and value from data object at the top and logs it to console.
def system_info(): # System Info
system = data["system"]
length = len(list(system.keys()))
keys = list(system.keys())
values = list(system.values())
print("="*40, "System Information", "="*40)
for i in range(length):
print(f"{keys[i].capitalize()}: {values[i]}")
def boot_time(): # Boot Time
time = data["boot_time"]
length = len(list(time.keys()))
keys = list(time.keys())
values = list(time.values())
print("="*40, "Boot Time", "="*40)
for i in range(length):
print(f"{keys[i].capitalize()}: {values[i]}")
def cpu_info(): # CPU Info
cpu = data["cpu_info"]
length = len(list(cpu.keys()))
keys = list(cpu.keys())
values = list(cpu.values())
print("="*40, "CPU Info", "="*40)
for i in range(length):
print(f"{keys[i].capitalize()}: {values[i]}")
def memory_info(): # Memory Info
memory = data["memory_info"]
length = len(list(memory.keys()))
keys = list(memory.keys())
values = list(memory.values())
print("="*40, "Memory Info", "="*40)
for i in range(length):
print(f"{keys[i].capitalize()}: {values[i]}")
def disk_info(): # Disk Info
disk = data["disk_info"]
length = len(list(disk.keys()))
keys = list(disk.keys())
values = list(disk.values())
print("="*40, "Disk Info", "="*40)
for i in range(length):
print(f"{keys[i].capitalize()}: {values[i]}")
def print_all(): # Calling all the functions
system_info()
cpu_info()
memory_info()
disk_info()
boot_time()
if __name__ == "__main__":
print_all()
save = input("Save the stats [y, n]: ")
if save == "y":
now = datetime.now()
data["save_date"] = str(now) # Adding save date to data
with open("./log.json","w") as file:
json.dump(data, file, ensure_ascii=False, indent=2)
print("Stats saved successfully. You can find the logs at logs.json")
else:
pass