Search in sources :

Example 41 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)

Example 42 with ResourcePath

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

the class NrtController method getMetaDataForReport.

private Map<String, String> getMetaDataForReport(final Set<RrdGraphAttribute> rrdGraphAttributes) {
    Map<String, String> metaData = new HashMap<String, String>();
    logger.debug("getMetaDataForReport: " + rrdGraphAttributes);
    // get all metaData for RrdGraphAttributes from the meta files next to the RRD/JRobin files
    for (final RrdGraphAttribute attr : rrdGraphAttributes) {
        // Convert the "relative RRD path" from the RrdGraphAttribute to a ResourcePath used
        // by the ResourceStorageDao.
        ResourcePath pathToMetaFile;
        if (TimeSeries.getTimeseriesStrategy() == Strategy.NEWTS) {
            String path = attr.getRrdRelativePath();
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            pathToMetaFile = ResourcePath.get(path.split(":"));
        } else {
            final String[] knownExtensions = new String[] { ".rrd", ".jrb" };
            String metaFileNameWithoutExtension = attr.getRrdFile();
            for (final String ext : knownExtensions) {
                if (metaFileNameWithoutExtension.endsWith(ext)) {
                    metaFileNameWithoutExtension = metaFileNameWithoutExtension.substring(0, metaFileNameWithoutExtension.lastIndexOf(ext));
                    break;
                }
            }
            pathToMetaFile = ResourcePath.get(attr.getResource().getPath(), metaFileNameWithoutExtension);
        }
        final Set<Entry<String, String>> metaDataEntrySet = m_resourceStorageDao.getMetaData(pathToMetaFile).entrySet();
        logger.debug("Meta-data for attribute '{}' at path '{}' contains: {}", attr, pathToMetaFile, metaDataEntrySet);
        if (metaDataEntrySet == null)
            continue;
        final String attrName = attr.getName();
        final String attrString = attr.toString();
        for (final Map.Entry<String, String> entry : metaDataEntrySet) {
            final String line = entry.getKey() + '=' + entry.getValue();
            if (line.endsWith(attrName)) {
                metaData.put(attrString, line);
            }
        }
        ;
    }
    return metaData;
}
Also used : Entry(java.util.Map.Entry) ResourcePath(org.opennms.netmgt.model.ResourcePath) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute)

Example 43 with ResourcePath

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

the class NewtsResourceStorageDao method getAttributes.

@Override
public Set<OnmsAttribute> getAttributes(ResourcePath path) {
    Set<OnmsAttribute> attributes = Sets.newHashSet();
    // Fetch the resource-level attributes in parallel
    Future<Map<String, String>> stringAttributes = ForkJoinPool.commonPool().submit(getResourceAttributesCallable(path));
    // Gather the list of metrics available under the resource path
    SearchResults results = searchFor(path, 0, true);
    for (Result result : results) {
        final String resourceId = result.getResource().getId();
        final ResourcePath resultPath = toResourcePath(resourceId);
        if (!path.equals(resultPath)) {
            // This shouldn't happen
            LOG.warn("Encountered non-child resource {} when searching for {} with depth {}. Ignoring resource.", result.getResource(), path, 0);
            continue;
        }
        for (String metric : result.getMetrics()) {
            // Use the metric name as the dsName
            // Store the resource id in the rrdFile field
            attributes.add(new RrdGraphAttribute(metric, "", resourceId));
        }
    }
    // Add the resource level attributes to the result set
    try {
        stringAttributes.get().entrySet().stream().map(e -> new StringPropertyAttribute(e.getKey(), e.getValue())).forEach(attributes::add);
    } catch (InterruptedException | ExecutionException e) {
        throw Throwables.propagate(e);
    }
    return attributes;
}
Also used : NewtsUtils.toResourcePath(org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath) IntStream(java.util.stream.IntStream) Context(org.opennms.newts.api.Context) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Callable(java.util.concurrent.Callable) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) CassandraIndexer(org.opennms.newts.cassandra.search.CassandraIndexer) StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) NewtsUtils(org.opennms.netmgt.newts.support.NewtsUtils) NewtsUtils.toResourceId(org.opennms.netmgt.newts.support.NewtsUtils.toResourceId) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Optional(com.google.common.base.Optional) Map(java.util.Map) Sample(org.opennms.newts.api.Sample) NewtsWriter(org.opennms.netmgt.newts.NewtsWriter) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Query(org.opennms.newts.api.search.Query) CassandraSampleRepository(org.opennms.newts.persistence.cassandra.CassandraSampleRepository) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Resource(org.opennms.newts.api.Resource) Set(java.util.Set) Throwables(com.google.common.base.Throwables) CassandraSearcher(org.opennms.newts.cassandra.search.CassandraSearcher) ResourceStorageDao(org.opennms.netmgt.dao.api.ResourceStorageDao) Result(org.opennms.newts.api.search.SearchResults.Result) SearchableResourceMetadataCache(org.opennms.netmgt.newts.support.SearchableResourceMetadataCache) Sets(com.google.common.collect.Sets) ExecutionException(java.util.concurrent.ExecutionException) NewtsUtils.findResourcesWithMetricsAtDepth(org.opennms.netmgt.newts.support.NewtsUtils.findResourcesWithMetricsAtDepth) List(java.util.List) SearchResults(org.opennms.newts.api.search.SearchResults) ForkJoinPool(java.util.concurrent.ForkJoinPool) ResourcePath(org.opennms.netmgt.model.ResourcePath) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) SearchResults(org.opennms.newts.api.search.SearchResults) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Result(org.opennms.newts.api.search.SearchResults.Result) StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) NewtsUtils.toResourcePath(org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath) ResourcePath(org.opennms.netmgt.model.ResourcePath) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 44 with ResourcePath

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

