Chapter 26. Triggering scripts for cluster events
A Pacemaker cluster is an event-driven system, where an event might be a resource or node failure, a configuration change, or a resource starting or stopping. You can configure Pacemaker cluster alerts to take some external action when a cluster event occurs by means of alert agents, which are external programs that the cluster calls in the same manner as the cluster calls resource agents to handle resource configuration and operation.
The cluster passes information about the event to the agent by means of environment variables. Agents can do anything with this information, such as send an email message or log to a file or update a monitoring system.
Pacemaker provides several sample alert agents, which are installed in
/usr/share/pacemaker/alertsby default. These sample scripts may be copied and used as is, or they may be used as templates to be edited to suit your purposes. Refer to the source code of the sample agents for the full set of attributes they support.
- If the sample alert agents do not meet your needs, you can write your own alert agents for a Pacemaker alert to call.
26.1. Installing and configuring sample alert agents
When you use one of the sample alert agents, you should review the script to ensure that it suits your needs. These sample agents are provided as a starting point for custom scripts for specific cluster environments. Note that while Red Hat supports the interfaces that the alert agents scripts use to communicate with Pacemaker, Red Hat does not provide support for the custom agents themselves.
To use one of the sample alert agents, you must install the agent on each node in the cluster. For example, the following command installs the
alert_file.sh.sample script as
install --mode=0755 /usr/share/pacemaker/alerts/alert_file.sh.sample /var/lib/pacemaker/alert_file.sh
After you have installed the script, you can create an alert that uses the script.
The following example configures an alert that uses the installed
alert_file.sh alert agent to log events to a file. Alert agents run as the user
hacluster, which has a minimal set of permissions.
This example creates the log file
pcmk_alert_file.log that will be used to record the events. It then creates the alert agent and adds the path to the log file as its recipient.
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
The following example installs the
alert_snmp.sh.sample script as
alert_snmp.sh and configures an alert that uses the installed
alert_snmp.sh alert agent to send cluster events as SNMP traps. By default, the script will send all events except successful monitor calls to the SNMP server. This example configures the timestamp format as a meta option. After configuring the alert, this example configures a recipient for the alert and displays the alert configuration.
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 alertAlerts: 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)
The following example installs the
alert_smtp.sh agent and then configures an alert that uses the installed alert agent to send cluster events as email messages. After configuring the alert, this example configures a recipient and displays the alert configuration.
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@example.com#
pcs alert recipient add smtp_alert firstname.lastname@example.org#
pcs alertAlerts: Alert: smtp_alert (path=/var/lib/pacemaker/alert_smtp.sh) Options: email@example.com Recipients: Recipient: smtp_alert-recipient (firstname.lastname@example.org)
26.2. Creating a cluster alert
The following command creates a cluster alert. The options that you configure are agent-specific configuration values that are passed to the alert agent script at the path you specify as additional environment variables. If you do not specify a value for
id, one will be generated.
pcs alert create path=path [id=alert-id] [description=description] [options [option=value]...] [meta [meta-option=value]...]
Multiple alert agents may be configured; the cluster will call all of them for each event. Alert agents will be called only on cluster nodes. They will be called for events involving Pacemaker Remote nodes, but they will never be called on those nodes.
The following example creates a simple alert that will call
myscript.sh for each event.
pcs alert create id=my_alert path=/path/to/myscript.sh
26.3. Displaying, modifying, and removing cluster alerts
The following command shows all configured alerts along with the values of the configured options.
pcs alert [config|show]
The following command updates an existing alert with the specified alert-id value.
pcs alert update alert-id [path=path] [description=description] [options [option=value]...] [meta [meta-option=value]...]
The following command removes an alert with the specified alert-id value.
pcs alert remove alert-id
Alternately, you can run the
pcs alert delete command, which is identical to the
pcs alert remove command. Both the
pcs alert delete and the
pcs alert remove commands allow you to specify more than one alert to be deleted.
26.4. Configuring alert recipients
Usually alerts are directed towards a recipient. Thus each alert may be additionally configured with one or more recipients. The cluster will call the agent separately for each recipient.
The recipient may be anything the alert agent can recognize: an IP address, an email address, a file name, or whatever the particular agent supports.
The following command adds a new recipient to the specified alert.
pcs alert recipient add alert-id value=recipient-value [id=recipient-id] [description=description] [options [option=value]...] [meta [meta-option=value]...]
The following command updates an existing alert recipient.
pcs alert recipient update recipient-id [value=recipient-value] [description=description] [options [option=value]...] [meta [meta-option=value]...]
The following command removes the specified alert recipient.
pcs alert recipient remove recipient-id
Alternately, you can run the
pcs alert recipient delete command, which is identical to the
pcs alert recipient remove command. Both the
pcs alert recipient remove and the
pcs alert recipient delete commands allow you to remove more than one alert recipient.
The following example command adds the alert recipient
my-alert-recipient with a recipient ID of
my-recipient-id to the alert
my-alert. This will configure the cluster to call the alert script that has been configured for
my-alert for each event, passing the recipient
some-address as an environment variable.
pcs alert recipient add my-alert value=my-alert-recipient id=my-recipient-id options value=some-address
26.5. Alert meta options
As with resource agents, meta options can be configured for alert agents to affect how Pacemaker calls them. Table 26.1, “Alert Meta Options” describes the alert meta options. Meta options can be configured per alert agent as well as per recipient.
Table 26.1. Alert Meta Options
| || |
Format the cluster will use when sending the event’s timestamp to the agent. This is a string as used with the
| || |
If the alert agent does not complete within this amount of time, it will be terminated.
The following example configures an alert that calls the script
myscript.sh and then adds two recipients to the alert. The first recipient has an ID of
my-alert-recipient1 and the second recipient has an ID of
my-alert-recipient2. The script will get called twice for each event, with each call using a 15-second timeout. One call will be passed to the recipient
email@example.com with a timestamp in the format %D %H:%M, while the other call will be passed to the recipient
firstname.lastname@example.org with a timestamp in the format %c.
pcs alert create id=my-alert path=/path/to/myscript.sh meta timeout=15s#
pcs alert recipient add my-alert email@example.com id=my-alert-recipient1 meta timestamp-format="%D %H:%M"#
pcs alert recipient add my-alert firstname.lastname@example.org id=my-alert-recipient2 meta timestamp-format="%c"
26.6. Alert configuration command examples
The following sequential examples show some basic alert configuration commands to show the format to use to create alerts, add recipients, and display the configured alerts.
Note that while you must install the alert agents themselves on each node in a cluster, you need to run the
pcs commands only once.
The following commands create a simple alert, add two recipients to the alert, and display the configured values.
Since no alert ID value is specified, the system creates an alert ID value of
The first recipient creation command specifies a recipient of
rec_value. Since this command does not specify a recipient ID, the value of
alert-recipientis used as the recipient ID.
The second recipient creation command specifies a recipient of
rec_value2. This command specifies a recipient ID of
my-recipientfor the recipient.
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 configAlerts: Alert: alert (path=/my/path) Recipients: Recipient: alert-recipient (value=rec_value) Recipient: my-recipient (value=rec_value2)
This following commands add a second alert and a recipient for that alert. The alert ID for the second alert is
my-alert and the recipient value is
my-other-recipient. Since no recipient ID is specified, the system provides a recipient id of
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 alertAlerts: 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)
The following commands modify the alert values for the alert
my-alert and for the 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 alertAlerts: 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
The following command removes the recipient
pcs alert recipient remove my-recipient#
pcs alertAlerts: Alert: alert (path=/my/path) Recipients: Recipient: alert-recipient (value=rec_value) Alert: my-alert (path=/path/to/script) Description: alert_description Options: opt=val option1=newvalue1 Meta options: timestamp-format="%M%B%S" timeout=50s Recipients: Recipient: my-alert-recipient (value=my-other-recipient) Options: option1=new Meta options: timeout=60s
The following command removes
myalert from the configuration.
pcs alert remove myalert#
pcs alertAlerts: Alert: alert (path=/my/path) Recipients: Recipient: alert-recipient (value=rec_value)
26.7. Writing an alert agent
There are three types of Pacemaker alerts: node alerts, fencing alerts, and resource alerts. The environment variables that are passed to the alert agents can differ, depending on the type of alert. Table 26.2, “Environment Variables Passed to Alert Agents” describes the environment variables that are passed to alert agents and specifies when the environment variable is associated with a specific alert type.
Table 26.2. Environment Variables Passed to Alert Agents
| || |
The type of alert (node, fencing, or resource)
| || |
The version of Pacemaker sending the alert
| || |
The configured recipient
| || |
A sequence number increased whenever an alert is being issued on the local node, which can be used to reference the order in which alerts have been issued by Pacemaker. An alert for an event that happened later in time reliably has a higher sequence number than alerts for earlier events. Be aware that this number has no cluster-wide meaning.
| || |
A timestamp created prior to executing the agent, in the format specified by the
| || |
Name of affected node
| || |
Detail about event. For node alerts, this is the node’s current state (member or lost). For fencing alerts, this is a summary of the requested fencing operation, including origin, target, and fencing operation error code, if any. For resource alerts, this is a readable string equivalent of
| || |
ID of node whose status changed (provided with node alerts only)
| || |
The requested fencing or resource operation (provided with fencing and resource alerts only)
| || |
The numerical return code of the fencing or resource operation (provided with fencing and resource alerts only)
| || |
The name of the affected resource (resource alerts only)
| || |
The interval of the resource operation (resource alerts only)
| || |
The expected numerical return code of the operation (resource alerts only)
| || |
A numerical code used by Pacemaker to represent the operation result (resource alerts only)
When writing an alert agent, you must take the following concerns into account.
- Alert agents may be called with no recipient (if none is configured), so the agent must be able to handle this situation, even if it only exits in that case. Users may modify the configuration in stages, and add a recipient later.
- If more than one recipient is configured for an alert, the alert agent will be called once per recipient. If an agent is not able to run concurrently, it should be configured with only a single recipient. The agent is free, however, to interpret the recipient as a list.
- When a cluster event occurs, all alerts are fired off at the same time as separate processes. Depending on how many alerts and recipients are configured and on what is done within the alert agents, a significant load burst may occur. The agent could be written to take this into consideration, for example by queueing resource-intensive actions into some other instance, instead of directly executing them.
Alert agents are run as the
haclusteruser, which has a minimal set of permissions. If an agent requires additional privileges, it is recommended to configure
sudoto allow the agent to run the necessary commands as another user with the appropriate privileges.
Take care to validate and sanitize user-configured parameters, such as
CRM_alert_timestamp(whose content is specified by the user-configured
CRM_alert_recipient, and all alert options. This is necessary to protect against configuration errors. In addition, if some user can modify the CIB without having
hacluster-level access to the cluster nodes, this is a potential security concern as well, and you should avoid the possibility of code injection.
If a cluster contains resources with operations for which the
on-failparameter is set to
fence, there will be multiple fence notifications on failure, one for each resource for which this parameter is set plus one additional notification. Both the
pacemaker-controldwill send notifications. Pacemaker performs only one actual fence operation in this case, however, no matter how many notifications are sent.
The alerts interface is designed to be backward compatible with the external scripts interface used by the
ocf:pacemaker:ClusterMon resource. To preserve this compatibility, the environment variables passed to alert agents are available prepended with
CRM_notify_ as well as
CRM_alert_. One break in compatibility is that the
ClusterMon resource ran external scripts as the root user, while alert agents are run as the