添加Prometheus高级配置脚本
This commit is contained in:
121
Prometheus/install_Alertmanager.sh
Normal file
121
Prometheus/install_Alertmanager.sh
Normal file
@@ -0,0 +1,121 @@
|
||||
#!/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.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 <<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: false # Set to true for 587/TLS, false if using SSL/465
|
||||
|
||||
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 "--------------------------------------------------------"
|
||||
Reference in New Issue
Block a user