the class ObjectNameStorageStrategyTest method testGetResourceNameFromIndex2.

@Test
public void testGetResourceNameFromIndex2() {
    ResourcePath parentResource = ResourcePath.get("1");
    CollectionResource resource = new MockCollectionResource(parentResource, "java.lang:type=MemoryPool,name=Survivor Space", "");
    List<org.opennms.netmgt.collection.api.Parameter> params = new ArrayList<>();
    Parameter p = new Parameter("index-format", "${domain}");
    params.add(p);
    ObjectNameStorageStrategy instance = new ObjectNameStorageStrategy();
    instance.setParameters(params);
    String expResult = "java.lang";
    String result = instance.getResourceNameFromIndex(resource);
    assertEquals(expResult, result);
    params.clear();
    p.setValue("${type}");
    params.add(p);
    instance.setParameters(params);
    expResult = "MemoryPool";
    result = instance.getResourceNameFromIndex(resource);
    assertEquals(expResult, result);
    params.clear();
    p.setValue("${name}");
    params.add(p);
    instance.setParameters(params);
    expResult = "Survivor Space";
    result = instance.getResourceNameFromIndex(resource);
    assertEquals(expResult, result);
    params.clear();
    p.setValue("${domain}:type=${type},name=${name}");
    params.add(p);
    instance.setParameters(params);
    expResult = "java.lang:type=MemoryPool,name=Survivor Space";
    result = instance.getResourceNameFromIndex(resource);
    assertEquals(expResult, result);
}
Also used : ObjectNameStorageStrategy(org.opennms.netmgt.collection.support.ObjectNameStorageStrategy) CollectionResource(org.opennms.netmgt.collection.api.CollectionResource) ResourcePath(org.opennms.netmgt.model.ResourcePath) ArrayList(java.util.ArrayList) Parameter(org.opennms.netmgt.config.datacollection.Parameter) Test(org.junit.Test)

Example 45 with ResourcePath

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

the class CollectionResourceWrapper method getFieldValue.

/**
 * <p>getFieldValue</p>
 *
 * @param ds a {@link java.lang.String} object.
 * @return a {@link java.lang.String} object.
 */
public String getFieldValue(String ds) {
    if (ds == null || "".equals(ds)) {
        return null;
    }
    LOG.debug("getLabelValue: Getting Value for {}::{}", m_resource.getResourceTypeName(), ds);
    if ("nodeid".equalsIgnoreCase(ds)) {
        return Integer.toString(m_nodeId);
    } else if ("ipaddress".equalsIgnoreCase(ds)) {
        return m_hostAddress;
    } else if ("iflabel".equalsIgnoreCase(ds)) {
        return getIfLabel();
    } else if ("id".equalsIgnoreCase(ds)) {
        return m_resource.getPath().getName().toString();
    }
    try {
        String retval = null;
        // Get Value from ifInfo only for Interface Resource
        if (isAnInterfaceResource()) {
            retval = getIfInfoValue(ds);
            if (retval != null) {
                return retval;
            }
        }
        // Find values saved in string attributes
        ResourcePath path = ResourceTypeUtils.getResourcePathWithRepository(m_repository, m_resource.getPath());
        retval = m_resourceStorageDao.getStringAttribute(path, ds);
        if (retval != null) {
            return retval;
        }
    } catch (Throwable e) {
        LOG.info("getFieldValue: Can't get value for attribute {} for resource {}.", ds, m_resource, e);
    }
    LOG.debug("getFieldValue: The field {} is not a string property. Trying to parse it as numeric metric.", ds);
    Double d = getAttributeValue(ds);
    if (d != null) {
        return d.toString();
    }
    return null;
}
Also used : ResourcePath(org.opennms.netmgt.model.ResourcePath)

Aggregations

ResourcePath (org.opennms.netmgt.model.ResourcePath)57 OnmsResource (org.opennms.netmgt.model.OnmsResource)19 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)14 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)9 MockResourceType (org.opennms.netmgt.mock.MockResourceType)9 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)8 HashSet (java.util.HashSet)6 Map (java.util.Map)6 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)6 Parameter (org.opennms.netmgt.config.datacollection.Parameter)6 Resource (org.opennms.newts.api.Resource)6 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)5 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)5 Sample (org.opennms.newts.api.Sample)5 Path (java.nio.file.Path)4 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)4 IOException (java.io.IOException)3 ObjectNameStorageStrategy (org.opennms.netmgt.collection.support.ObjectNameStorageStrategy)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3