Prometheus 监控PostGRESQL ,并设置自启动

2023年2月21日

Install and Configure PostgreSQL Server Exporter

cd /opt/
wget https://github.com/prometheus-community/postgres_exporter/releases/download/v0.11.1/postgres_exporter-0.11.1.linux-amd64.tar.gz
tar zxf postgres_exporter-0.11.1.linux-amd64.tar.gz
mv postgres_exporter-0.11.1.linux-amd64 postgres_exporter
cp postgres_exporter/postgres_exporter /usr/local/bin/

Create a configuration file

vim postgres_exporter.env

In the file, provide the DATA_SOURCE_NAME(DSN) variable for the PostgreSQL instance. For example:

##To monitor all Databases (可能数据不全)##
#DATA_SOURCE_NAME="postgresql://postgres:postgres@localhost:5432/?sslmode=disable"
###To monitor a Specific database, uncomment and edit the line below###
DATA_SOURCE_NAME="postgresql://username:password@localhost:5432/database-name?sslmode=disable"

You can replace localhost with the IP address if you are running PostgreSQL on a remote host. You also need to ensure that remote connectivity is enabled on PostgreSQL.

Now create a systemd service for the PostgreSQL Server Exporter.

sudo vim /etc/systemd/system/postgres_exporter.service

Add the below lines to the file:

[Unit]
Description=Prometheus exporter for Postgresql
Wants=network-online.target
After=network-online.target

[Service]
User=postgres
Group=postgres
WorkingDirectory=/opt/postgres_exporter
EnvironmentFile=/opt/postgres_exporter/postgres_exporter.env
ExecStart=/usr/local/bin/postgres_exporter --web.listen-address=:9187 --web.telemetry-path=/metrics
Restart=always

[Install]
WantedBy=multi-user.target


On RHEL-based systems, you need to configure SELinux as shown:

sudo /sbin/restorecon -v /usr/local/bin/postgres_exporter

如果使用 postgres 系统用户 Allow the user to own the created directory:

chown -R postgres:  /opt/postgres_exporter

Or 创建一个新的用户 postgres_exporter
Create a system user for the service:  

sudo adduser -M -r -s /sbin/nologin postgres_exporter

Allow the user to own the created directory:

sudo chown -R postgres_exporter:postgres_exporter     /opt/postgres_exporter


Reload the daemon:

sudo systemctl daemon-reload


Start postgres_exporter :

systemctl start postgres_exporter

Start and enable the service:

sudo systemctl enable --now postgres_exporter

Verify if the service is running:

$ systemctl status postgres_exporter
● postgres_exporter.service
     Loaded: loaded (/etc/systemd/system/postgres_exporter.service; enabled; vendor preset: disabled)
     Active: active (running) since Mon 2022-09-05 09:48:38 CEST; 5s ago
   Main PID: 33323 (postgres_export)
      Tasks: 4 (limit: 23441)
     Memory: 11.0M
        CPU: 12ms
     CGroup: /system.slice/postgres_exporter.service
             └─33323 /usr/local/bin/postgres_exporter --web.listen-address=:9187 --web.telemetry-path=/metrics

If you have a firewall running, allow the port through it:

##For UFW
sudo ufw allow 9187
##For Firewalld
sudo firewall-cmd --add-port=9187/tcp --permanent
sudo firewall-cmd --reload

 

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注