littleshop/setup-monitoring.sh
SysAdmin 68c5d2dfdf Production optimization: Docker configuration and monitoring stack
🚀 Docker Production Optimizations:
- Chiseled Ubuntu base image for minimal attack surface
- Non-root user execution with security hardening
- Read-only filesystem with targeted writable volumes
- Resource limits (1GB RAM, 1 CPU) with health checks
- Multi-stage builds optimized for caching
- Zero-downtime deployment automation

🔍 Comprehensive Monitoring Stack:
- Prometheus metrics collection with custom rules
- Grafana dashboards for application visualization
- AlertManager with email notifications for critical events
- Fluentd centralized logging with retention policies
- Node Exporter + cAdvisor for system/container metrics
- Health check endpoint (/health) for container orchestration

📋 Production Deployment Ready:
- Complete deployment scripts with backup strategy
- Environment templates for secure configuration
- Performance monitoring and alerting rules
- Enterprise-grade security and observability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-19 12:35:41 +01:00

195 lines
4.9 KiB
Bash

#!/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"