use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.
the class SnmpAssetProvisioningAdapter method doUpdateNode.
/**
* <p>doUpdate</p>
*
* @param nodeId a int.
* @param retry a boolean.
* @throws org.opennms.netmgt.provision.ProvisioningAdapterException if any.
*/
@Override
public void doUpdateNode(final int nodeId) throws ProvisioningAdapterException {
LOG.debug("doUpdate: updating nodeid: {}", nodeId);
final OnmsNode node = m_nodeDao.get(nodeId);
Assert.notNull(node, "doUpdate: failed to return node for given nodeId:" + nodeId);
final InetAddress ipaddress = m_template.execute(new TransactionCallback<InetAddress>() {
@Override
public InetAddress doInTransaction(final TransactionStatus arg0) {
return getIpForNode(node);
}
});
final String locationName = node.getLocation() != null ? node.getLocation().getLocationName() : null;
final SnmpAgentConfig agentConfig = m_snmpConfigDao.getAgentConfig(ipaddress, locationName);
final OnmsAssetRecord asset = node.getAssetRecord();
m_config.getReadLock().lock();
try {
for (AssetField field : m_config.getAssetFieldsForAddress(ipaddress, node.getSysObjectId())) {
try {
String value = fetchSnmpAssetString(m_locationAwareSnmpClient, agentConfig, locationName, field.getMibObjs(), field.getFormatString());
LOG.debug("doUpdate: Setting asset field \" {} \" to value: {}", value, field.getName());
// Use Spring bean-accessor classes to set the field value
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(asset);
try {
wrapper.setPropertyValue(field.getName(), value);
} catch (BeansException e) {
LOG.warn("doUpdate: Could not set property \" {} \" on asset object: {}", field.getName(), e.getMessage(), e);
}
} catch (Throwable t) {
// This exception is thrown if the SNMP operation fails or an incorrect number of
// parameters is returned by the agent or because of a misconfiguration.
LOG.warn("doUpdate: Could not set value for asset field \" {} \": {}", field.getName(), t.getMessage(), t);
}
}
} finally {
m_config.getReadLock().unlock();
}
node.setAssetRecord(asset);
m_nodeDao.saveOrUpdate(node);
m_nodeDao.flush();
}
use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.
the class SnmpHardwareInventoryProvisioningAdapter method synchronizeInventory.
/**
* Synchronize inventory.
* <p>Obtain the ENTITY-MIB and vendor attributes from the target node through SNMP.</p>
* <p>If the node has a hardware inventory data on the database, this is going to be overridden only,
* if the gathered data differs from the data at the database, otherwise the gathered data will be
* discarded.</p>
*
* @param nodeId the node id
*/
private void synchronizeInventory(int nodeId) {
final OnmsNode node = m_nodeDao.get(nodeId);
if (node == null) {
throw new ProvisioningAdapterException("Failed to return node for given nodeId: " + nodeId);
}
final OnmsIpInterface intf = node.getPrimaryInterface();
if (intf == null) {
throw new ProvisioningAdapterException("Can't find the SNMP Primary IP address for nodeId: " + nodeId);
}
final InetAddress ipAddress = intf.getIpAddress();
EventBuilder ebldr = null;
try {
if (node.getSysObjectId() == null) {
LOG.warn("Skiping hardware discover because the node {} doesn't support SNMP", nodeId);
return;
}
OnmsMonitoringLocation location = node.getLocation();
String locationName = (location == null) ? null : location.getLocationName();
SnmpAgentConfig agentConfig = m_snmpConfigDao.getAgentConfig(ipAddress, locationName);
final OnmsHwEntity newRoot = getRootEntity(agentConfig, node);
newRoot.setNode(node);
final OnmsHwEntity currentRoot = m_hwEntityDao.findRootByNodeId(node.getId());
if (newRoot.equals(currentRoot)) {
LOG.info("No changes detected on the hardware inventory for nodeId {}", nodeId);
return;
}
if (currentRoot == null) {
LOG.info("Saving hardware inventory for nodeId {}", nodeId);
} else {
LOG.info("Updating hardware inventory for nodeId {}", nodeId);
m_hwEntityDao.delete(currentRoot);
m_hwEntityDao.flush();
}
m_hwEntityDao.saveOrUpdate(newRoot);
ebldr = new EventBuilder(EventConstants.HARDWARE_INVENTORY_SUCCESSFUL_UEI, PREFIX + NAME);
} catch (Throwable e) {
ebldr = new EventBuilder(EventConstants.HARDWARE_INVENTORY_FAILED_UEI, PREFIX + NAME);
ebldr.addParam(EventConstants.PARM_REASON, e.getMessage());
}
if (ebldr != null) {
ebldr.setNodeid(nodeId);
ebldr.setInterface(ipAddress);
getEventForwarder().sendNow(ebldr.getEvent());
}
}
use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.
the class SnmpTrapHelper method getAgentConfig.
/**
* Gets the SNMP agent configuration.
*
* @param trapConfig The trap configuration mapping object
* @return the SNMP agent configuration
* @throws SnmpTrapException if any.
*/
// TODO Compare the estimated size with a maximum value:
// If the estimated is lower than the maximum in X percentage, log a warning.
// Otherwise, log an error and throw an exception
private SnmpAgentConfig getAgentConfig(SnmpTrapConfig trapConfig) throws SnmpTrapException {
SnmpAgentConfig agentConfig = trapConfig.getAgentConfig();
if (trapConfig.getVersion().intValue() != agentConfig.getVersion()) {
throw new SnmpTrapException("SNMP Version mismatch for " + trapConfig);
}
int estimatedSize = getEstimatedPacketSize(trapConfig, agentConfig);
LOG.info("Sending SNMP{} using {}. The estimated packet size is {} bytes", trapConfig.getVersion().stringValue(), trapConfig, estimatedSize);
return agentConfig;
}
use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.
the class SnmpTrapHelper method forwardV1Trap.
/**
* Create an SNMP V1 trap based on the content of the specified trap configuration, and send it to the appropriate destination.
*
* @param trapConfig The trap configuration mapping object
* @throws SnmpTrapException if any.
*/
private void forwardV1Trap(SnmpTrapConfig trapConfig) throws SnmpTrapException {
SnmpV1TrapBuilder trap = SnmpUtils.getV1TrapBuilder();
trap.setEnterprise(SnmpObjId.get(trapConfig.getEnterpriseId()));
trap.setAgentAddress(trapConfig.getHostAddress());
if (trapConfig.hasGeneric()) {
trap.setGeneric(trapConfig.getGeneric());
}
if (trapConfig.hasSpecific()) {
trap.setSpecific(trapConfig.getSpecific());
}
trap.setTimeStamp(System.currentTimeMillis() / 1000);
addParameters(trap, trapConfig);
try {
SnmpAgentConfig config = getAgentConfig(trapConfig);
trap.send(config.getAddress().getHostAddress(), config.getPort(), config.getReadCommunity());
} catch (Throwable e) {
throw new SnmpTrapException("Failed to send trap " + e.getMessage(), e);
}
}
use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.
the class SnmpTrapHelper method forwardV3Inform.
/**
* Create an SNMP V3 inform based on the content of the specified trap configuration, and send it to the appropriate destination.
*
* @param trapConfig The trap configuration mapping object
* @throws SnmpTrapException if any.
*/
private void forwardV3Inform(SnmpTrapConfig trapConfig) throws SnmpTrapException {
SnmpV3TrapBuilder trap = SnmpUtils.getV3InformBuilder();
populateTrapBuilder(trap, trapConfig);
try {
SnmpAgentConfig config = getAgentConfig(trapConfig);
trap.sendInform(config.getAddress().getHostAddress(), config.getPort(), config.getTimeout(), config.getTimeout(), config.getSecurityLevel(), config.getSecurityName(), config.getAuthPassPhrase(), config.getAuthProtocol(), config.getPrivPassPhrase(), config.getPrivProtocol());
} catch (Throwable e) {
throw new SnmpTrapException("Failed to send trap " + e.getMessage(), e);
}
}
Aggregations