Configure PostgreSQL Monitoring With Zabbix
This tutorial will show you how to set up monitoring of your PostgreSQL database in Zabbix. This tutorial assumes your PostgreSQL database is running on a Debian 12 or Ubuntu 22.04 host, that you have already set up for basic Linux monitoring as shown in my previous tutorials linked below:
Related Posts
Steps
Install Plugin Package
Install PostgreSQL agent plugin.
sudo apt update && \
sudo apt install zabbix-agent2-plugin-postgresql
Create Database User
Log into the database:
sudo -u postgres psql
CREATE USER zbx_monitor WITH PASSWORD 'passwordGoesHere' INHERIT;
GRANT pg_monitor TO zbx_monitor;
\q
Update Database Config
Now we need to update the pg_hba.conf
file wot add the zbx_monitor
user:
PG_VERSION=14
sudo editor /etc/postgresql/$PG_VERSION/main/pg_hba.conf
PG_VERSION
is there because you may not be using PostgreSQL 14, in which case you would need to adjust accordingly.
Append the following line:
host all zbx_monitor localhost scram-sha-256
Alternatively, execute the following line to do this for you:
PG_VERSION=14
sudo echo "host all zbx_monitor localhost scram-sha-256" >> /etc/postgresql/$PG_VERSION/main/pg_hba.conf
Restart PostgreSQL Database
Now restart the PostgreSQL database for the changes to take effect:
sudo service postgresql restart
Test User Login
It is probably a good idea to test that you can now log in with that user on the host by running:
psql \
--username zbx_monitor \
--password \
--host localhost \
postgres
Restart Zabbix Agent
Now restart the zabbix agent, so that having installed the postgresql plugin takes effect etc.
sudo service zabbix-agent2 restart
Configure Zabbix Server
Now we need to update our host in our Zabbix server to configure monitoring the PostgreSQL database.
Right click on the host, and click on Configuration to bring up the configuration modal.
Click Select to open the modal for selecting another template.
Scroll down to select PostgreSQL by Zabbix agent 2 (1) and then click the Select button (2)
Click on the Macros tab (1), and then select inherited and host macros on the toggle (2).
Scroll down to {$PG.PASSWORD}
and {$PG.USER}
and click Change on both
Fill in the username and password you set earlier when creating the zabbix monitoring user in the database, and then click the Update button.
Wait a while, and you should now be able to see metrics about PostgreSQL if you click on Latest data by your host.
That's all folks!
References
- Zabbix - Available solutions - PostgreSQL by Zabbix agent 2
- Zabbix Forums - Problem with PostgreSQL monitoring
First published: 17th July 2023