Pgbadger的安装和使用
pgBagder 是 PostgreSQL 的新一代日志分析器, 由 Darold (也是 ora2pg 的作者, 强大的迁移工具) 创建的。pgBadger 是一种快速、简便的工具, 用于分析 SQL 通信量, 并使用动态图来创建 HTML5 报告。pgBadger 是了解 PostgreSQL 服务器的行为并确定需要优化哪些 SQL 查询的完美工具。
pgBagder 是在linux操作系统中运行的程序。
环境:
操作系统:Linux。本文操作系统版本为 Redhat,CentOS 6.9,64位 。
数据库:PostgreSQL 10.0
依赖程序: Perl,php , httpd以及它们的依赖。
本文的Httpd的版本是httpd.x86_64 2.2.15,Perl的版本perl.x86_64 5.10,php.x86_64 5.3.3。
我们首先要确保所有的依赖都已经安装了成功。
安装Perl:
[[email protected] pgbadger-8.3]# yum install -y perl perl-devel
安装php:
[[email protected] pgbadger-8.3]# yum install -y php php-devel
安装httpd:
[[email protected] pgbadger-8.3]# yum install -y httpd httpd-devel
1. 下载 pgbadger的安装包。
在浏览器中访问官方地址https://github.com/darold/pgbadger/releases,选择一个版本来下载。
或者在linux命令行界面,运行下载命令。例如如果希望下载pgbadge v8.3,可以执行命令:
wget https://github.com/darold/pgbadger/archive/v8.3.tar.gz
2. 下载完成后。在安装包所在目录里解压,编译并安装。
解压安装包:
[[email protected] ~]# tar -zxvf pgbadger-8.3.tar.gz
进入解压后的目录:
[[email protected] ~]# cd pgbadger-8.3
编译Make文件:
[[email protected] pgbadger-8.3]# perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for pgBadger
然后编译和安装:
[[email protected] pgbadger-8.3]# make && make install
cp pgbadger blib/script/pgbadger
/usr/bin/perl -MExtUtils::MY -e 'MY->fixin(shift)' -- blib/script/pgbadger
Manifying blib/man1/pgbadger.1p
Installing /usr/local/share/man/man1/pgbadger.1p
Installing /usr/local/bin/pgbadger
Appending installation info to /usr/lib64/perl5/perllocal.pod
检测是否安装成功:
[[email protected] pgbadger-8.3]# which pgbadger
/usr/local/bin/pgbadger
[[email protected] pgbadger-8.3]# pgbadger -V
pgBadger version 8.3
3. 在“/var/www/”下创建一个叫作pgbadger的目录。
[[email protected] pgbadger-8.3]# mkdir /var/www/pgbadger
4. 修改httpd的参数。
对 /etc/httpd/conf/httpd.conf 做如下修改:
将这一行 DocumentRoot "/var/www/html" 修改为 DocumentRoot "/var/www/"
它表示程序能够读取的文档的根目录是“/var/www/”。
将这一行 <Directory "/var/www/html"> 修改为 <Directory "/var/www/pgBadger">
它表示最终会生成的报告文件所在的目录。
可以通过如下命令进行修改:
[[email protected] pgbadger-8.3]# vi /etc/httpd/conf/httpd.conf
尽量不要通过sed去修改,因为有些行的内容可能会包含在另外一些行中。
5. 启动httpd,并验证httpd能否正常工作。
执行下列命令启动httpd:
[[email protected] pgbadger-8.3]# service httpd start
正在启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using 10.33.47.114 for ServerName
[确定]
在/var/www/pgbadger/下创建一个php文件,名为test.php:
[[email protected] pgbadger]# vi /var/www/pgbadger/test.php
内容为:
<?php
echo "This is a test .";
?>
然后再浏览器中打开查看该网页:
http://10.33.47.114:/pgbadger/test.php
这里10.33.47.114是本文计算机的ip地址,这里你要将它替换为自己的ip地址。
如图所示,http可以正常工作。
6. 修改PostgreSQL的配置文件postgresql.conf 中的一些与日志有关的参数。
log_line_prefix,顾名思义是打印日志的前缀
# special values:
# %a = application name
# %u = user name
# %d = database name
# %r = remote host and port
# %h = remote host
# %p = process ID
# %t = timestamp without milliseconds
# %m = timestamp with milliseconds
# %n = timestamp with milliseconds (as a Unix epoch)
# %i = command tag
# %e = SQL state
# %c = session ID
# %l = session line number
# %s = session start timestamp
# %v = virtual transaction ID
# %x = transaction ID (0 if none)
# %q = stop here in non-session
我们将log_line_prefix设置如下并保存:
log_line_prefix = '%t %r %d %u '
这代表以时间戳,客户端的主机和端口,数据库名以及用户名作为日志的前缀。
下面的参数的值是默认的。你需要了解这些参数的含义。
log_destination = 'stderr' # Valid values are combinations of
# stderr, csvlog, syslog, and eventlog,
# depending on platform. csvlog
# requires logging_collector to be on.
# This is used when logging to stderr:
logging_collector = on # Enable capturing of stderr and csvlog
# into log files. Required to be on for
# csvlogs.
# (change requires restart)
# These are only used if logging_collector is on:
#log_directory = 'pg_log' # directory where log files are written,
# can be absolute or relative to PGDATA
7. 使用pgBadger来分析pg_log目录中的日志。
在postgresql产生的一段时间(例如几个小时或者几天)的日志后。我们可以就用pgBadger来分析它了。执行命令:
[[email protected]~]# pgbadger --prefix '%t-%d-%h-%u' /opt/postgresql10/data/pg_log/postgresql-*.log -o /var/www/pgbadger/out.html -f stderr
这里你需要将 “/opt/postgresql10/data/pg_log/postgresql-*.log” 是替换为你本地的postgresql日志文件的文件路径。
如果没有其他问题,pgBadger会马不停蹄地读取和分析这些日志,并最终生成一个全面的网页报表,名为out.html。你可以在浏览其中查看:http://10.33.47.114/pgbadger/out.html
pgBadger真是一个神奇的程序。
参考文献
[1] 转身泪倾城.pgbadger监控安装和使用. 2016-05-13