-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
49 lines (41 loc) · 1.35 KB
/
monitor.py
File metadata and controls
49 lines (41 loc) · 1.35 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
import psycopg2
import requests
import sys
# --- CONFIGURAÇÕES DO PUSHOVER ---
TOKEN = "axphvqtz8vwidbj6n9hekoktaf31by"
USER_KEY = "u7hqtt117htcvooinga8qmfpgxtdhn"
# Garanta que o 'def' esteja em uma linha nova!
def enviar_alerta(msg):
url = "https://api.pushover.net/1/messages.json"
data = {"token": TOKEN, "user": USER_KEY, "message": msg, "title": "🚨 MONITOR INFRA"}
requests.post(url, data=data)
try:
# Conectando com o IP 127.0.0.1 (sucesso garantido!)
conn = psycopg2.connect(
dbname="postgres",
user="postgres",
host="127.0.0.1",
password="88218110"
)
cur = conn.cursor()
# Removendo o fitro (WHERE) para trazer todos
query = "SELECT nome, status, cpu_uso, ram_uso FROM servidores;"
# Query de Monitoramento
# query = #"""
# SELECT nome, status, cpu_uso, ram_uso
# FROM servidores
# WHERE status = 'offline' OR cpu_uso > 80 OR ram_uso > 80;
# """
cur.execute(query)
problemas = cur.fetchall()
if problemas:
for srv in problemas:
alerta = f"ALERTA: Servidor {srv[0]} está {srv[1]}! (CPU: {srv[2]}%, RAM: {srv[3]}%)"
print(f"Disparando: {alerta}")
enviar_alerta(alerta)
else:
print("Tudo normal no reino da Infra!")
cur.close()
conn.close()
except Exception as e:
print(f"Erro na conexão: {e}")