Files
Linux-Shell/System-Init/system-init-shells.sh

92 lines
2.3 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
#检测操作系统如果是红帽操作系统则安装firewalld并开放端口
if grep -Ei "red hat|rocky|alma|centos" /etc/os-release > /dev/null 2>&1; then
echo "Detected RHEL-based system"
sudo dnf -y groupinstall "Server"
sudo systemctl enable --now firewalld
ports=("22" "80" "443" "9100" "10000-65535")
for port in "${ports[@]}"; do
sudo firewall-cmd --permanent --add-port=${port}/tcp
sudo firewall-cmd --permanent --add-port=${port}/udp
done
#启用Firewall NAT转发
sudo firewall-cmd --permanent --add-masquerade --zone=public
#重启Firewall
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports
sudo dnf install -y wget curl tar
# 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
# Create systemd service file
SERVICE_FILE="/etc/systemd/system/node_exporter.service"
echo "Creating systemd service file..."
sudo tee "$SERVICE_FILE" > /dev/null <<EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=root
ExecStart=/node_exporter/node_exporter --web.listen-address=\":9100\"
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd, enable and start service
echo "Enabling and starting node_exporter service..."
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter.service
echo "Node Exporter installation completed, listening on port 9100"
else
echo "Current system is not RHEL-based"
fi