#!/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 # Create system user if not exists if ! id -u blackbox_exporter >/dev/null 2>&1; then echo "Creating blackbox_exporter system user..." sudo useradd --no-create-home --shell /bin/false blackbox_exporter fi # Set permissions and capabilities echo "Setting permissions and capabilities..." sudo chown -R blackbox_exporter:blackbox_exporter /blackbox_exporter # Grant raw socket capability for ICMP probing if command -v setcap >/dev/null 2>&1; then sudo setcap 'cap_net_raw+ep' /blackbox_exporter/blackbox_exporter else echo "Warning: setcap not found. ICMP probing might require root." fi # Create systemd service file SERVICE_FILE="/etc/systemd/system/blackbox_exporter.service" echo "Creating systemd service file..." sudo tee "$SERVICE_FILE" > /dev/null <