use of com.vmware.vim25.mo.HostSystem in project opennms by OpenNMS.
the class VmwareCimMonitor method poll.
/**
* This method queries the Vmware hypervisor for sensor data.
*
* @param svc the monitored service
* @param parameters the parameter map
* @return the poll status for this system
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
final boolean ignoreStandBy = getKeyedBoolean(parameters, "ignoreStandBy", false);
final String vmwareManagementServer = getKeyedString(parameters, VMWARE_MANAGEMENT_SERVER_KEY, null);
final String vmwareManagedObjectId = getKeyedString(parameters, VMWARE_MANAGED_OBJECT_ID_KEY, null);
final String vmwareMangementServerUsername = getKeyedString(parameters, VMWARE_MANAGEMENT_SERVER_USERNAME_KEY, null);
final String vmwareMangementServerPassword = getKeyedString(parameters, VMWARE_MANAGEMENT_SERVER_PASSWORD_KEY, null);
TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
PollStatus serviceStatus = PollStatus.unknown();
for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
final VmwareViJavaAccess vmwareViJavaAccess = new VmwareViJavaAccess(vmwareManagementServer, vmwareMangementServerUsername, vmwareMangementServerPassword);
try {
vmwareViJavaAccess.connect();
} catch (MalformedURLException e) {
logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
return PollStatus.unavailable("Error connecting VMware management server '" + vmwareManagementServer + "'");
} catch (RemoteException e) {
logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
return PollStatus.unavailable("Error connecting VMware management server '" + vmwareManagementServer + "'");
}
if (!vmwareViJavaAccess.setTimeout(tracker.getConnectionTimeout())) {
logger.warn("Error setting connection timeout for VMware management server '{}'", vmwareManagementServer);
}
HostSystem hostSystem = vmwareViJavaAccess.getHostSystemByManagedObjectId(vmwareManagedObjectId);
String powerState = null;
if (hostSystem == null) {
return PollStatus.unknown("hostSystem=null");
} else {
HostRuntimeInfo hostRuntimeInfo = hostSystem.getRuntime();
if (hostRuntimeInfo == null) {
return PollStatus.unknown("hostRuntimeInfo=null");
} else {
HostSystemPowerState hostSystemPowerState = hostRuntimeInfo.getPowerState();
if (hostSystemPowerState == null) {
return PollStatus.unknown("hostSystemPowerState=null");
} else {
powerState = hostSystemPowerState.toString();
}
}
}
if ("poweredOn".equals(powerState)) {
List<CIMObject> cimObjects = null;
try {
cimObjects = vmwareViJavaAccess.queryCimObjects(hostSystem, "CIM_NumericSensor", svc.getIpAddr());
} catch (Exception e) {
logger.warn("Error retrieving CIM values from host system '{}'", vmwareManagedObjectId, e.getMessage());
vmwareViJavaAccess.disconnect();
return PollStatus.unavailable("Error retrieving cim values from host system '" + vmwareManagedObjectId + "'");
}
boolean success = true;
StringBuffer reason = new StringBuffer("VMware CIM query returned: ");
for (CIMObject cimObject : cimObjects) {
String healthState = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, "HealthState");
String cimObjectName = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, "Name");
if (healthState != null) {
int healthStateInt = Integer.valueOf(healthState).intValue();
if (healthStateInt != 5) {
if (!success) {
reason.append(", ");
}
success = false;
reason.append(cimObjectName + " ");
if (m_healthStates.containsKey(healthStateInt)) {
reason.append("(" + m_healthStates.get(healthStateInt) + ")");
} else {
reason.append("(" + healthStateInt + ")");
}
}
}
}
if (success) {
serviceStatus = PollStatus.available();
} else {
serviceStatus = PollStatus.unavailable(reason.toString());
}
} else {
if (ignoreStandBy && "standBy".equals(powerState)) {
serviceStatus = PollStatus.up();
} else {
serviceStatus = PollStatus.unresponsive("Host system's power state is '" + powerState + "'");
}
}
vmwareViJavaAccess.disconnect();
}
return serviceStatus;
}
use of com.vmware.vim25.mo.HostSystem in project opennms by OpenNMS.
the class VmwareViJavaAccess method getHostSystemByManagedObjectId.
/**
* Returns a host system by a given managed object Id.
*
* @param managedObjectId the managed object Id
* @return the host system object
*/
public HostSystem getHostSystemByManagedObjectId(String managedObjectId) {
ManagedObjectReference managedObjectReference = new ManagedObjectReference();
managedObjectReference.setType("HostSystem");
managedObjectReference.setVal(managedObjectId);
HostSystem hostSystem = (HostSystem) MorUtil.createExactManagedEntity(m_serviceInstance.getServerConnection(), managedObjectReference);
return hostSystem;
}
use of com.vmware.vim25.mo.HostSystem in project opennms by OpenNMS.
the class VmwareViJavaAccess method queryCimObjects.
/**
* Queries a host system for Cim data.
*
* @param hostSystem the host system to query
* @param cimClass the class of Cim objects to retrieve
* @param primaryIpAddress the Ip address to use
* @return the list of Cim objects
* @throws RemoteException
* @throws CIMException
*/
public List<CIMObject> queryCimObjects(HostSystem hostSystem, String cimClass, String primaryIpAddress) throws ConnectException, RemoteException, CIMException {
List<CIMObject> cimObjects = new ArrayList<CIMObject>();
if (!m_hostServiceTickets.containsKey(hostSystem)) {
m_hostServiceTickets.put(hostSystem, hostSystem.acquireCimServicesTicket());
}
HostServiceTicket hostServiceTicket = m_hostServiceTickets.get(hostSystem);
if (!m_hostSystemCimUrls.containsKey(hostSystem)) {
String ipAddress = primaryIpAddress;
if (ipAddress == null) {
ipAddress = getPrimaryHostSystemIpAddress(hostSystem);
}
if (ipAddress == null) {
logger.warn("Cannot determine ip address for host system '{}'", hostSystem.getMOR().getVal());
return cimObjects;
}
m_hostSystemCimUrls.put(hostSystem, "https://" + ipAddress + ":5989");
}
String cimAgentAddress = m_hostSystemCimUrls.get(hostSystem);
String namespace = "root/cimv2";
UserPrincipal userPr = new UserPrincipal(hostServiceTicket.getSessionId());
PasswordCredential pwCred = new PasswordCredential(hostServiceTicket.getSessionId().toCharArray());
CIMNameSpace ns = new CIMNameSpace(cimAgentAddress, namespace);
CIMClient cimClient = new CIMClient(ns, userPr, pwCred);
// very important to query esx5 hosts
cimClient.useMPost(false);
CIMObjectPath rpCOP = new CIMObjectPath(cimClass);
Enumeration<?> rpEnm = cimClient.enumerateInstances(rpCOP);
while (rpEnm.hasMoreElements()) {
CIMObject rp = (CIMObject) rpEnm.nextElement();
cimObjects.add(rp);
}
return cimObjects;
}
use of com.vmware.vim25.mo.HostSystem in project opennms by OpenNMS.
the class VmwareViJavaAccess method getHostSystemIpAddresses.
/**
* Searches for all ip addresses of a host system
*
* @param hostSystem the host system to query
* @return the ip addresses of the host system, the first one is the primary
* @throws RemoteException
*/
public TreeSet<String> getHostSystemIpAddresses(HostSystem hostSystem) throws RemoteException {
TreeSet<String> ipAddresses = new TreeSet<String>();
HostNetworkSystem hostNetworkSystem = hostSystem.getHostNetworkSystem();
if (hostNetworkSystem != null) {
HostNetworkInfo hostNetworkInfo = hostNetworkSystem.getNetworkInfo();
if (hostNetworkInfo != null) {
HostVirtualNic[] hostVirtualNics = hostNetworkInfo.getConsoleVnic();
if (hostVirtualNics != null) {
for (HostVirtualNic hostVirtualNic : hostVirtualNics) {
ipAddresses.add(hostVirtualNic.getSpec().getIp().getIpAddress());
}
}
hostVirtualNics = hostNetworkInfo.getVnic();
if (hostVirtualNics != null) {
for (HostVirtualNic hostVirtualNic : hostVirtualNics) {
ipAddresses.add(hostVirtualNic.getSpec().getIp().getIpAddress());
}
}
}
}
return ipAddresses;
}
use of com.vmware.vim25.mo.HostSystem in project opennms by OpenNMS.
the class VmwareCimQuery method cimQuery.
private static void cimQuery(String hostname, String username, String password) {
System.out.print("Trying to connect to " + hostname + "... ");
VmwareViJavaAccess vmwareViJavaAccess = new VmwareViJavaAccess(hostname, username, password);
try {
vmwareViJavaAccess.connect();
} catch (MalformedURLException e) {
System.out.println("Exception:");
e.printStackTrace();
return;
} catch (RemoteException e) {
System.out.println("Exception:");
e.printStackTrace();
return;
}
System.out.println("Success!");
ManagedEntity[] hostSystems;
System.out.print(" Querying " + hostname + " for host systems... ");
try {
hostSystems = vmwareViJavaAccess.searchManagedEntities("HostSystem");
} catch (RemoteException remoteException) {
remoteException.printStackTrace();
vmwareViJavaAccess.disconnect();
return;
}
if (hostSystems != null) {
System.out.println(hostSystems.length + " host system(s) found!");
for (ManagedEntity managedEntity : hostSystems) {
HostSystem hostSystem = (HostSystem) managedEntity;
if (hostSystem.getSummary() != null) {
if (hostSystem.getRuntime() != null) {
String powerState = hostSystem.getRuntime().getPowerState().toString();
if (!"poweredOn".equals(powerState)) {
System.out.println(" Ignoring host system " + hostSystem.getName() + " (powerState=" + powerState + ")... ");
continue;
} else {
System.out.print(" Determining primary Ip address of host system " + hostSystem.getName() + " (powerState=" + powerState + ")... ");
}
} else {
System.out.println(" Ignoring host system " + hostSystem.getName() + " (powerState=unknown)... ");
continue;
}
} else {
System.out.println(" Ignoring host system " + hostSystem.getName() + " (powerState=unknown)... ");
continue;
}
String ipAddress;
try {
ipAddress = vmwareViJavaAccess.getPrimaryHostSystemIpAddress(hostSystem);
} catch (RemoteException remoteException) {
System.out.println("Exception:");
remoteException.printStackTrace();
continue;
}
if (ipAddress != null) {
System.out.print(ipAddress + "\n Querying host system " + hostSystem.getName() + " for numeric sensors... ");
List<CIMObject> cimObjects;
try {
cimObjects = vmwareViJavaAccess.queryCimObjects(hostSystem, "CIM_NumericSensor");
} catch (Exception e) {
System.out.println("Exception:");
e.printStackTrace();
continue;
}
if (cimObjects != null) {
// FIXME queryCimObjects returns an empty list or a filled list, but never null
System.out.println(cimObjects.size() + " sensor(s) found!");
for (CIMObject cimObject : cimObjects) {
String healthState = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, "HealthState");
String cimObjectName = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, "Name");
System.out.print(" " + cimObjectName);
if (healthState != null) {
System.out.println(" " + m_healthStates.get(Integer.valueOf(healthState)));
} else {
System.out.println();
}
}
} else {
System.out.println("NULL - aborting...");
continue;
}
} else {
System.out.println("NULL - aborting...");
continue;
}
}
} else {
System.out.println("NULL - aborting...");
}
System.out.println("Cleaning up...");
vmwareViJavaAccess.disconnect();
}
Aggregations