Search in sources :

Example 1 with VmwareGroup

use of org.opennms.netmgt.config.vmware.vijava.VmwareGroup in project opennms by OpenNMS.

the class VmwareCollector method collect.

/**
     * This method collect the data for a given collection agent.
     *
     * @param agent      the collection agent
     * @param parameters the parameters map
     * @return the generated collection set
     * @throws CollectionException
     */
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
    final VmwareCollection collection = (VmwareCollection) parameters.get(VMWARE_COLLECTION_KEY);
    final String vmwareManagementServer = (String) parameters.get(VMWARE_MGMT_SERVER_KEY);
    final String vmwareManagedObjectId = (String) parameters.get(VMWARE_MGED_OBJECT_ID_KEY);
    final VmwareServer vmwareServer = (VmwareServer) parameters.get(VMWARE_SERVER_KEY);
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    builder.withStatus(CollectionStatus.FAILED);
    VmwareViJavaAccess vmwareViJavaAccess = new VmwareViJavaAccess(vmwareServer);
    int timeout = ParameterMap.getKeyedInteger(parameters, "timeout", -1);
    if (timeout > 0) {
        if (!vmwareViJavaAccess.setTimeout(timeout)) {
            logger.warn("Error setting connection timeout for VMware management server '{}'", vmwareManagementServer);
        }
    }
    if (collection.getVmwareGroup().length < 1) {
        logger.info("No groups to collect. Returning empty collection set.");
        builder.withStatus(CollectionStatus.SUCCEEDED);
        return builder.build();
    }
    try {
        vmwareViJavaAccess.connect();
    } catch (MalformedURLException e) {
        logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
        return builder.build();
    } catch (RemoteException e) {
        logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
        return builder.build();
    }
    ManagedEntity managedEntity = vmwareViJavaAccess.getManagedEntityByManagedObjectId(vmwareManagedObjectId);
    VmwarePerformanceValues vmwarePerformanceValues = null;
    try {
        vmwarePerformanceValues = vmwareViJavaAccess.queryPerformanceValues(managedEntity);
    } catch (RemoteException e) {
        logger.warn("Error retrieving performance values from VMware management server '" + vmwareManagementServer + "' for managed object '" + vmwareManagedObjectId + "'", e.getMessage());
        vmwareViJavaAccess.disconnect();
        return builder.build();
    }
    for (final VmwareGroup vmwareGroup : collection.getVmwareGroup()) {
        final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
        if ("node".equalsIgnoreCase(vmwareGroup.getResourceType())) {
            for (Attrib attrib : vmwareGroup.getAttrib()) {
                if (!vmwarePerformanceValues.hasSingleValue(attrib.getName())) {
                    // warning
                    logger.debug("Warning! No single value for '{}' defined as single instance attribute for node {}", attrib.getName(), agent.getNodeId());
                } else {
                    final Long value = vmwarePerformanceValues.getValue(attrib.getName());
                    logger.debug("Storing single instance value {}='{}' for node {}", attrib.getName(), value, agent.getNodeId());
                    final AttributeType type = attrib.getType();
                    if (type.isNumeric()) {
                        builder.withNumericAttribute(nodeResource, vmwareGroup.getName(), attrib.getAlias(), value, type);
                    } else {
                        builder.withStringAttribute(nodeResource, vmwareGroup.getName(), attrib.getAlias(), String.valueOf(value));
                    }
                }
            }
        } else {
            // multi instance value
            final Set<String> instanceSet = new TreeSet<>();
            final HashMap<String, Resource> resources = new HashMap<>();
            for (Attrib attrib : vmwareGroup.getAttrib()) {
                if (!vmwarePerformanceValues.hasInstances(attrib.getName())) {
                    // warning
                    logger.debug("Warning! No multi instance value for '{}' defined as multi instance attribute for node {}", attrib.getName(), agent.getNodeId());
                } else {
                    Set<String> newInstances = vmwarePerformanceValues.getInstances(attrib.getName());
                    for (String instance : newInstances) {
                        if (!instanceSet.contains(instance)) {
                            resources.put(instance, new DeferredGenericTypeResource(nodeResource, vmwareGroup.getResourceType(), instance));
                            instanceSet.add(instance);
                        }
                        final AttributeType type = attrib.getType();
                        final Long value = vmwarePerformanceValues.getValue(attrib.getName(), instance);
                        logger.debug("Storing multi instance value {}[{}='{}' for node {}", attrib.getName(), instance, value, agent.getNodeId());
                        if (type.isNumeric()) {
                            builder.withNumericAttribute(resources.get(instance), vmwareGroup.getName(), attrib.getAlias(), value, type);
                        } else {
                            builder.withStringAttribute(resources.get(instance), vmwareGroup.getName(), attrib.getAlias(), Long.toString(value));
                        }
                    }
                }
            }
            for (String instance : instanceSet) {
                logger.debug("Storing multi instance value {}[{}='{}' for node {}", vmwareGroup.getResourceType() + "Name", instance, instance, agent.getNodeId());
                builder.withStringAttribute(resources.get(instance), vmwareGroup.getName(), vmwareGroup.getResourceType() + "Name", instance);
            }
        }
    }
    builder.withStatus(CollectionStatus.SUCCEEDED);
    vmwareViJavaAccess.disconnect();
    return builder.build();
}
Also used : ManagedEntity(com.vmware.vim25.mo.ManagedEntity) MalformedURLException(java.net.MalformedURLException) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) HashMap(java.util.HashMap) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Attrib(org.opennms.netmgt.config.vmware.vijava.Attrib) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) VmwareGroup(org.opennms.netmgt.config.vmware.vijava.VmwareGroup) VmwareServer(org.opennms.netmgt.config.vmware.VmwareServer) AttributeType(org.opennms.netmgt.collection.api.AttributeType) TreeSet(java.util.TreeSet) VmwareCollection(org.opennms.netmgt.config.vmware.vijava.VmwareCollection) VmwarePerformanceValues(org.opennms.netmgt.collectd.vmware.vijava.VmwarePerformanceValues) RemoteException(java.rmi.RemoteException) VmwareViJavaAccess(org.opennms.protocols.vmware.VmwareViJavaAccess)

Aggregations

ManagedEntity (com.vmware.vim25.mo.ManagedEntity)1 MalformedURLException (java.net.MalformedURLException)1 RemoteException (java.rmi.RemoteException)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 VmwarePerformanceValues (org.opennms.netmgt.collectd.vmware.vijava.VmwarePerformanceValues)1 AttributeType (org.opennms.netmgt.collection.api.AttributeType)1 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)1 DeferredGenericTypeResource (org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource)1 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)1 Resource (org.opennms.netmgt.collection.support.builder.Resource)1 VmwareServer (org.opennms.netmgt.config.vmware.VmwareServer)1 Attrib (org.opennms.netmgt.config.vmware.vijava.Attrib)1 VmwareCollection (org.opennms.netmgt.config.vmware.vijava.VmwareCollection)1 VmwareGroup (org.opennms.netmgt.config.vmware.vijava.VmwareGroup)1 VmwareViJavaAccess (org.opennms.protocols.vmware.VmwareViJavaAccess)1