#!/usr/bin/env perl

# Use mandatory external modules
use strict;
use Cwd;
use File::Basename;
use Data::Dumper;
use POSIX;
use Config;
use Time::HiRes;
use DBI;

# Determine installation dir name
our $SELF_DIR = dirname(dirname(Cwd::abs_path(__FILE__)));

# Include parts of the system
require $SELF_DIR . '/lib/config.pm';

# Read config file and status
our $config = ReadConfig("mmm_agent.conf");

print TurnOffSlave();

exit(0);

#-----------------------------------------------------------------
sub TurnOffSlave() {
    my $this = $config->{this};
    
    # get self connection info
    my $host = $config->{host}->{$this}->{ip};
    my $port = $config->{host}->{$this}->{port};
    my $user = $config->{host}->{$this}->{user};
    my $pass = $config->{host}->{$this}->{password};
    
    # connect to server
    my $dsn = "DBI:mysql:host=$host;port=$port";
    my $dbh = DBI->connect($dsn, $user, $pass, { PrintError => 0 });
    return "ERROR: Can't connect to MySQL (host = $host:$port, user = $user)!" unless ($dbh);
    
    # Check server (simple)
    my $sth = $dbh->prepare("SLAVE STOP");
    my $res = $sth->execute;
    return "ERROR: SQL Query Error: " . $dbh->errstr unless($res);
    $sth->finish;
    
    $dbh->disconnect();
    $dbh = undef;
    
    return "OK";
}
