📜 ⬆️ ⬇️

Setting up PostgreSQL monitoring in Zabbix

image

PostgreSQL is a modern, dynamically developing database with a very large set of features that allow you to solve the widest range of tasks. Using PostgreSQL usually refers to a very critical segment of IT infrastructure that is associated with data processing and storage. Considering the special place of the DBMS in the infrastructure and the degree of criticality of the tasks assigned to it, the question arises of monitoring and proper control over the work of the DBMS. In this regard, PostgreSQL has extensive internal means of collecting and storing statistics. Collected statistics allows you to get a pretty detailed picture of what is happening under the hood in the process of the DBMS functioning. These statistics are stored in special system tables-views and are constantly updated. By performing regular SQL queries in these tables, you can get a variety of data about databases, tables, indexes, and other DBMS subsystems.
Below I describe the method and means for monitoring PostgreSQL in the Zabbix monitoring system. I like this monitoring system because it provides ample opportunities for the implementation of the most custom monitoring of various systems and processes.

Monitoring will be built on the basis of SQL queries to statistics tables. The queries themselves are in the form of an additional configuration file for the zabbix agent, in which SQL queries are wrapped in a so-called. UserParameters - user monitoring parameters. Zabbix user parameter is a great way that allows you to customize monitoring for non-standard things, such things in our case will be PostgreSQL operation parameters. Each user parameter consists of two elements: Key name and Command . The key name is a unique name that does not overlap with other key names. A command is the actual command-action that the zabbix agent must perform. In the extended version, various parameters can be passed to this command. In a zabbiks configuration, it looks like this:
UserParameter=custom.simple.key,/usr/local/bin/simple-script UserParameter=custom.ext.key[*],/usr/local/bin/ext-script $1 $2 

Thus, all requests for PostgreSQL statistics are psql client requests wrapped in user parameters.

Strengths:

Weak sides:

Monitoring capabilities:

Additionally, it is worth noting that to collect statistical data, it is necessary to include the following parameters in postgresql.conf:
track_activities - includes command tracking (queries / statements) by all client processes;
track_counts - includes collecting statistics on tables and indexes;

Installation and configuration.
Everything you need to configure is in the Github repository.
 # git clone https://github.com/lesovsky/zabbix-extensions/ # cp zabbix-extensions/files/postgresql/postgresql.conf /etc/zabbix/zabbix_agentd.d/ 

Further, it should be noted that to execute requests from the agent, it is necessary that the appropriate access is defined in the pg_hba configuration — the agent must be able to establish connections with the postgres service to the target database and execute the requests. In the simplest case, you need to add the following line to pg_hba.conf (for different distributions, the file location may be different) - we allow connections from the name of postgres to the mydb database from localhost.
 host mydb postgres 127.0.0.1/32 trust 

It’s unforgettable that after changing pg_hba.conf, the postgresql service needs to reload (pg_ctl reload). However, this is the easiest option and not entirely secure, so if you want to use a password or a more complex access scheme, then once again carefully read pg_hba and .pgpass .
')
So, the configuration file is copied, it remains to load it into the main configuration, make sure that there is an Include line in the main agent configuration file in the path specified where the additional configuration files are located. Now we restart the agent, after which we can check the work by performing the simplest check - we use pgsql.ping and in square brackets we specify the connection options to postgres, which will be transferred to the psql client.
 # systemctl restart zabbix-agent.service # zabbix-get -s 127.0.0.1 -k pgsql.ping['-h 127.0.0.1 -p 5432 -U postgres -d mydb'] 

If you have correctly registered the access, then the service response time in milliseconds will return to you. If an empty string is returned, then there is a problem with accessing pg_hba. If the ZBX_NOTSUPPORTED string is returned - the configuration did not load, check the agent configuration, paths to Include and rights set to the configuration.

When the verification command returns the correct answer, it remains to download the template and upload it to the web interface and assign it to the target host. You can also download the template from the repository (postgresql-extended-template.xml). After importing, you need to go to the template macro settings tab and configure them.
image
Below is a list and brief description:

From the text of the triggers it should be clear why these threshold values ​​are needed:

Low Level Discovery Rules

Available graphics, if we talk about graphics, I tried to group the observed parameters, while not overloading the graphics with an excessively large number of paramters. So the information from pg_stat_user_tables is separated into 4 graphs.

Finally, a few sample graphics:
Here, as we can see, temporary files are regularly created in the database, you should look for the culprit in the log and review work_mem.
image

Here, events taking place in the database - commits / rollbacks and conflicts / deadlocks - everything is alright here in general.
image

Here the status of streaming replication with one of the servers is the lag time in seconds and bytes.
image

And the final graph is the service response time and average request time.
image

That's all, thank you all for your attention!

Source: https://habr.com/ru/post/234481/


All Articles