Search in sources :

Example 16 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.

the class MockProxy method processGetNext.

/**
 * @param request
 * @return
 */
private PDU processGetNext(PDU request) {
    PDU response = request;
    response.setErrorIndex(0);
    response.setErrorStatus(0);
    response.setType(PDU.RESPONSE);
    Vector<? extends VariableBinding> varBinds = response.getVariableBindings();
    for (int i = 0; i < varBinds.size(); i++) {
        VariableBinding varBind = varBinds.get(i);
        VariableBinding nextVarBind = m_agent.getNext(varBind.getOid());
        if (nextVarBind == null) {
            if (response instanceof PDUv1) {
                if (response.getErrorIndex() == 0) {
                    response.setErrorIndex(i + 1);
                    response.setErrorStatus(PDU.noSuchName);
                }
            } else {
                varBind.setVariable(Null.endOfMibView);
            }
        } else {
            response.set(i, nextVarBind);
        }
    }
    return response;
}
Also used : PDU(org.snmp4j.PDU) PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 17 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project LogHub by fbacchella.

the class SnmpTrap method processPdu.

@Override
public void processPdu(CommandResponderEvent trap) {
    try {
        PDU pdu = trap.getPDU();
        Address localaddr = trap.getTransportMapping().getListenAddress();
        Address remoteaddr = trap.getPeerAddress();
        ConnectionContext ctx = ConnectionContext.EMPTY;
        if (localaddr instanceof TransportIpAddress && remoteaddr instanceof TransportIpAddress) {
            InetSocketAddress localinetaddr = getSA((TransportIpAddress) localaddr);
            InetSocketAddress remoteinetaddr = getSA((TransportIpAddress) remoteaddr);
            ctx = new IpConnectionContext(localinetaddr, remoteinetaddr, null);
        }
        Event event = emptyEvent(ctx);
        if (pdu instanceof PDUv1) {
            PDUv1 pduv1 = (PDUv1) pdu;
            String enterprise = (String) convertVar(pduv1.getEnterprise());
            event.put("enterprise", enterprise);
            event.put("agent_addr", pduv1.getAgentAddress().getInetAddress());
            if (pduv1.getGenericTrap() != PDUv1.ENTERPRISE_SPECIFIC) {
                event.put("generic_trap", GENERICTRAP.values()[pduv1.getGenericTrap()].toString());
            } else {
                String resolved = formatter.format(pduv1.getEnterprise(), new Integer32(pduv1.getSpecificTrap()), true);
                event.put("specific_trap", resolved);
            }
            event.put("time_stamp", 1.0 * pduv1.getTimestamp() / 100.0);
        }
        @SuppressWarnings("unchecked") Enumeration<VariableBinding> vbenum = (Enumeration<VariableBinding>) pdu.getVariableBindings().elements();
        for (VariableBinding i : Collections.list(vbenum)) {
            OID vbOID = i.getOid();
            Object value = convertVar(i.getVariable());
            smartPut(event, vbOID, value);
        }
        send(event);
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.catching(e);
    } finally {
        trap.setProcessed(true);
    }
}
Also used : PDU(org.snmp4j.PDU) Enumeration(java.util.Enumeration) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) UdpAddress(org.snmp4j.smi.UdpAddress) InetSocketAddress(java.net.InetSocketAddress) GenericAddress(org.snmp4j.smi.GenericAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) InetSocketAddress(java.net.InetSocketAddress) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) IOException(java.io.IOException) Integer32(org.snmp4j.smi.Integer32) UnsignedInteger32(org.snmp4j.smi.UnsignedInteger32) IpConnectionContext(loghub.IpConnectionContext) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Event(loghub.Event) ConnectionContext(loghub.ConnectionContext) IpConnectionContext(loghub.IpConnectionContext) PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 18 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project LogHub by fbacchella.

the class SNMPTrapGeneratorClient2 method createPdu.

private static PDU createPdu(int snmpVersion) {
    PDU pdu = DefaultPDUFactory.createPDU(snmpVersion);
    if (snmpVersion == SnmpConstants.version1) {
        pdu.setType(PDU.V1TRAP);
    } else {
        pdu.setType(PDU.TRAP);
    }
    pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(1000)));
    pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(trapOid)));
    pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress(ipAddress)));
    pdu.add(new VariableBinding(new OID(trapOid), new OctetString("Major")));
    return pdu;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) TimeTicks(org.snmp4j.smi.TimeTicks) IpAddress(org.snmp4j.smi.IpAddress) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 19 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project LogHub by fbacchella.

the class TestTrap method testtrapv1Generic.

