forked from russki/cluster-agents
-
Notifications
You must be signed in to change notification settings - Fork 1
/
haproxy
284 lines (246 loc) · 7.45 KB
/
haproxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/sh
#
# Resource script for haproxy daemon
#
# Description: Manages haproxy daemon as an OCF resource in
# an High Availability setup.
#
# HAProxy OCF script's Author: Russki
# Modification by: Johan Lyheden
# License: GNU General Public License (GPL)
#
#
# usage: $0 {start|stop|status|monitor|validate-all|meta-data}
#
# The "start" arg starts haproxy.
#
# The "stop" arg stops it.
#
# OCF parameters:
# OCF_RESKEY_binpath
# OCF_RESKEY_conffile
# OCF_RESKEY_pidfile
# OCF_RESKEY_statusurl
#
##########################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
USAGE="Usage: $0 {start|stop|status|monitor|validate-all|meta-data}";
##########################################################################
usage()
{
echo $USAGE >&2
}
meta_data()
{
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="haproxy">
<version>1.0</version>
<longdesc lang="en">
This script manages HAProxy daemon
</longdesc>
<shortdesc lang="en">Manages HAProxy daemon</shortdesc>
<parameters>
<parameter name="binpath">
<longdesc lang="en">
The HAProxy binary path.
For example, "/usr/sbin/haproxy"
</longdesc>
<shortdesc lang="en">Full path to the HAProxy binary</shortdesc>
<content type="string" default="/usr/sbin/haproxy"/>
</parameter>
<parameter name="conffile">
<longdesc lang="en">
The HAProxy daemon configuration file name with full path.
For example, "/etc/haproxy/haproxy.cfg"
</longdesc>
<shortdesc lang="en">Configuration file name with full path</shortdesc>
<content type="string" default="/etc/haproxy/haproxy.cfg" />
</parameter>
<parameter name="pidfile">
<longdesc lang="en">
The HAProxy daemon pid file with full path.
For example, "/var/run/haproxy.pid"
</longdesc>
<shortdesc lang="en">Pid file name with full path</shortdesc>
<content type="string" default="/var/run/haproxy.pid" />
</parameter>
<parameter name="statusurl">
<longdesc lang="en">
The HAProxy status URL to monitor. Needs to be configured in the configuration file like this:
listen health_check __ip-address__:__port__
mode health
</longdesc>
<shortdesc lang="en">The HAProxy status URL to monitor</shortdesc>
<content type="string" />
</parameter>
</parameters>
<actions>
<action name="start" timeout="20s"/>
<action name="stop" timeout="20s"/>
<action name="monitor" depth="0" timeout="20s" interval="60s" />
<action name="validate-all" timeout="20s"/>
<action name="meta-data" timeout="5s"/>
</actions>
</resource-agent>
END
exit $OCF_SUCCESS
}
haproxy_monitor_statusurl()
{
WGETOPTS="-O- -q -L --no-proxy --bind-address=127.0.0.1"
WGET=$(which wget)
if [ "$?" -ne 0 ]; then
ocf_log err "wget command not found"
return 1
fi
ocf_log info "testing status url"
$WGET $WGETOPTS "$1"
}
get_pid_and_conf_file()
{
if [ -n "$OCF_RESKEY_conffile" ]; then
CONF_FILE=$OCF_RESKEY_conffile
else
CONF_FILE="/etc/haproxy/haproxy.conf"
fi
if [ -n "$OCF_RESKEY_pidfile" ]; then
PIDFILE=$OCF_RESKEY_pidfile
else
PIDFILE="/var/run/haproxy.pid"
fi
}
haproxy_status()
{
if [ -n "$PIDFILE" -a -f $PIDFILE ]; then
# haproxy is probably running
PID=`cat $PIDFILE`
if [ -n "$PID" ]; then
if ps -p $PID | grep haproxy >/dev/null ; then
ocf_log info "haproxy daemon running"
return $OCF_SUCCESS
else
ocf_log info "haproxy daemon is not running but pid file exists"
return $OCF_NOT_RUNNING
fi
else
ocf_log err "PID file empty!"
return $OCF_ERR_GENERIC
fi
fi
# haproxy is not running
ocf_log info "haproxy daemon is not running"
return $OCF_NOT_RUNNING
}
haproxy_start()
{
# if haproxy is running return success
haproxy_status
retVal=$?
if [ $retVal -eq $OCF_SUCCESS ]; then
exit $OCF_SUCCESS
elif [ $retVal -ne $OCF_NOT_RUNNING ]; then
ocf_log err "Error. Unknown status."
exit $OCF_ERR_GENERIC
fi
if [ -n "$OCF_RESKEY_binpath" ]; then
COMMAND="$OCF_RESKEY_binpath"
else
COMMAND="/usr/sbin/haproxy"
fi
$COMMAND -f $CONF_FILE -p $PIDFILE;
if [ $? -ne 0 ]; then
ocf_log err "Error. haproxy daemon returned error $?."
exit $OCF_ERR_GENERIC
fi
ocf_log info "Started haproxy daemon."
exit $OCF_SUCCESS
}
haproxy_stop()
{
if haproxy_status ; then
PID=`cat $PIDFILE`
if [ -n "$PID" ] ; then
kill $PID
if [ $? -ne 0 ]; then
kill -SIGKILL $PID
if [ $? -ne 0 ]; then
ocf_log err "Error. Could not stop haproxy daemon."
return $OCF_ERR_GENERIC
fi
fi
rm $PIDFILE 2>/dev/null
fi
fi
ocf_log info "Stopped haproxy daemon."
exit $OCF_SUCCESS
}
haproxy_monitor()
{
if [ -z "$OCF_RESKEY_statusurl" ]; then
haproxy_status
return $?
else
haproxy_status
rt=$?
if [ "$rt" -eq $OCF_SUCCESS ]; then
haproxy_monitor_statusurl $OCF_RESKEY_statusurl |grep OK >/dev/null
return $?
else
return $rt
fi
fi
}
haproxy_validate_all()
{
if [ -n "$OCF_RESKEY_binpath" -a ! -x "$OCF_RESKEY_binpath" ]; then
ocf_log err "Binary path $OCF_RESKEY_binpath does not exist."
exit $OCF_ERR_ARGS
fi
if [ -n "$OCF_RESKEY_conffile" -a ! -f "$OCF_RESKEY_conffile" ]; then
ocf_log err "Config file $OCF_RESKEY_conffile does not exist."
exit $OCF_ERR_ARGS
fi
if grep -v "^#" "$CONF_FILE" | grep "pid file" > /dev/null ; then
:
else
ocf_log err "Error. \"pid file\" entry required in the haproxy config file by haproxy OCF RA."
return $OCF_ERR_GENERIC
fi
return $OCF_SUCCESS
}
#
# Main
#
if [ $# -ne 1 ]; then
usage
exit $OCF_ERR_ARGS
fi
case $1 in
start) get_pid_and_conf_file
haproxy_start
;;
stop) get_pid_and_conf_file
haproxy_stop
;;
status) get_pid_and_conf_file
haproxy_status
;;
monitor)get_pid_and_conf_file
haproxy_monitor
;;
validate-all) get_pid_and_conf_file
haproxy_validate_all
;;
meta-data) meta_data
;;
usage) usage
exit $OCF_SUCCESS
;;
*) usage
exit $OCF_ERR_UNIMPLEMENTED
;;
esac