create table heartbeat
(
master_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
slave_time TIMESTAMP NOT NULL
) ENGINE=MyISAM;
insert into heartbeat(slave_time) values(SYSDATE());
Insert Into heartbeat(slave_time)
Values( from_unixtime((unix_timestamp(sysdate()) + time_to_sec('HH:MM:SS'))) ;
Here, HH:MM:SS is slave's known time of lagging behind master.
Select master_time, timediff(slave_time, master_time)
From heartbeat
Where DATE(master_time) = DATE(NOW())
Order By master_time;
select timediff(NOW(), max(master_time)) from heartbeat;
##insert-master.bat
:begin
@echo off
set StartTime=%TIME%
set StartMin=%StartTime:~3,2%
:start
set EndTime=%TIME%
set EndMin=%EndTime:~3,2%
set /a Hour_Diff=StartMin - EndMin >nul
if %Hour_Diff%==0 (
goto start )
echo Beating...
mysql -uroot -pXXXX -P3307 -e "use master;insert into heartbeat(slave_time) values(SYSDATE());"
goto begin
pause
##monitor-slave.bat
@echo off
echo Observing slave delay - per minute basis
echo SBM - Slave Behind Master / Last Query Executed.
echo ETD - Execution Time Difference
echo ----------------------------------
echo SBM - ETD - Slave Status
echo ----------------------------------
:begin
set StartTime=%TIME%
set StartMin=%StartTime:~3,2%
:start
set EndTime=%TIME%
set EndMin=%EndTime:~3,2%
set /a Hour_Diff=StartMin - EndMin >nul
if %Hour_Diff%==0 (
goto start )
FOR /F "skip=1 tokens=*" %%I IN ('mysql -uroot -pXXXX -e "use master;select TIME_TO_SEC(timediff(NOW(), max(master_time)) ) last_query_time, second(timediff(max(slave_time), max(master_time))) master_slave_difference from heartbeat where DATE(master_time) = DATE(NOW()) order by master_time desc;"') DO set var=%%I%
@echo %var%
FOR /F "tokens=11 delims='| '" %%G IN ('mysql -uroot -pXXXX -e "show slave status;"') do @echo %%G
FOR /F "tokens=12 delims='| '" %%G IN ('mysql -uroot -pXXXX -e "show slave status;"') do @echo %%G
goto begin
pause
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)