use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.
the class CollectorRequestBuilderImpl method execute.
@Override
public CompletableFuture<CollectionSet> execute() {
if (serviceCollector == null) {
throw new IllegalArgumentException("Collector or collector class name is required.");
} else if (agent == null) {
throw new IllegalArgumentException("Agent is required.");
}
final RpcTarget target = client.getRpcTargetHelper().target().withNodeId(agent.getNodeId()).withLocation(agent.getLocationName()).withSystemId(systemId).withServiceAttributes(attributes).withLocationOverride((s) -> serviceCollector.getEffectiveLocation(s)).build();
CollectorRequestDTO request = new CollectorRequestDTO();
request.setLocation(target.getLocation());
request.setSystemId(target.getSystemId());
request.setClassName(serviceCollector.getClass().getCanonicalName());
request.setTimeToLiveMs(ttlInMs);
// Retrieve the runtime attributes, which may include attributes
// such as the agent details and other state related attributes
// which should be included in the request
final Map<String, Object> runtimeAttributes = serviceCollector.getRuntimeAttributes(agent, attributes);
final Map<String, Object> allAttributes = new HashMap<>();
allAttributes.putAll(attributes);
allAttributes.putAll(runtimeAttributes);
// Only marshal these if the request is being executed at another location.
if (MonitoringLocationUtils.isDefaultLocationName(request.getLocation())) {
// As-is
request.setAgent(agent);
request.addAttributes(allAttributes);
} else {
// Marshal
request.setAgent(new CollectionAgentDTO(agent));
final Map<String, String> marshaledParms = serviceCollector.marshalParameters(allAttributes);
marshaledParms.forEach(request::addAttribute);
request.setAttributesNeedUnmarshaling(true);
}
// Execute the request
return client.getDelegate().execute(request).thenApply(CollectorResponseDTO::getCollectionSet);
}
use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.
the class JMXCollector method collect.
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> map) {
final Map<String, String> stringMap = JmxUtils.convertToUnmodifiableStringMap(map);
final InetAddress ipaddr = agent.getAddress();
final JmxCollection jmxCollection = (JmxCollection) map.get(JMX_COLLECTION_KEY);
final MBeanServer mBeanServer = (MBeanServer) map.get(JMX_MBEAN_SERVER_KEY);
final String collectionName = ParameterMap.getKeyedString(map, ParameterName.COLLECTION.toString(), serviceName);
final String port = ParameterMap.getKeyedString(map, ParameterName.PORT.toString(), null);
final String friendlyName = ParameterMap.getKeyedString(map, ParameterName.FRIENDLY_NAME.toString(), port);
final String collDir = JmxUtils.getCollectionDirectory(stringMap, friendlyName, serviceName);
final int retries = ParameterMap.getKeyedInteger(map, ParameterName.RETRY.toString(), 3);
InetAddress ipAddr = agent.getAddress();
int nodeID = agent.getNodeId();
// Retrieve the name of the JMX data collector
final String hostAddress = InetAddressUtils.str(ipAddr);
LOG.debug("initialize: InetAddress={}, collectionName={}", hostAddress, collectionName);
JMXNodeInfo nodeInfo = new JMXNodeInfo(nodeID);
LOG.debug("nodeInfo: {} {} {}", hostAddress, nodeID, agent);
/*
* Retrieve list of MBean objects to be collected from the
* remote agent which are to be stored in the node-level RRD file.
* These objects pertain to the node itself not any individual
* interfaces.
*/
Map<String, List<Attrib>> attrMap = JMXDataCollectionConfigDao.getAttributeMap(jmxCollection, serviceName(), hostAddress);
nodeInfo.setAttributeMap(attrMap);
Map<String, JMXDataSource> dsList = buildDataSourceList(collectionName, attrMap);
nodeInfo.setDsMap(dsList);
nodeInfo.setMBeans(JMXDataCollectionConfigDao.getMBeanInfo(jmxCollection));
// Metrics collected from JMX are currently modeled as node level resources,
// but live in a sub-directory set to the service name
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId(), collDir);
// This parent resource used for generic resource
final NodeLevelResource parentResource = new NodeLevelResource(agent.getNodeId());
// Used to gather the results
final CollectionSetBuilder collectionSetBuilder = new CollectionSetBuilder(agent);
LOG.debug("connecting to {} on node ID {}", InetAddressUtils.str(ipaddr), nodeInfo.getNodeId());
try {
// create config for JmxCollector
final JmxCollectorConfig config = new JmxCollectorConfig();
config.setAgentAddress(InetAddressUtils.str(ipaddr));
config.setConnectionName(getConnectionName());
config.setRetries(retries);
config.setServiceProperties(stringMap);
config.setJmxCollection(jmxCollection);
final DefaultJmxCollector jmxCollector = new DefaultJmxCollector();
jmxCollector.collect(config, mBeanServer, new JmxSampleProcessor() {
@Override
public void process(JmxAttributeSample attributeSample, ObjectName objectName) {
final String mbeanObjectName = attributeSample.getMbean().getObjectname();
final String attributeName = attributeSample.getCollectedAttribute().getName();
final String dsKey = mbeanObjectName + "|" + attributeName;
final JMXDataSource ds = nodeInfo.getDsMap().get(dsKey);
if (ds == null) {
LOG.info("Could not find datasource for {}. Skipping.", dsKey);
return;
}
String resourceType = attributeSample.getMbean().getResourceType();
if (resourceType != null) {
final String parsedObjectName = fixGroupName(objectName.getCanonicalName());
final Resource resource = new DeferredGenericTypeResource(parentResource, resourceType, parsedObjectName);
addNumericAttributeToCollectionSet(ds, attributeSample, resource);
addStringAttributesToCollectionSet(ds, attributeSample, resource, objectName);
} else {
addNumericAttributeToCollectionSet(ds, attributeSample, nodeResource);
}
}
@Override
public void process(JmxCompositeSample compositeSample, ObjectName objectName) {
final String mbeanObjectName = compositeSample.getMbean().getObjectname();
final String attributeName = compositeSample.getCollectedAttribute().getName();
final String dsKey = mbeanObjectName + "|" + attributeName + "|" + compositeSample.getCompositeKey();
final JMXDataSource ds = nodeInfo.getDsMap().get(dsKey);
if (ds == null) {
LOG.info("Could not find datasource for {}. Skipping.", dsKey);
return;
}
String resourceType = compositeSample.getMbean().getResourceType();
if (resourceType != null) {
final String parsedObjectName = fixGroupName(objectName.getCanonicalName());
final Resource resource = new DeferredGenericTypeResource(parentResource, resourceType, parsedObjectName);
addNumericAttributeToCollectionSet(ds, compositeSample, resource);
addStringAttributesToCollectionSet(ds, compositeSample, resource, objectName);
} else {
addNumericAttributeToCollectionSet(ds, compositeSample, nodeResource);
}
}
private void addStringAttributesToCollectionSet(JMXDataSource ds, AbstractJmxSample sample, Resource resource, ObjectName objectName) {
final String groupName = fixGroupName(JmxUtils.getGroupName(stringMap, sample.getMbean()));
final String domain = objectName.getDomain();
final Hashtable<String, String> properties = objectName.getKeyPropertyList();
properties.forEach((key, value) -> collectionSetBuilder.withStringAttribute(resource, groupName, key, value));
if (domain != null) {
collectionSetBuilder.withStringAttribute(resource, groupName, "domain", objectName.getDomain());
}
}
private void addNumericAttributeToCollectionSet(JMXDataSource ds, AbstractJmxSample sample, Resource resource) {
final String groupName = fixGroupName(JmxUtils.getGroupName(stringMap, sample.getMbean()));
// Only numeric data comes back from JMX in data collection
final String valueAsString = sample.getCollectedValueAsString();
final Double value = NumericAttributeUtils.parseNumericValue(valueAsString);
// Construct the metric identifier (used by NRTG)
String metricId = groupName;
metricId = metricId.replace("_type_", ":type=");
metricId = metricId.replace("_", ".");
metricId = metricId.concat(".");
metricId = metricId.concat(ds.getName());
metricId = "JMX_".concat(metricId);
collectionSetBuilder.withIdentifiedNumericAttribute(resource, groupName, ds.getName(), value, ds.getType(), metricId);
}
});
} catch (final Exception e) {
LOG.debug("{} Collector.collect: IOException while collecting address: {}", serviceName, agent.getAddress(), e);
}
return collectionSetBuilder.build();
}
use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.
the class JMXCollectorTest method collectJvmDefaultComposites.
/**
* Check if CompositeAttributes will be collected
*/
@Test
public void collectJvmDefaultComposites() {
String mBeansObjectName = "java.lang:type=GarbageCollector,name=PS MarkSweep";
Map<String, BeanInfo> mBeans = new HashMap<String, BeanInfo>();
BeanInfo beanInfo = new BeanInfo();
beanInfo.setObjectName(mBeansObjectName);
List<String> attributes = new ArrayList<>();
attributes.add("CollectionCount");
attributes.add("LastGcInfo");
beanInfo.setAttributes(attributes);
List<String> compositeAttributes = new ArrayList<>();
compositeAttributes.add("LastGcInfo");
beanInfo.setCompositeAttributes(compositeAttributes);
mBeans.put("first", beanInfo);
jmxNodeInfo.setMBeans(mBeans);
Map<String, JMXDataSource> dataSourceMap = new HashMap<String, JMXDataSource>();
dataSourceMap.put(mBeansObjectName + "|CollectionCount", new JMXDataSource());
dataSourceMap.put(mBeansObjectName + "|LastGcInfo", new JMXDataSource());
jmxNodeInfo.setDsMap(dataSourceMap);
CollectionSet collectionSet = jmxCollector.collect(collectionAgent, Collections.emptyMap());
assertEquals("Collection of one Jvm default value failed", CollectionStatus.SUCCEEDED, collectionSet.getStatus());
}
use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.
the class JMXCollectorTest method collect.
private Map<String, Map<String, CollectionAttribute>> collect(String collectionName) {
final Map<String, Object> parms = new HashMap<String, Object>();
parms.put(ParameterName.COLLECTION.toString(), collectionName);
parms.putAll(jmxCollector.getRuntimeAttributes(collectionAgent, parms));
// start collection
final CollectionSet collectionSet = jmxCollector.collect(collectionAgent, parms);
assertEquals("Collection: " + collectionName + " failed", CollectionStatus.SUCCEEDED, collectionSet.getStatus());
return CollectionSetUtils.getAttributesByNameByGroup(collectionSet);
}
use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.
the class JMXCollectorTest method collectJvmMbeansWithWildCard.
@Test
public void collectJvmMbeansWithWildCard() {
final Map<String, Object> parms = new HashMap<String, Object>();
parms.put(ParameterName.COLLECTION.toString(), "collectBasicJvmValues");
parms.putAll(jmxCollector.getRuntimeAttributes(collectionAgent, parms));
CollectionSet collectionSet = jmxCollector.collect(collectionAgent, parms);
assertEquals("Collection of jvm values failed", CollectionStatus.SUCCEEDED, collectionSet.getStatus());
ResourceType rt = mock(ResourceType.class, RETURNS_DEEP_STUBS);
when(rt.getName()).thenReturn("jvm");
when(rt.getStorageStrategy().getClazz()).thenReturn(MockStorageStrategy.class.getCanonicalName());
when(rt.getPersistenceSelectorStrategy().getClazz()).thenReturn(MockPersistenceSelectorStrategy.class.getCanonicalName());
ResourceTypeMapper.getInstance().setResourceTypeMapper((name) -> rt);
Map<String, Map<String, CollectionAttribute>> attributesByNameByGroup = CollectionSetUtils.getAttributesByNameByGroup(collectionSet);
// 2 attributes from the defined Numeric attributes and 3 string
// attributes, domain, type, name
assertEquals(5, attributesByNameByGroup.get("java_lang_type_GarbageCollector_name__").size());
Map<String, CollectionAttribute> attributes = attributesByNameByGroup.get("java_lang_type_GarbageCollector_name__");
CollectionAttribute attribute1 = attributes.get("domain");
CollectionAttribute attribute2 = attributes.get("type");
assertEquals("java.lang", attribute1.getStringValue());
assertEquals("GarbageCollector", attribute2.getStringValue());
}
Aggregations