1. 首页 > PostgreSQL教程 > 正文

PostgreSQL教程FG112-PG监控告警配置实战教程

本文档详细介绍PostgreSQL数据库监控告警的配置方法,包括pgwatch2、Prometheus、Grafana等监控工具的配置,以及告警规则的设置,风哥教程参考PostgreSQL官方文档Monitoring内容,适合DBA在生产环境中实施监控告警系统。

Part01-基础概念与理论知识

1.1 PostgreSQL监控概念

PostgreSQL监控是指对数据库运行状态进行实时监测,收集性能指标,及时发现和处理问题。监控包括系统资源监控、数据库性能监控、会话监控、锁监控等多个方面。完善的监控体系是保障数据库稳定运行的重要基础。更多视频教程www.fgedu.net.cn

PostgreSQL监控核心指标:

  • 连接数和会话状态
  • 事务和锁等待
  • 缓冲区命中率
  • 检查点和WAL写入
  • 表和索引大小
  • 查询性能统计

1.2 PostgreSQL监控架构

# PostgreSQL监控架构

# 1. 监控层次
# 系统层:CPU、内存、磁盘、网络
# 数据库层:连接、事务、锁、缓冲区
# 应用层:查询性能、慢查询

# 2. 监控架构图
┌─────────────────────────────────────────────────────┐
│ 监控展示层 │
│ Grafana仪表盘 │
└─────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────┐
│ 数据存储层 │
│ Prometheus / InfluxDB │
└─────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────┐
│ 数据采集层 │
│ postgres_exporter / pgwatch2 / 自定义脚本 │
└─────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────┐
│ PostgreSQL数据库 │
│ 192.168.1.100:5432 │
└─────────────────────────────────────────────────────┘

# 3. 监控数据流
# PostgreSQL -> 采集器 -> 存储层 -> 展示层

# 4. 告警流程
# 指标采集 -> 规则匹配 -> 告警触发 -> 通知发送

1.3 PostgreSQL监控工具

PostgreSQL监控工具:

  • pgwatch2:PostgreSQL专用监控解决方案
  • Prometheus + postgres_exporter:时序数据库+导出器
  • Grafana:可视化仪表盘
  • pg_stat_statements:查询性能统计
  • pgBadger:日志分析工具
  • PMM:Percona监控管理
风哥提示:监控是数据库运维的重要环节,建议建立完善的监控体系,覆盖系统层和数据库层。选择合适的监控工具,配置合理的告警规则,及时发现和处理问题。学习交流加群风哥微信: itpux-com

Part02-生产环境规划与建议

2.1 PostgreSQL监控规划

# PostgreSQL监控规划

# 1. 监控节点规划
# 监控服务器:192.168.1.200
# PostgreSQL服务器:192.168.1.100, 192.168.1.101, 192.168.1.102

# 2. 组件规划
# Prometheus:192.168.1.200:9090
# Grafana:192.168.1.200:3000
# Alertmanager:192.168.1.200:9093
# postgres_exporter:每个PostgreSQL节点

# 3. 端口规划
# Prometheus:9090
# Grafana:3000
# Alertmanager:9093
# postgres_exporter:9187

# 4. 数据保留规划
# 监控数据:30天
# 告警历史:90天

# 5. 采集频率规划
# 系统指标:15秒
# 数据库指标:30秒
# 慢查询分析:60秒

2.2 PostgreSQL告警规划

PostgreSQL告警规划:

  • 紧急告警:数据库宕机、磁盘满、主从切换
  • 重要告警:连接数过高、复制延迟、锁等待
  • 一般告警:慢查询、表膨胀、索引缺失

2.3 PostgreSQL指标规划

# PostgreSQL指标规划

# 1. 系统指标
# CPU使用率
# 内存使用率
# 磁盘I/O
# 网络流量

# 2. 数据库指标
# 连接数:pg_stat_activity
# 事务数:pg_stat_database
# 锁等待:pg_locks
# 缓冲区:pg_stat_bgwriter
# 复制状态:pg_stat_replication

# 3. 表级指标
# 表大小:pg_relation_size
# 死元组:pg_stat_user_tables
# 索引使用率:pg_stat_user_indexes

# 4. 性能指标
# 查询响应时间
# TPS/QPS
# 缓存命中率
# 检查点频率

# 5. 告警阈值
# 连接数 > 80% max_connections
# 复制延迟 > 10MB
# 磁盘使用率 > 85%
# 缓存命中率 < 95%

