Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

第 13 章 为集群事件触发脚本

Pacemaker 集群是一个事件驱动的系统,其中事件可能是资源或节点故障、配置更改或资源启动或停止。您可以将 Pacemaker 集群警报配置为在集群事件发生时采取一些外部操作。您可以通过以下两种方式之一配置集群警报:

13.1. Pacemaker 警报代理(红帽企业 Linux 7.3 及更新的版本)

您可以创建 Pacemaker 警报代理,以便在集群事件发生时采取一些外部操作。集群使用环境变量将事件信息传递给代理。代理可以执行任何操作,比如发送电子邮件信息或登录到某个文件或更新监控系统。

13.1.1. 使用示例警报代理

当使用示例警报代理时,您应该检查该脚本以确保它适合您的需要。这些示例代理是作为特定集群环境自定义脚本的起点。请注意,红帽支持警报代理脚本用来与 Pacemaker 通信的界面,但红帽并不支持自定义代理本身。
要使用示例警报代理中的一个,您必须在集群中的每个节点上安装代理。例如,以下命令将 alert_file.sh.sample 脚本安装为 alert_file.sh
# install --mode=0755 /usr/share/pacemaker/alerts/alert_file.sh.sample /var/lib/pacemaker/alert_file.sh
安装脚本后,您可以创建使用该脚本的警报。
以下示例配置了使用安装的 alert_file.sh 警报代理将事件记录到文件中的警报。以用户 hacluster 身份运行的警报代理,该用户具有最小权限集。
这个示例创建日志文件 pcmk_alert_file.log,该文件将用于记录事件。然后,它会创建警报代理,并添加到日志文件的路径作为其接收者。
# touch /var/log/pcmk_alert_file.log
# chown hacluster:haclient /var/log/pcmk_alert_file.log
# chmod 600 /var/log/pcmk_alert_file.log 
# pcs alert create id=alert_file description="Log events to a file." path=/var/lib/pacemaker/alert_file.sh 
# pcs alert recipient add alert_file id=my-alert_logfile value=/var/log/pcmk_alert_file.log 
以下示例将 alert_snmp.sh.sample 脚本安装为 alert_snmp.sh,并配置使用安装的 alert_snmp.sh 警报代理将集群事件作为 SNMP 陷阱发送的警报。默认情况下,该脚本会发送除成功监控调用 SNMP 服务器外的所有事件。这个示例将时间戳格式配置为 meta 选项。有关 meta 选项的详情请参考 第 13.1.5 节 “警报元数据选项”。配置警报后,本例配置警报的接收者并显示警报配置。
# install --mode=0755 /usr/share/pacemaker/alerts/alert_snmp.sh.sample /var/lib/pacemaker/alert_snmp.sh
# pcs alert create id=snmp_alert path=/var/lib/pacemaker/alert_snmp.sh meta timestamp-format="%Y-%m-%d,%H:%M:%S.%01N"
# pcs alert recipient add snmp_alert value=192.168.1.2
# pcs alert
Alerts:
 Alert: snmp_alert (path=/var/lib/pacemaker/alert_snmp.sh)
  Meta options: timestamp-format=%Y-%m-%d,%H:%M:%S.%01N.
  Recipients:
   Recipient: snmp_alert-recipient (value=192.168.1.2)
以下示例安装 alert_smtp.sh 代理,然后配置使用安装的警报代理将集群事件作为电子邮件消息发送的警报。配置警报后,本示例配置了接收方并显示警报配置。
# install --mode=0755 /usr/share/pacemaker/alerts/alert_smtp.sh.sample /var/lib/pacemaker/alert_smtp.sh
# pcs alert create id=smtp_alert path=/var/lib/pacemaker/alert_smtp.sh options email_sender=donotreply@example.com
# pcs alert recipient add smtp_alert value=admin@example.com
# pcs alert
Alerts:
 Alert: smtp_alert (path=/var/lib/pacemaker/alert_smtp.sh)
  Options: email_sender=donotreply@example.com
  Recipients:
   Recipient: smtp_alert-recipient (value=admin@example.com)
