Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.46 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Script to make a proxy (ie HAProxy) capable of monitoring Percona XtraDB Cluster nodes properly
  4. #
  5. # Authors:
  6. # Raghavendra Prabhu <raghavendra.prabhu@percona.com>
  7. # Olaf van Zandwijk <olaf.vanzandwijk@nedap.com>
  8. #
  9. # Based on the original script from Unai Rodriguez and Olaf (https://github.com/olafz/percona-clustercheck)
  10. #
  11. # Grant privileges required:
  12. # GRANT PROCESS ON *.* TO 'clustercheckuser'@'localhost' IDENTIFIED BY 'clustercheckpassword!';
  13.  
  14. if [[ $1 == '-h' || $1 == '--help' ]];then
  15.     echo "Usage: $0 <user> <pass> <available_when_donor=0|1> <log_file> <available_when_readonly=0|1> <defaults_extra_file>"
  16.     exit
  17. fi
  18.  
  19. MYSQL_USERNAME="${1-clustercheckuser}"
  20. MYSQL_PASSWORD="${2-clustercheckpassword!}"
  21. AVAILABLE_WHEN_DONOR=${3:-1}
  22. ERR_FILE="${4:-/dev/null}"
  23. AVAILABLE_WHEN_READONLY=${5:-1}
  24. DEFAULTS_EXTRA_FILE=${6:-/etc/my.cnf}
  25. #Timeout exists for instances where mysqld may be hung
  26. TIMEOUT=10
  27.  
  28. EXTRA_ARGS=""
  29. if [[ -n "$MYSQL_USERNAME" ]]; then
  30.     EXTRA_ARGS="$EXTRA_ARGS --user=${MYSQL_USERNAME}"
  31. fi
  32. if [[ -n "$MYSQL_PASSWORD" ]]; then
  33.     EXTRA_ARGS="$EXTRA_ARGS --password=${MYSQL_PASSWORD}"
  34. fi
  35. if [[ -r $DEFAULTS_EXTRA_FILE ]];then
  36.     MYSQL_CMDLINE="mysql --defaults-extra-file=$DEFAULTS_EXTRA_FILE -nNE --connect-timeout=$TIMEOUT \
  37.                    ${EXTRA_ARGS}"
  38. else
  39.     MYSQL_CMDLINE="mysql -nNE --connect-timeout=$TIMEOUT ${EXTRA_ARGS}"
  40. fi
  41. #
  42. # Perform the query to check the wsrep_local_state
  43. #
  44. WSREP_STATUS=($($MYSQL_CMDLINE -e "SHOW GLOBAL STATUS LIKE 'wsrep_%';"  \
  45.     2>${ERR_FILE} | grep -A 1 -E 'wsrep_local_state$|wsrep_cluster_status$' \
  46.     | sed -n -e '2p'  -e '5p' | tr '\n' ' '))
  47.  
  48. if [[ ${WSREP_STATUS[1]} == 'Primary' && ( ${WSREP_STATUS[0]} -eq 4 || \
  49.     ( ${WSREP_STATUS[0]} -eq 2 && $AVAILABLE_WHEN_DONOR -eq 1 ) ) ]]
  50. then
  51.  
  52.     # Check only when set to 0 to avoid latency in response.
  53.     if [[ $AVAILABLE_WHEN_READONLY -eq 0 ]];then
  54.         READ_ONLY=$($MYSQL_CMDLINE -e "SHOW GLOBAL VARIABLES LIKE 'read_only';" \
  55.                     2>${ERR_FILE} | tail -1 2>>${ERR_FILE})
  56.  
  57.         if [[ "${READ_ONLY}" == "ON" ]];then
  58.             # Percona XtraDB Cluster node local state is 'Synced', but it is in
  59.             # read-only mode. The variable AVAILABLE_WHEN_READONLY is set to 0.
  60.             # => return HTTP 503
  61.             # Shell return-code is 1
  62.             echo -en "HTTP/1.1 503 Service Unavailable\r\n"
  63.             echo -en "Content-Type: text/plain\r\n"
  64.             echo -en "Connection: close\r\n"
  65.             echo -en "Content-Length: 43\r\n"
  66.             echo -en "\r\n"
  67.             echo -en "Percona XtraDB Cluster Node is read-only.\r\n"
  68.             sleep 0.1
  69.             exit 1
  70.         fi
  71.  
  72.     fi
  73.     # Percona XtraDB Cluster node local state is 'Synced' => return HTTP 200
  74.     # Shell return-code is 0
  75.     echo -en "HTTP/1.1 200 OK\r\n"
  76.     echo -en "Content-Type: text/plain\r\n"
  77.     echo -en "Connection: close\r\n"
  78.     echo -en "Content-Length: 40\r\n"
  79.     echo -en "\r\n"
  80.     echo -en "Percona XtraDB Cluster Node is synced.\r\n"
  81.     sleep 0.1
  82.     exit 0
  83. else
  84.     # Percona XtraDB Cluster node local state is not 'Synced' => return HTTP 503
  85.     # Shell return-code is 1
  86.     echo -en "HTTP/1.1 503 Service Unavailable\r\n"
  87.     echo -en "Content-Type: text/plain\r\n"
  88.     echo -en "Connection: close\r\n"
  89.     echo -en "Content-Length: 57\r\n"
  90.     echo -en "\r\n"
  91.     echo -en "Percona XtraDB Cluster Node is not synced or non-PRIM. \r\n"
  92.     sleep 0.1
  93.     exit 1
  94. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement