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

目录

描述

结合脚本

占位符

[repmgr命令 生成的事件:](#repmgr命令 生成的事件:)

[repmgrd 生成的事件(流复制模式):](#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 提供的值 可能包含空格,因此应在提供的命令配置中用引号引起来,例如:

bash 复制代码
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将通知类型过滤为明确命名的类型 ,例如:

只通知以下类型的事件:

bash 复制代码
event_notifications='primary_register,standby_register,witness_register'

repmgr命令 生成的事件:

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

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

通知脚本编写

shell 脚本

shell 脚本描述

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

bash 复制代码
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 中有以下日志

bash 复制代码
[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"'

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

相关推荐
wangchen_04 分钟前
MySQL复合查询
数据库·mysql
hygge99914 分钟前
MySQL 全体系深度解析(存储引擎、事务、日志、MVCC、锁、索引、执行计划、复制、调优)
数据库·经验分享·mysql·adb·面试
百***659524 分钟前
PON架构(全光网络)
网络·数据库·架构
Databend28 分钟前
Databend SQL 存储过程使用指南
数据库
冒泡的肥皂40 分钟前
说下数据存储
数据库·后端·mysql
码河漫步2 小时前
win11安装mysql社区版数据库
数据库·mysql
Wang's Blog2 小时前
MySQL: 存储引擎深度解析:Memory与Federated的特性与应用场景
数据库·mysql
学习中的程序媛~2 小时前
Spring 事务(@Transactional)与异步(@Async / CompletableFuture)结合的陷阱与最佳实践
java·数据库·sql
员大头硬花生2 小时前
九、InnoDB引擎-MVCC
数据库·sql·mysql
一条闲鱼_mytube2 小时前
mvcc 简介
数据库