Search in sources :

Example 6 with ResourcePath

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

the class NewtsConverter method buildResourcePath.

private ResourcePath buildResourcePath(final Path resourceDir) {
    final ResourcePath resourcePath;
    final Path relativeResourceDir = this.rrdDir.relativize(resourceDir);
    // Transform store-by-id path into store-by-foreign-source path
    if (relativeResourceDir.startsWith(Paths.get("snmp")) && !relativeResourceDir.startsWith(Paths.get("snmp", "fs"))) {
        // The part after snmp/ is considered the node ID
        final int nodeId = Integer.valueOf(relativeResourceDir.getName(1).toString());
        // Get the foreign source for the node
        final ForeignId foreignId = foreignIds.get(nodeId);
        if (foreignId == null) {
            return null;
        }
        // Make a store-by-foreign-source compatible path by using the found foreign ID and append the remaining path as-is
        resourcePath = ResourcePath.get(ResourcePath.get(ResourcePath.get("snmp", "fs"), foreignId.foreignSource, foreignId.foreignId), Iterables.transform(Iterables.skip(relativeResourceDir, 2), Path::toString));
    } else {
        resourcePath = ResourcePath.get(Iterables.transform(relativeResourceDir, Path::toString));
    }
    return resourcePath;
}
Also used : Path(java.nio.file.Path) ResourcePath(org.opennms.netmgt.model.ResourcePath) ResourcePath(org.opennms.netmgt.model.ResourcePath)

Example 7 with ResourcePath

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

the class NewtsConverter method injectSamplesToNewts.

private void injectSamplesToNewts(final ResourcePath resourcePath, final String group, final List<? extends AbstractDS> dataSources, final SortedMap<Long, List<Double>> samples) {
    final ResourcePath groupPath = ResourcePath.get(resourcePath, group);
    // Create a resource ID from the resource path
    final String groupId = NewtsUtils.toResourceId(groupPath);
    // Build indexing attributes
    final Map<String, String> attributes = Maps.newHashMap();
    NewtsUtils.addIndicesToAttributes(groupPath, attributes);
    // Create the NewTS resource to insert
    final Resource resource = new Resource(groupId, Optional.of(attributes));
    // Transform the RRD samples into NewTS samples
    List<Sample> batch = new ArrayList<>(this.batchSize);
    for (final Map.Entry<Long, List<Double>> s : samples.entrySet()) {
        for (int i = 0; i < dataSources.size(); i++) {
            final double value = s.getValue().get(i);
            if (Double.isNaN(value)) {
                continue;
            }
            final AbstractDS ds = dataSources.get(i);
            final Timestamp timestamp = Timestamp.fromEpochSeconds(s.getKey());
            try {
                batch.add(toSample(ds, resource, timestamp, value));
            } catch (IllegalArgumentException e) {
                // type i.e. negative for a counter, so we silently skip these
                continue;
            }
            if (batch.size() >= this.batchSize) {
                this.repository.insert(batch, true);
                this.processedSamples.getAndAdd(batch.size());
                batch = new ArrayList<>(this.batchSize);
            }
        }
    }
    if (!batch.isEmpty()) {
        this.repository.insert(batch, true);
        this.processedSamples.getAndAdd(batch.size());
    }
    this.processedMetrics.getAndAdd(dataSources.size());
    LOG.trace("Stats: {} / {}", this.processedMetrics, this.processedSamples);
}
Also used : Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) ArrayList(java.util.ArrayList) Timestamp(org.opennms.newts.api.Timestamp) AbstractDS(org.opennms.netmgt.rrd.model.AbstractDS) ResourcePath(org.opennms.netmgt.model.ResourcePath) UnsignedLong(com.google.common.primitives.UnsignedLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 8 with ResourcePath

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

the class NewtsPersistOperationBuilder method getSamplesToInsert.

public List<Sample> getSamplesToInsert() {
    final List<Sample> samples = Lists.newLinkedList();
    ResourcePath path = ResourceTypeUtils.getResourcePathWithRepository(m_repository, ResourcePath.get(m_resource.getPath(), m_name));
    // Add extra attributes that can be used to walk the resource tree.
    NewtsUtils.addIndicesToAttributes(path, m_metaData);
    Resource resource = new Resource(NewtsUtils.toResourceId(path), Optional.of(m_metaData));
    // Convert numeric attributes to samples
    Timestamp timestamp = Timestamp.fromEpochMillis(m_timeKeeper.getCurrentTime());
    for (Entry<CollectionAttributeType, Number> entry : m_declarations.entrySet()) {
        CollectionAttributeType attrType = entry.getKey();
        MetricType type = mapType(attrType.getType());
        if (type == null) {
            // Skip attributes with no type
            continue;
        }
        Number value = entry.getValue();
        if (value == null) {
            // Skip attributes with no value (see NMS-8103)
            continue;
        }
        samples.add(new Sample(timestamp, m_context, resource, attrType.getName(), type, ValueType.compose(entry.getValue(), type)));
    }
    return samples;
}
Also used : ResourcePath(org.opennms.netmgt.model.ResourcePath) Sample(org.opennms.newts.api.Sample) MetricType(org.opennms.newts.api.MetricType) Resource(org.opennms.newts.api.Resource) CollectionAttributeType(org.opennms.netmgt.collection.api.CollectionAttributeType) Timestamp(org.opennms.newts.api.Timestamp)

Example 9 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath 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 10 with ResourcePath

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

the class TcaCollectionHandler method setLastTimestamp.

/**
	 * Sets the last timestamp.
	 *
	 * @param resource the resource
	 * @param timestamp the timestamp
	 */
private void setLastTimestamp(CollectionResource resource, long timestamp) {
    ResourcePath path = ResourceTypeUtils.getResourcePathWithRepository(m_repository, resource.getPath());
    LOG.debug("Setting timestamp to {} at path {}", timestamp, path);
    m_resourceStorageDao.setStringAttribute(path, LAST_TIMESTAMP, Long.toString(timestamp));
}
Also used : ResourcePath(org.opennms.netmgt.model.ResourcePath)

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