use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class IpAddressTableEntry method getIpAddressNetMask.
/**
* <p>getIpAdEntNetMask</p>
*
* @return a {@link java.net.InetAddress} object.
*/
public InetAddress getIpAddressNetMask() {
final SnmpValue value = getValue(IP_ADDR_ENT_NETMASK);
// LOG.debug("getIpAddressNetMask: value = {}", value.toDisplayString());
final SnmpObjId netmaskRef = value.toSnmpObjId().getInstance(IPAddressTableTracker.IP_ADDRESS_PREFIX_ORIGIN_INDEX);
if (netmaskRef == null) {
LOG.warn("Unable to get netmask reference from instance.");
return null;
}
final int[] rawIds = netmaskRef.getIds();
final int addressType = rawIds[1];
final int addressLength = rawIds[2];
final InetAddress address = getInetAddress(rawIds, 3, addressLength);
final int mask = rawIds[rawIds.length - 1];
if (addressType == IPAddressTableTracker.TYPE_IPV4) {
return InetAddressUtils.convertCidrToInetAddressV4(mask);
} else if (addressType == IPAddressTableTracker.TYPE_IPV6) {
return InetAddressUtils.convertCidrToInetAddressV6(mask);
} else if (addressType == IPAddressTableTracker.TYPE_IPV6Z) {
LOG.debug("Got an IPv6z address, returning {}", address);
} else {
LOG.warn("Unsure how to handle IP address type ({})", addressType);
}
return address;
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class TableStrategy method call.
@Override
public OnmsAccessPointCollection call() throws IOException {
OnmsAccessPointCollection apsUp = new OnmsAccessPointCollection();
InetAddress ipaddr = m_iface.getIpAddress();
// Retrieve this interface's SNMP peer object
SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(ipaddr);
if (agentConfig == null) {
throw new IllegalStateException("SnmpAgentConfig object not available for interface " + ipaddr);
}
final String hostAddress = InetAddressUtils.str(ipaddr);
LOG.debug("poll: setting SNMP peer attribute for interface {}", hostAddress);
// Get configuration parameters
String oid = ParameterMap.getKeyedString(m_parameters, "oid", null);
if (oid == null) {
throw new IllegalStateException("oid parameter is not set.");
}
agentConfig.hashCode();
// Set timeout and retries on SNMP peer object
agentConfig.setTimeout(ParameterMap.getKeyedInteger(m_parameters, "timeout", agentConfig.getTimeout()));
agentConfig.setRetries(ParameterMap.getKeyedInteger(m_parameters, "retry", ParameterMap.getKeyedInteger(m_parameters, "retries", agentConfig.getRetries())));
agentConfig.setPort(ParameterMap.getKeyedInteger(m_parameters, "port", agentConfig.getPort()));
LOG.debug("TableStrategy.poll: SnmpAgentConfig address={}", agentConfig);
// Establish SNMP session with interface
try {
SnmpObjId snmpObjectId = SnmpObjId.get(oid);
Map<SnmpInstId, SnmpValue> map = SnmpUtils.getOidValues(agentConfig, "AccessPointMonitor::TableStrategy", snmpObjectId);
if (map.size() <= 0) {
throw new IOException("No entries found in table (possible timeout).");
}
for (Map.Entry<SnmpInstId, SnmpValue> entry : map.entrySet()) {
SnmpValue value = entry.getValue();
String physAddr = getPhysAddrFromValue(value);
LOG.debug("AP at value '{}' with MAC '{}' is considered to be ONLINE on controller '{}'", value.toHexString(), physAddr, m_iface.getIpAddress());
OnmsAccessPoint ap = m_accessPointDao.get(physAddr);
if (ap != null) {
if (ap.getPollingPackage().compareToIgnoreCase(getPackage().getName()) == 0) {
// Save the controller's IP address
ap.setControllerIpAddress(ipaddr);
apsUp.add(ap);
} else {
LOG.info("AP with MAC '{}' is in a different package.", physAddr);
}
} else {
LOG.info("No matching AP in database for value '{}'.", value.toHexString());
}
}
} catch (InterruptedException e) {
LOG.error("Interrupted while polling {}", hostAddress, e);
}
return apsUp;
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class EventCreator method createEventFrom.
public Event createEventFrom(final TrapDTO trapDTO, final String systemId, final String location, final InetAddress trapAddress) {
LOG.debug("{} trap - trapInterface: {}", trapDTO.getVersion(), trapDTO.getAgentAddress());
// Set event data
final EventBuilder eventBuilder = new EventBuilder(null, "trapd");
eventBuilder.setTime(new Date(trapDTO.getCreationTime()));
eventBuilder.setCommunity(trapDTO.getCommunity());
eventBuilder.setSnmpTimeStamp(trapDTO.getTimestamp());
eventBuilder.setSnmpVersion(trapDTO.getVersion());
eventBuilder.setSnmpHost(str(trapAddress));
eventBuilder.setInterface(trapAddress);
eventBuilder.setHost(InetAddressUtils.toIpAddrString(trapDTO.getAgentAddress()));
// Handle trap identity
final TrapIdentityDTO trapIdentity = trapDTO.getTrapIdentity();
if (trapIdentity != null) {
LOG.debug("Trap Identity {}", trapIdentity);
eventBuilder.setGeneric(trapIdentity.getGeneric());
eventBuilder.setSpecific(trapIdentity.getSpecific());
eventBuilder.setEnterpriseId(trapIdentity.getEnterpriseId());
}
// Handle var bindings
for (SnmpResult eachResult : trapDTO.getResults()) {
final SnmpObjId name = eachResult.getBase();
final SnmpValue value = eachResult.getValue();
eventBuilder.addParam(SyntaxToEvent.processSyntax(name.toString(), value));
if (EventConstants.OID_SNMP_IFINDEX.isPrefixOf(name)) {
eventBuilder.setIfIndex(value.toInt());
}
}
// Resolve Node id and set, if known by OpenNMS
final long nodeId = resolveNodeId(location, trapAddress);
if (nodeId != -1) {
eventBuilder.setNodeid(nodeId);
}
// systemId of the local system if it remains null here.
if (systemId != null) {
eventBuilder.setDistPoller(systemId);
}
// Get event template and set uei, if unknown
final Event event = eventBuilder.getEvent();
final org.opennms.netmgt.xml.eventconf.Event econf = eventConfDao.findByEvent(event);
if (econf == null || econf.getUei() == null) {
event.setUei("uei.opennms.org/default/trap");
} else {
event.setUei(econf.getUei());
}
return event;
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class TrapHandlerITCase method testV1EnterpriseIdAndGenericAndSpecificAndMatchWithVarbindsAndTC.
// These exist to provide testing for the new Textual Convention feature
// See EventConfDataTest for the other part of this testing
@Test
@DirtiesContext
public void testV1EnterpriseIdAndGenericAndSpecificAndMatchWithVarbindsAndTC() throws Exception {
SnmpValueFactory valueFactory = SnmpUtils.getValueFactory();
LinkedHashMap<String, SnmpValue> varbinds = new LinkedHashMap<String, SnmpValue>();
varbinds.put(".1.3.6.1.4.1.14179.2.6.2.20.0", valueFactory.getOctetString(new byte[] { (byte) 0x00, (byte) 0x14, (byte) 0xf1, (byte) 0xad, (byte) 0xa7, (byte) 0x50 }));
Collection<Event> events = anticipateAndSend(false, true, "uei.opennms.org/vendor/cisco/bsnAPNoiseProfileUpdatedToPass", "v1", ".1.3.6.1.4.1.14179.2.6.3", 6, 38, varbinds);
boolean foundMacAddress = false;
// Assert that the MAC address varbind has been formatted into a colon-separated octet string
for (Event event : events) {
for (Parm parm : event.getParmCollection()) {
if (".1.3.6.1.4.1.14179.2.6.2.20.0".equals(parm.getParmName())) {
assertEquals("MAC address does not match", "00:14:F1:AD:A7:50", parm.getValue().getContent());
foundMacAddress = true;
}
}
}
assertTrue("Did not find expected MAC address parm", foundMacAddress);
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class TrapHandlerITCase method sendV2Trap.
public void sendV2Trap(String enterprise, int specific, LinkedHashMap<String, SnmpValue> varbinds) throws Exception {
SnmpObjId enterpriseId = SnmpObjId.get(enterprise);
boolean isGeneric = false;
SnmpObjId trapOID;
if (SnmpObjId.get(".1.3.6.1.6.3.1.1.5").isPrefixOf(enterpriseId)) {
isGeneric = true;
trapOID = enterpriseId;
} else {
trapOID = SnmpObjId.get(enterpriseId, new SnmpInstId(specific));
// XXX or should it be this
// trap OID = enterprise + ".0." + specific;
}
SnmpTrapBuilder pdu = SnmpUtils.getV2TrapBuilder();
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.2.1.1.3.0"), SnmpUtils.getValueFactory().getTimeTicks(0));
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.1.0"), SnmpUtils.getValueFactory().getObjectId(trapOID));
if (isGeneric) {
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.3.0"), SnmpUtils.getValueFactory().getObjectId(enterpriseId));
}
for (Map.Entry<String, SnmpValue> entry : varbinds.entrySet()) {
pdu.addVarBind(SnmpObjId.get(entry.getKey()), entry.getValue());
}
pdu.send(getHostAddress(), m_trapdConfig.getSnmpTrapPort(), "public");
}
Aggregations