2011-09-23

【系統】pgFouine : Parsing PostgreSQL Log 的好工具



最近系統換了資料庫。
所以要開始學著怎麼管理,找問題。
後來找到一個不錯的東西 pgFouine
它可以對PostgreSQL 的Log 檔,以html的格式,產生一個詳細的分析檔案。
現在來記錄一下怎麼使用。

首先,要有PG的Log File。
我是使用PostgreSQL 9.0.4的版本。
設定如下:
log_destination = 'stderr'
logging_collector = on
log_directory = '/home/logs/pg/
log_filename = 'pg-%Y%m%d.log'
log_truncate_on_rotation = on
log_rotation_age = 1d
silent_mode = on
log_min_messages = error
log_min_duration_statement = 100
log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d '
我是使用stderr 的方式來Log,一天rotate一次,
只有error的訊息還有執行時間大於100milliseconds 才記錄。
記得 log_line_prefix 一定要設定正確【%t [%p]: [%l-1] 】,pgFouine才解析的出來。
基本上,這樣應該會有Log 產生了。

現在要講怎麼用pgFouine了。
首先去 pgFouine 的網站下載,我是下載1.2版本的 tar.gz 檔案。
因為只是執行php,不用rpm了。
然後解壓,直接執行指令如下就可以了。
php pgfouine.php -logtype stderr -file ${InFile} -report ${OutFile}=overall,bytype,slowest,n-mostfrequenterrors,n-mosttime,n-mostfrequent,n-slowestaverage,hourly -format html-with-graphs
${InFile}  = PostgreSQL Log File
${OutFile} = 輸出的 html
記得 logtype 要指定為 stderr。
搞定。


PS.
  1. 要寫Cron 定時去清Log檔(Ex:只保留30天),不然會無止盡的產生。
    本來我要用logrotate去清,但,PG一直死咬著log檔不放,
    就算更名了,還是一直用更名過的log檔…
  2. 如果是開發環境,log_min_messages 可以設為warning。上線的最好不用,會有太多LOG
參考:


0 comments:

張貼留言