reading the guide on configuring Agent 2 for Postgres Monitoring with Zabbix
it mentions … "Set the user name and password in host macros ({$PG.USER} and {$PG.PASSWORD}) if you want to override parameters from the Zabbix agent configuration file."
I'm curious how to go about setting the username\password in the zabbix_agent2.conf file so I'm not having to have them stored as macros on the zabbix server?
With Ubuntu at least, it doesn't create a /var/lib/zabbix directory to put a .pgpass
file, so I went ahead and did that, but it doesn't appear to be picking up those login credentials.
I'd like to get stats on Replication, which requires superuser, so I'm wanting to keep the credentials on the postgres server itself.
Best Answer
You can allow the read-only zabbix monitor user to access the postgres server from localhost without a password.
For PostgreSQL version 10 and above:
For PostgreSQL version 9.6 and below:
To collect WAL metrics, the user must have a
superuser
role.Copy postgresql/ folder to Zabbix agent home directory /var/lib/zabbix/. The postgresql/ directory contains the files needed to obtain metrics from PostgreSQL.
Copy template_db_postgresql.conf to Zabbix agent configuration directory /etc/zabbix/zabbix_agentd.d/ and restart Zabbix agent service.
Locate and edit postgres pg_hba.conf to allow connections from Zabbix agent https://www.postgresql.org/docs/current/auth-pg-hba-conf.html.
updatedb locate pg_hba.conf vi /etc/postgresql/11/main/pg_hba.conf (could be different in your host)
Add rows (for example):
Restart the postgres server. You should be allowed to connect using the new user without a password:
Example 1:
Example 2:
REMOTE_HOST could be 127.0.0.1 for localhost. Although you can skip thre previous point if the agent is installed in the same server that the postgres.
Restart the zabbix agent:
systemctl restart zabbix-agent
Import template_db_postgresql.yaml to Zabbix server and link it to the target host.
You'll find the files mentioned in this answer in Zabbix/templates/db/postgresql/ but you can also download them here: https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/db/postgresql