Search in sources :

Example 61 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class CategoryModel method getInterfaceAvailability.

/**
 * Return the availability percentage for all managed services on the given
 * interface from the given start time until the given end time. If there
 * are no managed services on this interface, then a value of -1 is
 * returned.
 *
 * @param nodeId a int.
 * @param ipAddr a {@link java.lang.String} object.
 * @param start a {@link java.util.Date} object.
 * @param end a {@link java.util.Date} object.
 * @return a double.
 * @throws java.sql.SQLException if any.
 */
static double getInterfaceAvailability(int nodeId, String ipAddr, Date start, Date end) throws SQLException {
    if (ipAddr == null || start == null || end == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    if (end.before(start)) {
        throw new IllegalArgumentException("Cannot have an end time before the start time.");
    }
    if (end.equals(start)) {
        throw new IllegalArgumentException("Cannot have an end time equal to the start time.");
    }
    double avail = -1;
    final DBUtils d = new DBUtils(CategoryModel.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement("select getManagePercentAvailIntfWindow(?, ?, ?, ?) as avail");
        d.watch(stmt);
        stmt.setInt(1, nodeId);
        stmt.setString(2, ipAddr);
        // yes, these are supposed to be backwards, the end time first
        stmt.setTimestamp(3, new Timestamp(end.getTime()));
        stmt.setTimestamp(4, new Timestamp(start.getTime()));
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        if (rs.next()) {
            avail = rs.getDouble("avail");
        }
    } catch (final SQLException e) {
        LOG.warn("Failed to get interface availability for nodeId {}, interface {}", nodeId, ipAddr, e);
    } finally {
        d.cleanUp();
    }
    return avail;
}
Also used : SQLException(java.sql.SQLException) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 62 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class GetInterfacesServlet method getInterfaces.

/**
 * <p>
 * Retrieve all the interfaces and services from the database, and keep them
 * in the user session.
 *
 * @param userSession
 *            Current user working session
 * @param nodeId
 *            Id of the node to manage
 */
private List<ManagedInterface> getInterfaces(HttpSession userSession, int nodeId) throws SQLException {
    Connection connection = null;
    List<ManagedInterface> allInterfaces = new ArrayList<>();
    int lineCount = 0;
    final DBUtils d = new DBUtils(getClass());
    try {
        connection = DataSourceFactory.getInstance().getConnection();
        d.watch(connection);
        PreparedStatement ifaceStmt = connection.prepareStatement(INTERFACE_QUERY);
        d.watch(ifaceStmt);
        ifaceStmt.setInt(1, nodeId);
        ResultSet ifaceResults = ifaceStmt.executeQuery();
        d.watch(ifaceResults);
        while (ifaceResults.next()) {
            lineCount++;
            ManagedInterface newInterface = new ManagedInterface();
            newInterface.setNodeid(nodeId);
            newInterface.setAddress(ifaceResults.getString(1));
            newInterface.setStatus(ifaceResults.getString(2));
            allInterfaces.add(newInterface);
            PreparedStatement svcStmt = connection.prepareStatement(SERVICE_QUERY);
            d.watch(svcStmt);
            svcStmt.setInt(1, nodeId);
            svcStmt.setString(2, newInterface.getAddress());
            ResultSet svcResults = svcStmt.executeQuery();
            d.watch(svcResults);
            while (svcResults.next()) {
                lineCount++;
                ManagedService newService = new ManagedService();
                newService.setId(svcResults.getInt(1));
                newService.setName(svcResults.getString(2));
                newService.setStatus(svcResults.getString(3));
                newInterface.addService(newService);
            }
        }
        userSession.setAttribute("lineItems.nodemanagement", Integer.valueOf(lineCount));
    } finally {
        d.cleanUp();
    }
    return allInterfaces;
}
Also used : Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 63 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class ManageNodeServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession userSession = request.getSession(false);
    List<ManagedInterface> allNodes = getManagedInterfacesFromSession(userSession);
    // the list of all interfaces marked as managed
    String[] parameters = request.getParameterValues("interfaceCheck");
    List<String> interfaceList = (parameters == null ? Collections.<String>emptyList() : Arrays.asList(parameters));
    // the list of all services marked as managed
    parameters = request.getParameterValues("serviceCheck");
    List<String> serviceList = (parameters == null ? Collections.<String>emptyList() : Arrays.asList(parameters));
    // the list of interfaces that need to be put into the URL file
    List<String> addToURL = new ArrayList<>();
    List<String> unmanageInterfacesList = new ArrayList<>();
    List<String> manageInterfacesList = new ArrayList<>();
    Date curDate = new Date();
    final DBUtils d = new DBUtils(getClass());
    try {
        Connection connection = DataSourceFactory.getInstance().getConnection();
        d.watch(connection);
        try {
            connection.setAutoCommit(false);
            PreparedStatement stmt = connection.prepareStatement(UPDATE_SERVICE);
            d.watch(stmt);
            PreparedStatement outagesstmt = connection.prepareStatement(DELETE_SERVICE_OUTAGES);
            d.watch(outagesstmt);
            for (ManagedInterface curInterface : allNodes) {
                String intKey = curInterface.getNodeid() + "-" + curInterface.getAddress();
                // see if this interface needs added to the url list
                if (interfaceList.contains(intKey)) {
                    addToURL.add(curInterface.getAddress());
                }
                // determine what is managed and unmanaged
                if (interfaceList.contains(intKey) && curInterface.getStatus().equals("unmanaged")) {
                    // Event newEvent = new Event();
                    // newEvent.setUei("uei.opennms.org/internal/interfaceManaged");
                    // newEvent.setSource("web ui");
                    // newEvent.setNodeid(curNode.getNodeID());
                    // newEvent.setInterface(curInterface.getAddress());
                    // newEvent.setTime(curDate);
                    // updateInterface(curInterface.getNodeid(),
                    // curInterface.getAddress(), new Event(), "M");
                    manageInterfacesList.add(curInterface.getAddress());
                } else if (!interfaceList.contains(intKey) && curInterface.getStatus().equals("managed")) {
                    // Event newEvent = new Event();
                    // newEvent.setUei("uei.opennms.org/internal/interfaceUnmanaged");
                    // newEvent.setSource("web ui");
                    // newEvent.setNodeid(curNode.getNodeID());
                    // newEvent.setInterface(curInterface.getAddress());
                    // newEvent.setTime(curDate);
                    // updateInterface(curInterface.getNodeid(),
                    // curInterface.getAddress(), new Event(), "F");
                    unmanageInterfacesList.add(curInterface.getAddress());
                }
                List<ManagedService> interfaceServices = curInterface.getServices();
                for (int k = 0; k < interfaceServices.size(); k++) {
                    ManagedService curService = interfaceServices.get(k);
                    String serviceKey = intKey + "-" + curService.getId();
                    if (serviceList.contains(serviceKey) && curService.getStatus().equals("unmanaged")) {
                        // Event newEvent = new Event();
                        // newEvent.setUei("uei.opennms.org/internal/serviceManaged");
                        // newEvent.setSource("web ui");
                        // newEvent.setNodeid(curNode.getNodeID());
                        // newEvent.setInterface(curInterface.getAddress());
                        // newEvent.setService(curService.getName());
                        // newEvent.setTime(curDate);
                        stmt.setString(1, String.valueOf("A"));
                        stmt.setInt(2, curInterface.getNodeid());
                        stmt.setString(3, curInterface.getAddress());
                        stmt.setInt(4, curService.getId());
                        LOG.debug("doPost: executing manage service update for {} {}", curInterface.getAddress(), curService.getName());
                        stmt.executeUpdate();
                        EventBuilder bldr = new EventBuilder(EventConstants.RESUME_POLLING_SERVICE_EVENT_UEI, "web ui", curDate);
                        bldr.setNodeid(curInterface.getNodeid());
                        bldr.setInterface(addr(curInterface.getAddress()));
                        bldr.setService(curService.getName());
                        sendEvent(bldr.getEvent());
                    } else if (!serviceList.contains(serviceKey) && curService.getStatus().equals("managed")) {
                        stmt.setString(1, String.valueOf("F"));
                        stmt.setInt(2, curInterface.getNodeid());
                        stmt.setString(3, curInterface.getAddress());
                        stmt.setInt(4, curService.getId());
                        outagesstmt.setInt(1, curInterface.getNodeid());
                        outagesstmt.setString(2, curInterface.getAddress());
                        outagesstmt.setInt(3, curService.getId());
                        LOG.debug("doPost: executing unmanage service update for {} {}", curInterface.getAddress(), curService.getName());
                        stmt.executeUpdate();
                        outagesstmt.executeUpdate();
                        EventBuilder bldr = new EventBuilder(EventConstants.SERVICE_UNMANAGED_EVENT_UEI, "web ui", curDate);
                        bldr.setNodeid(curInterface.getNodeid());
                        bldr.setInterface(addr(curInterface.getAddress()));
                        bldr.setService(curService.getName());
                        sendEvent(bldr.getEvent());
                        bldr.setUei(EventConstants.SUSPEND_POLLING_SERVICE_EVENT_UEI);
                        sendEvent(bldr.getEvent());
                    }
                }
            // end k loop
            }
            if (manageInterfacesList.size() > 0)
                manageInterfaces(manageInterfacesList, connection);
            if (unmanageInterfacesList.size() > 0)
                unmanageInterfaces(unmanageInterfacesList, connection);
            // update the packages url file
            writeURLFile(addToURL);
            connection.commit();
        } finally {
            // close off the db connection
            connection.setAutoCommit(true);
        }
    } catch (SQLException e) {
        throw new ServletException(e);
    } finally {
        d.cleanUp();
    }
    // send the event to restart SCM
    sendSCMRestartEvent();
    // forward the request for proper display
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/manageNodesFinish.jsp");
    dispatcher.forward(request, response);
}
Also used : SQLException(java.sql.SQLException) HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date) RequestDispatcher(javax.servlet.RequestDispatcher) ServletException(javax.servlet.ServletException) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) DBUtils(org.opennms.core.utils.DBUtils)

