#!/bin/bash # Calejo Control Adapter - Monitoring Setup Script # This script sets up Prometheus authentication and Grafana auto-configuration set -e echo "🚀 Setting up Calejo Control Adapter Monitoring..." # Load environment variables if [ -f ".env" ]; then echo "Loading environment variables from .env file..." export $(grep -v '^#' .env | xargs) fi # Set default values if not provided PROMETHEUS_USERNAME=${PROMETHEUS_USERNAME:-prometheus_user} PROMETHEUS_PASSWORD=${PROMETHEUS_PASSWORD:-prometheus_password} # Generate Prometheus password hash if needed echo "🔐 Setting up Prometheus authentication..." if [ ! -f "./monitoring/web.yml" ]; then echo "Generating Prometheus web configuration..." # Generate password hash using htpasswd PASSWORD_HASH=$(echo "$PROMETHEUS_PASSWORD" | docker run --rm -i prom/prometheus:latest htpasswd -niB "$PROMETHEUS_USERNAME" 2>/dev/null || echo "$2y$10$8J8J8J8J8J8J8J8J8J8J8u8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8") cat > ./monitoring/web.yml << EOF # Prometheus web configuration with basic authentication basic_auth_users: $PROMETHEUS_USERNAME: $PASSWORD_HASH EOF echo "Prometheus web configuration created!" echo "⚠️ Note: web.yml contains password hash and should not be committed to git" fi # Update Grafana datasource configuration echo "📊 Configuring Grafana datasource..." cat > ./monitoring/grafana/datasources/prometheus.yml << 'EOF' apiVersion: 1 datasources: - name: Prometheus type: prometheus access: proxy url: http://prometheus:9090 isDefault: true editable: true # Basic authentication configuration basicAuth: true basicAuthUser: prometheus_user basicAuthPassword: prometheus_password EOF echo "Grafana datasource configuration updated!" # Create dashboard provisioning echo "📈 Setting up Grafana dashboards..." if [ ! -d "./monitoring/grafana/dashboards" ]; then mkdir -p ./monitoring/grafana/dashboards fi # Create dashboard provisioning configuration cat > ./monitoring/grafana/dashboards/dashboard.yml << 'EOF' apiVersion: 1 providers: - name: 'default' orgId: 1 folder: '' type: file disableDeletion: false updateIntervalSeconds: 10 allowUiUpdates: true options: path: /var/lib/grafana/dashboards EOF echo "✅ Monitoring setup completed!" echo "" echo "📋 Summary:" echo " - Prometheus: Configured with basic auth ($PROMETHEUS_USERNAME/********)" echo " - Grafana: Auto-configured to connect to Prometheus with authentication" echo " - Access URLs:" echo " - Grafana: http://localhost:3000 (admin/admin)" echo " - Prometheus: http://localhost:9091 ($PROMETHEUS_USERNAME/********)" echo "" echo "🚀 To start the monitoring stack:" echo " docker-compose up -d prometheus grafana" echo "" echo "🔧 To manually configure Grafana if needed:" echo " ./monitoring/grafana/configure-grafana.sh"