-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_info.py
130 lines (115 loc) · 4.33 KB
/
sys_info.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
'''
Code Author: Kingsley Biney
Email: bineykingsley36@gmail.com
'''
import psutil
class sys_info:
def __init__(self):
pass
def check_cpu_util(self):
# check cpu utilization as a percentage
cpu_utilization = psutil.cpu_percent(interval=1, percpu=True)
# get cpu stats
cpu_stats = psutil.cpu_stats()
# get cpu frequency
cpu_freqs = psutil.cpu_freq(percpu=True)
# properly handle cpu frequencies: returned in MHz
cpu_freq_list = list()
for i, cpu_freq in enumerate(cpu_freqs):
cpu_freq_dict = dict()
cpu_freq_dict.update({
"label": "Core {}".format(i),
"current_freq": cpu_freq.current,
"min_freq": cpu_freq.min,
"max_freq": cpu_freq.max
})
cpu_freq_list.append(cpu_freq_dict)
# properly handle cpu utilization percentage returns
cpu_util_list = list()
for i, cpu in enumerate(cpu_utilization):
cpu_util_dict = dict()
cpu_util_dict.update({
"label": "Core {}".format(i),
"percentage": cpu
})
cpu_util_list.append(cpu_util_dict)
# handle other cpu information
cpu_info = {
"cpu_utilization": cpu_util_list,
"cpu_frequency": cpu_freq_list,
"context_switches": cpu_stats.ctx_switches,
"interrupts": cpu_stats.interrupts,
"soft_interrupts": cpu_stats.soft_interrupts,
"system_calls": cpu_stats.syscalls
}
return cpu_info
def check_memory(self):
# get available memory
memory = psutil.virtual_memory()
div_standard = 1024 * 1024 # 1MB
available_memory = memory.available // div_standard
used_memory = memory.used // div_standard
memory_info = {
"header": "units in MB",
"results":{
"available_memory": available_memory,
"used_memory": used_memory
}
}
return memory_info
def check_disk(self):
# get disk usage
disk = psutil.disk_usage('/')
div_standard = 1024 * 1024 # 1MB
disk_available = disk.free // div_standard
disk_used = disk.used // div_standard
disk_total = disk.total // div_standard
disk_percent_usage = disk.percent
disk_info = {
"header": "units in MB",
"results":{
"disk_available": disk_available,
"disk_total": disk_total,
"disk_used": disk_used,
"disk_percent_usage": disk_percent_usage,
}
}
return disk_info
def check_temperature(self):
# get temperatures : returns a list of the coretemp
temperatures = psutil.sensors_temperatures()
# get the temperature of each of the cpus and return the information as a list of dict objects
cpu_temps = temperatures.get('coretemp')
cpu_temp_list = list()
for item in cpu_temps:
cpu_temp_dict = dict()
cpu_temp_dict.update({
"label": item.label,
"current_temp": item.current,
"high": item.high,
"critical": item.critical
})
cpu_temp_list.append(cpu_temp_dict)
return cpu_temp_list
def check_fan(self):
# get the fans
cpu_fan = psutil.sensors_fans()
if len(cpu_fan) == 0:
cpu_fan.update({"Status": "Fan Sensors are not supported by the Operating System"})
return cpu_fan
# helper method for battery uptime
def sec2hours(self, secs):
mm, ss = divmod(secs, 60)
hh, mm = divmod(mm, 60)
return "%d:%02d:%02d" % (hh, mm, ss)
def check_battery(self):
battery = psutil.sensors_battery()
if battery.power_plugged:
percentage = round(battery.percent, 2)
battery_info = {"percentage": percentage, "power_plugged": True}
else:
time_left = self.sec2hours(battery.secsleft)
percentage = round(battery.percent, 2)
battery_info = {"timeLeft": time_left, "percentage": percentage, "power_plugged":False}
return battery_info
# print(check_disk())