use of org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource in project opennms by OpenNMS.
the class CollectionSetDTOTest method data.
@Parameters
public static Collection<Object[]> data() throws ParseException {
CollectionAgent collectionAgent = mock(CollectionAgent.class);
NodeLevelResource nodeLevelResource = new NodeLevelResource(1);
InterfaceLevelResource interfaceLevelResource = new InterfaceLevelResource(nodeLevelResource, "eth0");
ResourceType rt = mock(ResourceType.class, RETURNS_DEEP_STUBS);
when(rt.getName()).thenReturn("Charles");
when(rt.getStorageStrategy().getClazz()).thenReturn(MockStorageStrategy.class.getCanonicalName());
when(rt.getPersistenceSelectorStrategy().getClazz()).thenReturn(MockPersistenceSelectorStrategy.class.getCanonicalName());
DeferredGenericTypeResource deferredGenericTypeResource = new DeferredGenericTypeResource(nodeLevelResource, "Charles", "id");
GenericTypeResource genericTypeResource = new GenericTypeResource(nodeLevelResource, rt, "idx");
genericTypeResource.setTimestamp(new Date(0));
ResourceTypeMapper.getInstance().setResourceTypeMapper((name) -> rt);
// For complete coverage make sure that there is at least one attribute
// for every different resource type, and that every different type
// of attribute is represented at least once
CollectionSet collectionSet = new CollectionSetBuilder(collectionAgent).withTimestamp(new Date(0)).withNumericAttribute(nodeLevelResource, "ucd-sysstat", "CpuRawIdle", 99, AttributeType.GAUGE).withNumericAttribute(interfaceLevelResource, "mib2-X-interfaces", "ifHCInOctets", 1001, AttributeType.COUNTER).withStringAttribute(interfaceLevelResource, "mib2-X-interfaces", "ifDescr", "LAN").withIdentifiedNumericAttribute(deferredGenericTypeResource, "net-snmp-disk", "ns-dsk1", 1024, AttributeType.GAUGE, "some-oid").withIdentifiedNumericAttribute(genericTypeResource, "net-snmp-disk", "ns-dskTotal", 1024, AttributeType.GAUGE, "some-oid").build();
return Arrays.asList(new Object[][] { { collectionSet, "<collection-set status=\"SUCCEEDED\" timestamp=\"" + StringUtils.iso8601OffsetString(new Date(0), ZoneId.systemDefault(), ChronoUnit.SECONDS) + "\">\n" + " <agent type=\"0\" store-by-fs=\"false\" node-id=\"0\" sys-up-time=\"0\"/>\n" + " <collection-resource>\n" + " <node-level-resource node-id=\"1\"/>\n" + " <numeric-attribute group=\"ucd-sysstat\" name=\"CpuRawIdle\" type=\"gauge\" value=\"99\"/>\n" + " </collection-resource>\n" + " <collection-resource>\n" + " <interface-level-resource if-name=\"eth0\">\n" + " <node-level-resource node-id=\"1\"/>\n" + " </interface-level-resource>\n" + " <numeric-attribute group=\"mib2-X-interfaces\" name=\"ifHCInOctets\" type=\"counter\" value=\"1001\"/>\n" + " <string-attribute group=\"mib2-X-interfaces\" name=\"ifDescr\" type=\"string\" value=\"LAN\"/>\n" + " </collection-resource>\n" + " <collection-resource>\n" + " <generic-type-resource name=\"Charles\" instance=\"id\">\n" + " <node-level-resource node-id=\"1\"/>\n" + " </generic-type-resource>\n" + " <numeric-attribute group=\"net-snmp-disk\" name=\"ns-dsk1\" type=\"gauge\" identifier=\"some-oid\" value=\"1024\"/>\n" + " </collection-resource>\n" + " <collection-resource>\n" + " <generic-type-resource name=\"Charles\" instance=\"idx\" timestamp=\"" + StringUtils.iso8601OffsetString(new Date(0), ZoneId.systemDefault(), ChronoUnit.SECONDS) + "\">\n" + " <node-level-resource node-id=\"1\"/>\n" + " </generic-type-resource>\n" + " <numeric-attribute group=\"net-snmp-disk\" name=\"ns-dskTotal\" type=\"gauge\" identifier=\"some-oid\" value=\"1024\"/>\n" + " </collection-resource>\n" + "</collection-set>" } });
}
use of org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource in project opennms by OpenNMS.
the class JdbcCollector method collect.
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
final JdbcDataCollection collection = (JdbcDataCollection) parameters.get(JDBC_COLLECTION_KEY);
JdbcAgentState agentState = null;
Connection con = null;
ResultSet results = null;
Statement stmt = null;
try {
agentState = createAgentState(agent.getAddress(), parameters);
agentState.setupDatabaseConnections(parameters);
// Create a new collection set.
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
// Creating a single resource object, because all node-level metric must belong to the exact same resource.
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
// Cycle through all of the queries for this collection
for (JdbcQuery query : collection.getQueries()) {
// Verify if we should check for availability of a query.
if (agentState.shouldCheckAvailability(query.getQueryName(), query.getRecheckInterval())) {
// Check to see if the query is available.
if (!isGroupAvailable(agentState, query)) {
LOG.warn("Group is not available.");
continue;
}
}
try {
// If the query is available, lets collect it.
if (agentState.groupIsAvailable(query.getQueryName())) {
if (agentState.getUseDataSourceName()) {
initDatabaseConnectionFactory(agentState.getDataSourceName());
con = DataSourceFactory.getInstance(agentState.getDataSourceName()).getConnection();
} else {
con = agentState.getJdbcConnection();
}
stmt = agentState.createStatement(con);
results = agentState.executeJdbcQuery(stmt, query);
// Determine if there were any results for this query to
if (results.isBeforeFirst() && results.isAfterLast()) {
LOG.warn("Query '{}' returned no results.", query.getQueryName());
// Close the statement, but retain the connection.
agentState.closeResultSet(results);
agentState.closeStmt(stmt);
continue;
}
// Determine if there are results and how many.
results.last();
boolean singleInstance = (results.getRow() == 1) ? true : false;
results.beforeFirst();
// Iterate through each row.
while (results.next()) {
Resource resource = null;
// Create the appropriate resource container.
if (singleInstance) {
resource = nodeResource;
} else {
// Retrieve the name of the column to use as the instance key for multi-row queries.
String instance = results.getString(query.getInstanceColumn());
resource = new DeferredGenericTypeResource(nodeResource, query.getResourceType(), instance);
}
for (JdbcColumn curColumn : query.getJdbcColumns()) {
final AttributeType type = curColumn.getDataType();
String columnName = null;
if (curColumn.getDataSourceName() != null && curColumn.getDataSourceName().length() != 0) {
columnName = curColumn.getDataSourceName();
} else {
columnName = curColumn.getColumnName();
}
String columnValue = results.getString(columnName);
if (columnValue == null) {
LOG.debug("Skipping column named '{}' with null value.", curColumn.getColumnName());
continue;
}
if (type.isNumeric()) {
Double numericValue = Double.NaN;
try {
numericValue = Double.parseDouble(columnValue);
} catch (NumberFormatException e) {
LOG.warn("Value '{}' for column named '{}' cannot be converted to a number. Skipping.", columnValue, curColumn.getColumnName());
continue;
}
builder.withNumericAttribute(resource, query.getQueryName(), curColumn.getAlias(), numericValue, type);
} else {
builder.withStringAttribute(resource, query.getQueryName(), curColumn.getAlias(), columnValue);
}
}
}
}
} catch (SQLException e) {
// Close the statement but retain the connection, log the exception and continue to the next query.
LOG.warn("There was a problem executing query '{}' Please review the query or configuration. Reason: {}", query.getQueryName(), e.getMessage());
continue;
} finally {
agentState.closeResultSet(results);
agentState.closeStmt(stmt);
agentState.closeConnection(con);
}
}
builder.withStatus(CollectionStatus.SUCCEEDED);
return builder.build();
} finally {
if (agentState != null) {
// Make sure that when we're done we close all results, statements and connections.
agentState.closeResultSet(results);
agentState.closeStmt(stmt);
agentState.closeConnection(con);
//agentState.closeAgentConnection();
}
}
}
use of org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource in project opennms by OpenNMS.
the class WsManCollector method collectGroupUsing.
private void collectGroupUsing(Group group, CollectionAgent agent, WSManClient client, int retries, CollectionSetBuilder builder) throws CollectionException {
// Determine the appropriate resource type
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
final AtomicInteger instanceId = new AtomicInteger();
Supplier<Resource> resourceSupplier = () -> nodeResource;
if (!"node".equalsIgnoreCase(group.getResourceType())) {
resourceSupplier = () -> {
// Generate a unique instance for each node in each group to ensure
// that the attributes are grouped together properly.
// Since these instances have no real meaning, a storage strategy
// similar to the SiblingColumnStorageStrategy should be used instead
// of the IndexStorageStrategy.
final String instance = String.format("%s%d", group.getName(), instanceId.getAndIncrement());
return new DeferredGenericTypeResource(nodeResource, group.getResourceType(), instance);
};
}
if (LOG.isDebugEnabled()) {
LOG.debug("Using resource {} for group named {}", resourceSupplier.get(), group.getName());
}
// Enumerate
List<Node> nodes = Lists.newLinkedList();
RetryNTimesLoop retryLoop = new RetryNTimesLoop(retries);
while (retryLoop.shouldContinue()) {
try {
if (group.getFilter() == null) {
LOG.debug("Enumerating and pulling {} on {}.", group.getResourceUri(), client);
client.enumerateAndPull(group.getResourceUri(), nodes, true);
} else {
LOG.debug("Enumerating and pulling {} with dialect {} and filter {} on {}.", group.getResourceUri(), group.getDialect(), group.getFilter(), client);
client.enumerateAndPullUsingFilter(group.getResourceUri(), group.getDialect(), group.getFilter(), nodes, true);
}
break;
} catch (WSManException e) {
retryLoop.takeException(e);
}
}
LOG.debug("Found {} nodes.", nodes.size());
// Process the results
processEnumerationResults(group, builder, resourceSupplier, nodes);
}
use of org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource 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.DeferredGenericTypeResource 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();
}
Aggregations