@Ignore
@Test
public void testtrapv1Generic() throws InterruptedException, IOException {
    BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(2);
    SnmpTrap r = new SnmpTrap(receiver, new Pipeline(Collections.emptyList(), "testbig", null));
    r.setPort(0);
    Map<String, Object> props = new HashMap<>();
    props.put("mibdirs", new String[] { "/usr/share/snmp/mibs", "/tmp/mibs" });
    Assert.assertTrue(r.configure(new Properties(props)));
    r.start();
    CommandResponderEvent trapEvent = new CommandResponderEvent(new MessageDispatcherImpl(), new DefaultUdpTransportMapping(), TransportIpAddress.parse("127.0.0.1/162"), 0, 0, null, 0, null, null, 0, null);
    PDUv1 pdu = new PDUv1();
    pdu.setEnterprise(new OID("1.3.6.1.4.1.232"));
    pdu.setAgentAddress(new IpAddress());
    pdu.setGenericTrap(1);
    pdu.setTimestamp(10);
    pdu.add(new VariableBinding(new OID("1.3.6.1.6.3.1.1.4.1"), new OctetString("lldpRemTablesChange")));
    trapEvent.setPDU(pdu);
    r.processPdu(trapEvent);
    Event e = receiver.poll();
    Assert.assertEquals(0.1, (Double) e.get("time_stamp"), 1e-10);
    Assert.assertEquals("warmStart", e.get("generic_trap"));
    Assert.assertEquals("compaq", e.get("enterprise"));
    Assert.assertEquals(null, e.get("specific_trap"));
    Assert.assertEquals("lldpRemTablesChange", e.get("snmpTrapOID"));
    Assert.assertEquals(InetAddress.getByName("0.0.0.0"), e.get("agent_addr"));
    r.interrupt();
}
Also used : OctetString(org.snmp4j.smi.OctetString) HashMap(java.util.HashMap) CommandResponderEvent(org.snmp4j.CommandResponderEvent) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) Properties(loghub.configuration.Properties) Pipeline(loghub.Pipeline) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Event(loghub.Event) IpAddress(org.snmp4j.smi.IpAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project ovirt-engine by oVirt.

the class Snmp method addPayload.

private void addPayload(PDU pdu, AuditLogEvent event, Profile profile) {
    pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks((System.nanoTime() - nanoStart) / 10000000)));
    // { [baseoid] notifications(0) audit(1) }
    pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.getTrapOID(new OID(profile.oid), ENTERPRISE_SPECIFIC, AUDIT)));
    int auditLogId = AuditLogType.UNASSIGNED.getValue();
    try {
        auditLogId = AuditLogType.valueOf(event.getName()).getValue();
    } catch (IllegalArgumentException e) {
        log.warn("Could not find event: {} in auditLogTypes", event.getName());
    }
    // { [baseoid] objects(2) audit(1) }
    OID auditObjects = new OID(profile.oid).append(OBJECTS_AUDIT);
    addString(pdu, auditObjects, INSTANCE_ID, Long.toString(event.getId()), true);
    addString(pdu, auditObjects, NAME, event.getName(), true);
    addInt(pdu, auditObjects, ID, auditLogId, true);
    addInt(pdu, auditObjects, SEVERITY, event.getSeverity().getValue(), true);
    addString(pdu, auditObjects, MESSAGE, event.getMessage(), true);
    addInt(pdu, auditObjects, STATUS, event.getType().getValue(), true);
    addString(pdu, auditObjects, DATETIME, new SimpleDateFormat(ISO8601).format(event.getLogTime()), true);
    // Optional pdu:
    addString(pdu, auditObjects, USERNAME, event.getUserName(), false);
    addUuid(pdu, auditObjects, USER_ID, event.getUserId());
    addString(pdu, auditObjects, VM_NAME, event.getVmName(), false);
    addUuid(pdu, auditObjects, VM_ID, event.getVmId());
    addString(pdu, auditObjects, VDS_NAME, event.getVdsName(), false);
    addUuid(pdu, auditObjects, VDS_ID, event.getVdsId());
    addString(pdu, auditObjects, VM_TEMPLATE_NAME, event.getVmTemplateName(), false);
    addUuid(pdu, auditObjects, VM_TEMPLATE_ID, event.getVmTemplateId());
    addString(pdu, auditObjects, STORAGE_POOL_NAME, event.getStoragePoolName(), false);
    addUuid(pdu, auditObjects, STORAGE_POOL_ID, event.getStoragePoolId());
    addString(pdu, auditObjects, STORAGE_DOMAIN_NAME, event.getStorageDomainName(), false);
    addUuid(pdu, auditObjects, STORAGE_DOMAIN_ID, event.getStorageDomainId());
}
Also used : TimeTicks(org.snmp4j.smi.TimeTicks) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

VariableBinding (org.snmp4j.smi.VariableBinding)52 OID (org.snmp4j.smi.OID)37 OctetString (org.snmp4j.smi.OctetString)31 PDU (org.snmp4j.PDU)30 ScopedPDU (org.snmp4j.ScopedPDU)17 Test (org.junit.Test)11 PDUv1 (org.snmp4j.PDUv1)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)8 ResponseEvent (org.snmp4j.event.ResponseEvent)8 Variable (org.snmp4j.smi.Variable)8 ArrayList (java.util.ArrayList)7 IpAddress (org.snmp4j.smi.IpAddress)7 TimeTicks (org.snmp4j.smi.TimeTicks)6 DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)6 TableEvent (org.snmp4j.util.TableEvent)6 TableUtils (org.snmp4j.util.TableUtils)6 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Map (java.util.Map)4