Java Code Examples for org.snmp4j.PDU#add()
The following examples show how to use
org.snmp4j.PDU#add() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SNMPClient.java From mysql_perf_analyzer with Apache License 2.0 | 6 votes |
/** * This method is capable of handling multiple OIDs * @param oids * @return * @throws IOException */ public Map<OID, String> get(OID oids[]) throws IOException { PDU pdu = createPDU(); for (OID oid : oids) { pdu.add(new VariableBinding(oid)); } pdu.setType(PDU.GET); ResponseEvent event = snmp.send(pdu, getTarget(), null); if(event != null) { PDU pdu2 = event.getResponse(); VariableBinding[] binds = pdu2!=null?event.getResponse().toArray():null; if(binds!=null) { Map<OID, String> res = new LinkedHashMap<OID, String>(binds.length); for(VariableBinding b: binds) res.put(b.getOid(), b.getVariable().toString()); return res; }else return null; } throw new RuntimeException("GET timed out"); }
Example 2
Source File: SNMPClient.java From mysql_perf_analyzer with Apache License 2.0 | 5 votes |
public ResponseEvent getEvent(OID oids[]) throws IOException { PDU pdu = createPDU(); for (OID oid : oids) { pdu.add(new VariableBinding(oid)); } pdu.setType(PDU.GET); ResponseEvent event = snmp.send(pdu, getTarget(), null); if(event != null) { return event; } throw new RuntimeException("GET timed out"); }
Example 3
Source File: SNMPClient.java From mysql_perf_analyzer with Apache License 2.0 | 5 votes |
public List<SNMPTriple> getDiskData(String device) throws IOException { int index = this.getDiskIndex(device); if(index<0) { return new ArrayList<SNMPTriple>(); } logger.fine("Query disk stats for "+index); PDU pdu = createPDU(); for ( int i=1; i< DISK_TABLE_ENTRIES.length; i++) { if(DISK_TABLE_ENTRIES[i].length()==0)continue; pdu.add(new VariableBinding(new OID("."+DISK_TABLE_OID+"."+i+"."+index))); } pdu.setType(PDU.GET); Map<String, String> res = new HashMap<String, String>(13); ResponseEvent event = snmp.send(pdu, getTarget(), null); if(event != null) { VariableBinding[] binds = event.getResponse().toArray(); for(VariableBinding b: binds) res.put(b.getOid().toString(), b.getVariable().toString()); //logger.info(res.toString()); } List<SNMPTriple> resList = new ArrayList<SNMPTriple>(res.size()); for(int i=1;i<DISK_TABLE_ENTRIES.length; i++) { if(DISK_TABLE_ENTRIES[i].length()==0)continue; resList.add(new SNMPTriple("."+DISK_TABLE_OID+"."+i+"."+index, DISK_TABLE_ENTRIES[i], res.get(DISK_TABLE_OID+"."+i+"."+index))); } return resList; }
Example 4
Source File: SNMPUtils.java From ingestion with Apache License 2.0 | 5 votes |
public static void sendTrapV2(String port) throws IOException { PDU trap = new PDU(); trap.setType(PDU.TRAP); 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)); // Specify receiver Address targetaddress = new UdpAddress("127.0.0.1/" + port); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setVersion(SnmpConstants.version2c); target.setAddress(targetaddress); // Send Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); snmp.send(trap, target, null, null); snmp.close(); }
Example 5
Source File: SnmpHelper.java From cloudstack with Apache License 2.0 | 5 votes |
private PDU createPDU(SnmpTrapInfo snmpTrapInfo) { PDU trap = new PDU(); trap.setType(PDU.TRAP); int alertType = snmpTrapInfo.getAlertType() + 1; if (alertType > 0) { long sysUpTimeTicks = ManagementFactory.getRuntimeMXBean().getUptime() / 10; trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTimeTicks))); 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; }
Example 6
Source File: SNMPUtils.java From ingestion with Apache License 2.0 | 4 votes |
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(); }
Example 7
Source File: LumentumSdnRoadmFlowRuleProgrammable.java From onos with Apache License 2.0 | 4 votes |
private boolean removeCrossConnect(CrossConnectFlowRule xc) { int channel = toChannel(xc.ochSignal()); // Create the PDU object PDU pdu = new PDU(); pdu.setType(PDU.SET); // Disable the channel OID ctrlChannelState = new OID(CTRL_CHANNEL_STATE + (xc.isAddRule() ? "1." : "2.") + channel); pdu.add(new VariableBinding(ctrlChannelState, new Integer32(OUT_OF_SERVICE))); // Put cross connect back into default port 1 OID ctrlChannelAddDropPortIndex = new OID(CTRL_CHANNEL_ADD_DROP_PORT_INDEX + (xc.isAddRule() ? "1." : "2.") + channel); pdu.add(new VariableBinding(ctrlChannelAddDropPortIndex, new UnsignedInteger32(DISABLE_CHANNEL_ADD_DROP_PORT_INDEX))); // Put port/channel back to open loop OID ctrlChannelMode = new OID(CTRL_CHANNEL_MODE + (xc.isAddRule() ? "1." : "2.") + channel); pdu.add(new VariableBinding(ctrlChannelMode, new Integer32(OPEN_LOOP))); // Add rules are set to target power, drop rules are attenuated if (xc.isAddRule()) { OID ctrlChannelTargetPower = new OID(CTRL_CHANNEL_TARGET_POWER + "1." + channel); pdu.add(new VariableBinding(ctrlChannelTargetPower, new Integer32(DISABLE_CHANNEL_TARGET_POWER))); } else { OID ctrlChannelAbsoluteAttenuation = new OID(CTRL_CHANNEL_ABSOLUTE_ATTENUATION + "2." + channel); pdu.add(new VariableBinding( ctrlChannelAbsoluteAttenuation, new UnsignedInteger32(DISABLE_CHANNEL_ABSOLUTE_ATTENUATION))); } try { ResponseEvent response = snmp.set(pdu); // TODO: parse response } catch (IOException e) { log.error("Failed to remove cross connect, unable to connect to device: ", e); return false; } return true; }
Example 8
Source File: SnmpBinding.java From openhab1-addons with Eclipse Public License 2.0 | 4 votes |
/** * @{inheritDoc */ @Override public void execute() { for (SnmpBindingProvider provider : providers) { for (String itemName : provider.getInBindingItemNames()) { int refreshInterval = provider.getRefreshInterval(itemName); Long lastUpdateTimeStamp = lastUpdateMap.get(itemName); if (lastUpdateTimeStamp == null) { lastUpdateTimeStamp = 0L; } long age = System.currentTimeMillis() - lastUpdateTimeStamp; boolean needsUpdate; if (refreshInterval == 0) { needsUpdate = false; } else { needsUpdate = age >= refreshInterval; } if (needsUpdate) { logger.debug("Item '{}' is about to be refreshed", itemName); // Set up the target CommunityTarget target = new CommunityTarget(); target.setCommunity(provider.getCommunity(itemName)); target.setAddress(provider.getAddress(itemName)); target.setRetries(retries); target.setTimeout(timeout); target.setVersion(provider.getSnmpVersion(itemName)); // Create the PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(provider.getOID(itemName))); pdu.setType(PDU.GET); logger.debug("SNMP: Send PDU {} {}", provider.getAddress(itemName), pdu); if (snmp == null) { logger.error("SNMP: snmp not initialised - aborting request"); } else { sendPDU(target, pdu); } lastUpdateMap.put(itemName, System.currentTimeMillis()); } } } }
Example 9
Source File: SnmpmanSetTest.java From snmpman with Apache License 2.0 | 3 votes |
private ResponseEvent setVariableToOID(OID oid, Variable variable) throws IOException { final CommunityTarget target = getCommunityTarget(COMMUNITY, GenericAddress.parse(String.format("127.0.0.1/%d", PORT))); PDU pdu = new PDU(); pdu.setType(PDU.SET); VariableBinding variableBinding = new VariableBinding(oid); variableBinding.setVariable(variable); pdu.add(variableBinding); return snmp.set(pdu, target); }