diff --git a/Prometheus/advanced_prometheus_config.sh b/Prometheus/advanced_prometheus_config.sh new file mode 100644 index 0000000..7e0a3dd --- /dev/null +++ b/Prometheus/advanced_prometheus_config.sh @@ -0,0 +1,154 @@ +#!/bin/bash + +# Advanced Prometheus Configuration Script +# This script sets up a production-ready Prometheus configuration +# including alerting rules, rule files, and Alertmanager integration. + +set -e + +CONFIG_DIR="/etc/prometheus" +RULES_DIR="$CONFIG_DIR/rules" +DATA_DIR="$CONFIG_DIR/prometheus_data" +MAIN_CONFIG="$CONFIG_DIR/prometheus.yml" + +# Ensure directories exist +sudo mkdir -p "$RULES_DIR" +sudo mkdir -p "$DATA_DIR" + +echo "Configuring Advanced Prometheus Features..." + +# 1. Create a Sample Alert Rule File +echo "Creating default alert rules in $RULES_DIR/node_alerts.yml..." +sudo tee "$RULES_DIR/node_alerts.yml" > /dev/null <80%) + - alert: HighCPUUsage + expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80 + for: 5m + labels: + severity: warning + annotations: + summary: "High CPU usage on {{ \$labels.instance }}" + description: "CPU usage is at {{ \$value | printf \"%.2f\" }}% on {{ \$labels.instance }}." + + # Alert for high Memory usage (>85%) + - alert: HighMemoryUsage + expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 85 + for: 5m + labels: + severity: warning + annotations: + summary: "High Memory usage on {{ \$labels.instance }}" + description: "Memory usage is at {{ \$value | printf \"%.2f\" }}% on {{ \$labels.instance }}." + + # Alert for high Disk usage (>90%) + - alert: HighDiskUsage + expr: (node_filesystem_size_bytes{mountpoint="/"} - node_filesystem_free_bytes{mountpoint="/"}) / node_filesystem_size_bytes{mountpoint="/"} * 100 > 90 + for: 5m + labels: + severity: critical + annotations: + summary: "High Disk usage on {{ \$labels.instance }}" + description: "Disk usage on / is at {{ \$value | printf \"%.2f\" }}% on {{ \$labels.instance }}." +EOF + +# 2. Create the Advanced Main Configuration File +echo "Creating advanced prometheus.yml..." +sudo tee "$MAIN_CONFIG" > /dev/null </dev/null 2>&1; then + echo "Detected apt-based system" + sudo apt update + sudo apt install -y wget curl tar +elif command -v dnf >/dev/null 2>&1; then + echo "Detected dnf-based system" + sudo dnf install -y wget curl tar +else + echo "Unsupported package manager" + exit 1 +fi + +# Download Alertmanager +VERSION="0.27.0" +CN_URL="https://s3.cloudyun.top/downloads/alertmanager-${VERSION}.linux-amd64.tar.gz" +GLOBAL_URL="https://github.com/prometheus/alertmanager/releases/download/v${VERSION}/alertmanager-${VERSION}.linux-amd64.tar.gz" +TARGET="/tmp/alertmanager.tar.gz" + +is_cn=false +echo "Detecting geographic location..." +COUNTRY=$(curl -s --max-time 3 https://ipinfo.littlediary.cn/country || true) +if [ "$COUNTRY" = "CN" ]; then + is_cn=true + DOWNLOAD_URL="$CN_URL" +else + DOWNLOAD_URL="$GLOBAL_URL" +fi + +echo "Downloading from: $DOWNLOAD_URL" +curl -fL -o "$TARGET" "$DOWNLOAD_URL" + +# Extract and Install +echo "Extracting Alertmanager..." +tar -zxvf "$TARGET" -C /tmp +sudo mkdir -p /etc/alertmanager +sudo cp /tmp/alertmanager-${VERSION}.linux-amd64/alertmanager /usr/bin/ +sudo cp /tmp/alertmanager-${VERSION}.linux-amd64/amtool /usr/bin/ + +# Arguments for SMTP +SMTP_HOST="smtp.example.com:587" +SMTP_USER="user@example.com" +SMTP_PASS="password" +SMTP_FROM="alertmanager@example.com" +EMAIL_TO="recipient@example.com" + +# Interactive SMTP Configuration +echo "--- Alertmanager Email Setup ---" +read -p "Do you want to enable Email Notifications? [y/N]: " ENABLE_EMAIL +if [[ "$ENABLE_EMAIL" =~ ^[Yy]$ ]]; then + read -p "Enter SMTP Host (e.g. smtp.qq.com:465): " SMTP_HOST + read -p "Enter SMTP Authentication Username: " SMTP_USER + read -s -p "Enter SMTP Authentication Password: " SMTP_PASS + echo "" # New line after hidden password + read -p "Enter Sender Email (e.g. noreply@domain.com): " SMTP_FROM + read -p "Enter Recipient Email: " EMAIL_TO +fi + +# Create Configuration with Email Support +echo "Creating alertmanager.yml..." +sudo tee "/etc/alertmanager/alertmanager.yml" > /dev/null < /dev/null <