Search in sources :

Example 16 with HostSystem

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;
}
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)

Example 17 with HostSystem

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;
}
Also used : HostSystem(com.vmware.vim25.mo.HostSystem) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 18 with 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;
}
Also used : CIMObject(org.sblim.wbem.cim.CIMObject) CIMClient(org.sblim.wbem.client.CIMClient) ArrayList(java.util.ArrayList) HostServiceTicket(com.vmware.vim25.HostServiceTicket) PasswordCredential(org.sblim.wbem.client.PasswordCredential) CIMObjectPath(org.sblim.wbem.cim.CIMObjectPath) CIMNameSpace(org.sblim.wbem.cim.CIMNameSpace) UserPrincipal(org.sblim.wbem.client.UserPrincipal)

Example 19 with HostSystem

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;
}
Also used : HostNetworkInfo(com.vmware.vim25.HostNetworkInfo) HostVirtualNic(com.vmware.vim25.HostVirtualNic) TreeSet(java.util.TreeSet) HostNetworkSystem(com.vmware.vim25.mo.HostNetworkSystem)

Example 20 with HostSystem

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();
}
Also used : ManagedEntity(com.vmware.vim25.mo.ManagedEntity) MalformedURLException(java.net.MalformedURLException) CIMObject(org.sblim.wbem.cim.CIMObject) HostSystem(com.vmware.vim25.mo.HostSystem) RemoteException(java.rmi.RemoteException) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException)

Aggregations

TraversalSpec (com.vmware.vim25.TraversalSpec)13 ObjectSpec (com.vmware.vim25.ObjectSpec)12 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)12 PropertySpec (com.vmware.vim25.PropertySpec)12 ObjectContent (com.vmware.vim25.ObjectContent)10 ArrayList (java.util.ArrayList)10 HostSystem (com.vmware.vim25.mo.HostSystem)9 Gson (com.google.gson.Gson)8 RemoteException (java.rmi.RemoteException)8 MalformedURLException (java.net.MalformedURLException)6 ManagedEntity (com.vmware.vim25.mo.ManagedEntity)5 CIMObject (org.sblim.wbem.cim.CIMObject)5 HostRuntimeInfo (com.vmware.vim25.HostRuntimeInfo)4 HostSystemPowerState (com.vmware.vim25.HostSystemPowerState)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 VmwareViJavaAccess (org.opennms.protocols.vmware.VmwareViJavaAccess)4 SelectionSpec (com.vmware.vim25.SelectionSpec)3 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)3 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)2 AboutInfo (com.vmware.vim25.AboutInfo)2