Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 35 additions & 41 deletions prometheus-setup.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#!/bin/bash

# Prometheus Installation Script
# Standard paths: /etc/prometheus (config), /var/lib/prometheus (data)
# Version: 3.5.0
# Prometheus Installation Script (Improved)

set -e

# Ensure root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi

# Set hostname
echo "prometheus" > /etc/hostname
hostname prometheus
hostnamectl set-hostname prometheus

# Variables
PROM_VERSION="3.5.0"
DOWNLOAD_URL="https://github.com/prometheus/prometheus/releases/download/v${PROM_VERSION}/prometheus-${PROM_VERSION}.linux-amd64.tar.gz"
TAR_FILE="prometheus-${PROM_VERSION}.linux-amd64.tar.gz"
EXTRACT_DIR="prometheus-${PROM_VERSION}.linux-amd64"
WORK_DIR="/tmp/prom"
CONFIG_DIR="/etc/prometheus"
DATA_DIR="/var/lib/prometheus"
Expand All @@ -25,37 +26,38 @@ SERVICE_FILE="/etc/systemd/system/prometheus.service"
mkdir -p "${WORK_DIR}"
cd "${WORK_DIR}"

# Download and extract
wget "${DOWNLOAD_URL}"
tar xzvf "${TAR_FILE}"
# Install dependencies
apt update -y
apt install -y wget tar

# Create group and user
groupadd --system prometheus
useradd -s /sbin/nologin --system -g prometheus prometheus
# Download and extract
wget -q "${DOWNLOAD_URL}"
tar -xzf "prometheus-${PROM_VERSION}.linux-amd64.tar.gz"

# Create data directory
mkdir "${DATA_DIR}"
chown -R prometheus:prometheus "${DATA_DIR}"
chmod -R 775 "${DATA_DIR}"
# Create user/group safely
id "prometheus" &>/dev/null || useradd -rs /bin/false prometheus

# Create config subdirectories
mkdir -p "${CONFIG_DIR}/rules"
mkdir -p "${CONFIG_DIR}/rules.s"
mkdir -p "${CONFIG_DIR}/files_sd"
# Create directories
mkdir -p "${CONFIG_DIR}" "${DATA_DIR}" \
"${CONFIG_DIR}/consoles" \
"${CONFIG_DIR}/console_libraries"

# Enter extracted directory
cd "${EXTRACT_DIR}"
cd "prometheus-${PROM_VERSION}.linux-amd64"

# Move binaries
mv prometheus promtool "${BIN_DIR}"
cp prometheus promtool "${BIN_DIR}"

# Check version
prometheus --version
# Move config + consoles
cp prometheus.yml "${CONFIG_DIR}"
cp -r consoles/* "${CONFIG_DIR}/consoles/"
cp -r console_libraries/* "${CONFIG_DIR}/console_libraries/"

# Move config file
mv prometheus.yml "${CONFIG_DIR}"
# Set permissions
chown -R prometheus:prometheus "${CONFIG_DIR}" "${DATA_DIR}"
chmod -R 755 "${CONFIG_DIR}" "${DATA_DIR}"

# Create systemd service file
# Create systemd service
cat > "${SERVICE_FILE}" << EOF
[Unit]
Description=Prometheus
Expand All @@ -64,35 +66,27 @@ Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
Type=simple
ExecReload=/bin/kill -HUP \$MAINPID
ExecStart=/usr/local/bin/prometheus \\
--config.file=/etc/prometheus/prometheus.yml \\
--storage.tsdb.path=/var/lib/prometheus \\
--web.console.templates=/etc/prometheus/consoles \\
--web.console.libraries=/etc/prometheus/console_libraries \\
--web.listen-address=0.0.0.0:9090 \\
--web.enable-remote-write-receiver
--web.listen-address=0.0.0.0:9090

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# Set permissions
chown -R prometheus:prometheus "${CONFIG_DIR}"
chmod -R 775 "${CONFIG_DIR}"
chown -R prometheus:prometheus "${DATA_DIR}"

# Reload and start service
# Start service
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus --no-pager

# Display service file for verification
cat "${SERVICE_FILE}"
echo "Prometheus installed successfully!"
echo "Access: http://<Your-IP>:9090""${SERVICE_FILE}"