Search in sources :

Example 26 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class ThresholdingVisitorIT method runFileSystemDataTest.

private void runFileSystemDataTest(ThresholdingVisitor visitor, int resourceId, String fs, long value, long max) throws Exception {
    SnmpCollectionAgent agent = createCollectionAgent();
    // Creating Generic ResourceType
    GenericIndexResourceType resourceType = createGenericIndexResourceType(agent, "hrStorageIndex");
    // Creating strings.properties file
    ResourcePath path = ResourcePath.get("snmp", "1", "hrStorageIndex", Integer.toString(resourceId));
    m_resourceStorageDao.setStringAttribute(path, "hrStorageType", ".1.3.6.1.2.1.25.2.1.4");
    m_resourceStorageDao.setStringAttribute(path, "hrStorageDescr", fs);
    // Creating Resource
    SnmpInstId inst = new SnmpInstId(resourceId);
    SnmpCollectionResource resource = new GenericIndexResource(resourceType, "hrStorageIndex", inst);
    addAttributeToCollectionResource(resource, resourceType, "hrStorageUsed", "gauge", "hrStorageIndex", value);
    addAttributeToCollectionResource(resource, resourceType, "hrStorageSize", "gauge", "hrStorageIndex", max);
    addAttributeToCollectionResource(resource, resourceType, "hrStorageAllocUnits", "gauge", "hrStorageIndex", 1);
    // Run Visitor
    resource.visit(visitor);
    EasyMock.verify(agent);
}
Also used : ResourcePath(org.opennms.netmgt.model.ResourcePath) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) GenericIndexResource(org.opennms.netmgt.collectd.GenericIndexResource) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) GenericIndexResourceType(org.opennms.netmgt.collectd.GenericIndexResourceType)

Example 27 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class ThresholdingVisitorIT method testThresholdsFiltersOnNodeResourceWithCollectionSetBuilder.

/**
     * Similar to {@link #testThresholdsFiltersOnNodeResource()}, but
     * we generate the collection set using the CollectionSetBuilder instead
     * of using SnmpCollector specific types.
     */
