Search in sources :

Example 41 with OctetString

use of org.snmp4j.smi.OctetString in project cloudstack by apache.

the class SnmpHelper method createPDU.

private PDU createPDU(SnmpTrapInfo snmpTrapInfo) {
    PDU trap = new PDU();
    trap.setType(PDU.TRAP);
    int alertType = snmpTrapInfo.getAlertType() + 1;
    if (alertType > 0) {
        trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, getOID(CsSnmpConstants.TRAPS_PREFIX + alertType)));
        if (snmpTrapInfo.getDataCenterId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.DATA_CENTER_ID), new UnsignedInteger32(snmpTrapInfo.getDataCenterId())));
        }
        if (snmpTrapInfo.getPodId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.POD_ID), new UnsignedInteger32(snmpTrapInfo.getPodId())));
        }
        if (snmpTrapInfo.getClusterId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.CLUSTER_ID), new UnsignedInteger32(snmpTrapInfo.getClusterId())));
        }
        if (snmpTrapInfo.getMessage() != null) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.MESSAGE), new OctetString(snmpTrapInfo.getMessage())));
        } else {
            throw new CloudRuntimeException(" What is the use of alert without message ");
        }
        if (snmpTrapInfo.getGenerationTime() != null) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME), new OctetString(snmpTrapInfo.getGenerationTime().toString())));
        } else {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME)));
        }
    } else {
        throw new CloudRuntimeException(" Invalid alert Type ");
    }
    return trap;
}
Also used : PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsignedInteger32(org.snmp4j.smi.UnsignedInteger32) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 42 with OctetString

use of org.snmp4j.smi.OctetString in project Payara by payara.

the class SnmpNotificationRunnable method run.

@Override
public void run() {
    while (queue.size() > 0) {
        try {
            PDU pdu = DefaultPDUFactory.createPDU(snmpVersion);
            if (SnmpConstants.version1 == snmpVersion) {
                pdu.setType(PDU.V1TRAP);
            } else {
                pdu.setType(PDU.TRAP);
            }
            pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
            OID oidInstance = new OID(executionOptions.getOid());
            pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, oidInstance));
            pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress(executionOptions.getHost())));
            pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString(new Date().toString())));
            SnmpMessage message = queue.getMessage();
            pdu.add(new VariableBinding(oidInstance, new OctetString(message.getSubject() + "\n" + message.getMessage())));
            snmp.send(pdu, communityTarget);
            logger.log(Level.FINE, "Message sent successfully");
        } catch (IOException e) {
            logger.log(Level.SEVERE, "IO Error while sending SNMP message", e);
        }
    }
}
Also used : PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) IpAddress(org.snmp4j.smi.IpAddress) OID(org.snmp4j.smi.OID) IOException(java.io.IOException) VariableBinding(org.snmp4j.smi.VariableBinding) Date(java.util.Date)

Aggregations

OctetString (org.snmp4j.smi.OctetString)42 PDU (org.snmp4j.PDU)18 OID (org.snmp4j.smi.OID)18 Snmp (org.snmp4j.Snmp)15 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)14 CommunityTarget (org.snmp4j.CommunityTarget)12 UdpAddress (org.snmp4j.smi.UdpAddress)12 VariableBinding (org.snmp4j.smi.VariableBinding)12 ScopedPDU (org.snmp4j.ScopedPDU)11 USM (org.snmp4j.security.USM)11 Test (org.junit.Test)10 IOException (java.io.IOException)8 UsmUser (org.snmp4j.security.UsmUser)8 Integer32 (org.snmp4j.smi.Integer32)8 ResponseEvent (org.snmp4j.event.ResponseEvent)7 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Address (org.snmp4j.smi.Address)5 IpAddress (org.snmp4j.smi.IpAddress)5 TimeTicks (org.snmp4j.smi.TimeTicks)5 InetAddress (java.net.InetAddress)4