use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.
the class VmwareCimCollector 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 VmwareCimCollection collection = (VmwareCimCollection) 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.getVmwareCimGroup().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();
}
HostSystem hostSystem = vmwareViJavaAccess.getHostSystemByManagedObjectId(vmwareManagedObjectId);
String powerState = null;
if (hostSystem == null) {
logger.debug("hostSystem=null");
} else {
HostRuntimeInfo hostRuntimeInfo = hostSystem.getRuntime();
if (hostRuntimeInfo == null) {
logger.debug("hostRuntimeInfo=null");
} else {
HostSystemPowerState hostSystemPowerState = hostRuntimeInfo.getPowerState();
if (hostSystemPowerState == null) {
logger.debug("hostSystemPowerState=null");
} else {
powerState = hostSystemPowerState.toString();
}
}
}
logger.debug("The power state for host system '{}' is '{}'", vmwareManagedObjectId, powerState);
if ("poweredOn".equals(powerState)) {
HashMap<String, List<CIMObject>> cimObjects = new HashMap<String, List<CIMObject>>();
for (final VmwareCimGroup vmwareCimGroup : collection.getVmwareCimGroup()) {
String cimClass = vmwareCimGroup.getCimClass();
if (!cimObjects.containsKey(cimClass)) {
List<CIMObject> cimList = null;
try {
cimList = vmwareViJavaAccess.queryCimObjects(hostSystem, cimClass, InetAddressUtils.str(agent.getAddress()));
} catch (Exception e) {
logger.warn("Error retrieving CIM values from host system '{}'. Error message: '{}'", vmwareManagedObjectId, e.getMessage());
return builder.build();
} finally {
vmwareViJavaAccess.disconnect();
}
cimObjects.put(cimClass, cimList);
}
final List<CIMObject> cimList = cimObjects.get(cimClass);
if (cimList == null) {
logger.warn("Error getting objects of CIM class '{}' from host system '{}'", cimClass, vmwareManagedObjectId);
continue;
}
String keyAttribute = vmwareCimGroup.getKey();
String attributeValue = vmwareCimGroup.getValue();
String instanceAttribute = vmwareCimGroup.getInstance();
for (CIMObject cimObject : cimList) {
boolean addObject = false;
if (keyAttribute != null && attributeValue != null) {
String cimObjectValue = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, keyAttribute);
if (attributeValue.equals(cimObjectValue)) {
addObject = true;
} else {
addObject = false;
}
} else {
addObject = true;
}
if (addObject) {
final String instance = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, instanceAttribute);
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
final Resource resource = new DeferredGenericTypeResource(nodeResource, vmwareCimGroup.getResourceType(), instance);
for (Attrib attrib : vmwareCimGroup.getAttrib()) {
final AttributeType type = attrib.getType();
String value = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, attrib.getName());
if (valueModifiers.containsKey(attrib.getName())) {
String modifiedValue = valueModifiers.get(attrib.getName()).modifyValue(attrib.getName(), value, cimObject, vmwareViJavaAccess);
logger.debug("Applying value modifier for instance value " + attrib.getName() + "[" + instance + "]='" + value + "' => '" + modifiedValue + "' for node " + agent.getNodeId());
value = modifiedValue;
}
builder.withAttribute(resource, vmwareCimGroup.getName(), attrib.getAlias(), value, type);
}
}
}
}
builder.withStatus(CollectionStatus.SUCCEEDED);
}
vmwareViJavaAccess.disconnect();
return builder.build();
}
use of org.opennms.netmgt.collection.support.builder.NodeLevelResource 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.NodeLevelResource in project opennms by OpenNMS.
the class ThresholdingVisitorIT method testThresholdsFiltersOnNodeResourceWithCollectionSetBuilder.
/**
* Similar to {@link #testThresholdsFiltersOnNodeResource()}, but
* we generate the collection set using the CollectionSetBuilder instead
* of using SnmpCollector specific types.
*/
@Test
public void testThresholdsFiltersOnNodeResourceWithCollectionSetBuilder() throws Exception {
initFactories("/threshd-configuration.xml", "/test-thresholds-5.xml");
ThresholdingVisitor visitor = createVisitor();
// Adding Expected Thresholds
addHighThresholdEvent(1, 30, 25, 50, "/home", "node", "(hda1_hrStorageUsed/hda1_hrStorageSize)*100", null, null);
addHighThresholdEvent(1, 50, 45, 60, "/opt", "node", "(hda2_hrStorageUsed/hda2_hrStorageSize)*100", null, null);
// Creating Node ResourceType
SnmpCollectionAgent agent = createCollectionAgent();
NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
CollectionSet collectionSet = new CollectionSetBuilder(agent).withNumericAttribute(nodeResource, "hd-usage", "hda1_hrStorageUsed", 50, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda1_hrStorageSize", 100, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda2_hrStorageUsed", 60, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda2_hrStorageSize", 100, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda3_hrStorageUsed", 70, AttributeType.GAUGE).withNumericAttribute(nodeResource, "hd-usage", "hda3_hrStorageSize", 100, AttributeType.GAUGE).build();
// Creating strings.properties file
ResourcePath path = ResourcePath.get("snmp", "1");
m_resourceStorageDao.setStringAttribute(path, "hda1_hrStorageDescr", "/home");
m_resourceStorageDao.setStringAttribute(path, "hda2_hrStorageDescr", "/opt");
m_resourceStorageDao.setStringAttribute(path, "hda3_hrStorageDescr", "/usr");
// Run Visitor and Verify Events
collectionSet.visit(visitor);
EasyMock.verify(agent);
verifyEvents(0);
}
use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.
the class CollectableServiceTest method canWrapResourcesWithTimekeeper.
/**
* Validates that we can successfully wrap collection sets with a custom time-keeper,
* allowing us to override the timestamp of the attributes within the collection set.
*/
@Test
public void canWrapResourcesWithTimekeeper() throws CollectionInitializationException, CollectionException, IOException, RrdException {
System.setProperty(CollectableService.USE_COLLECTION_START_TIME_SYS_PROP, Boolean.TRUE.toString());
createCollectableService();
long collectionDelayInSecs = 2;
when(spec.collect(any())).then(new Answer<CollectionSet>() {
@Override
public CollectionSet answer(InvocationOnMock invocation) throws InterruptedException {
Thread.sleep(collectionDelayInSecs * 1000);
CollectionAgent agent = (CollectionAgent) invocation.getArguments()[0];
NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
return new CollectionSetBuilder(agent).withNumericAttribute(nodeResource, "mibGroup", "myCounter", 1000, AttributeType.COUNTER).build();
}
});
File nodeDir = fileAnticipator.expecting(getSnmpRrdDirectory(), "1");
File jrbFile = fileAnticipator.expecting(nodeDir, "myCounter" + rrdStrategy.getDefaultFileExtension());
fileAnticipator.expecting(nodeDir, "myCounter" + ".meta");
long beforeInMs = System.currentTimeMillis();
service.run();
long afterInMs = System.currentTimeMillis();
// Quick sanity check
assertTrue(String.format("Delay was not succesfully applied (delay was %d).", beforeInMs - afterInMs), afterInMs - beforeInMs >= collectionDelayInSecs * 1000);
// Verify the last update time match the start of the collection time
RrdDb rrdDb = new RrdDb(jrbFile);
long lastUpdateTimeInSecs = rrdDb.getLastUpdateTime();
long beforeInSecs = Math.floorDiv(beforeInMs, 1000);
long afterInSecs = Math.floorDiv(afterInMs, 1000) + 1;
assertTrue("Last update was before the collector was invoked!", lastUpdateTimeInSecs >= beforeInSecs);
assertTrue("Last update was too long after the collector was invoked!", lastUpdateTimeInSecs < (afterInSecs - (collectionDelayInSecs / 2d)));
}
use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.
the class XmpCollector method collect.
/**
* {@inheritDoc}
*
* Collect data, via XMP, from a particular agent EventProxy is
* used to send opennms events into the system in case a
* collection fails or if a system is back working again after a
* failure (suceed event). But otherwise, no events sent if
* collection succeeds. Collect is called once per agent per
* collection cycle. Parameters are a map of String Key/String
* Value passed in. Keys come from collectd config
* @throws CollectionException
*/
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
CollectionSetBuilder collectionSetBuilder;
XmpSession session;
long oldUptime;
int i;
XmpCollection collection;
LOG.debug("collect agent {}", agent);
oldUptime = 0;
// First go to the peer factory
XmpAgentConfig peerConfig = XmpPeerFactory.getInstance().getAgentConfig(agent.getAddress());
authenUser = peerConfig.getAuthenUser();
timeout = (int) peerConfig.getTimeout();
retries = peerConfig.getRetry();
xmpPort = peerConfig.getPort();
if (parameters.get("authenUser") != null)
authenUser = ParameterMap.getKeyedString(parameters, "authenUser", null);
if (parameters.get("timeout") != null) {
timeout = ParameterMap.getKeyedInteger(parameters, "timeout", 3000);
}
if (parameters.get("retry") != null) {
retries = ParameterMap.getKeyedInteger(parameters, "retries", 0);
}
parameters.get("collection");
if (parameters.get("port") != null) {
xmpPort = Integer.valueOf((String) parameters.get("port"));
}
// log().debug("collect got parameters for "+agent);
String collectionName = ParameterMap.getKeyedString(parameters, "collection", null);
// this would/will come from xmp-datacollection.xml
if (collectionName == null) {
// log this!
LOG.warn("collect found no collectionName for {}", agent);
return null;
}
// log().debug("collect got collectionName for "+agent);
LOG.debug("XmpCollector: collect {} from {}", collectionName, agent);
// get/create our collections set
collectionSetBuilder = new CollectionSetBuilder(agent).withStatus(// default to failed
CollectionStatus.FAILED).disableCounterPersistence(// don't persist counters by default
true);
// default collection resource for putting scalars in
final NodeLevelResource nodeLevelResource = new NodeLevelResource(agent.getNodeId());
// get the collection, again, from the data config file factory
// because it could have changed; its not necessarily re-parsed,
// but we are getting another copy of it for each agent
// that we are queried each time we are invoked
collection = XmpCollectionFactory.getInstance().getXmpCollection(collectionName);
if (collection == null) {
LOG.warn("collect found no matching collection for {}", agent);
return collectionSetBuilder.build();
}
if (collection.getGroups().getGroup().length < 1) {
LOG.info("No groups to collect.");
return collectionSetBuilder.withStatus(CollectionStatus.SUCCEEDED).build();
}
oldUptime = agent.getSavedSysUpTime();
// open/get a session with the target agent
LOG.debug("collect: attempting to open XMP session with {}:{},{}", agent.getAddress(), xmpPort, authenUser);
// Set the SO_TIMEOUT, why don't we...
sockopts.setConnectTimeout(timeout);
session = new XmpSession(sockopts, agent.getAddress(), xmpPort, authenUser);
if (session.isClosed()) {
LOG.warn("collect unable to open XMP session with {}", agent);
return collectionSetBuilder.build();
}
LOG.debug("collect: successfully opened XMP session with{}", agent);
for (Group group : collection.getGroups().getGroup()) {
// get name of group and MIB objects in group
String groupName = group.getName();
MibObj[] mibObjects = group.getMibObj();
XmpVar[] vars = new XmpVar[mibObjects.length];
LOG.debug("collecting XMP group {} with {} mib objects", groupName, mibObjects.length);
// prepare the query vars
for (i = 0; i < mibObjects.length; i++) {
vars[i] = new XmpVar(mibObjects[i].getMib(), mibObjects[i].getVar(), mibObjects[i].getInstance(), "", Xmp.SYNTAX_NULLSYNTAX);
}
if ((mibObjects[0].getTable() != null) && (mibObjects[0].getTable().length() != 0)) {
String[] tableInfo = new String[3];
tableInfo[0] = mibObjects[0].getMib();
tableInfo[1] = mibObjects[0].getTable();
tableInfo[2] = mibObjects[0].getInstance();
// tabular query
if (handleTableQuery(group.getName(), group.getResourceType(), agent, collectionSetBuilder, tableInfo, session, nodeLevelResource, vars) == false) {
session.closeSession();
return collectionSetBuilder.build();
}
} else {
// scalar query
if (handleScalarQuery(group.getName(), agent, collectionSetBuilder, oldUptime, session, nodeLevelResource, vars) == false) {
session.closeSession();
return collectionSetBuilder.build();
}
}
}
/* for each Group in collection Group list */
// done talking to this agent; close session
session.closeSession();
// Did agent restart since last query? If so, set
// ignorePersist to true; our scalar
// query will have handled this by searching returned
// MIB objects for sysUpTime
// WARNING, EACH COLLECTION SHOULD HAVE A SCALAR QUERY THAT
// INCLUDES Core.sysUpTime
collectionSetBuilder.withStatus(CollectionStatus.SUCCEEDED);
LOG.debug("XMP collect finished for {}, uptime for {} is {}", collectionName, agent, agent.getSavedSysUpTime());
return collectionSetBuilder.build();
}
Aggregations