在SNMP陷阱中发送表索引

时间:2016-07-09 04:32:34

标签: java snmp snmp4j

我已经使用snmp4j实现了一个SNMP代理,并且在如何正确地向SNMP管理器报告SNMPv3陷阱/通知方面遇到了一些麻烦。设置实现以管理由instanceId索引的值表。当您查询代理时,您会收到各种字段的OID,后缀为" .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 |
-----------------------------------------

这很有效。但是,我需要能够以类似的方式发送陷阱,以便使用陷阱发送表的索引。这样,当链接状态发生变化时,可以清除从SNMP查询触发的警报。我尝试简单地将instanceId添加为varbind,如下面的代码块所示,但实体始终报告为通用"链接"。有没有人遇到这个可以帮我解决这个问题?任何事情都非常受欢迎。

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);
        }
    }
}

编辑:

注意:链接是动态可配置的,因此我不能简单地使用单独的OID定义每个链接;我在MIB中定义了基本OID,需要动态添加索引。

1 个答案:

答案 0 :(得分:0)

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

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

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

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

管理员将实体报告为 Link1 Link2 等,与SNMP表一致。