当前位置: 欣欣网 > 码农

PG数据库巡检脚本之pg_gather、pg_collector和小麦苗PG巡检脚本比对和使用(均开源免费)

2024-04-27码农

简介

目前在GitHub上有3款 免费开源 的针对 PostgreSQL的巡检脚本 ,均为sql脚本,均可以生成html报告文件,可以 自行修改代码 ,并自己进行增删相关的功能。

下边麦老师对这3个工具做一下简单比对:

小麦苗PG巡检脚本 pg_gather pg_collector
GitHub地址 https://github.com/dbbao/dbhealthcheck https://github.com/jobinau/pg_gather https://github.com/awslabs/pg-collector
免费开源 Y Y Y
最近更新 2024.04.25 2024.04.21 2022.07.29
运行方式 psql直接跑 需要3条命令 psql直接跑
缺点 暂未加入报告总结部分 运行复杂,且会导入部分数据到数据库 更新慢,不同版本有不同的脚本
优点 持续更新,界面美观,中文报告 报告结尾有Findings部分,且报告中间异常部分特别标识 内容非常详细

小麦苗巡检脚本

更多请参考:

https://github.com/dbbao/dbhealthcheck

https://www.xmmup.com/shujukuxunjianjiaoben.html

运行方式

需要有psql客户端,运行方式如下:

1psql -U postgres -h 192.168.66.35 -p 54324 -d postgres -W -H -q -f D:\DB_PG_HC_lhr_v7.0.0.sql > d:\a.html

输入密码,回车即可。

报告示例

这里只列出 部分结果 ,其它的详细内容可以参考:https://share.weiyun.com/FQY9zcm0

pg_gather

更多请参考:

https://github.com/jobinau/pg_gather

https://xmmup.com/pgkaiyuandexunjiangongjupg_gather.html

运行方式

1-- 采集数据
2psql -U postgres -h 172.17.0.2 -X -f gather.sql > out.txt
3
4-- 导入数据
5sed -e '/^Pager/d; /^Tuples/d; /^Output/d; /^SELECT pg_sleep/d; /^PREPARE/d; /^\s*$/d' out.txt | psql -U postgres -h 172.17.0.2 -f gather_schema.sql -f -
6
7-- 生成报告
8psql -U postgres -h 172.17.0.2 -X -f gather_report.sql > GatherReport.html

报告示例

pg_collector

更多请参考:

https://github.com/awslabs/pg-collector

https://www.xmmup.com/pgkaiyuandexunjiangongjupg_collector.html

运行方式

1psql -h [hostname or RDS endpoint] -p [Port] -d [Database name ] -U [user name] -f pg_collector.sql

报告示例

http://pg-collector.s3-website-us-west-2.amazonaws.com/pg_collector_postgres-2021-08-02_181348.html

http://pg-collector.s3-website-us-west-2.amazonaws.com/pg_collector_postgres-2020-12-14_053537.html

一个示例:

image-20240426170057920

总结

在实际使用过程中,大家可以都去尝试一下,或者结合这3种脚本,自己再开发一套新的巡检脚本,也很不错。

若对小麦苗PG巡检脚本发现任何bug,或有任何建议,都可以联系麦老师(微信:db_bao),谢谢。