Files
Linux-Shell/Prometheus/install_Alertmanager.sh
2026-04-19 16:37:10 +08:00

135 lines
3.9 KiB
Bash

#!/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 <<EOF
global:
resolve_timeout: 5m
smtp_smarthost: '$SMTP_HOST'
smtp_from: '$SMTP_FROM'
smtp_auth_username: '$SMTP_USER'
smtp_auth_password: '$SMTP_PASS'
smtp_require_tls: $SMTP_REQUIRE_TLS
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 5m
repeat_interval: 1h
receiver: 'email-notifications'
receivers:
- name: 'email-notifications'
email_configs:
- to: '$EMAIL_TO'
send_resolved: true
EOF
# Create systemd service
echo "Creating systemd service for Alertmanager..."
sudo tee "/etc/systemd/system/alertmanager.service" > /dev/null <<EOF
[Unit]
Description=Alertmanager
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/bin/alertmanager \\
--config.file=/etc/alertmanager/alertmanager.yml \\
--storage.path=/etc/alertmanager/data
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Reload and Start
sudo systemctl daemon-reload
sudo systemctl enable --now alertmanager.service
echo "--------------------------------------------------------"
echo "Alertmanager installed and configured with EMAIL support!"
echo "Configuration File: /etc/alertmanager/alertmanager.yml"
echo "Please edit the configuration to set your SMTP details."
echo "--------------------------------------------------------"