#!/bin/bash source ~/.bashrc # Observability Stack Setup Script (Prometheus + Grafana) # Run this on the Observability VM after OS installation set -e # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } log_step() { echo -e "${BLUE}[STEP]${NC} $1" } # Check if running as root if [ "$EUID" -ne 0 ]; then log_error "Please run as root (use sudo)" exit 1 fi PROMETHEUS_VERSION="${PROMETHEUS_VERSION:-2.45.0}" GRAFANA_VERSION="${GRAFANA_VERSION:-10.0.0}" PROMETHEUS_USER="${PROMETHEUS_USER:-prometheus}" GRAFANA_USER="${GRAFANA_USER:-grafana}" log_step "Step 1: Installing dependencies..." apt-get update apt-get install -y wget curl log_step "Step 2: Installing Prometheus..." # Create Prometheus user useradd -r -s /bin/false "$PROMETHEUS_USER" || log_warn "User $PROMETHEUS_USER may already exist" # Download and install Prometheus cd /tmp wget "https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz" tar xzf "prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz" mv "prometheus-${PROMETHEUS_VERSION}.linux-amd64" /opt/prometheus mkdir -p /etc/prometheus mkdir -p /var/lib/prometheus chown -R "$PROMETHEUS_USER:$PROMETHEUS_USER" /opt/prometheus /etc/prometheus /var/lib/prometheus # Create Prometheus configuration cat > /etc/prometheus/prometheus.yml < /etc/systemd/system/prometheus.service < /etc/systemd/system/node-exporter.service < /etc/apt/sources.list.d/grafana.list apt-get update apt-get install -y grafana log_step "Step 5: Starting services..." systemctl daemon-reload systemctl enable prometheus node-exporter grafana-server systemctl start prometheus node-exporter grafana-server sleep 3 log_info "=========================================" log_info "Observability Stack Installation Complete!" log_info "=========================================" echo "" log_info "Services:" echo " - Prometheus: http://192.168.1.82:9090" echo " - Grafana: http://192.168.1.82:3000" echo " - Node Exporter: http://192.168.1.82:9100" echo "" log_info "Grafana default credentials:" echo " Username: admin" echo " Password: admin (change on first login)" echo "" log_info "Next steps:" echo " 1. Access Grafana and change default password" echo " 2. Add Prometheus as data source (http://localhost:9090)" echo " 3. Import dashboards from grafana.com/dashboards" echo " 4. Configure alerting rules"