100 lines
2.4 KiB
Bash
100 lines
2.4 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 Node Exporter
|
||
CN_URL="https://s3.cloudyun.top/downloads/cadvisor-v0.55.1-linux-amd64"
|
||
GLOBAL_URL="https://github.com/google/cadvisor/releases/download/v0.55.1/cadvisor-v0.55.1-linux-amd64"
|
||
TARGET=/tmp/cAdvisor
|
||
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"
|
||
|
||
sudo cp -r /tmp/cAdvisor /usr/bin/cadvisor
|
||
chmod 755 /usr/bin/cadvisor
|
||
|
||
SERVICE_FILE="/etc/systemd/system/cAdvisior.service"
|
||
|
||
echo "Creating systemd service file..."
|
||
|
||
#chose port
|
||
|
||
DEFAULT_PORT=8087
|
||
|
||
if [ -t 0 ]; then
|
||
# 有 TTY,交互式
|
||
while true; do
|
||
read -rp "Please enter node_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
|
||
# 无 TTY,自动模式
|
||
PORT=$DEFAULT_PORT
|
||
echo "No TTY detected, using default port: $PORT"
|
||
fi
|
||
|
||
|
||
|
||
sudo tee "$SERVICE_FILE" > /dev/null <<EOF
|
||
|
||
[Unit]
|
||
Description=cAdvisor - Container Resource Usage and Performance Analysis
|
||
Documentation=https://github.com/google/cadvisor
|
||
After=docker.service
|
||
Requires=docker.service
|
||
|
||
[Service]
|
||
Type=simple
|
||
ExecStart=/usr/bin/cadvisor --logtostderr=true --port=${PORT} --housekeeping_interval=10s --docker_only=true --store_container_labels=false
|
||
Restart=always
|
||
RestartSec=10
|
||
LimitNOFILE=1048576
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
|
||
EOF
|
||
# Reload systemd, enable and start service
|
||
echo "Enabling and starting cAdvisior service..."
|
||
sudo systemctl daemon-reload
|
||
sudo systemctl enable --now cAdvisior.service
|
||
|
||
echo "Node Exporter installation completed, listening on port ${PORT}"
|