PostgreSQL的日誌分析工具——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:

[root@node01 pgbadger-8.3]# yum install -y perl perl-devel

安裝php:

[root@node01 pgbadger-8.3]# yum install -y php php-devel

安裝httpd:

[root@node01 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.  下載完成後。在安裝包所在目錄裏解壓,編譯並安裝。

解壓安裝包:

[root@node01 ~]# tar -zxvf pgbadger-8.3.tar.gz

進入解壓後的目錄: 

[root@node01 ~]# cd pgbadger-8.3

編譯Make文件:

[root@node01 pgbadger-8.3]# perl Makefile.PL

Checking if your kit is complete...

Looks good

Writing Makefile for pgBadger

然後編譯和安裝:

[root@node01 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

檢測是否安裝成功:

[root@node01 pgbadger-8.3]# which pgbadger

/usr/local/bin/pgbadger



[root@node01 pgbadger-8.3]# pgbadger -V

pgBadger version 8.3

3.  在“/var/www/”下創建一個叫作pgbadger的目錄。

[root@node01 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">

它表示最終會生成的報告文件所在的目錄。

 

可以通過如下命令進行修改:

[root@node01 pgbadger-8.3]# vi /etc/httpd/conf/httpd.conf

儘量不要通過sed去修改,因爲有些行的內容可能會包含在另外一些行中。

 

5.  啓動httpd,並驗證httpd能否正常工作。

執行下列命令啓動httpd

[root@node01 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:

[root@node01 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 [%p] %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來分析它了。執行命令:

[root@node01~]# pgbadger -s 10 --prefix '%t [%p] %r %d %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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章