Linux shell mysql database Master/Slave synchronization status check alarm
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. # Global variables include the program path, binfile path, time, and SMS sending 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 to send SQLPARAM_SMS =" mysql-uXXXX-pXXXX-h192.168.1.1 "# Read the configuration file, and get the synchronization status # the content and format of the configuration file can be viewed in my previous blog. You can also analyze the command while read LINEdoCHOST = 'echo $ LINE | awk-F ': ''{print $2}'' DBNAME = 'echo $ LINE | awk-F ': ''{print $1}'' SQLPARAM = "mysql-h $ CHOST-uroot-pdascom" # obtain slave Database Synchronization Status and output it to a temporary file. Of course, you can directly process $ SQLPARAM <$ DIR/status. SQL> $ DIR/temp. logREIO = '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 this function, refer to the comprehensive reference link if ["$ REIO" x = "Yes" x-a "$ RESQL" x = "Yes" x]. thenecho $ TIME "" Check '$ dbname' is OK. ">>$ BINPATH/log/check. logecho-e "0 \ c"> $ DIR/status. Txtelseecho $ TIME "" Check '$ dbname' is down. ">>$ BINPATH/log/check. logecho-e "$ DBNAME \ c"> $ DIR/status.txt fidone <$ BINPATH/etc/chslave. conf # analyze the results, if an exception occurs, send an SMS alert. if grep-q "DB" $ DIR/status.txt thensed-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 mysq Rochelle slave is down. '); "> $ DIR/temp. SQL $ SQLPARAM_SMS <$ BINPATH/bin/temp. sqlelseecho $ TIME" All DB is OK! ">>$ BINPATH/log/check. logfi # 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.