From 974e7f6669df7debf310f0106dda73d85c8cdae0 Mon Sep 17 00:00:00 2001 From: Michel Date: Sun, 26 Apr 2026 17:02:13 +0200 Subject: [PATCH] =?UTF-8?q?Raccordement=20VM=20Git=C3=A9a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 5 +- app/Monitor_connexions.py | 77 ++++++++++++++++++++----- app/{Mqtt_meudon.py => Mqtt_Meudon.py} | 2 +- requirements.txt | Bin 3000 -> 348 bytes scripts/update_gestion_sondes.sh | 12 ++++ 5 files changed, 78 insertions(+), 18 deletions(-) rename app/{Mqtt_meudon.py => Mqtt_Meudon.py} (99%) create mode 100644 scripts/update_gestion_sondes.sh diff --git a/.env b/.env index be60b3f..d8476e2 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ # connexion mysql -DB_HOST=162.19.78.131 +DB_HOST=localhost DB_USER=sondes DB_PASS=TX.)-U1!zq5Axdk4 DB_NAME=Sondes @@ -25,9 +25,12 @@ SYNO_CHAT_WEBHOOK_GYRO_MEUDON=https://mj91.fr/webapi/entry.cgi?api=SYNO.Chat.Ext SYNO_CHAT_WEBHOOK_CONNEXIONS=https://mj91.fr/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=R585242twVz04qmzukxbtSTMe7p0GgdroKtO8opBglDx3VLtaLwJhYb93btH6Hya +SYNO_CHAT_WEBHOOK_CONNEXIONS_SIMPLE=https://mj91.fr/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=TahhvDjYKqvA7KbGgVOK7uZI8ri0JS1HSPvCGGXteyZaHGKpKJGvaJS4Favf9Xyj + SYNO_CHAT_BOTNAME_MONITOR="Injection données dans tables" SYNO_CHAT_BOTNAME_GYRO="Gestion Gyro" SYNO_CHAT_BOTNAME_CONNEXIONS="Journal Connexions" +SYNO_CHAT_BOTNAME_CONNEXIONS_SIMPLE="Connexions Simples" SYNO_CHAT_TIMEOUT=10 SYNO_CHAT_VERIFY_SSL=true diff --git a/app/Monitor_connexions.py b/app/Monitor_connexions.py index 277bd7a..dfdf786 100644 --- a/app/Monitor_connexions.py +++ b/app/Monitor_connexions.py @@ -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) diff --git a/app/Mqtt_meudon.py b/app/Mqtt_Meudon.py similarity index 99% rename from app/Mqtt_meudon.py rename to app/Mqtt_Meudon.py index 97351bf..d7ba5b8 100644 --- a/app/Mqtt_meudon.py +++ b/app/Mqtt_Meudon.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 """ -Mqtt_meudon.py (version nettoyée) +Mqtt_Meudon.py (version nettoyée) -------------------------------- - S'abonne à Meudon/# sur le broker MQTT - Parse les messages (topic -> nom de sonde, payload -> température) diff --git a/requirements.txt b/requirements.txt index 9c76af0f596aeea5978315b8c34aff018f9f21f3..0d592f6d2c805b57a0c373674b6f0d9ab113ce94 100644 GIT binary patch delta 166 zcmdlXeus(a|G&v^IHj0P8O$axWR+AkX3%3W0AeEsUIs3PJcd$+T!sRMN(NgXGy;m7 zfbisc7E5L`2EEDp9GZ+qlg(MQT`j@tix^UYs`D9&7)ls&7!nzh!1};?fcgx9CWB1P f2dYU0nhi3y0;mpTHppJX$-JDB0x+A5Az};w%{Uy) literal 3000 zcmZveUvC;o5X9&Cqpb*;=@xh+>U|5)~qhtqTAoh$E^PH*&C*DQ&05(wsT zx>uy79;%oqyOEG@Qr=g;<-vnIu3}tmH_Am7hyyOT<8@BtZFvbJPzGw6dorxA;oXEozSU{Rjru?`QS&~3+92rV(X^5bBoUmpVj{O@Yy zJZBv&b>(`v@235k0-O%Ds%p@IYSkuoOR7CSju{*mm95%G%3%88%{O<<#{#tpAAJ zUP=N5b;B?j;1I8s&UfOc*^CHv{?I8DijB-XwtmsMu6)-y*>rcPhb^rZQ3GwLW0K4n*(;DmrQG zUzKmCsvm^nU;TSo>o@A`3KjM8AY!lYRqcU!AgZI?iT!=KA1Df&yVq*Xs?7-uJ*YN}2v_nh;7S_$FipiX>S-n}BVs=?H@KN>}!>#7fLUL(WL^WsBv*y${KxKv%<4%`5f(5vScmCS^}?11t8AU?L8K8XO7L~j|` zoMwal=ey9oO@^PvKJ(~Z&smh16+Fi@!94nc4aYDF&?cX6d5)t0Y?-}O;3?ov&3S6~ zneP_1+26|amomW@^&aQQTH1_fPds(QsTzf@*y<>pUZujQkV>~=j@nucIgy+_z7fFU z$GvBIUuwkMSdCM*RVOv({zMyjuG!$ESIwx+9R?NsaW&jIY$*PA@@-Y)cNSW3PCV{a zCvo~3%W?SjtiSddslwl+4Yy5|x4=T#b6VhuIL7984>s=cEqM`n z+zYtbtG=g0-~AS26nkwDcJel?Rz!=AqON4+X6MFCh~Msc;hD<;2*v}jH3cq>AlWAMmd~iMtmnyo%`$#{-<0|`h~7ib-VrWA4k@V`chTw zJGM=X?Yv7p+uvfBQ diff --git a/scripts/update_gestion_sondes.sh b/scripts/update_gestion_sondes.sh new file mode 100644 index 0000000..ce13d67 --- /dev/null +++ b/scripts/update_gestion_sondes.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +cd /home/domo91/Gestion_sondes || exit 1 + +echo "Mise à jour depuis Gitea..." +git pull --ff-only origin master + +echo "Redémarrage de l'application..." +sudo supervisorctl restart Interface + +echo "Statut Supervisor :" +sudo supervisorctl status \ No newline at end of file