| 1 | package App::MadEye::Plugin::Agent::MySQLSlave; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use App::MadEye::Plugin::Agent::Base; | ||
| 5 | |||
| 6 | use DBI; | ||
| 7 | |||
| 8 | sub is_dead { | ||
| 9 | my ($self, $dsn) = @_; | ||
| 10 | |||
| 11 | App::MadEye->context->log('debug' => "watching $dsn"); | ||
| 12 | |||
| 13 | my $user = $self->config->{config}->{user} or die "missing user"; | ||
| 14 | my $password = $self->config->{config}->{password}; | ||
| 15 | my $threshold = $self->config->{config}->{threshold} or die "missing threshold"; | ||
| 16 | |||
| 17 | my $dbh; | ||
| 18 | eval { | ||
| 19 | $dbh = DBI->connect( | ||
| 20 | $dsn, | ||
| 21 | $user, | ||
| 22 | $password, | ||
| 23 | { RaiseError => 1, AutoCommit => 1 } | ||
| 24 | ) or return 'cannot connect'; | ||
| 25 | }; | ||
| 26 | if ($@) { | ||
| 27 | return $@; | ||
| 28 | } | ||
| 29 | |||
| 30 | my $sth = $dbh->prepare(q{SHOW SLAVE STATUS;}); | ||
| 31 | $sth->execute() or return 'DBI error: ' . $dbh->errstr; | ||
| 32 | |||
| 33 | if (my $row = $sth->fetchrow_hashref) { | ||
| 34 | # see. http://dev.mysql.com/doc/refman/4.1/ja/show-slave-status.html | ||
| 35 | |||
| 36 | if (my $le = $row->{'Last_Error'}) { | ||
| 37 | return "replication error : $le\n"; | ||
| 38 | } | ||
| 39 | |||
| 40 | if ( $row->{'Slave_IO_Running'} ne 'Yes' ) { | ||
| 41 | return "Slave IO Not Running\n"; | ||
| 42 | } | ||
| 43 | |||
| 44 | if ( $row->{'Slave_SQL_Running'} ne 'Yes' ) { | ||
| 45 | return "Slave SQL Not Running\n"; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $rmlp = $row->{'Read_Master_Log_Pos'}; | ||
| 49 | my $emlp = $row->{'Exec_Master_Log_Pos'}; | ||
| 50 | if ( abs($rmlp - $emlp) > $threshold ) { | ||
| 51 | return "considerable delay : $rmlp $emlp\n"; | ||
| 52 | } | ||
| 53 | |||
| 54 | return; # alive! | ||
| 55 | } else { | ||
| 56 | return "this is not a slave!"; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | __END__ |