2014-03-25 13 views
0

我想查找连接全部时间的服务器的负载平均值,我使用了下面的代码,但是显示正常运行时间时出现错误。请有人帮我解决这个问题...在php中使用ssh查找服务器的负载平均值

下面是代码

<?php 
$ip = '192.168.71.56'; 
$user = 'viju'; 
$pass = 'viju'; 
$connection = ssh2_connect($ip); 
if($connection) 
{ 
echo "connection successful"; 
} 
else 
{ 
echo "connection failed"; 
} 

if(!(ssh2_auth_password($connection,$user,$pass))) 
{ 
    echo "Authentication Failed"; 
} 
else 
{ 
echo "Authentication Successful"; 
$shell = ssh2_exec($connection,"uptime"); 
if($shell) 
{ 
    $loadAvgString=explode('average:',$shell); //error occurs here 
    print_r($loadAvgString); 
    $loadAvgInAnArray=explode(',',$loadAvgString); 
    print_r($loadAvgInAnArray); 
} 
else 
{ 
    echo "No data fetched"; 
} 
} 
?> 

不然我和警告以下输出

连接成功验证成功的警告:爆炸()预计参数2是字符串,资源在/var/www/extra/dis.php 26行Array([0] =>)

+0

'system'和'server'有什么区别? –

回答

0

您将需要结合您的PHP知识与Linux-fu使这发生。 获得平均负载的一种方法是在命令行上使用'uptime'命令。 Here is a nice example of uptime's output

通过SSH远程系统上使用它..您可以基于它关闭this example here

然后你只需要解析的正常运行时间的输出的输出。 这里是一个例子:

$connection = ssh2_connect('shell.example.com', 22); 
ssh2_auth_password($connection, 'username', 'password'); 

$response= ssh2_exec($connection, 'uptime'); 
//if you are new, regex is hard... so you can use explode to parse the string 
$loadAvgString=explode('average:',$response); 
//this array will contain your load avg amounts. you still need to trim() the whitespace 
$loadAvgInAnArray=explode(',',$loadAvgString);