风哥教程针对风哥教程针对生产环境建议:监控指标的选择要全面覆盖,但也要避免过度监控导致资源浪费。告警阈值要根据实际业务情况设置,避免告警风暴。学习交流加群风哥QQ113257174

Part03-生产环境项目实施方案

3.1 PostgreSQL pgwatch2安装配置

3.1.1 安装pgwatch2

# 安装pgwatch2

# 1. 使用Docker安装
$ docker run -d –name pgwatch2 \
-p 3000:3000 \
-p 8080:8080 \
-e PW2_PGHOST=192.168.1.100 \
-e PW2_PGPORT=5432 \
-e PW2_PGUSER=fgedu \
-e PW2_PGPASSWORD=Fgedu@2026 \
-e PW2_PGDATABASE=fgedudb \
cybertec/pgwatch2

# 2. 查看容器状态
$ docker ps
CONTAINER ID IMAGE STATUS PORTS NAMES
abc123def456 cybertec/pgwatch2 Up 2 hours 0.0.0.0:3000->3000/tcp, 0.0.0.0:8080->8080/tcp pgwatch2

# 3. 访问Web界面
# http://192.168.1.200:8080

# 4. 配置监控目标
# 在Web界面添加PostgreSQL服务器
# Host: 192.168.1.100
# Port: 5432
# User: fgedu
# Password: Fgedu@2026
# Database: fgedudb

# 5. 选择监控预设
# – Basic:基础监控
# – Standard:标准监控
# – Advanced:高级监控
# – Custom:自定义监控

3.1.2 配置监控指标

# 配置监控指标

# 1. 创建监控用户
$ psql -U fgedu -d fgedudb -c ”
CREATE USER pgwatch2 WITH PASSWORD ‘Pgwatch2@2026’ LOGIN;
GRANT pg_read_all_stats TO pgwatch2;
GRANT pg_read_all_settings TO pgwatch2;

# 2. 创建监控函数
$ psql -U fgedu -d fgedudb -c ”
CREATE SCHEMA IF NOT EXISTS pgwatch2;
GRANT USAGE ON SCHEMA pgwatch2 TO pgwatch2;

# 3. 配置pg_hba.conf
$ vi /postgresql/fgdata/pg_hba.conf
host all pgwatch2 192.168.1.200/32 scram-sha-256

$ pg_ctl -D /postgresql/fgdata reload

# 4. 验证监控数据采集
$ curl http://192.168.1.200:8080/metrics

# 5. 查看监控仪表盘
# 访问 http://192.168.1.200:3000
# 默认用户名:admin
# 默认密码:admin

3.2 PostgreSQL Prometheus监控配置

3.2.1 安装Prometheus

# 安装Prometheus

# 1. 下载Prometheus
$ wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
$ tar -xzf prometheus-2.45.0.linux-amd64.tar.gz
$ mv prometheus-2.45.0.linux-amd64 /postgresql/prometheus

# 2. 创建配置文件
$ vi /postgresql/prometheus/prometheus.yml

global:
scrape_interval: 15s
evaluation_interval: 15s

alerting:
alertmanagers:
– static_configs:
– targets:
– fgedu.localhost:9093

