Mysql Master/Slave monitoring script includes automatic configuration of sending Gmail Mail Client

Source: Internet
Author: User
Tags install openssl account gmail gmail mail

Common script backup

 
 
  1. Automatically configure msmtp and use mail to send emails
  2. #! /Bin/bash
  3. SRC =/usr/local/src
  4. Cd $ SRC
  5. # Install msmtp client
  6. # If your want support TLS/SSL, install openssl-devel
  7. # Yum install-y openssl-devel //
  8. Wget http://nchc.dl.sourceforge.net/project/msmtp/msmtp/1.4.28/msmtp-1.4.28.tar.bz2
  9. Tar xjf msmtp-1.4.28.tar.bz2
  10. Cd msmtp-1.4.28
  11. ./Configure -- prefix =/usr/local/msmtp & make install
  12. Cd/usr/local/msmtp/
  13. Mkdir etc
  14. Echo 'account gmail
  15. Host smtp.gmail.com
  16. From reportonline@gmail.com
  17. Auth on
  18. Tls on
  19. Tls_starttls on
  20. Tls_force_sslv3 on
  21. Tls_trust_file/usr/local/msmtp/etc/gmail. crt
  22. User reportonline@gmail.com
  23. Password 1234567890
  24. Port 587
  25. Syslog off
  26. Logfile/tmp/msmtp. log
  27. Account default: gmail '>/usr/local/msmtp/etc/msmtprc
  28. Wget-O/usr/local/msmtp/etc/gmail. crt http://www.geotrust.com/resources/extended-validation-ssl/certs/Equifax%20Secure%20Certificate%20Authority.crt
  29. Ln-s/usr/local/msmtp/bin/msmtp/usr/bin/
  30. Echo "set sendmail =/usr/bin/msmtp">/etc/mail. rc
  31.  
  32. Script 2 <monitoring mysql Server Master/Slave, from fuqin liquor cooking>
  33. #! /Bin/bash
  34. # Check MySQL_Slave Status
  35. # Crontab time every 10 min
  36. Test-e/data0/mysql/check_mysql_slave.log | touch/data0/mysql/check_mysql_slave.log
  37. Chown mysql. mysql/data0/mysql/check_mysql_slave.log
  38.  
  39. MYSQLPORT = 'netstat-na | grep "LISTEN" | grep "3306" | awk-F [: ""] + '{print $5 }''
  40. MYSQLIP = 'ifconfig eth0 | grep "inet addr" | awk-F [: ""] + '{print $4 }''
  41. STATUS = $ (/usr/local/mysql/bin/mysql-uadmin-ppassword-S/tmp/mysql. sock-e "show slave status \ G" | grep-I "running ")
  42. IO_env = 'echo $ STATUS | grep IO | awk '{print $2 }''
  43. SQL _env = 'echo $ STATUS | grep SQL | awk '{print $2 }''
  44. DATA = 'date + "% y-% m-% d % H: % M: % S "'
  45.  
  46. If ["$ MYSQLPORT" = "3306"]
  47. Then
  48. Echo "mysql is running"
  49. Else
  50. Mail-s "ERROR! Server: $ MYSQLIP mysql is down "start@gmail.com-c user1@gmail.com-c user2@gmail.com-c user3@gmail.com
  51. Fi
  52.  
  53. If ["$ IO_env" = "Yes"-a "$ SQL _env" = "Yes"]
  54. Then
  55. Echo "Slave is running! "
  56. Else
  57. Echo "######## $ DATA ########">/data0/mysql/check_mysql_slave.log
  58. Echo "Slave is not running! ">/Data0/mysql/check_mysql_slave.log
  59. Echo "Slave is not running! "| Mail-s" ERROR! $ MYSQLIP MySQL Slave is not running "start@gmail.com-c user1@gmail.com-c user2@gmail.com-c user3@gmail.com
  60. Fi

