use of org.opennms.netmgt.provision.persist.requisition.Requisition in project opennms by OpenNMS.
the class VmwareImporter method iterateHostSystems.
/**
* Iterates through the host systems and adds them to the requisition object.
*
* @param vmwareViJavaAccess the access/connection to use
*/
private void iterateHostSystems(VmwareViJavaAccess vmwareViJavaAccess, int apiVersion) {
ManagedEntity[] managedEntities = null;
logger.debug("Starting to iterate Host Systems on VMware host {} ...", request.getHostname());
try {
managedEntities = vmwareViJavaAccess.searchManagedEntities("HostSystem");
} catch (RemoteException e) {
logger.error("Error iterating Host Systems on VMware host {}", request.getHostname());
logger.error("Exception thrown while iterating for Host Systems: ", e);
}
if (managedEntities != null) {
Arrays.stream(managedEntities).map(m -> (HostSystem) m).forEach(hostSystem -> {
synchronized (m_hostSystemMap) {
m_hostSystemMap.put(hostSystem.getMOR().getVal(), hostSystem.getName());
}
});
final ExecutorService executor = Executors.newFixedThreadPool(Math.min(managedEntities.length, 10));
final List<CompletableFuture<Optional<RequisitionNode>>> completableFutures = Arrays.stream(managedEntities).map(managedEntity -> (HostSystem) managedEntity).filter(hostSystem -> checkHostPowerState(hostSystem)).filter(hostSystem -> checkForAttribute(hostSystem)).map(hostSystem -> CompletableFuture.supplyAsync(() -> {
logger.debug("Adding Host System '{}' (ID: {})", hostSystem.getName(), hostSystem.getMOR().getVal());
TreeSet<String> ipAddresses = vmwareViJavaAccess.getHostSystemIpAddresses(hostSystem);
logger.debug("Found {} IP addresses for Host System '{}' (ID: {}): {}", ipAddresses.size(), hostSystem.getName(), hostSystem.getMOR().getVal(), ipAddresses);
RequisitionNode node = createRequisitionNode(ipAddresses, hostSystem, apiVersion, vmwareViJavaAccess);
try {
node.putAsset(new RequisitionAsset("cpu", hostSystem.getHardware().getCpuInfo().getNumCpuCores() + " cores"));
} catch (Exception e) {
logger.debug("Can't find CPU information for '{}' (ID: {})", hostSystem.getName(), hostSystem.getMOR().getVal());
}
try {
node.putAsset(new RequisitionAsset("ram", Math.round(hostSystem.getHardware().getMemorySize() / 1000000f) + " MB"));
} catch (Exception e) {
logger.debug("Can't find Memory information for '{}' (ID: {})", hostSystem.getName(), hostSystem.getMOR().getVal());
}
return request.isPersistHosts() ? Optional.of(node) : Optional.<RequisitionNode>empty();
}, executor)).collect(Collectors.toList());
completableFutures.stream().map(CompletableFuture::join).filter(Optional::isPresent).forEach(m -> {
synchronized (m_requisition) {
m_requisition.insertNode(m.get());
}
});
executor.shutdown();
}
}
use of org.opennms.netmgt.provision.persist.requisition.Requisition in project opennms by OpenNMS.
the class VmwareImporter method iterateVirtualMachines.
/**
* Iterates through the virtual machines and adds them to the requisition object.
*
* @param vmwareViJavaAccess the access/connection to use
*/
private void iterateVirtualMachines(VmwareViJavaAccess vmwareViJavaAccess, int apiVersion) {
ManagedEntity[] managedEntities = null;
logger.debug("Starting to iterate Virtual Machines on VMware host {} ...", request.getHostname());
try {
managedEntities = vmwareViJavaAccess.searchManagedEntities("VirtualMachine");
} catch (RemoteException e) {
logger.error("Error iterating Virtual Machines on VMware host {}", request.getHostname());
logger.error("Exception thrown while iterating for Virtual Machines: ", e);
}
if (managedEntities != null) {
final ExecutorService executor = Executors.newFixedThreadPool(Math.min(managedEntities.length, 10));
final List<CompletableFuture<Optional<RequisitionNode>>> completableFutures = Arrays.stream(managedEntities).map(managedEntity -> (VirtualMachine) managedEntity).filter(virtualMachine -> checkVMPowerState(virtualMachine)).filter(virtualMachine -> checkForAttribute(virtualMachine)).map(virtualMachine -> CompletableFuture.supplyAsync(() -> {
logger.debug("Adding Virtual Machine '{}' (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal());
TreeSet<String> ipAddresses = vmwareViJavaAccess.getVirtualMachineIpAddresses(virtualMachine);
logger.debug("Found {} IP addresses for Virtual Machine '{}' (ID: {}): {}", ipAddresses.size(), virtualMachine.getName(), virtualMachine.getMOR().getVal(), ipAddresses);
RequisitionNode node = createRequisitionNode(ipAddresses, virtualMachine, apiVersion, vmwareViJavaAccess);
if (virtualMachine.getGuest().getGuestFullName() != null) {
node.putAsset(new RequisitionAsset("operatingSystem", virtualMachine.getGuest().getGuestFullName()));
}
try {
node.putAsset(new RequisitionAsset("cpu", virtualMachine.getConfig().getHardware().getNumCPU() + " vCPU"));
} catch (Exception e) {
logger.debug("Can't find CPU information for {} (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal());
}
try {
node.putAsset(new RequisitionAsset("ram", virtualMachine.getConfig().getHardware().getMemoryMB() + " MB"));
} catch (Exception e) {
logger.debug("Can't find Memory information for {} (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal());
}
return request.isPersistVMs() ? Optional.of(node) : Optional.<RequisitionNode>empty();
}, executor)).collect(Collectors.toList());
completableFutures.stream().map(CompletableFuture::join).filter(Optional::isPresent).forEach(m -> {
synchronized (m_requisition) {
m_requisition.insertNode(m.get());
}
});
executor.shutdown();
}
}
use of org.opennms.netmgt.provision.persist.requisition.Requisition 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.provision.persist.requisition.Requisition in project opennms by OpenNMS.
the class DefaultProvisionService method loadRequisition.
/* (non-Javadoc)
* @see org.opennms.netmgt.provision.service.ProvisionService#loadRequisition(java.lang.String, org.springframework.core.io.Resource)
*/
/**
* {@inheritDoc}
*/
@Override
public Requisition loadRequisition(final Resource resource) {
final Requisition r = m_foreignSourceRepository.importResourceRequisition(resource);
r.updateLastImported();
m_foreignSourceRepository.save(r);
m_foreignSourceRepository.flush();
return r;
}
use of org.opennms.netmgt.provision.persist.requisition.Requisition in project opennms by OpenNMS.
the class FilesystemForeignSourceRepository method getRequisitions.
/**
* <p>getRequisitions</p>
*
* @return a {@link java.util.Set} object.
* @throws org.opennms.netmgt.provision.persist.ForeignSourceRepositoryException if any.
*/
@Override
public Set<Requisition> getRequisitions() throws ForeignSourceRepositoryException {
m_readLock.lock();
try {
final File directory = new File(m_requisitionPath);
final TreeSet<Requisition> requisitions = new TreeSet<>();
if (directory.exists()) {
for (final File file : directory.listFiles()) {
if (file.getName().endsWith(".xml")) {
try {
requisitions.add(RequisitionFileUtils.getRequisitionFromFile(file));
} catch (ForeignSourceRepositoryException e) {
// race condition, probably got deleted by the importer as part of moving things
// need a better way to handle this; move "pending" to the database?
}
}
}
}
return requisitions;
} finally {
m_readLock.unlock();
}
}
Aggregations