-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (52 loc) · 1.87 KB
/
app.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
import psutil
from flask import Flask, render_template
from flask_socketio import SocketIO
import time
from threading import Thread
app = Flask(__name__)
socketio = SocketIO(app)
def get_system_info():
while True:
cpu_percent = psutil.cpu_percent(interval=None)
memory_info = psutil.virtual_memory()
# Emitir os dados para os clientes conectados via Socket.IO
socketio.emit(
"update_data",
{"cpu_percent": cpu_percent, "memory_percent": memory_info.percent},
)
# Verificar se o uso de memória é maior ou igual a 80%
if memory_info.percent >= 85:
socketio.emit(
"memory_alert", {"message": "Alerta: Uso de memória está alto!"}
)
time.sleep(1)
# Função para obter o uso de CPU e memória em tempo real
def get_system_info():
while True:
cpu_percent = psutil.cpu_percent(interval=None)
memory_info = psutil.virtual_memory()
# Emitir os dados para os clientes conectados via Socket.IO
socketio.emit(
"update_data",
{"cpu_percent": cpu_percent, "memory_percent": memory_info.percent},
)
time.sleep(1)
# Rota inicial que renderiza a página HTML
@app.route("/")
def index():
return render_template("index.html")
# Configuração do Socket.IO para atualizações em tempo real
@socketio.on("connect")
def handle_connect():
print("Client connected")
@socketio.on("disconnect")
def handle_disconnect():
print("Client disconnected")
# Executar o servidor Flask com Socket.IO em uma thread separada
if __name__ == "__main__":
# Iniciar a função de monitoramento em uma thread separada
monitor_thread = Thread(target=get_system_info)
monitor_thread.daemon = True
monitor_thread.start()
# Iniciar o servidor Flask com Socket.IO
socketio.run(app, debug=True)