change service monitor to send actiivity

This commit is contained in:
Luiz Costa 2024-06-05 10:31:13 +01:00
parent 661cbdf77f
commit 9196114bfb
3 changed files with 52 additions and 33 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
config/ config/
db-data/ db-data/
.vs/

View File

@ -40,6 +40,8 @@ services:
container_name: log_service container_name: log_service
build: ./docker-monitor build: ./docker-monitor
restart: unless-stopped restart: unless-stopped
environment:
- FRAPPE_URL="https://luizdev.itsdave.de"
ports: ports:
- 8085:8085 - 8085:8085
volumes: volumes:

View File

@ -1,20 +1,22 @@
import re import re
import requests
import docker import docker
import threading 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() 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 "\$([^"]+)"') start_pattern = re.compile(r'Connection ID is "\$([^"]+)"')
end_pattern = re.compile(r'Connection "\$([^"]+)" removed') 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): for log in client.containers.get('guacd').logs(stream=True):
log = log.decode('utf-8').strip() log = log.decode('utf-8').strip()
start_match = start_pattern.search(log) start_match = start_pattern.search(log)
@ -22,36 +24,50 @@ def capture_logs():
if start_match: if start_match:
session_id = start_match.group(1) session_id = start_match.group(1)
session_data["last_id"] = session_id start_time = datetime.now().isoformat()
session_data["sessions"][session_id] = {"start": log} send_start_session(session_id, start_time)
print(f"Captured Connection Start ID: {session_id}") print(f"Captured Connection Start ID: {session_id} at {start_time}")
if end_match: if end_match:
session_id = end_match.group(1) session_id = end_match.group(1)
if session_id in session_data["sessions"]: end_time = datetime.now().isoformat()
session_data["sessions"][session_id]["end"] = log send_end_session(session_id, end_time)
else: print(f"Captured Connection End ID: {session_id} at {end_time}")
session_data["sessions"][session_id] = {"end": log}
print(f"Captured Connection End ID: {session_id}")
@app.route('/last_id') def send_start_session(session_id, start_time):
def get_last_id(): data = {
if session_data["last_id"]: "session_id": session_id,
return {"last_id": session_data["last_id"]} "start_time": start_time
response.status = 404 }
return {"error": "No Connection ID found"} 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/<session_id>') def send_end_session(session_id, end_time):
def get_session(session_id): data = {
session = session_data["sessions"].get(session_id) "session_id": session_id,
if session: "end_time": end_time
return session }
response.status = 404 try:
return {"error": f"No session found with ID {session_id}"} 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__": if __name__ == "__main__":
# Inicia a captura de logs em uma thread separada # Start the log monitoring thread
threading.Thread(target=capture_logs, daemon=True).start() thread = threading.Thread(target=monitor_logs)
# Executa o servidor Bottle thread.daemon = True
run(app, host='0.0.0.0', port=8085) thread.start()
# Keep the main thread running
while True:
time.sleep(1)