Example 64 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class ManageNodesServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession userSession = request.getSession(false);
    List<ManagedInterface> allNodes = getManagedInterfacesFromSession(userSession);
    List<String> interfaceList = new ArrayList<>();
    List<String> serviceList = new ArrayList<>();
    // the list of all interfaces marked as managed
    if (request.getParameterValues("interfaceCheck") != null) {
        interfaceList = Arrays.asList(request.getParameterValues("interfaceCheck"));
    }
    // the list of all services marked as managed
    if (request.getParameterValues("serviceCheck") != null) {
        serviceList = Arrays.asList(request.getParameterValues("serviceCheck"));
    }
    // the list of interfaces that need to be put into the URL file
    List<String> addToURL = new ArrayList<>();
    List<String> unmanageInterfacesList = new ArrayList<>();
    List<String> manageInterfacesList = new ArrayList<>();
    final DBUtils d = new DBUtils(getClass());
    try {
        Connection connection = DataSourceFactory.getInstance().getConnection();
        d.watch(connection);
        try {
            connection.setAutoCommit(false);
            PreparedStatement stmt = connection.prepareStatement(UPDATE_SERVICE);
            d.watch(stmt);
            PreparedStatement outagesstmt = connection.prepareStatement(DELETE_SERVICE_OUTAGES);
            d.watch(outagesstmt);
            for (int j = 0; j < allNodes.size(); j++) {
                ManagedInterface curInterface = allNodes.get(j);
                String intKey = curInterface.getNodeid() + "-" + curInterface.getAddress();
                // see if this interface needs added to the url list
                if (interfaceList.contains(intKey)) {
                    addToURL.add(curInterface.getAddress());
                }
                // determine what is managed and unmanged
                if (interfaceList.contains(intKey) && curInterface.getStatus().equals("unmanaged")) {
                    // Event newEvent = new Event();
                    // newEvent.setUei("uei.opennms.org/internal/interfaceManaged");
                    // newEvent.setSource("web ui");
                    // newEvent.setNodeid(curNode.getNodeID());
                    // newEvent.setInterface(curInterface.getAddress());
                    // newEvent.setTime(curDate);
                    // updateInterface(curInterface.getNodeid(),
                    // curInterface.getAddress(), new Event(), "M");
                    manageInterfacesList.add(curInterface.getAddress());
                } else if (!interfaceList.contains(intKey) && curInterface.getStatus().equals("managed")) {
                    // Event newEvent = new Event();
                    // newEvent.setUei("uei.opennms.org/internal/interfaceUnmanaged");
                    // newEvent.setSource("web ui");
                    // newEvent.setNodeid(curNode.getNodeID());
                    // newEvent.setInterface(curInterface.getAddress());
                    // newEvent.setTime(curDate);
                    // updateInterface(curInterface.getNodeid(),
                    // curInterface.getAddress(), new Event(), "F");
                    unmanageInterfacesList.add(curInterface.getAddress());
                }
                List<ManagedService> interfaceServices = curInterface.getServices();
                for (int k = 0; k < interfaceServices.size(); k++) {
                    ManagedService curService = interfaceServices.get(k);
                    String serviceKey = intKey + "-" + curService.getId();
                    if (serviceList.contains(serviceKey) && curService.getStatus().equals("unmanaged")) {
                        // Event newEvent = new Event();
                        // newEvent.setUei("uei.opennms.org/internal/serviceManaged");
                        // newEvent.setSource("web ui");
                        // newEvent.setNodeid(curNode.getNodeID());
                        // newEvent.setInterface(curInterface.getAddress());
                        // newEvent.setService(curService.getName());
                        // newEvent.setTime(curDate);
                        stmt.setString(1, "R");
                        stmt.setInt(2, curInterface.getNodeid());
                        stmt.setString(3, curInterface.getAddress());
                        stmt.setInt(4, curService.getId());
                        this.log("DEBUG: executing manage service update for " + curInterface.getAddress() + " " + curService.getName());
                        stmt.executeUpdate();
                    } else if (!serviceList.contains(serviceKey) && curService.getStatus().equals("managed")) {
                        EventBuilder bldr = new EventBuilder(EventConstants.SERVICE_UNMANAGED_EVENT_UEI, "web ui");
                        bldr.setNodeid(curInterface.getNodeid());
                        bldr.setInterface(addr(curInterface.getAddress()));
                        bldr.setService(curService.getName());
                        sendEvent(bldr.getEvent());
                        stmt.setString(1, "S");
                        stmt.setInt(2, curInterface.getNodeid());
                        stmt.setString(3, curInterface.getAddress());
                        stmt.setInt(4, curService.getId());
                        outagesstmt.setInt(1, curInterface.getNodeid());
                        outagesstmt.setString(2, curInterface.getAddress());
                        outagesstmt.setInt(3, curService.getId());
                        this.log("DEBUG: executing unmanage service update for " + curInterface.getAddress() + " " + curService.getName());
                        stmt.executeUpdate();
                        outagesstmt.executeUpdate();
                    }
                }
            // end k loop
            }
            if (manageInterfacesList.size() > 0)
                manageInterfaces(manageInterfacesList, connection);
            if (unmanageInterfacesList.size() > 0)
                unmanageInterfaces(unmanageInterfacesList, connection);
            // update the packages url file
            writeURLFile(addToURL);
            connection.commit();
        } finally {
            // close off the db connection
            connection.setAutoCommit(true);
        }
    } catch (SQLException e) {
        throw new ServletException(e);
    } finally {
        d.cleanUp();
    }
    // send the event to restart SCM
    sendSCMRestartEvent();
    // forward the request for proper display
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/manageNodesFinish.jsp");
    dispatcher.forward(request, response);
}
Also used : SQLException(java.sql.SQLException) HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) RequestDispatcher(javax.servlet.RequestDispatcher) ServletException(javax.servlet.ServletException) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) DBUtils(org.opennms.core.utils.DBUtils)

