#!/bin/bash # Alertmanager Installation and Configuration Script # This script installs Alertmanager and configures email notifications. set -e # Detect Operation System if command -v apt >/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.32.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:465" SMTP_USER="user@example.com" SMTP_PASS="password" SMTP_FROM="alertmanager@example.com" EMAIL_TO="recipient@example.com" SMTP_REQUIRE_TLS="false" # Interactive SMTP Configuration echo "" echo "--------------------------------------------------------" echo " Alertmanager SMTP Configuration Setup" echo "--------------------------------------------------------" 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 Auth Username (Email): " SMTP_USER read -s -p "Enter SMTP Auth Password: " SMTP_PASS echo "" read -p "Enter Sender Email (Default: $SMTP_USER): " SMTP_FROM [ -z "$SMTP_FROM" ] && SMTP_FROM="$SMTP_USER" read -p "Enter Recipient Email: " EMAIL_TO # Simple logic to determine TLS requirement if [[ "$SMTP_HOST" == *":587" ]] || [[ "$SMTP_HOST" == *":25" ]]; then SMTP_REQUIRE_TLS="true" else SMTP_REQUIRE_TLS="false" fi echo "Notice: Detected port, setting smtp_require_tls to $SMTP_REQUIRE_TLS" fi # Create Configuration echo "Creating alertmanager.yml..." sudo tee "/etc/alertmanager/alertmanager.yml" > /dev/null < /dev/null <