198 lines
6.3 KiB
Bash
198 lines
6.3 KiB
Bash
#!/bin/bash
|
|
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 Blackbox Exporter
|
|
VERSION="0.28.0"
|
|
ARCH="amd64"
|
|
CN_URL="https://s3.cloudyun.top/downloads/blackbox_exporter-${VERSION}.linux-${ARCH}.tar.gz"
|
|
GLOBAL_URL="https://github.com/prometheus/blackbox_exporter/releases/download/v${VERSION}/blackbox_exporter-${VERSION}.linux-${ARCH}.tar.gz"
|
|
TARGET="/tmp/blackbox_exporter.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
|
|
fi
|
|
|
|
if [ "$is_cn" = true ]; then
|
|
echo "Geolocation: China mainland detected"
|
|
DOWNLOAD_URL="$CN_URL"
|
|
else
|
|
echo "Geolocation: non-China region detected"
|
|
DOWNLOAD_URL="$GLOBAL_URL"
|
|
fi
|
|
|
|
echo "Downloading from: $DOWNLOAD_URL"
|
|
curl -fL -o "$TARGET" "$DOWNLOAD_URL"
|
|
|
|
# Extract
|
|
echo "Extracting Blackbox Exporter..."
|
|
tar -zxvf "$TARGET" -C /tmp
|
|
sudo mkdir -p /blackbox_exporter
|
|
sudo cp "/tmp/blackbox_exporter-${VERSION}.linux-${ARCH}/blackbox_exporter" /blackbox_exporter/
|
|
sudo cp "/tmp/blackbox_exporter-${VERSION}.linux-${ARCH}/blackbox.yml" /blackbox_exporter/
|
|
|
|
# choosing port
|
|
DEFAULT_PORT=9115
|
|
if [ -t 0 ]; then
|
|
while true; do
|
|
read -rp "Please enter blackbox_exporter listen port [default: ${DEFAULT_PORT}]: " PORT
|
|
PORT=${PORT:-$DEFAULT_PORT}
|
|
if [[ "$PORT" =~ ^[0-9]+$ ]] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then
|
|
break
|
|
else
|
|
echo "Invalid port. Please enter a number between 1 and 65535."
|
|
fi
|
|
done
|
|
else
|
|
PORT=$DEFAULT_PORT
|
|
echo "No TTY detected, using default port: $PORT"
|
|
fi
|
|
|
|
# Configure monitoring targets
|
|
if [ -d "/etc/prometheus" ]; then
|
|
TARGETS_DIR="/etc/prometheus/blackbox_targets"
|
|
echo "Prometheus detected, targets will be saved to $TARGETS_DIR"
|
|
else
|
|
TARGETS_DIR="/blackbox_exporter/targets"
|
|
fi
|
|
if [ -t 0 ]; then
|
|
echo "========================================================="
|
|
echo "Configuring monitoring objects (optional)..."
|
|
read -rp "Enter targets for ICMP Ping (separated by space, e.g. 8.8.8.8 1.1.1.1): " ICMP_VAL
|
|
read -rp "Enter targets for HTTP check (separated by space, e.g. https://google.com): " HTTP_VAL
|
|
|
|
if [ -n "$ICMP_VAL" ] || [ -n "$HTTP_VAL" ]; then
|
|
sudo mkdir -p "$TARGETS_DIR"
|
|
if [ -n "$ICMP_VAL" ]; then
|
|
echo "- targets:" | sudo tee "$TARGETS_DIR/icmp.yml" > /dev/null
|
|
for t in $ICMP_VAL; do
|
|
echo " - $t" | sudo tee -a "$TARGETS_DIR/icmp.yml" > /dev/null
|
|
done
|
|
echo "Done: $TARGETS_DIR/icmp.yml"
|
|
fi
|
|
if [ -n "$HTTP_VAL" ]; then
|
|
echo "- targets:" | sudo tee "$TARGETS_DIR/http.yml" > /dev/null
|
|
for t in $HTTP_VAL; do
|
|
echo " - $t" | sudo tee -a "$TARGETS_DIR/http.yml" > /dev/null
|
|
done
|
|
echo "Done: $TARGETS_DIR/http.yml"
|
|
fi
|
|
fi
|
|
echo "========================================================="
|
|
fi
|
|
|
|
|
|
# Create systemd service file
|
|
SERVICE_FILE="/etc/systemd/system/blackbox_exporter.service"
|
|
echo "Creating systemd service file..."
|
|
sudo tee "$SERVICE_FILE" > /dev/null <<EOF
|
|
[Unit]
|
|
Description=Blackbox Exporter
|
|
Wants=network-online.target
|
|
After=network-online.target
|
|
|
|
[Service]
|
|
User=root
|
|
WorkingDirectory=/blackbox_exporter
|
|
ExecStart=/blackbox_exporter/blackbox_exporter --config.file=/blackbox_exporter/blackbox.yml --web.listen-address=":${PORT}"
|
|
Restart=always
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
# Reload systemd, enable and start service
|
|
echo "Enabling and starting blackbox_exporter service..."
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable --now blackbox_exporter.service
|
|
|
|
# Automatically integrate with Prometheus if present
|
|
PROMETHEUS_YML="/etc/prometheus/prometheus.yml"
|
|
if [ -f "$PROMETHEUS_YML" ]; then
|
|
echo "Integrating with Prometheus..."
|
|
TEMP_CONF="/tmp/blackbox_jobs.yml"
|
|
rm -f "$TEMP_CONF"
|
|
|
|
# Generate the job snippets
|
|
if [ -f "$TARGETS_DIR/icmp.yml" ] && ! grep -q "job_name: 'blackbox-icmp'" "$PROMETHEUS_YML"; then
|
|
cat <<EOF >> "$TEMP_CONF"
|
|
- job_name: 'blackbox-icmp'
|
|
metrics_path: /probe
|
|
params:
|
|
module: [icmp]
|
|
file_sd_configs:
|
|
- files:
|
|
- $TARGETS_DIR/icmp.yml
|
|
relabel_configs:
|
|
- source_labels: [__address__]
|
|
target_label: __param_target
|
|
- source_labels: [__param_target]
|
|
target_label: instance
|
|
- target_label: __address__
|
|
replacement: 127.0.0.1:${PORT}
|
|
|
|
EOF
|
|
fi
|
|
|
|
if [ -f "$TARGETS_DIR/http.yml" ] && ! grep -q "job_name: 'blackbox-http'" "$PROMETHEUS_YML"; then
|
|
cat <<EOF >> "$TEMP_CONF"
|
|
- job_name: 'blackbox-http'
|
|
metrics_path: /probe
|
|
params:
|
|
module: [http_2xx]
|
|
file_sd_configs:
|
|
- files:
|
|
- $TARGETS_DIR/http.yml
|
|
relabel_configs:
|
|
- source_labels: [__address__]
|
|
target_label: __param_target
|
|
- source_labels: [__param_target]
|
|
target_label: instance
|
|
- target_label: __address__
|
|
replacement: 127.0.0.1:${PORT}
|
|
|
|
EOF
|
|
fi
|
|
|
|
if [ -f "$TEMP_CONF" ]; then
|
|
if [ -t 0 ]; then
|
|
read -rp "Found $PROMETHEUS_YML. Would you like to append the blackbox jobs automatically? [y/N]: " DO_APPEND
|
|
else
|
|
DO_APPEND="y"
|
|
fi
|
|
|
|
if [[ "$DO_APPEND" =~ ^[Yy]$ ]]; then
|
|
cat "$TEMP_CONF" | sudo tee -a "$PROMETHEUS_YML" > /dev/null
|
|
echo "Jobs appended to $PROMETHEUS_YML."
|
|
if [ -f "/usr/bin/restart_prometheus" ]; then
|
|
echo "Restarting Prometheus..."
|
|
sudo /usr/bin/restart_prometheus
|
|
else
|
|
echo "Please restart Prometheus to apply changes: sudo systemctl restart prometheus"
|
|
fi
|
|
fi
|
|
else
|
|
echo "Blackbox jobs already exist in $PROMETHEUS_YML or no targets configured."
|
|
fi
|
|
fi
|
|
|
|
echo "========================================================="
|
|
echo "Blackbox Exporter installation and integration completed!"
|
|
echo "Listening on port: ${PORT}"
|
|
echo "========================================================="
|
|
|