在 SNMP 陷阱中发送 table 索引

Sending table index in SNMP trap

我已经使用 snmp4j 实现了一个 SNMP 代理,但我 运行 在如何正确地向 SNMP 管理器报告 SNMPv3 traps/notifications 方面遇到了一些麻烦。该实现设置为管理由 instanceId 索引的 table 个值。当您查询代理时,您会根据与 OID 值关联的特定 link 实例收到后缀为“.1”、“.2”、“.3”等的各个字段的 OID。所以查询结果为:

-----------------------------------------
| Entity  | OID          | Value        |
-----------------------------------------
| Link1   | linkAlias.1  | Link 1       |
| Link2   | linkAlias.2  | Link 2       |
| Link1   | linkState.1  | 1            |
| Link2   | linkState.2  | 3            |
| Link1   | linkText.1   | UP           |
| Link2   | linkText.2   | INITIALIZING |
-----------------------------------------

效果很好。但是,我需要能够以类似的方式发送陷阱,以便 table 的索引与陷阱一起发送。这样,当 link 状态更改时,可以清除从 SNMP 查询触发的警报 我尝试简单地将 instanceId 添加为 varbind,如下面的代码块所示,但实体始终报告为通用 "Link" .有没有人遇到过这个可以帮助我解决这个问题?不胜感激。

public static void updateLinkStatus(int instanceId, LinkState status) 
{   
    boolean varChanged = false;

    Variable[] currentManagedObject = currentManagedObjects.get(instanceId);
    if(currentManagedObject != null)
    {
        // If we are managing this object
        if(((Integer32)currentManagedObject[1]).toInt() != status.value)
            varChanged = true;

        // Update the managed object Status Integer and Status Text
        currentManagedObject[1] = new Integer32(status.value);
        currentManagedObject[2] = new OctetString(status.getLinkStateText());
    }
    else 
    {
        varChanged = true; // No previous record to check against
    }

    // Send trap now if not equal to previous value
    if(varChanged)
    {
        OID trapOid = null;
        int linkState = LinkState.UNKNOWN.value; // Will be overridden
        String linkStateText = null;

        if(status == LinkState.DOWN)
        {
            trapOid = oidLinkDown;
            linkState = LinkState.DOWN.value;
            linkStateText = LnkState.DOWN.getLinkStateText();
        }
        else if(status == LinkState.MISCONFIGURED)
        {
            trapOid = oidLinkMisconfigured;
            linkState = LinkState.MISCONFIGURED.value;
            linkStateText = LinkState.MISCONFIGURED.getLinkStateText();
        }
        else if(status == LinkState.UP)
        {
            trapOid = oidLinkUp;
            linkState = LinkState.UP.value;
            linkStateText = LinkState.UP.getLinkStateText();
        }
        else if(status == LinkState.INITIALIZING)
        {
            trapOid = oidLinkInitializing;
            linkState = LinkState.INITIALIZING.value;
            linkStateText = LinkState.INITIALIZING.getLinkStateText();
        }
        else
        {
            // Theoretically, should never change to LinkState.UNKNOWN - no trap available for it
            linkState = LinkState.UNKNOWN.value;
            linkStateText = LinkState.UNKNOWN.getLinkStateText();
        }

        // Create variable bindings for V3 trap
        if(trapOid != null)
        {
            List<VariableBinding> variableBindings = new ArrayList<VariableBinding>();
            variableBindings.add(new VariableBinding(oidLinkState, new Integer32(linkState)));
            variableBindings.add(new VariableBinding(oidLinkText, new OctetString(linkStateText)));
            variableBindings.add(new VariableBinding(oidLinkInstanceID, new Integer32(instanceId)));
            //I've tried the below varbind too and it also doesn't work
            //variableBindings.add(new VariableBinding(new OID(oidLinkInstanceID.toIntArray(), instanceId)));
            agent.sendTrap_Version3(trapOid, variableBindings);
        }
    }
}

编辑:

注意:link 是动态可配置的,所以我不能简单地用单独的 OID 定义每个 link;我在MIB中定义了基础OID,需要动态添加索引。

我找到了解决方案。我试图简单地将实例索引添加到 OID 并发送一个 NULLOBJ 作为 varbind 中的值,如下所示:

variableBindings.add(new VariableBinding(new OID(oidLinkInstanceID.toIntArray(), instanceId)));

但是经理拒绝了该消息。所以我将实例索引添加到 OID 以及 varbind 值,如下所示:

variableBindings.add(new VariableBinding(new OID(oidLinkInstanceID.toIntArray(), instanceId), new Integer32(instanceId)));

并且管理器将实体报告为Link1Link2等,与SNMPtable一致。