CalejoControl/setup-monitoring.sh

79 lines
2.7 KiB
Bash
Executable File

#!/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
# Check if user wants to use custom credentials or auto-generate
if [ -n "$PROMETHEUS_PASSWORD" ] && [ "$PROMETHEUS_PASSWORD" != "prometheus_password" ]; then
echo "🔐 Using custom Prometheus credentials from environment..."
PROMETHEUS_USERNAME=${PROMETHEUS_USERNAME:-prometheus_user}
# Generate Prometheus password hash with custom password
echo "Generating Prometheus web configuration..."
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 with custom credentials!"
else
echo "🔐 Auto-generating secure Prometheus credentials..."
./generate-monitoring-secrets.sh
# Load the generated credentials
if [ -f "./monitoring/.env.generated" ]; then
export $(grep -v '^#' ./monitoring/.env.generated | xargs)
fi
fi
# Grafana datasource configuration is now handled by generate-monitoring-secrets.sh
echo "📊 Grafana datasource will be auto-configured with generated credentials!"
# 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"