Skip to content

Instantly share code, notes, and snippets.

@ckxng
Created March 5, 2022 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckxng/1cf5885c67aa54e48b2ce0af75d1f114 to your computer and use it in GitHub Desktop.
Save ckxng/1cf5885c67aa54e48b2ce0af75d1f114 to your computer and use it in GitHub Desktop.
Check MySQL Replication Status
#!/usr/bin/perl
# $Id$
use strict;
use warnings;
my $VERSION = 0.006;
my $DEBUG = 1;
sub handle_wrn;
sub handle_err;
my $sql = "show slave status \\G;";
my $cmd = "mysql -e '$sql'";
my $status = `$cmd 2>&1`;
my $status_ret = ${^CHILD_ERROR_NATIVE};
handle_err "sql error" unless $status_ret == 0;
$status =~ /Last_Errno: (.+?)$/;
handle_err "replication error: $1" unless $1 == "0";
$status =~ /seconds_behind_master: (.+?)$/;
handle_err "replication slow: $1 seconds" if $1 > 1200;
$status =~ /seconds_behind_master: (.+?)$/;
handle_wrn "replication slow: $1 seconds" if $1 > 60;
print "OK\n";
exit 0;
sub handle_wrn {
my $msg = shift @_;
print "WARNING - $msg\n";
exit 1;
}
sub handle_err {
my $msg = shift @_;
print "ERROR - $msg\n";
exit 2;
}
=head1 NAME
checkmysql.pl
=head1 DESCRIPTION
Check MySQL slave status. Should be run on slave host as root.
Icinga compatible.
returns 0 - replication ok
returns 1 - replication > 60 sec behind
returns 2 - replication >1200 sec behind or error
=head1 VERSION
0.006
=head1 AUTHOR
Cameron King <http://cameronking.me>
=head1 COPYRIGHT
Copyright 2012 Cameron C. King. All rights reserved.
=head1 LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY CAMERON C. KING ''AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL CAMERON C. KING OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
=cut
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment