-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathPsutil_Example.py
More file actions
66 lines (54 loc) · 2.19 KB
/
Psutil_Example.py
File metadata and controls
66 lines (54 loc) · 2.19 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
from plyer import notification # pip install plyer
import psutil # pip install psutil
from datetime import datetime
def TestFeature_Memory():
memory = psutil.virtual_memory()
print(f"Total Memory: {memory.total / (1024**3):.2f} GB")
print(f"Available Memory: {memory.available / (1024**3):.2f} GB")
print(f"Used Memory: {memory.used / (1024**3):.2f} GB")
print(f"Memory Usage Percent: {memory.percent}%")
def TestFeature_CPU():
# Get system-wide CPU usage over 1 second
cpu_percent = psutil.cpu_percent(interval=1)
print(f"CPU Usage Percent: {cpu_percent}%")
# Get per-core CPU usage over 1 second
for i, percentage in enumerate(psutil.cpu_percent(interval=1, percpu=True)):
print(f"Core {i}: {percentage}%")
def TestFeature_Battery():
# psutil.sensors_battery() will return the information related to battery
battery = psutil.sensors_battery()
if battery is None:
print("Battery not found")
return
# battery percent will return the current battery prcentage
percent = battery.percent
charging = battery.power_plugged
# Notification(title, description, duration)--to send
# notification to desktop
# help(Notification)
if charging:
if percent == 100:
charging_message = "Unplug your Charger"
else:
charging_message = "Charging"
else:
charging_message = "Not Charging"
message = str(percent) + "% Charged\n" + charging_message
notification.notify("Battery Information", message, timeout=10)
def TestFeature_Network():
net_io = psutil.net_io_counters()
print(f"Bytes Sent: {net_io.bytes_sent}")
print(f"Bytes Received: {net_io.bytes_recv}")
def TestFeature_BootTime():
boot_time_timestamp = psutil.boot_time()
print(f"System booted at: {datetime.fromtimestamp(boot_time_timestamp).strftime('%Y-%m-%d %H:%M:%S')}")
def TestFeature_Processes():
for proc in psutil.process_iter(['pid', 'name']):
print(f"PID: {proc.info['pid']}, Name: {proc.info['name']}")
if __name__ == "__main__":
TestFeature_CPU()
TestFeature_Memory()
TestFeature_Battery()
TestFeature_Network()
TestFeature_BootTime()
TestFeature_Processes()