@Test
public void testThresholdsFiltersOnNodeResourceWithCollectionSetBuilder() throws Exception {
    initFactories("/threshd-configuration.xml", "/test-thresholds-5.xml");
    ThresholdingVisitor visitor = createVisitor();
    // Adding Expected Thresholds
    addHighThresholdEvent(1, 30, 25, 50, "/home", "node", "(hda1_hrStorageUsed/hda1_hrStorageSize)*100", null, null);
    addHighThresholdEvent(1, 50, 45, 60, "/opt", "node", "(hda2_hrStorageUsed/hda2_hrStorageSize)*100", null, null);
    // Creating Node ResourceType
    SnmpCollectionAgent agent = createCollectionAgent();
    NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
    CollectionSet collectionSet = new CollectionSetBuilder(agent).withNumericAttribute(nodeResource, "hd-usage", "hda1_hrStorageUsed", 50, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda1_hrStorageSize", 100, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda2_hrStorageUsed", 60, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda2_hrStorageSize", 100, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda3_hrStorageUsed", 70, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda3_hrStorageSize", 100, AttributeType.GAUGE).build();
    // Creating strings.properties file
    ResourcePath path = ResourcePath.get("snmp", "1");
    m_resourceStorageDao.setStringAttribute(path, "hda1_hrStorageDescr", "/home");
    m_resourceStorageDao.setStringAttribute(path, "hda2_hrStorageDescr", "/opt");
    m_resourceStorageDao.setStringAttribute(path, "hda3_hrStorageDescr", "/usr");
    // Run Visitor and Verify Events
    collectionSet.visit(visitor);
    EasyMock.verify(agent);
    verifyEvents(0);
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) ResourcePath(org.opennms.netmgt.model.ResourcePath) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 28 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class ThresholdingVisitorIT method testThresholdsFiltersOnNodeResource.

/*
     * This test uses this files from src/test/resources:
     * - threshd-configuration.xml
     * - test-thresholds-5.xml
     */
@Test
public void testThresholdsFiltersOnNodeResource() throws Exception {
    initFactories("/threshd-configuration.xml", "/test-thresholds-5.xml");
    ThresholdingVisitor visitor = createVisitor();
    // Adding Expected Thresholds
    addHighThresholdEvent(1, 30, 25, 50, "/home", "node", "(hda1_hrStorageUsed/hda1_hrStorageSize)*100", null, null);
    addHighThresholdEvent(1, 50, 45, 60, "/opt", "node", "(hda2_hrStorageUsed/hda2_hrStorageSize)*100", null, null);
    // Creating Node ResourceType
    SnmpCollectionAgent agent = createCollectionAgent();
    MockDataCollectionConfig dataCollectionConfig = new MockDataCollectionConfig();
    OnmsSnmpCollection collection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), dataCollectionConfig, m_locationAwareSnmpClient);
    NodeResourceType resourceType = new NodeResourceType(agent, collection);
    // Creating strings.properties file
    ResourcePath path = ResourcePath.get("snmp", "1");
    m_resourceStorageDao.setStringAttribute(path, "hda1_hrStorageDescr", "/home");
    m_resourceStorageDao.setStringAttribute(path, "hda2_hrStorageDescr", "/opt");
    m_resourceStorageDao.setStringAttribute(path, "hda3_hrStorageDescr", "/usr");
    // Creating Resource
    SnmpCollectionResource resource = new NodeInfo(resourceType, agent);
    addAttributeToCollectionResource(resource, resourceType, "hda1_hrStorageUsed", "gauge", "node", 50);
    addAttributeToCollectionResource(resource, resourceType, "hda1_hrStorageSize", "gauge", "node", 100);
    addAttributeToCollectionResource(resource, resourceType, "hda2_hrStorageUsed", "gauge", "node", 60);
    addAttributeToCollectionResource(resource, resourceType, "hda2_hrStorageSize", "gauge", "node", 100);
    addAttributeToCollectionResource(resource, resourceType, "hda3_hrStorageUsed", "gauge", "node", 70);
    addAttributeToCollectionResource(resource, resourceType, "hda3_hrStorageSize", "gauge", "node", 100);
    // Run Visitor and Verify Events
    resource.visit(visitor);
    EasyMock.verify(agent);
    verifyEvents(0);
}
Also used : NodeResourceType(org.opennms.netmgt.collectd.NodeResourceType) ResourcePath(org.opennms.netmgt.model.ResourcePath) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) NodeInfo(org.opennms.netmgt.collectd.NodeInfo) MockDataCollectionConfig(org.opennms.netmgt.mock.MockDataCollectionConfig) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) OnmsSnmpCollection(org.opennms.netmgt.collectd.OnmsSnmpCollection) Test(org.junit.Test)

Example 29 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class Sftp3gppXmlCollectionHandler method collect.

/* (non-Javadoc)
     * @see org.opennms.protocols.xml.collector.XmlCollectionHandler#collect(org.opennms.netmgt.collectd.CollectionAgent, org.opennms.protocols.xml.config.XmlDataCollection, java.util.Map)
     */
