一、概述
pgCenter的主要目标是帮助Postgres DBA管理他们在数据库中拥有的统计数据,通过内置统计数据视图和函数,以方便的格式查看所有必要的数据。
1. 主要特征
- 类似于TOP的界面,可随时监控统计信息的变化
- 配置管理功能允许查看和编辑当前配置文件,并在需要时重新加载服务
- 日志文件功能允许您快速检查Postgres日志,而无需停止统计信息监视
- "Poor man’s monitoring",允许将Postgres统计信息收集到文件中,并在以后构建报告
2. 统计指标
- current summary activity - 不同来源的一系列指标汇总,包括:postgres uptime, version, recovery status, number of clients grouped by their states, number of (auto)vacuums, statements per second, age of the longest transaction and the longest vacuum
- pg_stat_activity - 已连接客户端和Postgres后台进程的当前活动相关的信息
- pg_stat_database - 数据库的统计信息,例如提交/回滚的数量,处理的元组,死锁,临时文件等
- pg_stat_replication - 有关复制,已连接备用主机及其活动的统计信息
- pg_stat_user_tables,pg_statio_user_tables - 对表的访问(包括IO)的统计信息
- pg_stat_user_indexes,pg_statio_user_indexes - 对索引的访问(包括IO)的统计信息
- pg_stat_user_functions - 关于函数执行的统计信息
- pg_stat_statements - 执行的SQL语句的统计信息,包括时间和资源使用情况
- pg_stat_progress_vacuum - 有关autovacuum进程状态的信息
二、安装
-- 使用release版本就可以了
-- 下载地址 https://github.com/lesovsky/pgcenter/releases,下载后解压就可以使用了
[root@localhost ~]# tar zxf pgcenter.linux-amd64.tar.gz
[root@localhost ~]# mkdir /opt/pgcenter
[root@localhost ~]# mv pgcenter /opt/pgcenter
[root@localhost ~]# echo "export PATH=$PATH:/opt/pgcenter" >>/etc/bashrc
[root@localhost ~]# source /etc/bashrc
[root@localhost ~]# pgcenter --version
pgcenter 0.5.0
三、参数说明
[root@localhost ~]# pgcenter -?
pgCenter is a command line admin tool for PostgreSQL.
Usage:
pgcenter [flags]
pgcenter [command] [command-flags] [args]
Available commands:
config configures Postgres to work with pgcenter
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.
pgcenter config:安装pgcenter用来统计的一些sql函数
-- 安装示例,指定安装到test数据库
-- 该步骤需要test数据库安装了plperlu,及perl模块Linux::Ethtool::Settings
[root@localhost ~]# perl -MCPAN -e shell
cpan[1]> install Linux::Ethtool::Settings
cpan[1]> q
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ psql -d test
psql (8.4.18, server 10.5)
WARNING: psql version 8.4, server version 10.0.
Some psql features might not work.
Type "help" for help.
test=# CREATE LANGUAGE plperlu;
test=# \q
[postgres@localhost ~]$ exit
[root@localhost ~]# pgcenter config -i -d test -U postgres
Statistics schema installed.
以下是pgcenter config的参数
[root@localhost ~]# pgcenter config -?
'pgcenter config' configures Postgres to work with pgcenter
Usage:
pgcenter config [OPTIONS]... [DBNAME [USERNAME]]
Options:
-i, --install install pgcenter's stats schema
-u, --uninstall uninstall pgcenter's stats schema
-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
--version show version information and exit
pgcenter top:为Postgres统计提供top界面,具有扩展的功能集
pgcenter top 命令参数
[root@localhost ~]# pgcenter top -?
'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
--version show version information and exit
pgcenter top 交互式命令
Help for interactive commands
general actions:
a,d,f,r mode: 'a' activity, 'd' databases, 'f' functions, 'r' replication,
s,t,i,v 's' tables sizes, 't' tables, 'i' indexes, 'v' vacuum progress,
x,X mit'x' pg_stat_statements switch, 'X' pg_stat_statements menu.
Left,Right,<,/ 'Left,Right' change column sort, '<' desc/asc sort toggle, '/' set filter.
C,E,R config: 'C' show config, 'E' edit configs, 'R' reload config.
p start psql session.
l open log file with pager.
aux stats actions:
B,N,L 'B' diskstat, 'N' nicstat, 'L' logtail.
activity actions:
-,_ '-' cancel backend by pid, '_' terminate backend by pid.
n,m 'n' set new mask, 'm' show current mask.
k,K 'k' cancel group of queries using mask, 'K' terminate group of backends using mask
I show IDLE connections toggle.
A change activity age threshold.
G get query report.
other actions:
, Q ',' show system tables on/off, 'Q' reset postgresql statistics counters.
z,Z 'z' set refresh interval, 'Z' change color scheme.
space pause program execution.
h,F1 show this tab.
Ctrl+Q quit.
Type 'Esc' to continue.
四、测试
使用pgbench进行压力测试,用pgcenter监控postgresql,pgcenter与postgresql在同一台机器上
-- pgbench初始化压力测试表,默认已创建了pgbench库
[postgres@localhost ~]$ pgbench -i --unlogged-tables -s 16 pgbench
-- pgbench用例,8客户端
[postgres@localhost ~]$ pgbench -r -c 8 -T 120 pgbench
-- pgcenter top 监控,每秒刷新
[root@localhost ~]# pgcenter top -d pgbench -U postgres
pgcenter也可以监控远程postgresql,但是需要远程postgresql也有pgcenter,并创建了相应的sql函数和视图,否则功能不可用,另外某些功能不支持远程postgresql。
pgcenter top 部分截图