mirror of
				https://github.com/itsdave-de/dockercompose-guacamole-for-msp-remoteadmin.git
				synced 2025-11-03 17:31:01 -03:00 
			
		
		
		
	change service monitor to send actiivity
This commit is contained in:
		
							parent
							
								
									661cbdf77f
								
							
						
					
					
						commit
						9196114bfb
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,2 +1,3 @@
 | 
			
		||||
config/
 | 
			
		||||
db-data/
 | 
			
		||||
.vs/
 | 
			
		||||
@ -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:
 | 
			
		||||
 | 
			
		||||
@ -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/<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}"}
 | 
			
		||||
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)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user