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;
}
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);
}
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;
}
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);
}
}
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));
}
Aggregations