#!/bin/bash set -e # Arguments for Basic Auth USER_NAME="" PASSWORD_PLAIN="" while [[ "$#" -gt 0 ]]; do case $1 in --user) USER_NAME="$2"; shift ;; --pass) PASSWORD_PLAIN="$2"; shift ;; *) echo "Unknown option: $1"; exit 1 ;; esac shift done # If not provided via arguments, ask interactively if [ -z "$USER_NAME" ]; then read -p "Do you want to enable Basic Authentication for Node Exporter? [y/N]: " ENABLE_AUTH if [[ "$ENABLE_AUTH" =~ ^[Yy]$ ]]; then read -p "Enter Username: " USER_NAME read -s -p "Enter Password: " PASSWORD_PLAIN echo "" # New line after password if [ -z "$USER_NAME" ] || [ -z "$PASSWORD_PLAIN" ]; then echo "Error: Username and Password cannot be empty." exit 1 fi fi fi # 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 Node Exporter CN_URL="https://s3.cloudyun.top/downloads/node_exporter-1.10.2.linux-amd64.tar.gz" GLOBAL_URL="https://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gz" TARGET="/tmp/node_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 Node Exporter..." tar -zxvf /tmp/node_exporter.tar.gz -C /tmp # Copy to /node_exporter echo "Copying Node Exporter to /node_exporter..." sudo cp -r /tmp/node_exporter-1.10.2.linux-amd64 /node_exporter # Handle Basic Auth configuration if [ -n "$USER_NAME" ] && [ -n "$PASSWORD_PLAIN" ]; then echo "Configuring Basic Authentication for Node Exporter..." # Generate bcrypt hash using python3 (common on Linux) # If python3 is not available, you'll need to provide the hash manually if command -v python3 >/dev/null 2>&1; then HASHED_PASSWORD=$(python3 -c "import bcrypt; print(bcrypt.hashpw('$PASSWORD_PLAIN'.encode(), bcrypt.gensalt()).decode())" 2>/dev/null || true) # Fallback if bcrypt module is not installed if [ -z "$HASHED_PASSWORD" ]; then echo "Python bcrypt module not found, using simple crypt fallback (not bcrypt!)..." HASHED_PASSWORD=$(python3 -c "import crypt; print(crypt.crypt('$PASSWORD_PLAIN', crypt.mksalt(crypt.METHOD_SHA512)))" 2>/dev/null || true) fi fi if [ -n "$HASHED_PASSWORD" ]; then sudo tee "/node_exporter/web-config.yml" > /dev/null < /dev/null <