Skip to content

Instantly share code, notes, and snippets.

@temamagic
Created July 18, 2023 18:26
Show Gist options
  • Save temamagic/da19e0d9f72e6e4390b759b3810e4474 to your computer and use it in GitHub Desktop.
Save temamagic/da19e0d9f72e6e4390b759b3810e4474 to your computer and use it in GitHub Desktop.
Postgres prometheus exporter Installer script (Ubuntu)
#!/bin/bash
# Install required packages curl, jq, and wget if not already installed
sudo apt-get install -y curl jq wget
# Send GET request to GitHub API and retrieve the content
response=$(curl -s "https://api.github.com/repos/prometheus-community/postgres_exporter/releases/latest")
# Use awk to extract the tag_name value from the JSON response
VERSION=$(echo "$response" | awk -F'"' '/tag_name/{print $4}')
# Remove the 'v' prefix from the VERSION value
VERSION=${VERSION#v}
# Formulate the download link
download_link="https://github.com/prometheus-community/postgres_exporter/releases/download/v$VERSION/postgres_exporter-$VERSION.linux-amd64.tar.gz"
# Download the file using wget
wget "$download_link"
# Extract the archive
tar -xzv -C /tmp -f "postgres_exporter-$VERSION.linux-amd64.tar.gz" > /dev/null
# Copy the postgres_exporter file to /usr/local/bin
cp "/tmp/postgres_exporter-$VERSION.linux-amd64/postgres_exporter" /usr/local/bin
# Remove the temporary directory
rm -rf "/tmp/postgres_exporter-$VERSION.linux-amd64"
# Set permissions for the postgres user
chown -R postgres:postgres /usr/local/bin/postgres_exporter
# Create the postgres_exporter service file
sudo tee /etc/systemd/system/postgres_exporter.service > /dev/null <<EOT
[Unit]
Description=Prometheus PostgreSQL Exporter
After=network.target
[Service]
Type=simple
Restart=always
User=postgres
Group=postgres
Environment=DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable"
ExecStart=/usr/local/bin/postgres_exporter
[Install]
WantedBy=multi-user.target
EOT
# Reload systemd configuration and start the service
sudo systemctl daemon-reload
sudo systemctl enable postgres_exporter
sudo systemctl start postgres_exporter
# Check service status
sudo systemctl status postgres_exporter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment