本文整理汇总了Java中org.snmp4j.PDUv1类的典型用法代码示例。如果您正苦于以下问题:Java PDUv1类的具体用法?Java PDUv1怎么用?Java PDUv1使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PDUv1类属于org.snmp4j包,在下文中一共展示了PDUv1类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendTrap
import org.snmp4j.PDUv1; //导入依赖的package包/类
/**
* Send next-in-line trap from queue to to SNMP server
*/
public void sendTrap() {
String trapVal = trapQueue.poll();
if (trapVal == null) {
return;
}
try {
PDUv1 trapPdu = createTrapPDU(trapVal);
DefaultUdpTransportMapping tm = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(tm);
tm.listen();
OctetString community = new OctetString(SNMP_XAP_COMMUNITY);
Address add = GenericAddress.parse("udp" + ":" + snmpServerIP + "/" + snmpServerPort);
CommunityTarget target = new CommunityTarget(add, community);
target.setVersion(SnmpConstants.version1);
target.setRetries(0);
target.setTimeout(5000);
snmp.send(trapPdu, target);
}
catch (IOException e) {
e.printStackTrace();
}
}
示例2: sendTest
import org.snmp4j.PDUv1; //导入依赖的package包/类
public void sendTest(String agentAddress, int port, String community, PDU pdu) {
for (RegistrationInfo info : s_registrations.values()) {
if (port == info.getPort()) {
Snmp snmp = info.getSession();
MessageDispatcher dispatcher = snmp.getMessageDispatcher();
TransportMapping transport = info.getTransportMapping();
int securityModel = (pdu instanceof PDUv1 ? SecurityModel.SECURITY_MODEL_SNMPv1 :SecurityModel.SECURITY_MODEL_SNMPv2c);
int messageModel = (pdu instanceof PDUv1 ? MessageProcessingModel.MPv1 : MessageProcessingModel.MPv2c);
CommandResponderEvent e = new CommandResponderEvent(dispatcher, transport, new IpAddress(agentAddress), messageModel,
securityModel, community.getBytes(),
SecurityLevel.NOAUTH_NOPRIV, new PduHandle(), pdu, 1000, null);
info.getHandler().processPdu(e);
}
}
}
示例3: createTrapPDU
import org.snmp4j.PDUv1; //导入依赖的package包/类
private static PDUv1 createTrapPDU(String trapData) throws UnknownHostException{
PDUv1 trapPdu = (PDUv1)DefaultPDUFactory.createPDU(SnmpConstants.version1);
trapPdu.setType(PDU.V1TRAP);
VariableBinding vbm = new VariableBinding();
vbm.setOid(new OID(SNMP_XAP_ALERT_MSG_OID));
vbm.setVariable(new OctetString(trapData));
trapPdu.add(vbm);
trapPdu.setAgentAddress(getLocalAddress());
return trapPdu;
}
示例4: newNotification
import org.snmp4j.PDUv1; //导入依赖的package包/类
private Snmp4jNotification newNotification(CommandResponderEvent event,
SnmpTarget target, VarbindCollection varbinds) {
switch (event.getPDU().getType()) {
case PDU.V1TRAP:
Snmp4jV1Trap trap = new Snmp4jV1Trap(target, varbinds);
PDUv1 pdu = (PDUv1) event.getPDU();
trap.setEnterprise(pdu.getEnterprise().toString());
trap.setAgentAddress(pdu.getAgentAddress().toString());
MibTrapV1Support trapSupport = varbindFactory.getMib()
.getV1TrapSupport();
trap.setGenericType(new ImmutableObjectValue(SMIConstants.SYNTAX_INTEGER,
pdu.getGenericTrap(), trapSupport.getGenericTrapFormatter()));
trap.setSpecificType(new ImmutableObjectValue(SMIConstants.SYNTAX_INTEGER,
pdu.getSpecificTrap(), trapSupport.getSpecificTrapFormatter()));
trap.setTimestamp(new ImmutableObjectValue(SMIConstants.SYNTAX_TIMETICKS,
pdu.getTimestamp(), trapSupport.getTimestampFormatter()));
return trap;
case PDU.INFORM:
return new Snmp4jNotification(SnmpNotification.Type.INFORM, target,
varbinds);
case PDU.TRAP:
return new Snmp4jNotification(SnmpNotification.Type.TRAP, target,
varbinds);
default:
throw new IllegalArgumentException("unrecognized PDU type");
}
}
示例5: sendSnmpV1Trap
import org.snmp4j.PDUv1; //导入依赖的package包/类
/**
* This methods sends the V1 trap to the Localhost in port 163
*/
public void sendSnmpV1Trap()
{
try
{
//Create Transport Mapping
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
//Create Target
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(5000);
//Create PDU for V1
PDUv1 pdu = new PDUv1();
pdu.setType(PDU.V1TRAP);
pdu.setEnterprise(new OID(trapOid));
pdu.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC);
pdu.setSpecificTrap(1);
pdu.setAgentAddress(new IpAddress(ipAddress));
//Send the PDU
Snmp snmp = new Snmp(transport);
System.out.println("Sending V1 Trap to " + ipAddress + " on Port " + port);
snmp.send(pdu, comtarget);
snmp.close();
}
catch (Exception e)
{
System.err.println("Error in Sending V1 Trap to " + ipAddress + " on Port " + port);
System.err.println("Exception Message = " + e.getMessage());
}
}
示例6: createPDU
import org.snmp4j.PDUv1; //导入依赖的package包/类
@Override
public PDU createPDU(MessageProcessingModel mpm) {
switch (mpm.getID()) {
case MessageProcessingModel.MPv3:
return new ScopedPDU();
case MessageProcessingModel.MPv1:
return new PDUv1();
default:
return new PDU();
}
}
示例7: match
import org.snmp4j.PDUv1; //导入依赖的package包/类
/**
* Returns the matching severity for this PDU, or null if no match.
*/
public EventSeverity match(PDU pdu) {
if (pdu instanceof PDUv1) {
PDUv1 pdu1 = (PDUv1) pdu;
if (genericTrap != UNSET && genericTrap != pdu1.getGenericTrap()) {
log.trace("no match generic trap");
return null;
}
if (specificTrap != UNSET && specificTrap != pdu1.getSpecificTrap()) {
log.trace("no match specific trap");
return null;
}
if (!pdu1.getEnterprise().startsWith(enterprise)) {
log.trace("no match enterprise");
return null;
}
} else {
OID oid = (OID) pdu.getVariable(SnmpConstants.snmpTrapOID);
if (oid != null && oid.startsWith(trapOid)) {
log.trace("no match enterprise");
return null;
}
}
if (varbind.size() != 0) {
Variable v = pdu.getVariable(varbind);
if (v == null) {
log.trace("no required variable found");
return null;
}
if (!value.matcher(v.toString()).find()) {
log.trace("no match variable");
return null;
}
}
return severity;
}
示例8: processPdu
import org.snmp4j.PDUv1; //导入依赖的package包/类
public synchronized void processPdu(CommandResponderEvent cmdRespEvent) {
PDU pdu = cmdRespEvent.getPDU();
System.out.println("Received PDU... " + pdu);
if (pdu != null) {
System.out.println(pdu.getClass().getName());
System.out.println("trapType = " + pdu.getType());
System.out.println("isPDUv1 = " + (pdu instanceof PDUv1));
System.out.println("isTrap = " + (pdu.getType() == PDU.TRAP));
System.out.println("isInform = " + (pdu.getType() == PDU.INFORM));
System.out.println("variableBindings = " + pdu.getVariableBindings());
trapCount++;
} else {
System.err.println("ERROR: Can't create PDU");
}
}
示例9: buildAgentConfig
import org.snmp4j.PDUv1; //导入依赖的package包/类
protected SnmpAgentConfig buildAgentConfig(String address, int port, String community, PDU pdu) throws UnknownHostException {
SnmpAgentConfig config = new SnmpAgentConfig();
config.setAddress(InetAddress.getByName(address));
config.setPort(port);
config.setVersion(pdu instanceof PDUv1 ? SnmpAgentConfig.VERSION1 : SnmpAgentConfig.VERSION2C);
return config;
}
示例10: processPdu
import org.snmp4j.PDUv1; //导入依赖的package包/类
@Override
public void processPdu(CommandResponderEvent e) {
PDU command = new PDU(e.getPDU());
IpAddress addr = ((IpAddress)e.getPeerAddress());
if (command != null) {
if (command.getType() == PDU.INFORM) {
PDU response = new PDU(command);
response.setErrorIndex(0);
response.setErrorStatus(0);
response.setType(PDU.RESPONSE);
StatusInformation statusInformation = new StatusInformation();
StateReference ref = e.getStateReference();
try {
e.getMessageDispatcher().returnResponsePdu(e.getMessageProcessingModel(),
e.getSecurityModel(),
e.getSecurityName(),
e.getSecurityLevel(),
response,
e.getMaxSizeResponsePDU(),
ref,
statusInformation);
if (log().isDebugEnabled()) {
log().debug("Sent RESPONSE PDU to peer " + addr + " acknowledging receipt of INFORM (reqId=" + command.getRequestID() + ")");
}
} catch (MessageException ex) {
log().error("Error while sending RESPONSE PDU to peer " + addr + ": " + ex.getMessage() + "acknowledging receipt of INFORM (reqId=" + command.getRequestID() + ")");
}
}
}
if (e.getPDU() instanceof PDUv1) {
m_listener.trapReceived(new Snmp4JV1TrapInformation(addr.getInetAddress(), new String(e.getSecurityName()), (PDUv1)e.getPDU(), m_trapProcessorFactory.createTrapProcessor()));
} else {
m_listener.trapReceived(new Snmp4JV2TrapInformation(addr.getInetAddress(), new String(e.getSecurityName()), e.getPDU(), m_trapProcessorFactory.createTrapProcessor()));
}
}
示例11: processGet
import org.snmp4j.PDUv1; //导入依赖的package包/类
/**
* @param request
* @return
*/
@SuppressWarnings("unchecked")
private PDU processGet(PDU request) {
PDU response = request;
response.setErrorIndex(0);
response.setErrorStatus(0);
response.setType(PDU.RESPONSE);
Vector<VariableBinding> varBinds = response.getVariableBindings();
for(int i = 0; i < varBinds.size(); i++) {
VariableBinding varBind = varBinds.get(i);
VariableBinding nextVarBind = m_agent.get(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;
}
示例12: processGetNext
import org.snmp4j.PDUv1; //导入依赖的package包/类
/**
* @param request
* @return
*/
@SuppressWarnings("unchecked")
private PDU processGetNext(PDU request) {
PDU response = request;
response.setErrorIndex(0);
response.setErrorStatus(0);
response.setType(PDU.RESPONSE);
Vector<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;
}
示例13: sendTrapV1
import org.snmp4j.PDUv1; //导入依赖的package包/类
public static void sendTrapV1(String port) throws IOException {
TransportMapping<?> transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(new OctetString("public")));
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(new UdpAddress("127.0.0.1/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(5000);
PDU trap = new PDUv1();
trap.setType(PDU.V1TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
// Add Payload
Variable var = new OctetString("some string");
trap.add(new VariableBinding(oid, var));
// Send
Snmp snmp = new Snmp(transport);
snmp.send(trap, comtarget);
transport.close();
snmp.close();
}
示例14: processPdu
import org.snmp4j.PDUv1; //导入依赖的package包/类
public void processPdu(CommandResponderEvent e) {
PDU command = new PDU(e.getPDU());
IpAddress addr = ((IpAddress)e.getPeerAddress());
if (command != null) {
if (command.getType() == PDU.INFORM) {
PDU response = new PDU(command);
response.setErrorIndex(0);
response.setErrorStatus(0);
response.setType(PDU.RESPONSE);
StatusInformation statusInformation = new StatusInformation();
StateReference ref = e.getStateReference();
try {
e.getMessageDispatcher().returnResponsePdu(e.getMessageProcessingModel(),
e.getSecurityModel(),
e.getSecurityName(),
e.getSecurityLevel(),
response,
e.getMaxSizeResponsePDU(),
ref,
statusInformation);
if (log().isDebugEnabled()) {
log().debug("Sent RESPONSE PDU to peer " + addr + " acknowledging receipt of INFORM (reqId=" + command.getRequestID() + ")");
}
} catch (MessageException ex) {
log().error("Error while sending RESPONSE PDU to peer " + addr + ": " + ex.getMessage() + "acknowledging receipt of INFORM (reqId=" + command.getRequestID() + ")");
}
}
}
if (e.getPDU() instanceof PDUv1) {
m_listener.trapReceived(new Snmp4JV1TrapInformation(addr.getInetAddress(), new String(e.getSecurityName()), (PDUv1)e.getPDU(), m_trapProcessorFactory.createTrapProcessor()));
} else {
m_listener.trapReceived(new Snmp4JV2TrapInformation(addr.getInetAddress(), new String(e.getSecurityName()), e.getPDU(), m_trapProcessorFactory.createTrapProcessor()));
}
}
示例15: toString
import org.snmp4j.PDUv1; //导入依赖的package包/类
/**
* Converts the given snmp pdu to a String body.
*
* @param pdu the snmp pdu
* @return the text content
*/
@Converter
public static String toString(PDU pdu) {
// the output buffer
StringBuilder sb = new StringBuilder();
// prepare the header
if (pdu.getType() == PDU.V1TRAP) {
sb.append("<" + SNMP_TAG + " messageType=\"v1\">");
} else {
sb.append(SNMP_TAG_OPEN);
}
// Extract SNMPv1 specific variables
if (pdu.getType() == PDU.V1TRAP) {
PDUv1 v1pdu = (PDUv1) pdu;
entryAppend(sb, "enterprise", v1pdu.getEnterprise().toString());
entryAppend(sb, "agent-addr", v1pdu.getAgentAddress().toString());
entryAppend(sb, "generic-trap", Integer.toString(v1pdu.getGenericTrap()));
entryAppend(sb, "specific-trap", Integer.toString(v1pdu.getSpecificTrap()));
entryAppend(sb, "time-stamp", Long.toString(v1pdu.getTimestamp()));
}
// now loop all variables of the response
for (Object o : pdu.getVariableBindings()) {
VariableBinding b = (VariableBinding)o;
sb.append(ENTRY_TAG_OPEN);
sb.append(OID_TAG_OPEN);
sb.append(b.getOid().toString());
sb.append(OID_TAG_CLOSE);
sb.append(VALUE_TAG_OPEN);
sb.append(StringHelper.xmlEncode(b.getVariable().toString()));
sb.append(VALUE_TAG_CLOSE);
sb.append(ENTRY_TAG_CLOSE);
}
// prepare the footer
sb.append(SNMP_TAG_CLOSE);
return sb.toString();
}