mysql.pm

来自「mysql+ha. 实现高可用性 http://code.google.com」· PM 代码 · 共 55 行

PM
55
字号
use DBI;sub PerformCheck($$) {    my $timeout = shift;    my $host = shift;        # get connection info    my $peer = $config->{host}->{$host};    if (ref($peer) ne 'HASH') {        return "ERROR: Invalid host!";    }    my $host = $peer->{ip};    my $port = $peer->{port};    my $user = $peer->{user};    my $pass = $peer->{password};        my $res = eval {        local $SIG{ALRM} = sub { die "TIMEOUT"; };        alarm($timeout);                # connect to server        my $dsn = "DBI:mysql:host=$host;port=$port;mysql_connect_timeout=$timeout";        my $dbh = DBI->connect($dsn, $user, $pass, { PrintError => 0 });                unless ($dbh) {            alarm(0);            return "ERROR: Connect error (host = $host:$port, user = $user, pass = '$pass')! " . DBI::errstr;        }            # Check server (simple)        my $sth = $dbh->prepare("SELECT NOW()");        my $res = $sth->execute;        unless($res) {            alarm(0);            return "ERROR: SQL Query Error: " . $dbh->errstr;        }        $sth->finish;        $dbh->disconnect();        $dbh = undef;        alarm(0);        return 0;    };        alarm(0);    return $res if ($res);    return 'ERROR: Timeout' if ($@ =~ /^TIMEOUT/);    return "UNKNOWN: Error occurred: $@" if $@;    return "OK";}1;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?