Search in sources :

Example 6 with VmwareViJavaAccess

use of org.opennms.protocols.vmware.VmwareViJavaAccess 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;
}
Also used : MalformedURLException(java.net.MalformedURLException) CIMObject(org.sblim.wbem.cim.CIMObject) PollStatus(org.opennms.netmgt.poller.PollStatus) HostRuntimeInfo(com.vmware.vim25.HostRuntimeInfo) HostSystemPowerState(com.vmware.vim25.HostSystemPowerState) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) HostSystem(com.vmware.vim25.mo.HostSystem) RemoteException(java.rmi.RemoteException) VmwareViJavaAccess(org.opennms.protocols.vmware.VmwareViJavaAccess)

Aggregations

RemoteException (java.rmi.RemoteException)6 VmwareViJavaAccess (org.opennms.protocols.vmware.VmwareViJavaAccess)6 MalformedURLException (java.net.MalformedURLException)5 HostSystem (com.vmware.vim25.mo.HostSystem)4 HostRuntimeInfo (com.vmware.vim25.HostRuntimeInfo)3 HostSystemPowerState (com.vmware.vim25.HostSystemPowerState)3 CIMObject (org.sblim.wbem.cim.CIMObject)3 ManagedEntity (com.vmware.vim25.mo.ManagedEntity)2 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)2 HashMap (java.util.HashMap)2 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)2 AttributeType (org.opennms.netmgt.collection.api.AttributeType)2 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)2 DeferredGenericTypeResource (org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource)2 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)2 Resource (org.opennms.netmgt.collection.support.builder.Resource)2 VmwareServer (org.opennms.netmgt.config.vmware.VmwareServer)2 PollStatus (org.opennms.netmgt.poller.PollStatus)2 AboutInfo (com.vmware.vim25.AboutInfo)1 ElementDescription (com.vmware.vim25.ElementDescription)1