From 9196114bfb318cfad60529de504be3afa39f4e2e Mon Sep 17 00:00:00 2001 From: Luiz Costa Date: Wed, 5 Jun 2024 10:31:13 +0100 Subject: [PATCH] change service monitor to send actiivity --- .gitignore | 1 + docker-compose.yml | 2 + docker-monitor/log_service.py | 82 +++++++++++++++++++++-------------- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 14a49f4..f6a2eb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config/ db-data/ +.vs/ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index b08d529..6fdf8cb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,6 +40,8 @@ services: container_name: log_service build: ./docker-monitor restart: unless-stopped + environment: + - FRAPPE_URL="https://luizdev.itsdave.de" ports: - 8085:8085 volumes: diff --git a/docker-monitor/log_service.py b/docker-monitor/log_service.py index d492a56..4ec67f2 100644 --- a/docker-monitor/log_service.py +++ b/docker-monitor/log_service.py @@ -1,20 +1,22 @@ import re +import requests import docker import threading -from bottle import Bottle, run, response +import time +import os +from datetime import datetime -app = Bottle() +# Configurations for Docker client client = docker.from_env() -session_data = { - "last_id": None, - "sessions": {} -} -# Expressões regulares para capturar o início e término da conexão +# Match the connection ID from the log start_pattern = re.compile(r'Connection ID is "\$([^"]+)"') end_pattern = re.compile(r'Connection "\$([^"]+)" removed') -def capture_logs(): +# Frappe API url +FRAPPE_URL = os.getenv('FRAPPE_URL') + +def monitor_logs(): for log in client.containers.get('guacd').logs(stream=True): log = log.decode('utf-8').strip() start_match = start_pattern.search(log) @@ -22,36 +24,50 @@ def capture_logs(): if start_match: session_id = start_match.group(1) - session_data["last_id"] = session_id - session_data["sessions"][session_id] = {"start": log} - print(f"Captured Connection Start ID: {session_id}") + start_time = datetime.now().isoformat() + send_start_session(session_id, start_time) + print(f"Captured Connection Start ID: {session_id} at {start_time}") if end_match: session_id = end_match.group(1) - if session_id in session_data["sessions"]: - session_data["sessions"][session_id]["end"] = log - else: - session_data["sessions"][session_id] = {"end": log} - print(f"Captured Connection End ID: {session_id}") + end_time = datetime.now().isoformat() + send_end_session(session_id, end_time) + print(f"Captured Connection End ID: {session_id} at {end_time}") -@app.route('/last_id') -def get_last_id(): - if session_data["last_id"]: - return {"last_id": session_data["last_id"]} - response.status = 404 - return {"error": "No Connection ID found"} +def send_start_session(session_id, start_time): + data = { + "session_id": session_id, + "start_time": start_time + } + try: + response = requests.post(f"{FRAPPE_URL}/api/method/msp_remoteadmin.tools.log_start_session", json=data) + if response.status_code == 200: + print(f"Successfully sent start session data for ID: {session_id}") + else: + print(f"Failed to send start session data for ID: {session_id}, Status Code: {response.status_code}") + except Exception as e: + print(f"Error sending start session data for ID: {session_id}: {str(e)}") -@app.route('/session/') -def get_session(session_id): - session = session_data["sessions"].get(session_id) - if session: - return session - response.status = 404 - return {"error": f"No session found with ID {session_id}"} +def send_end_session(session_id, end_time): + data = { + "session_id": session_id, + "end_time": end_time + } + try: + response = requests.post(f"{FRAPPE_URL}/api/method/msp_remoteadmin.tools.log_end_session", json=data) + if response.status_code == 200: + print(f"Successfully sent end session data for ID: {session_id}") + else: + print(f"Failed to send end session data for ID: {session_id}, Status Code: {response.status_code}") + except Exception as e: + print(f"Error sending end session data for ID: {session_id}: {str(e)}") if __name__ == "__main__": - # Inicia a captura de logs em uma thread separada - threading.Thread(target=capture_logs, daemon=True).start() - # Executa o servidor Bottle - run(app, host='0.0.0.0', port=8085) + # Start the log monitoring thread + thread = threading.Thread(target=monitor_logs) + thread.daemon = True + thread.start() + # Keep the main thread running + while True: + time.sleep(1)