歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux管理 >> Linux配置

mysql主從監控腳本含自動配置發Gmail郵件客戶端

常用腳本備份
自動配置msmtp,使用mail發郵件 
#!/bin/bash 
SRC=/usr/local/src 
cd $SRC 
#install msmtp client 
# if your want support TLS/SSL,install openssl-devel 
# yum install -y openssl-devel  // 
wget http://nchc.dl.sourceforge.net/project/msmtp/msmtp/1.4.28/msmtp-1.4.28.tar.bz2 
tar xjf msmtp-1.4.28.tar.bz2 
cd msmtp-1.4.28 
./configure --prefix=/usr/local/msmtp && make && make install  
cd /usr/local/msmtp/ 
mkdir etc 
echo 'account gmail 
host smtp.gmail.com 
from [email protected] 
auth on 
tls on 
tls_starttls on 
tls_force_sslv3 on 
tls_trust_file /usr/local/msmtp/etc/gmail.crt 
user [email protected] 
password 1234567890
port 587 
syslog off 
logfile /tmp/msmtp.log 
account default: gmail'  >  /usr/local/msmtp/etc/msmtprc 
wget -O /usr/local/msmtp/etc/gmail.crt http://www.geotrust.com/resources/extended-validation-ssl/certs/Equifax%20Secure%20Certificate%20Authority.crt 
ln -s /usr/local/msmtp/bin/msmtp /usr/bin/ 
echo "set sendmail=/usr/bin/msmtp" >> /etc/mail.rc  
 
腳本2 <監控mysql服務器主從,來自撫琴煮酒> 
#!/bin/bash  
#check MySQL_Slave Status 
#crontab time every 10 min
test -e /data0/mysql/check_mysql_slave.log || touch /data0/mysql/check_mysql_slave.log  
chown mysql.mysql /data0/mysql/check_mysql_slave.log  
 
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'` 
MYSQLIP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'` 
STATUS=$(/usr/local/mysql/bin/mysql -uadmin -ppassword -S /tmp/mysql.sock -e "show slave status\G" | grep -i "running") 
IO_env=`echo $STATUS | grep IO | awk  ' {print $2}'` 
SQL_env=`echo $STATUS | grep SQL | awk  '{print $2}'` 
DATA=`date +"%y-%m-%d %H:%M:%S"` 
 
if [ "$MYSQLPORT" == "3306" ] 
then 
    echo "mysql is running" 
else 
    mail -s "ERROR!server: $MYSQLIP mysql is down" [email protected]  -c [email protected] -c [email protected] -c [email protected] 
fi 
 
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ] 
then 
    echo "Slave is running!" 
else 
    echo "####### $DATA #########">> /data0/mysql/check_mysql_slave.log 
    echo "Slave is not running!" >>  /data0/mysql/check_mysql_slave.log 
    echo "Slave is not running!" | mail -s "ERROR! $MYSQLIP MySQL Slave is not running" [email protected] -c [email protected] -c [email protected] -c [email protected] 
fi

最近因實際需求,對腳本做了以下更改,添加了DNS的監控。與上一個有些區別的就是,經一發郵件出來。而不是有一個錯就發會郵件出來。
#!/bin/bash  -x 
#check MySQL_Slave Status and check dns status 
#crontab time 00:10 
test -e /data0/mysql/check_mysql_slave.log || touch /data0/mysql/check_mysql_slave.log  
chown mysql.mysql /data0/mysql/check_mysql_slave.log  
 
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'` 
MYSQLIP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'` 
STATUS=$(/usr/local/mysql/bin/mysql -uadmin -ppassword -S /tmp/mysql.sock -e "show slave status\G" | grep -i "running") 
IO_env=`echo $STATUS | grep IO | awk  ' {print $2}'` 
SQL_env=`echo $STATUS | grep SQL | awk  '{print $2}'` 
DATA=`date +"%y-%m-%d %H:%M:%S"` 
 
 > /data0/mysql/check_mysql_slave.log  
 
if [ "$MYSQLPORT" == "3306" ] 
then 
    echo "mysql is running" 
else 
    echo "Mysql Server is not running!" >>  /data0/mysql/check_mysql_slave.log 
fi 
 
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ];then 
    echo "Slave is running"  
    ps aux | grep bind | grep -v grep > /dev/null  
    if [ "$?" == "0" ];then 
        echo "Bind9 is running" 
    else 
        echo "####### $DATA #########">> /data0/mysql/check_mysql_slave.log 
        echo "Bind9 is not running!" >>  /data0/mysql/check_mysql_slave.log 
    fi 
else 
    echo "####### $DATA #########">> /data0/mysql/check_mysql_slave.log 
    echo "Slave is not running!" >>  /data0/mysql/check_mysql_slave.log 
fi 
 
LINE=`wc -l /data0/mysql/check_mysql_slave.log | awk '{print $1}' ` 
if [ "$LINE" -gt "0" ];then 
    cat /data0/mysql/check_mysql_slave.log | mail -s "PLOBLEAM: DNS SERVER $MYSQLIP ERROR INFORMATION" [email protected] 
fi

線上機器運行
+ test -e /data0/mysql/check_mysql_slave.log 
+ chown mysql.mysql /data0/mysql/check_mysql_slave.log 
++ netstat -na 
++ grep LISTEN 
++ grep 3306 
++ awk '-F[: ]+' '{print $5}' 
+ MYSQLPORT=3306 
++ ifconfig eth0 
++ grep 'inet addr' 
++ awk '-F[: ]+' '{print $4}' 
+ MYSQLIP=118.X.X.X 
++ /usr/local/mysql/bin/mysql -uadmin -ppassword -S /tmp/mysql.sock -e 'show slave status\G' 
++ grep -i running 
+ STATUS='             Slave_IO_Running: Yes 
            Slave_SQL_Running: Yes' 
++ echo Slave_IO_Running: Yes Slave_SQL_Running: Yes 
++ grep IO 
++ awk ' {print $2}' 
+ IO_env=Yes 
++ echo Slave_IO_Running: Yes Slave_SQL_Running: Yes 
++ grep SQL 
++ awk '{print $2}' 
+ SQL_env=Yes 
++ date '+%y-%m-%d %H:%M:%S' 
+ DATA='12-12-28 18:44:14' 
+ '[' 3306 == 3306 ']' 
+ echo 'mysql is running' 
mysql is running 
+ '[' YesYes = Yes -a YesYes = Yes ']' 
+ echo 'Slave is running' 
Slave is running 
+ ps aux 
+ grep bind 
+ grep -v grep 
+ '[' 0 == 0 ']' 
+ echo 'Bind9 is running' 
Bind9 is running 
++ wc -l /data0/mysql/check_mysql_slave.log 
++ awk '{print $1}' 
+ LINE=5 
+ '[' 5 -ne 0 ']' 
+ cat /data0/mysql/check_mysql_slave.log 
+ mail -s 'PLOBLEAM: DNS SERVER 118.X.X.X ERROR INFORMATION' [email protected]
Copyright © Linux教程網 All Rights Reserved