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