获取slave的性能
array Core_Controller_RunTime::mysql_get_slave_status( array $mysql_servers )
参数列表
参数 类型 描述 默认值 $mysql_servers
array
$mysql_servers
array
protected function mysql_get_slave_status($mysql_servers)
{
// 记录所有服务器的相应时间
$link_status = array();
// 循环所有的MySQL数据库
foreach ($mysql_servers as $host => $mysql_config)
{
$link_status[$host]['status'] = false;
try
{
$time = microtime(1);
$link = mysql_connect( $host, $mysql_config['username'], $mysql_config['password'] , true );
if (!$link)
{
// 输出错误信息
$this->output('mysql connect error.hoss='.$host.'.error msg='.mysql_error());
continue;
}
$result = mysql_query('show slave status',$link);
$row = mysql_fetch_array($result);
// 进程数,越小越好,至少会是1
$result = mysql_query('show global status like \'Threads_running\'',$link);
$threads_running = mysql_fetch_array($result);
$threads_running = $threads_running['Threads_running'];
// 已连接数,越小越好
$result = mysql_query('show global status like \'Threads_connected\'',$link);
$threads_connected = mysql_fetch_array($result);
$threads_connected = $threads_connected['Threads_connected'];
// 关闭数据库连接
mysql_close($link);
// 获取执行时间,可反应出连接数据
$time = (microtime(1)-$time);
$link_status[$host]['status'] = true;
// 同步状态
if ($row['Slave_IO_Running']=='No')
{
// 不同步
$link_status[$host]['status'] = false;
$this->output($host.' slave io not running.');
continue;
}
if ($row['Slave_SQL_Running']=='No')
{
// 没运行
$link_status[$host]['status'] = false;
$this->output($host.' slave sql not running.');
continue;
}
$link_status[$host]['delay'] = (int)$row['Seconds_Behind_Master'];
$this->output($host.' delay '.(int)$row['Seconds_Behind_Master'].'s.');
$link_status[$host]['running'] = (int)$threads_running;
$link_status[$host]['connected'] = (int)$threads_connected;
$link_status[$host]['time'] = $time;
$this->output($host.' check successfully. total time '.$time);
// 清除变量
unset($link,$result,$time,$threads_running,$threads_connected);
}
catch (Exception $e)
{
$this->output( 'mysql connect error.hoss='.$host.'.error msg='.$e->getMessage() );
}
}
$this->output('status:');
print_r($link_status);
return $link_status;
}