@Override
public CollectionSet collect(CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters) throws CollectionException {
    String status = "finished";
    // Create a new collection set.
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    // TODO We could be careful when handling exceptions because parsing exceptions will be treated different from connection or retrieval exceptions
    DateTime startTime = new DateTime();
    Sftp3gppUrlConnection connection = null;
    try {
        ResourcePath resourcePath = ResourcePath.get(ResourceTypeUtils.SNMP_DIRECTORY, Integer.toString(agent.getNodeId()));
        for (XmlSource source : collection.getXmlSources()) {
            if (!source.getUrl().startsWith(Sftp3gppUrlHandler.PROTOCOL)) {
                throw new CollectionException("The 3GPP SFTP Collection Handler can only use the protocol " + Sftp3gppUrlHandler.PROTOCOL);
            }
            final String urlStr = source.getUrl();
            final Request request = source.getRequest();
            URL url = UrlFactory.getUrl(urlStr, request);
            String lastFile = Sftp3gppUtils.getLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath());
            connection = (Sftp3gppUrlConnection) url.openConnection();
            if (lastFile == null) {
                lastFile = connection.get3gppFileName();
                LOG.debug("collect(single): retrieving file from {}{}{} from {}", url.getPath(), File.separatorChar, lastFile, agent.getHostAddress());
                Document doc = getXmlDocument(urlStr, request);
                fillCollectionSet(agent, builder, source, doc);
                Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), lastFile);
                Sftp3gppUtils.deleteFile(connection, lastFile);
            } else {
                connection.connect();
                List<String> files = connection.getFileList();
                long lastTs = connection.getTimeStampFromFile(lastFile);
                boolean collected = false;
                for (String fileName : files) {
                    if (connection.getTimeStampFromFile(fileName) > lastTs) {
                        LOG.debug("collect(multiple): retrieving file {} from {}", fileName, agent.getHostAddress());
                        InputStream is = connection.getFile(fileName);
                        try {
                            Document doc = getXmlDocument(is, request);
                            IOUtils.closeQuietly(is);
                            fillCollectionSet(agent, builder, source, doc);
                        } finally {
                            IOUtils.closeQuietly(is);
                        }
                        Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), fileName);
                        Sftp3gppUtils.deleteFile(connection, fileName);
                        collected = true;
                    }
                }
                if (!collected) {
                    LOG.warn("collect: could not find any file after {} on {}", lastFile, agent);
                }
            }
        }
        return builder.build();
    } catch (Exception e) {
        status = "failed";
        throw new CollectionException(e.getMessage(), e);
    } finally {
        DateTime endTime = new DateTime();
        LOG.debug("collect: {} collection {}: duration: {} ms", status, collection.getName(), endTime.getMillis() - startTime.getMillis());
        UrlFactory.disconnect(connection);
    }
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) CollectionException(org.opennms.netmgt.collection.api.CollectionException) InputStream(java.io.InputStream) Request(org.opennms.protocols.xml.config.Request) Document(org.w3c.dom.Document) DateTime(org.joda.time.DateTime) XmlSource(org.opennms.protocols.xml.config.XmlSource) URL(java.net.URL) CollectionException(org.opennms.netmgt.collection.api.CollectionException) ResourcePath(org.opennms.netmgt.model.ResourcePath) Sftp3gppUrlConnection(org.opennms.protocols.sftp.Sftp3gppUrlConnection)

Example 30 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class TcaCollectionHandler method getLastTimestamp.

/**
	 * Gets the last timestamp.
	 *
	 * @param resource the TCA resource
	 * @return the last timestamp
	 */
private long getLastTimestamp(CollectionResource resource) {
    long timestamp = 0;
    ResourcePath path = ResourceTypeUtils.getResourcePathWithRepository(m_repository, resource.getPath());
    try {
        LOG.debug("Retrieving timestamp from path {}", path);
        String ts = m_resourceStorageDao.getStringAttribute(path, LAST_TIMESTAMP);
        if (ts != null) {
            timestamp = Long.parseLong(ts);
        }
    } catch (Exception e) {
        LOG.error("Failed to retrieve timestamp from path {}", path, e);
    }
    return timestamp;
}
Also used : ResourcePath(org.opennms.netmgt.model.ResourcePath) RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) CollectionException(org.opennms.netmgt.collection.api.CollectionException) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ResourcePath (org.opennms.netmgt.model.ResourcePath)51 OnmsResource (org.opennms.netmgt.model.OnmsResource)19 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)14 MockResourceType (org.opennms.netmgt.mock.MockResourceType)9 Test (org.junit.Test)8 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)8 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Resource (org.opennms.newts.api.Resource)6 Path (java.nio.file.Path)5 ArrayList (java.util.ArrayList)5 Sample (org.opennms.newts.api.Sample)5 IOException (java.io.IOException)3 List (java.util.List)3 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)3 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 Timestamp (org.opennms.newts.api.Timestamp)3 Optional (com.google.common.base.Optional)2 Preconditions (com.google.common.base.Preconditions)2