Guest User

Untitled

a guest
Jan 26th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # This script checks if a mysql server is healthy running on localhost. It will
  4. # return:
  5. #
  6. # "HTTP/1.x 200 OK\r" (if mysql is running smoothly)
  7. #
  8. # - OR -
  9. #
  10. # "HTTP/1.x 500 Internal Server Error\r" (else)
  11. #
  12. # The purpose of this script is make haproxy capable of monitoring mysql properly
  13. #
  14. # Author: Unai Rodriguez
  15. #
  16. # It is recommended that a low-privileged-mysql user is created to be used by
  17. # this script. Something like this:
  18. #
  19. # mysql> GRANT SELECT on mysql.* TO 'mysqlchkusr'@'localhost' \
  20. # -> IDENTIFIED BY '257retfg2uysg218' WITH GRANT OPTION;
  21. # mysql> flush privileges;
  22. #
  23. # Script modified by Alex Williams - August 4, 2009
  24. # - removed the need to write to a tmp file, instead store results in memory
  25.  
  26.  
  27. MYSQL_HOST="172.16.0.60"
  28. MYSQL_PORT="3306"
  29. MYSQL_USERNAME="replication_user"
  30. MYSQL_PASSWORD="replication_pass"
  31.  
  32. #
  33. # We perform a simple query that should return a few results :-p
  34.  
  35. ERROR_MSG=`/usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "show databases;" 2>/dev/null`
  36.  
  37. #
  38. # Check the output. If it is not empty then everything is fine and we return
  39. # something. Else, we just do not return anything.
  40. #
  41. if [ "$ERROR_MSG" != "" ]
  42. then
  43. # mysql is fine, return http 200
  44. /bin/echo -e "HTTP/1.1 200 OK\r\n"
  45. /bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
  46. /bin/echo -e "\r\n"
  47. /bin/echo -e "MySQL is running.\r\n"
  48. /bin/echo -e "\r\n"
  49. else
  50. # mysql is fine, return http 503
  51. /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
  52. /bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
  53. /bin/echo -e "\r\n"
  54. /bin/echo -e "MySQL is *down*.\r\n"
  55. /bin/echo -e "\r\n"
  56. fi
Add Comment
Please, Sign In to add comment