Raccordement VM Gitéa
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
from typing import Optional
|
||||
import json
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
|
||||
import pymysql
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
BASE_DIR = Path("/home/debian/Gestion_sondes")
|
||||
load_dotenv(BASE_DIR / ".env")
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -37,6 +41,10 @@ DB_CONFIG = {
|
||||
|
||||
SYNO_CHAT_WEBHOOK = env_str("SYNO_CHAT_WEBHOOK_CONNEXIONS")
|
||||
SYNO_CHAT_BOTNAME = env_str("SYNO_CHAT_BOTNAME_CONNEXIONS", "Journal Connexions")
|
||||
|
||||
SYNO_CHAT_WEBHOOK_SIMPLE = env_str("SYNO_CHAT_WEBHOOK_CONNEXIONS_SIMPLE")
|
||||
SYNO_CHAT_BOTNAME_SIMPLE = env_str("SYNO_CHAT_BOTNAME_CONNEXIONS_SIMPLE", "Connexions Simples")
|
||||
|
||||
POLL_INTERVAL = int(env_str("POLL_INTERVAL", "10"))
|
||||
|
||||
|
||||
@@ -62,12 +70,20 @@ def format_message(row: dict) -> str:
|
||||
)
|
||||
|
||||
|
||||
def format_simple_message(row: dict) -> str:
|
||||
user = row.get("NomUtilisateur", "")
|
||||
site = row.get("SiteDemande", "")
|
||||
statut = row.get("Statut", "")
|
||||
return f"[Connexion site]\n{user} -> {site} ({statut})"
|
||||
|
||||
|
||||
def send_synology_chat(message: str) -> None:
|
||||
if not SYNO_CHAT_WEBHOOK:
|
||||
raise RuntimeError("SYNO_CHAT_WEBHOOK_CONNEXIONS non configuré")
|
||||
raise RuntimeError("SYNO_CHAT_WEBHOOK_CONNEXIONS non configure")
|
||||
|
||||
syno_payload = {
|
||||
"text": message
|
||||
"text": message,
|
||||
"botName": SYNO_CHAT_BOTNAME
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
@@ -76,13 +92,37 @@ def send_synology_chat(message: str) -> None:
|
||||
timeout=10
|
||||
)
|
||||
|
||||
log.info("Synology Chat HTTP=%s body=%s", response.status_code, response.text)
|
||||
log.info("Synology Chat DETAIL HTTP=%s body=%s", response.status_code, response.text)
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
body = response.json()
|
||||
if not body.get("success", False):
|
||||
raise RuntimeError(f"Synology Chat erreur: {body}")
|
||||
raise RuntimeError(f"Synology Chat DETAIL erreur: {body}")
|
||||
|
||||
|
||||
def send_synology_chat_simple(message: str) -> None:
|
||||
if not SYNO_CHAT_WEBHOOK_SIMPLE:
|
||||
return
|
||||
|
||||
syno_payload = {
|
||||
"text": message,
|
||||
"botName": SYNO_CHAT_BOTNAME_SIMPLE
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
SYNO_CHAT_WEBHOOK_SIMPLE,
|
||||
data={"payload": json.dumps(syno_payload, ensure_ascii=False)},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
log.info("Synology Chat SIMPLE HTTP=%s body=%s", response.status_code, response.text)
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
body = response.json()
|
||||
if not body.get("success", False):
|
||||
raise RuntimeError(f"Synology Chat SIMPLE erreur: {body}")
|
||||
|
||||
|
||||
def fetch_pending_rows(conn) -> list[dict]:
|
||||
@@ -106,9 +146,6 @@ def fetch_pending_rows(conn) -> list[dict]:
|
||||
ORDER BY DateHeure ASC, Id_Journal ASC
|
||||
LIMIT 50
|
||||
"""
|
||||
print("SQL fetch_pending_rows =")
|
||||
print(sql)
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql)
|
||||
return cur.fetchall()
|
||||
@@ -116,7 +153,7 @@ def fetch_pending_rows(conn) -> list[dict]:
|
||||
|
||||
def mark_sent(conn, row_id: int) -> None:
|
||||
sql = """
|
||||
UPDATE Acces.JournalConnexions
|
||||
UPDATE `Acces`.`JournalConnexions`
|
||||
SET NotificationEnvoyee = 1,
|
||||
DateNotification = NOW(),
|
||||
ErreurNotification = NULL
|
||||
@@ -148,18 +185,26 @@ def process_once() -> None:
|
||||
|
||||
for row in rows:
|
||||
row_id = row["Id_Journal"]
|
||||
|
||||
try:
|
||||
message = format_message(row)
|
||||
send_synology_chat(message)
|
||||
detailed_message = format_message(row)
|
||||
send_synology_chat(detailed_message)
|
||||
|
||||
# Envoi simplifie uniquement pour la vraie connexion site,
|
||||
# pas pour la ligne meta Domo91 / Acces
|
||||
if row.get("SiteDemande") and row.get("DSN") != "Domo91":
|
||||
simple_message = format_simple_message(row)
|
||||
send_synology_chat_simple(simple_message)
|
||||
|
||||
mark_sent(conn, row_id)
|
||||
log.info("Notification envoyée pour Id_Journal=%s", row_id)
|
||||
log.info("Notification envoyee pour Id_Journal=%s", row_id)
|
||||
|
||||
except Exception as exc:
|
||||
log.exception("Erreur d'envoi pour Id_Journal=%s", row_id)
|
||||
try:
|
||||
mark_error(conn, row_id, str(exc))
|
||||
except Exception:
|
||||
log.exception("Impossible d'écrire ErreurNotification pour Id_Journal=%s", row_id)
|
||||
log.exception("Impossible d'ecrire ErreurNotification pour Id_Journal=%s", row_id)
|
||||
|
||||
|
||||
def main():
|
||||
@@ -170,12 +215,12 @@ def main():
|
||||
if not SYNO_CHAT_WEBHOOK:
|
||||
raise RuntimeError("SYNO_CHAT_WEBHOOK_CONNEXIONS manquant")
|
||||
|
||||
log.info("Surveillance de JournalConnexions démarrée.")
|
||||
log.info("Surveillance de JournalConnexions demarree.")
|
||||
while True:
|
||||
try:
|
||||
process_once()
|
||||
except Exception:
|
||||
log.exception("Erreur générale dans la boucle de surveillance")
|
||||
log.exception("Erreur generale dans la boucle de surveillance")
|
||||
time.sleep(POLL_INTERVAL)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user