PG监控工具之pgcenter
简介
GitHub:https://github.com/lesovsky/pgcenter
pgCenter is a command-line admin tool for observing and troubleshooting Postgres.
pgCenter's main goal is to help Postgres DBA working with statistics and provide a convenient way to observe Postgres in runtime.
安装
普通安装使用
可以tar.gz也可以rpm包:
1 2 3 4 5 6 7 8 9 10 11 12 13 | wget https://github.com/lesovsky/pgcenter/releases/download/v0.9.2/pgcenter_0.9.2_linux_amd64.tar.gz tar zxvf pgcenter_0.9.2_linux_amd64.tar.gz chmod 755 pgcenter ./pgcenter --version wget https://github.com/lesovsky/pgcenter/releases/download/v0.9.2/pgcenter_0.9.2_linux_amd64.rpm [root@docker35 soft]# rpm -ivh pgcenter_0.9.2_linux_amd64.rpm Preparing... ################################# [100%] Updating / installing... 1:pgcenter-0:0.9.2-1 ################################# [100%] [root@docker35 soft]# rpm -ql pgcenter-0:0.9.2-1 /usr/bin/pgcenter |
Docker使用
地址:https://hub.docker.com/r/lesovsky/pgcenter
1 2 | docker pull lesovsky/pgcenter:latest docker run -it --rm lesovsky/pgcenter:latest pgcenter top -h 1.2.3.4 -U user -d dbname -p 5432 |
使用过程:
1 2 3 4 5 6 7 8 9 | [root@docker35 ~]# docker pull lesovsky/pgcenter:latest latest: Pulling from lesovsky/pgcenter 540db60ca938: Pull complete 21f2e7685a5c: Pull complete Digest: sha256:5fb7a26a146d10e31a641e474d01ebea5319668962310c7fbf2a6ef8c47f3b3a Status: Downloaded newer image for lesovsky/pgcenter:latest docker.io/lesovsky/pgcenter:latest [root@docker35 ~]# docker run -it --rm lesovsky/pgcenter:latest pgcenter top -h 172.17.0.4 -U postgres -d postgres -p 5432 Password for user postgres: |
输入密码后就可以返回如下结果:
帮助命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | [root@docker35 soft]# pgcenter --help pgCenter is a command line admin tool for PostgreSQL. Usage: pgcenter [flags] pgcenter [command] [command-flags] [args] Available commands: config installs or uninstalls pgcenter stats schema to Postgres profile wait events profiler record record stats to file report make report based on previously saved statistics top top-like stats viewer Flags: -?, --help show this help and exit --version show version information and exit Use "pgcenter [command] --help" for more information about a command. Report bugs to <https://github.com/lesovsky/pgcenter/issues>. [root@docker35 soft]# pgcenter top --help 'pgcenter top' is the top-like stats viewer. Usage: pgcenter top [OPTIONS]... [DBNAME [USERNAME]] Options: -d, --dbname DBNAME database name to connect to -h, --host HOSTNAME database server host or socket directory -p, --port PORT database server port (default 5432) -U, --username USERNAME database user name General options: -?, --help show this help and exit Report bugs to <https://github.com/lesovsky/pgcenter/issues>. |
pgcenter top命令常用的指令选项
使用pgcenter top命令进行监控,默认是一秒刷新一次
- h,F1 查看帮助
- q,‘ESC’,‘Ctrl+Q’ 退出
- z 设置刷新间隔,默认是1秒,可调范围是1到300
- Q 重置数据库统计信息
- ‘-’ 中划线符号,输入pid,取消查询,相当于调用pg_cancel_backend函数
- ‘_’ 下划线符号,输入pid,取消查询,相当于调用pg_terminate_backend函数
- B 同时查看磁盘信息,交替按会取消。
- N 同时查看网络接口信息,交替按会取消。
- L 查看数据库日志尾部行
- l 查看数据库的日志信息,翻页形式
- ‘~’ 打开psql会话,\q退出到pgcenter界面
- C、E、R 显示、编辑、重载PG配置参数
- a、d、f、r 活动连接信息, 数据库信息, 函数信息, 流复制信息
- s、t、i 分别显示表大小、表统计信息、索引统计信息
- x、X 小写x交替显示不同维度pg_stat_statements信息,大写X下拉选取pg_stat_statements 菜单
- p、P 小写p交替显示pg_statprogress信息,大写P下拉选取pg_statprogress菜单
- Up、Down 上下键调整列的宽度
- Left,Right,<,/ 左右键选择列, 左方括号交替排序, '/'过滤
pgcenter top示例
先使用pgbench压测,准备环境
1 2 3 | $ pgbench -i -s8 --unlogged-tables postgres $ pgbench -r -c8 -T120 postgres |
使用pgcenter top命令进行监控,默认是一秒刷新一次
1 2 | [root@docker35 ~]# pgcenter top -h 172.17.0.4 -U postgres -d postgres -p 5433 Password for user postgres: |
按下“ctrl+c”结束监控。
官方示例
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | [root@docker35 ~]# pgcenter config -i -d postgres -h 172.17.0.4 -p 5433 -U postgres Password for user postgres: ERROR: language "plperlu" does not exist (SQLSTATE 42704) postgres=# create extension plperlu; ERROR: could not open extension control file "/pg13/pg13/share/postgresql/extension/plperlu.control": No such file or directory postgres=# select * from pg_language ; oid | lanname | lanowner | lanispl | lanpltrusted | lanplcallfoid | laninline | lanvalidator | lanacl -------+----------+----------+---------+--------------+---------------+-----------+--------------+-------- 12 | internal | 10 | f | f | 0 | 0 | 2246 | 13 | c | 10 | f | f | 0 | 0 | 2247 | 14 | sql | 10 | f | t | 0 | 0 | 2248 | 12651 | plpgsql | 10 | t | t | 12648 | 12649 | 12650 | (4 rows) [root@lhrpg /]# find / -name plperlu.control [root@lhrpg /]# yum list | grep postgres | grep perl Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast postgresql-plperl.x86_64 9.2.24-7.el7_9 updates postgresql10-plperl.x86_64 10.17-1PGDG.rhel7 pgdg10 postgresql11-plperl.x86_64 11.12-1PGDG.rhel7 pgdg11 postgresql12-plperl.x86_64 12.7-1PGDG.rhel7 pgdg12 postgresql13-plperl.x86_64 13.3-1PGDG.rhel7 pgdg13 postgresql96-plperl.x86_64 9.6.22-1PGDG.rhel7 pgdg96 [root@lhrpg /]# yum install -y postgresql13-plperl.x86_64 Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile ..... Total download size: 62 k Installed size: 194 k Downloading packages: postgresql13-plperl-13.3-1PGDG.rhel7.x86_64.rpm | 62 kB 00:00:10 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : postgresql13-plperl-13.3-1PGDG.rhel7.x86_64 1/1 Verifying : postgresql13-plperl-13.3-1PGDG.rhel7.x86_64 1/1 Installed: postgresql13-plperl.x86_64 0:13.3-1PGDG.rhel7 Complete! [root@lhrpg /]# rpm -ql postgresql13-plperl-13.3-1PGDG.rhel7.x86_64 /usr/pgsql-13/lib/bool_plperl.so /usr/pgsql-13/lib/plperl.so /usr/pgsql-13/share/extension/bool_plperl--1.0.sql /usr/pgsql-13/share/extension/bool_plperl.control /usr/pgsql-13/share/extension/bool_plperlu--1.0.sql /usr/pgsql-13/share/extension/bool_plperlu.control /usr/pgsql-13/share/extension/plperl--1.0.sql /usr/pgsql-13/share/extension/plperl.control /usr/pgsql-13/share/extension/plperlu--1.0.sql /usr/pgsql-13/share/extension/plperlu.control /usr/pgsql-13/share/locale/cs/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/de/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/es/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/fr/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/it/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/ja/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/ko/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/pl/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/pt_BR/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/ro/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/ru/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/sv/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/tr/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/uk/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/vi/LC_MESSAGES/plperl-13.mo /usr/pgsql-13/share/locale/zh_CN/LC_MESSAGES/plperl-13.mo postgres=# create extension plperlu; CREATE EXTENSION postgres=# select * from pg_language ; oid | lanname | lanowner | lanispl | lanpltrusted | lanplcallfoid | laninline | lanvalidator | lanacl -------+----------+----------+---------+--------------+---------------+-----------+--------------+-------- 12 | internal | 10 | f | f | 0 | 0 | 2246 | 13 | c | 10 | f | f | 0 | 0 | 2247 | 14 | sql | 10 | f | t | 0 | 0 | 2248 | 13245 | plpgsql | 10 | t | t | 13242 | 13243 | 13244 | 16434 | plperlu | 10 | t | f | 16431 | 16432 | 16433 | (5 rows) |
README: pgcenter config
参考:https://github.com/lesovsky/pgcenter/blob/master/doc/pgcenter-config-readme.md
pgcenter config
is a supplementary tool which allows management of pgCenter’s additional SQL functions.
General information
As mentioned earlier, pgCenter tracks system's usage through local procfs
filesystem. It works very well when pgCenter runs on the same host with Postgres, however, what do you do if you want to run pgCenter on your laptop and connect it to a remote Postgres on a far datacenter?
It's not an issue and pgCenter can track remote system statistics through established Postgres connection using pgCenter's own SQL functions. All you need is to install Postgres built-in procedural language and pgCenter's functions into a remote database and connect as usual.
Note: when pgCenter runs on the same host with Postgres, it reads stats directly from /proc and doesn't use Postgres connection for reading system stats.
Installing and removing functions is possible with pgcenter config
, however, with few limitations:
plperlu
(which means untrusted plperl) procedural language must be installed manually in the database you want to connect pgCenter to (see details here).- perl modules
Linux::Ethtool::Settings
,Filesys::Df
should be installed in the system, it's used to get stats about network interfaces and mounted filesystems needed to calculate metrics.
Main functions
- installing and removing SQL functions and views in a desired database.
Usage
Run config
command and install stats schema into a database:
1 | pgcenter config --install -h 1.2.3.4 -U postgres db_production |
If Linux::Ethtool::Settings
module is not installed in the system, pgcenter config -i
will fail with the following error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # pgcenter config -i ERROR: Can't locate Linux/Ethtool/Settings.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at line 2. BEGIN failed--compilation aborted at line 2. DETAIL: HINT: STATEMENT: CREATE FUNCTION pgcenter.get_netdev_link_settings(INOUT iface CHARACTER VARYING, OUT speed BIGINT, OUT duplex INTEGER) RETURNS RECORD LANGUAGE plperlu AS $$ use Linux::Ethtool::Settings; if (my $settings = Linux::Ethtool::Settings->new($_[0])) { my $if_speed = $settings->speed(); my $if_duplex = $settings->duplex() ? 1 : 0; return {iface => $_[0], speed => $if_speed, duplex => $if_duplex}; } else { return {iface => $_[0], speed => 0, duplex => -1}; } $$; |
As you can see the problem is related to pgcenter.get_netdev_link_settings()
function which depends on Linux::Ethtool::Settings
module. To fix the issue you need to install the module into your system.
In general, a preferred way is installing dependencies using distro's default package manager, but it might happen that Linux::Ethtool::Settings
will not be available in the official package repo. In this case, you can install perl module using CPAN, but extra dependencies would have to be resolved, such as make
,gcc
and others.
Here is a complete example reproduced in Docker environment using using official docker image for postgresql.
1 2 3 4 5 6 7 8 9 10 11 12 | # yum install -y postgresql-plperl # yum install -y gcc make perl cpan # cpan Module::Build # cpan Linux::Ethtool::Settings # cpan Filesys::Df # psql -U postgres -c 'CREATE LANGUAGE plperlu' # pgcenter config -i -U postgres # psql -U postgres -c "select * from pgcenter.get_netdev_link_settings('eth0')" iface | speed | duplex -------+-------+-------- eth0 | 10000 | 1 (1 row) |
As you can see, finally function pgcenter.get_netdev_link_settings()
works well.
Perhaps it’s possible to use the same approach in other distros, because of perl module name is the same, but names of other packages may vary (eg. postgresql-10-plperl
instead of postgresql-plperl
).
Other notes
Of course, pgcenter top
can also work with remote Postgres which don't have these SQL functions installed. In this case zeroes will be shown in the system stats interface (load average, cpu, memory, swap, io, network) and multiple errors will appear in Postgres log. For easier distribution, SQL functions and views used by pgCenter are hard-coded into the source code, but their usage is not limited, so feel free to use it.
Another limitation is related to procfs
filesystem, which is Linux-specific file system, hence there might be problematic to run pgCenter on operation systems other than Linux. But you can still run pgCenter in Docker.
See other usage examples here.