From dbda0787a288f00b190714a5b6eb70de1fa6b4f7 Mon Sep 17 00:00:00 2001 From: sylvain Date: Sat, 7 Mar 2026 17:08:22 +0000 Subject: [PATCH] =?UTF-8?q?on=5Fmqtt=5Fregister=20:=20mise=20=C3=A0=20jour?= =?UTF-8?q?=20automatique=20du=20registre=20+=20distinction=20NOUVEAU/EN?= =?UTF-8?q?=20LIGNE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- agent1.py | 23 +++++++++++++++++++++-- config/agents_registry.json | 26 +++++++++++++------------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/agent1.py b/agent1.py index dea632b..831b80c 100644 --- a/agent1.py +++ b/agent1.py @@ -134,7 +134,7 @@ def on_mqtt_notification(client, userdata, msg): print("[MQTT] Erreur parsing notification scheduler : {}".format(e)) def on_mqtt_register(client, userdata, msg): - """Reçoit les déclarations de mise en ligne des agents.""" + """Reçoit les déclarations de mise en ligne des agents et met à jour le registre.""" try: data = json.loads(msg.payload.decode(errors="replace")) agent = data.get("agent", "?") @@ -142,9 +142,28 @@ def on_mqtt_register(client, userdata, msg): mqtt_inbox = data.get("mqtt_inbox", "?") speciality = data.get("speciality", "") print("[REGISTER] {} en ligne (JID: {}, inbox: {})".format(agent, jid, mqtt_inbox)) + + # Mettre à jour agents_registry.json + registry_file = CONFIG_DIR / "agents_registry.json" + try: + registry = json.loads(registry_file.read_text(encoding="utf-8")) + except Exception: + registry = {} + is_new = agent not in registry + registry[agent] = { + "jid" : jid, + "mqtt_inbox" : mqtt_inbox, + "mqtt_outbox": "agents/agent1/inbox", + "speciality" : speciality, + } + registry_file.write_text(json.dumps(registry, indent=2, ensure_ascii=False), encoding="utf-8") + if is_new: + print("[REGISTER] {} ajouté au registre.".format(agent)) + # Notifier sylvain via XMPP if xmpp_bot: - notif = "[EN LIGNE] {}\n JID : {}\n MQTT : {}".format(agent, jid, mqtt_inbox) + status = "NOUVEAU" if is_new else "EN LIGNE" + notif = "[{}] {}\n JID : {}\n MQTT : {}".format(status, agent, jid, mqtt_inbox) if speciality: notif += "\n Rôle : {}".format(speciality) xmpp_bot.send_message(mto=ADMIN_JID, mbody=notif, mtype='chat') diff --git a/config/agents_registry.json b/config/agents_registry.json index c460c68..ae2dd14 100644 --- a/config/agents_registry.json +++ b/config/agents_registry.json @@ -1,20 +1,20 @@ { "agent2_debian13": { - "jid" : "agent2_debian13@xmpp.ovh", - "mqtt_inbox" : "agents/agent2_debian13/inbox", - "mqtt_outbox" : "agents/agent1/inbox", - "speciality" : "Administration Debian : apt, dpkg, systemd, conteneurs LXC/Docker, KVM, réseau, sécurité système" + "jid": "agent2_debian13@xmpp.ovh", + "mqtt_inbox": "agents/agent2_debian13/inbox", + "mqtt_outbox": "agents/agent1/inbox", + "speciality": "Administration Debian : apt, dpkg, systemd, conteneurs LXC/Docker, KVM, réseau, sécurité système" }, "agent2_ansible": { - "jid" : "agent2_ansible@xmpp.ovh", - "mqtt_inbox" : "agents/agent2_ansible/inbox", - "mqtt_outbox" : "agents/agent1/inbox", - "speciality" : "Automatisation infrastructure via Ansible : playbooks, commandes ad-hoc, déploiement multi-hôtes, gestion de configuration sur le réseau local" + "jid": "agent2_ansible@xmpp.ovh", + "mqtt_inbox": "agents/agent2_ansible/inbox", + "mqtt_outbox": "agents/agent1/inbox", + "speciality": "Automatisation infrastructure via Ansible : playbooks, commandes ad-hoc, déploiement multi-hôtes, gestion de configuration sur le réseau local" }, "agent2_deploy": { - "jid" : "agent2_deploy@xmpp.ovh", - "mqtt_inbox" : "agents/agent2_deploy/inbox", - "mqtt_outbox" : "agents/agent1/inbox", - "speciality" : "Déploiement d'agents : installe et configure d'autres agents sur des machines distantes ou locales via SSH" + "jid": "agent2_deploy@xmpp.ovh", + "mqtt_inbox": "agents/agent2_deploy/inbox", + "mqtt_outbox": "agents/agent1/inbox", + "speciality": "Déploiement d'agents : installe et configure d'autres agents sur des machines distantes ou locales via SSH" } -} +} \ No newline at end of file