Example 65 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class AssetModel method searchNodesWithAssets.

public static MatchingAsset[] searchNodesWithAssets() throws SQLException {
    List<MatchingAsset> list = new ArrayList<>();
    final DBUtils d = new DBUtils(AssetModel.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement("select nodeid, nodelabel from node where nodeid in (select nodeid from assets where coalesce(manufacturer,'') != '' or coalesce(vendor,'') != '' or coalesce(modelNumber,'') != '' or coalesce(serialNumber,'') != '' or coalesce(description,'') != '' or coalesce(circuitId,'') != '' or coalesce(assetNumber,'') != '' or coalesce(operatingSystem,'') != '' or coalesce(rack,'') != '' or coalesce(slot,'') != '' or coalesce(port,'') != '' or coalesce(region,'') != '' or coalesce(division,'') != '' or coalesce(department,'') != '' or coalesce(address1,'') != '' or coalesce(address2,'') != '' or coalesce(city,'') != '' or coalesce(state,'') != '' or coalesce(zip,'') != '' or coalesce(building,'') != '' or coalesce(floor,'') != '' or coalesce(room,'') != '' or coalesce(vendorPhone,'') != '' or coalesce(vendorFax,'') != '' or coalesce(dateInstalled,'') != '' or coalesce(lease,'') != '' or coalesce(leaseExpires,'') != '' or coalesce(supportPhone,'') != '' or coalesce(maintContract,'') != '' or coalesce(vendorAssetNumber,'') != '' or coalesce(maintContractExpires,'') != '' or coalesce(displayCategory,'') != '' or coalesce(notifyCategory,'') != '' or coalesce(pollerCategory,'') != '' or coalesce(thresholdCategory,'') != '' or coalesce(comment,'') != '' or coalesce(username,'') != '' or coalesce(password,'') != '' or coalesce(enable,'') != '' or coalesce(connection,'') != '' or coalesce(autoenable,'') != '' or coalesce(cpu,'') != '' or coalesce(ram,'') != '' or coalesce(storagectrl,'') != '' or coalesce(hdd1,'') != '' or coalesce(hdd2,'') != '' or coalesce(hdd3,'') != '' or coalesce(hdd4,'') != '' or coalesce(hdd5,'') != '' or coalesce(hdd6,'') != '' or coalesce(numpowersupplies,'') != '' or coalesce(inputpower,'') != '' or coalesce(additionalhardware,'') != '' or coalesce(admin,'') != '' or coalesce(snmpcommunity,'') != '' or coalesce(rackunitheight,'') != '')");
        d.watch(stmt);
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        while (rs.next()) {
            MatchingAsset asset = new MatchingAsset();
            asset.nodeId = rs.getInt("nodeID");
            asset.nodeLabel = rs.getString("nodelabel");
            asset.matchingValue = "";
            asset.columnSearched = "";
            list.add(asset);
        }
    } finally {
        d.cleanUp();
    }
    return list.toArray(new MatchingAsset[list.size()]);
}
Also used : ArrayList(java.util.ArrayList) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

DBUtils (org.opennms.core.utils.DBUtils)79 Connection (java.sql.Connection)70 PreparedStatement (java.sql.PreparedStatement)70 ResultSet (java.sql.ResultSet)51 SQLException (java.sql.SQLException)17 ArrayList (java.util.ArrayList)14 Timestamp (java.sql.Timestamp)12 Statement (java.sql.Statement)10 Filter (org.opennms.web.filter.Filter)9 ServletException (javax.servlet.ServletException)5 IfIndexFilter (org.opennms.web.event.filter.IfIndexFilter)5 InterfaceFilter (org.opennms.web.event.filter.InterfaceFilter)5 NodeFilter (org.opennms.web.event.filter.NodeFilter)5 ServiceFilter (org.opennms.web.event.filter.ServiceFilter)5 SeverityFilter (org.opennms.web.event.filter.SeverityFilter)5 Date (java.util.Date)4 FilterParseException (org.opennms.netmgt.filter.api.FilterParseException)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 InetAddress (java.net.InetAddress)3 HashMap (java.util.HashMap)3