mirror of
https://github.com/itsdave-de/dockercompose-guacamole-for-msp-remoteadmin.git
synced 2025-12-17 23:53:08 -03:00
create a service to log monitor from guacd
This commit is contained in:
8
docker-monitor/Dockerfile
Normal file
8
docker-monitor/Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM python:3.8-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY log_service.py /app/log_service.py
|
||||
RUN pip install docker bottle
|
||||
|
||||
CMD ["python", "log_service.py"]
|
||||
57
docker-monitor/log_service.py
Normal file
57
docker-monitor/log_service.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import re
|
||||
import docker
|
||||
import threading
|
||||
from bottle import Bottle, run, response
|
||||
|
||||
app = Bottle()
|
||||
client = docker.from_env()
|
||||
session_data = {
|
||||
"last_id": None,
|
||||
"sessions": {}
|
||||
}
|
||||
|
||||
# Expressões regulares para capturar o início e término da conexão
|
||||
start_pattern = re.compile(r'Connection ID is "\$([^"]+)"')
|
||||
end_pattern = re.compile(r'Connection "\$([^"]+)" removed')
|
||||
|
||||
def capture_logs():
|
||||
for log in client.containers.get('guacd').logs(stream=True):
|
||||
log = log.decode('utf-8').strip()
|
||||
start_match = start_pattern.search(log)
|
||||
end_match = end_pattern.search(log)
|
||||
|
||||
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}")
|
||||
|
||||
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}")
|
||||
|
||||
@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"}
|
||||
|
||||
@app.route('/session/<session_id>')
|
||||
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}"}
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user