我一直在研究自定义 SNMP Mib,但在尝试让代理返回正确数据时遇到了困难。
MIB(通过运行验证smilint -l 6
):
IDB-MIB DEFINITIONS ::= BEGIN
IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, Integer32, enterprises
FROM SNMPv2-SMI
MODULE-COMPLIANCE, OBJECT-GROUP FROM SNMPv2-CONF;
idb MODULE-IDENTITY
LAST-UPDATED "201307300000Z" -- Midnight 30 July 2013
ORGANIZATION "*********"
CONTACT-INFO "email: *******"
DESCRIPTION "description"
REVISION "201307300000Z" -- Midnight 29 July 2013
DESCRIPTION "First Draft"
::= { enterprises 42134 }
iDBCompliance MODULE-COMPLIANCE
STATUS current
DESCRIPTION
"Compliance statement for iDB"
MODULE
GROUP testGroup
DESCRIPTION
"This group is a test group"
::= {idb 1}
test2 OBJECT-TYPE
SYNTAX Integer32
UNITS "tests"
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"A test object"
DEFVAL { 5 }
::= { idb 3 }
testGroup OBJECT-GROUP
OBJECTS {
test2
}
STATUS current
DESCRIPTION "all test objects"
::= { idb 2 }
END
代理文件:
#!/usr/bin/perl
use NetSNMP::OID(':all');
use NetSNMP::agent(':all');
use NetSNMP::ASN(':all');
sub myhandler {
my ($handler, $registration_info, $request_info, $requests) = @_;
print "Handling request\n";
for ($request = $requests; $request; $request = $request->next()) {
#
# Work through the list of varbinds
#
my $oid = $request->getOID();
print "Got request for oid $oi\n";
if ($request_info->getMode() == MODE_GET) {
if ($oid == new NetSNMP::OID($rootOID . ".3")) {
$request->setValue(ASN_INTEGER, 2);
}
}
}
}
{
$subagent = 0;
print "Running new agent\n";
my $rootOID = ".1.3.6.1.4.1.42134";
my $regoid = new NetSNMP::OID($rootOID);
if (!$agent) {
$agent = new NetSNMP::agent('Name'=>'my_agent_name','AgentX' => 1);
$subagent = 1;
print "Starting subagent\n";
}
print "Registering agent\n";
$agent->register("my_agent_name", $regoid, \&myhandler);
print "Agent registered\n";
if ($subagent) {
$SIG{'INT'} = \&shut_it_down;
$SIG{'QUIT'} = \&shut_it_down;
$running = 1;
while ($running) {
$agent->agent_check_and_process(1);
}
$agent->shutdown();
}
}
sub shut_it_down() {
$running = 0;
print "Shutting down agent\n";
}
当我运行代理时,我得到以下信息:
Running new agent
Starting subagent!
Registering agent with oid idb
Agent registered
所以我知道有很多工作。但是,当我运行以下命令时:
snmpget -v 1 -c mycommunity localhost:161 test2.0
我收到此错误消息:
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: IDB-MIB::test2.0
我从 snmptranslate 知道 mib 文件设置正确。我什至查看了 snmpget 的调试(使用 -DALL),以确保正确加载和解析 mib。
所以我的问题是:为什么我的子代理没有通过请求?
更新:
@EhevuTov 告诉我我的 MIB 文件无效,但是 smilint 没有报告任何问题,并且运行snmpget -v 2c -c mycommunity localhost:161 .1.3.6.1.4.1.42134.3.0
确实正确报告了对象的名称 (IDB-MIB::test2.0),但没有找到任何数据为了它。
我得到了IDB-MIB::test2 = No Such Object available on this agent at this OID
,这让我认为我的代理没有正确注册,但它没有抛出任何错误。
更新 2:
我一直在摆弄代理代码。根据 CPAN 文档 ( http://metacpan.org/pod/NetSNMP::agent ),如果成功,函数调用似乎$agent->register
应该返回。0
所以我检查了返回码并得到了这个:
Agent registered. Result: NetSNMP::agent::netsnmp_handler_registration=SCALAR(0x201e688)
Data::Dumper
使用结果打印出来:
$VAR1 = bless( do{\(my $o = 34434624)}, 'NetSNMP::agent::netsnmp_handler_registration' );
我隐约明白 bless 是做什么的,但即便如此,我也不知道这个结果应该是什么意思。所以我开始认为代理在某种程度上是错误的。有谁知道如何调试这些代理?有没有什么地方可以查看它是否正确加载到主 snmpd 中?