有关 pcs alert create 和 pcs alert receiver add 命令格式的更多信息,请参阅 第 13.1.2 节 “创建警报”第 13.1.4 节 “警报 Recipients”

13.1.2. 创建警报

以下命令创建集群警报。您配置的选项是特定于代理的配置文件,这些值会被传递给您指定为额外环境变量的路径的警报代理脚本。如果没有为 id 指定值,则会生成一个值。如需关于警报 meta 选项的信息,请参阅 第 13.1.5 节 “警报元数据选项”
pcs alert create path=path [id=alert-id] [description=description] [options [option=value]...] [meta [meta-option=value]...]
可能会配置多个警报代理,集群会在每个事件中调用它们。只有集群节点上才会调用警报代理。会为涉及 Pacemaker 远程节点的事件调用它们,但不会在这些节点上调用它们。
以下示例创建了一个简单的警报,它将为每个事件调用 myscript.sh
# pcs alert create id=my_alert path=/path/to/myscript.sh
有关如何创建使用其中一个示例警报代理的集群警报的示例,请参考 第 13.1.1 节 “使用示例警报代理”

13.1.3. 显示、修改和删除警报

以下命令显示所有配置的警报以及配置选项的值。
pcs alert [config|show]
以下命令使用指定的 alert-id 值更新现有警报。
pcs alert update alert-id [path=path] [description=description] [options [option=value]...] [meta [meta-option=value]...]
以下命令移除具有指定 alert-id 值的警报。
pcs alert remove alert-id
或者,您可以运行 pcs alert delete 命令,该命令与 pcs alert remove 命令相同。pcs alert deletepcs alert remove 命令都允许您指定要删除的多个警报。

13.1.4. 警报 Recipients

通常,警报是针对接收方的。因此,每个警报可能被额外配置为一个或多个接收方。集群将为每个接收者单独调用代理。
接收者可以是警告代理可识别的任何内容:IP 地址、电子邮件地址、文件名或特定代理支持的任何内容。
以下命令为指定警报添加新的接收者。
pcs alert recipient add alert-id value=recipient-value [id=recipient-id] [description=description] [options [option=value]...] [meta [meta-option=value]...]
以下命令更新现有警报接收者。
pcs alert recipient update recipient-id [value=recipient-value] [description=description] [options [option=value]...] [meta [meta-option=value]...]
以下命令移除指定警报接收者。
pcs alert recipient remove recipient-id
或者,您可以运行 pcs alert receiver delete 命令,该命令与 pcs alert receiver remove 命令相同。pcs alert receiver removepcs alert receiver delete 命令都允许您删除多个警报接收者。
以下示例命令将警报接收者 my-alert-reci pipient -id 添加到警报 my-alert 中。这会将群集配置为调用为每个事件配置了 my-alert 的警报脚本,并将接收者 some-address 作为环境变量传递。
#  pcs alert recipient add my-alert value=my-alert-recipient id=my-recipient-id options value=some-address

13.1.5. 警报元数据选项

与资源代理一样,可以对警报代理配置 meta 选项来影响 Pacemaker 调用它们的方式。表 13.1 “警报元数据选项” 描述警报 meta 选项。meta 选项可以为每个警报代理和接收者配置。

表 13.1. 警报元数据选项

meta-Attribute默认值描述
timestamp-format
%H:%M:%S.%06N
将事件时间戳发送到代理时,集群将使用的格式。这是与 date(1)命令一起使用的字符串。
timeout
30s
如果警报代理没有在这段时间内完成,它将被终止。
以下示例配置了调用脚本 myscript.sh 的警报,然后为警报添加两个接收者。第一个接收者 ID 为 my-alert-recipient1,第二个收件人的 ID 为 my-alert-recipient2。这个脚本会为每个事件调用两次,每个调用都使用 15 秒超时。一个调用将被传递给接收者 someuser@example.com,格式为 %D %H:%M,另一个调用将被传递给接收者 otheruser@example.com,格式为 %c。
# pcs alert create id=my-alert path=/path/to/myscript.sh meta timeout=15s
# pcs alert recipient add my-alert value=someuser@example.com id=my-alert-recipient1 meta timestamp-format="%D %H:%M"
# pcs alert recipient add my-alert value=otheruser@example.com id=my-alert-recipient2 meta timestamp-format=%c

