use of org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation in project opennms by OpenNMS.
the class SnmpHardwareInventoryProvisioningAdapter method synchronizeInventory.
/**
* Synchronize inventory.
* <p>Obtain the ENTITY-MIB and vendor attributes from the target node through SNMP.</p>
* <p>If the node has a hardware inventory data on the database, this is going to be overridden only,
* if the gathered data differs from the data at the database, otherwise the gathered data will be
* discarded.</p>
*
* @param nodeId the node id
*/
private void synchronizeInventory(int nodeId) {
final OnmsNode node = m_nodeDao.get(nodeId);
if (node == null) {
throw new ProvisioningAdapterException("Failed to return node for given nodeId: " + nodeId);
}
final OnmsIpInterface intf = node.getPrimaryInterface();
if (intf == null) {
throw new ProvisioningAdapterException("Can't find the SNMP Primary IP address for nodeId: " + nodeId);
}
final InetAddress ipAddress = intf.getIpAddress();
EventBuilder ebldr = null;
try {
if (node.getSysObjectId() == null) {
LOG.warn("Skiping hardware discover because the node {} doesn't support SNMP", nodeId);
return;
}
OnmsMonitoringLocation location = node.getLocation();
String locationName = (location == null) ? null : location.getLocationName();
SnmpAgentConfig agentConfig = m_snmpConfigDao.getAgentConfig(ipAddress, locationName);
final OnmsHwEntity newRoot = getRootEntity(agentConfig, node);
newRoot.setNode(node);
final OnmsHwEntity currentRoot = m_hwEntityDao.findRootByNodeId(node.getId());
if (newRoot.equals(currentRoot)) {
LOG.info("No changes detected on the hardware inventory for nodeId {}", nodeId);
return;
}
if (currentRoot == null) {
LOG.info("Saving hardware inventory for nodeId {}", nodeId);
} else {
LOG.info("Updating hardware inventory for nodeId {}", nodeId);
m_hwEntityDao.delete(currentRoot);
m_hwEntityDao.flush();
}
m_hwEntityDao.saveOrUpdate(newRoot);
ebldr = new EventBuilder(EventConstants.HARDWARE_INVENTORY_SUCCESSFUL_UEI, PREFIX + NAME);
} catch (Throwable e) {
ebldr = new EventBuilder(EventConstants.HARDWARE_INVENTORY_FAILED_UEI, PREFIX + NAME);
ebldr.addParam(EventConstants.PARM_REASON, e.getMessage());
}
if (ebldr != null) {
ebldr.setNodeid(nodeId);
ebldr.setInterface(ipAddress);
getEventForwarder().sendNow(ebldr.getEvent());
}
}
use of org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation in project opennms by OpenNMS.
the class DefaultProvisionService method createUndiscoveredNode.
/**
* {@inheritDoc}
*/
@Transactional
@Override
public OnmsNode createUndiscoveredNode(final String ipAddress, final String foreignSource, final String locationString) {
final String effectiveForeignSource = foreignSource == null ? FOREIGN_SOURCE_FOR_DISCOVERED_NODES : foreignSource;
final String effectiveLocationName = MonitoringLocationUtils.isDefaultLocationName(locationString) ? null : locationString;
final OnmsNode node = new UpsertTemplate<OnmsNode, NodeDao>(m_transactionManager, m_nodeDao) {
@Override
protected OnmsNode query() {
// Find all of the nodes in the target requisition with the given IP address
return m_nodeDao.findByForeignSourceAndIpAddress(effectiveForeignSource, ipAddress).stream().filter(n -> {
// Now filter the nodes by location
final String existingLocationName = MonitoringLocationUtils.getLocationNameOrNullIfDefault(n);
return Objects.equals(existingLocationName, effectiveLocationName);
}).findFirst().orElse(null);
}
@Override
protected OnmsNode doUpdate(OnmsNode existingNode) {
// we found an existing node so exit by returning null;
return null;
}
@Override
protected OnmsNode doInsert() {
final Date now = new Date();
OnmsMonitoringLocation location = createLocationIfNecessary(locationString);
// Associate the location with the node
final OnmsNode node = new OnmsNode(location);
final String hostname = getHostnameResolver().getHostname(addr(ipAddress), locationString);
if (hostname == null || ipAddress.equals(hostname)) {
node.setLabel(ipAddress);
node.setLabelSource(NodeLabelSource.ADDRESS);
} else {
node.setLabel(hostname);
node.setLabelSource(NodeLabelSource.HOSTNAME);
}
node.setForeignSource(effectiveForeignSource);
node.setType(NodeType.ACTIVE);
node.setLastCapsdPoll(now);
final OnmsIpInterface iface = new OnmsIpInterface(InetAddressUtils.addr(ipAddress), node);
iface.setIsManaged("M");
iface.setIpHostName(hostname);
iface.setIsSnmpPrimary(PrimaryType.NOT_ELIGIBLE);
iface.setIpLastCapsdPoll(now);
m_nodeDao.save(node);
m_nodeDao.flush();
return node;
}
}.execute();
if (node != null) {
if (effectiveForeignSource != null) {
node.setForeignId(node.getNodeId());
createUpdateRequistion(ipAddress, node, effectiveLocationName, effectiveForeignSource);
}
// we do this here rather than in the doInsert method because
// the doInsert may abort
node.visit(new AddEventVisitor(m_eventForwarder));
}
return node;
}
use of org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation in project opennms by OpenNMS.
the class EnLinkdIT method testLoadFourLevelTopology.
/*
* 1400=
* [1544, 1534]
* 1534=
* [1364, 405, 1310]}
* 1364=
* [2965, 5022]
*/
@Test
public void testLoadFourLevelTopology() {
final OnmsMonitoringLocation location = new OnmsMonitoringLocation(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID, MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID);
OnmsNode lnode1400 = new OnmsNode();
lnode1400.setId(1400);
lnode1400.setForeignSource("linkd");
lnode1400.setForeignId("lnode1400");
lnode1400.setLabel("lnode1400");
lnode1400.setLocation(location);
OnmsNode lnode1544 = new OnmsNode();
lnode1544.setId(1544);
lnode1544.setForeignSource("linkd");
lnode1544.setForeignId("lnode1544");
lnode1544.setLabel("lnode1544");
lnode1544.setLocation(location);
OnmsNode lnode1534 = new OnmsNode();
lnode1534.setId(1534);
lnode1534.setForeignSource("linkd");
lnode1534.setForeignId("lnode1534");
lnode1534.setLabel("lnode1534");
lnode1534.setLocation(location);
OnmsNode lnode1364 = new OnmsNode();
lnode1364.setId(1364);
lnode1364.setForeignSource("linkd");
lnode1364.setForeignId("lnode1364");
lnode1364.setLabel("lnode1364");
lnode1364.setLocation(location);
OnmsNode lnode1310 = new OnmsNode();
lnode1310.setId(1310);
lnode1310.setForeignSource("linkd");
lnode1310.setForeignId("lnode1310");
lnode1310.setLabel("lnode1310");
lnode1310.setLocation(location);
OnmsNode lnode405 = new OnmsNode();
lnode405.setId(405);
lnode405.setForeignSource("linkd");
lnode405.setForeignId("lnode405");
lnode405.setLabel("lnode405");
lnode405.setLocation(location);
OnmsNode lnode2965 = new OnmsNode();
lnode2965.setId(2965);
lnode2965.setForeignSource("linkd");
lnode2965.setForeignId("lnode2965");
lnode2965.setLabel("lnode2965");
lnode2965.setLocation(location);
OnmsNode lnode5022 = new OnmsNode();
lnode5022.setId(5022);
lnode5022.setForeignSource("linkd");
lnode5022.setForeignId("lnode5022");
lnode5022.setLabel("lnode5022");
lnode5022.setLocation(location);
m_nodeDao.save(lnode1400);
m_nodeDao.save(lnode1544);
m_nodeDao.save(lnode1534);
m_nodeDao.save(lnode1364);
m_nodeDao.save(lnode1310);
m_nodeDao.save(lnode405);
m_nodeDao.save(lnode2965);
m_nodeDao.save(lnode5022);
OnmsNode node1400 = m_nodeDao.findByForeignId("linkd", "lnode1400");
OnmsNode node1544 = m_nodeDao.findByForeignId("linkd", "lnode1544");
OnmsNode node1534 = m_nodeDao.findByForeignId("linkd", "lnode1534");
OnmsNode node1364 = m_nodeDao.findByForeignId("linkd", "lnode1364");
OnmsNode node1310 = m_nodeDao.findByForeignId("linkd", "lnode1310");
OnmsNode node405 = m_nodeDao.findByForeignId("linkd", "lnode405");
OnmsNode node2965 = m_nodeDao.findByForeignId("linkd", "lnode2965");
OnmsNode node5022 = m_nodeDao.findByForeignId("linkd", "lnode5022");
BridgeBridgeLink link7 = new BridgeBridgeLink();
link7.setNode(node5022);
link7.setBridgePort(4);
link7.setDesignatedNode(node2965);
link7.setDesignatedPort(5);
link7.setBridgeBridgeLinkLastPollTime(link7.getBridgeBridgeLinkCreateTime());
m_bridgeBridgeLinkDao.save(link7);
m_bridgeBridgeLinkDao.flush();
BridgeBridgeLink link1 = new BridgeBridgeLink();
link1.setNode(node1544);
link1.setBridgePort(15);
link1.setDesignatedNode(node1400);
link1.setDesignatedPort(1);
link1.setBridgeBridgeLinkLastPollTime(link1.getBridgeBridgeLinkCreateTime());
m_bridgeBridgeLinkDao.save(link1);
m_bridgeBridgeLinkDao.flush();
BridgeBridgeLink link3 = new BridgeBridgeLink();
link3.setNode(node1310);
link3.setBridgePort(1);
link3.setDesignatedNode(node1534);
link3.setDesignatedPort(1);
link3.setBridgeBridgeLinkLastPollTime(link3.getBridgeBridgeLinkCreateTime());
m_bridgeBridgeLinkDao.save(link3);
m_bridgeBridgeLinkDao.flush();
BridgeBridgeLink link4 = new BridgeBridgeLink();
link4.setNode(node1364);
link4.setBridgePort(24);
link4.setDesignatedNode(node1534);
link4.setDesignatedPort(4);
link4.setBridgeBridgeLinkLastPollTime(link4.getBridgeBridgeLinkCreateTime());
m_bridgeBridgeLinkDao.save(link4);
m_bridgeBridgeLinkDao.flush();
BridgeBridgeLink link2 = new BridgeBridgeLink();
link2.setNode(node1534);
link2.setBridgePort(11);
link2.setDesignatedNode(node1400);
link2.setDesignatedPort(11);
link2.setBridgeBridgeLinkLastPollTime(link2.getBridgeBridgeLinkCreateTime());
m_bridgeBridgeLinkDao.save(link2);
m_bridgeBridgeLinkDao.flush();
BridgeBridgeLink link6 = new BridgeBridgeLink();
link6.setNode(node2965);
link6.setBridgePort(2);
link6.setDesignatedNode(node1364);
link6.setDesignatedPort(5);
link6.setBridgeBridgeLinkLastPollTime(link6.getBridgeBridgeLinkCreateTime());
m_bridgeBridgeLinkDao.save(link6);
m_bridgeBridgeLinkDao.flush();
BridgeBridgeLink link5 = new BridgeBridgeLink();
link5.setNode(node405);
link5.setBridgePort(1);
link5.setDesignatedNode(node1534);
link5.setDesignatedPort(1);
link5.setBridgeBridgeLinkLastPollTime(link5.getBridgeBridgeLinkCreateTime());
m_bridgeBridgeLinkDao.save(link5);
m_bridgeBridgeLinkDao.flush();
assertEquals(0, m_bridgeMacLinkDao.countAll());
assertEquals(7, m_bridgeBridgeLinkDao.countAll());
assertNotNull(m_bridgeTopologyDao);
m_linkd.getQueryManager().loadBridgeTopology();
assertEquals(1, m_linkd.getQueryManager().getAllBroadcastDomains().size());
BroadcastDomain fourlevelDomain = m_linkd.getQueryManager().getAllBroadcastDomains().iterator().next();
assertEquals(8, fourlevelDomain.getBridgeNodesOnDomain().size());
System.err.println(fourlevelDomain.printTopology());
}
use of org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation in project opennms by OpenNMS.
the class AgentConfigurationResource method getResponses.
protected List<AgentResponse> getResponses(final String filterName, final String serviceName) throws ConfigurationResourceException {
LOG.debug("getAgentsForService(): filterName={}, serviceName={}", filterName, serviceName);
if (filterName == null || serviceName == null) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN).entity("You must specify a filter name and service name!").build());
}
final Filter filter = m_collectdConfigurationResource.get().getFilter(filterName);
if (filter == null) {
LOG.warn("No filter matching {} could be found.", filterName);
throw new WebApplicationException(Status.NOT_FOUND);
}
final List<InetAddress> addresses = m_filterDao.getActiveIPAddressList(filter.getContent());
LOG.debug("Matched {} IP addresses for filter {}", addresses == null ? 0 : addresses.size(), filterName);
if (addresses == null || addresses.size() == 0) {
return Collections.emptyList();
}
final CriteriaBuilder builder = new CriteriaBuilder(OnmsMonitoredService.class);
builder.createAlias("ipInterface", "iface");
builder.createAlias("serviceType", "type");
builder.createAlias("iface.node", "node");
builder.in("iface.ipAddress", addresses);
builder.eq("type.name", serviceName);
final List<OnmsMonitoredService> services = m_monitoredServiceDao.findMatching(builder.toCriteria());
int defaultPort = -1;
// TODO: We shouldn't have to hardcode like this; what's the right way to know the port to return?
final CollectdConfiguration collectdConfiguration = m_collectdConfigurationResource.get();
org.opennms.netmgt.config.collectd.Package pack = collectdConfiguration.getPackage(filterName);
if (pack == null) {
for (final org.opennms.netmgt.config.collectd.Package p : collectdConfiguration.getPackages()) {
if (filterName.equals(p.getFilter().getName())) {
pack = p;
break;
}
}
}
if (pack != null) {
final Service svc = pack.getService(serviceName);
final String port = svc.getParameter("port");
if (port != null) {
try {
defaultPort = Integer.valueOf(port);
} catch (final NumberFormatException e) {
LOG.debug("Unable to turn port {} from service {} into a number.", port, serviceName);
}
}
}
final List<AgentResponse> responses = new ArrayList<>();
for (final OnmsMonitoredService service : services) {
final InetAddress ipAddress = service.getIpAddress();
final OnmsIpInterface iface = service.getIpInterface();
OnmsNode node = null;
if (iface != null) {
node = iface.getNode();
}
final Map<String, String> parameters = new TreeMap<String, String>();
// all service parameters from collectd configuration to parameters map
for (Parameter eachParameter : pack.getService(serviceName).getParameters()) {
parameters.put(eachParameter.getKey(), eachParameter.getValue());
}
int port = defaultPort;
if ("SNMP".equals(serviceName)) {
final String sysObjectId = node == null ? null : node.getSysObjectId();
if (sysObjectId != null) {
parameters.put("sysObjectId", sysObjectId);
}
OnmsMonitoringLocation location = (node == null) ? null : node.getLocation();
String locationName = (location == null) ? null : location.getLocationName();
final SnmpAgentConfig config = m_agentConfigFactory.getAgentConfig(ipAddress, locationName);
if (config != null) {
port = config.getPort();
}
}
if (node != null) {
if (node.getNodeId() != null && !node.getNodeId().trim().isEmpty()) {
parameters.put("nodeId", node.getNodeId());
}
if (node.getForeignSource() != null && !node.getForeignSource().trim().isEmpty()) {
parameters.put("foreignSource", node.getForeignSource());
}
if (node.getForeignId() != null && !node.getForeignId().trim().isEmpty()) {
parameters.put("foreignId", node.getForeignId());
}
}
responses.add(new AgentResponse(ipAddress, port, service.getServiceName(), parameters));
}
return responses;
}
use of org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation in project opennms by OpenNMS.
the class NodeRestService method doCreate.
@Override
public Response doCreate(final SecurityContext securityContext, final UriInfo uriInfo, final OnmsNode object) {
if (object == null) {
throw getException(Status.BAD_REQUEST, "Node object cannot be null");
}
if (object.getLocation() == null) {
OnmsMonitoringLocation location = m_locationDao.getDefaultLocation();
LOG.debug("doCreate: Assigning new node to default location: {}", location.getLocationName());
object.setLocation(location);
}
// See NMS-9855
if (object.getAssetRecord() != null && object.getAssetRecord().getNode() == null) {
object.getAssetRecord().setNode(object);
}
final Integer id = getDao().save(object);
final Event e = EventUtils.createNodeAddedEvent("Rest", id, object.getLabel(), object.getLabelSource());
sendEvent(e);
return Response.created(RedirectHelper.getRedirectUri(uriInfo, id)).build();
}
Aggregations