mysql_replication_cron.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. #
  3. # CopyLeft Mathieu Moulin <lemathou@free.fr>
  4. # GNU GPL v2
  5. #
  6. # Mysql Replication Verification Script
  7. # Usage : send an email when the replication process is stopped or too slow
  8. # to prevent desynchronization problems
  9. #
  10. #
  11. # You need to create a Mysql User replication_cron
  12. # with "replication client" global administration privilege
  13. #
  14. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  15. . ./params.inc
  16. if [ -f $tmp_replication_slave_reason_filepath ]; then
  17. rm -rf $tmp_replication_slave_reason_filepath
  18. fi
  19. echo "show slave status\G;" |mysql $cmd_slave_username $cmd_slave_hostname $cmd_slave_userpass > $tmp_replication_slave_log_filepath
  20. grep "Access denied" $tmp_replication_slave_log_filepath
  21. if [ "$?" -ne "1" ]; then
  22. msg="Access denied to MySQL slave user"
  23. echo $msg
  24. echo $msg >> $tmp_replication_slave_reason_filepath
  25. fi
  26. if [ ! -s $tmp_replication_slave_log_filepath ]; then
  27. msg="Slave status empty => Slave not running ?"
  28. echo $msg
  29. echo $msg >> $tmp_replication_slave_reason_filepath
  30. fi
  31. grep "Slave_IO_Running: No" $tmp_replication_slave_log_filepath
  32. if [ "$?" -ne "1" ]; then
  33. msg="Slave IO not Running"
  34. echo $msg
  35. echo $msg >> $tmp_replication_slave_reason_filepath
  36. fi
  37. grep "Slave_SQL_Running: No" $tmp_replication_slave_log_filepath
  38. if [ "$?" -ne "1" ]; then
  39. msg="Slave SQL not Running"
  40. echo $msg
  41. echo $msg >> $tmp_replication_slave_reason_filepath
  42. fi
  43. regex="Seconds_Behind_Master: \(\d+\)"
  44. seconds=`grep "Seconds_Behind_Master" $tmp_replication_slave_log_filepath | tr -dc '0-9'`
  45. if [ $seconds="" ]; then
  46. seconds=0
  47. fi
  48. if (( $seconds > $slave_verif_seconds_min )); then
  49. seconds_orig=$seconds
  50. minutes=$(( $seconds/60 ))
  51. hours=$(( $minutes/60 ))
  52. days=$(( $hours/24 ))
  53. hours=$(( $hours - 24*$days ))
  54. minutes=$(( $minutes - 60*(24*$days + $hours) ))
  55. seconds=$(( $seconds - 60*(60*(24*$days + $hours) + $minutes) ))
  56. msg="Seconds behind master : $seconds_orig => $days days, $hours hours, $minutes minutes, $seconds seconds"
  57. echo $msg
  58. echo $msg >> $tmp_replication_slave_reason_filepath
  59. fi
  60. if [ -f $tmp_replication_slave_reason_filepath ]; then
  61. mail -r $slave_verif_email_from -s "$slave_verif_email_subject" $slave_verif_email_to < $tmp_replication_slave_reason_filepath
  62. echo $tmp_replication_slave_reason_filepath
  63. fi