-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat_watch.py
More file actions
33 lines (26 loc) · 825 Bytes
/
stat_watch.py
File metadata and controls
33 lines (26 loc) · 825 Bytes
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
import json
import psutil
import threading
import time
def create_payload() -> str:
return json.dumps(
{
"data": "sysstats",
"mem": psutil.virtual_memory().percent,
"cpu": psutil.cpu_percent(),
}
)
def send_payload(client, payload: str) -> None:
client.sendMessage(payload)
class StatWatcher:
def __init__(self, authed_clients: dict) -> None:
self.authed_clients = authed_clients
self.thread = threading.Thread(target=self.loop, daemon=True)
self.thread.start()
def loop(self) -> None:
while True:
if self.authed_clients:
payload = create_payload()
for client in self.authed_clients.values():
send_payload(client, payload)
time.sleep(15)