3 # Resource script for haproxy daemon
5 # Description: Manages haproxy daemon as an OCF resource in
6 # an High Availability setup.
8 # HAProxy OCF script's Author: Andreas Grueninger
10 # Copyright 2016 Andreas Grueninger, Grueninger GmbH, (grueni). All rights reserved.
13 # usage: $0 {start|stop|status|monitor|validate-all|meta-data}
15 # The "start" arg starts haproxy.
17 # The "stop" arg stops it.
22 # OCF_RESKEY_extraconf
24 # Note:This RA requires that the haproxy config files has a "pidfile"
25 # entry so that it is able to act on the correct process
26 ##########################################################################
29 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
30 .
${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
32 USAGE
="Usage: $0 {start|stop|status|monitor|validate-all|meta-data}";
34 ##########################################################################
45 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
46 <resource-agent name="haproxy">
47 <version>1.0</version>
49 This script manages haproxy daemon
51 <shortdesc lang="en">Manages an haproxy daemon</shortdesc>
55 <parameter name="binpath">
57 The haproxy binary path.
58 For example, "/usr/sbin/haproxy"
60 <shortdesc lang="en">Full path to the haproxy binary</shortdesc>
61 <content type="string" default="/usr/sbin/haproxy"/>
64 <parameter name="conffile">
66 The haproxy daemon configuration file name with full path.
67 For example, "/etc/haproxy/haproxy.cfg"
69 <shortdesc lang="en">Configuration file name with full path</shortdesc>
70 <content type="string" default="/etc/haproxy/haproxy.cfg" />
73 <parameter name="extraconf">
75 Extra command line arguments to pass to haproxy.
76 For example, "-f /etc/haproxy/shared.cfg"
78 <shortdesc lang="en">Extra command line arguments for haproxy</shortdesc>
79 <content type="string" default="" />
85 <action name="start" timeout="20s"/>
86 <action name="stop" timeout="20s"/>
87 <action name="monitor" depth="0" timeout="20s" interval="60s" />
88 <action name="validate-all" timeout="20s"/>
89 <action name="meta-data" timeout="5s"/>
96 get_pid_and_conf_file
()
98 if [ -n "$OCF_RESKEY_conffile" ]; then
99 CONF_FILE
=$OCF_RESKEY_conffile
101 CONF_FILE
="/etc/haproxy/haproxy.cfg"
104 PIDFILE
="`grep -v \"#\" ${CONF_FILE} | grep \"pidfile\" | sed 's/^[ \t]*pidfile[ \t]*//'`"
105 if [ "${PIDFILE}" = '' ]; then
106 PIDFILE
="/var/run/${OCF_RESOURCE_INSTANCE}.pid"
112 if [ -n "$PIDFILE" -a -f "$PIDFILE" ]; then
113 # haproxy is probably running
115 if [ -n "$PID" ]; then
116 if ps
-p $PID |
grep haproxy
>/dev
/null
; then
117 # ocf_log info "haproxy daemon running"
120 ocf_log info
"haproxy daemon is not running but pid file exists"
121 return $OCF_NOT_RUNNING
124 ocf_log err
"PID file empty!"
125 return $OCF_ERR_GENERIC
129 # haproxy is not running
130 ocf_log info
"haproxy daemon is not running"
131 return $OCF_NOT_RUNNING
136 # if haproxy is running return success
139 if [ $retVal -eq $OCF_SUCCESS ]; then
141 elif [ $retVal -ne $OCF_NOT_RUNNING ]; then
142 ocf_log err
"Error. Unknown status."
143 exit $OCF_ERR_GENERIC
146 if [ -n "$OCF_RESKEY_binpath" ]; then
147 COMMAND
="$OCF_RESKEY_binpath"
149 COMMAND
="/usr/sbin/haproxy"
152 mkdir
-p /var
/run
/haproxy
153 $COMMAND $OCF_RESKEY_extraconf -D -f $CONF_FILE -p $PIDFILE;
154 if [ $?
-ne 0 ]; then
155 ocf_log err
"Error. haproxy daemon returned error $?."
156 exit $OCF_ERR_GENERIC
159 ocf_log info
"Started haproxy daemon."
166 if haproxy_status
; then
168 if [ -n "$PID" ] ; then
170 if [ $?
-ne 0 ]; then
172 if [ $?
-ne 0 ]; then
173 ocf_log err
"Error. Could not stop haproxy daemon."
174 return $OCF_ERR_GENERIC
177 rm $PIDFILE 2>/dev
/null
180 ocf_log info
"Stopped haproxy daemon."
189 haproxy_validate_all
()
191 if [ -n "$OCF_RESKEY_binpath" -a ! -x "$OCF_RESKEY_binpath" ]; then
192 ocf_log err
"Binary path $OCF_RESKEY_binpath does not exist."
195 if [ -n "$OCF_RESKEY_conffile" -a ! -f "$OCF_RESKEY_conffile" ]; then
196 ocf_log err
"Config file $OCF_RESKEY_conffile does not exist."
200 if grep -v "^#" "$CONF_FILE" |
grep "pidfile" > /dev
/null
; then
203 ocf_log err
"Error. \"pidfile\" entry required in the haproxy config file by haproxy OCF RA."
204 return $OCF_ERR_GENERIC
215 if [ $# -ne 1 ]; then
221 start
) get_pid_and_conf_file
225 stop
) get_pid_and_conf_file
229 status
) get_pid_and_conf_file
233 monitor
)get_pid_and_conf_file
237 validate-all
) get_pid_and_conf_file
249 exit $OCF_ERR_UNIMPLEMENTED