← back to Watches
scripts/setup-monitoring.sh
250 lines
#!/bin/bash
################################################################################
# OMEGA WATCHES - MONITORING SETUP SCRIPT
# Sets up Prometheus, Grafana, Alertmanager, and Redis
################################################################################
set -euo pipefail
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
log_info() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
log_error() {
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR:${NC} $1"
}
print_header() {
echo -e "${BLUE}"
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ OMEGA WATCHES - MONITORING SETUP ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
# Check prerequisites
check_prerequisites() {
log_info "Checking prerequisites..."
if ! command -v docker &> /dev/null; then
log_error "Docker is not installed!"
echo "Install with: curl -fsSL https://get.docker.com | bash"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
log_error "Docker Compose is not installed!"
echo "Install with: apt install docker-compose"
exit 1
fi
log "Prerequisites check passed ✓"
}
# Create directories
setup_directories() {
log_info "Creating monitoring directories..."
mkdir -p /root/Projects/watches/monitoring/{prometheus,grafana,alertmanager}
mkdir -p /root/Projects/watches/monitoring/grafana/{provisioning,dashboards}
mkdir -p /root/Projects/watches/monitoring/grafana/provisioning/{datasources,dashboards}
log "Directories created ✓"
}
# Configure firewall
configure_firewall() {
log_info "Configuring firewall rules..."
# Prometheus
ufw allow 7510/tcp comment "Prometheus"
# Grafana
ufw allow 7511/tcp comment "Grafana"
# Node Exporter
ufw allow 7512/tcp comment "Node Exporter"
# Alertmanager
ufw allow 7513/tcp comment "Alertmanager"
# Redis
ufw allow 7514/tcp comment "Redis"
log "Firewall configured ✓"
}
# Start monitoring stack
start_monitoring() {
log_info "Starting monitoring stack..."
cd /root/Projects/watches
# Pull images
docker-compose -f docker-compose.monitoring.yml pull
# Start containers
docker-compose -f docker-compose.monitoring.yml up -d
log "Monitoring stack started ✓"
}
# Wait for services
wait_for_services() {
log_info "Waiting for services to be ready..."
# Wait for Prometheus
log_info "Waiting for Prometheus..."
for i in {1..30}; do
if curl -f -s http://localhost:7510/-/ready > /dev/null 2>&1; then
log "Prometheus is ready ✓"
break
fi
sleep 2
done
# Wait for Grafana
log_info "Waiting for Grafana..."
for i in {1..30}; do
if curl -f -s http://localhost:7511/api/health > /dev/null 2>&1; then
log "Grafana is ready ✓"
break
fi
sleep 2
done
# Wait for Redis
log_info "Waiting for Redis..."
sleep 5
log "Redis is ready ✓"
}
# Import Grafana dashboards
import_grafana_dashboards() {
log_info "Importing Grafana dashboards..."
# Wait a bit more for Grafana to be fully ready
sleep 10
# Create Omega Watches dashboard via API
cat > /tmp/grafana-dashboard.json << 'EOF'
{
"dashboard": {
"title": "Omega Watches - Production Monitoring",
"tags": ["omega-watches", "production"],
"timezone": "browser",
"panels": [
{
"title": "Application Status",
"type": "stat",
"targets": [
{
"expr": "up{job=\"omega-watches\"}",
"refId": "A"
}
],
"gridPos": {"h": 4, "w": 6, "x": 0, "y": 0}
},
{
"title": "Request Rate",
"type": "graph",
"targets": [
{
"expr": "rate(http_requests_total[5m])",
"refId": "A"
}
],
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 4}
},
{
"title": "CPU Usage",
"type": "graph",
"targets": [
{
"expr": "100 - (avg by(instance) (rate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)",
"refId": "A"
}
],
"gridPos": {"h": 8, "w": 12, "x": 12, "y": 4}
},
{
"title": "Memory Usage",
"type": "graph",
"targets": [
{
"expr": "(1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100",
"refId": "A"
}
],
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 12}
}
]
},
"overwrite": true
}
EOF
curl -X POST -H "Content-Type: application/json" \
-d @/tmp/grafana-dashboard.json \
http://admin:omega-watches-admin@localhost:7511/api/dashboards/db \
> /dev/null 2>&1 || log_error "Dashboard import failed (will try again later)"
rm -f /tmp/grafana-dashboard.json
log "Grafana dashboards imported ✓"
}
# Display access information
show_access_info() {
echo ""
log "${GREEN}╔════════════════════════════════════════════════════════════════╗${NC}"
log "${GREEN}║ MONITORING SETUP COMPLETE! ║${NC}"
log "${GREEN}╚════════════════════════════════════════════════════════════════╝${NC}"
echo ""
log_info "Access URLs:"
echo ""
echo " Grafana Dashboard: http://45.61.58.125:7511"
echo " Username: admin"
echo " Password: omega-watches-admin"
echo ""
echo " Prometheus: http://45.61.58.125:7510"
echo " Alertmanager: http://45.61.58.125:7513"
echo " Node Exporter: http://45.61.58.125:7512"
echo ""
log_info "Container Status:"
docker-compose -f /root/Projects/watches/docker-compose.monitoring.yml ps
echo ""
log_info "Useful Commands:"
echo " View logs: docker-compose -f docker-compose.monitoring.yml logs -f"
echo " Stop stack: docker-compose -f docker-compose.monitoring.yml down"
echo " Restart stack: docker-compose -f docker-compose.monitoring.yml restart"
echo ""
}
# Main function
main() {
print_header
check_prerequisites
setup_directories
configure_firewall
start_monitoring
wait_for_services
import_grafana_dashboards
show_access_info
}
# Run main
main "$@"