use of org.opennms.netmgt.model.RrdGraphAttribute in project opennms by OpenNMS.
the class DefaultRrdDaoTest method preparePrintValueTest.
private OnmsResource preparePrintValueTest(long start, long end, String printLine) throws IOException, RrdException {
String rrdDir = "snmp" + File.separator + "1" + File.separator + "eth0";
String rrdFile = "ifInOctets.jrb";
String escapedFile = rrdDir + File.separator + rrdFile;
if (File.separatorChar == '\\') {
escapedFile = escapedFile.replace("\\", "\\\\");
}
String[] command = new String[] { m_dao.getRrdBinaryPath(), "graph", "-", "--start=" + (start / 1000), "--end=" + (end / 1000), "DEF:ds1=\"" + escapedFile + "\":ifInOctets:AVERAGE", "PRINT:ds1:AVERAGE:\"%le\"" };
String commandString = StringUtils.arrayToDelimitedString(command, " ");
OnmsResource topResource = new OnmsResource("1", "Node One", new MockResourceType(), new HashSet<OnmsAttribute>(0), new ResourcePath("foo"));
OnmsAttribute attribute = new RrdGraphAttribute("ifInOctets", rrdDir, rrdFile);
HashSet<OnmsAttribute> attributeSet = new HashSet<OnmsAttribute>(1);
attributeSet.add(attribute);
MockResourceType childResourceType = new MockResourceType();
OnmsResource childResource = new OnmsResource("eth0", "Interface One: eth0", childResourceType, attributeSet, new ResourcePath("foo"));
childResource.setParent(topResource);
DefaultRrdGraphDetails details = new DefaultRrdGraphDetails();
details.setPrintLines(new String[] { printLine });
expect(m_rrdStrategy.createGraphReturnDetails(commandString, m_dao.getRrdBaseDirectory())).andReturn(details);
return childResource;
}
use of org.opennms.netmgt.model.RrdGraphAttribute in project opennms by OpenNMS.
the class NewtsFetchStrategy method fetch.
@Override
public FetchResults fetch(long start, long end, long step, int maxrows, Long interval, Long heartbeat, List<Source> sources, boolean relaxed) {
final LateAggregationParams lag = getLagParams(step, interval, heartbeat);
final Optional<Timestamp> startTs = Optional.of(Timestamp.fromEpochMillis(start));
final Optional<Timestamp> endTs = Optional.of(Timestamp.fromEpochMillis(end));
final Map<String, Object> constants = Maps.newHashMap();
// Group the sources by resource id to avoid calling the ResourceDao
// multiple times for the same resource
Map<ResourceId, List<Source>> sourcesByResourceId = sources.stream().collect(Collectors.groupingBy((source) -> ResourceId.fromString(source.getResourceId())));
// Lookup the OnmsResources in parallel
Map<ResourceId, Future<OnmsResource>> resourceFuturesById = Maps.newHashMapWithExpectedSize(sourcesByResourceId.size());
for (ResourceId resourceId : sourcesByResourceId.keySet()) {
resourceFuturesById.put(resourceId, threadPool.submit(getResourceByIdCallable(resourceId)));
}
// Gather the results, fail if any of the resources were not found
Map<OnmsResource, List<Source>> sourcesByResource = Maps.newHashMapWithExpectedSize(sourcesByResourceId.size());
for (Entry<ResourceId, Future<OnmsResource>> entry : resourceFuturesById.entrySet()) {
try {
OnmsResource resource = entry.getValue().get();
if (resource == null) {
if (relaxed)
continue;
LOG.error("No resource with id: {}", entry.getKey());
return null;
}
sourcesByResource.put(resource, sourcesByResourceId.get(entry.getKey()));
} catch (ExecutionException | InterruptedException e) {
throw Throwables.propagate(e);
}
}
// Now group the sources by Newts Resource ID, which differs from the OpenNMS Resource ID.
Map<String, List<Source>> sourcesByNewtsResourceId = Maps.newHashMap();
for (Entry<OnmsResource, List<Source>> entry : sourcesByResource.entrySet()) {
final OnmsResource resource = entry.getKey();
for (Source source : entry.getValue()) {
// Gather the values from strings.properties
Utils.convertStringAttributesToConstants(source.getLabel(), resource.getStringPropertyAttributes(), constants);
// Grab the attribute that matches the source
RrdGraphAttribute rrdGraphAttribute = resource.getRrdGraphAttributes().get(source.getAttribute());
if (rrdGraphAttribute == null && !Strings.isNullOrEmpty(source.getFallbackAttribute())) {
LOG.error("No attribute with name '{}', using fallback-attribute with name '{}'", source.getAttribute(), source.getFallbackAttribute());
source.setAttribute(source.getFallbackAttribute());
source.setFallbackAttribute(null);
rrdGraphAttribute = resource.getRrdGraphAttributes().get(source.getAttribute());
}
if (rrdGraphAttribute == null) {
if (relaxed)
continue;
LOG.error("No attribute with name: {}", source.getAttribute());
return null;
}
// The Newts Resource ID is stored in the rrdFile attribute
String newtsResourceId = rrdGraphAttribute.getRrdRelativePath();
// Remove the file separator prefix, added by the RrdGraphAttribute class
if (newtsResourceId.startsWith(File.separator)) {
newtsResourceId = newtsResourceId.substring(File.separator.length(), newtsResourceId.length());
}
List<Source> listOfSources = sourcesByNewtsResourceId.get(newtsResourceId);
// Create the list if it doesn't exist
if (listOfSources == null) {
listOfSources = Lists.newLinkedList();
sourcesByNewtsResourceId.put(newtsResourceId, listOfSources);
}
listOfSources.add(source);
}
}
// The Newts API only allows us to perform a query using a single (Newts) Resource ID,
// so we perform multiple queries in parallel, and aggregate the results.
Map<String, Future<Collection<Row<Measurement>>>> measurementsByNewtsResourceId = Maps.newHashMapWithExpectedSize(sourcesByNewtsResourceId.size());
for (Entry<String, List<Source>> entry : sourcesByNewtsResourceId.entrySet()) {
measurementsByNewtsResourceId.put(entry.getKey(), threadPool.submit(getMeasurementsForResourceCallable(entry.getKey(), entry.getValue(), startTs, endTs, lag)));
}
long[] timestamps = null;
Map<String, double[]> columns = Maps.newHashMap();
for (Entry<String, Future<Collection<Row<Measurement>>>> entry : measurementsByNewtsResourceId.entrySet()) {
Collection<Row<Measurement>> rows;
try {
rows = entry.getValue().get();
} catch (InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
final int N = rows.size();
if (timestamps == null) {
timestamps = new long[N];
int k = 0;
for (final Row<Measurement> row : rows) {
timestamps[k] = row.getTimestamp().asMillis();
k++;
}
}
int k = 0;
for (Row<Measurement> row : rows) {
for (Measurement measurement : row.getElements()) {
double[] column = columns.get(measurement.getName());
if (column == null) {
column = new double[N];
columns.put(measurement.getName(), column);
}
column[k] = measurement.getValue();
}
k += 1;
}
}
FetchResults fetchResults = new FetchResults(timestamps, columns, lag.getStep(), constants);
if (relaxed) {
Utils.fillMissingValues(fetchResults, sources);
}
LOG.trace("Fetch results: {}", fetchResults);
return fetchResults;
}
use of org.opennms.netmgt.model.RrdGraphAttribute in project opennms by OpenNMS.
the class NrtController method getRequiredRrdGraphAttributes.
public Set<RrdGraphAttribute> getRequiredRrdGraphAttributes(OnmsResource reportResource, PrefabGraph prefabGraph) {
Map<String, RrdGraphAttribute> available = reportResource.getRrdGraphAttributes();
Set<RrdGraphAttribute> reqAttrs = new LinkedHashSet<RrdGraphAttribute>();
for (String attrName : prefabGraph.getColumns()) {
RrdGraphAttribute attr = available.get(attrName);
if (attr != null) {
reqAttrs.add(attr);
}
}
return reqAttrs;
}
use of org.opennms.netmgt.model.RrdGraphAttribute in project opennms by OpenNMS.
the class NewtsResourceStorageDaoTest method getAttributes.
@Test
public void getAttributes() {
// Attributes are empty when the resource does not exist
replay();
assertEquals(0, m_nrs.getAttributes(ResourcePath.get("should", "not", "exist")).size());
verify();
// Metrics from all buckets should be present
index(ResourcePath.get("a", "bucket1"), Sets.newHashSet("metric11", "metric12"));
index(ResourcePath.get("a", "bucket2"), Sets.newHashSet("metric21", "metric22"));
replay();
Set<OnmsAttribute> attributes = m_nrs.getAttributes(ResourcePath.get("a"));
assertEquals(4, attributes.size());
// Verify the properties of a specific attribute
RrdGraphAttribute metric11 = null;
for (OnmsAttribute attribute : attributes) {
if (attribute instanceof RrdGraphAttribute) {
RrdGraphAttribute graphAttribute = (RrdGraphAttribute) attribute;
if ("metric11".equals(graphAttribute.getName())) {
metric11 = graphAttribute;
}
}
}
assertNotNull(metric11);
verify();
}
use of org.opennms.netmgt.model.RrdGraphAttribute in project opennms by OpenNMS.
the class DefaultRrdDao method getLastFetchValue.
/** {@inheritDoc} */
@Override
public Double getLastFetchValue(OnmsAttribute attribute, int interval) throws DataAccessResourceFailureException {
Assert.notNull(attribute, "attribute argument must not be null");
Assert.isTrue(interval > 0, "interval argument must be greater than zero");
Assert.isAssignable(attribute.getClass(), RrdGraphAttribute.class, "attribute argument must be assignable to RrdGraphAttribute");
RrdGraphAttribute rrdAttribute = (RrdGraphAttribute) attribute;
File rrdFile = new File(m_rrdBaseDirectory, rrdAttribute.getRrdRelativePath());
try {
return m_rrdStrategy.fetchLastValue(rrdFile.getAbsolutePath(), attribute.getName(), interval);
} catch (Throwable e) {
throw new DataAccessResourceFailureException("Failure to fetch last value from file '" + rrdFile + "' with interval " + interval, e);
}
}
Aggregations