2014-03-25 20 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);