use of org.opennms.netmgt.collection.support.builder.Resource in project opennms by OpenNMS.
the class VmwareCollector method collect.
/**
* This method collect the data for a given collection agent.
*
* @param agent the collection agent
* @param parameters the parameters map
* @return the generated collection set
* @throws CollectionException
*/
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
final VmwareCollection collection = (VmwareCollection) parameters.get(VMWARE_COLLECTION_KEY);
final String vmwareManagementServer = (String) parameters.get(VMWARE_MGMT_SERVER_KEY);
final String vmwareManagedObjectId = (String) parameters.get(VMWARE_MGED_OBJECT_ID_KEY);
final VmwareServer vmwareServer = (VmwareServer) parameters.get(VMWARE_SERVER_KEY);
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
builder.withStatus(CollectionStatus.FAILED);
VmwareViJavaAccess vmwareViJavaAccess = new VmwareViJavaAccess(vmwareServer);
int timeout = ParameterMap.getKeyedInteger(parameters, "timeout", -1);
if (timeout > 0) {
if (!vmwareViJavaAccess.setTimeout(timeout)) {
logger.warn("Error setting connection timeout for VMware management server '{}'", vmwareManagementServer);
}
}
if (collection.getVmwareGroup().length < 1) {
logger.info("No groups to collect. Returning empty collection set.");
builder.withStatus(CollectionStatus.SUCCEEDED);
return builder.build();
}
try {
vmwareViJavaAccess.connect();
} catch (MalformedURLException e) {
logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
return builder.build();
} catch (RemoteException e) {
logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
return builder.build();
}
ManagedEntity managedEntity = vmwareViJavaAccess.getManagedEntityByManagedObjectId(vmwareManagedObjectId);
VmwarePerformanceValues vmwarePerformanceValues = null;
try {
vmwarePerformanceValues = vmwareViJavaAccess.queryPerformanceValues(managedEntity);
} catch (RemoteException e) {
logger.warn("Error retrieving performance values from VMware management server '" + vmwareManagementServer + "' for managed object '" + vmwareManagedObjectId + "'", e.getMessage());
vmwareViJavaAccess.disconnect();
return builder.build();
}
for (final VmwareGroup vmwareGroup : collection.getVmwareGroup()) {
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
if ("node".equalsIgnoreCase(vmwareGroup.getResourceType())) {
for (Attrib attrib : vmwareGroup.getAttrib()) {
if (!vmwarePerformanceValues.hasSingleValue(attrib.getName())) {
// warning
logger.debug("Warning! No single value for '{}' defined as single instance attribute for node {}", attrib.getName(), agent.getNodeId());
} else {
final Long value = vmwarePerformanceValues.getValue(attrib.getName());
logger.debug("Storing single instance value {}='{}' for node {}", attrib.getName(), value, agent.getNodeId());
final AttributeType type = attrib.getType();
if (type.isNumeric()) {
builder.withNumericAttribute(nodeResource, vmwareGroup.getName(), attrib.getAlias(), value, type);
} else {
builder.withStringAttribute(nodeResource, vmwareGroup.getName(), attrib.getAlias(), String.valueOf(value));
}
}
}
} else {
// multi instance value
final Set<String> instanceSet = new TreeSet<>();
final HashMap<String, Resource> resources = new HashMap<>();
for (Attrib attrib : vmwareGroup.getAttrib()) {
if (!vmwarePerformanceValues.hasInstances(attrib.getName())) {
// warning
logger.debug("Warning! No multi instance value for '{}' defined as multi instance attribute for node {}", attrib.getName(), agent.getNodeId());
} else {
Set<String> newInstances = vmwarePerformanceValues.getInstances(attrib.getName());
for (String instance : newInstances) {
if (!instanceSet.contains(instance)) {
resources.put(instance, new DeferredGenericTypeResource(nodeResource, vmwareGroup.getResourceType(), instance));
instanceSet.add(instance);
}
final AttributeType type = attrib.getType();
final Long value = vmwarePerformanceValues.getValue(attrib.getName(), instance);
logger.debug("Storing multi instance value {}[{}='{}' for node {}", attrib.getName(), instance, value, agent.getNodeId());
if (type.isNumeric()) {
builder.withNumericAttribute(resources.get(instance), vmwareGroup.getName(), attrib.getAlias(), value, type);
} else {
builder.withStringAttribute(resources.get(instance), vmwareGroup.getName(), attrib.getAlias(), Long.toString(value));
}
}
}
}
for (String instance : instanceSet) {
logger.debug("Storing multi instance value {}[{}='{}' for node {}", vmwareGroup.getResourceType() + "Name", instance, instance, agent.getNodeId());
builder.withStringAttribute(resources.get(instance), vmwareGroup.getName(), vmwareGroup.getResourceType() + "Name", instance);
}
}
}
builder.withStatus(CollectionStatus.SUCCEEDED);
vmwareViJavaAccess.disconnect();
return builder.build();
}
use of org.opennms.netmgt.collection.support.builder.Resource 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.Resource 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.Resource in project opennms by OpenNMS.
the class CollectionSetDTO method buildCollectionResources.
private Set<CollectionResource> buildCollectionResources() {
final Set<CollectionResource> collectionResources = new LinkedHashSet<>();
for (CollectionResourceDTO entry : this.collectionResources) {
final Resource resource = entry.getResource();
final AbstractCollectionResource collectionResource = CollectionSetBuilder.toCollectionResource(resource, agent);
for (Attribute<?> attribute : entry.getAttributes()) {
final AttributeGroupType groupType = new AttributeGroupType(attribute.getGroup(), AttributeGroupType.IF_TYPE_ALL);
final AbstractCollectionAttributeType attributeType = new AbstractCollectionAttributeType(groupType) {
@Override
public AttributeType getType() {
return attribute.getType();
}
@Override
public String getName() {
return attribute.getName();
}
@Override
public void storeAttribute(CollectionAttribute collectionAttribute, Persister persister) {
if (AttributeType.STRING.equals(attribute.getType())) {
persister.persistStringAttribute(collectionAttribute);
} else {
persister.persistNumericAttribute(collectionAttribute);
}
}
@Override
public String toString() {
return attribute.toString();
}
};
collectionResource.addAttribute(new AbstractCollectionAttribute(attributeType, collectionResource) {
@Override
public String getMetricIdentifier() {
return attribute.getName();
}
@Override
public Number getNumericValue() {
return attribute.getNumericValue();
}
@Override
public String getStringValue() {
return attribute.getStringValue();
}
@Override
public boolean shouldPersist(ServiceParameters params) {
return !(Boolean.FALSE.equals(disableCounterPersistence) && AttributeType.COUNTER.equals(attribute.getType()));
}
@Override
public String toString() {
return String.format("Attribute[%s:%s]", getMetricIdentifier(), attribute.getValue());
}
});
}
collectionResources.add(collectionResource);
}
return collectionResources;
}
use of org.opennms.netmgt.collection.support.builder.Resource 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();
}
Aggregations