use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.
the class XmpCollector method handleTableQuery.
/* handleScalarQuery */
// handle a tabular query, save each row in its own
// collection resource
private boolean handleTableQuery(String groupName, String resourceType, CollectionAgent agent, CollectionSetBuilder collectionSetBuilder, String[] tableInfo, XmpSession session, NodeLevelResource nodeLevelResource, XmpVar[] queryVars) throws CollectionException {
int numColumns, numRows;
XmpMessage reply;
int i, j;
XmpVar[] vars;
String targetInstance;
numColumns = queryVars.length;
// make sure we have an instance or * for all rows; preserve
// passed in value as targetInstance so we know if we are
// are going to use targetInstance for saving results or
// use returned instance for saving values
// if resourceType is present, we use it as a subDir in
// our RRD dir
targetInstance = tableInfo[2];
if ((tableInfo[2] == null) || (tableInfo[2].length() == 0)) {
tableInfo[2] = new String("*");
targetInstance = null;
}
LOG.debug("sending table query {},{},{} target: {}", tableInfo[0], tableInfo[1], tableInfo[2], targetInstance);
reply = session.queryTableVars(tableInfo, 0, queryVars);
if (reply == null) {
LOG.warn("collect: query to {} failed, {}", agent, Xmp.errorStatusToString(session.getErrorStatus()));
return false;
}
vars = reply.getMIBVars();
// we have to go through the reply and find out how
// many rows we have
// for each row: create a CollectionResource of
// appropriate type, instance, etc.
// create AttributeGroup to put
// the values in
numRows = vars.length / numColumns;
LOG.info("query returned valid table data for {} numRows={} numColumns={}", groupName, numRows, numColumns);
for (i = 0; i < numRows; i++) {
String rowInstance;
// determine instance for this row
// we use either the rowInstance or targetInstance for
// naming the instance for saving RRD file; if user
// wanted all rows (blank instance), then we will use
// the returned instance; if user specified an instance
// we use that instance for specifying the RRD file
// and collection resource
rowInstance = vars[i * numColumns].getKey();
// instead of using '*' for the nodeTypeName, use the
// table name so that the proper rrd file is spec'd
final String instanceName;
if (targetInstance != null) {
instanceName = targetInstance;
} else {
instanceName = rowInstance;
}
// node type can be "node" for scalars or
// "if" for network interface resources and
// "*" for all other resource types
final String nodeTypeName = tableInfo[1];
final Resource resource = getResource(nodeLevelResource, nodeTypeName, resourceType, instanceName);
LOG.debug("queryTable instance={}", rowInstance);
for (j = 0; j < numColumns; j++) {
final XmpVar var = vars[i * numColumns + j];
collectionSetBuilder.withAttribute(resource, groupName, var.getObjName(), var.getValue(), getType(var));
}
/* for each column */
}
return true;
}
use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.
the class WmiCollector method collect.
/**
* {@inheritDoc}
*/
@Override
public CollectionSet collect(final CollectionAgent agent, final Map<String, Object> parameters) {
// Find attributes to collect - check groups in configuration. For each,
// check scheduled nodes to see if that group should be collected
final WmiCollection collection = (WmiCollection) parameters.get(WMI_COLLECTION_KEY);
final WmiAgentConfig agentConfig = (WmiAgentConfig) parameters.get(WMI_AGENT_CONFIG_KEY);
final WmiAgentState agentState = new WmiAgentState(agent.getAddress(), agentConfig, parameters);
// Create a new collection set.
CollectionSetBuilder builder = new CollectionSetBuilder(agent).withStatus(CollectionStatus.FAILED);
if (collection.getWpms().size() < 1) {
LOG.info("No groups to collect.");
return builder.withStatus(CollectionStatus.SUCCEEDED).build();
}
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
// Iterate through the WMI collection groups.
for (final Wpm wpm : collection.getWpms()) {
// A wpm consists of a list of attributes, identified by name
if (agentState.shouldCheckAvailability(wpm.getName(), wpm.getRecheckInterval())) {
if (!isGroupAvailable(agentState, wpm)) {
continue;
}
}
if (agentState.groupIsAvailable(wpm.getName())) {
WmiClient client = null;
// Collect the data
try {
// Tell the agent to connect
agentState.connect(wpm.getWmiNamespace());
// And retrieve the client object for working.
client = (WmiClient) agentState.getWmiClient();
// Retrieve the WbemObjectSet from the class defined on the group.
final OnmsWbemObjectSet wOS = client.performInstanceOf(wpm.getWmiClass());
// If we received a WbemObjectSet result, lets go through it and collect it.
if (wOS != null) {
// Go through each object (class instance) in the object set.
for (int i = 0; i < wOS.count(); i++) {
// Create a new collection resource.
Resource resource = null;
// Fetch our WBEM Object
final OnmsWbemObject obj = wOS.get(i);
// If this is multi-instance, fetch the instance name and store it.
if (wOS.count() > 1) {
// Fetch the value of the key value. e.g. Name.
final OnmsWbemProperty prop = obj.getWmiProperties().getByName(wpm.getKeyvalue());
final Object propVal = prop.getWmiValue();
String instance = null;
if (propVal instanceof String) {
instance = (String) propVal;
} else {
instance = propVal.toString();
}
resource = getWmiResource(agent, wpm.getResourceType(), nodeResource, instance);
} else {
resource = nodeResource;
}
for (final Attrib attrib : wpm.getAttribs()) {
final OnmsWbemProperty prop = obj.getWmiProperties().getByName(attrib.getWmiObject());
final AttributeType type = attrib.getType();
final String stringValue = prop.getWmiValue().toString();
if (type.isNumeric()) {
Double numericValue = Double.NaN;
try {
numericValue = Double.parseDouble(stringValue);
} catch (NumberFormatException e) {
LOG.warn("Value '{}' for attribute named '{}' cannot be converted to a number. Skipping.", prop.getWmiValue(), attrib.getName());
continue;
}
builder.withNumericAttribute(resource, wpm.getName(), attrib.getAlias(), numericValue, type);
} else {
builder.withStringAttribute(resource, wpm.getName(), attrib.getAlias(), stringValue);
}
}
}
}
builder.withStatus(CollectionStatus.SUCCEEDED);
} catch (final WmiException e) {
LOG.info("unable to collect params for wpm '{}'", wpm.getName(), e);
} finally {
if (client != null) {
try {
client.disconnect();
} catch (final WmiException e) {
LOG.warn("An error occurred disconnecting while collecting from WMI.", e);
}
}
}
}
}
return builder.build();
}
use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.
the class EvaluateStatsIT method testPersister.
/**
* Test persister.
*
* @throws Exception the exception
*/
@Test
public void testPersister() throws Exception {
RrdRepository repo = new RrdRepository();
repo.setRrdBaseDir(new File("/tmp"));
EvaluateGroupPersister persister = new EvaluateGroupPersister(stats, new ServiceParameters(new HashMap<String, Object>()), repo);
MockCollectionAgent agent = new MockCollectionAgent(1, "node.local", "Test", "001", InetAddressUtils.addr("127.0.0.1"));
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
NodeLevelResource node = new NodeLevelResource(agent.getNodeId());
InterfaceLevelResource eth0 = new InterfaceLevelResource(node, "eth0");
builder.withNumericAttribute(eth0, "mib2-interfaces", "ifInErrors", 0.0, AttributeType.COUNTER);
builder.withNumericAttribute(eth0, "mib2-interfaces", "ifOutErrors", 0.0, AttributeType.COUNTER);
builder.withNumericAttribute(eth0, "mib2-X-interfaces", "ifHCInOctets", 100.0, AttributeType.COUNTER);
builder.withNumericAttribute(eth0, "mib2-X-interfaces", "ifHCOutOctets", 100.0, AttributeType.COUNTER);
builder.withStringAttribute(eth0, "mib2-X-interfaces", "ifHighSpeed", "1000");
InterfaceLevelResource eth1 = new InterfaceLevelResource(node, "eth1");
builder.withNumericAttribute(eth1, "mib2-interfaces", "ifInErrors", 0.0, AttributeType.COUNTER);
builder.withNumericAttribute(eth1, "mib2-interfaces", "ifOutErrors", 0.0, AttributeType.COUNTER);
builder.withNumericAttribute(eth1, "mib2-X-interfaces", "ifHCInOctets", 100.0, AttributeType.COUNTER);
builder.withNumericAttribute(eth1, "mib2-X-interfaces", "ifHCOutOctets", 100.0, AttributeType.COUNTER);
builder.withStringAttribute(eth1, "mib2-X-interfaces", "ifHighSpeed", "1000");
builder.build().visit(persister);
stats.dumpCache();
Assert.assertEquals(1, registry.getGauges().get("evaluate.nodes").getValue());
Assert.assertEquals(2, registry.getGauges().get("evaluate.resources").getValue());
Assert.assertEquals(4, registry.getGauges().get("evaluate.groups").getValue());
Assert.assertEquals(8, registry.getGauges().get("evaluate.numeric-attributes").getValue());
Assert.assertEquals(2, registry.getGauges().get("evaluate.string-attributes").getValue());
Assert.assertEquals(8, registry.getMeters().get("evaluate.samples").getCount());
}
use of org.opennms.netmgt.collection.support.builder.NodeLevelResource 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.support.builder.NodeLevelResource in project opennms by OpenNMS.
the class TcaCollectionHandler method process.
/**
* Process.
*
* <p>A sample TCA Data looks like the following:</p>
* <ul>
* <li>OID=.1.3.6.1.4.1.27091.3.1.6.1.1.172.19.37.60.1, Type=OctetString, Value=172.19.37.60 </li>
* <li>OID=.1.3.6.1.4.1.27091.3.1.6.1.2.172.19.37.60.1, Type=OctetString, Value=
* |25|1327451762,11,0,11,0,1|1327451763,11,0,11,0,1|1327451764,11,0,11,0,1|1327451765,11,0,11,0,1|1327451766,11,0,11,0,1|
* 1327451767,11,0,11,0,1|1327451768,11,0,11,0,1|1327451769,11,0,11,0,1|1327451770,11,0,11,0,1|1327451771,11,0,11,0,1|
* 1327451772,11,0,11,0,1|1327451773,11,0,11,0,1|1327451774,11,0,11,0,1|1327451775,11,0,11,0,1|1327451776,11,0,11,0,1|
* 1327451777,11,0,11,0,1|1327451778,11,0,11,0,1|1327451779,11,0,11,0,1|1327451780,11,0,11,0,1|1327451781,11,0,11,0,1|
* 1327451782,11,0,11,0,1|1327451783,11,0,11,0,1|1327451784,11,0,11,0,1|1327451785,11,0,11,0,1|1327451786,11,0,11,0,1|</li>
* </ul>
*
* <ul>
* <li>timestamp (epoch)</li>
* <li>delay local-remote ~ current inbound-delay</li>
* <li>jitter local-remote ~ current inbound-jitter</li>
* <li>delay remote-local ~ current outbound-delay</li>
* <li>jitter remote-local ~ current outbound-jitter-</li>
* <li>timesync status (1 = good, time is synced, 0 = bad, out-of sync)</li>
* </ul>
*
* @param tracker the tracker
* @throws Exception the exception
*/
private void process(TcaData tracker, CollectionSetBuilder builder) {
LOG.debug("process: processing raw TCA data for {} peers.", tracker.size());
final NodeLevelResource nodeResource = new NodeLevelResource(m_agent.getNodeId());
long timestamp = 0;
for (TcaDataEntry entry : tracker.getEntries()) {
GenericTypeResource resource = new GenericTypeResource(nodeResource, m_resourceType, entry.getPeerAddress());
CollectionResource collectionResource = CollectionSetBuilder.toCollectionResource(resource, m_agent);
long lastTimestamp = getLastTimestamp(collectionResource);
String[] rawData = entry.getRawData().split("\\|");
int samples = Integer.parseInt(rawData[1]);
SnmpObjId entryObjId = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2", entry.getInstance().toString());
String identifierPrefix = String.format("TCA_%s_", entryObjId);
for (int i = 0; i < samples; i++) {
LOG.debug("process: processing row {}: {}", i, rawData[2 + i]);
String[] rawEntry = rawData[2 + i].split(",");
timestamp = Long.parseLong(rawEntry[0]);
if (timestamp > lastTimestamp) {
resource = new GenericTypeResource(nodeResource, m_resourceType, entry.getPeerAddress());
resource.setTimestamp(new Date(timestamp * 1000));
builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, INBOUND_DELAY, Double.parseDouble(rawEntry[1]), AttributeType.GAUGE, identifierPrefix + INBOUND_DELAY);
builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, INBOUND_JITTER, Double.parseDouble(rawEntry[2]), AttributeType.GAUGE, identifierPrefix + INBOUND_JITTER);
builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, OUTBOUND_DELAY, Double.parseDouble(rawEntry[3]), AttributeType.GAUGE, identifierPrefix + OUTBOUND_DELAY);
builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, OUTBOUND_JITTER, Double.parseDouble(rawEntry[4]), AttributeType.GAUGE, identifierPrefix + OUTBOUND_JITTER);
builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, TIMESYNC_STATUS, Double.parseDouble(rawEntry[5]), AttributeType.GAUGE, identifierPrefix + TIMESYNC_STATUS);
} else {
LOG.debug("process: skipping row {} {} because it was already processed.", i, rawData[2 + i]);
}
}
setLastTimestamp(collectionResource, timestamp);
}
}
Aggregations