Skip to content

Instantly share code, notes, and snippets.

@kakuno
Created December 18, 2012 06:52
Show Gist options
  • Save kakuno/4325666 to your computer and use it in GitHub Desktop.
Save kakuno/4325666 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use DBI;
use Getopt::Long;
use constant {
OK => 0,
WARNING => 1,
CRITICAL => 2,
UNKOWN => 3,
};
my %options;
GetOptions(
"host=s" => \$options{host},
"user=s" => \$options{user},
"pass=s" => \$options{pass},
"w=i" => \$options{warn_sec},
"c=i" => \$options{critical_sec},
);
my $dbh = DBI->connect("dbi:mysql:mysql;host=$options{host}", $options{user}, $options{pass}, {
PrintError => 1,
});
unless ($dbh) {
print $DBI::errstr . "\n";
exit CRITICAL;
}
my $status = $dbh->selectrow_hashref("show slave status;");
if ($status->{Last_Error}) {
print sprintf("Last_Error => %s\n", $status->{Last_Error});
exit CRITICAL;
}
my $behind_secs = $status->{Seconds_Behind_Master};
if ($behind_secs >= $options{critical_sec}) {
print sprintf("Seconds_Behind_Master => %d\n", $behind_secs);
exit CRITICAL;
}
if ($behind_secs >= $options{warn_sec}) {
print sprintf("Seconds_Behind_Master => %d\n", $behind_secs);
exit WARNING;
}
print $status->{Slave_IO_State} . "\n";
exit OK;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment