Search in sources :

Example 1 with Resource

use of org.opennms.netmgt.collection.support.builder.Resource in project opennms by OpenNMS.

the class HttpCollectionHandler method fillCollectionSet.

@Override
protected void fillCollectionSet(String urlString, Request request, CollectionAgent agent, CollectionSetBuilder builder, XmlSource source) throws Exception {
    Document doc = getJsoupDocument(urlString, request);
    for (XmlGroup group : source.getXmlGroups()) {
        LOG.debug("fillCollectionSet: getting resources for XML group {} using selector {}", group.getName(), group.getResourceXpath());
        Date timestamp = getTimeStamp(doc, group);
        Elements elements = doc.select(group.getResourceXpath());
        LOG.debug("fillCollectionSet: {} => {}", group.getResourceXpath(), elements);
        String resourceName = getResourceName(elements, group);
        LOG.debug("fillCollectionSet: processing XML resource {}", resourceName);
        final Resource collectionResource = getCollectionResource(agent, resourceName, group.getResourceType(), timestamp);
        LOG.debug("fillCollectionSet: processing resource {}", collectionResource);
        for (XmlObject object : group.getXmlObjects()) {
            Elements el = elements.select(object.getXpath());
            if (el == null) {
                LOG.info("No value found for object named '{}'. Skipping.", object.getName());
            }
            builder.withAttribute(collectionResource, group.getName(), object.getName(), el.html(), object.getDataType());
        }
        processXmlResource(builder, collectionResource, resourceName, group.getName());
    }
}
Also used : XmlGroup(org.opennms.protocols.xml.config.XmlGroup) Resource(org.opennms.netmgt.collection.support.builder.Resource) XmlObject(org.opennms.protocols.xml.config.XmlObject) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Date(java.util.Date)

Example 2 with Resource

use of org.opennms.netmgt.collection.support.builder.Resource in project opennms by OpenNMS.

the class WsManCollector method collectGroupUsing.

private void collectGroupUsing(Group group, CollectionAgent agent, WSManClient client, int retries, CollectionSetBuilder builder) throws CollectionException {
    // Determine the appropriate resource type
    final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
    final AtomicInteger instanceId = new AtomicInteger();
    Supplier<Resource> resourceSupplier = () -> nodeResource;
    if (!"node".equalsIgnoreCase(group.getResourceType())) {
        resourceSupplier = () -> {
            // Generate a unique instance for each node in each group to ensure
            // that the attributes are grouped together properly.
            // Since these instances have no real meaning, a storage strategy
            // similar to the SiblingColumnStorageStrategy should be used instead
            // of the IndexStorageStrategy.
            final String instance = String.format("%s%d", group.getName(), instanceId.getAndIncrement());
            return new DeferredGenericTypeResource(nodeResource, group.getResourceType(), instance);
        };
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Using resource {} for group named {}", resourceSupplier.get(), group.getName());
    }
    // Enumerate
    List<Node> nodes = Lists.newLinkedList();
    RetryNTimesLoop retryLoop = new RetryNTimesLoop(retries);
    while (retryLoop.shouldContinue()) {
        try {
            if (group.getFilter() == null) {
                LOG.debug("Enumerating and pulling {} on {}.", group.getResourceUri(), client);
                client.enumerateAndPull(group.getResourceUri(), nodes, true);
            } else {
                LOG.debug("Enumerating and pulling {} with dialect {} and filter {} on {}.", group.getResourceUri(), group.getDialect(), group.getFilter(), client);
                client.enumerateAndPullUsingFilter(group.getResourceUri(), group.getDialect(), group.getFilter(), nodes, true);
            }
            break;
        } catch (WSManException e) {
            retryLoop.takeException(e);
        }
    }
    LOG.debug("Found {} nodes.", nodes.size());
    // Process the results
    processEnumerationResults(group, builder, resourceSupplier, nodes);
}
Also used : WSManException(org.opennms.core.wsman.exceptions.WSManException) RetryNTimesLoop(org.opennms.core.wsman.utils.RetryNTimesLoop) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.w3c.dom.Node) OnmsNode(org.opennms.netmgt.model.OnmsNode) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource)

Example 3 with Resource

use of org.opennms.netmgt.collection.support.builder.Resource in project opennms by OpenNMS.

the class JdbcCollector method collect.

@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
    final JdbcDataCollection collection = (JdbcDataCollection) parameters.get(JDBC_COLLECTION_KEY);
    JdbcAgentState agentState = null;
    Connection con = null;
    ResultSet results = null;
    Statement stmt = null;
    try {
        agentState = createAgentState(agent.getAddress(), parameters);
        agentState.setupDatabaseConnections(parameters);
        // Create a new collection set.
        CollectionSetBuilder builder = new CollectionSetBuilder(agent);
        // Creating a single resource object, because all node-level metric must belong to the exact same resource.
        final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
        // Cycle through all of the queries for this collection
        for (JdbcQuery query : collection.getQueries()) {
            // Verify if we should check for availability of a query.
            if (agentState.shouldCheckAvailability(query.getQueryName(), query.getRecheckInterval())) {
                // Check to see if the query is available.
                if (!isGroupAvailable(agentState, query)) {
                    LOG.warn("Group is not available.");
                    continue;
                }
            }
            try {
                // If the query is available, lets collect it.
                if (agentState.groupIsAvailable(query.getQueryName())) {
                    if (agentState.getUseDataSourceName()) {
                        initDatabaseConnectionFactory(agentState.getDataSourceName());
                        con = DataSourceFactory.getInstance(agentState.getDataSourceName()).getConnection();
                    } else {
                        con = agentState.getJdbcConnection();
                    }
                    stmt = agentState.createStatement(con);
                    results = agentState.executeJdbcQuery(stmt, query);
                    // Determine if there were any results for this query to
                    if (results.isBeforeFirst() && results.isAfterLast()) {
                        LOG.warn("Query '{}' returned no results.", query.getQueryName());
                        // Close the statement, but retain the connection.
                        agentState.closeResultSet(results);
                        agentState.closeStmt(stmt);
                        continue;
                    }
                    // Determine if there are results and how many.
                    results.last();
                    boolean singleInstance = (results.getRow() == 1) ? true : false;
                    results.beforeFirst();
                    // Iterate through each row.
                    while (results.next()) {
                        Resource resource = null;
                        // Create the appropriate resource container.
                        if (singleInstance) {
                            resource = nodeResource;
                        } else {
                            // Retrieve the name of the column to use as the instance key for multi-row queries.
                            String instance = results.getString(query.getInstanceColumn());
                            resource = new DeferredGenericTypeResource(nodeResource, query.getResourceType(), instance);
                        }
                        for (JdbcColumn curColumn : query.getJdbcColumns()) {
                            final AttributeType type = curColumn.getDataType();
                            String columnName = null;
                            if (curColumn.getDataSourceName() != null && curColumn.getDataSourceName().length() != 0) {
                                columnName = curColumn.getDataSourceName();
                            } else {
                                columnName = curColumn.getColumnName();
                            }
                            String columnValue = results.getString(columnName);
                            if (columnValue == null) {
                                LOG.debug("Skipping column named '{}' with null value.", curColumn.getColumnName());
                                continue;
                            }
                            if (type.isNumeric()) {
                                Double numericValue = Double.NaN;
                                try {
                                    numericValue = Double.parseDouble(columnValue);
                                } catch (NumberFormatException e) {
                                    LOG.warn("Value '{}' for column named '{}' cannot be converted to a number. Skipping.", columnValue, curColumn.getColumnName());
                                    continue;
                                }
                                builder.withNumericAttribute(resource, query.getQueryName(), curColumn.getAlias(), numericValue, type);
                            } else {
                                builder.withStringAttribute(resource, query.getQueryName(), curColumn.getAlias(), columnValue);
                            }
                        }
                    }
                }
            } catch (SQLException e) {
                // Close the statement but retain the connection, log the exception and continue to the next query.
                LOG.warn("There was a problem executing query '{}' Please review the query or configuration. Reason: {}", query.getQueryName(), e.getMessage());
                continue;
            } finally {
                agentState.closeResultSet(results);
                agentState.closeStmt(stmt);
                agentState.closeConnection(con);
            }
        }
        builder.withStatus(CollectionStatus.SUCCEEDED);
        return builder.build();
    } finally {
        if (agentState != null) {
            // Make sure that when we're done we close all results, statements and connections.
            agentState.closeResultSet(results);
            agentState.closeStmt(stmt);
            agentState.closeConnection(con);
        // agentState.closeAgentConnection();
        }
    }
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) JdbcColumn(org.opennms.netmgt.config.jdbc.JdbcColumn) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) 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) JdbcAgentState(org.opennms.netmgt.collectd.jdbc.JdbcAgentState) JdbcQuery(org.opennms.netmgt.config.jdbc.JdbcQuery) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) AttributeType(org.opennms.netmgt.collection.api.AttributeType) JdbcDataCollection(org.opennms.netmgt.config.jdbc.JdbcDataCollection) ResultSet(java.sql.ResultSet)

Example 4 with Resource

use of org.opennms.netmgt.collection.support.builder.Resource in project opennms by OpenNMS.

the class AbstractVTDXmlCollectionHandler method fillCollectionSet.

/**
 * Fill collection set.
 *
 * @param agent the agent
 * @param collectionSet the collection set
 * @param source the source
 * @param document the document
 * @throws ParseException the parse exception
 * @throws XPathParseException the x-path parse exception
 * @throws XPathEvalException the x-path evaluation exception
 * @throws NavException the navigation exception
 */
protected void fillCollectionSet(CollectionAgent agent, CollectionSetBuilder builder, XmlSource source, VTDNav document) throws ParseException, XPathParseException, XPathEvalException, NavException {
    AutoPilot resAP = new AutoPilot(document);
    for (XmlGroup group : source.getXmlGroups()) {
        LOG.debug("fillCollectionSet: getting resources for XML group {} using XPATH {}", group.getName(), group.getResourceXpath());
        Date timestamp = getTimeStamp(document, group);
        resAP.selectXPath(group.getResourceXpath());
        while (resAP.evalXPath() != -1) {
            String resourceName = getResourceName(document, group);
            LOG.debug("fillCollectionSet: processing XML resource {}", resourceName);
            final Resource collectionResource = getCollectionResource(agent, resourceName, group.getResourceType(), timestamp);
            LOG.debug("fillCollectionSet: processing resource {}", collectionResource);
            for (XmlObject object : group.getXmlObjects()) {
                document.push();
                AutoPilot ap = new AutoPilot();
                ap.bind(document);
                ap.selectXPath(object.getXpath());
                String value = ap.evalXPathToString();
                document.pop();
                builder.withAttribute(collectionResource, group.getName(), object.getName(), value, object.getDataType());
            }
            processXmlResource(builder, collectionResource, resourceName, group.getName());
        }
    }
}
Also used : XmlGroup(org.opennms.protocols.xml.config.XmlGroup) AutoPilot(com.ximpleware.AutoPilot) Resource(org.opennms.netmgt.collection.support.builder.Resource) XmlObject(org.opennms.protocols.xml.config.XmlObject) Date(java.util.Date)

Example 5 with Resource

use of org.opennms.netmgt.collection.support.builder.Resource in project opennms by OpenNMS.

the class VmwareCimCollector 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 VmwareCimCollection collection = (VmwareCimCollection) 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.getVmwareCimGroup().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();
    }
    HostSystem hostSystem = vmwareViJavaAccess.getHostSystemByManagedObjectId(vmwareManagedObjectId);
    String powerState = null;
    if (hostSystem == null) {
        logger.debug("hostSystem=null");
    } else {
        HostRuntimeInfo hostRuntimeInfo = hostSystem.getRuntime();
        if (hostRuntimeInfo == null) {
            logger.debug("hostRuntimeInfo=null");
        } else {
            HostSystemPowerState hostSystemPowerState = hostRuntimeInfo.getPowerState();
            if (hostSystemPowerState == null) {
                logger.debug("hostSystemPowerState=null");
            } else {
                powerState = hostSystemPowerState.toString();
            }
        }
    }
    logger.debug("The power state for host system '{}' is '{}'", vmwareManagedObjectId, powerState);
    if ("poweredOn".equals(powerState)) {
        HashMap<String, List<CIMObject>> cimObjects = new HashMap<String, List<CIMObject>>();
        for (final VmwareCimGroup vmwareCimGroup : collection.getVmwareCimGroup()) {
            String cimClass = vmwareCimGroup.getCimClass();
            if (!cimObjects.containsKey(cimClass)) {
                List<CIMObject> cimList = null;
                try {
                    cimList = vmwareViJavaAccess.queryCimObjects(hostSystem, cimClass, InetAddressUtils.str(agent.getAddress()));
                } catch (Exception e) {
                    logger.warn("Error retrieving CIM values from host system '{}'. Error message: '{}'", vmwareManagedObjectId, e.getMessage());
                    return builder.build();
                } finally {
                    vmwareViJavaAccess.disconnect();
                }
                cimObjects.put(cimClass, cimList);
            }
            final List<CIMObject> cimList = cimObjects.get(cimClass);
            if (cimList == null) {
                logger.warn("Error getting objects of CIM class '{}' from host system '{}'", cimClass, vmwareManagedObjectId);
                continue;
            }
            String keyAttribute = vmwareCimGroup.getKey();
            String attributeValue = vmwareCimGroup.getValue();
            String instanceAttribute = vmwareCimGroup.getInstance();
            for (CIMObject cimObject : cimList) {
                boolean addObject = false;
                if (keyAttribute != null && attributeValue != null) {
                    String cimObjectValue = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, keyAttribute);
                    if (attributeValue.equals(cimObjectValue)) {
                        addObject = true;
                    } else {
                        addObject = false;
                    }
                } else {
                    addObject = true;
                }
                if (addObject) {
                    final String instance = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, instanceAttribute);
                    final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
                    final Resource resource = new DeferredGenericTypeResource(nodeResource, vmwareCimGroup.getResourceType(), instance);
                    for (Attrib attrib : vmwareCimGroup.getAttrib()) {
                        final AttributeType type = attrib.getType();
                        String value = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, attrib.getName());
                        if (valueModifiers.containsKey(attrib.getName())) {
                            String modifiedValue = valueModifiers.get(attrib.getName()).modifyValue(attrib.getName(), value, cimObject, vmwareViJavaAccess);
                            logger.debug("Applying value modifier for instance value " + attrib.getName() + "[" + instance + "]='" + value + "' => '" + modifiedValue + "' for node " + agent.getNodeId());
                            value = modifiedValue;
                        }
                        builder.withAttribute(resource, vmwareCimGroup.getName(), attrib.getAlias(), value, type);
                    }
                }
            }
        }
        builder.withStatus(CollectionStatus.SUCCEEDED);
    }
    vmwareViJavaAccess.disconnect();
    return builder.build();
}
Also used : MalformedURLException(java.net.MalformedURLException) CIMObject(org.sblim.wbem.cim.CIMObject) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) HostRuntimeInfo(com.vmware.vim25.HostRuntimeInfo) 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) VmwareCimCollection(org.opennms.netmgt.config.vmware.cim.VmwareCimCollection) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) HostSystemPowerState(com.vmware.vim25.HostSystemPowerState) CollectionException(org.opennms.netmgt.collection.api.CollectionException) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) CollectionInitializationException(org.opennms.netmgt.collection.api.CollectionInitializationException) Attrib(org.opennms.netmgt.config.vmware.cim.Attrib) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) VmwareServer(org.opennms.netmgt.config.vmware.VmwareServer) AttributeType(org.opennms.netmgt.collection.api.AttributeType) HostSystem(com.vmware.vim25.mo.HostSystem) List(java.util.List) VmwareCimGroup(org.opennms.netmgt.config.vmware.cim.VmwareCimGroup) RemoteException(java.rmi.RemoteException) VmwareViJavaAccess(org.opennms.protocols.vmware.VmwareViJavaAccess)

Aggregations

Resource (org.opennms.netmgt.collection.support.builder.Resource)17 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)13 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)9 AttributeType (org.opennms.netmgt.collection.api.AttributeType)8 List (java.util.List)6 GenericTypeResource (org.opennms.netmgt.collection.support.builder.GenericTypeResource)6 OnmsNode (org.opennms.netmgt.model.OnmsNode)6 Node (org.w3c.dom.Node)6 Map (java.util.Map)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Collectors (java.util.stream.Collectors)5 InetAddressUtils (org.opennms.core.utils.InetAddressUtils)5 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)5 CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)5 CollectionException (org.opennms.netmgt.collection.api.CollectionException)5 CollectionInitializationException (org.opennms.netmgt.collection.api.CollectionInitializationException)5 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)5 Attrib (org.opennms.netmgt.config.wsman.Attrib)5 Maps (com.google.common.collect.Maps)4 Sets (com.google.common.collect.Sets)4