os: centos 7.4
postgresql: 9.1
pgbadger:9.2
pgbadger 是一款分析postgresql 日志文件的开软软件。
pgBadger is a PostgreSQL log analyzer build for speed with fully detailed reports from your PostgreSQL log file.
下载
https://github.com/dalibo/pgbadger
安装
# cd /tmp
# git clone https://github.com/dalibo/pgbadger.git
# cd ./pgbadger
# perl Makefile.PL
# make
# make install
# which pgbadger
/usr/local/bin/pgbadger
# pgbadger --version
pgBadger version 9.2
使用
https://github.com/dalibo/pgbadger 这个页面有相对比较详细的介绍说明。
另外也可以参考 http://dalibo.github.io/pgbadger/
自己写了几个定时分析的脚本:
crontab -l
# crontab -l
00 03 * * * script -c "/apps/log_done.sh >> /apps/log_done.log"
tree /apps
# tree /apps
/apps
├── log_done.log
├── log_done.sh
├── postgresql_log_trade
│ ├── postgresql.log-20180619-192.168.56.100
│ └── postgresql.log-20180619-192.168.56.101
├── postgresql_out_trade
│ ├── pgbadger-postgresql.log-20180619-192.168.56.100.html
│ └── pgbadger-postgresql.log-20180619-192.168.56.101.html
└── postgresql_script
├── postgresql_log_scp.sh
├── postgresql_mail_trade.sh
└── postgresql_out_trade.sh
postgresql_log_scp.sh
# cat postgresql_log_scp.sh
#!/bin/bash
#20180619 peiyb add pgbadger deal postgresql log
#CDATE=`date +%Y%m%d`
#CDATEM=`date +%Y-%m-%d`
CDATE=$1
CDATEM=${CDATE:0:4}'-'${CDATE:4:2}'-'${CDATE:6:2}
#############################################
#
#拷贝 postgresql 的日志
#
#############################################
rm -f /apps/postgresql_log_trade/*
echo "`date ` scp postgresql trade log file"
scp root@192.168.56.100:/var/log/postgresql/postgresql-$CDATEM.csv /apps/postgresql_log_trade/postgresql.log-$CDATE-192.168.56.100
scp root@192.168.56.101:/var/log/postgresql/postgresql-$CDATEM.csv /apps/postgresql_log_trade/postgresql.log-$CDATE-192.168.56.101
postgresql_out_trade.sh
# cat ./postgresql_out_trade.sh
#!/bin/bash
#20180619 peiyb add pgbadger deal postgres log
CDATE=$1
LOGBASEDIR='/apps/postgresql_log_trade'
OUTBASEDIR='/apps/postgresql_out_trade'
rm -f ${OUTBASEDIR}/*
#############################################
#
#使用 pgbadger 处理
#
#############################################
for LFILE in `ls ${LOGBASEDIR}/`
do
echo $LFILE
/usr/local/bin/pgbadger --prefix='%t ' ${LOGBASEDIR}/${LFILE} -f csv -o ${OUTBASEDIR}/pgbadger-${LFILE}.html
done
postgresql_mail_trade.sh
# cat postgresql_mail_trade.sh
#!/bin/bash
#20180619 peiyb add mail send
CDATE=$1
LOGBASEDIR='/apps/postgresql_log_trade'
OUTBASEDIR='/apps/postgresql_out_trade'
MAILLIST='peiyb@163.com'
ATTACHLIST=''
#############################################
#
#使用 linux mailx 发送 postgresql trade 生成的out文件
#
#############################################
#获取不同机器个数
for LFILE in `ls ${OUTBASEDIR}/ |grep -i ".html"|cut -d- -f4 | sort | uniq`
do
ATTACHLIST=''
#拼接找出符合的文件
for AFILE in `ls ${OUTBASEDIR}/ |grep -i $LFILE`
do
ATTACHLIST=$ATTACHLIST' -a '$OUTBASEDIR'/'$AFILE
done
#echo $ATTACHLIST
#发送邮件
echo "见附件" | /bin/mailx -s "[db][pg001][log analyze][$LFILE]" $ATTACHLIST $MAILLIST
done
postgresql一些参数
log_duration = off #每一个完成的语句的持续时间被记录,记录会很多,建议关闭
log_statement = 'ddl'
log_min_duration_statement = 10000 #单位为ms,大于这个时间的语句会被记录
log_line_prefix = '%t '
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on
log_temp_files = 0
log_autovacuum_min_duration = 0
log_error_verbosity = default
lc_messages='C'
参考:
http://dalibo.github.io/pgbadger/
https://github.com/dalibo/pgbadger
https://github.com/dalibo/pgbadger/releases