SNMPv3到SNMPv2粘合层

时间:2016-06-03 15:58:28

标签: perl snmp

我被要求在远程Unix平台上安装HP OpenView代理,以捕获客户端应用程序转发的SNMP陷阱。

我发现客户端应用程序只能转发SNMPv3陷阱,我们使用的代理只能接受SNMPv2陷阱。

我发现我可以通过使用Perl模块NetSNMP::TrapReceiver来解决此问题,但我不清楚这是如何设置的,或者如何/如何将v3陷阱转换为v2陷阱。

我的目标是捕获v3陷阱,并希望通过脚本或工具将其转换为v2陷阱并将其发送回另一个端口。

1 个答案:

答案 0 :(得分:0)

我偶然有同样的任务。所以我对NetSNMP :: TrapReceiver的决定就在这里:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

use Net::SNMP;
use SNMP;
use Switch;


my $snmpTrapDestHost = "127.0.0.1";
my $snmpTrapRemotePort = 16162; #other than default trap port or we got infinite trap loop
my $snmpTrapCommunity = "public";
my $snmpTrapVersion = "2c";


my $sess = new SNMP::TrapSession(DestHost => $snmpTrapDestHost, Community => $snmpTrapCommunity, Version => $snmpTrapVersion, RemotePort => $snmpTrapRemotePort);

sub my_receiver {
    print "********** PERL RECEIVED A NOTIFICATION:\n";

    my @bunchOfVarbinds;
    foreach my $x (@{$_[1]})
    {
        printf "'%s' type='%d' value='%s'\n", $x->[0], $x->[2], $x->[1];
        my $lsOid = $x->[0];
        my $liOid = &SNMP::translateObj($lsOid);
        my $lsVal = $x->[1];
        my $lType = $x->[2];
        my $lVal;
# You need some convertion here, cause we got data in "human readable" format
        switch($lType)
        {
            case Net::SNMP::OBJECT_IDENTIFIER
            {
                $lType = "OBJECTID";
                if ($lsVal =~ /OID\:\s+(.*)$/)
                {
                    $lVal = $1
                }
                else
                {
                    $lVal = $lsVal;
                }
                $lVal = &SNMP::translateObj($lVal);
            }
            case Net::SNMP::TIMETICKS
            {
                $lType = "TICKS";
                if($lsVal =~ /Timeticks\:\s+\((\d+)\)\s+.*$/)
                {
                    $lVal = $1;
                }
                else
                {
                    my ($d, $h, $m, $s) = split(/\:/, $lsVal);
                    $lVal = $d * 24;
                    $lVal = ($lVal + $h)*60;
                    $lVal = ($lVal + $m)*60;
                    $lVal = ($lVal + $s)*100;
                }
            }
            case Net::SNMP::OCTET_STRING
            {
                $lType = "OCTETSTR";
                my $isHex;
                ($isHex, $lVal) = split(/STRING\:\s*/, $lsVal);
                if (!defined($lVal))
                {
                    $lVal = $lsVal;
                }
                $lVal =~ s/"//g;

                if (defined($isHex))
                {
                    if($isHex eq "Hex-")
                    {
                        $lVal =~ s/\n//g;
                        $lVal =~ s/ //g;
                    }
                }
            }
            case Net::SNMP::INTEGER
            {
                $lType = "INTEGER";
                $lVal = $lsVal;
            }
            case Net::SNMP::IPADDRESS
            {
# IPADDR
# NETADDR
                $lType = "IPADDR";
                $lVal = $lsVal;
            }
            case Net::SNMP::COUNTER
            {
# COUNTER
                $lType = "COUNTER";
                $lVal = $lsVal;
            }
            case Net::SNMP::COUNTER64
            {
# COUNTER64
                $lType = "COUNTER64";
                $lVal = $lsVal;
            }
            case Net::SNMP::GAUGE
            {
# GAUGE
                $lType = "GAUGE";
                $lVal = $lsVal;
            }
            case Net::SNMP::NULL
            {
# NULL
                $lType = "NULL";
                $lVal = $lsVal;
            }
            else
            {
# UINTEGER
# OPAQUE
                $lVal = $lsVal;
            }
        }        
        push @bunchOfVarbinds, new SNMP::Varbind([$liOid, undef, $lVal, $lType]);

    }

    my $vl = new SNMP::VarList(@bunchOfVarbinds);

    $sess->trap(
                    oid => $initiTrapProxyServiceOid,
                    uptime => $trapUptimeValue,
                    $vl
               );

    return NETSNMPTRAPD_HANDLER_OK;
}

$SNMP::use_numeric = 1;
NetSNMP::TrapReceiver::register("all", \&my_receiver) ||
 warn "failed to register perl trap handler\n";
print STDERR "Loaded perl snmptrapd handler\n";

我是perl的新手,所以任何修复都会受到赞赏。