13.1.6. 警报配置命令示例

以下后续示例演示了一些基本警报配置命令,以显示用于创建警报、添加接收方和显示配置的警报的格式。请注意,虽然您必须在集群中的每个节点上安装警报代理,但您需要只运行一次 'pcs' 命令。
以下命令创建了一个简单的警报,为警报添加两个接受者,并显示配置的值。
  • 由于没有指定警报 ID 值,系统会创建警报的警报 ID 值
  • 第一个接收者创建命令指定 rec_value 的接收者。由于这个命令没有指定接收者 ID,alert-recipient 的值被用作接收者 ID。
  • 第二个接收者创建命令指定 rec_value2 的接收者。此命令 为接收者指定 my- repient 的接收者 ID。
# pcs alert create path=/my/path
# pcs alert recipient add alert value=rec_value
# pcs alert recipient add alert value=rec_value2 id=my-recipient
# pcs alert config
Alerts:
 Alert: alert (path=/my/path)
  Recipients:
   Recipient: alert-recipient (value=rec_value)
   Recipient: my-recipient (value=rec_value2)
以下命令添加第二个警报以及该警报的接收者。第二个警报的警报 ID 是 my-alert,接收者值为 my-other-recipient。因为没有指定接收者 ID,系统会提供接收者 ID my-alert-recipient
# pcs alert create id=my-alert path=/path/to/script description=alert_description options option1=value1 opt=val meta timeout=50s timestamp-format="%H%B%S"
# pcs alert recipient add my-alert value=my-other-recipient
# pcs alert
Alerts:
 Alert: alert (path=/my/path)
  Recipients:
   Recipient: alert-recipient (value=rec_value)
   Recipient: my-recipient (value=rec_value2)
 Alert: my-alert (path=/path/to/script)
  Description: alert_description
  Options: opt=val option1=value1
  Meta options: timestamp-format=%H%B%S timeout=50s
  Recipients:
   Recipient: my-alert-recipient (value=my-other-recipient)
以下命令修改警报 my-alert 和接收者 my-alert -recipient 的警报值。
# pcs alert update my-alert options option1=newvalue1 meta timestamp-format="%H%M%S"
# pcs alert recipient update my-alert-recipient options option1=new meta timeout=60s
# pcs alert
Alerts:
 Alert: alert (path=/my/path)
  Recipients:
   Recipient: alert-recipient (value=rec_value)
   Recipient: my-recipient (value=rec_value2)
 Alert: my-alert (path=/path/to/script)
  Description: alert_description
  Options: opt=val option1=newvalue1
  Meta options: timestamp-format=%H%M%S timeout=50s
  Recipients:
   Recipient: my-alert-recipient (value=my-other-recipient)
    Options: option1=new
    Meta options: timeout=60s
以下命令从 警报 中删除接收者 my-alert-recipient
# pcs alert recipient remove my-recipient
# pcs alert
Alerts:
 Alert: alert (path=/my/path)
  Recipients:
   Recipient: alert-recipient (value=rec_value)
 Alert: my-alert (path=/path/to/script)
  Description: alert_description
  Meta options: timestamp-format="%M%B%S" timeout=50s
  Meta options: m=newval meta-option1=2
  Recipients:
   Recipient: my-alert-recipient (value=my-other-recipient)
    Options: option1=new
    Meta options: timeout=60s
以下命令将从配置中删除 myalert
# pcs alert remove my-alert
# pcs alert
Alerts:
 Alert: alert (path=/my/path)
  Recipients:
   Recipient: alert-recipient (value=rec_value)

13.1.7. 编写警报代理

Pacemaker 警报有三种类型:节点警报、保护警报和资源警报。传递给警报代理的环境变量可能会根据警报类型而有所不同。表 13.2 “传递给警报代理的环境变量” 描述传递给警报代理的环境变量,并指定环境变量何时与特定警报类型关联。

