Saltstack Official Galera Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.6KB

  1. #!/bin/bash
  2. #
  3. # Script to make a proxy (ie HAProxy) capable of monitoring Percona XtraDB Cluster nodes properly
  4. #
  5. # Author: Olaf van Zandwijk <olaf.vanzandwijk@nedap.com>
  6. # Author: Raghavendra Prabhu <raghavendra.prabhu@percona.com>
  7. #
  8. # Documentation and download: https://github.com/olafz/percona-clustercheck
  9. #
  10. # Based on the original script from Unai Rodriguez
  11. #
  12. if [[ $1 == '-h' || $1 == '--help' ]];then
  13. echo "Usage: $0 <user> <pass> <available_when_donor=0|1> <log_file> <available_when_readonly=0|1> <defaults_extra_file>"
  14. exit
  15. fi
  16. # if the disabled file is present, return 503. This allows
  17. # admins to manually remove a node from a cluster easily.
  18. if [ -e "/var/tmp/clustercheck.disabled" ]; then
  19. # Shell return-code is 1
  20. echo -en "HTTP/1.1 503 Service Unavailable\r\n"
  21. echo -en "Content-Type: text/plain\r\n"
  22. echo -en "Connection: close\r\n"
  23. echo -en "Content-Length: 51\r\n"
  24. echo -en "\r\n"
  25. echo -en "Percona XtraDB Cluster Node is manually disabled.\r\n"
  26. sleep 0.1
  27. exit 1
  28. fi
  29. MYSQL_USERNAME="${1-clustercheckuser}"
  30. MYSQL_PASSWORD="${2-clustercheckpassword!}"
  31. AVAILABLE_WHEN_DONOR=${3:-0}
  32. ERR_FILE="${4:-/dev/null}"
  33. AVAILABLE_WHEN_READONLY=${5:-1}
  34. DEFAULTS_EXTRA_FILE=${6:-/etc/my.cnf}
  35. #Timeout exists for instances where mysqld may be hung
  36. TIMEOUT=10
  37. EXTRA_ARGS=""
  38. if [[ -n "$MYSQL_USERNAME" ]]; then
  39. EXTRA_ARGS="$EXTRA_ARGS --user=${MYSQL_USERNAME}"
  40. fi
  41. if [[ -n "$MYSQL_PASSWORD" ]]; then
  42. EXTRA_ARGS="$EXTRA_ARGS --password=${MYSQL_PASSWORD}"
  43. fi
  44. if [[ -r $DEFAULTS_EXTRA_FILE ]];then
  45. MYSQL_CMDLINE="mysql --defaults-extra-file=$DEFAULTS_EXTRA_FILE -nNE --connect-timeout=$TIMEOUT \
  46. ${EXTRA_ARGS}"
  47. else
  48. MYSQL_CMDLINE="mysql -nNE --connect-timeout=$TIMEOUT ${EXTRA_ARGS}"
  49. fi
  50. #
  51. # Perform the query to check the wsrep_local_state
  52. #
  53. WSREP_STATUS=$($MYSQL_CMDLINE -e "SHOW STATUS LIKE 'wsrep_local_state';" \
  54. 2>${ERR_FILE} | tail -1 2>>${ERR_FILE})
  55. if [[ "${WSREP_STATUS}" == "4" ]] || [[ "${WSREP_STATUS}" == "2" && ${AVAILABLE_WHEN_DONOR} == 1 ]]
  56. then
  57. # Check only when set to 0 to avoid latency in response.
  58. if [[ $AVAILABLE_WHEN_READONLY -eq 0 ]];then
  59. READ_ONLY=$($MYSQL_CMDLINE -e "SHOW GLOBAL VARIABLES LIKE 'read_only';" \
  60. 2>${ERR_FILE} | tail -1 2>>${ERR_FILE})
  61. if [[ "${READ_ONLY}" == "ON" ]];then
  62. # Percona XtraDB Cluster node local state is 'Synced', but it is in
  63. # read-only mode. The variable AVAILABLE_WHEN_READONLY is set to 0.
  64. # => return HTTP 503
  65. # Shell return-code is 1
  66. echo -en "HTTP/1.1 503 Service Unavailable\r\n"
  67. echo -en "Content-Type: text/plain\r\n"
  68. echo -en "Connection: close\r\n"
  69. echo -en "Content-Length: 43\r\n"
  70. echo -en "\r\n"
  71. echo -en "Percona XtraDB Cluster Node is read-only.\r\n"
  72. sleep 0.1
  73. exit 1
  74. fi
  75. fi
  76. # Percona XtraDB Cluster node local state is 'Synced' => return HTTP 200
  77. # Shell return-code is 0
  78. echo -en "HTTP/1.1 200 OK\r\n"
  79. echo -en "Content-Type: text/plain\r\n"
  80. echo -en "Connection: close\r\n"
  81. echo -en "Content-Length: 40\r\n"
  82. echo -en "\r\n"
  83. echo -en "Percona XtraDB Cluster Node is synced.\r\n"
  84. sleep 0.1
  85. exit 0
  86. else
  87. # Percona XtraDB Cluster node local state is not 'Synced' => return HTTP 503
  88. # Shell return-code is 1
  89. echo -en "HTTP/1.1 503 Service Unavailable\r\n"
  90. echo -en "Content-Type: text/plain\r\n"
  91. echo -en "Connection: close\r\n"
  92. echo -en "Content-Length: 44\r\n"
  93. echo -en "\r\n"
  94. echo -en "Percona XtraDB Cluster Node is not synced.\r\n"
  95. sleep 0.1
  96. exit 1
  97. fi