#!/bin/bash
# GuruConnect Monitoring Setup Script
# Installs and configures Prometheus and Grafana

set -e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "========================================="
echo "GuruConnect Monitoring Setup"
echo "========================================="

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo -e "${RED}ERROR: This script must be run as root (sudo)${NC}"
    exit 1
fi

# Update package list
echo "Updating package list..."
apt-get update

# Install Prometheus
echo ""
echo "Installing Prometheus..."
apt-get install -y prometheus prometheus-node-exporter

# Copy Prometheus configuration
echo "Copying Prometheus configuration..."
cp prometheus.yml /etc/prometheus/prometheus.yml
if [ -f "alerts.yml" ]; then
    cp alerts.yml /etc/prometheus/alerts.yml
fi

# Set permissions
chown prometheus:prometheus /etc/prometheus/prometheus.yml
if [ -f "/etc/prometheus/alerts.yml" ]; then
    chown prometheus:prometheus /etc/prometheus/alerts.yml
fi

# Restart Prometheus
echo "Restarting Prometheus..."
systemctl restart prometheus
systemctl enable prometheus
systemctl restart prometheus-node-exporter
systemctl enable prometheus-node-exporter

# Install Grafana
echo ""
echo "Installing Grafana..."
apt-get install -y software-properties-common
add-apt-repository -y "deb https://packages.grafana.com/oss/deb stable main"
wget -q -O - https://packages.grafana.com/gpg.key | apt-key add -
apt-get update
apt-get install -y grafana

# Start Grafana
echo "Starting Grafana..."
systemctl start grafana-server
systemctl enable grafana-server

# Wait for Grafana to start
sleep 5

# Configure Grafana data source (Prometheus)
echo ""
echo "Configuring Grafana data source..."
curl -X POST -H "Content-Type: application/json" \
    -d '{
        "name":"Prometheus",
        "type":"prometheus",
        "url":"http://localhost:9090",
        "access":"proxy",
        "isDefault":true
    }' \
    http://admin:admin@localhost:3000/api/datasources || true

echo ""
echo "========================================="
echo "Monitoring Setup Complete!"
echo "========================================="
echo ""
echo "Services:"
echo "  Prometheus:  http://172.16.3.30:9090"
echo "  Grafana:     http://172.16.3.30:3000  (default login: admin/admin)"
echo "  Node Exporter: http://172.16.3.30:9100/metrics"
echo ""
echo "Next steps:"
echo "1. Access Grafana at http://172.16.3.30:3000"
echo "2. Login with default credentials (admin/admin)"
echo "3. Change the default password"
echo "4. Import the dashboard from grafana-dashboard.json"
echo "5. Configure alerting (optional)"
echo ""
echo "To import the dashboard:"
echo "  Grafana > Dashboards > Import > Upload JSON file"
echo "  Select: infrastructure/grafana-dashboard.json"
echo ""
