[MySQL]: Replication monitoring alert script

[ root @ dbversity : ~ ] cat Slave_status.sh
#!/bin/bash
LOGFILE=slavestatus.log
mkdir -p /binlogs/cronjobs/Error_Log
yum -y -q mail

## ======================================================= **********SLAVE STATUS************* =======================================================================
array=(10.x.xx.xx)

for i in “${array[@]}”
do

#mysql -h$i -umonitor -p’password’ -e”show slave status\G” > /binlogs/cronjobs/Error_Log/$i_Slave_Status.txt 2>/binlogs/cronjobs/Error_Log/$i_Slave_Status.txt
mysql -h$i -e”show slave status\G” > /binlogs/cronjobs/Error_Log/$i_Slave_Status.txt 2>/binlogs/cronjobs/Error_Log/$i_Slave_Status.txt

Slave_IO_Running=`cat /binlogs/cronjobs/Error_Log/$i_Slave_Status.txt |grep Slave_IO_Running: | awk ‘{print $2}’`
Slave_SQL_Running=`cat /binlogs/cronjobs/Error_Log/$i_Slave_Status.txt |grep Slave_SQL_Running: | awk ‘{print $2}’`
Seconds_Behind_Master=`cat /binlogs/cronjobs/Error_Log/$i_Slave_Status.txt |grep Seconds_Behind_Master: | awk ‘{print $2}’`

if [ $Slave_IO_Running = No ]
then
echo “Alert — $i IO thread is not running” > $LOGFILE
echo “Please see the below IO error” >> $LOGFILE
echo `cat /binlogs/cronjobs/Error_Log/$i_Slave_Status.txt |grep Last_IO_Error:` >> $LOGFILE
mail -s “Alert — $i IO thread is down” dl@dbversity.com < $LOGFILE
fi

if [ $Slave_SQL_Running = No ]
then
echo “Alert — $i SQL thread is not running” >> $LOGFILE
echo “Please see the below IO error” >> $LOGFILE
echo `cat /binlogs/cronjobs/Error_Log/$i_Slave_Status.txt |grep Last_SQL_Error:` >> $LOGFILE
mail -s “Alert — $i SQL Thread is down” dl@dbversity.com < $LOGFILE
fi

if [ $Seconds_Behind_Master -gt 500 ]
then
## echo $Slave_IO_Running $Seconds_Behind_Master
echo “Alert — $i is $Seconds_Behind_Master seconds behind the master” > $LOGFILE
mail -s “Alert — $i is too behind the master” dl@dbversity.com < $LOGFILE
fi
done

[ root @ dbversity : ~ ]
[ root @ dbversity : ~ ]
[ root @ dbversity : ~ ]
[ root @ dbversity : ~ ]
[ root @ dbversity : ~ ] mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 10.42.105.100
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 120
Relay_Log_File: dbversity-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 151
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: The slave IO thread stops because the master has @@GLOBAL.GTID_MODE OFF and this server has @@GLOBAL.GTID_MODE ON
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 150805 08:24:29
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: cc3a3014-1e36-11e5-928c-005056008cb1:1-1418
Auto_Position: 0
1 row in set (0.00 sec)

mysql>
mysql>
mysql> ^DBye
[ root @ dbversity : ~ ]
[ root @ dbversity : ~ ]
[ root @ dbversity : ~ ]
[ root @ dbversity : ~ ] sh -x Slave_status.sh
+ LOGFILE=slavestatus.log
+ mkdir -p /binlogs/cronjobs/Error_Log
+ yum -y -q mail
No such command: mail. Please use /usr/bin/yum –help
+ array=(10.116.34.35)
+ for i in ‘”${array[@]}”‘
+ mysql -h10.116.34.35 ‘-eshow slave status\G’
++ cat /binlogs/cronjobs/Error_Log/.txt
++ grep Slave_IO_Running:
++ awk ‘{print $2}’
+ Slave_IO_Running=No
++ cat /binlogs/cronjobs/Error_Log/.txt
++ grep Slave_SQL_Running:
++ awk ‘{print $2}’
+ Slave_SQL_Running=Yes
++ cat /binlogs/cronjobs/Error_Log/.txt
++ awk ‘{print $2}’
++ grep Seconds_Behind_Master:
+ Seconds_Behind_Master=NULL
+ ‘[‘ No = No ‘]’
+ echo ‘Alert — 10.116.34.35 IO thread is not running’
+ echo ‘Please see the below IO error’
++ cat /binlogs/cronjobs/Error_Log/.txt
++ grep Last_IO_Error:
+ echo Last_IO_Error: The slave IO thread stops because the master has @@GLOBAL.GTID_MODE OFF and this server has @@GLOBAL.GTID_MODE ON
+ mail -s ‘Alert — 10.116.34.35 IO thread is down’ dl@dbversity.com
+ ‘[‘ Yes = No ‘]’
+ ‘[‘ NULL -gt 500 ‘]’
Slave_status.sh: line 38: [: NULL: integer expression expected
[ root @ dbversity : ~ ]

  • Ask Question