| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# Description: Manages a PostgreSQL Server as an OCF High-Availability |
| 4 |
# resource |
| 5 |
# |
| 6 |
# Authors: Serge Dubrouski ([email protected]) -- original RA |
| 7 |
# Florian Haas ([email protected]) -- makeover |
| 8 |
# Takatoshi MATSUO ([email protected]) -- support replication |
| 9 |
# David Corlette ([email protected]) -- add support for non-standard library locations and non-standard port |
| 10 |
# |
| 11 |
# Copyright: 2006-2012 Serge Dubrouski <[email protected]> |
| 12 |
# and other Linux-HA contributors |
| 13 |
# License: GNU General Public License (GPL) |
| 14 |
# |
| 15 |
############################################################################### |
| 16 |
# Initialization: |
| 17 |
|
| 18 |
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat} |
| 19 |
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs |
| 20 |
|
| 21 |
# Use runuser if available for SELinux. |
| 22 |
if [ -x /sbin/runuser ]; then |
| 23 |
SU=runuser |
| 24 |
else |
| 25 |
SU=su |
| 26 |
fi |
| 27 |
|
| 28 |
# |
| 29 |
# Get PostgreSQL Configuration parameter |
| 30 |
# |
| 31 |
get_pgsql_param() { |
| 32 |
local param_name |
| 33 |
|
| 34 |
param_name=$1 |
| 35 |
perl_code="if (/^\s*$param_name[\s=]+\s*(.*)$/) { |
| 36 |
\$dir=\$1; |
| 37 |
\$dir =~ s/\s*\#.*//; |
| 38 |
\$dir =~ s/^'(\S*)'/\$1/; |
| 39 |
print \$dir;}" |
| 40 |
|
| 41 |
perl -ne "$perl_code" < $OCF_RESKEY_config |
| 42 |
} |
| 43 |
|
| 44 |
# Defaults |
| 45 |
OCF_RESKEY_pgctl_default=/usr/bin/pg_ctl |
| 46 |
OCF_RESKEY_psql_default=/usr/bin/psql |
| 47 |
OCF_RESKEY_pgdata_default=/var/lib/pgsql/data |
| 48 |
OCF_RESKEY_pgdba_default=postgres |
| 49 |
OCF_RESKEY_pghost_default="" |
| 50 |
OCF_RESKEY_pgport_default=5432 |
| 51 |
OCF_RESKEY_pglibs_default=/usr/lib |
| 52 |
OCF_RESKEY_start_opt_default="" |
| 53 |
OCF_RESKEY_ctl_opt_default="" |
| 54 |
OCF_RESKEY_pgdb_default=template1 |
| 55 |
OCF_RESKEY_logfile_default=/dev/null |
| 56 |
OCF_RESKEY_socketdir_default="" |
| 57 |
OCF_RESKEY_stop_escalate_default=90 |
| 58 |
OCF_RESKEY_monitor_user_default="" |
| 59 |
OCF_RESKEY_monitor_password_default="" |
| 60 |
OCF_RESKEY_monitor_sql_default="select now();" |
| 61 |
OCF_RESKEY_check_wal_receiver_default="false" |
| 62 |
# Defaults for replication |
| 63 |
OCF_RESKEY_rep_mode_default=none |
| 64 |
OCF_RESKEY_node_list_default="" |
| 65 |
OCF_RESKEY_restore_command_default="" |
| 66 |
OCF_RESKEY_archive_cleanup_command_default="" |
| 67 |
OCF_RESKEY_recovery_end_command_default="" |
| 68 |
OCF_RESKEY_master_ip_default="" |
| 69 |
OCF_RESKEY_repuser_default="postgres" |
| 70 |
OCF_RESKEY_primary_conninfo_opt_default="" |
| 71 |
OCF_RESKEY_restart_on_promote_default="false" |
| 72 |
OCF_RESKEY_tmpdir_default="/var/lib/pgsql/tmp" |
| 73 |
OCF_RESKEY_xlog_check_count_default="3" |
| 74 |
OCF_RESKEY_crm_attr_timeout_default="5" |
| 75 |
OCF_RESKEY_stop_escalate_in_slave_default=90 |
| 76 |
OCF_RESKEY_replication_slot_name_default="" |
| 77 |
|
| 78 |
: ${OCF_RESKEY_pgctl=${OCF_RESKEY_pgctl_default}} |
| 79 |
: ${OCF_RESKEY_psql=${OCF_RESKEY_psql_default}} |
| 80 |
: ${OCF_RESKEY_pgdata=${OCF_RESKEY_pgdata_default}} |
| 81 |
: ${OCF_RESKEY_pgdba=${OCF_RESKEY_pgdba_default}} |
| 82 |
: ${OCF_RESKEY_pghost=${OCF_RESKEY_pghost_default}} |
| 83 |
: ${OCF_RESKEY_pgport=${OCF_RESKEY_pgport_default}} |
| 84 |
: ${OCF_RESKEY_pglibs=${OCF_RESKEY_pglibs_default}} |
| 85 |
: ${OCF_RESKEY_config=${OCF_RESKEY_pgdata}/postgresql.conf} |
| 86 |
: ${OCF_RESKEY_start_opt=${OCF_RESKEY_start_opt_default}} |
| 87 |
: ${OCF_RESKEY_ctl_opt=${OCF_RESKEY_ctl_opt_default}} |
| 88 |
: ${OCF_RESKEY_pgdb=${OCF_RESKEY_pgdb_default}} |
| 89 |
: ${OCF_RESKEY_logfile=${OCF_RESKEY_logfile_default}} |
| 90 |
: ${OCF_RESKEY_socketdir=${OCF_RESKEY_socketdir_default}} |
| 91 |
: ${OCF_RESKEY_stop_escalate=${OCF_RESKEY_stop_escalate_default}} |
| 92 |
: ${OCF_RESKEY_monitor_user=${OCF_RESKEY_monitor_user_default}} |
| 93 |
: ${OCF_RESKEY_monitor_password=${OCF_RESKEY_monitor_password_default}} |
| 94 |
: ${OCF_RESKEY_monitor_sql=${OCF_RESKEY_monitor_sql_default}} |
| 95 |
: ${OCF_RESKEY_check_wal_receiver=${OCF_RESKEY_check_wal_receiver_default}} |
| 96 |
|
| 97 |
# for replication |
| 98 |
: ${OCF_RESKEY_rep_mode=${OCF_RESKEY_rep_mode_default}} |
| 99 |
: ${OCF_RESKEY_node_list=${OCF_RESKEY_node_list_default}} |
| 100 |
: ${OCF_RESKEY_restore_command=${OCF_RESKEY_restore_command_default}} |
| 101 |
: ${OCF_RESKEY_archive_cleanup_command=${OCF_RESKEY_archive_cleanup_command_default}} |
| 102 |
: ${OCF_RESKEY_recovery_end_command=${OCF_RESKEY_recovery_end_command_default}} |
| 103 |
: ${OCF_RESKEY_master_ip=${OCF_RESKEY_master_ip_default}} |
| 104 |
: ${OCF_RESKEY_repuser=${OCF_RESKEY_repuser_default}} |
| 105 |
: ${OCF_RESKEY_primary_conninfo_opt=${OCF_RESKEY_primary_conninfo_opt_default}} |
| 106 |
: ${OCF_RESKEY_restart_on_promote=${OCF_RESKEY_restart_on_promote_default}} |
| 107 |
: ${OCF_RESKEY_tmpdir=${OCF_RESKEY_tmpdir_default}} |
| 108 |
: ${OCF_RESKEY_xlog_check_count=${OCF_RESKEY_xlog_check_count_default}} |
| 109 |
: ${OCF_RESKEY_crm_attr_timeout=${OCF_RESKEY_crm_attr_timeout_default}} |
| 110 |
: ${OCF_RESKEY_stop_escalate_in_slave=${OCF_RESKEY_stop_escalate_in_slave_default}} |
| 111 |
: ${OCF_RESKEY_replication_slot_name=${OCF_RESKEY_replication_slot_name_default}} |
| 112 |
|
| 113 |
usage() { |
| 114 |
cat <<EOF |
| 115 |
usage: $0 start|stop|status|monitor|promote|demote|notify|meta-data|validate-all|methods |
| 116 |
|
| 117 |
$0 manages a PostgreSQL Server as an HA resource. |
| 118 |
|
| 119 |
The 'start' operation starts the PostgreSQL server. |
| 120 |
The 'stop' operation stops the PostgreSQL server. |
| 121 |
The 'status' operation reports whether the PostgreSQL is up. |
| 122 |
The 'monitor' operation reports whether the PostgreSQL is running. |
| 123 |
The 'promote' operation promotes the PostgreSQL server. |
| 124 |
The 'demote' operation demotes the PostgreSQL server. |
| 125 |
The 'validate-all' operation reports whether the parameters are valid. |
| 126 |
The 'methods' operation reports on the methods $0 supports. |
| 127 |
EOF |
| 128 |
return $OCF_ERR_ARGS |
| 129 |
} |
| 130 |
|
| 131 |
meta_data() { |
| 132 |
cat <<EOF |
| 133 |
<?xml version="1.0"?> |
| 134 |
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> |
| 135 |
<resource-agent name="pgsql" version="1.0"> |
| 136 |
<version>1.0</version> |
| 137 |
|
| 138 |
<longdesc lang="en"> |
| 139 |
Resource script for PostgreSQL. It manages a PostgreSQL as an HA resource. |
| 140 |
</longdesc> |
| 141 |
<shortdesc lang="en">Manages a PostgreSQL database instance</shortdesc> |
| 142 |
|
| 143 |
<parameters> |
| 144 |
<parameter name="pgctl" unique="0" required="0"> |
| 145 |
<longdesc lang="en"> |
| 146 |
Path to pg_ctl command. |
| 147 |
</longdesc> |
| 148 |
<shortdesc lang="en">pgctl</shortdesc> |
| 149 |
<content type="string" default="${OCF_RESKEY_pgctl_default}" /> |
| 150 |
</parameter> |
| 151 |
|
| 152 |
<parameter name="start_opt" unique="0" required="0"> |
| 153 |
<longdesc lang="en"> |
| 154 |
Start options (-o start_opt in pg_ctl). "-i -p 5432" for example. |
| 155 |
</longdesc> |
| 156 |
<shortdesc lang="en">start_opt</shortdesc> |
| 157 |
<content type="string" default="${OCF_RESKEY_start_opt_default}" /> |
| 158 |
|
| 159 |
</parameter> |
| 160 |
<parameter name="ctl_opt" unique="0" required="0"> |
| 161 |
<longdesc lang="en"> |
| 162 |
Additional pg_ctl options (-w, -W etc..). |
| 163 |
</longdesc> |
| 164 |
<shortdesc lang="en">ctl_opt</shortdesc> |
| 165 |
<content type="string" default="${OCF_RESKEY_ctl_opt_default}" /> |
| 166 |
</parameter> |
| 167 |
|
| 168 |
<parameter name="psql" unique="0" required="0"> |
| 169 |
<longdesc lang="en"> |
| 170 |
Path to psql command. |
| 171 |
</longdesc> |
| 172 |
<shortdesc lang="en">psql</shortdesc> |
| 173 |
<content type="string" default="${OCF_RESKEY_psql_default}" /> |
| 174 |
</parameter> |
| 175 |
|
| 176 |
<parameter name="pgdata" unique="0" required="0"> |
| 177 |
<longdesc lang="en"> |
| 178 |
Path to PostgreSQL data directory. |
| 179 |
</longdesc> |
| 180 |
<shortdesc lang="en">pgdata</shortdesc> |
| 181 |
<content type="string" default="${OCF_RESKEY_pgdata_default}" /> |
| 182 |
</parameter> |
| 183 |
|
| 184 |
<parameter name="pgdba" unique="0" required="0"> |
| 185 |
<longdesc lang="en"> |
| 186 |
User that owns PostgreSQL. |
| 187 |
</longdesc> |
| 188 |
<shortdesc lang="en">pgdba</shortdesc> |
| 189 |
<content type="string" default="${OCF_RESKEY_pgdba_default}" /> |
| 190 |
</parameter> |
| 191 |
|
| 192 |
<parameter name="pghost" unique="0" required="0"> |
| 193 |
<longdesc lang="en"> |
| 194 |
Hostname/IP address where PostgreSQL is listening |
| 195 |
</longdesc> |
| 196 |
<shortdesc lang="en">pghost</shortdesc> |
| 197 |
<content type="string" default="${OCF_RESKEY_pghost_default}" /> |
| 198 |
</parameter> |
| 199 |
|
| 200 |
<parameter name="pgport" unique="0" required="0"> |
| 201 |
<longdesc lang="en"> |
| 202 |
Port where PostgreSQL is listening |
| 203 |
</longdesc> |
| 204 |
<shortdesc lang="en">pgport</shortdesc> |
| 205 |
<content type="integer" default="${OCF_RESKEY_pgport_default}" /> |
| 206 |
</parameter> |
| 207 |
|
| 208 |
<parameter name="pglibs" unique="0" required="0"> |
| 209 |
<longdesc lang="en"> |
| 210 |
Custom location of the Postgres libraries. If not set, the standard location |
| 211 |
will be used. |
| 212 |
</longdesc> |
| 213 |
<shortdesc lang="en">pglibs</shortdesc> |
| 214 |
<content type="string" default="${OCF_RESKEY_pglibs_default}" /> |
| 215 |
</parameter> |
| 216 |
|
| 217 |
<parameter name="monitor_user" unique="0" required="0"> |
| 218 |
<longdesc lang="en"> |
| 219 |
PostgreSQL user that pgsql RA will use for monitor operations. If it's not set |
| 220 |
pgdba user will be used. |
| 221 |
</longdesc> |
| 222 |
<shortdesc lang="en">monitor_user</shortdesc> |
| 223 |
<content type="string" default="${OCF_RESKEY_monitor_user_default}" /> |
| 224 |
</parameter> |
| 225 |
|
| 226 |
<parameter name="monitor_password" unique="0" required="0"> |
| 227 |
<longdesc lang="en"> |
| 228 |
Password for monitor user. |
| 229 |
</longdesc> |
| 230 |
<shortdesc lang="en">monitor_password</shortdesc> |
| 231 |
<content type="string" default="${OCF_RESKEY_monitor_password_default}" /> |
| 232 |
</parameter> |
| 233 |
|
| 234 |
<parameter name="monitor_sql" unique="0" required="0"> |
| 235 |
<longdesc lang="en"> |
| 236 |
SQL script that will be used for monitor operations. |
| 237 |
</longdesc> |
| 238 |
<shortdesc lang="en">monitor_sql</shortdesc> |
| 239 |
<content type="string" default="${OCF_RESKEY_monitor_sql_default}" /> |
| 240 |
</parameter> |
| 241 |
|
| 242 |
<parameter name="config" unique="0" required="0"> |
| 243 |
<longdesc lang="en"> |
| 244 |
Path to the PostgreSQL configuration file for the instance. |
| 245 |
</longdesc> |
| 246 |
<shortdesc lang="en">Configuration file</shortdesc> |
| 247 |
<content type="string" default="${OCF_RESKEY_pgdata}/postgresql.conf" /> |
| 248 |
</parameter> |
| 249 |
|
| 250 |
<parameter name="pgdb" unique="0" required="0"> |
| 251 |
<longdesc lang="en"> |
| 252 |
Database that will be used for monitoring. |
| 253 |
</longdesc> |
| 254 |
<shortdesc lang="en">pgdb</shortdesc> |
| 255 |
<content type="string" default="${OCF_RESKEY_pgdb_default}" /> |
| 256 |
</parameter> |
| 257 |
|
| 258 |
<parameter name="logfile" unique="0" required="0"> |
| 259 |
<longdesc lang="en"> |
| 260 |
Path to PostgreSQL server log output file. |
| 261 |
</longdesc> |
| 262 |
<shortdesc lang="en">logfile</shortdesc> |
| 263 |
<content type="string" default="${OCF_RESKEY_logfile_default}" /> |
| 264 |
</parameter> |
| 265 |
|
| 266 |
<parameter name="socketdir" unique="0" required="0"> |
| 267 |
<longdesc lang="en"> |
| 268 |
Unix socket directory for PostgreSQL. |
| 269 |
|
| 270 |
If you use PostgreSQL 9.3 or higher and define unix_socket_directories in the postgresql.conf, then you must set socketdir to determine which directory is used for psql command. |
| 271 |
</longdesc> |
| 272 |
<shortdesc lang="en">socketdir</shortdesc> |
| 273 |
<content type="string" default="${OCF_RESKEY_socketdir_default}" /> |
| 274 |
</parameter> |
| 275 |
|
| 276 |
<parameter name="stop_escalate" unique="0" required="0"> |
| 277 |
<longdesc lang="en"> |
| 278 |
Number of seconds to wait for stop (using -m fast) before resorting to -m immediate |
| 279 |
</longdesc> |
| 280 |
<shortdesc lang="en">stop escalation</shortdesc> |
| 281 |
<content type="integer" default="${OCF_RESKEY_stop_escalate_default}" /> |
| 282 |
</parameter> |
| 283 |
|
| 284 |
<parameter name="rep_mode" unique="0" required="0"> |
| 285 |
<longdesc lang="en"> |
| 286 |
Replication mode may be set to "async" or "sync" or "slave". |
| 287 |
They require PostgreSQL 9.1 or later. |
| 288 |
Once set, "async" and "sync" require node_list, master_ip, and |
| 289 |
restore_command parameters,as well as configuring PostgreSQL |
| 290 |
for replication (in postgresql.conf and pg_hba.conf). |
| 291 |
|
| 292 |
"slave" means that RA only makes recovery.conf before starting |
| 293 |
to connect to primary which is running somewhere. |
| 294 |
It doesn't need master/slave setting. |
| 295 |
It requires master_ip restore_command parameters. |
| 296 |
</longdesc> |
| 297 |
<shortdesc lang="en">rep_mode</shortdesc> |
| 298 |
<content type="string" default="${OCF_RESKEY_rep_mode_default}" /> |
| 299 |
</parameter> |
| 300 |
|
| 301 |
<parameter name="node_list" unique="0" required="0"> |
| 302 |
<longdesc lang="en"> |
| 303 |
All node names. Please separate each node name with a space. |
| 304 |
This is optional for replication. Defaults to all nodes in the cluster |
| 305 |
</longdesc> |
| 306 |
<shortdesc lang="en">node list</shortdesc> |
| 307 |
<content type="string" default="${OCF_RESKEY_node_list_default}" /> |
| 308 |
</parameter> |
| 309 |
|
| 310 |
<parameter name="restore_command" unique="0" required="0"> |
| 311 |
<longdesc lang="en"> |
| 312 |
restore_command for recovery.conf. |
| 313 |
This is required for replication. |
| 314 |
</longdesc> |
| 315 |
<shortdesc lang="en">restore_command</shortdesc> |
| 316 |
<content type="string" default="${OCF_RESKEY_restore_command_default}" /> |
| 317 |
</parameter> |
| 318 |
|
| 319 |
<parameter name="archive_cleanup_command" unique="0" required="0"> |
| 320 |
<longdesc lang="en"> |
| 321 |
archive_cleanup_command for recovery.conf. |
| 322 |
This is used for replication and is optional. |
| 323 |
</longdesc> |
| 324 |
<shortdesc lang="en">archive_cleanup_command</shortdesc> |
| 325 |
<content type="string" default="${OCF_RESKEY_archive_cleanup_command_default}" /> |
| 326 |
</parameter> |
| 327 |
|
| 328 |
<parameter name="recovery_end_command" unique="0" required="0"> |
| 329 |
<longdesc lang="en"> |
| 330 |
recovery_end_command for recovery.conf. |
| 331 |
This is used for replication and is optional. |
| 332 |
</longdesc> |
| 333 |
<shortdesc lang="en">recovery_end_command</shortdesc> |
| 334 |
<content type="string" default="${OCF_RESKEY_recovery_end_command_default}" /> |
| 335 |
</parameter> |
| 336 |
|
| 337 |
<parameter name="master_ip" unique="0" required="0"> |
| 338 |
<longdesc lang="en"> |
| 339 |
Master's floating IP address to be connected from hot standby. |
| 340 |
This parameter is used for "primary_conninfo" in recovery.conf. |
| 341 |
This is required for replication. |
| 342 |
</longdesc> |
| 343 |
<shortdesc lang="en">master ip</shortdesc> |
| 344 |
<content type="string" default="${OCF_RESKEY_master_ip_default}" /> |
| 345 |
</parameter> |
| 346 |
|
| 347 |
<parameter name="repuser" unique="0" required="0"> |
| 348 |
<longdesc lang="en"> |
| 349 |
User used to connect to the master server. |
| 350 |
This parameter is used for "primary_conninfo" in recovery.conf. |
| 351 |
This is required for replication. |
| 352 |
</longdesc> |
| 353 |
<shortdesc lang="en">repuser</shortdesc> |
| 354 |
<content type="string" default="${OCF_RESKEY_repuser_default}" /> |
| 355 |
</parameter> |
| 356 |
|
| 357 |
<parameter name="primary_conninfo_opt" unique="0" required="0"> |
| 358 |
<longdesc lang="en"> |
| 359 |
primary_conninfo options of recovery.conf except host, port, user and application_name. |
| 360 |
This is optional for replication. |
| 361 |
</longdesc> |
| 362 |
<shortdesc lang="en">primary_conninfo_opt</shortdesc> |
| 363 |
<content type="string" default="${OCF_RESKEY_primary_conninfo_opt_default}" /> |
| 364 |
</parameter> |
| 365 |
|
| 366 |
<parameter name="restart_on_promote" unique="0" required="0"> |
| 367 |
<longdesc lang="en"> |
| 368 |
If this is true, RA deletes recovery.conf and restarts PostgreSQL |
| 369 |
on promote to keep Timeline ID. It probably makes fail-over slower. |
| 370 |
It's recommended to set on-fail of promote up as fence. |
| 371 |
This is optional for replication. |
| 372 |
</longdesc> |
| 373 |
<shortdesc lang="en">restart_on_promote</shortdesc> |
| 374 |
<content type="boolean" default="${OCF_RESKEY_restart_on_promote_default}" /> |
| 375 |
</parameter> |
| 376 |
|
| 377 |
<parameter name="replication_slot_name" unique="0" required="0"> |
| 378 |
<longdesc lang="en"> |
| 379 |
Set this option when using replication slots. |
| 380 |
Can only use lower case letters, numbers and underscore for replication_slot_name. |
| 381 |
|
| 382 |
The replication slots would be created for each node, with the name adding the node name as postfix. |
| 383 |
For example, replication_slot_name is "sample" and 2 slaves which are "node1" and "node2" connect to |
| 384 |
their slots, the slots names are "sample_node1" and "sample_node2". |
| 385 |
If the node name contains a upper case letter, hyphen and dot, those characters will be converted to a lower case letter or an underscore. |
| 386 |
For example, Node-1.example.com to node_1_example_com. |
| 387 |
|
| 388 |
pgsql RA doesn't monitor and delete the replication slot. |
| 389 |
When the slave node has been disconnected in failure or the like, execute one of the following manually. |
| 390 |
Otherwise it may eventually cause a disk full because the master node will continue to accumulate the unsent WAL. |
| 391 |
1. recover and reconnect the slave node to the master node as soon as possible. |
| 392 |
2. delete the slot on the master node by following psql command. |
| 393 |
$ select pg_drop_replication_slot('replication_slot_name'); |
| 394 |
</longdesc> |
| 395 |
<shortdesc lang="en">replication_slot_name</shortdesc> |
| 396 |
<content type="string" default="${OCF_RESKEY_replication_slot_name_default}" /> |
| 397 |
</parameter> |
| 398 |
|
| 399 |
<parameter name="tmpdir" unique="0" required="0"> |
| 400 |
<longdesc lang="en"> |
| 401 |
Path to temporary directory. |
| 402 |
This is optional for replication. |
| 403 |
</longdesc> |
| 404 |
<shortdesc lang="en">tmpdir</shortdesc> |
| 405 |
<content type="string" default="${OCF_RESKEY_tmpdir_default}" /> |
| 406 |
</parameter> |
| 407 |
|
| 408 |
<parameter name="xlog_check_count" unique="0" required="0"> |
| 409 |
<longdesc lang="en"> |
| 410 |
Number of checks of xlog on monitor before promote. |
| 411 |
This is optional for replication. |
| 412 |
|
| 413 |
Note: For backward compatibility, the terms are unified with PostgreSQL 9. |
| 414 |
If you are using PostgreSQL 10 or later, replace "xlog" with "wal". |
| 415 |
Likewise, replacing "location" with "lsn". |
| 416 |
</longdesc> |
| 417 |
<shortdesc lang="en">xlog check count</shortdesc> |
| 418 |
<content type="integer" default="${OCF_RESKEY_xlog_check_count_default}" /> |
| 419 |
</parameter> |
| 420 |
|
| 421 |
<parameter name="crm_attr_timeout" unique="0" required="0"> |
| 422 |
<longdesc lang="en"> |
| 423 |
The timeout of crm_attribute forever update command. |
| 424 |
Default value is 5 seconds. |
| 425 |
This is optional for replication. |
| 426 |
</longdesc> |
| 427 |
<shortdesc lang="en">The timeout of crm_attribute forever update command.</shortdesc> |
| 428 |
<content type="integer" default="${OCF_RESKEY_crm_attr_timeout_default}" /> |
| 429 |
</parameter> |
| 430 |
|
| 431 |
<parameter name="stop_escalate_in_slave" unique="0" required="0"> |
| 432 |
<longdesc lang="en"> |
| 433 |
Number of seconds to wait for stop (using -m fast) before resorting to -m immediate |
| 434 |
in slave state. |
| 435 |
This is optional for replication. |
| 436 |
</longdesc> |
| 437 |
<shortdesc lang="en">stop escalation_in_slave</shortdesc> |
| 438 |
<content type="integer" default="${OCF_RESKEY_stop_escalate_in_slave_default}" /> |
| 439 |
</parameter> |
| 440 |
|
| 441 |
<parameter name="check_wal_receiver" unique="0" required="0"> |
| 442 |
<longdesc lang="en"> |
| 443 |
If this is true, RA checks wal_receiver process on monitor |
| 444 |
and notifies its status using "(resource name)-receiver-status" attribute. |
| 445 |
It's useful for checking whether PostgreSQL (hot standby) connects to primary. |
| 446 |
The attribute shows status as "normal" or "normal (master)" or "ERROR". |
| 447 |
Note that if you configure PostgreSQL as master/slave resource, then |
| 448 |
wal receiver is not running in the master and the attribute shows status as |
| 449 |
"normal (master)" consistently because it is normal status. |
| 450 |
</longdesc> |
| 451 |
<shortdesc lang="en">check_wal_receiver</shortdesc> |
| 452 |
<content type="boolean" default="${OCF_RESKEY_check_wal_receiver_default}" /> |
| 453 |
</parameter> |
| 454 |
</parameters> |
| 455 |
|
| 456 |
<actions> |
| 457 |
<action name="start" timeout="120s" /> |
| 458 |
<action name="stop" timeout="120s" /> |
| 459 |
<action name="status" timeout="60s" /> |
| 460 |
<action name="monitor" depth="0" timeout="30s" interval="30s"/> |
| 461 |
<action name="monitor" depth="0" timeout="30s" interval="29s" role="Promoted" /> |
| 462 |
<action name="promote" timeout="120s" /> |
| 463 |
<action name="demote" timeout="120s" /> |
| 464 |
<action name="notify" timeout="90s" /> |
| 465 |
<action name="meta-data" timeout="5s" /> |
| 466 |
<action name="validate-all" timeout="5s" /> |
| 467 |
<action name="methods" timeout="5s" /> |
| 468 |
</actions> |
| 469 |
</resource-agent> |
| 470 |
EOF |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
# |
| 475 |
# Run the given command in the Resource owner environment... |
| 476 |
# |
| 477 |
runasowner() { |
| 478 |
local quietrun="" |
| 479 |
local loglevel="-err" |
| 480 |
local var |
| 481 |
|
| 482 |
for var in 1 2 |
| 483 |
do |
| 484 |
case "$1" in |
| 485 |
"-q") |
| 486 |
quietrun="-q" |
| 487 |
shift 1;; |
| 488 |
"info"|"warn"|"err"|"debug") |
| 489 |
loglevel="-$1" |
| 490 |
shift 1;; |
| 491 |
*) |
| 492 |
;; |
| 493 |
esac |
| 494 |
done |
| 495 |
|
| 496 |
ocf_run $quietrun $loglevel $SU $OCF_RESKEY_pgdba -c "cd $OCF_RESKEY_pgdata; $*" |
| 497 |
} |
| 498 |
|
| 499 |
# |
| 500 |
# Shell escape |
| 501 |
# |
| 502 |
escape_string() { |
| 503 |
echo "$*" | sed -e "s/'/'\\\\''/g" |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
# |
| 508 |
# methods: What methods/operations do we support? |
| 509 |
# |
| 510 |
|
| 511 |
pgsql_methods() { |
| 512 |
cat <<EOF |
| 513 |
start |
| 514 |
stop |
| 515 |
status |
| 516 |
monitor |
| 517 |
promote |
| 518 |
demote |
| 519 |
notify |
| 520 |
methods |
| 521 |
meta-data |
| 522 |
validate-all |
| 523 |
EOF |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
# Execulte SQL and return the result. |
| 528 |
exec_sql() { |
| 529 |
local user="$1" |
| 530 |
local sql="$2" |
| 531 |
local output |
| 532 |
local rc |
| 533 |
|
| 534 |
output=`$SU $OCF_RESKEY_pgdba -c "cd $OCF_RESKEY_pgdata; \ |
| 535 |
$OCF_RESKEY_psql $psql_options -U $user \ |
| 536 |
-Atc \"$sql\""` |
| 537 |
rc=$? |
| 538 |
|
| 539 |
echo $output |
| 540 |
return $rc |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
#pgsql_real_start: Starts PostgreSQL |
| 545 |
pgsql_real_start() { |
| 546 |
local pgctl_options |
| 547 |
local postgres_options |
| 548 |
local rc |
| 549 |
|
| 550 |
pgsql_real_monitor info |
| 551 |
rc=$? |
| 552 |
if [ $rc -eq $OCF_SUCCESS -o $rc -eq $OCF_RUNNING_MASTER ]; then |
| 553 |
ocf_log info "PostgreSQL is already running. PID=`cat $PIDFILE`" |
| 554 |
if is_replication; then |
| 555 |
return $OCF_ERR_GENERIC |
| 556 |
else |
| 557 |
return $OCF_SUCCESS |
| 558 |
fi |
| 559 |
fi |
| 560 |
|
| 561 |
# Remove postmaster.pid if it exists |
| 562 |
rm -f $PIDFILE |
| 563 |
|
| 564 |
# Remove backup_label if it exists |
| 565 |
if [ -f $BACKUPLABEL ] && ! is_replication; then |
| 566 |
ocf_log info "Removing $BACKUPLABEL. The previous backup might have failed." |
| 567 |
rm -f $BACKUPLABEL |
| 568 |
fi |
| 569 |
|
| 570 |
# Check if we need to create a log file |
| 571 |
if ! check_log_file $OCF_RESKEY_logfile |
| 572 |
then |
| 573 |
ocf_exit_reason "PostgreSQL can't write to the log file: $OCF_RESKEY_logfile" |
| 574 |
return $OCF_ERR_PERM |
| 575 |
fi |
| 576 |
|
| 577 |
# Check socket directory |
| 578 |
if [ -n "$OCF_RESKEY_socketdir" ] |
| 579 |
then |
| 580 |
check_socket_dir |
| 581 |
fi |
| 582 |
|
| 583 |
check_stat_temp_directory |
| 584 |
|
| 585 |
if [ "$OCF_RESKEY_rep_mode" = "slave" ]; then |
| 586 |
rm -f $RECOVERY_CONF |
| 587 |
make_recovery_conf || return $OCF_ERR_GENERIC |
| 588 |
fi |
| 589 |
|
| 590 |
# Set options passed to pg_ctl |
| 591 |
pgctl_options="$OCF_RESKEY_ctl_opt -D $OCF_RESKEY_pgdata -l $OCF_RESKEY_logfile" |
| 592 |
|
| 593 |
# Set options passed to the PostgreSQL server process |
| 594 |
postgres_options="-c config_file=${OCF_RESKEY_config}" |
| 595 |
|
| 596 |
if [ -n "$OCF_RESKEY_pghost" ]; then |
| 597 |
postgres_options="$postgres_options -h $OCF_RESKEY_pghost" |
| 598 |
fi |
| 599 |
if [ -n "$OCF_RESKEY_start_opt" ]; then |
| 600 |
postgres_options="$postgres_options $OCF_RESKEY_start_opt" |
| 601 |
fi |
| 602 |
|
| 603 |
# Tack pass-through options onto pg_ctl options |
| 604 |
pgctl_options="$pgctl_options -o '$postgres_options'" |
| 605 |
|
| 606 |
# Invoke pg_ctl |
| 607 |
runasowner "unset PGUSER; unset PGPASSWORD; $OCF_RESKEY_pgctl $pgctl_options -W start" |
| 608 |
|
| 609 |
if [ $? -eq 0 ]; then |
| 610 |
# Probably started..... |
| 611 |
ocf_log info "PostgreSQL start command sent." |
| 612 |
else |
| 613 |
ocf_exit_reason "Can't start PostgreSQL." |
| 614 |
return $OCF_ERR_GENERIC |
| 615 |
fi |
| 616 |
|
| 617 |
while : |
| 618 |
do |
| 619 |
pgsql_real_monitor warn |
| 620 |
rc=$? |
| 621 |
if [ $rc -eq $OCF_SUCCESS -o $rc -eq $OCF_RUNNING_MASTER ]; then |
| 622 |
break; |
| 623 |
fi |
| 624 |
sleep 1 |
| 625 |
ocf_log debug "PostgreSQL still hasn't started yet. Waiting..." |
| 626 |
done |
| 627 |
|
| 628 |
# delete replication slots on all nodes. On master node will be created during promotion. |
| 629 |
if use_replication_slot; then |
| 630 |
delete_replication_slots |
| 631 |
if [ $? -eq $OCF_ERR_GENERIC ]; then |
| 632 |
ocf_exit_reason "PostgreSQL can't clean up replication_slot." |
| 633 |
return $OCF_ERR_GENERIC |
| 634 |
fi |
| 635 |
fi |
| 636 |
|
| 637 |
ocf_log info "PostgreSQL is started." |
| 638 |
return $rc |
| 639 |
} |
| 640 |
|
| 641 |
pgsql_replication_start() { |
| 642 |
local rc |
| 643 |
local synchronous_standby_names |
| 644 |
|
| 645 |
# initializing for replication |
| 646 |
change_pgsql_status "$NODENAME" "STOP" |
| 647 |
delete_master_baseline |
| 648 |
exec_with_retry 0 ocf_promotion_score -v $CAN_NOT_PROMOTE |
| 649 |
rm -f ${XLOG_NOTE_FILE}.* $REP_MODE_CONF $RECOVERY_CONF |
| 650 |
if ! make_recovery_conf || ! delete_xlog_location || ! set_async_mode_all; then |
| 651 |
return $OCF_ERR_GENERIC |
| 652 |
fi |
| 653 |
|
| 654 |
if [ -f $PGSQL_LOCK ]; then |
| 655 |
ocf_exit_reason "My data may be inconsistent. You have to remove $PGSQL_LOCK file to force start." |
| 656 |
return $OCF_ERR_GENERIC |
| 657 |
fi |
| 658 |
|
| 659 |
# start |
| 660 |
pgsql_real_start |
| 661 |
if [ $? -ne $OCF_SUCCESS ]; then |
| 662 |
return $OCF_ERR_GENERIC |
| 663 |
fi |
| 664 |
|
| 665 |
synchronous_standby_names=$(exec_sql "$OCF_RESKEY_monitor_user" "${CHECK_SYNCHRONOUS_STANDBY_NAMES_SQL}") |
| 666 |
if [ -n "${synchronous_standby_names}" ]; then |
| 667 |
ocf_exit_reason "Invalid synchronous_standby_names is set in postgresql.conf." |
| 668 |
return $OCF_ERR_CONFIGURED |
| 669 |
fi |
| 670 |
|
| 671 |
change_pgsql_status "$NODENAME" "HS:alone" |
| 672 |
return $OCF_SUCCESS |
| 673 |
} |
| 674 |
|
| 675 |
#pgsql_start: pgsql_real_start() wrapper for replication |
| 676 |
pgsql_start() { |
| 677 |
if ! is_replication; then |
| 678 |
pgsql_real_start |
| 679 |
return $? |
| 680 |
else |
| 681 |
pgsql_replication_start |
| 682 |
return $? |
| 683 |
fi |
| 684 |
} |
| 685 |
|
| 686 |
#pgsql_promote: Promote PostgreSQL |
| 687 |
pgsql_promote() { |
| 688 |
local output |
| 689 |
local target |
| 690 |
local rc |
| 691 |
|
| 692 |
if ! is_replication; then |
| 693 |
ocf_exit_reason "Not in a replication mode." |
| 694 |
return $OCF_ERR_CONFIGURED |
| 695 |
fi |
| 696 |
|
| 697 |
output=`exec_sql "$OCF_RESKEY_monitor_user" "${CHECK_MS_SQL}"` |
| 698 |
if [ $? -ne 0 ]; then |
| 699 |
report_psql_error $rc $loglevel "Can't get PostgreSQL recovery status on promote." |
| 700 |
return $OCF_ERR_GENERIC |
| 701 |
fi |
| 702 |
|
| 703 |
if [ "$output" = "f" ]; then |
| 704 |
ocf_log info "PostgreSQL is already Master. Don't execute promote." |
| 705 |
return $OCF_SUCCESS |
| 706 |
fi |
| 707 |
|
| 708 |
rm -f ${XLOG_NOTE_FILE}.* |
| 709 |
|
| 710 |
for target in $NODE_LIST; do |
| 711 |
[ "$target" = "$NODENAME" ] && continue |
| 712 |
change_data_status "$target" "DISCONNECT" |
| 713 |
change_master_score "$target" "$CAN_NOT_PROMOTE" |
| 714 |
done |
| 715 |
|
| 716 |
ocf_log info "Creating $PGSQL_LOCK." |
| 717 |
touch $PGSQL_LOCK |
| 718 |
show_master_baseline |
| 719 |
|
| 720 |
if ocf_is_true ${OCF_RESKEY_restart_on_promote}; then |
| 721 |
ocf_log info "Restarting PostgreSQL instead of promote." |
| 722 |
#stop : this function returns $OCF_SUCCESS only. |
| 723 |
pgsql_real_stop slave |
| 724 |
if "${USE_STANDBY_SIGNAL}"; then |
| 725 |
rm -f ${OCF_RESKEY_pgdata}/standby.signal |
| 726 |
else |
| 727 |
rm -f $RECOVERY_CONF |
| 728 |
fi |
| 729 |
pgsql_real_start |
| 730 |
rc=$? |
| 731 |
if [ $rc -ne $OCF_RUNNING_MASTER ]; then |
| 732 |
ocf_exit_reason "Can't start PostgreSQL as primary on promote." |
| 733 |
if [ $rc -ne $OCF_SUCCESS ]; then |
| 734 |
change_pgsql_status "$NODENAME" "STOP" |
| 735 |
fi |
| 736 |
return $OCF_ERR_GENERIC |
| 737 |
fi |
| 738 |
else |
| 739 |
runasowner "$OCF_RESKEY_pgctl -D $OCF_RESKEY_pgdata -W promote" |
| 740 |
if [ $? -eq 0 ]; then |
| 741 |
ocf_log info "PostgreSQL promote command sent." |
| 742 |
else |
| 743 |
ocf_exit_reason "Can't promote PostgreSQL." |
| 744 |
return $OCF_ERR_GENERIC |
| 745 |
fi |
| 746 |
|
| 747 |
while : |
| 748 |
do |
| 749 |
pgsql_real_monitor warn |
| 750 |
rc=$? |
| 751 |
if [ $rc -eq $OCF_RUNNING_MASTER ]; then |
| 752 |
break; |
| 753 |
elif [ $rc -eq $OCF_ERR_GENERIC ]; then |
| 754 |
ocf_exit_reason "Can't promote PostgreSQL." |
| 755 |
return $rc |
| 756 |
fi |
| 757 |
sleep 1 |
| 758 |
ocf_log debug "PostgreSQL still hasn't promoted yet. Waiting..." |
| 759 |
done |
| 760 |
ocf_log info "PostgreSQL is promoted." |
| 761 |
fi |
| 762 |
|
| 763 |
# create replication slots on master after promotion |
| 764 |
if use_replication_slot; then |
| 765 |
create_replication_slots |
| 766 |
if [ $? -eq $OCF_ERR_GENERIC ]; then |
| 767 |
ocf_exit_reason "PostgreSQL can't create replication_slot." |
| 768 |
return $OCF_ERR_GENERIC |
| 769 |
fi |
| 770 |
fi |
| 771 |
|
| 772 |
change_data_status "$NODENAME" "LATEST" |
| 773 |
exec_with_retry 0 ocf_promotion_score -v $PROMOTE_ME |
| 774 |
change_pgsql_status "$NODENAME" "PRI" |
| 775 |
return $OCF_SUCCESS |
| 776 |
} |
| 777 |
|
| 778 |
#pgsql_demote: Demote PostgreSQL |
| 779 |
pgsql_demote() { |
| 780 |
local rc |
| 781 |
|
| 782 |
if ! is_replication; then |
| 783 |
ocf_exit_reason "Not in a replication mode." |
| 784 |
return $OCF_ERR_CONFIGURED |
| 785 |
fi |
| 786 |
|
| 787 |
exec_with_retry 0 ocf_promotion_score -v $CAN_NOT_PROMOTE |
| 788 |
delete_master_baseline |
| 789 |
|
| 790 |
if ! pgsql_status; then |
| 791 |
ocf_log info "PostgreSQL is already stopped on demote." |
| 792 |
else |
| 793 |
ocf_log info "Stopping PostgreSQL on demote." |
| 794 |
pgsql_real_stop master |
| 795 |
rc=$? |
| 796 |
if [ "$rc" -ne "$OCF_SUCCESS" ]; then |
| 797 |
change_pgsql_status "$NODENAME" "UNKNOWN" |
| 798 |
return $rc |
| 799 |
fi |
| 800 |
fi |
| 801 |
change_pgsql_status "$NODENAME" "STOP" |
| 802 |
return $OCF_SUCCESS |
| 803 |
} |
| 804 |
|
| 805 |
#pgsql_real_stop: Stop PostgreSQL |
| 806 |
pgsql_real_stop() { |
| 807 |
local rc |
| 808 |
local count |
| 809 |
local stop_escalate |
| 810 |
|
| 811 |
if ocf_is_true ${OCF_RESKEY_check_wal_receiver}; then |
| 812 |
attrd_updater -n "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -D |
| 813 |
fi |
| 814 |
|
| 815 |
if ! pgsql_status |
| 816 |
then |
| 817 |
#Already stopped |
| 818 |
return $OCF_SUCCESS |
| 819 |
fi |
| 820 |
|
| 821 |
stop_escalate=$OCF_RESKEY_stop_escalate |
| 822 |
if [ "$1" = "slave" ]; then |
| 823 |
stop_escalate="$OCF_RESKEY_stop_escalate_in_slave" |
| 824 |
fi |
| 825 |
# adjust stop_escalate time when it is longer than the timeout |
| 826 |
if [ -n "$OCF_RESKEY_CRM_meta_timeout" ] && \ |
| 827 |
[ "$stop_escalate" -ge $((OCF_RESKEY_CRM_meta_timeout/1000)) ]; then |
| 828 |
stop_escalate=$(((OCF_RESKEY_CRM_meta_timeout/1000) - 10)) |
| 829 |
ocf_log info "stop_escalate(or stop_escalate_in_slave) time is adjusted to ${stop_escalate} based on the configured timeout." |
| 830 |
fi |
| 831 |
|
| 832 |
# Stop PostgreSQL, do not wait for clients to disconnect |
| 833 |
if [ $stop_escalate -gt 0 ]; then |
| 834 |
runasowner "$OCF_RESKEY_pgctl -W -D $OCF_RESKEY_pgdata stop -m fast" |
| 835 |
fi |
| 836 |
|
| 837 |
# stop waiting |
| 838 |
count=0 |
| 839 |
while [ $count -lt $stop_escalate ] |
| 840 |
do |
| 841 |
if ! pgsql_status |
| 842 |
then |
| 843 |
#PostgreSQL stopped |
| 844 |
break; |
| 845 |
fi |
| 846 |
count=`expr $count + 1` |
| 847 |
sleep 1 |
| 848 |
done |
| 849 |
|
| 850 |
if pgsql_status |
| 851 |
then |
| 852 |
#PostgreSQL is still up. Use another shutdown mode. |
| 853 |
ocf_log info "PostgreSQL failed to stop after ${stop_escalate}s using -m fast. Trying -m immediate..." |
| 854 |
runasowner "$OCF_RESKEY_pgctl -W -D $OCF_RESKEY_pgdata stop -m immediate" |
| 855 |
fi |
| 856 |
|
| 857 |
while : |
| 858 |
do |
| 859 |
pgsql_real_monitor |
| 860 |
rc=$? |
| 861 |
if [ $rc -eq $OCF_NOT_RUNNING ]; then |
| 862 |
# An unnecessary debug log is prevented. |
| 863 |
break; |
| 864 |
fi |
| 865 |
sleep 1 |
| 866 |
ocf_log debug "PostgreSQL still hasn't stopped yet. Waiting..." |
| 867 |
done |
| 868 |
|
| 869 |
# Remove postmaster.pid if it exists |
| 870 |
rm -f $PIDFILE |
| 871 |
|
| 872 |
if [ "$1" = "master" -a "$OCF_RESKEY_CRM_meta_notify_slave_uname" = " " ]; then |
| 873 |
ocf_log info "Removing $PGSQL_LOCK." |
| 874 |
rm -f $PGSQL_LOCK |
| 875 |
fi |
| 876 |
return $OCF_SUCCESS |
| 877 |
} |
| 878 |
|
| 879 |
pgsql_replication_stop() { |
| 880 |
local rc |
| 881 |
|
| 882 |
exec_with_retry 5 ocf_promotion_score -v $CAN_NOT_PROMOTE |
| 883 |
delete_xlog_location |
| 884 |
|
| 885 |
if ! pgsql_status |
| 886 |
then |
| 887 |
ocf_log info "PostgreSQL is already stopped." |
| 888 |
change_pgsql_status "$NODENAME" "STOP" |
| 889 |
return $OCF_SUCCESS |
| 890 |
fi |
| 891 |
|
| 892 |
pgsql_real_stop slave |
| 893 |
rc=$? |
| 894 |
if [ $rc -ne $OCF_SUCCESS ]; then |
| 895 |
change_pgsql_status "$NODENAME" "UNKNOWN" |
| 896 |
return $rc |
| 897 |
fi |
| 898 |
|
| 899 |
change_pgsql_status "$NODENAME" "STOP" |
| 900 |
set_async_mode_all |
| 901 |
delete_master_baseline |
| 902 |
return $OCF_SUCCESS |
| 903 |
} |
| 904 |
|
| 905 |
#pgsql_stop: pgsql_real_stop() wrapper for replication |
| 906 |
pgsql_stop() { |
| 907 |
if ! is_replication; then |
| 908 |
pgsql_real_stop |
| 909 |
return $? |
| 910 |
else |
| 911 |
pgsql_replication_stop |
| 912 |
return $? |
| 913 |
fi |
| 914 |
} |
| 915 |
|
| 916 |
# |
| 917 |
# pgsql_status: is PostgreSQL up? |
| 918 |
# |
| 919 |
|
| 920 |
pgsql_status() { |
| 921 |
if [ -f $PIDFILE ] |
| 922 |
then |
| 923 |
PID=`head -n 1 $PIDFILE` |
| 924 |
runasowner -q debug "kill -s 0 $PID" |
| 925 |
return $? |
| 926 |
fi |
| 927 |
|
| 928 |
# No PID file |
| 929 |
false |
| 930 |
} |
| 931 |
|
| 932 |
pgsql_wal_receiver_status() { |
| 933 |
local PID |
| 934 |
local receiver_parent_pids |
| 935 |
local pgsql_real_monitor_status=$1 |
| 936 |
|
| 937 |
PID=`head -n 1 $PIDFILE` |
| 938 |
receiver_parent_pids=`ps -ef | tr -s " " | grep "[w]al\s*receiver" | cut -d " " -f 3` |
| 939 |
|
| 940 |
if echo "$receiver_parent_pids" | grep -q -w "$PID" ; then |
| 941 |
attrd_updater -n "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -v "normal" |
| 942 |
return 0 |
| 943 |
fi |
| 944 |
|
| 945 |
if [ $pgsql_real_monitor_status -eq "$OCF_RUNNING_MASTER" ]; then |
| 946 |
attrd_updater -n "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -v "normal (master)" |
| 947 |
return 0 |
| 948 |
fi |
| 949 |
|
| 950 |
attrd_updater -n "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -v "ERROR" |
| 951 |
ocf_log warn "wal receiver process is not running" |
| 952 |
return 1 |
| 953 |
} |
| 954 |
|
| 955 |
# |
| 956 |
# pgsql_real_monitor |
| 957 |
# |
| 958 |
|
| 959 |
pgsql_real_monitor() { |
| 960 |
local loglevel |
| 961 |
local rc |
| 962 |
local output |
| 963 |
|
| 964 |
# Set the log level of the error message |
| 965 |
loglevel=${1:-err} |
| 966 |
|
| 967 |
if ! pgsql_status |
| 968 |
then |
| 969 |
ocf_log info "PostgreSQL is down" |
| 970 |
return $OCF_NOT_RUNNING |
| 971 |
fi |
| 972 |
|
| 973 |
if is_replication; then |
| 974 |
#Check replication state |
| 975 |
output=`exec_sql "$OCF_RESKEY_monitor_user" "${CHECK_MS_SQL}"` |
| 976 |
rc=$? |
| 977 |
|
| 978 |
if [ $rc -ne 0 ]; then |
| 979 |
report_psql_error $rc $loglevel "Can't get PostgreSQL recovery status." |
| 980 |
return $OCF_ERR_GENERIC |
| 981 |
fi |
| 982 |
|
| 983 |
case "$output" in |
| 984 |
f) ocf_log debug "PostgreSQL is running as a primary." |
| 985 |
if [ "$OCF_RESKEY_monitor_sql" = "$OCF_RESKEY_monitor_sql_default" ]; then |
| 986 |
if ocf_is_probe; then |
| 987 |
# Set initial score for primary. |
| 988 |
exec_with_retry 0 ocf_promotion_score -v $PROMOTE_ME |
| 989 |
fi |
| 990 |
return $OCF_RUNNING_MASTER |
| 991 |
fi |
| 992 |
;; |
| 993 |
|
| 994 |
t) ocf_log debug "PostgreSQL is running as a hot standby." |
| 995 |
if ocf_is_probe; then |
| 996 |
# Set initial score for hot standby. |
| 997 |
exec_with_retry 0 ocf_promotion_score -v $CAN_NOT_PROMOTE |
| 998 |
fi |
| 999 |
return $OCF_SUCCESS;; |
| 1000 |
|
| 1001 |
*) ocf_exit_reason "$CHECK_MS_SQL output is $output" |
| 1002 |
return $OCF_ERR_GENERIC;; |
| 1003 |
esac |
| 1004 |
fi |
| 1005 |
|
| 1006 |
OCF_RESKEY_monitor_sql=`escape_string "$OCF_RESKEY_monitor_sql"` |
| 1007 |
runasowner -q $loglevel "$OCF_RESKEY_psql $psql_options -U "$OCF_RESKEY_monitor_user" \ |
| 1008 |
-c '$OCF_RESKEY_monitor_sql'" |
| 1009 |
rc=$? |
| 1010 |
if [ $rc -ne 0 ]; then |
| 1011 |
report_psql_error $rc $loglevel "PostgreSQL $OCF_RESKEY_pgdb isn't running." |
| 1012 |
return $OCF_ERR_GENERIC |
| 1013 |
fi |
| 1014 |
|
| 1015 |
if is_replication; then |
| 1016 |
return $OCF_RUNNING_MASTER |
| 1017 |
fi |
| 1018 |
return $OCF_SUCCESS |
| 1019 |
} |
| 1020 |
|
| 1021 |
pgsql_replication_monitor() { |
| 1022 |
local rc |
| 1023 |
|
| 1024 |
rc=$1 |
| 1025 |
if [ $rc -ne $OCF_SUCCESS -a $rc -ne "$OCF_RUNNING_MASTER" ]; then |
| 1026 |
return $rc |
| 1027 |
fi |
| 1028 |
# If I am Master |
| 1029 |
if [ $rc -eq $OCF_RUNNING_MASTER ]; then |
| 1030 |
change_data_status "$NODENAME" "LATEST" |
| 1031 |
change_pgsql_status "$NODENAME" "PRI" |
| 1032 |
control_slave_status || return $OCF_ERR_GENERIC |
| 1033 |
if [ "$RE_CONTROL_SLAVE" = "true" ]; then |
| 1034 |
sleep 2 |
| 1035 |
ocf_log info "re-controlling slave status." |
| 1036 |
RE_CONTROL_SLAVE="none" |
| 1037 |
control_slave_status || return $OCF_ERR_GENERIC |
| 1038 |
fi |
| 1039 |
return $rc |
| 1040 |
fi |
| 1041 |
|
| 1042 |
# I can't get master node name from $OCF_RESKEY_CRM_meta_notify_master_uname on monitor, |
| 1043 |
# so I will get master node name using crm_mon -n |
| 1044 |
print_crm_mon | grep -q -i -E "<resource id=\"${RESOURCE_NAME}\" .* role=\"(Promoted|Master)\"" |
| 1045 |
if [ $? -ne 0 ] ; then |
| 1046 |
# If I am Slave and Master is not exist |
| 1047 |
ocf_log info "Master does not exist." |
| 1048 |
change_pgsql_status "$NODENAME" "HS:alone" |
| 1049 |
have_master_right |
| 1050 |
if [ $? -eq 0 ]; then |
| 1051 |
rm -f ${XLOG_NOTE_FILE}.* |
| 1052 |
fi |
| 1053 |
else |
| 1054 |
output=`exec_with_retry 0 $CRM_ATTR_FOREVER -N "$NODENAME" \ |
| 1055 |
-n "$PGSQL_DATA_STATUS_ATTR" -G -q` |
| 1056 |
if [ "$output" = "DISCONNECT" ]; then |
| 1057 |
change_pgsql_status "$NODENAME" "HS:alone" |
| 1058 |
fi |
| 1059 |
fi |
| 1060 |
return $rc |
| 1061 |
} |
| 1062 |
|
| 1063 |
#pgsql_monitor: pgsql_real_monitor() wrapper for replication |
| 1064 |
pgsql_monitor() { |
| 1065 |
local rc |
| 1066 |
|
| 1067 |
pgsql_real_monitor |
| 1068 |
rc=$? |
| 1069 |
|
| 1070 |
if ocf_is_true ${OCF_RESKEY_check_wal_receiver}; then |
| 1071 |
pgsql_wal_receiver_status $rc |
| 1072 |
fi |
| 1073 |
|
| 1074 |
if ! is_replication; then |
| 1075 |
return $rc |
| 1076 |
else |
| 1077 |
pgsql_replication_monitor $rc |
| 1078 |
return $? |
| 1079 |
fi |
| 1080 |
} |
| 1081 |
|
| 1082 |
# pgsql_post_demote |
| 1083 |
pgsql_post_demote() { |
| 1084 |
DEMOTE_NODE=`echo $OCF_RESKEY_CRM_meta_notify_demote_uname | sed "s/ /\n/g" | head -1 | tr '[A-Z]' '[a-z]'` |
| 1085 |
ocf_log debug "post-demote called. Demote uname is $DEMOTE_NODE" |
| 1086 |
if [ "$DEMOTE_NODE" != "$NODENAME" ]; then |
| 1087 |
if ! echo $OCF_RESKEY_CRM_meta_notify_master_uname | tr '[A-Z]' '[a-z]' | grep $NODENAME; then |
| 1088 |
show_master_baseline |
| 1089 |
change_pgsql_status "$NODENAME" "HS:alone" |
| 1090 |
fi |
| 1091 |
fi |
| 1092 |
return $OCF_SUCCESS |
| 1093 |
} |
| 1094 |
|
| 1095 |
pgsql_pre_promote() { |
| 1096 |
local master_baseline |
| 1097 |
local my_master_baseline |
| 1098 |
local cmp_location |
| 1099 |
local number_of_nodes |
| 1100 |
|
| 1101 |
# If my data is newer than new master's one, I fail my resource. |
| 1102 |
PROMOTE_NODE=`echo $OCF_RESKEY_CRM_meta_notify_promote_uname | \ |
| 1103 |
sed "s/ /\n/g" | head -1 | tr '[A-Z]' '[a-z]'` |
| 1104 |
number_of_nodes=`echo $NODE_LIST | wc -w` |
| 1105 |
if [ $number_of_nodes -ge 3 -a \ |
| 1106 |
"$OCF_RESKEY_rep_mode" = "sync" -a \ |
| 1107 |
"$PROMOTE_NODE" != "$NODENAME" ]; then |
| 1108 |
master_baseline=`$CRM_ATTR_REBOOT -N "$PROMOTE_NODE" -n \ |
| 1109 |
"$PGSQL_MASTER_BASELINE" -G -q 2>/dev/null` |
| 1110 |
if [ $? -eq 0 ]; then |
| 1111 |
my_master_baseline=`$CRM_ATTR_REBOOT -N "$NODENAME" -n \ |
| 1112 |
"$PGSQL_MASTER_BASELINE" -G -q 2>/dev/null` |
| 1113 |
# get older location |
| 1114 |
cmp_location=`printf "$master_baseline\n$my_master_baseline\n" |\ |
| 1115 |
sort | head -1` |
| 1116 |
if [ "$cmp_location" != "$my_master_baseline" ]; then |
| 1117 |
# We used to set the failcount to INF for the resource here in |
| 1118 |
# order to move the master to the other node. However, setting |
| 1119 |
# the failcount should be done only by the CRM and so this use |
| 1120 |
# got deprecated in pacemaker version 1.1.17. Now we do the |
| 1121 |
# "ban resource from the node". |
| 1122 |
ocf_exit_reason "My data is newer than new master's one. New master's location : $master_baseline" |
| 1123 |
exec_with_retry 0 $CRM_RESOURCE -B -r $OCF_RESOURCE_INSTANCE -N $NODENAME -Q |
| 1124 |
return $OCF_ERR_GENERIC |
| 1125 |
fi |
| 1126 |
fi |
| 1127 |
fi |
| 1128 |
return $OCF_SUCCESS |
| 1129 |
} |
| 1130 |
|
| 1131 |
pgsql_notify() { |
| 1132 |
local type="${OCF_RESKEY_CRM_meta_notify_type}" |
| 1133 |
local op="${OCF_RESKEY_CRM_meta_notify_operation}" |
| 1134 |
local rc |
| 1135 |
|
| 1136 |
if ! is_replication; then |
| 1137 |
return $OCF_SUCCESS |
| 1138 |
fi |
| 1139 |
|
| 1140 |
ocf_log debug "notify: ${type} for ${op}" |
| 1141 |
case $type in |
| 1142 |
pre) |
| 1143 |
case $op in |
| 1144 |
promote) |
| 1145 |
pgsql_pre_promote |
| 1146 |
return $? |
| 1147 |
;; |
| 1148 |
esac |
| 1149 |
;; |
| 1150 |
post) |
| 1151 |
case $op in |
| 1152 |
promote) |
| 1153 |
delete_xlog_location |
| 1154 |
PROMOTE_NODE=`echo $OCF_RESKEY_CRM_meta_notify_promote_uname | \ |
| 1155 |
sed "s/ /\n/g" | head -1 | tr '[A-Z]' '[a-z]'` |
| 1156 |
if [ "$PROMOTE_NODE" != "$NODENAME" ]; then |
| 1157 |
delete_master_baseline |
| 1158 |
fi |
| 1159 |
return $OCF_SUCCESS |
| 1160 |
;; |
| 1161 |
demote) |
| 1162 |
pgsql_post_demote |
| 1163 |
return $? |
| 1164 |
;; |
| 1165 |
start|stop) |
| 1166 |
MASTER_NODE=`echo $OCF_RESKEY_CRM_meta_notify_master_uname | \ |
| 1167 |
sed "s/ /\n/g" | head -1 | tr '[A-Z]' '[a-z]'` |
| 1168 |
if [ "$NODENAME" = "$MASTER_NODE" ]; then |
| 1169 |
control_slave_status |
| 1170 |
fi |
| 1171 |
return $OCF_SUCCESS |
| 1172 |
;; |
| 1173 |
esac |
| 1174 |
;; |
| 1175 |
esac |
| 1176 |
return $OCF_SUCCESS |
| 1177 |
} |
| 1178 |
|
| 1179 |
control_slave_status() { |
| 1180 |
local rc |
| 1181 |
local data_status |
| 1182 |
local target |
| 1183 |
local all_data_status |
| 1184 |
local tmp_data_status |
| 1185 |
local number_of_nodes |
| 1186 |
|
| 1187 |
all_data_status=`exec_sql "$OCF_RESKEY_monitor_user" "${CHECK_REPLICATION_STATE_SQL}"` |
| 1188 |
rc=$? |
| 1189 |
|
| 1190 |
if [ $rc -eq 0 ]; then |
| 1191 |
if [ -n "$all_data_status" ]; then |
| 1192 |
all_data_status=`echo $all_data_status | sed "s/\n/ /g"` |
| 1193 |
fi |
| 1194 |
else |
| 1195 |
report_psql_error $rc err "Can't get PostgreSQL replication status." |
| 1196 |
return 1 |
| 1197 |
fi |
| 1198 |
|
| 1199 |
number_of_nodes=`echo $NODE_LIST | wc -w` |
| 1200 |
for target in $NODE_LIST; do |
| 1201 |
if [ "$target" = "$NODENAME" ]; then |
| 1202 |
continue |
| 1203 |
fi |
| 1204 |
|
| 1205 |
data_status="DISCONNECT" |
| 1206 |
if [ -n "$all_data_status" ]; then |
| 1207 |
for tmp_data_status in $all_data_status; do |
| 1208 |
if ! echo $tmp_data_status | grep -q "^${target}|"; then |
| 1209 |
continue |
| 1210 |
fi |
| 1211 |
data_status=`echo $tmp_data_status | cut -d "|" -f 2,3` |
| 1212 |
ocf_log debug "node_name and data_status is $tmp_data_status" |
| 1213 |
break |
| 1214 |
done |
| 1215 |
fi |
| 1216 |
|
| 1217 |
case "$data_status" in |
| 1218 |
"STREAMING|SYNC") |
| 1219 |
change_data_status "$target" "$data_status" |
| 1220 |
change_master_score "$target" "$CAN_PROMOTE" |
| 1221 |
change_pgsql_status "$target" "HS:sync" |
| 1222 |
;; |
| 1223 |
"STREAMING|ASYNC") |
| 1224 |
change_data_status "$target" "$data_status" |
| 1225 |
if [ "$OCF_RESKEY_rep_mode" = "sync" ]; then |
| 1226 |
change_master_score "$target" "$CAN_NOT_PROMOTE" |
| 1227 |
set_sync_mode "$target" |
| 1228 |
else |
| 1229 |
if [ $number_of_nodes -le 2 ]; then |
| 1230 |
change_master_score "$target" "$CAN_PROMOTE" |
| 1231 |
else |
| 1232 |
# I can't determine which slave's data is newest in async mode. |
| 1233 |
change_master_score "$target" "$CAN_NOT_PROMOTE" |
| 1234 |
fi |
| 1235 |
fi |
| 1236 |
change_pgsql_status "$target" "HS:async" |
| 1237 |
;; |
| 1238 |
"STREAMING|POTENTIAL") |
| 1239 |
change_data_status "$target" "$data_status" |
| 1240 |
change_master_score "$target" "$CAN_NOT_PROMOTE" |
| 1241 |
change_pgsql_status "$target" "HS:potential" |
| 1242 |
;; |
| 1243 |
"DISCONNECT") |
| 1244 |
change_data_status "$target" "$data_status" |
| 1245 |
change_master_score "$target" "$CAN_NOT_PROMOTE" |
| 1246 |
if [ "$OCF_RESKEY_rep_mode" = "sync" ]; then |
| 1247 |
set_async_mode "$target" |
| 1248 |
fi |
| 1249 |
;; |
| 1250 |
*) |
| 1251 |
change_data_status "$target" "$data_status" |
| 1252 |
change_master_score "$target" "$CAN_NOT_PROMOTE" |
| 1253 |
if [ "$OCF_RESKEY_rep_mode" = "sync" ]; then |
| 1254 |
set_async_mode "$target" |
| 1255 |
fi |
| 1256 |
change_pgsql_status "$target" "HS:connected" |
| 1257 |
;; |
| 1258 |
esac |
| 1259 |
done |
| 1260 |
return 0 |
| 1261 |
} |
| 1262 |
|
| 1263 |
have_master_right() { |
| 1264 |
local old |
| 1265 |
local new |
| 1266 |
local output |
| 1267 |
local data_status |
| 1268 |
local node |
| 1269 |
local mylocation |
| 1270 |
local count |
| 1271 |
local newestXlog |
| 1272 |
local oldfile |
| 1273 |
local newfile |
| 1274 |
|
| 1275 |
ocf_log debug "Checking if I have a master right." |
| 1276 |
|
| 1277 |
data_status=`$CRM_ATTR_FOREVER -N "$NODENAME" -n \ |
| 1278 |
"$PGSQL_DATA_STATUS_ATTR" -G -q 2>/dev/null` |
| 1279 |
if [ "$OCF_RESKEY_rep_mode" = "sync" ]; then |
| 1280 |
if [ -n "$data_status" -a "$data_status" != "STREAMING|SYNC" -a \ |
| 1281 |
"$data_status" != "LATEST" ]; then |
| 1282 |
ocf_log warn "My data is out-of-date. status=$data_status" |
| 1283 |
return 1 |
| 1284 |
fi |
| 1285 |
else |
| 1286 |
if [ -n "$data_status" -a "$data_status" != "STREAMING|SYNC" -a \ |
| 1287 |
"$data_status" != "STREAMING|ASYNC" -a \ |
| 1288 |
"$data_status" != "LATEST" ]; then |
| 1289 |
ocf_log warn "My data is out-of-date. status=$data_status" |
| 1290 |
return 1 |
| 1291 |
fi |
| 1292 |
fi |
| 1293 |
ocf_log info "My data status=$data_status." |
| 1294 |
|
| 1295 |
show_xlog_location |
| 1296 |
if [ $? -ne 0 ]; then |
| 1297 |
ocf_exit_reason "Failed to show my xlog location." |
| 1298 |
exit $OCF_ERR_GENERIC |
| 1299 |
fi |
| 1300 |
|
| 1301 |
old=0 |
| 1302 |
for count in `seq $OCF_RESKEY_xlog_check_count`; do |
| 1303 |
if [ -f ${XLOG_NOTE_FILE}.$count ]; then |
| 1304 |
old=$count |
| 1305 |
continue |
| 1306 |
fi |
| 1307 |
break |
| 1308 |
done |
| 1309 |
new=`expr $old + 1` |
| 1310 |
|
| 1311 |
# get xlog locations of all nodes |
| 1312 |
for node in ${NODE_LIST}; do |
| 1313 |
output=`$CRM_ATTR_REBOOT -N "$node" -n \ |
| 1314 |
"$PGSQL_XLOG_LOC_NAME" -G -q 2>/dev/null` |
| 1315 |
if [ $? -ne 0 ]; then |
| 1316 |
ocf_log warn "Can't get $node xlog location." |
| 1317 |
continue |
| 1318 |
else |
| 1319 |
ocf_log info "$node xlog location : $output" |
| 1320 |
echo "$node $output" >> ${XLOG_NOTE_FILE}.${new} |
| 1321 |
if [ "$node" = "$NODENAME" ]; then |
| 1322 |
mylocation=$output |
| 1323 |
fi |
| 1324 |
fi |
| 1325 |
done |
| 1326 |
|
| 1327 |
oldfile=`cat ${XLOG_NOTE_FILE}.${old} 2>/dev/null` |
| 1328 |
newfile=`cat ${XLOG_NOTE_FILE}.${new} 2>/dev/null` |
| 1329 |
if [ "$oldfile" != "$newfile" ]; then |
| 1330 |
# reset counter |
| 1331 |
rm -f ${XLOG_NOTE_FILE}.* |
| 1332 |
printf "$newfile\n" > ${XLOG_NOTE_FILE}.0 |
| 1333 |
return 1 |
| 1334 |
fi |
| 1335 |
|
| 1336 |
if [ "$new" -ge "$OCF_RESKEY_xlog_check_count" ]; then |
| 1337 |
newestXlog=`printf "$newfile\n" | sort -t " " -k 2,3 -r | \ |
| 1338 |
head -1 | cut -d " " -f 2` |
| 1339 |
if [ "$newestXlog" = "$mylocation" ]; then |
| 1340 |
ocf_log info "I have a master right." |
| 1341 |
exec_with_retry 5 ocf_promotion_score -v $PROMOTE_ME |
| 1342 |
return 0 |
| 1343 |
fi |
| 1344 |
change_data_status "$NODENAME" "DISCONNECT" |
| 1345 |
ocf_log info "I don't have correct master data." |
| 1346 |
# reset counter |
| 1347 |
rm -f ${XLOG_NOTE_FILE}.* |
| 1348 |
printf "$newfile\n" > ${XLOG_NOTE_FILE}.0 |
| 1349 |
fi |
| 1350 |
|
| 1351 |
return 1 |
| 1352 |
} |
| 1353 |
|
| 1354 |
is_replication() { |
| 1355 |
if [ "$OCF_RESKEY_rep_mode" != "none" -a "$OCF_RESKEY_rep_mode" != "slave" ]; then |
| 1356 |
return 0 |
| 1357 |
fi |
| 1358 |
return 1 |
| 1359 |
} |
| 1360 |
|
| 1361 |
use_replication_slot() { |
| 1362 |
if [ -n "$OCF_RESKEY_replication_slot_name" ]; then |
| 1363 |
return 0 |
| 1364 |
fi |
| 1365 |
|
| 1366 |
return 1 |
| 1367 |
} |
| 1368 |
|
| 1369 |
create_replication_slot_name() { |
| 1370 |
local number_of_nodes=0 |
| 1371 |
local target |
| 1372 |
local replication_slot_name |
| 1373 |
local replication_slot_name_list_tmp |
| 1374 |
local replication_slot_name_list |
| 1375 |
|
| 1376 |
if [ -n "$NODE_LIST" ]; then |
| 1377 |
number_of_nodes=`echo $NODE_LIST | wc -w` |
| 1378 |
fi |
| 1379 |
|
| 1380 |
if [ $number_of_nodes -le 0 ]; then |
| 1381 |
replication_slot_name_list="" |
| 1382 |
|
| 1383 |
# The Master node should have some slots equal to the number of Slaves, and |
| 1384 |
# the Slave nodes connect to their dedicated slot on the Master. |
| 1385 |
# To ensuring that the slots name are each unique, add postfix to $OCF_RESKEY_replication_slot. |
| 1386 |
# The postfix is "_$target". |
| 1387 |
else |
| 1388 |
for target in $NODE_LIST |
| 1389 |
do |
| 1390 |
if [ "$target" != "$NODENAME" ]; then |
| 1391 |
# The Uppercase, "-" and "." don't allow to use in slot_name. |
| 1392 |
# If the NODENAME contains them, convert upper case to lower case and "_" and "." to "_". |
| 1393 |
target=`echo "$target" | tr 'A-Z.-' 'a-z__'` |
| 1394 |
replication_slot_name="$OCF_RESKEY_replication_slot_name"_"$target" |
| 1395 |
replication_slot_name_list_tmp="$replication_slot_name_list" |
| 1396 |
replication_slot_name_list="$replication_slot_name_list_tmp $replication_slot_name" |
| 1397 |
fi |
| 1398 |
done |
| 1399 |
fi |
| 1400 |
|
| 1401 |
echo $replication_slot_name_list |
| 1402 |
} |
| 1403 |
|
| 1404 |
delete_replication_slot(){ |
| 1405 |
DELETE_REPLICATION_SLOT_sql="SELECT pg_drop_replication_slot('$1');" |
| 1406 |
output=`exec_sql "$OCF_RESKEY_pgdba" "$DELETE_REPLICATION_SLOT_sql"` |
| 1407 |
return $? |
| 1408 |
} |
| 1409 |
|
| 1410 |
delete_replication_slots() { |
| 1411 |
local replication_slot_name_list |
| 1412 |
local replication_slot_name |
| 1413 |
|
| 1414 |
replication_slot_name_list=`create_replication_slot_name` |
| 1415 |
ocf_log debug "replication slot names are $replication_slot_name_list." |
| 1416 |
|
| 1417 |
for replication_slot_name in $replication_slot_name_list |
| 1418 |
do |
| 1419 |
if [ `check_replication_slot $replication_slot_name` = "1" ]; then |
| 1420 |
delete_replication_slot $replication_slot_name |
| 1421 |
if [ $? -eq 0 ]; then |
| 1422 |
ocf_log info "PostgreSQL delete the replication slot($replication_slot_name)." |
| 1423 |
else |
| 1424 |
ocf_exit_reason "$output" |
| 1425 |
return $OCF_ERR_GENERIC |
| 1426 |
fi |
| 1427 |
fi |
| 1428 |
done |
| 1429 |
} |
| 1430 |
|
| 1431 |
create_replication_slots() { |
| 1432 |
local replication_slot_name |
| 1433 |
local replication_slot_name_list |
| 1434 |
local output |
| 1435 |
local rc |
| 1436 |
local CREATE_REPLICATION_SLOT_sql |
| 1437 |
local DELETE_REPLICATION_SLOT_sql |
| 1438 |
|
| 1439 |
replication_slot_name_list=`create_replication_slot_name` |
| 1440 |
ocf_log debug "replication slot names are $replication_slot_name_list." |
| 1441 |
|
| 1442 |
for replication_slot_name in $replication_slot_name_list |
| 1443 |
do |
| 1444 |
# If the same name slot is already exists, initialize(delete and create) the slot. |
| 1445 |
if [ `check_replication_slot $replication_slot_name` = "1" ]; then |
| 1446 |
delete_replication_slot $replication_slot_name |
| 1447 |
if [ $? -eq 0 ]; then |
| 1448 |
ocf_log info "PostgreSQL delete the replication slot($replication_slot_name)." |
| 1449 |
else |
| 1450 |
ocf_exit_reason "$output" |
| 1451 |
return $OCF_ERR_GENERIC |
| 1452 |
fi |
| 1453 |
fi |
| 1454 |
|
| 1455 |
CREATE_REPLICATION_SLOT_sql="SELECT pg_create_physical_replication_slot('$replication_slot_name');" |
| 1456 |
output=`exec_sql "$OCF_RESKEY_pgdba" "$CREATE_REPLICATION_SLOT_sql"` |
| 1457 |
rc=$? |
| 1458 |
|
| 1459 |
if [ $rc -eq 0 ]; then |
| 1460 |
ocf_log info "PostgreSQL creates the replication slot($replication_slot_name)." |
| 1461 |
else |
| 1462 |
ocf_exit_reason "$output" |
| 1463 |
return $OCF_ERR_GENERIC |
| 1464 |
fi |
| 1465 |
done |
| 1466 |
|
| 1467 |
return 0 |
| 1468 |
} |
| 1469 |
|
| 1470 |
# This function check the replication slot does exists. |
| 1471 |
check_replication_slot(){ |
| 1472 |
local replication_slot_name=$1 |
| 1473 |
local output |
| 1474 |
local CHECK_REPLICATION_SLOT_sql="SELECT count(*) FROM pg_replication_slots WHERE slot_name = '$replication_slot_name'" |
| 1475 |
|
| 1476 |
output=`exec_sql "$OCF_RESKEY_monitor_user" "$CHECK_REPLICATION_SLOT_sql"` |
| 1477 |
echo "$output" |
| 1478 |
} |
| 1479 |
|
| 1480 |
# On postgreSQL 10 or later, "location" means "lsn". |
| 1481 |
get_my_location() { |
| 1482 |
local rc |
| 1483 |
local output |
| 1484 |
local replay_loc |
| 1485 |
local receive_loc |
| 1486 |
local output1 |
| 1487 |
local output2 |
| 1488 |
local log1 |
| 1489 |
local log2 |
| 1490 |
local newer_location |
| 1491 |
|
| 1492 |
output=`exec_sql "$OCF_RESKEY_monitor_user" "$CHECK_XLOG_LOC_SQL"` |
| 1493 |
rc=$? |
| 1494 |
|
| 1495 |
if [ $rc -ne 0 ]; then |
| 1496 |
report_psql_error $rc err "Can't get my xlog location." |
| 1497 |
return 1 |
| 1498 |
fi |
| 1499 |
replay_loc=`echo $output | cut -d "|" -f 1` |
| 1500 |
receive_loc=`echo $output | cut -d "|" -f 2` |
| 1501 |
|
| 1502 |
output1=`echo "$replay_loc" | cut -d "/" -f 1` |
| 1503 |
output2=`echo "$replay_loc" | cut -d "/" -f 2` |
| 1504 |
log1=`printf "%08s\n" $output1 | sed "s/ /0/g"` |
| 1505 |
log2=`printf "%08s\n" $output2 | sed "s/ /0/g"` |
| 1506 |
replay_loc="${log1}${log2}" |
| 1507 |
|
| 1508 |
output1=`echo "$receive_loc" | cut -d "/" -f 1` |
| 1509 |
output2=`echo "$receive_loc" | cut -d "/" -f 2` |
| 1510 |
log1=`printf "%08s\n" $output1 | sed "s/ /0/g"` |
| 1511 |
log2=`printf "%08s\n" $output2 | sed "s/ /0/g"` |
| 1512 |
receive_loc="${log1}${log2}" |
| 1513 |
|
| 1514 |
newer_location=`printf "$replay_loc\n$receive_loc" | sort -r | head -1` |
| 1515 |
echo "$newer_location" |
| 1516 |
return 0 |
| 1517 |
} |
| 1518 |
|
| 1519 |
# On postgreSQL 10 or later, "xlog_location" means "wal_lsn". |
| 1520 |
show_xlog_location() { |
| 1521 |
local location |
| 1522 |
|
| 1523 |
location=`get_my_location` || return 1 |
| 1524 |
exec_with_retry 0 $CRM_ATTR_REBOOT -N "$NODENAME" -n "$PGSQL_XLOG_LOC_NAME" -v "$location" |
| 1525 |
} |
| 1526 |
|
| 1527 |
# On postgreSQL 10 or later, "xlog_location" means "wal_lsn". |
| 1528 |
delete_xlog_location() { |
| 1529 |
exec_with_retry 5 $CRM_ATTR_REBOOT -N "$NODENAME" -n "$PGSQL_XLOG_LOC_NAME" -D |
| 1530 |
} |
| 1531 |
|
| 1532 |
show_master_baseline() { |
| 1533 |
local rc |
| 1534 |
local location |
| 1535 |
|
| 1536 |
location=`get_my_location` |
| 1537 |
ocf_log info "My master baseline : $location." |
| 1538 |
exec_with_retry 0 $CRM_ATTR_REBOOT -N "$NODENAME" -n "$PGSQL_MASTER_BASELINE" -v "$location" |
| 1539 |
} |
| 1540 |
|
| 1541 |
delete_master_baseline() { |
| 1542 |
exec_with_retry 5 $CRM_ATTR_REBOOT -N "$NODENAME" -n "$PGSQL_MASTER_BASELINE" -D |
| 1543 |
} |
| 1544 |
|
| 1545 |
set_async_mode_all() { |
| 1546 |
[ "$OCF_RESKEY_rep_mode" = "sync" ] || return 0 |
| 1547 |
ocf_log info "Set all nodes into async mode." |
| 1548 |
runasowner -q err "echo \"synchronous_standby_names = ''\" > \"$REP_MODE_CONF\"" |
| 1549 |
if [ $? -ne 0 ]; then |
| 1550 |
ocf_exit_reason "Can't set all nodes into async mode." |
| 1551 |
return 1 |
| 1552 |
fi |
| 1553 |
return 0 |
| 1554 |
} |
| 1555 |
|
| 1556 |
set_async_mode() { |
| 1557 |
cat $REP_MODE_CONF | grep -q -E "(\"$1\")|([,' ]$1[,' ])" |
| 1558 |
if [ $? -eq 0 ]; then |
| 1559 |
ocf_log info "Setup $1 into async mode." |
| 1560 |
runasowner -q err "echo \"synchronous_standby_names = ''\" > \"$REP_MODE_CONF\"" |
| 1561 |
else |
| 1562 |
ocf_log debug "$1 is already in async mode." |
| 1563 |
return 0 |
| 1564 |
fi |
| 1565 |
exec_with_retry 0 reload_conf |
| 1566 |
} |
| 1567 |
|
| 1568 |
set_sync_mode() { |
| 1569 |
local sync_node_in_conf |
| 1570 |
|
| 1571 |
sync_node_in_conf=`cat $REP_MODE_CONF | cut -d "'" -f 2` |
| 1572 |
if [ -n "$sync_node_in_conf" ]; then |
| 1573 |
ocf_log debug "$sync_node_in_conf is already sync mode." |
| 1574 |
else |
| 1575 |
ocf_log info "Setup $1 into sync mode." |
| 1576 |
runasowner -q err "echo \"synchronous_standby_names = '\\\"$1\\\"'\" > \"$REP_MODE_CONF\"" |
| 1577 |
[ "$RE_CONTROL_SLAVE" = "false" ] && RE_CONTROL_SLAVE="true" |
| 1578 |
exec_with_retry 0 reload_conf |
| 1579 |
fi |
| 1580 |
} |
| 1581 |
|
| 1582 |
reload_conf() { |
| 1583 |
# Invoke pg_ctl |
| 1584 |
runasowner "$OCF_RESKEY_pgctl -D $OCF_RESKEY_pgdata reload" |
| 1585 |
if [ $? -eq 0 ]; then |
| 1586 |
ocf_log info "Reload configuration file." |
| 1587 |
else |
| 1588 |
ocf_exit_reason "Can't reload configuration file." |
| 1589 |
return 1 |
| 1590 |
fi |
| 1591 |
|
| 1592 |
return 0 |
| 1593 |
} |
| 1594 |
|
| 1595 |
user_recovery_conf() { |
| 1596 |
local nodename_tmp |
| 1597 |
|
| 1598 |
# put archive_cleanup_command and recovery_end_command only when defined by user |
| 1599 |
if [ -n "$OCF_RESKEY_archive_cleanup_command" ]; then |
| 1600 |
echo "archive_cleanup_command = '${OCF_RESKEY_archive_cleanup_command}'" |
| 1601 |
fi |
| 1602 |
if [ -n "$OCF_RESKEY_recovery_end_command" ]; then |
| 1603 |
echo "recovery_end_command = '${OCF_RESKEY_recovery_end_command}'" |
| 1604 |
fi |
| 1605 |
|
| 1606 |
if use_replication_slot; then |
| 1607 |
nodename_tmp=`echo "$NODENAME" | tr 'A-Z.-' 'a-z__'` |
| 1608 |
echo "primary_slot_name = '${OCF_RESKEY_replication_slot_name}_$nodename_tmp'" |
| 1609 |
fi |
| 1610 |
} |
| 1611 |
|
| 1612 |
make_recovery_conf() { |
| 1613 |
runasowner "touch $RECOVERY_CONF" |
| 1614 |
if [ $? -ne 0 ]; then |
| 1615 |
ocf_exit_reason "Can't create recovery.conf." |
| 1616 |
return 1 |
| 1617 |
fi |
| 1618 |
|
| 1619 |
cat > $RECOVERY_CONF <<END |
| 1620 |
primary_conninfo = 'host=${OCF_RESKEY_master_ip} port=${OCF_RESKEY_pgport} user=${OCF_RESKEY_repuser} application_name=${NODENAME} ${OCF_RESKEY_primary_conninfo_opt}' |
| 1621 |
restore_command = '${OCF_RESKEY_restore_command}' |
| 1622 |
recovery_target_timeline = 'latest' |
| 1623 |
END |
| 1624 |
|
| 1625 |
if "${USE_STANDBY_SIGNAL}"; then |
| 1626 |
# create a standby.signal to start standby server. |
| 1627 |
runasowner "touch ${OCF_RESKEY_pgdata}/standby.signal" |
| 1628 |
if [ $? -ne 0 ]; then |
| 1629 |
ocf_exit_reason "Can't create ${OCF_RESKEY_pgdata}/standby.signal." |
| 1630 |
return 1 |
| 1631 |
fi |
| 1632 |
else |
| 1633 |
cat >> $RECOVERY_CONF <<END |
| 1634 |
standby_mode = 'on' |
| 1635 |
END |
| 1636 |
fi |
| 1637 |
|
| 1638 |
user_recovery_conf >> $RECOVERY_CONF |
| 1639 |
ocf_log debug "Created recovery.conf. host=${OCF_RESKEY_master_ip}, user=${OCF_RESKEY_repuser}" |
| 1640 |
return 0 |
| 1641 |
} |
| 1642 |
|
| 1643 |
# change pgsql-status. |
| 1644 |
# arg1:node, arg2: value |
| 1645 |
change_pgsql_status() { |
| 1646 |
local output |
| 1647 |
|
| 1648 |
if ! is_node_online $1; then |
| 1649 |
return 0 |
| 1650 |
fi |
| 1651 |
|
| 1652 |
output=`$CRM_ATTR_REBOOT -N "$1" -n "$PGSQL_STATUS_ATTR" -G -q 2>/dev/null` |
| 1653 |
if [ "$output" != "$2" ]; then |
| 1654 |
# If slave's disk is broken, RA cannot read PID file |
| 1655 |
# and misjudges the PostgreSQL as down while it is running. |
| 1656 |
# It causes overwriting of pgsql-status by Master because replication is still connected. |
| 1657 |
if [ "$output" = "STOP" -o "$output" = "UNKNOWN" ]; then |
| 1658 |
if [ "$1" != "$NODENAME" ]; then |
| 1659 |
ocf_log warn "Changing $PGSQL_STATUS_ATTR on $1 : $output->$2 by $NODENAME is prohibited." |
| 1660 |
return 0 |
| 1661 |
fi |
| 1662 |
fi |
| 1663 |
ocf_log info "Changing $PGSQL_STATUS_ATTR on $1 : $output->$2." |
| 1664 |
exec_with_retry 0 $CRM_ATTR_REBOOT -N "$1" -n "$PGSQL_STATUS_ATTR" -v "$2" |
| 1665 |
fi |
| 1666 |
return 0 |
| 1667 |
} |
| 1668 |
|
| 1669 |
# change pgsql-data-status. |
| 1670 |
# arg1:node, arg2: value |
| 1671 |
change_data_status() { |
| 1672 |
local output |
| 1673 |
|
| 1674 |
if ! node_exist $1; then |
| 1675 |
return 0 |
| 1676 |
fi |
| 1677 |
|
| 1678 |
while : |
| 1679 |
do |
| 1680 |
output=`$CRM_ATTR_FOREVER -N "$1" -n "$PGSQL_DATA_STATUS_ATTR" -G -q 2>/dev/null` |
| 1681 |
if [ "$output" != "$2" ]; then |
| 1682 |
ocf_log info "Changing $PGSQL_DATA_STATUS_ATTR on $1 : $output->$2." |
| 1683 |
exec_with_retry 0 exec_with_timeout 0 "$CRM_ATTR_FOREVER" -N $1 -n $PGSQL_DATA_STATUS_ATTR -v "$2" |
| 1684 |
else |
| 1685 |
break |
| 1686 |
fi |
| 1687 |
done |
| 1688 |
return 0 |
| 1689 |
} |
| 1690 |
|
| 1691 |
# set master-score |
| 1692 |
# arg1:node, arg2: score, arg3: resoure |
| 1693 |
set_master_score() { |
| 1694 |
local current_score |
| 1695 |
|
| 1696 |
current_score=`$CRM_ATTR_REBOOT -N "$1" -n "master-$3" -G -q 2>/dev/null` |
| 1697 |
if [ -n "$current_score" -a "$current_score" != "$2" ]; then |
| 1698 |
ocf_log info "Changing $3 master score on $1 : $current_score->$2." |
| 1699 |
exec_with_retry 0 $CRM_ATTR_REBOOT -N "$1" -n "master-$3" -v "$2" |
| 1700 |
fi |
| 1701 |
return 0 |
| 1702 |
} |
| 1703 |
|
| 1704 |
# change master-score |
| 1705 |
# arg1:node, arg2: score |
| 1706 |
change_master_score() { |
| 1707 |
local instance |
| 1708 |
|
| 1709 |
if ! is_node_online $1; then |
| 1710 |
return 0 |
| 1711 |
fi |
| 1712 |
|
| 1713 |
if echo $OCF_RESOURCE_INSTANCE | grep -q ":"; then |
| 1714 |
# If Pacemaker version is 1.0.x |
| 1715 |
instance=0 |
| 1716 |
while : |
| 1717 |
do |
| 1718 |
if [ "$instance" -ge "$OCF_RESKEY_CRM_meta_clone_max" ]; then |
| 1719 |
break |
| 1720 |
fi |
| 1721 |
if [ "${RESOURCE_NAME}:${instance}" = "$OCF_RESOURCE_INSTANCE" ]; then |
| 1722 |
instance=`expr $instance + 1` |
| 1723 |
continue |
| 1724 |
fi |
| 1725 |
set_master_score $1 $2 "${RESOURCE_NAME}:${instance}" || return 1 |
| 1726 |
instance=`expr $instance + 1` |
| 1727 |
done |
| 1728 |
else |
| 1729 |
# If globally-unique=false and Pacemaker version is 1.1.8 or higher |
| 1730 |
# Master/Slave resource has no instance number |
| 1731 |
set_master_score $1 $2 ${RESOURCE_NAME} || return 1 |
| 1732 |
fi |
| 1733 |
return 0 |
| 1734 |
} |
| 1735 |
|
| 1736 |
report_psql_error() |
| 1737 |
{ |
| 1738 |
local rc |
| 1739 |
local loglevel |
| 1740 |
local message |
| 1741 |
|
| 1742 |
rc=$1 |
| 1743 |
loglevel=${2:-err} |
| 1744 |
message="$3" |
| 1745 |
|
| 1746 |
ocf_log $loglevel "$message rc=$rc" |
| 1747 |
if [ $rc -eq 1 ]; then |
| 1748 |
ocf_exit_reason "Fatal error (out of memory, file not found, etc.) occurred while executing the psql command." |
| 1749 |
elif [ $rc -eq 2 ]; then |
| 1750 |
ocf_log $loglevel "Connection error (connection to the server went bad and the session was not interactive) occurred while executing the psql command." |
| 1751 |
elif [ $rc -eq 3 ]; then |
| 1752 |
ocf_exit_reason "Script error (the variable ON_ERROR_STOP was set) occurred while executing the psql command." |
| 1753 |
fi |
| 1754 |
} |
| 1755 |
|
| 1756 |
# |
| 1757 |
# timeout management function |
| 1758 |
# arg1 timeout >= 0 (if arg1 is 0, OCF_RESKEY_crm_attr_timeout is used.) |
| 1759 |
# arg2 : command |
| 1760 |
# arg3 : command's args |
| 1761 |
exec_with_timeout() { |
| 1762 |
local func_pid |
| 1763 |
local count=$OCF_RESKEY_crm_attr_timeout |
| 1764 |
local rc |
| 1765 |
|
| 1766 |
if [ "$1" -ne 0 ]; then |
| 1767 |
count=$1 |
| 1768 |
fi |
| 1769 |
shift |
| 1770 |
|
| 1771 |
$* & |
| 1772 |
func_pid=$! |
| 1773 |
sleep .1 |
| 1774 |
|
| 1775 |
while kill -s 0 $func_pid >/dev/null 2>&1; do |
| 1776 |
sleep 1 |
| 1777 |
count=`expr $count - 1` |
| 1778 |
if [ $count -le 0 ]; then |
| 1779 |
ocf_exit_reason "\"$*\" (pid=$func_pid) timed out." |
| 1780 |
kill -s 9 $func_pid >/dev/null 2>&1 |
| 1781 |
return 1 |
| 1782 |
fi |
| 1783 |
ocf_log info "Waiting($count). \"$*\" (pid=$func_pid)." |
| 1784 |
done |
| 1785 |
wait $func_pid |
| 1786 |
} |
| 1787 |
|
| 1788 |
# retry command when command doesn't return 0 |
| 1789 |
# arg1 : count >= 0 (if arg1 is 0, it retries command in infinitum(1day)) |
| 1790 |
# arg2..argN : command and args |
| 1791 |
exec_with_retry() { |
| 1792 |
local count="86400" |
| 1793 |
local output |
| 1794 |
local rc |
| 1795 |
|
| 1796 |
if [ "$1" -ne 0 ]; then |
| 1797 |
count=$1 |
| 1798 |
fi |
| 1799 |
shift |
| 1800 |
|
| 1801 |
while [ $count -gt 0 ]; do |
| 1802 |
output=`$*` |
| 1803 |
rc=$? |
| 1804 |
if [ $rc -ne 0 ]; then |
| 1805 |
ocf_log warn "Retrying(remain $count). \"$*\" failed. rc=$rc. stdout=\"$output\"." |
| 1806 |
count=`expr $count - 1` |
| 1807 |
sleep 1 |
| 1808 |
else |
| 1809 |
printf "${output}" |
| 1810 |
return 0 |
| 1811 |
fi |
| 1812 |
done |
| 1813 |
|
| 1814 |
ocf_exit_reason "giving up executing \"$*\"" |
| 1815 |
return $rc |
| 1816 |
} |
| 1817 |
|
| 1818 |
is_node_online() { |
| 1819 |
print_crm_mon | grep -q -i "<node name=\"$1\" .* online=\"true\"" |
| 1820 |
} |
| 1821 |
|
| 1822 |
node_exist() { |
| 1823 |
print_crm_mon | grep -q -i "<node name=\"$1\" .* online" |
| 1824 |
} |
| 1825 |
|
| 1826 |
check_binary2() { |
| 1827 |
if ! have_binary "$1"; then |
| 1828 |
ocf_exit_reason "Setup problem: couldn't find command: $1" |
| 1829 |
return 1 |
| 1830 |
fi |
| 1831 |
return 0 |
| 1832 |
} |
| 1833 |
|
| 1834 |
check_config() { |
| 1835 |
local rc=0 |
| 1836 |
|
| 1837 |
if [ ! -f "$1" ]; then |
| 1838 |
if ocf_is_probe; then |
| 1839 |
ocf_log info "Unable to read $1 during probe." |
| 1840 |
rc=1 |
| 1841 |
else |
| 1842 |
ocf_exit_reason "Configuration file $1 doesn't exist" |
| 1843 |
rc=2 |
| 1844 |
fi |
| 1845 |
fi |
| 1846 |
|
| 1847 |
return $rc |
| 1848 |
} |
| 1849 |
|
| 1850 |
validate_ocf_check_level_10() { |
| 1851 |
local version |
| 1852 |
local check_config_rc |
| 1853 |
local rep_mode_string |
| 1854 |
local recovery_conf_string |
| 1855 |
local socket_directories |
| 1856 |
local rc |
| 1857 |
|
| 1858 |
version=`cat $OCF_RESKEY_pgdata/PG_VERSION` |
| 1859 |
|
| 1860 |
if ! check_binary2 "$OCF_RESKEY_pgctl" || |
| 1861 |
! check_binary2 "$OCF_RESKEY_psql"; then |
| 1862 |
return $OCF_ERR_INSTALLED |
| 1863 |
fi |
| 1864 |
|
| 1865 |
check_config "$OCF_RESKEY_config" |
| 1866 |
check_config_rc=$? |
| 1867 |
[ $check_config_rc -eq 2 ] && return $OCF_ERR_INSTALLED |
| 1868 |
if [ $check_config_rc -eq 0 ]; then |
| 1869 |
ocf_version_cmp "$version" "9.3" |
| 1870 |
if [ $? -eq 0 ]; then |
| 1871 |
: ${OCF_RESKEY_socketdir=`get_pgsql_param unix_socket_directory`} |
| 1872 |
else |
| 1873 |
# unix_socket_directories is used by PostgreSQL 9.3 or higher. |
| 1874 |
socket_directories=`get_pgsql_param unix_socket_directories` |
| 1875 |
if [ -n "$socket_directories" ]; then |
| 1876 |
# unix_socket_directories may have multiple socket directories and the pgsql RA can not know which directory is used for psql command. |
| 1877 |
# Therefore, the user must set OCF_RESKEY_socketdir explicitly. |
| 1878 |
if [ -z "$OCF_RESKEY_socketdir" ]; then |
| 1879 |
ocf_exit_reason "In PostgreSQL 9.3 or higher, socketdir can't be empty if you define unix_socket_directories in the postgresql.conf." |
| 1880 |
return $OCF_ERR_CONFIGURED |
| 1881 |
fi |
| 1882 |
fi |
| 1883 |
fi |
| 1884 |
fi |
| 1885 |
|
| 1886 |
if ocf_is_probe; then |
| 1887 |
ocf_log info "Don't check $OCF_RESKEY_pgdata during probe" |
| 1888 |
else |
| 1889 |
if ! runasowner "test -w $OCF_RESKEY_pgdata"; then |
| 1890 |
ocf_exit_reason "Directory $OCF_RESKEY_pgdata is not writable by $OCF_RESKEY_pgdba" |
| 1891 |
return $OCF_ERR_PERM; |
| 1892 |
fi |
| 1893 |
fi |
| 1894 |
|
| 1895 |
if is_replication || [ "$OCF_RESKEY_rep_mode" = "slave" ]; then |
| 1896 |
if [ `printf "$version\n9.1" | sort -n | head -1` != "9.1" ]; then |
| 1897 |
ocf_exit_reason "Replication mode needs PostgreSQL 9.1 or higher." |
| 1898 |
return $OCF_ERR_INSTALLED |
| 1899 |
fi |
| 1900 |
ocf_version_cmp "$version" "12" |
| 1901 |
rc=$? |
| 1902 |
if [ $rc -eq 1 ]||[ $rc -eq 2 ]; then |
| 1903 |
# change the standby method for PosrgreSQL 12 or later. |
| 1904 |
USE_STANDBY_SIGNAL=true |
| 1905 |
# change the path to recovery.conf because it cause PostgreSQL start error. |
| 1906 |
RECOVERY_CONF=${OCF_RESKEY_tmpdir}/recovery.conf |
| 1907 |
if [ $check_config_rc -eq 0 ]; then |
| 1908 |
# adding recovery parameters to postgresql.conf. |
| 1909 |
recovery_conf_string="include '$RECOVERY_CONF' # added by pgsql RA" |
| 1910 |
if ! grep -q "^[[:space:]]*$recovery_conf_string" $OCF_RESKEY_config; then |
| 1911 |
ocf_log info "adding include directive $recovery_conf_string into $OCF_RESKEY_config" |
| 1912 |
echo "$recovery_conf_string" >> $OCF_RESKEY_config |
| 1913 |
fi |
| 1914 |
fi |
| 1915 |
fi |
| 1916 |
if [ ! -n "$OCF_RESKEY_master_ip" ]; then |
| 1917 |
ocf_exit_reason "master_ip can't be empty." |
| 1918 |
return $OCF_ERR_CONFIGURED |
| 1919 |
fi |
| 1920 |
fi |
| 1921 |
|
| 1922 |
if is_replication; then |
| 1923 |
REP_MODE_CONF=${OCF_RESKEY_tmpdir}/rep_mode.conf |
| 1924 |
PGSQL_LOCK=${OCF_RESKEY_tmpdir}/PGSQL.lock |
| 1925 |
XLOG_NOTE_FILE=${OCF_RESKEY_tmpdir}/xlog_note |
| 1926 |
|
| 1927 |
CRM_ATTR_REBOOT="${HA_SBIN_DIR}/crm_attribute -l reboot" |
| 1928 |
CRM_ATTR_FOREVER="${HA_SBIN_DIR}/crm_attribute -l forever" |
| 1929 |
CRM_RESOURCE="${HA_SBIN_DIR}/crm_resource" |
| 1930 |
|
| 1931 |
CAN_NOT_PROMOTE="-INFINITY" |
| 1932 |
CAN_PROMOTE="100" |
| 1933 |
PROMOTE_ME="1000" |
| 1934 |
|
| 1935 |
CHECK_MS_SQL="select pg_is_in_recovery()" |
| 1936 |
CHECK_SYNCHRONOUS_STANDBY_NAMES_SQL="show synchronous_standby_names" |
| 1937 |
ocf_version_cmp "$version" "10" |
| 1938 |
rc=$? |
| 1939 |
if [ $rc -eq 1 ]||[ $rc -eq 2 ]; then |
| 1940 |
CHECK_XLOG_LOC_SQL="select pg_last_wal_replay_lsn(),pg_last_wal_receive_lsn()" |
| 1941 |
else |
| 1942 |
CHECK_XLOG_LOC_SQL="select pg_last_xlog_replay_location(),pg_last_xlog_receive_location()" |
| 1943 |
fi |
| 1944 |
CHECK_REPLICATION_STATE_SQL="select application_name,upper(state),upper(sync_state) from pg_stat_replication" |
| 1945 |
|
| 1946 |
PGSQL_STATUS_ATTR="${RESOURCE_NAME}-status" |
| 1947 |
PGSQL_DATA_STATUS_ATTR="${RESOURCE_NAME}-data-status" |
| 1948 |
PGSQL_XLOG_LOC_NAME="${RESOURCE_NAME}-xlog-loc" |
| 1949 |
PGSQL_MASTER_BASELINE="${RESOURCE_NAME}-master-baseline" |
| 1950 |
|
| 1951 |
NODE_LIST=`echo $OCF_RESKEY_node_list | tr '[A-Z]' '[a-z]'` |
| 1952 |
RE_CONTROL_SLAVE="false" |
| 1953 |
|
| 1954 |
if ! ocf_is_ms; then |
| 1955 |
ocf_exit_reason "Replication(rep_mode=async or sync) requires Master/Slave configuration." |
| 1956 |
return $OCF_ERR_CONFIGURED |
| 1957 |
fi |
| 1958 |
if [ ! "$OCF_RESKEY_rep_mode" = "sync" -a ! "$OCF_RESKEY_rep_mode" = "async" ]; then |
| 1959 |
ocf_exit_reason "Invalid rep_mode : $OCF_RESKEY_rep_mode" |
| 1960 |
return $OCF_ERR_CONFIGURED |
| 1961 |
fi |
| 1962 |
if [ ! -n "$NODE_LIST" ]; then |
| 1963 |
ocf_exit_reason "node_list can't be empty." |
| 1964 |
return $OCF_ERR_CONFIGURED |
| 1965 |
fi |
| 1966 |
if [ $check_config_rc -eq 0 ]; then |
| 1967 |
rep_mode_string="include '$REP_MODE_CONF' # added by pgsql RA" |
| 1968 |
if [ "$OCF_RESKEY_rep_mode" = "sync" ]; then |
| 1969 |
if ! grep -q "^[[:space:]]*$rep_mode_string" $OCF_RESKEY_config; then |
| 1970 |
ocf_log info "adding include directive into $OCF_RESKEY_config" |
| 1971 |
echo "$rep_mode_string" >> $OCF_RESKEY_config |
| 1972 |
fi |
| 1973 |
else |
| 1974 |
if grep -q "$rep_mode_string" $OCF_RESKEY_config; then |
| 1975 |
ocf_log info "deleting include directive from $OCF_RESKEY_config" |
| 1976 |
rep_mode_string=`echo $rep_mode_string | sed -e 's|/|\\\\/|g'` |
| 1977 |
sed -i "/$rep_mode_string/d" $OCF_RESKEY_config |
| 1978 |
fi |
| 1979 |
fi |
| 1980 |
fi |
| 1981 |
if ! mkdir -p $OCF_RESKEY_tmpdir || ! chown $OCF_RESKEY_pgdba $OCF_RESKEY_tmpdir || ! chmod 700 $OCF_RESKEY_tmpdir; then |
| 1982 |
ocf_exit_reason "Can't create directory $OCF_RESKEY_tmpdir or it is not readable by $OCF_RESKEY_pgdba" |
| 1983 |
return $OCF_ERR_PERM |
| 1984 |
fi |
| 1985 |
fi |
| 1986 |
|
| 1987 |
if [ "$OCF_RESKEY_rep_mode" = "slave" ]; then |
| 1988 |
if ocf_is_ms; then |
| 1989 |
ocf_exit_reason "Replication(rep_mode=slave) does not support Master/Slave configuration." |
| 1990 |
return $OCF_ERR_CONFIGURED |
| 1991 |
fi |
| 1992 |
fi |
| 1993 |
|
| 1994 |
if use_replication_slot; then |
| 1995 |
ocf_version_cmp "$version" "9.4" |
| 1996 |
rc=$? |
| 1997 |
if [ $rc -eq 0 ]||[ $rc -eq 3 ]; then |
| 1998 |
ocf_exit_reason "Replication slot needs PostgreSQL 9.4 or higher." |
| 1999 |
return $OCF_ERR_CONFIGURED |
| 2000 |
fi |
| 2001 |
|
| 2002 |
echo "$OCF_RESKEY_replication_slot_name" | grep -q -e '[^a-z0-9_]' |
| 2003 |
if [ $? -eq 0 ]; then |
| 2004 |
ocf_exit_reason "Invalid replication_slot_name($OCF_RESKEY_replication_slot_name). only use lower case letters, numbers, and the underscore character." |
| 2005 |
return $OCF_ERR_CONFIGURED |
| 2006 |
fi |
| 2007 |
fi |
| 2008 |
|
| 2009 |
return $OCF_SUCCESS |
| 2010 |
} |
| 2011 |
|
| 2012 |
# Validate most critical parameters |
| 2013 |
pgsql_validate_all() { |
| 2014 |
local rc |
| 2015 |
|
| 2016 |
getent passwd $OCF_RESKEY_pgdba >/dev/null 2>&1 |
| 2017 |
if [ ! $? -eq 0 ]; then |
| 2018 |
ocf_exit_reason "User $OCF_RESKEY_pgdba doesn't exist"; |
| 2019 |
return $OCF_ERR_INSTALLED; |
| 2020 |
fi |
| 2021 |
|
| 2022 |
if [ -z "$OCF_RESKEY_monitor_user" ] && [ -n "$OCF_RESKEY_monitor_password" ]; then |
| 2023 |
ocf_exit_reason "monitor_user has to be set if monitor_password is set" |
| 2024 |
return $OCF_ERR_CONFIGURED |
| 2025 |
fi |
| 2026 |
|
| 2027 |
if [ "$OCF_CHECK_LEVEL" -eq 10 ]; then |
| 2028 |
validate_ocf_check_level_10 |
| 2029 |
rc=$? |
| 2030 |
[ $rc -ne "$OCF_SUCCESS" ] && exit $rc |
| 2031 |
fi |
| 2032 |
|
| 2033 |
return $OCF_SUCCESS |
| 2034 |
} |
| 2035 |
|
| 2036 |
|
| 2037 |
# |
| 2038 |
# Check if we need to create a log file |
| 2039 |
# |
| 2040 |
|
| 2041 |
check_log_file() { |
| 2042 |
if [ ! -e "$1" ] |
| 2043 |
then |
| 2044 |
touch $1 > /dev/null 2>&1 |
| 2045 |
chown $OCF_RESKEY_pgdba:`getent passwd $OCF_RESKEY_pgdba | cut -d ":" -f 4` $1 |
| 2046 |
fi |
| 2047 |
|
| 2048 |
#Check if $OCF_RESKEY_pgdba can write to the log file |
| 2049 |
if ! runasowner "test -w $1" |
| 2050 |
then |
| 2051 |
return 1 |
| 2052 |
fi |
| 2053 |
|
| 2054 |
return 0 |
| 2055 |
} |
| 2056 |
|
| 2057 |
# |
| 2058 |
# Check if we need to create stats temp directory in tmpfs |
| 2059 |
# |
| 2060 |
|
| 2061 |
check_stat_temp_directory() { |
| 2062 |
local stats_temp |
| 2063 |
|
| 2064 |
stats_temp=`get_pgsql_param stats_temp_directory` |
| 2065 |
|
| 2066 |
if [ -z "$stats_temp" ]; then |
| 2067 |
return |
| 2068 |
fi |
| 2069 |
|
| 2070 |
if [ "${stats_temp#/}" = "$stats_temp" ]; then |
| 2071 |
stats_temp="$OCF_RESKEY_pgdata/$stats_temp" |
| 2072 |
fi |
| 2073 |
|
| 2074 |
if [ -d "$stats_temp" ]; then |
| 2075 |
return |
| 2076 |
fi |
| 2077 |
|
| 2078 |
if ! mkdir -p "$stats_temp"; then |
| 2079 |
ocf_exit_reason "Can't create directory $stats_temp" |
| 2080 |
exit $OCF_ERR_PERM |
| 2081 |
fi |
| 2082 |
|
| 2083 |
if ! chown $OCF_RESKEY_pgdba: "$stats_temp"; then |
| 2084 |
ocf_exit_reason "Can't change ownership for $stats_temp" |
| 2085 |
exit $OCF_ERR_PERM |
| 2086 |
fi |
| 2087 |
|
| 2088 |
if ! chmod 700 "$stats_temp"; then |
| 2089 |
ocf_exit_reason "Can't change permissions for $stats_temp" |
| 2090 |
exit $OCF_ERR_PERM |
| 2091 |
fi |
| 2092 |
} |
| 2093 |
|
| 2094 |
# |
| 2095 |
# Check socket directory |
| 2096 |
# |
| 2097 |
check_socket_dir() { |
| 2098 |
if [ ! -d "$OCF_RESKEY_socketdir" ]; then |
| 2099 |
if ! mkdir "$OCF_RESKEY_socketdir"; then |
| 2100 |
ocf_exit_reason "Can't create directory $OCF_RESKEY_socketdir" |
| 2101 |
exit $OCF_ERR_PERM |
| 2102 |
fi |
| 2103 |
|
| 2104 |
if ! chown $OCF_RESKEY_pgdba:`getent passwd \ |
| 2105 |
$OCF_RESKEY_pgdba | cut -d ":" -f 4` "$OCF_RESKEY_socketdir" |
| 2106 |
then |
| 2107 |
ocf_exit_reason "Can't change ownership for $OCF_RESKEY_socketdir" |
| 2108 |
exit $OCF_ERR_PERM |
| 2109 |
fi |
| 2110 |
|
| 2111 |
if ! chmod 2775 "$OCF_RESKEY_socketdir"; then |
| 2112 |
ocf_exit_reason "Can't change permissions for $OCF_RESKEY_socketdir" |
| 2113 |
exit $OCF_ERR_PERM |
| 2114 |
fi |
| 2115 |
else |
| 2116 |
if ! runasowner "touch $OCF_RESKEY_socketdir/test.$$"; then |
| 2117 |
ocf_exit_reason "$OCF_RESKEY_pgdba can't create files in $OCF_RESKEY_socketdir" |
| 2118 |
exit $OCF_ERR_PERM |
| 2119 |
fi |
| 2120 |
rm $OCF_RESKEY_socketdir/test.$$ |
| 2121 |
fi |
| 2122 |
} |
| 2123 |
|
| 2124 |
print_crm_mon() { |
| 2125 |
if [ -z "$CRM_MON_OUTPUT" ]; then |
| 2126 |
ocf_version_cmp "$OCF_RESKEY_crm_feature_set" "3.1.0" |
| 2127 |
res=$? |
| 2128 |
if [ -z "$OCF_RESKEY_crm_feature_set" ] || [ $res -eq 2 ]; then |
| 2129 |
XMLOPT="--output-as=xml" |
| 2130 |
ocf_version_cmp "$OCF_RESKEY_crm_feature_set" "3.2.0" |
| 2131 |
if [ $? -eq 1 ]; then |
| 2132 |
crm_mon -1 $XMLOPT >/dev/null 2>&1 |
| 2133 |
if [ $? -ne 0 ]; then |
| 2134 |
XMLOPT="--as-xml" |
| 2135 |
fi |
| 2136 |
fi |
| 2137 |
else |
| 2138 |
XMLOPT="--as-xml" |
| 2139 |
fi |
| 2140 |
CRM_MON_OUTPUT=`exec_with_retry 0 crm_mon -1 $XMLOPT` |
| 2141 |
fi |
| 2142 |
printf "${CRM_MON_OUTPUT}\n" |
| 2143 |
} |
| 2144 |
|
| 2145 |
# |
| 2146 |
# 'main' starts here... |
| 2147 |
# |
| 2148 |
|
| 2149 |
|
| 2150 |
if [ $# -ne 1 ] |
| 2151 |
then |
| 2152 |
usage |
| 2153 |
exit $OCF_ERR_GENERIC |
| 2154 |
fi |
| 2155 |
|
| 2156 |
PIDFILE=${OCF_RESKEY_pgdata}/postmaster.pid |
| 2157 |
BACKUPLABEL=${OCF_RESKEY_pgdata}/backup_label |
| 2158 |
RESOURCE_NAME=`echo $OCF_RESOURCE_INSTANCE | cut -d ":" -f 1` |
| 2159 |
PGSQL_WAL_RECEIVER_STATUS_ATTR="${RESOURCE_NAME}-receiver-status" |
| 2160 |
RECOVERY_CONF=${OCF_RESKEY_pgdata}/recovery.conf |
| 2161 |
NODENAME=$(ocf_local_nodename | tr '[A-Z]' '[a-z]') |
| 2162 |
USE_STANDBY_SIGNAL=false |
| 2163 |
|
| 2164 |
case "$1" in |
| 2165 |
methods) pgsql_methods |
| 2166 |
exit $?;; |
| 2167 |
|
| 2168 |
meta-data) meta_data |
| 2169 |
exit $OCF_SUCCESS;; |
| 2170 |
esac |
| 2171 |
|
| 2172 |
[ "$__OCF_ACTION" != "validate-all" ] && OCF_CHECK_LEVEL=10 |
| 2173 |
pgsql_validate_all |
| 2174 |
rc=$? |
| 2175 |
|
| 2176 |
[ "$1" = "validate-all" ] && exit $rc |
| 2177 |
|
| 2178 |
if [ $rc -ne 0 ] |
| 2179 |
then |
| 2180 |
case "$1" in |
| 2181 |
stop) if is_replication; then |
| 2182 |
change_pgsql_status "$NODENAME" "UNKNOWN" |
| 2183 |
fi |
| 2184 |
exit $OCF_SUCCESS;; |
| 2185 |
monitor) exit $OCF_NOT_RUNNING;; |
| 2186 |
status) exit $OCF_NOT_RUNNING;; |
| 2187 |
*) exit $rc;; |
| 2188 |
esac |
| 2189 |
fi |
| 2190 |
|
| 2191 |
US=`id -u -n` |
| 2192 |
|
| 2193 |
if [ $US != root -a $US != $OCF_RESKEY_pgdba ] |
| 2194 |
then |
| 2195 |
ocf_exit_reason "$0 must be run as root or $OCF_RESKEY_pgdba" |
| 2196 |
exit $OCF_ERR_GENERIC |
| 2197 |
fi |
| 2198 |
|
| 2199 |
# make psql command options |
| 2200 |
[ -z "$OCF_RESKEY_monitor_user" ] && OCF_RESKEY_monitor_user="$OCF_RESKEY_pgdba" |
| 2201 |
[ -n "$OCF_RESKEY_monitor_password" ] && export PGPASSWORD="$OCF_RESKEY_monitor_password" |
| 2202 |
psql_options="-p $OCF_RESKEY_pgport $OCF_RESKEY_pgdb" |
| 2203 |
|
| 2204 |
if [ -n "$OCF_RESKEY_pghost" ]; then |
| 2205 |
psql_options="$psql_options -h $OCF_RESKEY_pghost" |
| 2206 |
else |
| 2207 |
if [ -n "$OCF_RESKEY_socketdir" ]; then |
| 2208 |
psql_options="$psql_options -h $OCF_RESKEY_socketdir" |
| 2209 |
fi |
| 2210 |
fi |
| 2211 |
|
| 2212 |
if [ -n "$OCF_RESKEY_pgport" ]; then |
| 2213 |
export PGPORT=$OCF_RESKEY_pgport |
| 2214 |
fi |
| 2215 |
|
| 2216 |
if [ -n "$OCF_RESKEY_pglibs" ]; then |
| 2217 |
if [ -n "$LD_LIBRARY_PATH" ]; then |
| 2218 |
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OCF_RESKEY_pglibs |
| 2219 |
else |
| 2220 |
export LD_LIBRARY_PATH=$OCF_RESKEY_pglibs |
| 2221 |
fi |
| 2222 |
fi |
| 2223 |
|
| 2224 |
|
| 2225 |
# What kind of method was invoked? |
| 2226 |
case "$1" in |
| 2227 |
status) if pgsql_status |
| 2228 |
then |
| 2229 |
ocf_log info "PostgreSQL is up" |
| 2230 |
exit $OCF_SUCCESS |
| 2231 |
else |
| 2232 |
ocf_log info "PostgreSQL is down" |
| 2233 |
exit $OCF_NOT_RUNNING |
| 2234 |
fi;; |
| 2235 |
|
| 2236 |
monitor) pgsql_monitor |
| 2237 |
exit $?;; |
| 2238 |
|
| 2239 |
start) pgsql_start |
| 2240 |
exit $?;; |
| 2241 |
|
| 2242 |
promote) pgsql_promote |
| 2243 |
exit $?;; |
| 2244 |
|
| 2245 |
demote) pgsql_demote |
| 2246 |
exit $?;; |
| 2247 |
|
| 2248 |
notify) pgsql_notify |
| 2249 |
exit $?;; |
| 2250 |
|
| 2251 |
stop) pgsql_stop |
| 2252 |
exit $?;; |
| 2253 |
*) |
| 2254 |
exit $OCF_ERR_UNIMPLEMENTED;; |
| 2255 |
esac |