Recently, due to actual needs, the following changes have been made to the script and DNS monitoring has been added. The difference from the previous one is that an email is sent. Instead of sending an email when there is an error.

 
 
  1. #!/bin/bash  -x 
  2. #check MySQL_Slave Status and check dns status 
  3. #crontab time 00:10 
  4. test -e /data0/mysql/check_mysql_slave.log || touch /data0/mysql/check_mysql_slave.log  
  5. chown mysql.mysql /data0/mysql/check_mysql_slave.log  
  6.  
  7. MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'` 
  8. MYSQLIP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'` 
  9. STATUS=$(/usr/local/mysql/bin/mysql -uadmin -ppassword -S /tmp/mysql.sock -e "show slave status\G" | grep -i "running") 
  10. IO_env=`echo $STATUS | grep IO | awk  ' {print $2}'` 
  11. SQL_env=`echo $STATUS | grep SQL | awk  '{print $2}'` 
  12. DATA=`date +"%y-%m-%d %H:%M:%S"` 
  13.  
  14.  > /data0/mysql/check_mysql_slave.log  
  15.  
  16. if [ "$MYSQLPORT" == "3306" ] 
  17. then 
  18.     echo "mysql is running" 
  19. else 
  20.     echo "Mysql Server is not running!" >>  /data0/mysql/check_mysql_slave.log 
  21. fi 
  22.  
  23. if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ];then 
  24.     echo "Slave is running"  
  25.     ps aux | grep bind | grep -v grep > /dev/null  
  26.     if [ "$?" == "0" ];then 
  27.         echo "Bind9 is running" 
  28.     else 
  29.         echo "####### $DATA #########">> /data0/mysql/check_mysql_slave.log 
  30.         echo "Bind9 is not running!" >>  /data0/mysql/check_mysql_slave.log 
  31.     fi 
  32. else 
  33.     echo "####### $DATA #########">> /data0/mysql/check_mysql_slave.log 
  34.     echo "Slave is not running!" >>  /data0/mysql/check_mysql_slave.log 
  35. fi 
  36.  
  37. LINE=`wc -l /data0/mysql/check_mysql_slave.log | awk '{print $1}' ` 
  38. if [ "$LINE" -gt "0" ];then 
  39.     cat /data0/mysql/check_mysql_slave.log | mail -s "PLOBLEAM: DNS SERVER $MYSQLIP ERROR INFORMATION" baoch8@163.com 
  40. fi 

Online machine running

 
 
  1. + test -e /data0/mysql/check_mysql_slave.log 
  2. + chown mysql.mysql /data0/mysql/check_mysql_slave.log 
  3. ++ netstat -na 
  4. ++ grep LISTEN 
  5. ++ grep 3306 
  6. ++ awk '-F[: ]+' '{print $5}' 
  7. + MYSQLPORT=3306 
  8. ++ ifconfig eth0 
  9. ++ grep 'inet addr' 
  10. ++ awk '-F[: ]+' '{print $4}' 
  11. + MYSQLIP=118.X.X.X 
  12. ++ /usr/local/mysql/bin/mysql -uadmin -ppassword -S /tmp/mysql.sock -e 'show slave status\G' 
  13. ++ grep -i running 
  14. + STATUS='             Slave_IO_Running: Yes 
  15.             Slave_SQL_Running: Yes' 
  16. ++ echo Slave_IO_Running: Yes Slave_SQL_Running: Yes 
  17. ++ grep IO 
  18. ++ awk ' {print $2}' 
  19. + IO_env=Yes 
  20. ++ echo Slave_IO_Running: Yes Slave_SQL_Running: Yes 
  21. ++ grep SQL 
  22. ++ awk '{print $2}' 
  23. + SQL_env=Yes 
  24. ++ date '+%y-%m-%d %H:%M:%S' 
  25. + DATA='12-12-28 18:44:14' 
  26. + '[' 3306 == 3306 ']' 
  27. + echo 'mysql is running' 
  28. mysql is running 
  29. + '[' YesYes = Yes -a YesYes = Yes ']' 
  30. + echo 'Slave is running' 
  31. Slave is running 
  32. + ps aux 
  33. + grep bind 
  34. + grep -v grep 
  35. + '[' 0 == 0 ']' 
  36. + echo 'Bind9 is running' 
  37. Bind9 is running 
  38. ++ wc -l /data0/mysql/check_mysql_slave.log 
  39. ++ awk '{print $1}' 
  40. + LINE=5 
  41. + '[' 5 -ne 0 ']' 
  42. + cat /data0/mysql/check_mysql_slave.log 
  43. + mail -s 'PLOBLEAM: DNS SERVER 118.X.X.X ERROR INFORMATION' baoch8@163.com

 

This article from the "diving into the ocean of technology" blog, please be sure to keep this source http://myhat.blog.51cto.com/391263/1102487

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.