#! /bin/bash
#
#
#
#监控mysql主从同步状态
#
#
#
STATE_OK=0
#正常
STATE_WARNING=1
#警告
STATE_CRITICAL=2
#危险
STATE_UNKNOWN=3
#未知
SLAVE_IO_STATUS=0
SLAVE_SQL_STATUS=0
MYSQL=$(
which
mysql)
SLAVE_IO=`$MYSQL -e
"show slave status\G"
|
grep
-i Slave_IO_Running |
awk
-F:
‘{print $2}‘
`
SLAVE_SQL=`$MYSQL -e
"show slave status\G"
|
grep
-i Slave_SQL_Running|
awk
-F:
‘{print $2}‘
`
IO_ERROR=`$MYSQL -e
"show slave status\G"
|
grep
-i Last_IO_Error|
awk
-F:
‘{print $2}‘
`
SQL_ERROR=`$MYSQL -e
"show slave status\G"
|
grep
-i Last_SQL_Error|
awk
-F:
‘{print $2}‘
`
#if [ "`echo ${SLAVE_IO%% .*}`" == "Yes" ];then
# echo "slave_io is OK"
if
[
"`echo ${SLAVE_IO%% .*}`"
==
"No"
];
then
#echo "slave_io is $IO_ERROR"
SLAVE_IO_STATUS=2
fi
#if [ "`echo ${SLAVE_SQL%% .*}`" == "Yes" ];then
# echo "slave_sql is OK"
if
[
"`echo ${SLAVE_SQL%% .*}`"
==
"No"
];
then
#echo "slave_sql is $SQL_ERROR"
SLAVE_SQL_STATUS=2
fi
if
[ $SLAVE_IO_STATUS -
ne
0 -o $SLAVE_SQL_STATUS -
ne
0 ];
then
echo
"error $IO_ERROR $SQL_ERROR"
exit
$STATE_CRITICAL
else
echo
"SLAVE STATUS OK"
exit
$STATE_OK
fi