use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.
the class KscReportsMigrator method getInterfacesToMerge.
/**
* Gets the interfaces to merge.
*
* @return the list of interfaces to merge
* @throws OnmsUpgradeException the OpenNMS upgrade exception
*/
protected List<SnmpInterface> getInterfacesToMerge() throws OnmsUpgradeException {
List<SnmpInterface> interfacesToMerge = new ArrayList<>();
Connection conn = getDbConnection();
final DBUtils db = new DBUtils(getClass());
db.watch(conn);
try {
Statement st = conn.createStatement();
db.watch(st);
String query = "SELECT n.nodeid, n.foreignsource, n.foreignid, i.snmpifdescr, i.snmpifname, i.snmpphysaddr from node n, snmpinterface i where n.nodeid = i.nodeid and i.snmpphysaddr is not null order by n.nodeid, i.snmpifdescr";
ResultSet rs = st.executeQuery(query);
db.watch(rs);
while (rs.next()) {
interfacesToMerge.add(new SnmpInterface(rs, isStoreByForeignSourceEnabled()));
}
} catch (Throwable t) {
log("Error: can't retrieve the required data from the OpenNMS Database or there were problems while processing them.\n");
String reason = t.getMessage();
if (reason == null) {
reason = "Unknown";
}
log("Reason(%s): %s\n", t.getClass().getName(), reason);
// TODO This is not elegant, but it helps.
t.printStackTrace();
} finally {
db.cleanUp();
}
return interfacesToMerge;
}
use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.
the class SnmpInterfaceRrdMigratorOnline method getInterfacesToMerge.
/**
* Gets the interfaces to merge.
*
* @return the list of interfaces to merge
* @throws OnmsUpgradeException the OpenNMS upgrade exception
*/
protected List<SnmpInterfaceUpgrade> getInterfacesToMerge() throws OnmsUpgradeException {
List<SnmpInterfaceUpgrade> interfacesToMerge = new ArrayList<>();
Connection conn = getDbConnection();
final DBUtils db = new DBUtils(getClass());
db.watch(conn);
try {
Statement st = conn.createStatement();
db.watch(st);
String query = "SELECT n.nodeid, n.foreignsource, n.foreignid, i.snmpifdescr, i.snmpifname, i.snmpphysaddr from node n, snmpinterface i where n.nodeid = i.nodeid and i.snmpphysaddr is not null order by n.nodeid, i.snmpifdescr";
ResultSet rs = st.executeQuery(query);
db.watch(rs);
int count = 0;
while (rs.next()) {
count++;
SnmpInterfaceUpgrade intf = new SnmpInterfaceUpgrade(rs, isStoreByForeignSourceEnabled());
if (intf.shouldMerge()) {
interfacesToMerge.add(intf);
}
log(" Should merge %s ? %s\n", intf, intf.shouldMerge());
}
log(" Found %s of %s SNMP interfaces that require a merge.\n", interfacesToMerge.size(), count);
} catch (Throwable t) {
log("Error: can't retrieve the required data from the OpenNMS Database or there were problems while processing them.\n");
String reason = t.getMessage();
if (reason == null) {
reason = "Unknown";
}
log("Reason(%s): %s\n", t.getClass().getName(), reason);
// TODO This is not elegant, but it helps.
t.printStackTrace();
} finally {
db.cleanUp();
}
return interfacesToMerge;
}
use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.
the class NodeLabelJDBCImpl method retrieveLabel.
/**
* This method queries the 'node' table for the value of the 'nodelabel' and
* 'nodelabelsource' fields for the node with the provided nodeID. A
* NodeLabel object is returned initialized with the retrieved values.
*
* WARNING: A properly instantiated and initialized Vault class object is
* required prior to calling this method. This method will initially only be
* called from the WEB UI.
*
* @param nodeID
* Unique identifier of the node to be updated.
* @return Object containing label and source values.
* @throws java.sql.SQLException if any.
*
* @deprecated Use a {@link NodeDao#load(Integer)} method call instead
*/
@Override
public NodeLabel retrieveLabel(final int nodeID) throws SQLException {
final Connection dbConnection = DataSourceFactory.getInstance().getConnection();
final DBUtils d = new DBUtils(NodeLabelJDBCImpl.class, dbConnection);
try {
return retrieveLabel(nodeID, dbConnection);
} finally {
d.cleanUp();
}
}
use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.
the class NodeLabelJDBCImpl method assignLabel.
/**
* This method updates the 'nodelabel' and 'nodelabelsource' fields of the
* 'node' table for the specified nodeID. A database connection is retrieved
* from the Vault.
*
* WARNING: A properly instantiated and initialized Vault class object is
* required prior to calling this method. This method will initially only be
* called from the WEB UI.
*
* @param nodeID
* Unique identifier of the node to be updated.
* @param nodeLabel
* Object containing label and source values.
* @throws java.sql.SQLException if any.
*
* @deprecated Use a {@link NodeDao#update(org.opennms.netmgt.model.OnmsNode)} method call instead
*/
@Override
public void assignLabel(final int nodeID, final NodeLabel nodeLabel) throws SQLException {
final Connection dbConnection = DataSourceFactory.getInstance().getConnection();
final DBUtils d = new DBUtils(NodeLabelJDBCImpl.class, dbConnection);
try {
assignLabel(nodeID, nodeLabel, dbConnection);
} finally {
d.cleanUp();
}
}
use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.
the class NodeLabelJDBCImpl method computeLabel.
/**
* This method determines what label should be associated with a particular
* node.
*
* Algorithm for determining a node's label is as follows: 1) If node has a
* NetBIOS name associated with it, the NetBIOS name is used as the node's
* label. 2) If no NetBIOS name available, retrieve all the 'ipinterface'
* table entries associated with the node with an 'isManaged' field value of
* 'M' 3) Find the primary interface where "primary" is defined as the
* managed interface with the smallest IP address (each IP address is
* converted to an integer value -- the IP address with the smallest integer
* value wins). 4) IF the primary interface's IP host name is known it
* becomes the node's label. ELSE IF the node's MIB-II sysName value is
* known it becomes the node's label ELSE the primary interface's IP address
* becomes the node's label.
*
* NOTE: If for some reason a node has no "managed" interfaces null is
* returned for the NodeLabel.
*
* @param nodeID
* Unique identifier of the node to be updated.
* @param dbConnection
* SQL database connection
* @return NodeLabel Object containing label and source values or null if
* node does not have a primary interface.
* @throws java.sql.SQLException if any.
*
* @deprecated Update this to use modern DAO methods instead of raw SQL
*/
private NodeLabel computeLabel(final int nodeID, final Connection dbConnection) throws SQLException {
String netbiosName = null;
PreparedStatement stmt = null;
ResultSet rs = null;
final DBUtils d = new DBUtils(NodeLabelJDBCImpl.class);
try {
stmt = dbConnection.prepareStatement(SQL_DB_RETRIEVE_NETBIOS_NAME);
d.watch(stmt);
stmt.setInt(1, nodeID);
rs = stmt.executeQuery();
d.watch(rs);
// Process result set, retrieve node's sysname
while (rs.next()) {
netbiosName = rs.getString(1);
}
if (netbiosName != null) {
// Truncate sysName if it exceeds max node label length
if (netbiosName.length() > MAX_NODE_LABEL_LENGTH) {
netbiosName = netbiosName.substring(0, MAX_NODE_LABEL_LENGTH);
}
LOG.debug("NodeLabel.computeLabel: returning NetBIOS name as nodeLabel: {}", netbiosName);
return new NodeLabelJDBCImpl(netbiosName, NodeLabelSource.NETBIOS);
}
} finally {
d.cleanUp();
}
// OK, if we get this far the node has no NetBIOS name associated with it so,
// retrieve the primary interface select method property which indicates
// the method to use for determining which interface on a multi-interface
// system is to be deemed the primary interface. The primary interface
// will then determine what the node's label is.
String method = System.getProperty(PROP_PRIMARY_INTERFACE_SELECT_METHOD);
if (method == null) {
method = DEFAULT_SELECT_METHOD;
}
if (!method.equals(SELECT_METHOD_MIN) && !method.equals(SELECT_METHOD_MAX)) {
LOG.warn("Interface selection method is '{}'. Valid values are 'min' & 'max'. Will use default value: {}", method, DEFAULT_SELECT_METHOD);
method = DEFAULT_SELECT_METHOD;
}
List<InetAddress> ipv4AddrList = new ArrayList<>();
List<String> ipHostNameList = new ArrayList<>();
// Issue SQL query to retrieve all managed interface IP addresses from 'ipinterface' table
try {
stmt = dbConnection.prepareStatement(SQL_DB_RETRIEVE_MANAGED_INTERFACES);
d.watch(stmt);
stmt.setInt(1, nodeID);
rs = stmt.executeQuery();
d.watch(rs);
// Process result set, store retrieved addresses/host names in lists
loadAddressList(rs, ipv4AddrList, ipHostNameList);
} catch (Throwable e) {
LOG.warn("Exception thrown while fetching managed interfaces: {}", e.getMessage(), e);
} finally {
d.cleanUp();
}
InetAddress primaryAddr = selectPrimaryAddress(ipv4AddrList, method);
// and select the primary interface from them.
if (primaryAddr == null) {
LOG.debug("NodeLabel.computeLabel: unable to find a primary address for node {}, returning null", nodeID);
ipv4AddrList.clear();
ipHostNameList.clear();
try {
// retrieve all non-managed interface IP addresses from 'ipinterface' table
stmt = dbConnection.prepareStatement(SQL_DB_RETRIEVE_NON_MANAGED_INTERFACES);
d.watch(stmt);
stmt.setInt(1, nodeID);
rs = stmt.executeQuery();
d.watch(rs);
loadAddressList(rs, ipv4AddrList, ipHostNameList);
} catch (Throwable e) {
LOG.warn("Exception thrown while fetching managed interfaces: {}", e.getMessage(), e);
} finally {
d.cleanUp();
}
primaryAddr = selectPrimaryAddress(ipv4AddrList, method);
}
if (primaryAddr == null) {
LOG.warn("Could not find primary interface for node {}, cannot compute nodelabel", nodeID);
return new NodeLabelJDBCImpl("Unknown", NodeLabelSource.UNKNOWN);
}
// We now know the IP address of the primary interface so
// now see if it has a IP host name
int index = ipv4AddrList.indexOf(primaryAddr);
String primaryHostName = ipHostNameList.get(index);
// If length of string is > 0 then the primary interface has a hostname
if (primaryHostName.length() != 0) {
// Truncate host name if it exceeds max node label length
if (primaryHostName.length() > MAX_NODE_LABEL_LENGTH) {
primaryHostName = primaryHostName.substring(0, MAX_NODE_LABEL_LENGTH);
}
return new NodeLabelJDBCImpl(primaryHostName, NodeLabelSource.HOSTNAME);
}
// If we get this far either the primary interface does not have
// a host name or the node does not have a primary interface...
// so we need to use the node's sysName if available...
// retrieve sysName for the node
String primarySysName = null;
try {
stmt = dbConnection.prepareStatement(SQL_DB_RETRIEVE_SYSNAME);
d.watch(stmt);
stmt.setInt(1, nodeID);
rs = stmt.executeQuery();
d.watch(rs);
while (rs.next()) {
primarySysName = rs.getString(1);
}
} finally {
d.cleanUp();
}
if (primarySysName != null && primarySysName.length() > 0) {
// Truncate sysName if it exceeds max node label length
if (primarySysName.length() > MAX_NODE_LABEL_LENGTH) {
primarySysName = primarySysName.substring(0, MAX_NODE_LABEL_LENGTH);
}
return new NodeLabelJDBCImpl(primarySysName, NodeLabelSource.SYSNAME);
}
// use the ipAddress as the nodeLabel
return new NodeLabelJDBCImpl(InetAddressUtils.str(primaryAddr), NodeLabelSource.ADDRESS);
}
Aggregations