【PG高可用】六 PostgreSQL高可用之repmgr事件通知

目录

描述

结合脚本

占位符

repmgr命令 生成的事件:

repmgrd 生成的事件(流复制模式):


描述

每次repmgr或repmgrd执行重大事件时,都会将该事件的记录连同时间戳、失败或成功的标识以及进一步的详细信息(如果适用)一起写入表中。这对于了解影响复制集群的事件的概况很有用。但请注意,此表具有咨询性质,应与repmgrrepmgr.events和 PostgreSQL 日志结合使用 以获取任何事件的详细信息。

或者,使用repmgr cluster event输出格式化的事件列表。

结合脚本

此外,事件通知可以传递给用户定义的程序或脚本,以便采取进一步行动,例如发送电子邮件通知。这可以通过设置配置文件repmgr.conf中的参数event_notification_command 来完成。

 ./repmgrEventNotifiction -eventTimestamp "%t" -primaryNodeId "%p" -primaryConnInfo "%c" -primaryNodeName "%a" -nodeId "%n" -eventType "%e" -isSuccess "%s" -eventDetails "%d"

占位符

所有事件通知均提供了以下格式占位符:

%n 节点 ID

%e 事件类型

%s 成功 (1) 或失败 (0)

%t 时间戳

%d 细节

注意:

%t和 %d 提供的值 可能包含空格,因此应在提供的命令配置中用引号引起来,例如:

event_notification_command='/path/to/some/script %n %e %s "%t" "%d"'

为部分事件通知提供了以下参数;它们的含义可能根据上下文而改变:

%p

当前主节点的节点 ID  (repmgr standby register and repmgr standby follow)

降级主节点的节点 ID   (repmgr standby switchover only)

前主节点的节点 ID (repmgrd_failover_promote only)

%c conninfo主节点的字符串 (repmgr standby registerrepmgr standby follow

%a 当前主节点的名称 (repmgr standby registerrepmgr standby follow

%c和 %a 提供的值可能包含空格,因此应始终用引号引起来。

默认情况下,所有通知类型都将传递给指定的脚本;可以使用参数event_notifications将通知类型过滤为明确命名的类型 ,例如:

只通知以下类型的事件:

event_notifications='primary_register,standby_register,witness_register'

repmgr命令 生成的事件:

repmgrd 生成的事件(流复制模式):

请注意,在某些情况下(例如,当找不到复制集群主节点时),将无法将条目写入表中 repmgr.events ,在这种情况下,通过执行脚本event_notification_command 可以通过生成某种形式的通知作为后备。

通知脚本编写

shell 脚本

shell 脚本描述

将事件通知写到文件 /data/storage/repmgr/repmgr_notifications.log 中

event_notification_command='echo -e "[Repmgr Event Notification]\n"primary_node_id:"%p\n"primary_conninfo:"%c\n"primary_node_name:"%a\n"node_id:"%n\n"event_type:"%e\n"is_success:"%s\n"timestamp:"%t\n"details:"%d\n">> /data/storage/repmgr/repmgr_notifications.log'

将该配置文件写到/etc/repmgr.conf配置文件中

执行

repmgr -f /etc/repmgr.conf standby register

查看文件 /data/storage/repmgr/repmgr_notifications.log 中有以下日志

[Repmgr Event Notification]
primary_node_id:2
primary_conninfo:host=10.90.211.30 port=5432 user=repmgr dbname=repmgr connect_timeout=2
primary_node_name:node2
node_id:3
event_type:standby_register
is_success:1
timestamp:2024-07-16 17:03:31.912615+08
details:standby registration succeeded; upstream node ID is 2

shell 脚本验证成功

GO脚本

event_notification_command='/data/storage/repmgr/scripts/repmgrEventNotifiction -eventTimestamp "%t" -primaryNodeId "%p" -primaryConnInfo "%c" -primaryNodeName "%a" -nodeId "%n" -eventType "%e" -isSuccess "%s" -eventDetails "%d"'

发送通知到办公通讯软件中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DBA之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值