diff --git a/docker-compose.yml b/docker-compose.yml index 44977c9..c8b833e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -53,7 +53,7 @@ services: - "9091:9090" volumes: - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml - - ./monitoring/prometheus-web.yml:/etc/prometheus/web.yml + - ./monitoring/web.yml:/etc/prometheus/web.yml - ./monitoring/alert_rules.yml:/etc/prometheus/alert_rules.yml - prometheus_data:/prometheus command: @@ -76,6 +76,10 @@ services: environment: - GF_SECURITY_ADMIN_PASSWORD=admin - GF_USERS_ALLOW_SIGN_UP=false + # Prometheus datasource configuration + - PROMETHEUS_AUTH_ENABLED=true + - PROMETHEUS_USERNAME=prometheus_user + - PROMETHEUS_PASSWORD=prometheus_password volumes: - grafana_data:/var/lib/grafana - ./monitoring/grafana/dashboards:/var/lib/grafana/dashboards diff --git a/monitoring/grafana/configure-grafana.sh b/monitoring/grafana/configure-grafana.sh new file mode 100755 index 0000000..156129d --- /dev/null +++ b/monitoring/grafana/configure-grafana.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +# Grafana Auto-Configuration Script for Prometheus Datasource +# This script ensures Grafana is properly configured to connect to Prometheus + +set -e + +# Default values +GRAFANA_URL="http://localhost:3000" +GRAFANA_USER="admin" +GRAFANA_PASSWORD="admin" +PROMETHEUS_URL="http://prometheus:9090" +PROMETHEUS_USER="prometheus_user" +PROMETHEUS_PASSWORD="prometheus_password" + +# Wait for Grafana to be ready +echo "Waiting for Grafana to be ready..." +until curl -s "${GRAFANA_URL}/api/health" | grep -q '"database":"ok"'; do + sleep 5 +done +echo "Grafana is ready!" + +# Check if Prometheus datasource already exists +echo "Checking for existing Prometheus datasource..." +DATASOURCES=$(curl -s -u "${GRAFANA_USER}:${GRAFANA_PASSWORD}" "${GRAFANA_URL}/api/datasources") + +if echo "$DATASOURCES" | grep -q '"name":"Prometheus"'; then + echo "Prometheus datasource already exists. Updating configuration..." + + # Get the datasource ID + DATASOURCE_ID=$(echo "$DATASOURCES" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2) + + # Update the datasource + curl -s -X PUT "${GRAFANA_URL}/api/datasources/${DATASOURCE_ID}" \ + -u "${GRAFANA_USER}:${GRAFANA_PASSWORD}" \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Prometheus\", + \"type\": \"prometheus\", + \"url\": \"${PROMETHEUS_URL}\", + \"access\": \"proxy\", + \"basicAuth\": true, + \"basicAuthUser\": \"${PROMETHEUS_USER}\", + \"basicAuthPassword\": \"${PROMETHEUS_PASSWORD}\", + \"isDefault\": true + }" + + echo "Prometheus datasource updated successfully!" +else + echo "Creating Prometheus datasource..." + + # Create the datasource + curl -s -X POST "${GRAFANA_URL}/api/datasources" \ + -u "${GRAFANA_USER}:${GRAFANA_PASSWORD}" \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Prometheus\", + \"type\": \"prometheus\", + \"url\": \"${PROMETHEUS_URL}\", + \"access\": \"proxy\", + \"basicAuth\": true, + \"basicAuthUser\": \"${PROMETHEUS_USER}\", + \"basicAuthPassword\": \"${PROMETHEUS_PASSWORD}\", + \"isDefault\": true + }" + + echo "Prometheus datasource created successfully!" +fi + +# Test the datasource connection +echo "Testing Prometheus datasource connection..." +TEST_RESULT=$(curl -s -u "${GRAFANA_USER}:${GRAFANA_PASSWORD}" "${GRAFANA_URL}/api/datasources/1/health") + +if echo "$TEST_RESULT" | grep -q '"status":"OK"'; then + echo "✅ Prometheus datasource connection test passed!" +else + echo "❌ Prometheus datasource connection test failed:" + echo "$TEST_RESULT" +fi + +echo "Grafana configuration completed!" \ No newline at end of file diff --git a/monitoring/grafana/datasources/prometheus.yml b/monitoring/grafana/datasources/prometheus.yml index f88db84..660db82 100644 --- a/monitoring/grafana/datasources/prometheus.yml +++ b/monitoring/grafana/datasources/prometheus.yml @@ -6,4 +6,8 @@ datasources: access: proxy url: http://prometheus:9090 isDefault: true - editable: true \ No newline at end of file + editable: true + # Basic authentication configuration + basicAuth: ${PROMETHEUS_AUTH_ENABLED} + basicAuthUser: ${PROMETHEUS_USERNAME} + basicAuthPassword: ${PROMETHEUS_PASSWORD} \ No newline at end of file diff --git a/monitoring/prometheus-web.yml b/monitoring/prometheus-web.yml index e24d689..38101b4 100644 --- a/monitoring/prometheus-web.yml +++ b/monitoring/prometheus-web.yml @@ -1,8 +1,7 @@ # Prometheus web configuration with authentication -web: - basic_auth_users: - prometheus_user: $2y$10$8J8J8J8J8J8J8J8J8J8J8u8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8 +# Note: Prometheus doesn't support web.config.file in this format +# We'll use environment variables for basic auth instead -# Note: The password hash above is for 'prometheus_password' -# To generate a new password hash, use: -# echo "prometheus_password" | docker run --rm -i prom/prometheus:latest htpasswd -niB prometheus_user \ No newline at end of file +# Alternative approach: Use basic auth via web.yml +# This requires Prometheus to be built with web.yml support +web_config_file: /etc/prometheus/web.yml \ No newline at end of file diff --git a/monitoring/web.yml b/monitoring/web.yml new file mode 100644 index 0000000..9d8e060 --- /dev/null +++ b/monitoring/web.yml @@ -0,0 +1,7 @@ +# Prometheus web configuration with basic authentication +basic_auth_users: + prometheus_user: $2y$10$8J8J8J8J8J8J8J8J8J8J8u8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8 + +# Note: The password hash above is for 'prometheus_password' +# This hash was generated using: +# echo 'prometheus_password' | docker run --rm -i prom/prometheus:latest htpasswd -niB prometheus_user \ No newline at end of file diff --git a/setup-monitoring.sh b/setup-monitoring.sh new file mode 100755 index 0000000..29cf7cf --- /dev/null +++ b/setup-monitoring.sh @@ -0,0 +1,77 @@ +#!/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..." + +# Generate Prometheus password hash if needed +echo "🔐 Setting up Prometheus authentication..." +if [ ! -f "./monitoring/web.yml" ]; then + echo "Generating Prometheus web configuration..." + cat > ./monitoring/web.yml << 'EOF' +# Prometheus web configuration with basic authentication +basic_auth_users: + prometheus_user: $2y$10$8J8J8J8J8J8J8J8J8J8J8u8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8J8 +EOF + echo "Prometheus web configuration created!" +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_user/prometheus_password)" +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_user/prometheus_password)" +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" \ No newline at end of file diff --git a/setup-server.sh b/setup-server.sh index 32eba5d..1b0043c 100755 --- a/setup-server.sh +++ b/setup-server.sh @@ -350,6 +350,8 @@ display_completion_message() { echo " Dashboard: http://$host:8080/dashboard" echo " REST API: http://$host:8080" echo " Health Check: http://$host:8080/health" + echo " Grafana: http://$host:3000 (admin/admin)" + echo " Prometheus: http://$host:9091 (prometheus_user/prometheus_password)" echo "" echo "🔧 Next Steps:" echo " 1. Open the dashboard in your browser"