#!/bin/bash # LittleShop Monitoring Stack Setup Script # This script sets up comprehensive monitoring and logging set -e # Exit on any error # Configuration MONITORING_DIR="/opt/littleshop/monitoring" GRAFANA_DIR="/opt/littleshop/grafana" # Colors for output GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color log() { echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" } warn() { echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] WARNING: $1${NC}" } error() { echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}" exit 1 } log "Setting up LittleShop monitoring stack..." # Check prerequisites command -v docker >/dev/null 2>&1 || error "Docker is not installed" command -v docker-compose >/dev/null 2>&1 || error "Docker Compose is not installed" # Create monitoring directories log "Creating monitoring directories..." sudo mkdir -p "$MONITORING_DIR"/{prometheus,grafana,alertmanager,fluentd} sudo mkdir -p "$GRAFANA_DIR"/{provisioning/{datasources,dashboards},dashboards} # Set up Grafana provisioning log "Setting up Grafana provisioning..." # Create datasource configuration cat > "$GRAFANA_DIR/provisioning/datasources/prometheus.yml" << EOF apiVersion: 1 datasources: - name: Prometheus type: prometheus access: proxy url: http://prometheus:9090 isDefault: true editable: false EOF # Create dashboard configuration cat > "$GRAFANA_DIR/provisioning/dashboards/default.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 # Create LittleShop dashboard cat > "$GRAFANA_DIR/dashboards/littleshop.json" << 'EOF' { "dashboard": { "id": null, "title": "LittleShop Application Dashboard", "tags": ["littleshop"], "timezone": "browser", "panels": [ { "id": 1, "title": "Application Health", "type": "stat", "targets": [ { "expr": "up{job=\"littleshop\"}", "legendFormat": "Application Status" } ], "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "thresholds": { "steps": [ {"color": "red", "value": 0}, {"color": "green", "value": 1} ] } } }, "gridPos": {"h": 8, "w": 12, "x": 0, "y": 0} }, { "id": 2, "title": "HTTP Request Rate", "type": "graph", "targets": [ { "expr": "rate(http_requests_total{job=\"littleshop\"}[5m])", "legendFormat": "Requests/sec" } ], "gridPos": {"h": 8, "w": 12, "x": 12, "y": 0} } ], "time": { "from": "now-1h", "to": "now" }, "refresh": "30s" } } EOF # Set permissions sudo chown -R 472:472 "$GRAFANA_DIR" # Grafana user ID sudo chmod -R 755 "$MONITORING_DIR" # Create environment file for monitoring if it doesn't exist if [[ ! -f .env.monitoring ]]; then log "Creating monitoring environment file..." cat > .env.monitoring << EOF # Monitoring Configuration GRAFANA_ADMIN_USER=admin GRAFANA_ADMIN_PASSWORD=admin123 PROMETHEUS_RETENTION=15d ALERTMANAGER_EMAIL=admin@silverlabs.uk EOF warn "Please update .env.monitoring with secure passwords and email addresses" fi # Start monitoring stack log "Starting monitoring stack..." docker-compose -f docker-compose.monitoring.yml --env-file .env.monitoring up -d # Wait for services to start log "Waiting for services to start..." sleep 30 # Verify services log "Verifying monitoring services..." # Check Prometheus if curl -f http://localhost:9090/-/healthy >/dev/null 2>&1; then log "✅ Prometheus is healthy" else warn "❌ Prometheus health check failed" fi # Check Grafana if curl -f http://localhost:3000/api/health >/dev/null 2>&1; then log "✅ Grafana is healthy" else warn "❌ Grafana health check failed" fi # Check AlertManager if curl -f http://localhost:9093/-/healthy >/dev/null 2>&1; then log "✅ AlertManager is healthy" else warn "❌ AlertManager health check failed" fi log "Monitoring stack setup completed!" log "" log "Access URLs (if Traefik is configured):" log " • Grafana: https://grafana.silverlabs.uk" log " • Prometheus: https://prometheus.silverlabs.uk" log " • AlertManager: https://alerts.silverlabs.uk" log "" log "Local access URLs:" log " • Grafana: http://localhost:3000 (admin/admin123)" log " • Prometheus: http://localhost:9090" log " • AlertManager: http://localhost:9093" log "" log "Next steps:" log " 1. Update .env.monitoring with secure passwords" log " 2. Configure email alerts in docker/alertmanager.yml" log " 3. Import additional Grafana dashboards" log " 4. Set up backup for monitoring data"