2013-12-11 45 views
2

我有這段代碼來檢索一些複製機的countervalues。使用snmp測試與網絡設備的連接

foreach($sett as $key => $value){ 
    if (intval(str_replace("INTEGER: ","",snmpget($ip, "public", $base.$value["MIB"])))) { 
     $c = intval(str_replace("INTEGER: ","",snmpget($ip, "public", $base.$value["MIB"]))); 
     $error = false; 
    } 
    else { 
     $c = 0; 
     $error = true; 
    } 
    $counters = array_push_assoc($counters,ucwords($key),array("total" => $c, "code" => $value["code"])); 
} 

一切就像一個魅力,但是這是問題是,當一臺機器宕機連接的代碼不能進行SNMPGET的唯一的事情,整個腳本失敗。

首先我要檢查,如果該設備的連接是活的,然後用SNMPGET

檢索計數器是否有你們可以給我任何解決辦法嗎?

THX

回答

0

如果無法檢索對象的snmpget()函數返回FALSE。

見文檔:http://www.php.net/manual/en/function.snmpget.php

您應該在代碼中做這樣的檢查,例如:

try 
{ 
    foreach($sett as $key => $value){ 

    $sntpReturn = snmpget($ip, "public", $base.$value["MIB"]); 

    if ($sntpReturn === false) 
    { 
     // Do something to handle failed SNTP request. 
     throw new Exception("Failed to execute the SNTP request to the machine."); 
    } 
    else 
    { 

    if (intval(str_replace("INTEGER: ","", $sntpReturn))) { 
     $c = intval(str_replace("INTEGER: ","",snmpget($ip, "public", $base.$value["MIB"]))); 
     $error = false; 
    } 
    else { 
     $c = 0; 
     $error = true; 
    } 
    $counters = array_push_assoc($counters,ucwords($key),array("total" => $c, "code" => $value["code"])); 

    } 
} 
catch (Exception $e) 
{ 
    // Handle the exception, maybe kill the script because it failed? 
}