Search in sources :

Example 1 with Sftp3gppUrlConnection

use of org.opennms.protocols.sftp.Sftp3gppUrlConnection in project opennms by OpenNMS.

the class Sftp3gppVTDXmlCollectionHandler method collect.

@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 {
        // FIXME: Does not support storeByFS
        ResourcePath resourcePath = ResourcePath.get(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(source.getUrl(), source.getRequest());
            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());
                VTDNav doc = getVTDXmlDocument(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 {
                            VTDNav doc = getVTDXmlDocument(is, request);
                            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) 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) VTDNav(com.ximpleware.VTDNav)

Example 2 with Sftp3gppUrlConnection

use of org.opennms.protocols.sftp.Sftp3gppUrlConnection 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 3 with Sftp3gppUrlConnection

use of org.opennms.protocols.sftp.Sftp3gppUrlConnection in project opennms by OpenNMS.

the class UrlFactoryTest method testUrlFactory.

/**
     * Test time parser.
     *
     * @throws Exception the exception
     */
@Test
public void testUrlFactory() throws Exception {
    URL url = UrlFactory.getUrl("https://www.google.com", null);
    Assert.assertEquals(HttpsUrlHandler.HTTPS, url.getProtocol());
    Assert.assertTrue(url.openConnection() instanceof HttpUrlConnection);
    url = UrlFactory.getUrl("HTTP://www.opennms.org", null);
    Assert.assertEquals(HttpUrlHandler.HTTP, url.getProtocol());
    Assert.assertTrue(url.openConnection() instanceof HttpUrlConnection);
    url = UrlFactory.getUrl("sftp://www.opennms.org", null);
    Assert.assertEquals(SftpUrlHandler.PROTOCOL, url.getProtocol());
    Assert.assertTrue(url.openConnection() instanceof SftpUrlConnection);
    url = UrlFactory.getUrl("sftp.3GPP://junier-router.local/opt/3gpp/data", null);
    Assert.assertEquals(Sftp3gppUrlHandler.PROTOCOL, url.getProtocol());
    Assert.assertTrue(url.openConnection() instanceof Sftp3gppUrlConnection);
}
Also used : HttpUrlConnection(org.opennms.protocols.http.HttpUrlConnection) SftpUrlConnection(org.opennms.protocols.sftp.SftpUrlConnection) Sftp3gppUrlConnection(org.opennms.protocols.sftp.Sftp3gppUrlConnection) URL(java.net.URL) Test(org.junit.Test)

Aggregations

URL (java.net.URL)3 Sftp3gppUrlConnection (org.opennms.protocols.sftp.Sftp3gppUrlConnection)3 InputStream (java.io.InputStream)2 DateTime (org.joda.time.DateTime)2 CollectionException (org.opennms.netmgt.collection.api.CollectionException)2 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)2 ResourcePath (org.opennms.netmgt.model.ResourcePath)2 Request (org.opennms.protocols.xml.config.Request)2 XmlSource (org.opennms.protocols.xml.config.XmlSource)2 VTDNav (com.ximpleware.VTDNav)1 Test (org.junit.Test)1 HttpUrlConnection (org.opennms.protocols.http.HttpUrlConnection)1 SftpUrlConnection (org.opennms.protocols.sftp.SftpUrlConnection)1 Document (org.w3c.dom.Document)1