Files
Linux-Shell/Prometheus/install_Prometheus.sh
2026-01-25 05:44:44 +00:00

102 lines
2.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 Node Exporter
CN_URL="https://s3.cloudyun.top/relayx/prometheus-3.9.1.linux-amd64.tar.gz"
GLOBAL_URL="https://github.com/prometheus/prometheus/releases/download/v3.9.1/prometheus-3.9.1.linux-amd64.tar.gz"
TARGET="/tmp/prometheus-3.9.1.linux-amd64.tar.gz"
is_cn=false
echo "Detecting geographic location..."
COUNTRY=$(curl -s --max-time 3 https://ipinfo.io/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 Prometheus..."
tar -zxvf /tmp/prometheus-3.9.1.linux-amd64.tar.gz -C /tmp
#Create Config Floder
mkdir -p /etc/prometheus/prometheus_data
echo "Copying Prometheus to /usr/bin/prometheus ..."
sudo cp -r /tmp/prometheus-3.9.1.linux-amd64/prometheus /usr/bin/prometheus
# Create Blank Prometheus Config File
sudo tee "/etc/prometheus/prometheus.yml" > /dev/null <<EOF
global:
scrape_interval: 5s
scrape_configs:
- job_name: 'SIMPLE'
static_configs:
- targets: ['8.8.8.8:9100']
EOF
# Create systemd service file
SERVICE_FILE="/etc/systemd/system/prometheus.service"
echo "Creating systemd service file..."
sudo tee "$SERVICE_FILE" > /dev/null <<EOF
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/etc/prometheus/prometheus_data \
--storage.tsdb.retention.time=15d \
--web.listen-address=0.0.0.0:9090
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd, enable and start service
echo "Enabling and starting Prometheus service..."
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus.service
echo "Prometheus installation completed, listening on port 9090"