Search in sources :

Example 66 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<String>();
    List<String> serviceList = new ArrayList<String>();
    // 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<String>();
    List<String> unmanageInterfacesList = new ArrayList<String>();
    List<String> manageInterfacesList = new ArrayList<String>();
    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 67 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<ManagedInterface>();
    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 68 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<String>();
    List<String> unmanageInterfacesList = new ArrayList<String>();
    List<String> manageInterfacesList = new ArrayList<String>();
    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 69 with DBUtils

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

the class AddNewInterfaceServlet method getNodeId.

private int getNodeId(String ipaddr) throws SQLException {
    int nodeId = -1;
    Connection conn = null;
    PreparedStatement stmt = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        stmt = conn.prepareStatement(SQL_INTERFACE_EXIST);
        d.watch(stmt);
        stmt.setString(1, ipaddr);
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        if (rs.next()) {
            nodeId = rs.getInt(1);
        }
        return nodeId;
    } finally {
        d.cleanUp();
    }
}
Also used : Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 70 with DBUtils

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

the class SetCriticalPathServlet method deleteCriticalPath.

private static void deleteCriticalPath(int node) throws SQLException {
    final DBUtils d = new DBUtils(SetCriticalPathServlet.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement(SQL_DELETE_CRITICAL_PATH);
        d.watch(stmt);
        stmt.setInt(1, node);
        stmt.executeUpdate();
    } finally {
        d.cleanUp();
    }
}
Also used : DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

DBUtils (org.opennms.core.utils.DBUtils)77 Connection (java.sql.Connection)68 PreparedStatement (java.sql.PreparedStatement)64 ResultSet (java.sql.ResultSet)50 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 FilterParseException (org.opennms.netmgt.filter.api.FilterParseException)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 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 InetAddress (java.net.InetAddress)3 RequestDispatcher (javax.servlet.RequestDispatcher)3