use of org.opennms.netmgt.config.vmware.VmwareServer in project opennms by OpenNMS.
the class VmwareCimCollector method getRuntimeAttributes.
@Override
public Map<String, Object> getRuntimeAttributes(CollectionAgent agent, Map<String, Object> parameters) {
final Map<String, Object> runtimeAttributes = new HashMap<>();
final OnmsNode onmsNode = m_nodeDao.get(agent.getNodeId());
if (onmsNode == null) {
throw new IllegalArgumentException(String.format("VmwareCollector: No node found with id: %d", agent.getNodeId()));
}
// retrieve the assets
final String vmwareManagementServer = onmsNode.getAssetRecord().getVmwareManagementServer();
if (Strings.isNullOrEmpty(vmwareManagementServer)) {
throw new IllegalArgumentException(String.format("VmwareCollector: No management server is set on node with id %d.", onmsNode.getId()));
}
runtimeAttributes.put(VMWARE_MGMT_SERVER_KEY, vmwareManagementServer);
final String vmwareManagedObjectId = onmsNode.getForeignId();
if (Strings.isNullOrEmpty(vmwareManagedObjectId)) {
throw new IllegalArgumentException(String.format("VmwareCollector: No foreign id is set on node with id %d.", onmsNode.getId()));
}
runtimeAttributes.put(VMWARE_MGED_OBJECT_ID_KEY, vmwareManagedObjectId);
// retrieve the collection
final String collectionName = ParameterMap.getKeyedString(parameters, "collection", ParameterMap.getKeyedString(parameters, "vmware-collection", null));
final VmwareCimCollection collection = m_vmwareCimDatacollectionConfigDao.getVmwareCimCollection(collectionName);
if (collection == null) {
throw new IllegalArgumentException(String.format("VmwareCollector: No collection found with name '%s'.", collectionName));
}
runtimeAttributes.put(VMWARE_COLLECTION_KEY, collection);
// retrieve the server configuration
final Map<String, VmwareServer> serverMap = m_vmwareConfigDao.getServerMap();
if (serverMap == null) {
throw new IllegalStateException(String.format("VmwareCollector: Error getting vmware-config.xml's server map."));
}
final VmwareServer vmwareServer = serverMap.get(vmwareManagementServer);
if (vmwareServer == null) {
throw new IllegalStateException(String.format("VmwareCollector: Error getting credentials for VMware management server: %s", vmwareManagementServer));
}
runtimeAttributes.put(VMWARE_SERVER_KEY, vmwareServer);
return runtimeAttributes;
}
use of org.opennms.netmgt.config.vmware.VmwareServer in project opennms by OpenNMS.
the class AbstractVmwareMonitor method getRuntimeAttributes.
@Override
public Map<String, Object> getRuntimeAttributes(MonitoredService svc, Map<String, Object> parameters) {
if (m_nodeDao == null) {
m_nodeDao = BeanUtils.getBean("daoContext", "nodeDao", NodeDao.class);
}
if (m_vmwareConfigDao == null) {
m_vmwareConfigDao = BeanUtils.getBean("daoContext", "vmwareConfigDao", VmwareConfigDao.class);
}
final OnmsNode onmsNode = m_nodeDao.get(svc.getNodeId());
if (onmsNode == null) {
throw new IllegalArgumentException("No node found with ID: " + svc.getNodeId());
}
// retrieve the assets
final String vmwareManagementServer = onmsNode.getAssetRecord().getVmwareManagementServer();
final String vmwareManagedEntityType = onmsNode.getAssetRecord().getVmwareManagedEntityType();
final String vmwareManagedObjectId = onmsNode.getForeignId();
String vmwareMangementServerUsername = null;
String vmwareMangementServerPassword = null;
final Map<String, VmwareServer> serverMap = m_vmwareConfigDao.getServerMap();
if (serverMap == null) {
logger.error("Error getting vmware-config.xml's server map.");
} else {
final VmwareServer vmwareServer = serverMap.get(vmwareManagementServer);
if (vmwareServer == null) {
logger.error("Error getting credentials for VMware management server '{}'.", vmwareManagementServer);
} else {
vmwareMangementServerUsername = vmwareServer.getUsername();
vmwareMangementServerPassword = vmwareServer.getPassword();
}
}
return new ImmutableMap.Builder<String, Object>().put(VMWARE_MANAGEMENT_SERVER_KEY, vmwareManagementServer).put(VMWARE_MANAGED_ENTITY_TYPE_KEY, vmwareManagedEntityType).put(VMWARE_MANAGED_OBJECT_ID_KEY, vmwareManagedObjectId).put(VMWARE_MANAGEMENT_SERVER_USERNAME_KEY, vmwareMangementServerUsername).put(VMWARE_MANAGEMENT_SERVER_PASSWORD_KEY, vmwareMangementServerPassword).build();
}
use of org.opennms.netmgt.config.vmware.VmwareServer in project opennms by OpenNMS.
the class VmwareCimCollectorComplianceTest method getRequiredBeans.
@Override
public Map<String, Object> getRequiredBeans() {
OnmsNode node = mock(OnmsNode.class, RETURNS_DEEP_STUBS);
NodeDao nodeDao = mock(NodeDao.class);
when(nodeDao.get(anyInt())).thenReturn(node);
when(node.getAssetRecord().getVmwareManagementServer()).thenReturn("mdx");
when(node.getAssetRecord().getVmwareManagedEntityType()).thenReturn("tsx");
when(node.getForeignId()).thenReturn("rsx");
VmwareCimCollection collection = new VmwareCimCollection();
VmwareCimDatacollectionConfigDao vmwareCimDatacollectionConfigDao = mock(VmwareCimDatacollectionConfigDao.class);
when(vmwareCimDatacollectionConfigDao.getVmwareCimCollection(COLLECTION)).thenReturn(collection);
when(vmwareCimDatacollectionConfigDao.getRrdRepository(COLLECTION)).thenReturn(new RrdRepository());
VmwareServer vmwareServer = new VmwareServer();
vmwareServer.setHostname(InetAddrUtils.getLocalHostAddress().getCanonicalHostName());
Map<String, VmwareServer> serverMap = new ImmutableMap.Builder<String, VmwareServer>().put("mdx", vmwareServer).build();
VmwareConfigDao vmwareConfigDao = mock(VmwareConfigDao.class);
when(vmwareConfigDao.getServerMap()).thenReturn(serverMap);
return new ImmutableMap.Builder<String, Object>().put("nodeDao", nodeDao).put("vmwareCimDatacollectionConfigDao", vmwareCimDatacollectionConfigDao).put("vmwareConfigDao", vmwareConfigDao).build();
}
use of org.opennms.netmgt.config.vmware.VmwareServer in project opennms by OpenNMS.
the class VmwareRequisitionTool method main.
public static void main(String[] args) throws Exception {
final Options options = new Options();
final CommandLineParser parser = new PosixParser();
final CommandLine cmd = parser.parse(options, args);
@SuppressWarnings("unchecked") List<String> arguments = (List<String>) cmd.getArgList();
if (arguments.size() < 1) {
usage(options, cmd);
System.exit(1);
}
// Internal trick to avoid confusions.
String urlString = arguments.remove(0).replaceFirst("vmware", "http");
URL url = new URL(urlString);
// Parse vmware-config.xml and retrieve the credentials to avoid initialize Spring
if (!url.getQuery().contains("username") && url.getUserInfo() == null) {
File cfg = new File(ConfigFileConstants.getFilePathString(), "vmware-config.xml");
if (cfg.exists()) {
String username = null;
String password = null;
VmwareConfig config = JaxbUtils.unmarshal(VmwareConfig.class, cfg);
for (VmwareServer srv : config.getVmwareServerCollection()) {
if (srv.getHostname().equals(url.getHost())) {
username = srv.getUsername();
password = srv.getPassword();
}
}
if (username == null || password == null) {
throw new IllegalArgumentException("Can't retrieve credentials for " + url.getHost() + " from " + cfg);
}
// Add credentials to URL
urlString = urlString + ";username=" + username + ";password=" + password;
url = new URL(urlString);
}
}
VmwareRequisitionUrlConnection c = new VmwareRequisitionUrlConnection(url) {
@Override
protected Requisition getExistingRequisition(String foreignSource) {
// This is not elegant but it is necessary to avoid booting Spring
File req = new File(ConfigFileConstants.getFilePathString(), "imports" + File.separator + foreignSource + ".xml");
if (req.exists()) {
return JaxbUtils.unmarshal(Requisition.class, req);
}
return null;
}
};
c.connect();
InputStream is = c.getInputStream();
if (is == null) {
System.err.println("Couldn't generate requisition from " + urlString);
System.exit(1);
} else {
System.out.println(IOUtils.toString(is, StandardCharsets.UTF_8));
}
}
Aggregations