rust/cargo-c: update to 0.10.7+cargo-0.84.0
[oi-userland.git] / components / cluster / resource-agents / files / pgpool2
blobc0ac5a82392101a52a2ea8a82ebb6ea0a3a436f2
1 #!/usr/bin/bash
3 # pgpool-II resource agent.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of version 2 of the GNU General Public License as
7 # published by the Free Software Foundation.
9 # This program is distributed in the hope that it would be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 # Further, this software is distributed without any warranty that it is
14 # free of the rightful claim of any third person regarding infringement
15 # or the like. Any license provided herein, whether implied or
16 # otherwise, applies only to this software file. Patent licenses, if
17 # any, provided herein do not apply to combinations of this program with
18 # other software, or any other product whatsoever.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write the Free Software Foundation,
22 # Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
24 #######################################################################
26 # This resource agent was written by Eliot Gable <egable at gmail.com>
28 #######################################################################
30 #######################################################################
31 # Initialization:
33 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
34 . ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
36 #######################################################################
38 meta_data() {
39 cat <<END
40 <?xml version="1.0"?>
41 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
42 <resource-agent name="pgpool2" version="0.1">
43 <version>1.0</version>
45 <longdesc lang="en">
46 This resource agent provides basic management of pgpool-II.
47 It starts and stops pgpool-II and monitors its status. It will
48 also monitor the status of each connection and can optionally
49 attempt to automatically reconnect detached nodes or can
50 mark the service as failed if there are any detached nodes.
51 </longdesc>
52 <shortdesc lang="en">Manages pgpool-II</shortdesc>
54 <parameters>
56 <parameter name="pcp_admin_username" required="1">
57 <longdesc lang="en">
58 Administrative username for pgpool-II control.
59 </longdesc>
60 <shortdesc lang="en">Administrative username.</shortdesc>
61 <content type="string" default="" />
62 </parameter>
64 <parameter name="pcp_admin_password" required="1">
65 <longdesc lang="en">
66 Administrative password for pgpool-II control.
67 </longdesc>
68 <shortdesc lang="en">Administrative password.</shortdesc>
69 <content type="string" default="" />
70 </parameter>
72 <parameter name="pcp_admin_port" required="1">
73 <longdesc lang="en">
74 Administrative port for pgpool-II control.
75 </longdesc>
76 <shortdesc lang="en">Administrative port for PCP commands.</shortdesc>
77 <content type="string" default="" />
78 </parameter>
80 <parameter name="pcp_admin_host">
81 <longdesc lang="en">
82 Administrative host for pgpool-II control.
83 </longdesc>
84 <shortdesc lang="en">Administrative host for PCP commands.</shortdesc>
85 <content type="string" default="localhost" />
86 </parameter>
88 <parameter name="pgpool_bin">
89 <longdesc lang="en">
90 Path to the pgpool-II binary.
91 </longdesc>
92 <shortdesc lang="en">Path to pgpool.</shortdesc>
93 <content type="string" default="/usr/bin/pgpool" />
94 </parameter>
96 <parameter name="pcp_attach_node_bin">
97 <longdesc lang="en">
98 Path to the pcp_attach_node binary.
99 </longdesc>
100 <shortdesc lang="en">Path to pcp_attach_node.</shortdesc>
101 <content type="string" default="/usr/bin/pcp_attach_node" />
102 </parameter>
104 <parameter name="pcp_detach_node_bin">
105 <longdesc lang="en">
106 Path to the pcp_detach_node binary.
107 </longdesc>
108 <shortdesc lang="en">Path to pcp_detach_node.</shortdesc>
109 <content type="string" default="/usr/bin/pcp_detach_node" />
110 </parameter>
112 <parameter name="pcp_node_count_bin">
113 <longdesc lang="en">
114 Path to the pcp_node_count binary.
115 </longdesc>
116 <shortdesc lang="en">Path to pcp_node_count.</shortdesc>
117 <content type="string" default="/usr/bin/pcp_node_count" />
118 </parameter>
120 <parameter name="pcp_node_info_bin">
121 <longdesc lang="en">
122 Path to the pcp_node_info binary.
123 </longdesc>
124 <shortdesc lang="en">Path to pcp_node_info.</shortdesc>
125 <content type="string" default="/usr/bin/pcp_node_info" />
126 </parameter>
128 <parameter name="stop_mode">
129 <longdesc lang="en">
130 Stop mode to use when stopping pgpool-II.
131 </longdesc>
132 <shortdesc lang="en">Stop mode for pgpool-II.</shortdesc>
133 <content type="string" default="f" />
134 </parameter>
136 <parameter name="auto_reconnect">
137 <longdesc lang="en">
138 If "true", then during monitoring actions,
139 the resource agent will attempt to re-attach any disconnected
140 nodes. No error will be reported if re-attachment fails.
141 </longdesc>
142 <shortdesc lang="en">Automatically reattach failed nodes</shortdesc>
143 <content type="boolean" default="" />
144 </parameter>
146 <parameter name="fail_on_detached">
147 <longdesc lang="en">
148 This instructs the resource agent to mark pgpool-II in a
149 failed state if one or more of the nodes is detached. The monitor
150 action will always mark pgpool-II in a failed state if all nodes are
151 detached, so this is only useful if you want to mark pgpool-II in a
152 failed state if at least one node is detached. The auto_reconnect
153 option will always try to reconnect detached nodes (if enabled)
154 before this fail_on_detached mechanism triggers.
155 </longdesc>
156 <shortdesc lang="en">Marks resource as failed if at least one node is detached.</shortdesc>
157 <content type="string" default="" />
158 </parameter>
160 <parameter name="fail_on_node_detached">
161 <longdesc lang="en">
162 This is similar to fail_on_detached, except you can
163 specify a comma-seperated list of node IDs. If specified, pgpool2
164 will only be marked as "failed" if one of the nodes in the list
165 is detached or if all nodes are detached.
166 </longdesc>
167 <shortdesc lang="en">Specify a list of nodes to monitor for failure.</shortdesc>
168 <content type="string" default="" />
169 </parameter>
171 </parameters>
173 <actions>
174 <action name="start" timeout="20" />
175 <action name="stop" timeout="40" />
176 <action name="monitor" timeout="20" interval="5" depth="0"/>
177 <action name="reload" timeout="20" />
178 <action name="meta-data" timeout="5" />
179 <action name="validate-all" timeout="20" />
180 </actions>
181 </resource-agent>
185 #######################################################################
187 pgpool2_usage() {
188 cat <<END
189 usage: $0 {start|stop|status|monitor|validate-all|meta-data}
191 Expects to have a fully populated OCF RA-compliant environment set.
195 pgpool2_start() {
196 if pgpool2_status; then
197 ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : already started."
198 return $OCF_SUCCESS
200 if $PGPOOL; then
201 #echo "Trying to start pgpool..." >> /tmp/pgpool
202 sleep 2
203 if pgpool2_status; then
204 ocf_log info "${OCF_RESOURCE_INSTANCE} Successfully started pgpool-II"
205 return $OCF_SUCCESS
206 else
207 #echo "pgpool couldn't be started, why?" /tmp/pgpool
208 ocf_log error "${OCF_RESOURCE_INSTANCE} Failed to start pgpool-II"
209 return $OCF_ERR_GENERIC
211 else
212 ocf_log error "${OCF_RESOURCE_INSTANCE} Failed to start pgpool-II"
214 return $OCF_ERR_GENERIC
217 pgpool2_stop() {
218 ocf_log info "Using $PGPOOL -m $STOP_MODE stop to stop pgpool-II"
219 if $PGPOOL -m $STOP_MODE stop; then
220 ocf_log info "${OCF_RESOURCE_INSTANCE} Successfully stopped pgpool-II"
221 return $OCF_SUCCESS
222 else
223 ocf_log info "${OCF_RESOURCE_INSTANCE} Already stopped pgpool-II"
224 return $OCF_SUCCESS
228 pgpool2_status() {
229 if [ ! -r "/var/postgres/pgpool-II/pgpool.pid" ]; then
230 return $OCF_NOT_RUNNING
232 ps_info=$(ps ax | grep "pgpool" | grep $(cat /var/postgres/pgpool-II/pgpool.pid))
233 #echo "ps_info: $ps_info" >> /tmp/pgpool
234 if [ -z "$ps_info" ]; then
235 #echo "Found pgpool is NOT running..." >> /tmp/pgpool
236 return $OCF_NOT_RUNNING
237 else
238 #echo "Found pgpool is running..." >> /tmp/pgpool
239 # Try to reconnect any detached nodes
240 if ocf_is_true ${OCF_RESKEY_auto_reconnect} ; then
241 NODE_COUNT=$($PCP_NODE_COUNT 1 $OCF_RESKEY_pcp_admin_host $OCF_RESKEY_pcp_admin_port $OCF_RESKEY_pcp_admin_username $OCF_RESKEY_pcp_admin_password)
242 for node in `seq 0 $((NODE_COUNT-1))`; do
243 NODE_INFO=$($PCP_NODE_INFO 1 $OCF_RESKEY_pcp_admin_host $OCF_RESKEY_pcp_admin_port $OCF_RESKEY_pcp_admin_username $OCF_RESKEY_pcp_admin_password $node | awk '{print $3}')
244 if [ "$NODE_INFO" = "3" ]; then
245 ocf_log info "Node $node is currently detached. Attempting to reattach the node."
246 $PCP_ATTACH_NODE 1 $OCF_RESKEY_pcp_admin_host $OCF_RESKEY_pcp_admin_port $OCF_RESKEY_pcp_admin_username $OCF_RESKEY_pcp_admin_password $node
247 ATTACHED="1"
249 done
250 if [ -n "${ATTACHED}" ]; then
251 sleep 1
254 # Fail if configured to fail on one or more detached nodes and a node is still detached
255 if ocf_is_true ${OCF_RESKEY_fail_on_detached} ; then
256 NODE_COUNT=$($PCP_NODE_COUNT 1 $OCF_RESKEY_pcp_admin_host $OCF_RESKEY_pcp_admin_port $OCF_RESKEY_pcp_admin_username $OCF_RESKEY_pcp_admin_password)
257 for node in `seq 0 $((NODE_COUNT-1))`; do
258 NODE_INFO=$($PCP_NODE_INFO 1 $OCF_RESKEY_pcp_admin_host $OCF_RESKEY_pcp_admin_port $OCF_RESKEY_pcp_admin_username $OCF_RESKEY_pcp_admin_password $node | awk '{print $3}')
259 if [ "$NODE_INFO" = "3" ]; then
260 ocf_log error "Node $node is detached. The pgpool-II service has failed."
261 return $OCF_ERR_GENERIC
263 done
265 # Fail if one of the specifically configured nodes is detached at this point
266 if [ -n "${OCF_RESKEY_fail_on_node_detached}" ]; then
267 NODE_COUNT=$($PCP_NODE_COUNT 1 $OCF_RESKEY_pcp_admin_host $OCF_RESKEY_pcp_admin_port $OCF_RESKEY_pcp_admin_username $OCF_RESKEY_pcp_admin_password)
268 for node in `seq 0 $((NODE_COUNT-1))`; do
269 NODE_INFO=$($PCP_NODE_INFO 1 $OCF_RESKEY_pcp_admin_host $OCF_RESKEY_pcp_admin_port $OCF_RESKEY_pcp_admin_username $OCF_RESKEY_pcp_admin_password $node | awk '{print $3}')
270 if [ "$NODE_INFO" = "3" ]; then
271 TOKEN=${OCF_RESKEY_fail_on_node_detached%%,*}
272 TOKEN_STRING=${OCF_RESKEY_fail_on_node_detached#*,}
273 while [ -n "$TOKEN" ] && [ "${TOKEN}" != "${TOKEN_STRING}" ]; do
274 if [ "$TOKEN" -eq $node ]; then
275 ocf_log error "Node $node is detached. The pgpool-II service has failed."
276 return $OCF_ERR_GENERIC
278 TOKEN=${TOKEN_STRING%%,*}
279 if [ "${TOKEN_STRING}" == "${TOKEN_STRING#*,}" ]; then
280 TOKEN_STRING=""
281 else
282 TOKEN_STRING=${TOKEN_STRING#*,}
284 done
286 done
289 # Service is running and there is no reason to fail
290 #echo "pgpool should be running..." >> /tmp/pgpool
291 return $OCF_SUCCESS
294 pgpool2_validate() {
295 # If we're running as a clone, are the clone meta attrs OK?
296 # if [ "${OCF_RESKEY_CRM_meta_clone}" ]; then
297 # if [ "${OCF_RESKEY_CRM_meta_clone_node_max}" != 1 ]; then
298 # ocf_log error "Misconfigured clone parameters. Must set meta attribute \"clone_node_max\" to 1, got ${OCF_RESKEY_CRM_meta_clone_node_max}."
299 # return $OCF_ERR_ARGS
300 # fi
301 # fi
302 if [ -z "$OCF_RESKEY_pcp_admin_username" ]; then
303 ocf_log error "Missing required parameter \"pcp_admin_username\"."
304 return $OCF_ERR_ARGS
306 if [ -z "$OCF_RESKEY_pcp_admin_password" ]; then
307 ocf_log error "Missing required parameter \"pcp_admin_password\"."
308 return $OCF_ERR_ARGS
310 if [ -z "$OCF_RESKEY_pcp_admin_host" ]; then
311 ocf_log error "Missing required parameter \"pcp_admin_host\"."
312 return $OCF_ERR_ARGS
314 if [ -z "$OCF_RESKEY_pcp_admin_port" ]; then
315 ocf_log error "Missing required parameter \"pcp_admin_port\"."
316 return $OCF_ERR_ARGS
318 # Did we get a path for the pgpool binary?
319 if [ -z "$OCF_RESKEY_pgpool_bin" ]; then
320 ocf_log error "Missing required parameter \"pgpool_bin\"."
321 return $OCF_ERR_ARGS
322 else
323 if [ -x "$PGPOOL" ]; then
324 ocf_log error "The pgpool binary is not executable or is not installed."
325 return $OCF_ERR_INSTALLED
328 # Did we get a path for the pcp_attach_node binary?
329 if [ -z "$OCF_RESKEY_pcp_attach_node_bin" ]; then
330 ocf_log error "Missing required parameter \"pcp_attach_node_bin\"."
331 return $OCF_ERR_ARGS
332 else
333 if [ -x "$PCP_ATTACH_NODE" ]; then
334 ocf_log error "The pcp_attach_node binary is not executable or is not installed."
335 return $OCF_ERR_INSTALLED
338 # Did we get a path for the pcp_detach_node binary?
339 if [ -z "$OCF_RESKEY_pcp_detach_node_bin" ]; then
340 ocf_log error "Missing required parameter \"pcp_detach_node_bin\"."
341 return $OCF_ERR_ARGS
342 else
343 if [ -x "$PCP_DETACH_NODE" ]; then
344 ocf_log error "The pcp_detach_node binary is not executable or is not installed."
345 return $OCF_ERR_INSTALLED
348 # Did we get a path for the pcp_node_count binary?
349 if [ -z "$OCF_RESKEY_pcp_node_count_bin" ]; then
350 ocf_log error "Missing required parameter \"pcp_node_count_bin\"."
351 return $OCF_ERR_ARGS
352 else
353 if [ -x "$PCP_NODE_COUNT" ]; then
354 ocf_log error "The pcp_node_count binary is not executable or is not installed."
355 return $OCF_ERR_INSTALLED
358 # Did we get a path for the pcp_node_info binary?
359 if [ -z "$OCF_RESKEY_pcp_node_info_bin" ]; then
360 ocf_log error "Missing required parameter \"pcp_node_info_bin\"."
361 return $OCF_ERR_ARGS
362 else
363 if [ -x "$PCP_NODE_INFO" ]; then
364 ocf_log error "The pcp_node_info binary is not executable or is not installed."
365 return $OCF_ERR_INSTALLED
368 if [ -n "$OCF_RESKEY_stop_mode" ]; then
369 if ! echo "$OCF_RESKEY_stop_mode" | grep -E '[fsi]|fast|start|immediate' >> /dev/null; then
370 ocf_log error "Stop mode is invalid."
371 return $OCF_ERR_ARGS
373 else
374 ocf_log error "Stop mode was not specified."
375 return $OCF_ERR_ARGS
377 if [ -n "$OCF_RESKEY_auto_reconnect" ] &&
378 [ "$OCF_RESKEY_auto_reconnect" != "true" ] &&
379 [ "$OCF_RESKEY_auto_reconnect" != "false" ]; then
380 ocf_log error "Parameter 'auto_reconnect' must be empty, 'true', or 'false'."
381 return $OCF_ERR_ARGS
383 if [ -n "$OCF_RESKEY_fail_on_detached" ] &&
384 [ "$OCF_RESKEY_fail_on_detached" != "true" ] &&
385 [ "${OCF_RESKEY_fail_on_detached}" != "false" ]; then
386 ocf_log error "Parameter 'fail_on_detached' must be empty, 'true', or 'false'."
387 return $OCF_ERR_ARGS
389 shopt -s extglob
390 if [ -n "$OCF_RESKEY_fail_on_node_detached" ]; then
391 TOKEN=${OCF_RESKEY_fail_on_node_detached%%,*}
392 TOKEN_STRING=${OCF_RESKEY_fail_on_node_detached#*,}
393 while [ -n $TOKEN ] && [ "${TOKEN}" != "${TOKEN_STRING}" ]; do
394 case $TOKEN in
395 [^0-9])
396 ocf_log error "Invalid token '${TOKEN}' in parameter 'fail_on_node_detached'."
397 return $OCF_ERR_ARGS
399 esac
400 TOKEN=${TOKEN_STRING%%,*}
401 if [ "${TOKEN_STRING}" == "${TOKEN_STRING#*,}" ]; then
402 TOKEN_STRING=""
403 else
404 TOKEN_STRING=${TOKEN_STRING#*,}
406 done
408 return $OCF_SUCCESS
411 # These two actions must always succeed
412 case $__OCF_ACTION in
413 meta-data) meta_data
414 # OCF variables are not set when querying meta-data
415 exit 0
417 usage|help) pgpool2_usage
418 exit $OCF_SUCCESS
420 esac
422 pgpool2_validate || exit $?
424 PGPOOL=$OCF_RESKEY_pgpool_bin
425 PCP_ATTACH_NODE=$OCF_RESKEY_pcp_attach_node_bin
426 PCP_DETACH_NODE=$OCF_RESKEY_pcp_detach_node_bin
427 PCP_NODE_COUNT=$OCF_RESKEY_pcp_node_count_bin
428 PCP_NODE_INFO=$OCF_RESKEY_pcp_node_info_bin
429 STOP_MODE=$OCF_RESKEY_stop_mode
430 #echo "$(date)" >> /tmp/pgpool
431 case $__OCF_ACTION in
432 start) pgpool2_start;;
433 stop) pgpool2_stop;;
434 status|monitor) pgpool2_status;;
435 validate-all) ;;
436 *) pgpool2_usage
437 exit $OCF_ERR_UNIMPLEMENTED
439 esac
440 rc=$?
441 ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION returned $rc"
442 exit $rc