當前位置: 妍妍網 > 碼農

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),謝謝。