Skip to content

Instantly share code, notes, and snippets.

@azwan082
Last active February 14, 2025 04:32
Show Gist options
  • Save azwan082/ab500911ab5ca15ed42a955d10b5a1ff to your computer and use it in GitHub Desktop.
Save azwan082/ab500911ab5ca15ed42a955d10b5a1ff to your computer and use it in GitHub Desktop.
Install prometheus on RHEL-based distro (Centos 9), x64 processor & configure systemd service.
#!/bin/bash
#
# NOTE: untested!
#
# Usage:
# sudo install_prometheus.sh
#
# Reference:
# https://devopscube.com/install-configure-prometheus-linux/
#
VERSION=2.53.3
NAME=prometheus-$VERSION.linux-amd64
# dependencies
if ! command -v tar 2>&1 >/dev/null; then
dnf install tar
fi
# download binary
curl -LO https://github.com/prometheus/prometheus/releases/download/v$VERSION/$NAME.tar.gz
tar -xvf $NAME.tar.gz
mv $NAME prometheus-files
# create user
useradd --no-create-home --shell /bin/false prometheus
mkdir /etc/prometheus
mkdir /var/lib/prometheus
chown prometheus:prometheus /etc/prometheus
chown prometheus:prometheus /var/lib/prometheus
# copy binary to PATH
cp prometheus-files/prometheus /usr/local/bin/
cp prometheus-files/promtool /usr/local/bin/
chown prometheus:prometheus /usr/local/bin/prometheus
chown prometheus:prometheus /usr/local/bin/promtool
# copy consoles files
cp -r prometheus-files/consoles /etc/prometheus
cp -r prometheus-files/console_libraries /etc/prometheus
chown -R prometheus:prometheus /etc/prometheus/consoles
chown -R prometheus:prometheus /etc/prometheus/console_libraries
# create config file
cat /etc/prometheus/prometheus.yml << EOL
global:
scrape_interval: 10s
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
EOL
chown prometheus:prometheus /etc/prometheus/prometheus.yml
# create systemd service
cat /etc/systemd/system/prometheus.service << EOL
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
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
[Install]
WantedBy=multi-user.target
EOL
systemctl daemon-reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment