use of org.opennms.netmgt.collection.api.AttributeType in project opennms by OpenNMS.
the class HttpCollector method processResponse.
private static void processResponse(final Locale responseLocale, final String responseBodyAsString, final HttpCollectorAgent collectorAgent, final CollectionSetBuilder collectionSetBuilder) {
LOG.debug("processResponse:");
LOG.debug("responseBody = {}", responseBodyAsString);
LOG.debug("getmatches = {}", collectorAgent.getUriDef().getUrl().getMatches());
int numberOfButes = 0;
int flags = 0;
if (collectorAgent.getUriDef().getUrl().isCanonicalEquivalence()) {
flags |= Pattern.CANON_EQ;
}
if (collectorAgent.getUriDef().getUrl().isCaseInsensitive()) {
flags |= Pattern.CASE_INSENSITIVE;
}
if (collectorAgent.getUriDef().getUrl().isComments()) {
flags |= Pattern.COMMENTS;
}
if (collectorAgent.getUriDef().getUrl().isDotall()) {
flags |= Pattern.DOTALL;
}
if (collectorAgent.getUriDef().getUrl().isLiteral()) {
flags |= Pattern.LITERAL;
}
if (collectorAgent.getUriDef().getUrl().isMultiline()) {
flags |= Pattern.MULTILINE;
}
if (collectorAgent.getUriDef().getUrl().isUnicodeCase()) {
flags |= Pattern.UNICODE_CASE;
}
if (collectorAgent.getUriDef().getUrl().isUnixLines()) {
flags |= Pattern.UNIX_LINES;
}
LOG.debug("flags = {}", flags);
Pattern p = Pattern.compile(collectorAgent.getUriDef().getUrl().getMatches(), flags);
Matcher m = p.matcher(responseBodyAsString);
final boolean matches = m.matches();
if (matches) {
LOG.debug("processResponse: found matching attributes: {}", matches);
final List<Attrib> attribDefs = collectorAgent.getUriDef().getAttributes();
final List<Locale> locales = new ArrayList<Locale>();
if (responseLocale != null) {
locales.add(responseLocale);
}
locales.add(Locale.getDefault());
if (Locale.getDefault() != Locale.ENGLISH) {
locales.add(Locale.ENGLISH);
}
// All node resources for HTTP; nothing of interface or "indexed resource" type
final NodeLevelResource resource = new NodeLevelResource(collectorAgent.getAgent().getNodeId());
for (final Attrib attribDef : attribDefs) {
final AttributeType type = attribDef.getType();
String value = null;
try {
value = m.group(attribDef.getMatchGroup());
} catch (final IndexOutOfBoundsException e) {
LOG.error("IndexOutOfBoundsException thrown while trying to find regex group, your regex does not contain the following group index: {}", attribDef.getMatchGroup());
LOG.error("Regex statement: {}", collectorAgent.getUriDef().getUrl().getMatches());
continue;
}
if (type.isNumeric()) {
Number num = null;
for (final Locale locale : locales) {
try {
num = NumberFormat.getNumberInstance(locale).parse(value);
LOG.debug("processResponse: found a parsable number with locale \"{}\".", locale);
break;
} catch (final ParseException e) {
LOG.warn("attribute {} failed to match a parsable number with locale \"{}\"! Matched \"{}\" instead.", attribDef.getAlias(), locale, value);
}
}
if (num == null) {
LOG.warn("processResponse: gave up attempting to parse numeric value, skipping group {}", attribDef.getMatchGroup());
continue;
}
LOG.debug("processResponse: adding numeric attribute {}", num);
collectionSetBuilder.withNumericAttribute(resource, collectorAgent.getUriDef().getName(), attribDef.getAlias(), num, type);
numberOfButes++;
} else {
LOG.debug("processResponse: adding string attribute {}", value);
collectionSetBuilder.withStringAttribute(resource, collectorAgent.getUriDef().getName(), attribDef.getAlias(), value);
numberOfButes++;
}
}
} else {
LOG.debug("processResponse: found matching attributes: {}", matches);
}
if (numberOfButes < 1) {
LOG.warn("doCollection: no attributes defined by the response: {}", responseBodyAsString.trim());
throw new HttpCollectorException("No attributes specified were found.");
}
}
use of org.opennms.netmgt.collection.api.AttributeType 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.api.AttributeType 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.api.AttributeType 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.api.AttributeType 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();
}
Aggregations