rule_files:
– /postgresql/prometheus/rules/*.yml

scrape_configs:
– job_name: ‘prometheus’
static_configs:
– targets: [‘fgedu.localhost:9090’]

– job_name: ‘postgres’
static_configs:
– targets:
– ‘192.168.1.100:9187’
– ‘192.168.1.101:9187’
– ‘192.168.1.102:9187’
relabel_configs:
– source_labels: [__address__]
target_label: instance
regex: ‘([^:]+):\d+’
replacement: ‘${1}’

# 3. 启动Prometheus
$ cd /postgresql/prometheus
$ ./prometheus –config.file=prometheus.yml –storage.tsdb.path=data &

# 4. 验证Prometheus
$ curl http://fgedu.localhost:9090/-/healthy
Prometheus is Healthy.

# 5. 访问Web界面
# http://192.168.1.200:9090

3.2.2 安装postgres_exporter

# 安装postgres_exporter(在所有PostgreSQL节点执行)

# 1. 下载postgres_exporter
$ wget https://github.com/prometheus-community/postgres_exporter/releases/download/v0.12.1/postgres_exporter-0.12.1.linux-amd64.tar.gz
$ tar -xzf postgres_exporter-0.12.1.linux-amd64.tar.gz
$ mv postgres_exporter-0.12.1.linux-amd64/postgres_exporter /postgresql/fgapp/bin/

# 2. 创建监控用户
$ psql -U fgedu -d fgedudb -c ”
CREATE USER postgres_exporter WITH PASSWORD ‘Exporter@2026’ LOGIN;
GRANT pg_read_all_stats TO postgres_exporter;
GRANT pg_read_all_settings TO postgres_exporter;

# 3. 配置数据源
$ vi /postgresql/scripts/postgres_exporter.env

DATA_SOURCE_NAME=”postgresql://postgres_exporter:Exporter@2026@fgedu.localhost:5432/fgedudb?sslmode=disable”

# 4. 创建systemd服务
$ vi /etc/systemd/system/postgres_exporter.service

[Unit]
Description=PostgreSQL Exporter
After=network.target

[Service]
Type=simple
User=pgsql
EnvironmentFile=/postgresql/scripts/postgres_exporter.env
ExecStart=/postgresql/fgapp/bin/postgres_exporter –web.listen-address=:9187
Restart=always

[Install]
WantedBy=multi-user.target

# 5. 启动服务
$ systemctl daemon-reload
$ systemctl start postgres_exporter
$ systemctl enable postgres_exporter

# 6. 验证指标采集
$ curl http://fgedu.localhost:9187/metrics | head -20

# HELP pg_up Whether the PostgreSQL server is up.
# TYPE pg_up gauge
pg_up 1
# HELP pg_stat_activity_count Number of connections
# TYPE pg_stat_activity_count gauge
pg_stat_activity_count{datid=”16384″,datname=”fgedudb”} 5

3.3 PostgreSQL告警规则配置实战

3.3.1 配置告警规则

# 配置告警规则

# 1. 创建告警规则目录
$ mkdir -p /postgresql/prometheus/rules

# 2. 创建PostgreSQL告警规则
$ vi /postgresql/prometheus/rules/postgres_alerts.yml

groups:
– name: postgres_alerts
rules:
# 数据库宕机告警
– alert: PostgreSQLDown
expr: pg_up == 0
for: 1m
labels:
severity: critical
annotations:
summary: “PostgreSQL实例宕机”
description: “PostgreSQL实例 {{ $labels.instance }} 已宕机超过1分钟”

# 连接数过高告警
– alert: PostgreSQLTooManyConnections
expr: pg_stat_activity_count / pg_settings_max_connections * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: “PostgreSQL连接数过高”
description: “PostgreSQL实例 {{ $labels.instance }} 连接数已超过80%”

# 复制延迟告警
– alert: PostgreSQLReplicationLag
expr: pg_replication_lag_bytes > 10485760
for: 5m
labels:
severity: warning
annotations:
summary: “PostgreSQL复制延迟过大”
description: “PostgreSQL复制延迟已超过10MB”

# 磁盘空间不足告警
– alert: PostgreSQLDiskSpaceLow
expr: pg_database_size_bytes / 1073741824 > 100
for: 5m
labels:
severity: warning
annotations:
summary: “PostgreSQL数据库空间过大”
description: “PostgreSQL数据库 {{ $labels.datname }} 大小已超过100GB”

# 死锁告警
– alert: PostgreSQLDeadlocks
expr: rate(pg_stat_database_deadlocks[5m]) > 0
for: 5m
labels:
severity: warning
annotations:
summary: “PostgreSQL检测到死锁”
description: “PostgreSQL实例 {{ $labels.instance }} 检测到死锁”

# 慢查询告警
– alert: PostgreSQLSlowQueries
expr: pg_stat_statements_mean_exec_time_seconds > 1
for: 5m
labels:
severity: info
annotations:
summary: “PostgreSQL存在慢查询”
description: “PostgreSQL平均查询时间超过1秒”

# 3. 重载Prometheus配置
$ curl -X POST http://fgedu.localhost:9090/-/reload

# 4. 验证告警规则
$ curl http://fgedu.localhost:9090/api/v1/rules | jq

3.3.2 配置Alertmanager

# 配置Alertmanager

# 1. 下载Alertmanager
$ wget https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz
$ tar -xzf alertmanager-0.26.0.linux-amd64.tar.gz
$ mv alertmanager-0.26.0.linux-amd64 /postgresql/alertmanager

# 2. 创建配置文件
$ vi /postgresql/alertmanager/alertmanager.yml

global:
resolve_timeout: 5m
smtp_smarthost: ‘smtp.fgedu.net.cn:25’
smtp_from: ‘alertmanager@fgedu.net.cn’
smtp_auth_username: ‘alertmanager@fgedu.net.cn’
smtp_auth_password: ‘password’

route:
group_by: [‘alertname’, ‘severity’]
group_wait: 30s
group_interval: 5m
repeat_interval: 4h
receiver: ‘default-receiver’
routes:
– match:
severity: critical
receiver: ‘critical-receiver’
– match:
severity: warning
receiver: ‘warning-receiver’

receivers:
– name: ‘default-receiver’
email_configs:
– to: ‘dba@fgedu.net.cn’
send_resolved: true

– name: ‘critical-receiver’
email_configs:
– to: ‘dba@fgedu.net.cn,oncall@fgedu.net.cn’
send_resolved: true
webhook_configs:
– url: ‘http://192.168.1.200:8080/webhook’
send_resolved: true

– name: ‘warning-receiver’
email_configs:
– to: ‘dba@fgedu.net.cn’
send_resolved: true

inhibit_rules:
– source_match:
severity: ‘critical’
target_match:
severity: ‘warning’
equal: [‘alertname’, ‘instance’]

# 3. 启动Alertmanager
$ cd /postgresql/alertmanager
$ ./alertmanager –config.file=alertmanager.yml –storage.path=data &

# 4. 验证Alertmanager
$ curl http://fgedu.localhost:9093/-/healthy
OK

# 5. 访问Web界面
# http://192.168.1.200:9093

风哥提示:告警配置要根据实际业务需求设置合理的阈值和告警级别。避免告警风暴,确保重要告警能够及时通知到相关人员。更多学习教程公众号风哥教程itpux_com

Part04-生产案例与实战讲解

4.1 PostgreSQL监控实战案例

# PostgreSQL监控实战案例

# 场景:监控PostgreSQL集群状态

# 1. 查看连接数监控
$ curl ‘http://fgedu.localhost:9090/api/v1/query?query=pg_stat_activity_count’
{
“status”: “success”,
“data”: {
“result”: [
{
“metric”: {
“datname”: “fgedudb”,
“instance”: “192.168.1.100”
},
“value”: [1712460000, “15”]
}
]
}
}

# 2. 查看复制状态
$ curl ‘http://fgedu.localhost:9090/api/v1/query?query=pg_replication_lag_bytes’
{
“status”: “success”,
“data”: {
“result”: [
{
“metric”: {
“instance”: “192.168.1.100”
},
“value”: [1712460000, “0”]
}
]
}
}

# 3. 查看缓冲区命中率
$ curl ‘http://fgedu.localhost:9090/api/v1/query?query=pg_stat_bgwriter_buffers_hit_ratio’
{
“status”: “success”,
“data”: {
“result”: [
{
“metric”: {
“instance”: “192.168.1.100”
},
“value”: [1712460000, “0.98”]
}
]
}
}

# 4. 查看表大小
$ psql -U fgedu -d fgedudb -c ”
SELECT
schemaname,
relname,
pg_size_pretty(pg_total_relation_size(schemaname||’.’||relname)) AS total_size,
n_live_tup,
n_dead_tup
FROM pg_stat_user_tables
ORDER BY pg_total_relation_size(schemaname||’.’||relname) DESC
LIMIT 10;

schemaname | relname | total_size | n_live_tup | n_dead_tup
————+——————+————+————+————
public | fgedu_orders | 50 GB | 50000000 | 100000
public | fgedu_customers | 20 GB | 20000000 | 50000
public | fgedu_products | 10 GB | 10000000 | 20000
(3 rows)

# 5. 查看慢查询
$ psql -U fgedu -d fgedudb -c ”
SELECT
query,
calls,
total_exec_time / 1000 / 60 as total_minutes,
mean_exec_time / 1000 as mean_seconds
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;

query | calls | total_minutes | mean_seconds
———————————————–+——–+—————+————–
SELECT * FROM fgedu_orders WHERE … | 12345 | 120.50 | 0.58
UPDATE fgedu_orders SET status = … | 5678 | 85.30 | 0.90
INSERT INTO fgedu_logs (…) VALUES … | 98765 | 45.20 | 0.03
(3 rows)

4.2 PostgreSQL告警实战案例

# PostgreSQL告警实战案例

# 场景:处理PostgreSQL连接数过高告警

# 1. 收到告警
# Alert: PostgreSQLTooManyConnections
# Instance: 192.168.1.100
# Current connections: 85% of max_connections

# 2. 查看当前连接
$ psql -U fgedu -d fgedudb -c ”
SELECT
datname,
usename,
fgapplication_name,
client_addr,
state,
count(*) as conn_count
FROM pg_stat_activity
GROUP BY datname, usename, fgapplication_name, client_addr, state
ORDER BY conn_count DESC;

datname | usename | fgapplication_name | client_addr | state | conn_count
———-+———+——————+—————+——–+————
fgedudb | fgapp_user| fgedu_fgapp | 192.168.1.50 | active | 150
fgedudb | fgapp_user| fgedu_fgapp | 192.168.1.51 | idle | 120
fgedudb | fgedu | psql | 192.168.1.100 | active | 5
(3 rows)

# 3. 查看空闲连接
$ psql -U fgedu -d fgedudb -c ”
SELECT pid, usename, fgapplication_name, client_addr,
now() – state_change as idle_duration
FROM pg_stat_activity
WHERE state = ‘idle’
ORDER BY idle_duration DESC
LIMIT 20;

pid | usename | fgapplication_name | client_addr | idle_duration
——-+———+——————+—————+—————
12345 | fgapp_user| fgedu_fgapp | 192.168.1.50 | 02:30:00
12346 | fgapp_user| fgedu_fgapp | 192.168.1.51 | 01:45:00
(3 rows)

# 4. 终止长时间空闲连接
$ psql -U fgedu -d fgedudb -c ”
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = ‘idle’
AND now() – state_change > interval ‘1 hour’
AND usename = ‘fgapp_user’;

pg_terminate_backend
———————-
t
t
(2 rows)

# 5. 调整连接池配置
$ vi /postgresql/fgdata/postgresql.conf
max_connections = 300
idle_in_transaction_session_timeout = 600000

$ pg_ctl -D /postgresql/fgdata reload

# 6. 确认告警恢复
$ curl ‘http://fgedu.localhost:9093/api/v1/alerts’ | jq
{
“status”: “success”,
“data”: []
}

4.3 PostgreSQL仪表盘配置案例

# PostgreSQL仪表盘配置案例

# 1. 安装Grafana
$ yum install -y grafana
$ systemctl start grafana-server
$ systemctl enable grafana-server

# 2. 添加Prometheus数据源
# 访问 http://192.168.1.200:3000
# Configuration -> Data Sources -> Add data source
# 选择 Prometheus
# URL: http://fgedu.localhost:9090

# 3. 导入PostgreSQL仪表盘
# Dashboards -> Import
# 输入仪表盘ID: 9628 (PostgreSQL Database)
# 或上传JSON文件

# 4. 自定义仪表盘面板

# 连接数面板
# Query: pg_stat_activity_count
# Legend: {{instance}} – {{datname}}

# 复制延迟面板
# Query: pg_replication_lag_bytes / 1024 / 1024
# Legend: {{instance}}
# Unit: MB

# TPS面板
# Query: rate(pg_stat_database_xact_commit[5m]) + rate(pg_stat_database_xact_rollback[5m])
# Legend: {{instance}}
# Unit: transactions/sec

# 缓存命中率面板
# Query: pg_stat_bgwriter_buffers_hit / (pg_stat_bgwriter_buffers_hit + pg_stat_bgwriter_buffers_miss) * 100
# Legend: {{instance}}
# Unit: percent (0-100)

# 5. 配置仪表盘变量
# 变量名: instance
# 类型: Query
# Query: label_values(pg_up, instance)

# 6. 设置仪表盘刷新间隔
# Refresh: 30s
# Time range: Last 6 hours

风哥教程针对风哥教程针对生产环境建议:监控仪表盘要简洁明了,重点突出关键指标。建议创建多个仪表盘,分别用于概览、详细分析、告警历史等不同场景。from PostgreSQL视频:www.itpux.com

Part05-风哥经验总结与分享

5.1 PostgreSQL监控最佳实践

PostgreSQL监控最佳实践:

  • 全面覆盖:监控系统层和数据库层
  • 合理阈值:根据业务设置告警阈值
  • 及时响应:建立告警响应机制
  • 定期检查:定期检查监控数据
  • 持续优化:根据实际情况调整监控策略
  • 文档记录:记录监控配置和操作流程

5.2 PostgreSQL监控脚本

#!/bin/bash
# pg_monitor.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn

# PostgreSQL监控脚本

PGHOME=/postgresql/fgapp
PGHOST=fgedu.localhost
PGPORT=5432
PGUSER=fgedu
PGDATABASE=fgedudb
LOG_FILE=/postgresql/scripts/logs/pg_monitor.log

log_message() {
echo “$(date ‘+%Y-%m-%d %H:%M:%S’) – $1” >> $LOG_FILE
}

# 检查数据库状态
check_db_status() {
local result=$($PGHOME/bin/pg_isready -h $PGHOST -p $PGPORT -U $PGUSER -d $PGDATABASE 2>/dev/null)

if echo “$result” | grep -q “accepting connections”; then
log_message “数据库状态正常”
else
log_message “ERROR: 数据库状态异常 – $result”
return 1
fi
}

# 检查连接数
check_connections() {
local count=$($PGHOME/bin/psql -h $PGHOST -p $PGPORT -U $PGUSER -d $PGDATABASE -t -c ”
SELECT count(*) FROM pg_stat_activity;
” 2>/dev/null | tr -d ‘ ‘)

local max_conn=$($PGHOME/bin/psql -h $PGHOST -p $PGPORT -U $PGUSER -d $PGDATABASE -t -c ”
SELECT setting FROM pg_settings WHERE name = ‘max_connections’;
” 2>/dev/null | tr -d ‘ ‘)

local ratio=$(echo “scale=2; $count * 100 / $max_conn” | bc)

if [ $(echo “$ratio > 80” | bc) -eq 1 ]; then
log_message “WARNING: 连接数过高 – 当前: $count, 最大: $max_conn, 比率: ${ratio}%”
else
log_message “连接数正常 – 当前: $count, 最大: $max_conn, 比率: ${ratio}%”
fi
}

# 检查复制状态
check_replication() {
local is_primary=$($PGHOME/bin/psql -h $PGHOST -p $PGPORT -U $PGUSER -d $PGDATABASE -t -c ”
SELECT NOT pg_is_in_recovery();
” 2>/dev/null | tr -d ‘ ‘)

if [ “$is_primary” = “t” ]; then
local lag=$($PGHOME/bin/psql -h $PGHOST -p $PGPORT -U $PGUSER -d $PGDATABASE -t -c ”
SELECT COALESCE(max(pg_wal_lsn_diff(sent_lsn, replay_lsn)), 0)
FROM pg_stat_replication;
” 2>/dev/null | tr -d ‘ ‘)

if [ “$lag” -gt 10485760 ]; then
log_message “WARNING: 复制延迟过大 – ${lag} bytes”
else
log_message “复制状态正常 – 延迟: ${lag} bytes”
fi
else
log_message “当前为备库”
fi
}

# 检查锁等待
check_locks() {
local count=$($PGHOME/bin/psql -h $PGHOST -p $PGPORT -U $PGUSER -d $PGDATABASE -t -c ”
SELECT count(*) FROM pg_locks WHERE NOT granted;
” 2>/dev/null | tr -d ‘ ‘)

if [ “$count” -gt 10 ]; then
log_message “WARNING: 锁等待过多 – $count”
else
log_message “锁等待正常 – $count”
fi
}

# 主函数
main() {
log_message “==========================================”
log_message “开始PostgreSQL监控检查”
log_message “==========================================”

check_db_status
check_connections
check_replication
check_locks

log_message “==========================================”
log_message “监控检查完成”
log_message “==========================================”
}

main

5.3 PostgreSQL监控检查清单

# PostgreSQL监控检查清单

# 监控系统部署
– [ ] 安装Prometheus
– [ ] 安装postgres_exporter
– [ ] 安装Grafana
– [ ] 配置数据源
– [ ] 导入仪表盘

# 监控指标配置
– [ ] 系统资源监控
– [ ] 连接数监控
– [ ] 复制状态监控
– [ ] 锁等待监控
– [ ] 查询性能监控

# 告警规则配置
– [ ] 数据库宕机告警
– [ ] 连接数过高告警
– [ ] 复制延迟告警
– [ ] 磁盘空间告警
– [ ] 死锁告警

# 告警通知配置
– [ ] 邮件通知
– [ ] 钉钉/企业微信通知
– [ ] 短信通知
– [ ] 值班人员通知

# 监控维护
– [ ] 定期检查监控数据
– [ ] 定期调整告警阈值
– [ ] 定期清理历史数据
– [ ] 定期备份监控配置

风哥提示:监控告警是数据库运维的重要保障,需要在平时多加练习,熟悉各种监控工具的使用。建议建立完善的监控体系,及时发现和处理问题,确保数据库稳定运行。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息