freedsx/snmp

纯PHP SNMP库

0.5.0 2022-01-29 15:15 UTC

This package is auto-updated.

Last update: 2024-09-22 03:37:37 UTC


README

FreeDSx SNMP是一个纯PHP SNMP库。它不依赖于PHP核心SNMP扩展。它实现了RFC 3412 / RFC 3416 / RFC 3414中描述的SNMP客户端功能。它还包括了SHA2身份验证(RFC 7860)和强加密机制(3DES / AES-192-256)等RFC中描述的功能。一些主要特性包括

  • 支持SNMP版本1、2和3。
  • 支持所有身份验证机制(md5、sha1、sha224、sha256、sha384、sha512)。
  • 支持所有隐私加密机制(des、3des、aes128、aes192、aes256)。
  • 支持所有客户端请求类型(Get、GetNext、GetBulk、Set、Inform、TrapV1、TrapV2)。
  • 支持发送SNMPv1和SNMPv2陷阱(包括inform请求)。
  • 陷阱接收服务器,用于接收和处理传入的陷阱。

需要OpenSSL扩展来支持隐私/加密。需要GMP扩展来支持64位计数器(BigCounter)。

文档

入门

通过composer安装

composer require freedsx/snmp

使用SnmpClient类和辅助类

use FreeDSx\Snmp\SnmpClient;

$snmp = new SnmpClient([
    'host' => 'servername',
    'version' => 2,
    'community' => 'secret',
]);

# Get a specific OID value as a string...
echo $snmp->getValue('1.3.6.1.2.1').PHP_EOL;

# Get a specific OID as an object...
$oid = $snmp->getOid('1.3.6.1.2.1');
var_dump($oid);

echo sprintf("%s == %s", $oid->getOid(), (string) $oid->getValue()).PHP_EOL;

# Get multiple OIDs and iterate through them as needed...
$oids = $snmp->get('1.3.6.1.2.1.1.1', '1.3.6.1.2.1.1.3', '1.3.6.1.2.1.1.5');
 
foreach($oids as $oid) {
    echo sprintf("%s == %s", $oid->getOid(), (string) $oid->getValue()).PHP_EOL;
}

# Using the SnmpClient, get the helper class for an SNMP walk...
$walk = $snmp->walk();

# Keep the walk going until there are no more OIDs left
while($walk->hasOids()) {
    try {
        # Get the next OID in the walk
        $oid = $walk->next();
        echo sprintf("%s = %s", $oid->getOid(), $oid->getValue()).PHP_EOL;
    } catch (\Exception $e) {
        # If we had an issue, display it here (network timeout, etc)
        echo "Unable to retrieve OID. ".$e->getMessage().PHP_EOL;
    }
}

echo sprintf("Walked a total of %s OIDs.", $walk->count()).PHP_EOL; 

有关完整的配置参考,请参阅配置文档。在通用用法文档中还有SNMP v3的示例,包括NoAuthNoPrivAuthNoPrivAuthPriv