表 13.2. 传递给警报代理的环境变量

环境变量描述
CRM_alert_kind
警报类型(节点、保护或资源)
CRM_alert_version
Pacemaker 发送警报的版本
CRM_alert_recipient
配置的接收者
CRM_alert_node_sequence
每当在本地节点上发出警报时,序列数量会增加,它可以用来引用 Pacemaker 发出警报的顺序。稍后发生事件警告的序列号比之前的事件的警报要高。请注意,这个数字没有集群范围的含义。
CRM_alert_timestamp
执行代理前创建的时间戳,采用由 timestamp-format meta 选项指定的格式。这可以确保在事件发生时代理有一个可靠、高度准确的时间,无论代理本身何时被调用(这可能会因为系统负载或其他情况而延迟)。
CRM_alert_node
受影响节点的名称
CRM_alert_desc
有关事件的详情。对于节点警报,这是节点的当前状态(成员或丢失)。对于隔离警报,这是请求的隔离操作的总结,其中包括原始数据、目标以及隔离操作错误代码(若有)。对于资源警报,这是等同于 CRM_alert_status 的可读字符串。
CRM_alert_nodeid
状态更改的节点 ID(仅由节点警报提供)
CRM_alert_task
请求的隔离或资源操作(仅由隔离和资源警报提供)
CRM_alert_rc
保护或资源操作的数字返回代码(仅由隔离和资源警告提供)
CRM_alert_rsc
受影响资源的名称(仅限资源警报)
CRM_alert_interval
资源操作的时间间隔(仅限资源警报)
CRM_alert_target_rc
操作的预期数字返回代码(仅用于资源警报)
CRM_alert_status
Pacemaker 用来表示操作结果的数字代码(仅用于资源警报)
在编写警报代理时,您必须考虑以下问题。
  • 警告代理可以在没有接收者的情况下被调用(如果没有配置任何接收者),因此代理必须能够处理这种情况,即使它只在那种情况下才会退出。用户可以修改配置阶段,并在以后添加一个接收者。
  • 如果为警报配置了多个接收者,则会为每个接收者调用一个警报代理。如果代理无法同时运行,则应该只使用单个的接收者进行配置。不过,代理可以自由地将接收者解析为一个列表。
  • 当发生集群事件时,所有警报都会与独立进程同时触发。根据配置了警报和接收方的数量以及警报代理中的操作,可能会发生大量负载。可以编写代理来考虑这一点,例如将资源密集型操作排队到其他实例中,而不是直接执行。
  • 警报代理以 hacluster 用户身份运行,该用户具有最小权限集。如果代理需要额外的特权,建议配置 sudo 以允许代理以具有适当特权的另一用户身份运行必要的命令。
  • 请小心地验证和清理用户配置的参数,如 CRM_alert_timestamp (由用户配置的 timestamp-format)、CRM_alert_recipient 和所有警报选项指定的内容。这是防止配置错误所必需的。此外,如果某些用户可以在没有 hacluster-level 访问集群节点的情况下修改 CIB,则也是潜在的安全问题,您应该避免注入代码的可能性。
  • 如果群集包含将 on-fail 参数设置为 隔离 的操作的资源,则失败时会有多个隔离通知,每个资源都有一个用于设置此参数的资源,再加上一个附加通知。STONITH 守护进程和 crmd 守护进程都将发送通知。pacemaker 在这种情况下只能执行一个实际隔离操作,无论发送了多少条通知。
注意
警报接口设计为与 ocf:pacemaker:ClusterMon 资源使用 的外部脚本界面向后兼容。为了保持这种兼容性,传递给警报代理的环境变量会预先带有 CRM_notify_CRM_alert_。兼容性问题之一是 ClusterMon 资源以 root 用户身份运行外部脚本,而警报代理则以 hacluster 用户身份运行。有关配置由 ClusterMon 触发的脚本的详情请参考 第 13.2 节 “使用监控资源的事件通知”