2024 年 PHP Conference Japan

SNMP 類別

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

簡介

表示 SNMP 工作階段。

類別概要

class SNMP {
/* 常數 */
public const int VERSION_1;
public const int VERSION_2c;
public const int VERSION_2C;
public const int VERSION_3;
公開 常數 整數 ERRNO_NOERROR;
公開 常數 整數 ERRNO_ANY;
公開 常數 整數 ERRNO_GENERIC;
公開 常數 整數 ERRNO_TIMEOUT;
公開 常數 整數 ERRNO_ERROR_IN_REPLY;
公開 常數 整數 ERRNO_OID_NOT_INCREASING;
公開 常數 整數 ERRNO_OID_PARSING_ERROR;
公開 常數 整數 ERRNO_MULTIPLE_SET_QUERIES;
/* 屬性 */
公開 只讀 陣列 $info;
公開 可為空值 整數 $max_oids;
公開 整數 $valueretrieval;
公開 布林值 $quick_print;
公開 布林值 $enum_print;
公開 整數 $oid_output_format;
公開 布林值 $oid_increasing_check;
公開 整數 $exceptions_enabled;
/* 方法 */
公開 __construct(
    整數 $version,
    字串 $hostname,
    字串(string$community,
    整數(int$timeout = -1,
    整數(int$retries = -1
)
公開(publicclose(): 布林值(bool
公開(publicget(陣列(array)|字串(string $objectId, 布林值(bool$preserveKeys = false): 混合型別(mixed
公開(publicgetErrno(): 整數(int
公開(publicgetError(): 字串(string
公開(publicgetnext(陣列(array)|字串(string $objectId): 混合型別(mixed
公開(publicset(陣列(array)|字串(string $objectId, 陣列(array)|字串(string $type, 陣列(array)|字串(string $value): 布林值(bool
公開(publicsetSecurity(
    字串(string$securityLevel,
    字串(string$authProtocol = "",
    字串(string$authPassphrase = "",
    字串(string$privacyProtocol = "",
    字串(string$privacyPassphrase = "",
    字串(string$contextName = "",
    字串(string$contextEngineId = ""
): 布林值(bool
公開(publicwalk(
    陣列(array)|字串(string $objectId,
    布林值(bool$suffixAsKey = false,
    整數(int$maxRepetitions = -1,
    整數(int$nonRepeaters = -1
): 陣列(array)|false
}

屬性

max_oids

每個 GET/SET/GETBULK 請求的最大 OID 數量

valueretrieval

控制 SNMP 值的返回方式

SNMP_VALUE_LIBRARY返回值將與 Net-SNMP 函式庫返回的值相同。
SNMP_VALUE_PLAIN返回值將是不帶 SNMP 類型資訊的純值。
SNMP_VALUE_OBJECT返回值將是具有「value」和「type」屬性的物件,其中後者是 SNMP_OCTET_STR、SNMP_COUNTER 等常數之一。「value」的返回方式取決於 SNMP_VALUE_LIBRARYSNMP_VALUE_PLAIN 的設定。
quick_print

NET-SNMP 函式庫中 quick_print 的值

設定 NET-SNMP 函式庫中 quick_print 的值。設定此項 (1) 時,SNMP 函式庫將返回「快速列印」的值。這表示只會列印值。未啟用 quick_print 時(預設),NET-SNMP 函式庫會列印額外資訊,包括值的類型(例如 IpAddress 或 OID)。此外,如果未啟用 quick_print,函式庫會為所有三個字元或更少的字串列印額外的十六進位值。

enum_print

控制列印 enum 值的方式

此參數切換 walk/get 等是否應自動在 MIB 中查找 enum 值,並將它們與其人類可讀的字串一起返回。

oid_output_format

控制 OID 輸出格式

不同 oid_output_format 值的 OID .1.3.6.1.2.1.1.3.0 表示法
SNMP_OID_OUTPUT_FULL.iso.org.dod.internet.mgmt.mib-2.system.sysUpTime.sysUpTimeInstance
SNMP_OID_OUTPUT_NUMERIC.1.3.6.1.2.1.1.3.0
SNMP_OID_OUTPUT_MODULEDISMAN-EVENT-MIB::sysUpTimeInstance
SNMP_OID_OUTPUT_SUFFIXsysUpTimeInstance
SNMP_OID_OUTPUT_UCDsystem.sysUpTime.sysUpTimeInstance
SNMP_OID_OUTPUT_NONE未定義
oid_increasing_check

控制是否停用在走訪 OID 樹時檢查 OID 是否遞增

已知某些 SNMP 代理會以非循序方式返回 OID,但仍可完成走訪。其他代理返回的 OID 不按順序,可能導致 SNMP::walk() 無限循環,直到達到記憶體限制。PHP SNMP 函式庫預設會執行 OID 遞增檢查,並在偵測到可能的循環時停止走訪 OID 樹,同時發出關於遇到非遞增 OID 的警告。將 oid_increasing_check 設定為 false 可停用此檢查。

exceptions_enabled

控制哪些錯誤會引發 SNMPException 而不是警告。使用以位元 OR 運算的 SNMP::ERRNO_* 常數。預設情況下,所有 SNMP 例外狀況皆停用。

info

唯讀屬性,包含遠端代理設定:主機名稱、連接埠、預設逾時、預設重試次數

預先定義的常數

SNMP 錯誤類型

SNMP::ERRNO_NOERROR

沒有發生 SNMP 特定的錯誤。

SNMP::ERRNO_GENERIC

發生了一般 SNMP 錯誤。

SNMP::ERRNO_TIMEOUT

SNMP 代理的請求逾時。

SNMP::ERRNO_ERROR_IN_REPLY

SNMP 代理在回覆中返回錯誤。

SNMP::ERRNO_OID_NOT_INCREASING

SNMP 代理在執行 (BULK)WALK 命令時遇到 OID 循環,回報了非遞增的 OID。這表示遠端 SNMP 代理有問題。

SNMP::ERRNO_OID_PARSING_ERROR

程式庫在解析 OID(及/或 SET 命令的類型)時失敗。尚未執行任何查詢。

SNMP::ERRNO_MULTIPLE_SET_QUERIES

程式庫將針對要求的 SET 操作使用多個查詢。這表示操作將以非交易方式執行,如果遇到類型或值錯誤,第二個或後續的區塊可能會失敗。

SNMP::ERRNO_ANY

所有 SNMP::ERRNO_* 程式碼的位元或運算結果。

目錄

新增註解

使用者貢獻的註解 2 則註解

0
swannie at swannie dot net
3 年前
希望這能幫助到其他人,因為這讓我困擾了整整兩個小時。看起來 valueretrieval 和 enum_print 以一種我沒想到的方式一起工作,在重新閱讀之後,這可能是設計使然,但我不確定。如果您無法列印 enums,這可能是原因。

<?php
$snmp
= new SNMP(SNMP::VERSION_2C,'192.168.1.9','test');
$snmp->oid_output_format = SNMP_OID_OUTPUT_SUFFIX;
$snmp->valueretrieval = SNMP_VALUE_PLAIN;
$snmp->enum_print = 0;
echo
$snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
$snmp->enum_print = 1;
echo
$snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
$snmp->quick_print = 1;
echo
$snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
$snmp->valueretrieval = SNMP_VALUE_LIBRARY;
echo
$snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
$snmp->enum_print = 0;
echo
$snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
?>

輸出
1
1
1
1
-1
madjev1990 at gmail dot com
8 年前
我的畢業論文一部分是創建一個透過 SNMP 控制設備的網頁介面。所以我基於 SNMP 類別建立了我自己的抽象層。

<?php

/**
* Snmp library which add one level of abstraction to snmp native library.
* It adds functionality to work with module PicoIP.With this library you can:
* 1.Activate/deactive defined pin;
* 2.Get status of all pins.
*
* When make an instance you should pass to the constructor key word which will
* make the library create an object with necessary properetis and access permissions.
*
* Private properties set some of configurations:
* Host is IP address of the divece which we will command.
* Two passwords are set for reading and writing.
* Version of snmp protocol that we will use is version 1.
*
* @author Radoslav Madjev
* @year 2016
* @version 1.0 beta
*
*
*/
class snmp_lib {

private
$snmpInstance;
private
$VERSION = SNMP::VERSION_1;
private
$HOST = '192.168.0.150';
private
$passwordRead = '000000000000';
private
$passwordWrite = 'private';
private
$releys = array(1 => '1.3.6.1.4.1.19865.1.2.1.1.0',
2 => '1.3.6.1.4.1.19865.1.2.1.2.0');
private
$allPorts = array('3' => '1.3.6.1.4.1.19865.1.2.1.33.0',
'5' => '1.3.6.1.4.1.19865.1.2.2.33.0');

/**
* Create instance of SNMP native class, based on actions that we will
* perform.
*
* @param string $action
*/
public function __construct($action) {
if (
in_array($action, array('read', 'write'))) {
if (
strcmp($action, 'read') === 0) {
$this->_read();
} else {
$this->_write();
}
}
}

/**
* Create instance with reading permissions.
*/
private function _read() {
$this->snmpInstance = new SNMP($this->VERSION, $this->HOST, $this->passwordRead);
}

/**
* Create instance with writing permissions.
*/
private function _write() {
$this->snmpInstance = new SNMP($this->VERSION, $this->HOST, $this->passwordWrite);
}

/**
* Close snmp session.
*
* @return boolean
*/
public function closeSession() {
return
$this->snmpInstance->close();
}

/**
* Set integer 1 as value of defined pin.
*/
public function activate($relay) {
$this->snmpInstance->set($this->releys[$relay], 'i', '1');
}

/**
* Set integer 0 as value of defined pin.
*/
public function deactivate($relay) {
$this->snmpInstance->set($this->releys[$relay], 'i', '0');
}

/**
* Get pin status of all ports of the module.
*
* @return array
*/
public function getAllPortsStatus() {
$allPins = array();
foreach (
$this->allPorts as $number => $port) {
//get active pins as 8-bit integer of defined port
$getbits = $this->snmpInstance->get($port);
$bits = str_replace('INTEGER: ', '', $getbits);
//get pins status
$pinsStatus = $this->_getActivePins($bits);
$allPins[$number] = $pinsStatus;
}

return
$allPins;
}

/**
* Make bitwise operation which will determine,
* which are active pins.
*
* @param int $bits
* @return array
*/
private function _getActivePins($bits) {

$bitMapping = array(
1 => 1,
2 => 2,
3 => 4,
4 => 8,
5 => 16,
6 => 32,
7 => 64,
8 => 128
);

$pinsStatus = array();

foreach (
$bitMapping as $int => $bit) {
if ((
$bits & $bit) == $bit) {
$pinsStatus[$int] = true;
continue;
}
$pinsStatus[$int] = false;
}

return
$pinsStatus;
}

}

?>

我有一個模組可以接收 SNMP 請求並發送命令到繼電器。以下是一些使用此函式庫的範例程式碼。
開啟程式碼
<?php
require_once 'snmp_lib.php';

$snmp = new snmp_lib('write');
$snmp->activate($getRelayNumber);
$snmp->closeSession();
?>

關閉程式碼
<?php
require_once 'snmp_lib.php';

$snmp = new snmp_lib('write');
$snmp->deactivate($getRelayNumber);
$snmp->closeSession();
?>

取得所有埠狀態的腳本
<?php
require_once 'snmp_lib.php';

$snmp = new snmp_lib('read');
$getActive = $snmp->getAllPortsStatus();
?>
To Top