PG 慢日志分析工具pgbadger

失效地址
https://github.com/dalibo/pgbadger
https://github.com/darold/pgbadger

https://blog.csdn.net/ctypyb2002/article/details/80733465

perl Makefile.PL
make
make install 
log_destination = 'stderr'
# 日志记录类型,默认是stderr,只记录错误输出
log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,client=%h '
# log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
log_checkpoints = on
# 系统一天之类发生了多少次checkpoint,以及每次checkpoint的一些详细信息,频繁的checkpoint影响系统性能
log_connections = on
# log_connections    --用户session登陆时是否写入日志,默认off
log_disconnections = on
# 用户session退出时是否写入日志,默认off
log_lock_waits = on
# 一天内有多少个超过死锁时间的锁发生,默认是off,可以设置开启。这个可以区分SQL慢是资源紧张还是锁等待的问题
log_temp_files = 0
log_autovacuum_min_duration = 0
log_error_verbosity = default
log_statement = off
lc_messages='C'
Log_min_duration_statement = 1000
# 单位ms,超过1s为慢查询
# 其他日志
logging_collector      --是否开启日志收集开关,默认off,开启要重启DB
log_directory      --日志路径,默认是$PGDATA/pg_log
log_filename       --日志名称,默认是postgresql-%Y-%m-%d_%H%M%S.log
log_rotation_age   --保留单个文件的最大时长,默认是1d,也有1h,1min,1s,个人觉得不实用
log_rotation_size  --保留单个文件的最大尺寸,默认是10MB
pg_statement  = log_statement
# 参数值是none,即不记录,可以设置ddl(记录create,drop和alter)、mod(记录ddl+insert,delete,update和truncate)和all(mod+select)
  • 使用pg_ctl reload参数log_line_prefix可能不会生效,在psql下直接更改
alter system set log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,client=%h ';
  • 生成html格式
pgbadger --prefix='%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h ' postgresql-5.log -f stderr

SELECT pg_reload_conf();

shell脚本分析log

log_filename = 'postgresql-%a_39.19-%Y-%m-%d_%H%M%S.log'

  • 同步日记

#!/bin/sh
rsyncuser=scm
rsync_hostip='192.168.32.57'
rsync_binlog_monule=pglog
logdir=/data/var/lib/pgsql/9.4/data/pg_log/
tmpdir=/tmp/pglog/
filename=39.2_log.tar.gz
mkdir -p $tmpdir&&cd $logdir&&tar -zcf ${tmpdir}${filename} *|| exit 1
echo "1" > /tmp/39.2.flag&& flag=/tmp/39.2.flag
rsync -az --password-file=/etc/rsyncd.password ${tmpdir}${filename} $rsyncuser@${rsync_hostip}::$rsync_binlog_monule&&rsync -az --password-file=/etc/rsyncd.password $flag $rsyncuser@${rsy
nc_hostip}::$rsync_binlog_monule&&cd ${tmpdir}&& rm -rf ${tmpdir}${filename} || exit 1
  • 分析日志
#!/bin/sh
baslog='/backup/pglog/pg_log/'
pgoutput='/backup/pglog/pgbadger/'
workdir='/backup/pglog/tmp/'
tarfiledir=${baslog}
curtime=`date "+%F_%H%M%S"`
# 解压tar文件到tmp目录
for tarfile in `ls ${baslog}|grep "tar.gz"`
do
        echo $tarfile
        if [ $tarfile = '39.2_log.tar.gz' ];then
                tar -zxf ${baslog}${tarfile} -C ${workdir}&&tar -zxf ${baslog}${tarfile} -C ${baslog}192.168.39.2
                cd ${workdir}&&file=`ls ${workdir}`&& /usr/local/bin/pgbadger --prefix='%t [%p]: [%l-1] user=%u,db=%d,client=%h ' ${workdir}${file} -f stderr -o ${pgoutput}39.2_${curtime}.html&&rm -rf  /backup/pglog/tmp/*.log&&rm -rf ${baslog}${tarfile}

        elif [ $tarfile = '39.20_log.tar.gz' ];then
                tar -zxf ${baslog}${tarfile} -C ${workdir}&&tar -zxf ${baslog}${tarfile} -C ${baslog}192.168.39.20
                cd ${workdir}&&file=`ls ${workdir}`&& /usr/local/bin/pgbadger --prefix='%t [%p]: [%l-1] user=%u,db=%d,client=%h ' ${workdir}${file} -f stderr -o ${pgoutput}39.20_${curtime}.html&&rm -rf  /backup/pglog/tmp/*.log&&rm -rf ${baslog}${tarfile}
        fi
done
posted @ 2018-09-01 17:43  Jenvid  阅读(2497)  评论(0编辑  收藏  举报