Alarms for master-slave synchronization status check and mysql alarms in linux shell mysql Databases
Requirements: 1. Monitor the Master/Slave status of the database. 2. Obtain the main database parameters. 3. Read the configuration file. 4. Adaptive deployment location. Reference: http://blog.csdn.net/yf210yf/article/details/9207147http://blog.csdn.net/lllxy/article/details/3423580http://www.jb51.net/article/53022.htmhttp://www.2cto.com/ OS /201307/225515.html # global variables include program path, binfile path, time, send SMS Database Configuration #! /Bin/bash # obtain the shell script execution path DIR = "$ (cd" $ (dirname "$0") "& pwd )"
BINPATH = 'echo $ DIR | sed-e's/\/bin //''
TIME = 'date "+ % Y-% m-% d % H: % M: % S" '# database connection, used for text message sending
SQLPARAM_SMS = "mysql-uXXXX-pXXXX-h192.168.1.1" # Read the configuration file and get the synchronization status # view the content and format of the configuration file in my previous blog, in addition, you can analyze the command while read LINE.
Do
CHOST = 'echo $ LINE | awk-F': ''{print $2 }''
DBNAME = 'echo $ LINE | awk-F': ''{print $1 }''
SQLPARAM = "mysql-h $ CHOST-uroot-pdascom" # obtain the slave Database Synchronization status and output it to a temporary file. Of course, you can also directly process it.
$ SQLPARAM <$ DIR/status. SQL> $ DIR/temp. log
REIO = 'grep Slave_IO_Running $ DIR/temp. log | awk-F': ''{print $2}'' RESQL = 'grep Slave_ SQL _Running $ DIR/temp. log | awk-F': ''{print $2}'' # judge the synchronization status and output the result to a temporary file # if to judge, nothing to say, for details about how to use it, refer to the reference link I have provided. if ["$ REIO" x = "Yes" x-a "$ RESQL" x = "Yes" x]; then
Echo $ TIME "" Check '$ dbname' is OK. "> $ BINPATH/log/check. log
Echo-e "0 \ c"> $ DIR/status.txt
Else
Echo $ TIME "" Check '$ dbname' is down. "> $ BINPATH/log/check. log
Echo-e "$ DBNAME \ c"> $ DIR/status.txt
Fi
Done <$ BINPATH/etc/chslave. conf # analyzes the result. if an exception occurs, an SMS alert is sent. if grep-q "DB" $ DIR/status.txt
Then
Sed-I "s/0 // g" $ DIR/status.txt
ERRDB = 'sed-n 1 p $ DIR/status.txt'
Echo"
Set names 'gbk ';
Insert into ultrax. msgsend (service, srcNo, destNo, msgcontent) values ('30', '000000', '000000', '$ ERRDB mysql_slave is down .');
"> $ DIR/temp. SQL
$ SQLPARAM_SMS <$ BINPATH/bin/temp. SQL
Else
Echo $ TIME "All DB is OK! ">>$ BINPATH/log/check. log
Fi # Delete the temporary file rm-rf $ DIR/status.txt $ DIR/temp. log $ DIR/status.txt $ DIR/temp. SQL PS: The explanation here is quite weak, but it is also a problem of slightly writing ideas. The reason why the if loop for sending alert messages is outside the while loop is that, considering that I need to check the status of multiple databases, it will cause two problems if I judge the status of a text message for each loop: 1. If a problem occurs in multiple databases, multiple text messages will be sent instantly, and the SMS platform will block the behavior of sending text messages to a single number continuously for a short period of time. 2. Waste. Text messages cannot be wasted because it is easier to write. Of course, this script is still a weak function, and I will continue to work hard.