-
Notifications
You must be signed in to change notification settings - Fork 3
/
PsutilSensor.py
63 lines (56 loc) · 1.66 KB
/
PsutilSensor.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
import psutil
import numpy as np
import datetime
import time
import math
# cpu
def getCpuUsage():
# cpu usage(single)
cpuUsage = psutil.cpu_percent(interval=1, percpu=False)
# cpu usage free
# cpuUsageFree = psutil.cpu_times_percent().idle
return cpuUsage
# Only on Linux
def getCpuTemp():
# cpu temp
try:
cpuTemp = psutil.sensors_temperatures()['coretemp'][0].current
except AttributeError:
cpuTemp = -1000
return cpuTemp
def getCpuFreq(mode):
if(mode == 0):
return psutil.cpu_freq(percpu=False).current
elif(mode == 1):
return psutil.cpu_freq(percpu=False).min
elif(mode == 2):
return psutil.cpu_freq(percpu=False).max
# mem
def getMemUsage():
return psutil.virtual_memory().percent
# uptime
def getUptime():
now = time.time()
rawUptime = math.floor(now - psutil.boot_time())
return datetime.timedelta(seconds=rawUptime)
# info
def getComputerInfo():
data = {}
cpuCore = psutil.cpu_count(logical=False)
cpuThread = psutil.cpu_count(logical=True)
memTotal = psutil.virtual_memory().total/1024/1024
data["cpuCore"] = cpuCore
data["cpuThread"] = cpuThread
data["memTotal"] = memTotal
return data
def getAllInfo():
uptime = getUptime().seconds
hr = int(uptime / 3600)
min = int((uptime % 3600) / 60)
sec = uptime % 60
msg = f"CPU 使用率: {np.round(getCpuUsage(), 1)}%\n"
msg += f"CPU 溫度: {np.round(getCpuTemp(), 1)} ºC\n"
msg += f"CPU 頻率: {int(getCpuFreq(0))} Mhz\n"
msg += f"RAM 使用率: {np.round(getMemUsage(), 1)}%\n"
msg += f"開機時間: {getUptime().days}天 {hr}小時 {min}分 {sec}秒"
return msg