diff --git a/consul-monitor/Dockerfile b/consul-monitor/Dockerfile index 0c5adcb..ce05788 100644 --- a/consul-monitor/Dockerfile +++ b/consul-monitor/Dockerfile @@ -6,13 +6,25 @@ WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt +# Install SQLite for debugging +RUN apt-get update && apt-get install -y sqlite3 + # Copy application COPY . . -# Create non-root user -RUN useradd -m appuser && chown -R appuser:appuser /app +# Create non-root user and ensure data directory exists +RUN mkdir -p /data && \ + useradd -m appuser && \ + chown -R appuser:appuser /app /data + USER appuser +# Set working directory to application code +WORKDIR /app + +# Create data directory for database +RUN mkdir -p /data && chown appuser:appuser /data + # Expose port EXPOSE 5000 diff --git a/consul-monitor/app.py b/consul-monitor/app.py index 0452c00..d16cce6 100644 --- a/consul-monitor/app.py +++ b/consul-monitor/app.py @@ -13,7 +13,6 @@ def get_db(): """Get a thread-local database connection""" if 'db_conn' not in g: g.db_conn = database.init_database() - database.create_tables(g.db_conn) return g.db_conn @app.teardown_appcontext @@ -222,6 +221,30 @@ def aggregate_health_data(raw_history, granularity_minutes): return chart_data +@app.route('/api/debug/db') +def debug_db(): + """Debug endpoint to inspect database contents""" + db_conn = get_db() + cursor = db_conn.cursor() + + # Get services + cursor.execute("SELECT * FROM services") + services = cursor.fetchall() + services = [dict(id=row[0], name=row[1], address=row[2], port=row[3], + tags=json.loads(row[4]), meta=json.loads(row[5]), + first_seen=row[6], last_seen=row[7]) for row in services] + + # Get health checks + cursor.execute("SELECT * FROM health_checks") + health_checks = cursor.fetchall() + health_checks = [dict(id=row[0], service_id=row[1], check_name=row[2], + status=row[3], timestamp=row[4]) for row in health_checks] + + return jsonify({ + 'services': services, + 'health_checks': health_checks + }) + @app.route('/health') def health_check(): """Health check endpoint""" diff --git a/consul-monitor/background_poller.py b/consul-monitor/background_poller.py index f5802dd..879cbb8 100644 --- a/consul-monitor/background_poller.py +++ b/consul-monitor/background_poller.py @@ -67,6 +67,11 @@ class ConsulPoller: if not service_data: logger.warning("No service data received from Consul") return + + # Detailed logging of service data + logger.info(f"Received {len(service_data)} services from Consul") + for service_id, data in list(service_data.items())[:5]: # Log first 5 services + logger.info(f"Service: {service_id}, Name: {data['name']}, Checks: {len(data['health_checks'])}") # Get database connection conn = self.get_db_conn() diff --git a/consul-monitor/database.py b/consul-monitor/database.py index d4c8142..78200d4 100644 --- a/consul-monitor/database.py +++ b/consul-monitor/database.py @@ -44,8 +44,10 @@ def create_tables(conn): conn.commit() def init_database(): - """Initialize database and return connection""" - return sqlite3.connect('file:consul-monitor.db?mode=memory&cache=shared', uri=True) + """Initialize database, create tables, and return connection""" + conn = sqlite3.connect('/data/consul-monitor.db') + create_tables(conn) + return conn def upsert_service(conn, service_data): cursor = conn.cursor()