3
# Resource script for Postfix
5
# Description: Manages Postfix as an OCF resource in
6
# an high-availability setup.
8
# Tested with postfix 2.5.5 on Debian 5.0.
9
# Based on the mysql-proxy and mysql OCF resource agents.
11
# Author: Raoul Bhatia <r.bhatia@ipax.at> : Original Author
12
# License: GNU General Public License (GPL)
13
# Note: if you want to run multiple postfix instances, please see
14
# http://amd.co.at/adminwiki/Postfix#Adding_a_Second_Postfix_Instance_on_one_Server
15
# http://www.postfix.org/postconf.5.html
18
# usage: $0 {start|stop|reload|status|monitor|validate-all|meta-data}
20
# The "start" arg starts a Postfix instance
22
# The "stop" arg stops it.
26
# * /usr/sbin/ocf-tester -n post1 /usr/lib/ocf/resource.d/heartbeat/postfix; echo $?
27
# * /usr/sbin/ocf-tester -n post1 -o binary="/usr/sbin/postfix" \
28
# -o config_dir="" /usr/lib/ocf/resource.d/heartbeat/postfix; echo $?
29
# * /usr/sbin/ocf-tester -n post1 -o binary="/usr/sbin/postfix" \
30
# -o config_dir="/root/postfix/" /usr/lib/ocf/resource.d/heartbeat/postfix; echo $?
35
# OCF_RESKEY_config_dir
36
# OCF_RESKEY_parameters
38
##########################################################################
42
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
43
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
45
: ${OCF_RESKEY_binary="/usr/sbin/postfix"}
46
: ${OCF_RESKEY_config_dir=""}
47
: ${OCF_RESKEY_parameters=""}
48
USAGE="Usage: $0 {start|stop|reload|status|monitor|validate-all|meta-data}";
50
##########################################################################
59
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
60
<resource-agent name="postfix">
61
<version>0.1</version>
63
This script manages Postfix as an OCF resource in a high-availability setup.
64
Tested with Postfix 2.5.5 on Debian 5.0.
66
<shortdesc lang="en">Manages a highly available Postfix mail server instance</shortdesc>
70
<parameter name="binary" unique="0" required="0">
72
Full path to the Postfix binary.
73
For example, "/usr/sbin/postfix".
75
<shortdesc lang="en">Full path to Postfix binary</shortdesc>
76
<content type="string" default="/usr/sbin/postfix" />
79
<parameter name="config_dir" unique="1" required="0">
81
Full path to a Postfix configuration directory.
82
For example, "/etc/postfix".
84
<shortdesc lang="en">Full path to configuration directory</shortdesc>
85
<content type="string" default="" />
88
<parameter name="parameters" unique="0" required="0">
90
The Postfix daemon may be called with additional parameters.
91
Specify any of them here.
93
<shortdesc lang="en"></shortdesc>
94
<content type="string" default="" />
100
<action name="start" timeout="20s" />
101
<action name="stop" timeout="20s" />
102
<action name="reload" timeout="20s" />
103
<action name="monitor" depth="0" timeout="20s" interval="60s" />
104
<action name="validate-all" timeout="20s" />
105
<action name="meta-data" timeout="5s" />
113
$binary $OPTION_CONFIG_DIR status >/dev/null 2>&1
124
# if Postfix is running return success
125
if postfix_status; then
126
ocf_log info "Postfix already running."
131
$binary $OPTIONS start >/dev/null 2>&1
134
if [ $ret -ne 0 ]; then
135
ocf_log err "Postfix returned error." $ret
136
return $OCF_ERR_GENERIC
145
# if Postfix is not running return success
146
if ! postfix_status; then
147
ocf_log info "Postfix already stopped."
152
$binary $OPTIONS stop >/dev/null 2>&1
155
if [ $ret -ne 0 ]; then
156
ocf_log err "Postfix returned an error while stopping." $ret
157
return $OCF_ERR_GENERIC
160
# grant some time for shutdown and recheck 5 times
161
for i in 1 2 3 4 5; do
162
if postfix_status; then
167
# escalate to abort if we did not stop by now
168
# @TODO shall we loop here too?
169
if postfix_status; then
170
ocf_log err "Postfix failed to stop. Escalating to 'abort'"
172
$binary $OPTIONS abort >/dev/null 2>&1; ret=$?
174
postfix_status && $OCF_ERR_GENERIC
182
if postfix_status; then
183
ocf_log info "Reloading Postfix."
184
$binary $OPTIONS reload
190
if postfix_status; then
194
return $OCF_NOT_RUNNING
197
postfix_validate_all()
199
# check that the Postfix binary exists and can be executed
200
if [ ! -x "$binary" ]; then
201
ocf_log err "Postfix binary '$binary' does not exist or cannot be executed."
202
return $OCF_ERR_GENERIC
205
# check config_dir and alternate_config_directories parameter
206
if [ "x$config_dir" != "x" ]; then
207
if [ ! -d "$config_dir" ]; then
208
ocf_log err "Postfix configuration directory '$config_dir' does not exist." $ret
209
return $OCF_ERR_GENERIC
212
alternate_config_directories=`postconf -h alternate_config_directories 2>/dev/null | grep $config_dir`
213
if [ "x$alternate_config_directories" = "x" ]; then
214
ocf_log err "Postfix main configuration must contain correct 'alternate_config_directories' parameter."
215
return $OCF_ERR_GENERIC
219
# check spool/queue directory
220
queue=`postconf $OPTION_CONFIG_DIR -h queue_directory 2>/dev/null`
221
if [ ! -d "$queue" ]; then
222
ocf_log err "Postfix spool/queue directory '$queue' does not exist." $ret
223
return $OCF_ERR_GENERIC
226
# run postfix internal check
227
$binary $OPTIONS check >/dev/null 2>&1
229
if [ $ret -ne 0 ]; then
230
ocf_log err "Postfix 'check' failed." $ret
231
return $OCF_ERR_GENERIC
241
if [ $# -ne 1 ]; then
246
binary=$OCF_RESKEY_binary
247
config_dir=$OCF_RESKEY_config_dir
248
parameters=$OCF_RESKEY_parameters
251
#echo OCF_RESKEY_binary=$OCF_RESKEY_binary >> /tmp/prox_conf_$OCF_RESOURCE_INSTANCE
252
#echo OCF_RESKEY_config_dir=$OCF_RESKEY_config_dir >> /tmp/prox_conf_$OCF_RESOURCE_INSTANCE
253
#echo OCF_RESKEY_parameters=$OCF_RESKEY_parameters >> /tmp/prox_conf_$OCF_RESOURCE_INSTANCE
256
# build Postfix options string *outside* to access from each method
260
# check if the Postfix config_dir exist
261
if [ "x$config_dir" != "x" ]; then
262
# save OPTION_CONFIG_DIR seperatly
263
OPTION_CONFIG_DIR="-c $config_dir"
264
OPTIONS=$OPTION_CONFIG_DIR
267
if [ "x$parameters" != "x" ]; then
268
OPTIONS="$OPTIONS $parameters"
284
#echo "debug[$1:$ret]"
286
if [ $ret -ne $OCF_SUCCESS ]; then
288
stop) exit $OCF_SUCCESS ;;
289
monitor) exit $OCF_NOT_RUNNING;;
290
status) exit $LSB_STATUS_STOPPED;;
296
monitor) postfix_monitor
307
reload) postfix_reload
311
status) if postfix_status; then
312
ocf_log info "Postfix is running."
315
ocf_log info "Postfix is stopped."
316
exit $OCF_NOT_RUNNING
320
validate-all) exit $OCF_SUCCESS
324
exit $OCF_ERR_UNIMPLEMENTED